Here's a little Python script to make it easier to deal with designs that contain changeable text - serial numbers, for example. To use it, just create text objects as normal, and type in the word auto in the Tag field for each of them. Every time you run the script, you will be prompted for new text content for each such object, with the suggested value being auto-incremented if the previous value was numeric. It will then generate toolpaths, and save g-code automatically.
The increment value is equal to the number of distinct numeric values in all of your auto fields - this handles the possibility that you might need more than one instance of each serial number in the design. Some examples:
* If you had ten fields, all initially with the value 1000, they would all increment to 1001.
* If you had five instances each of 1000 and 1001, they would become 1002 and 1003, respectively.
* If you had ten fields with individual values from 1000 to 1009, they would become 1010 thru 1019.
* If you had individual values that aren't sequential, the results aren't likely to be useful.
# autonumber.py v1.0, by jasonharper@pobox.com
def main():
view.CurrentEditMode = None
oldselect = view.SelectedEntities
fields = []
valuesSeen = set()
for layer in doc.Layers:
for ent in layer.Entities:
if ent.PrimitiveType == "Text" and "auto" in ent.Tag:
fields.append(ent)
try:
valuesSeen.add(int(ent.Text))
except ValueError:
pass
if not fields:
print 'No text objects with a tag of "auto" found.'
return
increment = len(valuesSeen)
for ent in fields:
view.Select(ent)
value = ent.Text
desc = 'Old value was "%s"' % value
try:
value = str(int(value) + increment)
desc += ', incremented by %d' % increment
except ValueError:
pass
# Comment out the next line if you want auto-increment only,
# with no prompts.
value = ThisApplication.PromptForValue("New value for text ID=%d" % ent.ID, desc, str, value)
if value is None:
print "Cancelled"
return
ent.Text = value
ent.Update()
view.Select(oldselect)
CAMUtils.GenerateToolpaths(view)
while view.CurrentEditMode is not None:
app.Sleep(1)
CAMUtils.GenerateGCodeOutput(view)
main()