• Login
sketchucation logo sketchucation
  • Login
πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

How do I move a Group of entities on their own?

Scheduled Pinned Locked Moved Developers' Forum
5 Posts 2 Posters 437 Views 2 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.
  • S Offline
    SJH333
    last edited by 30 Apr 2011, 10:47

    Please excuse my ignorance, this is all new to me!
    I'm having trouble moving a Group of entities on their own; if I move both my Groups (all entities) everything is OK, but if I move entities from only one Group, SU displays a crazy result.
    I'm really not too sure that I'm understanding, the entities = model.entities
    vs entities = model.active_entities idea, is this my problem?

    Any help would be appreciated. Thanks, Steve.

    I've attached two screen shots, one OK and one very obviously NOT OK!
    Here's my Code:

    # 30/04/2011 SJH_Groups_Add_v02.rb
    puts "Running... SJH_Groups_Add"
    
      require 'sketchup.rb'
    
      model = Sketchup.active_model
      entities = model.entities
      #entities = model.active_entities
      selection = model.selection
    
      # Set some length variables
      rl = 1
      gl = 1
      bl = 1
      
      rlmid = rl*0.5  
      glmid = gl*0.5
      blmid = bl*0.5
      
      # To convert to SU current Model units
      rl = rl.to_s.to_l
      gl = gl.to_s.to_l
      bl = bl.to_s.to_l
      
      rlmid = rlmid.to_s.to_l
      glmid = glmid.to_s.to_l
      blmid = blmid.to_s.to_l
      
      
      # Create Face points array
      fpts = []
      fpts[0] = [0, 0, 0]
      fpts[1] = [rl, 0, 0]
      fpts[2] = [rl, gl, 0]
      fpts[3] = [0, gl, 0]
      
      # Create String and counter for incrementing group names
      sjhname = "SJHGroup"
      groupcount = 0
    
    # ---------- Start Main Code ----------
    
      groupcount+= 1
      groupname = sjhname + groupcount.to_s
      
      
    # Create a Group - mygroup1
      mygroup1 = []                                        # Create an empty array to prevent Bug Splat!
      mygroup1 = entities.add_group                        # Make the array a Group Entities class
      gentities1 = mygroup1.entities                       # Create an array of the Entities in mygroup
      mygroup1.name = groupname                            # Change the Group name
      face = gentities1.add_face fpts                      # Add a face to gentities1
      pullface = face.pushpull -bl                         # Do a push/pull to the face
      tcoordinates = [rlmid, glmid, blmid]                 # Set coordinates for text
      tpoint = Geom;;Point3d.new tcoordinates              # Create a 3d point for text
      mytext = gentities1.add_text "#{groupname}", tpoint  # Add a text label
      model.close_active                                   # Close mygroup1
    # Closed the Group
      
      
    # Move everything in the model
      entlen = entities.length
      transform = Geom;;Transformation.new([(rl*2),0,0])
      
      for e in (0... entlen)
      entities.transform_entities(transform, entities[e])
      end
      
    # Select mygroup1
      #selection.add(mygroup1)  
    
    
      groupcount+= 1
      groupname = sjhname + groupcount.to_s
      
      
    # Create a Group - mygroup2
      mygroup2 = []                                        # Create an empty array to prevent Bug Splat!
      mygroup2 = entities.add_group                        # Make the array a Group Entities class
      gentities2 = mygroup2.entities                       # Create an array of the Entities in mygroup
      mygroup2.name = groupname                            # Change the Group name
      face = gentities2.add_face fpts                      # Add a face to gentities2
      pullface = face.pushpull -bl                         # Do a push/pull to the face
      tcoordinates = [rlmid, glmid, blmid]                 # Set coordinates for text
      tpoint = Geom;;Point3d.new tcoordinates              # Create a 3d point for text
      mytext = gentities2.add_text "#{groupname}", tpoint  # Add a text label
      model.close_active                                   # Close mygroup2
    # Closed the Group
      
      
    # Move everything in the model
      entlen = entities.length
      transform = Geom;;Transformation.new([(rl*2),0,0])
      
      for e in (0... entlen)
      entities.transform_entities(transform, entities[e])
      end
      
    # Select mygroup
      #selection.add(mygroup2)  
    
    
    # ---------- With this Code it goes crazy ----------
    # Move gentities1 (mygroup1) ONLY
      entlen = gentities1.length
      transform = Geom;;Transformation.new([(rl*2),0,0])
      
      for e in (0... entlen)
      entities.transform_entities(transform, gentities1[e])
      end
     
    # Question; How do I move a Group on it's own (using Ruby API Code)?  
    
    # ---------- End Main Code ----------
      
    puts("\n   END \n..........")
    # end of SJH_Groups_Add.rb
    

    Capture1-Moved-OK


    Capture2-Move_NOT_OK

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 30 Apr 2011, 11:31

      Your request is a little unclear...

      You can move a group simply by applying a transformation to the group.
      This 'transforms' the group within it's context [here the model].
      You can move a group's entities [all or in part] by applying a transformation to them.
      This moves the specified group's entities within the group itself.

      You have a reference to the group - e.g. mygroup1 - so use mygroup1.transform!(tr) - assuming 'tr' is a point-transformation or a translation-transformation, then the group moves to that new location or by that vector to suit... This is the simplest way to relocate whole groups within the model or even when they are within other groups etc...

      A transformation can move to a point, move by a vector, rotate, scale etc...

      When you want to move a collection of entities [say within a group] use something like this...
      ents=mygroup1.entities ents.transform_entities(tr, ents.to_a)
      This transforms all of the group's entities by 'tr' - note how we change the 'ents' to an array [with .to_a] to construct the list that will be changed...

      TIG

      1 Reply Last reply Reply Quote 0
      • S Offline
        SJH333
        last edited by 30 Apr 2011, 12:05

        Thanks Tig.
        I've got it to work with your suggestion:

        # Move (mygroup1) ONLY  
          tr = [(rl*2),0,0]            # Set transformation
          mygroup1.transform!(tr)      # Move mygroup1 (apply transformation)
         
        

        I was making it more difficult than it needed to be! πŸ˜‰
        I didn't need to touch the Groups Entities.

        Easy, when you know how! πŸ˜„
        Thanks again.

        Attached is the 'moved' (transform!)ed Group.


        Capture3-Working as required

        1 Reply Last reply Reply Quote 0
        • T Offline
          TIG Moderator
          last edited by 30 Apr 2011, 12:15

          Try making it as a proper transformation
          pt=Geom::Point3d.new(rl*2, 0, 0) tr=Geom::Transformation.new(pt) mygroup1.transform!(tr)
          To move by a set vector use
          vt=Geom::Vector3d.new(rl*2, 0, 0) tr=Geom::Transformation.translation(vt) mygroup1.transform!(tr)
          Using an array as the transformation might give unexpected results...
          Also the Geom::Transformation model has several other useful methods like rotation and scaling.......

          TIG

          1 Reply Last reply Reply Quote 0
          • S Offline
            SJH333
            last edited by 30 Apr 2011, 15:28

            Now the Penny is dropping! Slowly... πŸ˜‰
            After a day going round in circles, I've had a productive evening...

            For absolute Newbies, here are some Group transformations (thanks yet again TIG):

            # 30/04/2011 SJH_Groups_Add_v03.rb
            puts "Running... SJH_Groups_Add"
            
              require 'sketchup.rb'
            
              model = Sketchup.active_model
              entities = model.entities
              #entities = model.active_entities
              selection = model.selection
            
              # Set some length variables
              rl = 1
              gl = 1
              bl = 1
              
              rlmid = rl*0.5  
              glmid = gl*0.5
              blmid = bl*0.5
              
              # To convert to SU current Model units
              rl = rl.to_s.to_l
              gl = gl.to_s.to_l
              bl = bl.to_s.to_l
              
              rlmid = rlmid.to_s.to_l
              glmid = glmid.to_s.to_l
              blmid = blmid.to_s.to_l
              
              
              # Create Face points array
              fpts = []
              fpts[0] = [0, 0, 0]
              fpts[1] = [rl, 0, 0]
              fpts[2] = [rl, gl, 0]
              fpts[3] = [0, gl, 0]
              
              # Create String and counter for incrementing group names
              sjhname = "SJHGroup"
              groupcount = 0
            
            # ---------- Start Main Code ----------
            
              groupcount+= 1
              groupname = sjhname + groupcount.to_s
              
              
            # Create a Group - mygroup1
              mygroup1 = []                                        # Create an empty array to prevent Bug Splat!
              mygroup1 = entities.add_group                        # Make the array a Group Entities class
              gentities1 = mygroup1.entities                       # Create an array of the Entities in mygroup
              mygroup1.name = groupname                            # Change the Group name
              face = gentities1.add_face fpts                      # Add a face to gentities1
              pullface = face.pushpull -bl                         # Do a push/pull to the face
              tcoordinates = [rlmid, glmid, blmid]                 # Set coordinates for text
              tpoint = Geom;;Point3d.new tcoordinates              # Create a 3d point for text
              mytext = gentities1.add_text "#{groupname}", tpoint  # Add a text label
              model.close_active                                   # Close mygroup1
            # Closed the Group
             
              UI.messagebox("mygroup1 added.  What's next?")       # User Message to see what happened!
              
            # Move everything in the model
              entlen = entities.length
              transform = Geom;;Transformation.new([(rl*2),0,0])
              
              for e in (0... entlen)
              entities.transform_entities(transform, entities[e])
              end
              
            # Select mygroup1
              #selection.add(mygroup1)  
            
              UI.messagebox("All entities moved.  What's next?")   # User Message to see what happened!
              
              groupcount+= 1
              groupname = sjhname + groupcount.to_s
              
              
            # Create a Group - mygroup2
              mygroup2 = []                                        # Create an empty array to prevent Bug Splat!
              mygroup2 = entities.add_group                        # Make the array a Group Entities class
              gentities2 = mygroup2.entities                       # Create an array of the Entities in mygroup
              mygroup2.name = groupname                            # Change the Group name
              face = gentities2.add_face fpts                      # Add a face to gentities2
              pullface = face.pushpull -bl                         # Do a push/pull to the face
              tcoordinates = [rlmid, glmid, blmid]                 # Set coordinates for text
              tpoint = Geom;;Point3d.new tcoordinates              # Create a 3d point for text
              mytext = gentities2.add_text "#{groupname}", tpoint  # Add a text label
              model.close_active                                   # Close mygroup2
            # Closed the Group
              
              UI.messagebox("mygroup2 added.  What's next?")       # User Message to see what happened!
              
            # Move everything in the model
              entlen = entities.length
              transform = Geom;;Transformation.new([(rl*2),0,0])
              
              for e in (0... entlen)
              entities.transform_entities(transform, entities[e])
              end
              
            # Select mygroup
              #selection.add(mygroup2)  
            
              UI.messagebox("All entities moved.  What's next?")         # User Message to see what happened!
                
            # Move (mygroup1) ONLY
            # Try making it as a proper transformation (TIG 30/04/2011)
              pt=Geom;;Point3d.new(rl*2, 0, 0)
              tr=Geom;;Transformation.new(pt)
              mygroup1.transform!(tr)
            
              UI.messagebox("mygroup1 moved by Point3d.  What's next?")  # User Message to see what happened!  
              
            # Move (mygroup2) ONLY
            # To move by a set vector use (TIG 30/04/2011)
              vt=Geom;;Vector3d.new(-rl*2, 0, 0)
              tr=Geom;;Transformation.translation(vt)
              mygroup2.transform!(tr)
            
              UI.messagebox("mygroup2 moved by Vector3d.  What's next?")  # User Message to see what happened!  
              
            # Rotate (mygroup1) along Red axis
              pt=Geom;;Point3d.new(0, 0, 0)
              vt=Geom;;Vector3d.new(rl, 0, 0)
              an=45.degrees
              tr=Geom;;Transformation.rotation(pt,vt,an)
              mygroup1.transform!(tr)
            
              UI.messagebox("mygroup1 rotated along Red axis.  What's next?")  # User Message to see what happened!  
            
            # Rotate (mygroup1) along Green axis
              pt=Geom;;Point3d.new(rl*6, 0, 0)
              vt=Geom;;Vector3d.new(0, gl, 0)
              an=45.degrees
              tr=Geom;;Transformation.rotation(pt,vt,-an)
              mygroup1.transform!(tr)
            
              UI.messagebox("mygroup1 rotated along Green axis.  What's next?")  # User Message to see what happened!  
            
            # Scale (mygroup2) uniform about origin x3
              tr=Geom;;Transformation.scaling(3)
              mygroup2.transform!(tr)
            
              UI.messagebox("mygroup2 scaled uniform from origin.  What's next?")  # User Message to see what happened!  
            
            # Scale (mygroup2) uniform about origin
              tr=Geom;;Transformation.scaling(0.3)
              mygroup2.transform!(tr)
            
              UI.messagebox("mygroup2 scaled uniform from origin.  What's next?")  # User Message to see what happened!  
            
            # Scale (mygroup2) non-uniform about origin
              tr=Geom;;Transformation.scaling(3, 2, 1.5)
              mygroup2.transform!(tr)
            
              UI.messagebox("mygroup2 scaled non-uniform from origin.  The End! ;o\)")  # User Message to see what happened!  
              
            # ---------- End Main Code ---------- 
              
            puts("\n   END \n..........")
            # end of SJH_Groups_Add.rb
            

            Attached is a pic of my moved, rotated and scaled Groups. Doesn't seem much but this morning that would have been a pic of a Bug Splat! Happy, happy! πŸ˜„


            Capture4-Rotate-Scale

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

            Advertisement