Looping through components in a script?
-
Hi,
I am trying to write a plugin to loop through the components in a model and display the unique name for each item. I've been through the "Hello World" example and reviewed other example plugins.In my example, I have a model with three components, each of which is comprised of a couple of sub-components. E.g.
A
a1
a2
B
b1
C
c1
c2
c3If I do this (omitting the requisite definition statements earlier in the code)
for i in 0..(component_definitions.length) if component_definitions[i] != NIL #UI.messagebox("Item " + i.to_s + " : " + component_definitions[i].name) end end
I'll get a list of all the component names. If I specify "entities" instead of "component_definitions", then I correctly get a list of 3 items (though ".name" is not a valid qualifier).Can someone point me to a bit of code or a resource which would show me how to print out the full name of a component (e.g. A.a1, C.c3, etc.)? My goal is to be able to loop through the model, showing only one component at a time while hiding all the rest. If this (or something similar) has already been done and I could be pointed to that, I'd be appreciative.
Finally, I'm new to this forum. I hope the above is appropriately phrased and that this is the right spot for the question. If I've made any newbie error, I apologize in advance.
Regards,
Matt -
Hi Matt and welcome.
I don't know if your post is well phrased as scripting is Greek to me but I am moving it to the "Developers" Forum where it has a much better place (and hopefully attention, too).
-
Let's assume that the component definitions are named as you suggest.
We'll skip over the separate naming of component-instances...
The quickest way is to iterate the model's definitions, and get the parent of the first instance of each one; stepping back through the 'parentage' till yo get to the model...model=Sketchup.active_model defns=model.definitions defns.each{|defn| data="Definition; " dats=[defn.name] if inst=defn.instances[0] parent=inst.parent until parent==model dats << parent.name if parent.class==Sketchup;;ComponentDefinition parent=parent.instance[0].parent end#until ### we now have an array of parents that's backwards, so we dats.reverse! ### join them with a '.' strg=dats.join(".") data=data+strg else data=data+dats[0]+" Has NO Instances." end#if puts data ### output to Ruby Console }You will get A.a1.a2 and a1.a2 this way - I leave you to resolve that step...

PS: this is 'untested' and might have typos... -
Hi TIG,
Thank you for your reply and the code snippet. As written I got a couple of errors, so I changed the middle section of what you provided to thisuntil ((parent==model) || (parent==nil)) #MattC dats << parent.name if parent.class==Sketchup::ComponentDefinition if parent.instances[0] != nil #MattC parent=parent.instances[0].parent #MattC fix typo else #MattC parent=nil #MattC end #MattC end#untiland this gets me the list hierarchy I was looking for. Dunno if I missed something else - there's certainly got to be a better way than what I wrote - but this seems to traverse the component tree.
At the risk of prolonging this thread, can you suggest where I want to add code to "select" a component? I was working on looping through entities like this
if (an_entity.typename == 'ComponentInstance') component_name = an_entity.definition.name puts(" Dealing with : " + component_name) status = our_selection.add an_entity #select it curritem = our_selection[0] if (curritem) puts(" Selected entity = " + curritem.name); else puts(" Some error") end UI.messagebox(component_name + " should be selected. ") subcomponents = curritem.explode #some recursion to to work down through the component status = our_selection.remove curritem #need to reassemble component here???? end
hopting that the "Attribute Reporter" (attrreporter.rb) plugin could serve as a guide. But I'm getting stuck at the drill-down-and-reassemble point. Your approach would seem to naturally work better. Is there a plugin out there that you know of which you could point me to as a guide?(My end goal is to loop through a model to show different views of each component, for what it's worth.)
I appreciate the help you've given on this - thanks!
Matt -
Okay, it looks like I'm getting close to what I want with the Export CI ID plugin :
http://www.scriptspot.com/sketchup/scripts/export-ci-id
Now to figure out a quick way of hiding everything not selected as I loop through.
Thanks again for the help here - I am appreciative.
Regards,
Matt
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement