Author Topic: autonumber script  (Read 10204 times)

Offline jasonharper

  • Ewok
  • *
  • Posts: 4
    • View Profile
autonumber script
« on: March 18, 2014, 13:59:59 pm »
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.

Code: [Select]
# 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()
Have ShapeOko mill, K40 laser cutter upgraded with Smoothieboard.

Offline pixelmaker

  • CNC Jedi
  • *****
  • Posts: 1967
    • View Profile
    • pixelmaker
Re: autonumber script
« Reply #1 on: March 18, 2014, 15:14:36 pm »
hello Jason,
the script is nice.
At first I think, great it is translateable, because with installed translation plugin I get  a exclamation mark in front of the text.
But in the language file I see the problem. For every increment the script writes two new lines in the language file.
I think, if I use this script 10 times, the language file will grow up fast and Cambam will get slower and slower, especially at the start.
If I work with a translation file I can clean the language file by hand. But when using CB in english this is not possible.
 
ralf

Offline jasonharper

  • Ewok
  • *
  • Posts: 4
    • View Profile
Re: autonumber script
« Reply #2 on: March 18, 2014, 19:47:11 pm »
It doesn't look like the translation plug-in supports varying text.  To avoid this problem, you'd need to make the prompts static:
Code: [Select]
value = ThisApplication.PromptForValue("New value for text", "", str, value)
Have ShapeOko mill, K40 laser cutter upgraded with Smoothieboard.

Offline lloydsp

  • CNC Jedi
  • *****
  • Posts: 8969
    • View Profile
Re: autonumber script
« Reply #3 on: March 18, 2014, 20:25:11 pm »
Yep.

Lloyd
"Pyro for Fun and Profit for More Than Fifty Years"

Offline pixelmaker

  • CNC Jedi
  • *****
  • Posts: 1967
    • View Profile
    • pixelmaker
Re: autonumber script
« Reply #4 on: March 18, 2014, 21:01:02 pm »
hello jason,
NOW it is a very helpfull script.
The line "New value for text" is now translateable with the language file.
Thank you


@Lloyd, you are, with David, the best CamBam-Plugin-Programmer in this time. The text I don´t translate in the script, but in the translation file. Is this not also possible to do in the plugins in this way? Sorry, this is maybee a stupid question…

ralf
« Last Edit: March 18, 2014, 21:15:38 pm by pixelmaker »

Offline OTJtraining...Again

  • Ewok
  • *
  • Posts: 24
    • View Profile
Re: autonumber script
« Reply #5 on: February 14, 2020, 03:01:21 am »
This autonumber script is really useful. I'd like to take it a step further, but am not sure how.

If it could also change the name of the Out File to the same value as the 'autonumber'.nc it would be really useful for generating a series of serial number cut files.

Can anyone point out how / what commands would be needed to take this extra step?