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

      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