• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

Basic question - groups as array ?

Scheduled Pinned Locked Moved Developers' Forum
5 Posts 3 Posters 269 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.
  • A Offline
    artmusicstudio
    last edited by 8 Nov 2013, 11:20

    hi,
    is it possible to create and define GROUPS as ARRAY?

    example: #create needed groups for later element assignment

    
    $groupname[0] = Sketchup.active_model.entities.add_group
    $groupname[0].entities.add_line([0,-50,0],[0,-50,140])   #element [0] will be later deleted
    $groupname[0] = "$groupname[0]"
    
    $groupname[1] = Sketchup.active_model.entities.add_group
    $groupname[1].entities.add_line([0,-50,0],[0,-50,140])   #element [0] will be later deleted
    $groupname[1] = "$groupname[1]"
    
    .....
    
    
    

    after this, one could assign elements to those groups within a loop using a
    the loop-step as the arry-index

    thanx stan

    1 Reply Last reply Reply Quote 0
    • D Offline
      Dan Rathbun
      last edited by 8 Nov 2013, 13:17

      (1) Creating groups for later use requires at least 1 entity in them, so we usually add a cpoint into them at ORIGIN (the predefined global constant which is a Geom::Point3d == [0,0,0].)

      (2) Ruby does NOT have variables. It has 2 things. Objecs and references to those objects.

      You can create as many references to a single object as you wish, depending upon your needs.

      a = []

      a is a reference to a Ruby instance of class Array.

      A Ruby array is a non-unique collection of references to other objects.

      (3) So YES you CAN use arrays to hold references to ANY kind of object you wish.

      (4) You need to stop using global references, and use module, class, instance or local references, within your own namespace module:

      module AMS
        module SomePlugin
          @@groups = [] unless defined?(@@groups)
          # other code
        end # SomePlugin
      end # AMS
      

      💭

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • T Offline
        tt_su
        last edited by 8 Nov 2013, 14:27

        Mind describing what you are trying to achieve on a more abstract level?

        1 Reply Last reply Reply Quote 0
        • D Offline
          Dan Rathbun
          last edited by 8 Nov 2013, 16:01

          @artmusicstudio said:

          ... after this, one could assign elements to those groups within a loop using a the loop-step as the array-index

          You CAN use an index, but it is not needed:

          
          @@groups.each {|g|
            g.entities.add_line([0,-50,0],[0,-50,140]
          }
          

          IF you WANT to use an index:

          
          0.step( @@groups.size-1, 2 ) {|i|
            ents = @@groups[i].entities
            ents.add_line([0,-50,0],[0,-50,140]
          }
          

          But you can also use the each_with_index method, (because the Enumerable module is mixed into the Array class:

          
          @@groups.each_with_index {|g,i|
            if i.remainder(2).zero? # even indices only
              ents = g.entities
              ents.add_line([0,-50,0],[0,-50,140]
            end # only even
          }
          

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • A Offline
            artmusicstudio
            last edited by 9 Nov 2013, 09:51

            hi, thank you all, i tried to put my groups in the suggested ways into a loop, but i always get errors. with '@@groups' i would reach all grups, wouldn't i ?
            i will try to describe the whole problem:

            i have a defined amount of groups with one basic construction point (thanx dan), the amount changes by user input, so it must be variable, so there may be 2 or 20 @group_handlauf_mitte - groups.

            @group_handlauf_mitte1
            @group_handlauf_mitte2
            @group_handlauf_mitte3
            @group_handlauf_mitte4
            and so on

            i cannot find out, how can i access those 'group_handlauf_mitte ' groups in a loop to be able to select a group by
            @group_handlauf_mitte[index].entities. (my index here ist PODESTNR)

            i also tried to create the group name as a string-combination within a loop

            
            for step 1..20
            a = "group" + step.tp_s.name 
            a.name =  a
            end
            
            

            but that's obviously not working , too.

            at the moment i have to define like this:

            
                                                    if @podestnr == 1
            					entities = @group_handlauf_mitte1.entities
            					new_line = entities.add_line rl13, rl14
            					entities = @group_handlauf_mitte1.entities
            
            					elsif @podestnr == 2
            					entities = @group_handlauf_mitte2.entities
            					new_line = entities.add_line rl13, rl14
            					entities = @group_handlauf_mitte2.entities      				
            
            					elsif @podestnr == 3
            					entities = @group_handlauf_mitte3.entities
            					new_line = entities.add_line rl13, rl14
            					entities = @group_handlauf_mitte3.entities  
                				
                   					elsif @podestnr == 4
            					entities = @group_handlauf_mitte4.entities
            					new_line = entities.add_line rl13, rl14
            					entities = @group_handlauf_mitte4.entities 
            					
                   					end
            
            

            instead of this i would like to make a loop in which STEP decides, which grop's entities will become ENTITIES, but only for those groups, not all groups i have.

            hopefully i can once understand the syntax for group selection by index, because i could then make the ruby-code many times shorter.

            in this loop i need the griup name 1,2,3,4 to become dependent from the index MTH

            
            model = Sketchup.active_model
                        
             
             y1 = 0 - @run ## the overall variables come from earlier code !!!
             y2 = 0
             y3 = @run
             z = @rise
             pf = []
             for mth in 1..10
             
                        entities = @group_handlauf_mitte1.entities ### MTH instead of 1 needed
                        entities.erase_entities entities[0]
                         
                        rl1 = [0, y1, 0]
                        rl2 = [0, y2, z]
                         
            group = @group_handlauf_mitte1 = entities.add_group      ### MTH instead of 1 needed
                        entities2 = @group_handlauf_mitte1.entities  ### MTH instead of 1 needed
                        new_line = entities2.add_line rl1, rl2
                        length = new_line.length
             
             pf[mth] =  [0,  0 - @run+@run*@il*(mth-1)+@run*@la*(mth-1)+@ro.abs, (mth-1)*@il*@rise+@sr] #mitte
                         
                       #point1 = Geom;;Point3d.new pf51
                       #constpoint = entities.add_cpoint point1
                       
                       centerpoint = pf[mth]
                       new_line = entities2.add_line rl1, rl2                              
                       vector = new_line.line[1]
                       vector2 = vector.normalize!
                       
                       edges = entities2.add_circle centerpoint, vector2, @thradius
                       kreis = entities2.add_face edges
                       entity1 = entities[0]
               	   kurvem = entity1.all_connected
               	   
             	   status1 = kreis.followme kurvem
              
             end
            
            
            

            thanx for helping
            stan

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

            Advertisement