CamBam
News:
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
February 19, 2020, 13:56:08 pm


Login with username, password and session length


Pages: [1]
  Print  
Author Topic: autonumber script  (Read 4024 times)
jasonharper
CNC Ewok
*
Offline Offline

Posts: 2


View Profile
« 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:
# 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()
Logged

Have ShapeOko mill, K40 laser cutter upgraded with Smoothieboard.
pixelmaker
CNC Jedi
*****
Offline Offline

Posts: 1724


View Profile WWW
« 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


* Bildschirmfoto 2014-03-18 um 16.31.16.png (9.28 KB, 491x129 - viewed 458 times.)

* Bildschirmfoto 2014-03-18 um 16.33.16.png (32 KB, 696x439 - viewed 548 times.)
Logged
jasonharper
CNC Ewok
*
Offline Offline

Posts: 2


View Profile
« 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:
value = ThisApplication.PromptForValue("New value for text", "", str, value)
Logged

Have ShapeOko mill, K40 laser cutter upgraded with Smoothieboard.
lloydsp
CNC Jedi
*****
Offline Offline

Posts: 8136



View Profile
« Reply #3 on: March 18, 2014, 20:25:11 pm »

Yep.

Lloyd
Logged

"Pyro for Fun and Profit for More Than Fifty Years"
pixelmaker
CNC Jedi
*****
Offline Offline

Posts: 1724


View Profile WWW
« 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


* Bildschirmfoto 2014-03-18 um 22.21.12.png (7.97 KB, 489x130 - viewed 439 times.)
« Last Edit: March 18, 2014, 21:15:38 pm by pixelmaker » Logged
OTJtraining...Again
CNC Ewok
*
Offline Offline

Posts: 14


View Profile
« 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?
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM
Page created in 0.12 seconds with 19 queries.

Copyright © 2018 HexRay Ltd. | Sitemap