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

    Copy _ group array(newbie)

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 3 Posters 326 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.
    • jolranJ Offline
      jolran
      last edited by jolran

      Hello.

      Can someone please give me a hint how I can copy groups in an array, similar or same as in Sketchup manual modeling.
      Im stuck... And need to move on to next step in problemsolving.

      Can't find more information than just group.copy single object in the API and similar threads in here.

      How does one go about? Must be trivial for most people in here. Ive tried som "each do" iterator, stuff and * operators but no GO..

      Logically I suspect 1 edge cannot form a group? So I have 2 edges connected together. I havent found any copy methods
      for edges or entities(thats why I've grouped the edges).

      So for ex. Lets say there are 2 edges that are grouped, called = line_group at [0,0,0]

      Just like in Sketchup modeling, I want to copy line_group with 5 multiple copies 10 units(cm in my case) in X axis.

      BTW. The result of the array will be grouped and used with intersect_with method later on so I guess they wont have to be unique?

      mod = Sketchup.active_model # Open model
      ent = mod.entities # All entities in model
      sel = mod.selection # Current selection
      
      h_line1 = ent.add_line [0,0,0], [10,0,0]
      h_line2 = ent.add_line [10,0,0], [12,0,0]
      line_group1 = ent.add_group(h_line1, h_line2)
      
      tr = Geom;;Transformation.translation([0,25,0])
      group_copy = line_group1.copy
      number_of_copies = 10
      
      number_of_copies.times do
      # Move
      group_copy.transform!(tr)
      # next
      group_copy = group_copy.copy
      end
      
      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by

        You need to move the copied Group which is done using a Transformation.

        Something like this:

        Calculate a Vector3d which represents the direction and length between objects.

        ` tr = Geom::Transformation.translation(...)
        group_copy = your_group.copy
        number_of_copies = 5

        number_of_copies.times do

        Move

        group_copy.transform!(tr)

        next

        group_copy = group_copy.copy
        end`

        Hi

        1 Reply Last reply Reply Quote 0
        • TIGT Offline
          TIG Moderator
          last edited by

          Jim beat me to it but since I had already typed it... here it is...

          <span class="syntaxdefault"></span><span class="syntaxcomment">### assume we gave the 'group' already placed at [0,0,0]<br /></span><span class="syntaxdefault">vector</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Vector3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">10.cm</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 0</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">gp</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">group<br />groups</span><span class="syntaxkeyword">=[</span><span class="syntaxdefault">gp</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault">5.times</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">i</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault">  tgp</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">gp</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">copy<br />  tgp</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transform</span><span class="syntaxkeyword">!(</span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Transformation</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">translation</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">vector</span><span class="syntaxkeyword">))<br /></span><span class="syntaxdefault">  gp</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">tgp<br />  groups </span><span class="syntaxkeyword"><<</span><span class="syntaxdefault"> gp<br /></span><span class="syntaxkeyword">}<br /></span><span class="syntaxcomment">### we now have the 5 copies at 10cm in X...<br />### and a 6 element array containing references to all groups &nbsp;</span><span class="syntaxdefault"></span>
          

          TIG

          1 Reply Last reply Reply Quote 0
          • jolranJ Offline
            jolran
            last edited by

            Its very nice I got 2 different versions of the method. Helps me understand. I think I get the logic in the methods, but I havent configured my group correctly to adapt to your code's. I get = nil.

            h_line1 = ent.add_line [0,0,0], [250,0,0]
            h_line2 = ent.add_line [250,0,0], [255,0,0]
            line_group1 = ent.add_group(h_line1, h_line2)
            

            Is my group. Do the center of the new group have to be on [0,0,0] ?

            so for JIM's code I added:

            tr = Geom;;Transformation.translation [25,0,0]
            group_copy = your_group.copy
            

            There is no need to call for ent.transform_entities tr, ?

            In TIG's code I just added: gp=line_group1

            Thanks a lot Guys! Im happy, at least I now have something to go on.

            1 Reply Last reply Reply Quote 0
            • jolranJ Offline
              jolran
              last edited by

              Working code is at top. What it does?

              Makes 2 edges and group them.
              Copies the group 10 times 25 units apart in Y axis.

              Thanks Jim and Tig.

              1 Reply Last reply Reply Quote 0
              • TIGT Offline
                TIG Moderator
                last edited by

                Add the group and THEN add objects to its entities - it's much easier AND safer =Bugsplats are common making a group from entities - if you cross the context-barrier out of active_entities...

                group=model.active_entities.add_group() ###==new empty group
                group.entities.add_line([0,0,0],[250.cm,0,0]
                group.entities.add_line([250.cm,0,0],[255.cm,0,0])
                

                I assume you want cm and NOT inches!

                Now use my code to repeat the group to the right 5 times - I think that this won't make much sense because you are drawing a horizontal line 250cm long and the another from the end of it 5cm long... BUT then repeating the copy at 10cm centers horizontally, so they'll overlap weirdly??
                I think you need to draw this out manually to see what happens - perhaps you want the lines up in the Y ?... or the copies in the Y ???

                The methods works but your dims are to pot... 😕

                TIG

                1 Reply Last reply Reply Quote 0
                • jolranJ Offline
                  jolran
                  last edited by

                  Tig, I will try what you say. Your version is a little harder to understand, but I need to get it to work I think for my next step that has to do with group.boundingbox and positions etc.

                  @unknownuser said:

                  Bugsplats are common making a group from entities

                  Yes, I imagine that. I got a little carried away when something actually does work.
                  And the plugin I'm trying to do (I'm in over my head) will need a lot of copies.

                  I'm sorry about the dimension thing. 10 cm was not important. Just an example. I needed the concept to work
                  regardless of the dimensions. The submitted code actually copies in Y direction now

                  1 Reply Last reply Reply Quote 0
                  • TIGT Offline
                    TIG Moderator
                    last edited by

                    There are some 'typos'...
                    The first three lines do nothing for the rest as 'mod' is NOT 'model'...
                    Note - do not put spaces between methods and their parenthesized () arguments...
                    Also it's 25.cm NOT 25cm............
                    Here's a fixed version

                    <span class="syntaxdefault">&nbsp;&nbsp;&nbsp;&nbsp;model</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />&nbsp;&nbsp;&nbsp;&nbsp;ents</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities<br />&nbsp;&nbsp;&nbsp;&nbsp;group</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_group</span><span class="syntaxkeyword">()&nbsp;</span><span class="syntaxcomment">###==new&nbsp;empty&nbsp;group<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_line</span><span class="syntaxkeyword">([</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">],[</span><span class="syntaxdefault">25.cm</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">])&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_line</span><span class="syntaxkeyword">([</span><span class="syntaxdefault">25.cm</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">],[</span><span class="syntaxdefault">30.cm</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">])<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">vector</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Vector3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">10.cm</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">)<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">gp</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">group<br />&nbsp;&nbsp;&nbsp;&nbsp;groups</span><span class="syntaxkeyword">=[</span><span class="syntaxdefault">gp</span><span class="syntaxkeyword">]<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">5.times</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">i</span><span class="syntaxkeyword">|<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">tgp</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">gp</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">copy<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tgp</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transform</span><span class="syntaxkeyword">!(</span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Transformation</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">translation</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">vector</span><span class="syntaxkeyword">))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">gp</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">tgp<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups&nbsp;</span><span class="syntaxkeyword"><<&nbsp;</span><span class="syntaxdefault">gp<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">}&nbsp;</span><span class="syntaxdefault"></span>
                    

                    TIG

                    1 Reply Last reply Reply Quote 0
                    • jolranJ Offline
                      jolran
                      last edited by

                      It's not working for me properly 😳 I get nil..
                      Probably did not understand you fully. Does the declaration on first 3 lines cause a conflict somehow?
                      I'm using Alex ruby code editor.

                      I like the idea of using vector though and avoiding bugsplats 😄

                      mod = Sketchup.active_model # Open model
                      ent = mod.entities # All entities in model
                      sel = mod.selection # Current selection
                      
                      group = model.active_entities.add_group() ###==new empty group
                      group.entities.add_line ([0,0,0],[25.cm,0,0])  
                      group.entities.add_line ([25.cm,0,0],[30.cm,0,0])
                      
                      ### assume we gave the 'group' already placed at [0,0,0]
                      vector=Geom;;Vector.new(0,10.cm, 0)
                      gp=group
                      groups=[gp]
                      5.times{|i|
                        tgp=gp.copy
                        tgp.transform!(Geom;;Transformation.translation(vector))
                        gp=tgp
                        groups << gp
                      }
                      ### we now have the 5 copies at 10cm in X...
                      ### and a 6 element array containing references to all groups
                      
                      1 Reply Last reply Reply Quote 0
                      • jolranJ Offline
                        jolran
                        last edited by

                        AAHH! Yes! Now it works.

                        I spotted the . 25cm mistake. But did not think about the spaces.

                        Thank you very much TIG and Jim!!

                        I've learned a lot from this. Now I need to learn what has really happend in the code 😄

                        And acctually I only needed 1 edge in the group. I did not know one could add the entities in an empty group.
                        This is a lot better.

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

                        Advertisement