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

    Creating new variables in for-loop?

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 3 Posters 103 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.
    • R Offline
      rvs1977
      last edited by

      Hi!

      Is it possible to create new variables in a for-loop?

      Now I have something like this, which works:

      
      loop_length = 3
      
      for i in 0..(loop_length-1)
        if (i == 0)
          @yv_group0 = Sketchup.active_model.entities.add_group
          @yv_face0 = @yv_group0.entities.add_face (@pts[1], @pts[2], @pts[3], @pts[4]) 
        end
      
        if (i == 1)
          @yv_group1 = Sketchup.active_model.entities.add_group
          @yv_face1 = @yv_group1.entities.add_face (@pts[5], @pts[6], @pts[7], @pts[8]) 
        end
       
        if (i == 2)
          @yv_group2 = Sketchup.active_model.entities.add_group
          @yv_face2 = @yv_group2.entities.add_face (@pts[9], @pts[10], @pts[11], @pts[12]) 
        end
      end
      
      

      I think it would be smarter if it was possible to create new variabels in the for-loop, like:

      
      for i in 0..(loop_length-1)
        
          @yv_group_i = Sketchup.active_model.entities.add_group
          @yv_face_i  = @yv_group_i.entities.add_face (@pts[1+(i*4)], @pts[2+(i*4)], @pts[3+(i*4)], @pts[4+(i*4)]) 
      end
      
      

      As you can see, I would like to try to put the i on the variables.
      Is that possible in some way?? πŸŽ‰

      -Rasmus


      Get a Ruby

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by

        Use an Array?

        Hi

        1 Reply Last reply Reply Quote 0
        • R Offline
          rvs1977
          last edited by

          yes, I thought about but Im unsure how to do it.

          
          face_array = Array.new(loop_length,Hash.new)
          
          for i in 0..(loop_length-1)
              face_array[i][@yv_group_i] = Sketchup.active_model.entities.add_group
              face_array[i][@yv_face_i]  = @yv_group_i.entities.add_face (@pts[1+(i*4)], @pts[2+(i*4)], @pts[3+(i*4)], @pts[4+(i*4)]) 
          end
          
          

          Something like that...??


          Get a Ruby

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            No.. in a similar way you that are using the @pts array.

            
            @yv_group = []
            @yv_face  = []
            for i in 0..(loop_length-1)
                @yv_group[i] = Sketchup.active_model.entities.add_group()
                @yv_face[i]  = @yv_group[i].entities.add_face (@pts[1+(i*4)], @pts[2+(i*4)], @pts[3+(i*4)], @pts[4+(i*4)])
            end
            

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • R Offline
              rvs1977
              last edited by

              Ok thank you Dan. I will try that. πŸ˜„


              Get a Ruby

              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