sketchucation logo sketchucation
    • Login
    โ„น๏ธ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Help with Components

    Scheduled Pinned Locked Moved Developers' Forum
    30 Posts 3 Posters 3.1k 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.
    • Chris FullmerC Offline
      Chris Fullmer
      last edited by

      #1 - you don't add entities to the instance, you add them to the definition. And the definition is always located where its origin is aligned with the real world origin.

      #2 - you do something like:

      my_group = entities.add_group(entities_array) my_comp = my_group.to_component

      I don't have the API handy or SketchUp, but I think those are the exact method names. Can I ask where you got the entities array from? You might find that you actually have an array of the definition entities, not the actual entities inside your instance. But you might have more luck capturing the array of entities when you explode the component instance. This is very handy:

      exploded_array = my_comp_instance.explode

      Exploding a group or instance returns an array of the entities (plus other stuff so you have to filter out the non entities (such as loop objects)).

      #3 - ? You want to turn that comp back into an instance of the original? Easiest would be to just insert another instance of the definition. So you would want to capture the transformation of the instance before exploding it. So when you go to insert a new one, you have the exact transformation you want to insert it with.

      Hope that gets you pointed in the right direction.

      Chris

      Lately you've been tan, suspicious for the winter.
      All my Plugins I've written

      1 Reply Last reply Reply Quote 0
      • honoluludesktopH Offline
        honoluludesktop
        last edited by

        Hi Chris, Thanks for the direction. I did not think that I would have to make a group first. Actually, I don't remember the first way I created an array of entities, but eventually found ComponentInstance.explode. I think I made a blank array, then copied the selected component's entities to it. Filter out?, hmm.., OK. Thanks again, I will run with what you have provided, and see if I can finish my program.

        1 Reply Last reply Reply Quote 0
        • honoluludesktopH Offline
          honoluludesktop
          last edited by

          OK, I found, filtered out "EdgeUse", "Loop", and attempted my_group = entities.add_group(comp_ok_entities), and got Error: #<ArgumentError: All Entities must have a common parent>. How do I do that?

          1 Reply Last reply Reply Quote 0
          • Chris FullmerC Offline
            Chris Fullmer
            last edited by

            That is interesting. It sounds like somehow your array of ents still has something in there that SU does not want. Specifically that error is one that would be given if you tried to add a face and edges from the main model space, plus a face and edges nested inside a component already to a new group.

            So when you explode the component, are there sub components in it? If so, it sounds like perhaps exploding it is returning the edge and face objectID's of nested geometry. When you really can't use that. You need it to return only edge and face objectID's of geometry in the uppermost level and componentinstancesID's and groupID's from the same level.

            I'd test it out myself, but I'm not on a SU machine right now ๐Ÿ˜ž Good luck. You might need to get your array differently I suppose?

            Chris

            Lately you've been tan, suspicious for the winter.
            All my Plugins I've written

            1 Reply Last reply Reply Quote 0
            • honoluludesktopH Offline
              honoluludesktop
              last edited by

              Chris, you are right about the sub-component. Actually it is a critical part of the parent component, and I can't lose the attributes assigned to it. Let me try a few things :-{

              1 Reply Last reply Reply Quote 0
              • Chris FullmerC Offline
                Chris Fullmer
                last edited by

                Try iterating through all the entities that are returned in the "explosion". And make an array of all entities that are groups, comp_instances, and edges and faces. And you will need to check the parent of each of these to verify it is the same as the current level of editing. I think you should be able to tell if you are iterating over a face that is not grouped vs. one that is actually a part of an instance. Then if a face or edge or group or whatever does have a different parent than the current level, then you would obviously not want to add it to your final array of entities. So this would effectively weed out all entities that are being listed, even though they are inside an sub-component.

                See if you can get that code up and running,

                Chris

                Lately you've been tan, suspicious for the winter.
                All my Plugins I've written

                1 Reply Last reply Reply Quote 0
                • honoluludesktopH Offline
                  honoluludesktop
                  last edited by

                  Chris, I have filtered out everything, and visually checked the parent of each entity. All have the model as their parent, but the error still persist.

                  The entities takes the form of #Sketchup::Face:0xe4d85c8, etc. A previous error messages made reference to requiring the form as Sketchup::Entity, and I wondered if I had to edit out "<#", and ">" from the variable. I tried to use v1.chop, but that failed, and the compiler reported that it was a "private" method. In any case, do I need to edit the form the array takes?

                  1 Reply Last reply Reply Quote 0
                  • thomthomT Offline
                    thomthom
                    last edited by

                    @honoluludesktop said:

                    Chris, I have filtered out everything, and visually checked the parent of each entity. All have the model as their parent, but the error still persist.

                    The entities takes the form of #Sketchup::Face:0xe4d85c8, etc. A previous error messages made reference to requiring the form as Sketchup::Entity, and I wondered if I had to edit out "<#", and ">" from the variable. I tried to use v1.chop, but that failed, and the compiler reported that it was a "private" method. In any case, do I need to edit the form the array takes?

                    No - not need to edit out <# and >. This is just what the string Entity.to_s or Entity.inspect method outputs. It's automatically called when you puts entity to the console.

                    What would help is if you posted more of the script so we can see the how it's working. Much easier than guessing.

                    Thomas Thomassen โ€” SketchUp Monkey & Coding addict
                    List of my plugins and link to the CookieWare fund

                    1 Reply Last reply Reply Quote 0
                    • honoluludesktopH Offline
                      honoluludesktop
                      last edited by

                      I basically tried it two ways:

                      if comp.is_a? Sketchup;;ComponentInstance 
                        if @sel == "yes"
                          comp.definition.entities.each do |subcomp|
                          ###if entity is a sub componant
                          if subcomp.is_a? Sketchup;;ComponentInstance 
                            comp_entities = []
                            comp_entities = comp.definition.entities
                            #do stuff with entities here
                      				
                            my_group = entities.add_group(comp_entities)
                            #my_comp = my_group.to_component
                            #my_comp.name= my_comp_name
                          end
                        end
                      

                      ๐Ÿ™‚
                      and

                      if comp.is_a? Sketchup;;ComponentInstance 
                        if @sel == "yes"
                          comp.definition.entities.each do |subcomp|
                            ###if entity is a sub componant
                            if subcomp.is_a? Sketchup;;ComponentInstance
                              comp_entities = comp.explode
                              #do stuff here
                      				
                               comp_entities.each do |filter|
                               if filter.to_s.split(';0x').first != "#<Sketchup;;Loop" and filter.to_s.split(';0x').first != "#<Sketchup;;EdgeUse" and filter.to_s.split(';0x').first != "#<Sketchup;;AttributeDictionary" and filter.to_s.split(';0x').first != "#<Sketchup;;AttributeDictionaries"
                                  comp_ok_entities.push filter
                      	  end
                      	end
                      				
                      	my_group = entities.add_group(comp_ok_entities)
                      	#my_comp = my_group.to_component
                      	#my_comp.name= my_comp_name
                           end
                          end	
                      
                      1 Reply Last reply Reply Quote 0
                      • thomthomT Offline
                        thomthom
                        last edited by

                        Instead of adding the entities to a new group and converting it back to a component, make the component unique. http://code.google.com/apis/sketchup/docs/ourdoc/componentinstance.html#make_unique
                        (note, the API says it return true/ false, but it really returns a new ComponentInstance.)

                        
                            if comp.is_a? Sketchup;;ComponentInstance
                              if @sel == "yes"
                                comp.definition.entities.each do |subcomp|
                                ###if entity is a sub componant
                                if subcomp.is_a? Sketchup;;ComponentInstance
                                  my_comp = subcomp.make_unique
                                  comp_entities = my_comp.definition.entities
                                  #do stuff with entities here
                                end
                              end
                        

                        Thomas Thomassen โ€” SketchUp Monkey & Coding addict
                        List of my plugins and link to the CookieWare fund

                        1 Reply Last reply Reply Quote 0
                        • honoluludesktopH Offline
                          honoluludesktop
                          last edited by

                          Understood, however my problem came about because I was having difficulty understanding transformations of individual ComponentInstance.definition.entities, and was only able to do it after exploding the component. Which brought me back to returning it into a component form. I assume that I can not return to the original definition (some component entities have been relocated), so I was trying to create a new component.

                          When I do the following with a component selection that has no sub-components:

                          comp_entities = []
                          selection.each do |g|
                            if g.is_a? Sketchup;;ComponentInstance
                              comp_entities = g.definition.entities
                              #do stuff here
                          
                              my_group = entities.add_group(comp_entities)
                            end
                          end
                          

                          I get (eval):10:in โ€˜add_groupโ€™: wrong argument type (expected Sketchup::Entity). When I add after #do stuff here

                          comp_entities.each do |f|
                            puts f.parent
                          end
                          

                          Puts echo's #Sketchup::ComponentDefinition:0xe4ba1a8 for each entity to the display, demonstrating that the entities have the same parent. So what is a "wrong argument type" for my_group = entities.add_group(comp_entities)?

                          1 Reply Last reply Reply Quote 0
                          • thomthomT Offline
                            thomthom
                            last edited by

                            Are you doing anything to the comp_entities variable?

                            if you add right before your add_group

                            adebug = comp_entities.to_a.select { |e| !e.is_a?(Sketchup;;Entity) }
                            adebug.inspect
                            

                            What does it output? That snippet should return an array of everything in that collection which isn't inherited from Sketchup::Entity.

                            Thomas Thomassen โ€” SketchUp Monkey & Coding addict
                            List of my plugins and link to the CookieWare fund

                            1 Reply Last reply Reply Quote 0
                            • honoluludesktopH Offline
                              honoluludesktop
                              last edited by

                              At this time, I am not doing anything, just trying to group the array of entities. Once I figure how to do that, I will place my procedure (rotates one of the entities) in the if-end "block" (is that the right word?).

                              1 Reply Last reply Reply Quote 0
                              • Chris FullmerC Offline
                                Chris Fullmer
                                last edited by

                                In your snippet there, the variable comp_entities is pointing to an entities object. That is not what .add_group is expecting. That is what the error is. .add_group is expecting a single entitiy OR an array of entities. So just change the loine to this and see if it works:

                                my_group = entities.add_group(comp_entities.to_a)

                                I just added a .to_a to turn the entities object into an actual aray. Now it should work, I think.

                                Chris

                                Lately you've been tan, suspicious for the winter.
                                All my Plugins I've written

                                1 Reply Last reply Reply Quote 0
                                • Chris FullmerC Offline
                                  Chris Fullmer
                                  last edited by

                                  ok, and I just got onto a machine with SU on it. I tested what happens if I explode a group and then try to add the entities returned in the explosion to a group. And I got the error about adding entities with the same parent. So far so good.

                                  I found that exploding the component instance does not return the inner geometry of each sub-component like I thought it might be doing. So there really is no need to test the parent of each entity. It is just a matter of making an array of only the parts you want.

                                  So I exploded the component instance, and I then made an array of just faces, edges, groups and componentinstances. And then added that array to a new group and it worked just fine. Here is what that code looks like:

                                  model = Sketchup.active_model
                                  ents = model.active_entities
                                  explode_array = model.selection[0].explode
                                  good_ents = []
                                  
                                  explode_array.each do |e|
                                  good_ents << e if e.is_a? Sketchup;;Face
                                  good_ents << e if e.is_a? Sketchup;;Edge
                                  good_ents << e if e.is_a? Sketchup;;Group
                                  good_ents << e if e.is_a? Sketchup;;ComponentInstance
                                  end
                                  
                                  ents.add_group( good_ents )
                                  
                                  

                                  And that works from the web console with a single component Instance selected, and it can have sub components and sub groups (but not sub-images since I didn't test for those.

                                  Anyhow, I hope it helps. But in the end I think you might be better off approaching this entirely differently.

                                  Chris

                                  Lately you've been tan, suspicious for the winter.
                                  All my Plugins I've written

                                  1 Reply Last reply Reply Quote 0
                                  • thomthomT Offline
                                    thomthom
                                    last edited by

                                    If you are rotating (or tranforming) everything inside a componentinstance, apply your tranformation to all the entities by using Entities.transform_entities http://code.google.com/apis/sketchup/docs/ourdoc/entities.html#transform_entities

                                    Chris, that snippet can be made easier if you check for inheritance from the Drawingelements instead of individual classes.

                                    
                                    model = Sketchup.active_model
                                    ents = model.active_entities
                                    explode_array = model.selection[0].explode
                                    good_ents = explode_array.select { |e| e.is_a?(Sketchup;;Drawingelement) }
                                    ents.add_group( good_ents )
                                    
                                    

                                    Thomas Thomassen โ€” SketchUp Monkey & Coding addict
                                    List of my plugins and link to the CookieWare fund

                                    1 Reply Last reply Reply Quote 0
                                    • honoluludesktopH Offline
                                      honoluludesktop
                                      last edited by

                                      Thanks, Guys. Not able to get back to this tonight, will try tomorrow night.

                                      1 Reply Last reply Reply Quote 0
                                      • Chris FullmerC Offline
                                        Chris Fullmer
                                        last edited by

                                        I like that Thom. I've also seen you use "select" a few times recently. I'll try to look at it, but is it different than collect? I recently ran across that one, and I never rememeber to use it. But they both appear to do about the same thing, and are remarkably useful for populating an array.

                                        Chris

                                        Lately you've been tan, suspicious for the winter.
                                        All my Plugins I've written

                                        1 Reply Last reply Reply Quote 0
                                        • thomthomT Offline
                                          thomthom
                                          last edited by

                                          @chris fullmer said:

                                          I like that Thom. I've also seen you use "select" a few times recently. I'll try to look at it, but is it different than collect? I recently ran across that one, and I never rememeber to use it. But they both appear to do about the same thing, and are remarkably useful for populating an array.

                                          Chris

                                          They are not the same.

                                          Enumerable.collect (alias Enumerable.map)

                                          arr = [0, 1, 2, 3, 4] arr.collect { |i| i * 2 }
                                          returns: [0, 2, 4, 6, 8]
                                          I use it for instance to collect Point3ds from vertices collections.
                                          points = face.vertices.collect { |vertex| vertex.position }

                                          Enumerable.select (alias Enumerable.find_all)
                                          arr = [0, 1, 2, 3, 4] arr.select { |i| i%2 > 0 } # block returns true if i is an odd number
                                          returns: [1, 3]

                                          I recently noticed this:

                                          Enumerable.partition
                                          arr = [0, 1, 2, 3, 4] arr.partition { |i| i%2 > 0 } # block returns true if i is and odd number
                                          returns: [[1, 3], [0, 2, 4]]

                                          Also rather handy.

                                          The Enumerable module, which the Array class includes, has a number of very nice methods which I've found do many of the things I often do with arrays. I just haven't noticed them, partly because not everything in the Enumerable module isn't listed in the Array class docs.

                                          model.selection.all? { |e| e.is_a?(Sketchup::Edge) }

                                          or

                                          [ruby:af7phbud]model.selection.any? { |e| e.is_a?(Sketchup::Edge) }[/ruby:af7phbud]

                                          Thomas Thomassen โ€” SketchUp Monkey & Coding addict
                                          List of my plugins and link to the CookieWare fund

                                          1 Reply Last reply Reply Quote 0
                                          • Chris FullmerC Offline
                                            Chris Fullmer
                                            last edited by

                                            Wow those are great Thom. I see the difference now there with collect and select. And I really like that partition method. Not sure where I'd use it right now, but I have a feeling it might come in handy eventually.

                                            Thanks!

                                            Chris

                                            Lately you've been tan, suspicious for the winter.
                                            All my Plugins I've written

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

                                            Advertisement