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.
    • 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
                        • thomthomT Offline
                          thomthom
                          last edited by

                          Those five methods there has cut down many lines in my older codes. 😄

                          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

                            Hi Guy, Finally got around to working on my program, and thanks to you it works. I have one question. How do I understand the following bit of code?

                            comp_new = comp_entities.select do |e| 
                              e.is_a?(Sketchup;;Drawingelement)
                            end
                            

                            Sorry for the long-hand, but it helps me to read my code, perhaps when I get better:-}

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

                              your variable name is a bit confusing. "comp_entities" is the resulting array from .explode ? Assuming it is:

                              "comp_new" sounds like it's a ComponentInstance or ComponentDefinition, but in this case it will be a collection of entities.

                              Example of .select: arr1.select will return a new array will all the elements of arr1 when the block returns true.

                              In your case, you have an array returned from .explode. That array contains lots of thing you don't want. But the common denominator for the items you do want is that they inherit from the DrawingElement class. So we use the .select method to extract only the items that inherits from that class e.is_a?(Sketchup::Drawingelement).

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

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

                                Yes.

                                When you see in the API manual, for the Face class for example: http://code.google.com/intl/nb/apis/sketchup/docs/ourdoc/face.html
                                Notice it says: Parent: Drawingelement

                                And the Drawingelement class says Parent: Entity

                                And the Loop class: Parent: Entity

                                So a Face
                                .is_a?(Sketchup::Face) == true
                                .is_a?(Sketchup::Drawingelement) == true
                                .is_a?(Sketchup::Entity) == true

                                But a Loop
                                .is_a?(Sketchup::Loop) == true
                                .is_a?(Sketchup::Drawingelement) == false <- Notice
                                .is_a?(Sketchup::Entity) == true

                                Because the relationship of a Face is:
                                Sketchup::Face < Sketchup::Drawingelement < Sketchup::Entity < Object

                                While a Loop is:
                                Sketchup::Loop < Sketchup::Entity < Object

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

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

                                  The Object Diagram is a nice graphical overview of the classes relationships: http://code.google.com/intl/nb/apis/sketchup/docs/diagram.html

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

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

                                    @honoluludesktop said:

                                    Yes, "comp_entities" is the exploded array. OK, so when I see is_a? in this manner, the do loop is "select(ing) the entity(s)" in the array that is a (are) %(#FF0000)Sketchup::Drawingelement. Is it faster (better) then:

                                    comp_entities.select do |e| 
                                    >   if e.is_a? Sketchup;;Drawingelement
                                    >     comp_new.push e
                                    >   end
                                    > end
                                    

                                    In your examples, what you mean to do is:

                                    comp_entities.each do |e| 
                                      if e.is_a? Sketchup;;Drawingelement
                                        comp_new.push e
                                      end
                                    end
                                    

                                    Notice the .each instead of .select

                                    .each is a simple iterator - it does not return any values.
                                    .select returns a new array - where the content depends on when the block yields true.

                                    
                                    comp_new = []
                                    comp_entities.each do |e|
                                      if e.is_a? Sketchup;;Drawingelement
                                        comp_new.push e
                                      end
                                    end
                                    
                                    

                                    Does the exact same thing as:

                                    
                                    comp_new = comp_entities.select do |e|
                                      e.is_a?(Sketchup;;Drawingelement)
                                    end
                                    
                                    

                                    Can also we written as one line:

                                    
                                    comp_new = comp_entities.select { |e| e.is_a?(Sketchup;;Drawingelement) }
                                    
                                    

                                    There are many ways to do things - but Ruby has many useful methods for repeated tasks that saves your from typing the same code structure over and over again.
                                    In these examples - I'm not sure if anyone is any significantly faster than the other. But .select saves some characters.

                                    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

                                      Opps, I meant comp_entities.each. Thanks for the lesson.

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

                                        Yes, "comp_entities" is the exploded array. OK, so when I see is_a? in this manner, the do loop is "select(ing) the entity(s)" in the array that is a (are) %(#FF0000)Sketchup::Drawingelement. Is it faster (better) then:

                                        comp_entities.each do |e| 
                                          if e.is_a? Sketchup;;Drawingelement
                                            comp_new.push e
                                          end
                                        end
                                        

                                        Addenda: Corrected as noted below.

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

                                        Advertisement