Name of component
-
Hi i am struggling with what i am sure is something really easy but after hours of search i can't work out how to get the name of the active component as a string. I can set the name in Component attributes.
This is what I've tried
model = Sketchup.active_model
selection = model.selection.name
but its no where nearCheers in advance
Fred
-
model = Sketchup.active_model name = model.selection[0].name
BUT of course that assumes that the first object in the model's selection has a 'name'. A ComponentInstance or a Group might have a name [BUT it might also be ""], AND many other things like a Face or Edge never will have a name, so you'll then get an error...
If [as I suspect] you want the name of the component-definition of the first item [component-instance] in the current selection... AND you know it is definitely a ComponentInstance then you can usename = model.selection[0].definition.name
which will return the unique string that is that component-definition's name.
To stop errors you really ought to test for the selection's suitability, 'type' thus...model = Sketchup.active_model sel=model.selection name=nil if sel and sel[0] and sel[0].is_a?(Sketchup.ComponentInstance) name=sel[0].definition.name end
This way name is either 'nil' or a string, so the next step in your code could be
if name ### do something with 'name' else UI.messagebox("Please select a Component Instance to find its Name !") return nil end
-
Nice one that has worked well cheers for the help
fred
-
Note that there is component instance name and component name. Component instance name is the same as a group's name.
Advertisement