Hi,
I am trying to number all my parts of an assembly with the script below. It does it’s job, but I would like to start the numbering from 1 for each object name. How do I add this functionality?
"""25-03-2015"""
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
def AddPartNo():
msg="Select objects for part numbering"
objs=rs.GetObjects(msg,16,preselect=True)
if not objs: return
section=rs.GetString("Section number")
if section==None: return
rs.EnableRedraw(False)
for i, obj in enumerate(objs):
name=rs.ObjectName(obj)
name_upper=name.upper()
if name:
rs.SetUserText(obj, "Section", section)
rs.SetUserText(obj, "PartNo", section + "-" + name_upper + "-" + str(i+1))
sc.doc.Views.Redraw()
AddPartNo()
Something like this I guess, I didn’t have time to check if it works…
"""25-03-2015"""
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
def AddPartNo():
msg="Select objects for part numbering"
objs=rs.GetObjects(msg,16,preselect=True)
if not objs: return
section=rs.GetString("Section number")
if section==None: return
rs.EnableRedraw(False)
#create a set of unique object names
name_set=set()
for obj in objs:
name=rs.ObjectName(obj)
if name: name_set.add(name)
#iterate through the set and find all objects with each name
if len(name_set)>0:
for name in name_set:
named_objs=rs.ObjectsByName(name)
#iterate through objects with same name and sequentially number
for i,named_obj in enumerate(named_objs):
rs.SetUserText(obj, "Section", section)
rs.SetUserText(obj, "PartNo", section + "-" + name.upper() + "-" + str(i+1))
sc.doc.Views.Redraw()
AddPartNo()
–Mitch
Thanks Mitch!
The script seems to only give the first or the last object the partno.
My debugging goes not further than to check if the set is working with print name_set and that seems to work.
any thoughts?
Dunno, seems to be working here… ??
3partnames.3dm (1.7 MB)
"""25-03-2015"""
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
def AddPartNo():
msg="Select objects for part numbering"
objs=rs.GetObjects(msg,16,preselect=True)
if not objs: return
section=rs.GetString("Section number")
if section==None: return
rs.EnableRedraw(False)
#create a set of unique object names
name_set=set()
for obj in objs:
name=rs.ObjectName(obj)
if name: name_set.add(name)
#iterate through the set and find all objects with each name
if len(name_set)>0:
for name in name_set:
named_objs=rs.ObjectsByName(name)
#iterate through objects with same name and sequentially number
for i,named_obj in enumerate(named_objs):
rs.SetUserText(obj, "Section", section)
value=section + "-" + name.upper() + "-" + str(i+1)
print "Object name: {}({}) value={}".format(name,i+1,value)
rs.SetUserText(obj, "PartNo", value)
print "User text 'Section': {}".format(rs.GetUserText(obj,"Section"))
print "User text 'PartNo': {}".format(rs.GetUserText(obj,"PartNo"))
print ""
sc.doc.Views.Redraw()
AddPartNo()
When printing the results the all seem to be there, but when you select an various object all keys are empty.
It is direct visible true the datamanager. Only one item is provided by a key. Every run a random object is provide by a key.
UserDataManager.rhp (64 KB)
There is definitely something wrong here. If I set user text via the script with SetUserText(), and save and close the file, I can retrieve the same usertext also via a similar script with GetUserText()… However, I have to run the script TWICE to get the thing to save the values in the file. So there is a bug somewhere - and I’m not sure it’s in the script.
I run the “set” script once to set the names, then save and close it; I then re-open the file and run the “check” script.
The first time I do this the changes I made previously are not there - previously stored names are (if any, otherwise blank). If I run the “set” script again, save, close and reopen, then run the check script again, the changes “stick”…
However running the normal GetUserText command in Rhino still produces nothing. So I don’t know where the bug lies.
Nothing wrong here except for the hole in my brain…
–Mitch
Check script:
import rhinoscriptsyntax as rs
def CheckPartNo():
msg="Select objects to check"
objs=rs.GetObjects(msg,16,preselect=True)
if not objs: return
name_set=set()
for obj in objs:
name=rs.ObjectName(obj)
if name: name_set.add(name)
if len(name_set)>0:
for name in name_set:
named_objs=rs.ObjectsByName(name)
for i,named_obj in enumerate(named_objs):
print "Object name: {}({})".format(name,i+1)
print "User text 'Section': {}".format(rs.GetUserText(obj,"Section"))
print "User text 'PartNo': {}".format(rs.GetUserText(obj,"PartNo"))
print ""
CheckPartNo()
Okay that’s very strange… I tested the very first script again, with the continues numbering and that works every time.
I don’t know what’s happening either…
haha, got it…
#iterate through objects with same name and sequentially number
for i,named_obj in enumerate(named_objs):
rs.SetUserText(obj, "Section", section)
rs.SetUserText(obj, "PartNo", section + "-" + name.upper() + "-" + str(i+1))
“obj” should be “named_obj”