• Login
sketchucation logo sketchucation
  • Login
ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

Looping through components in a script?

Scheduled Pinned Locked Moved Developers' Forum
5 Posts 3 Posters 439 Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    mconsidine
    last edited by 3 Nov 2010, 18:29

    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
    c3

    If 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

    1 Reply Last reply Reply Quote 0
    • G Offline
      Gaieus
      last edited by 4 Nov 2010, 10:31

      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).

      Gai...

      1 Reply Last reply Reply Quote 0
      • T Offline
        TIG Moderator
        last edited by 4 Nov 2010, 10:52

        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...

        TIG

        1 Reply Last reply Reply Quote 0
        • M Offline
          mconsidine
          last edited by 7 Nov 2010, 22:39

          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 this

          until ((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#until

          and 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

          1 Reply Last reply Reply Quote 0
          • M Offline
            mconsidine
            last edited by 7 Nov 2010, 23:19

            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

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            1 / 1
            • First post
              1/5
              Last post
            Buy SketchPlus
            Buy SUbD
            Buy WrapR
            Buy eBook
            Buy Modelur
            Buy Vertex Tools
            Buy SketchCuisine
            Buy FormFonts

            Advertisement