sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🫛 Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    Problem with component-definition in a loop

    Scheduled Pinned Locked Moved Developers' Forum
    2 Posts 2 Posters 210 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.
    • artmusicstudioA Offline
      artmusicstudio
      last edited by artmusicstudio

      hello ruby friends,
      i took 2 weeks off to study some programminmg guides etc. for ruby. i could solve some variable-problems, but i cannot find a solution for the following:

      i want to define some master-components, which are copied later in the routine as instances.
      this works well, when i define one component after each other (linear).
      this all happens in one routine like this:

      def_programm

      define compo 1
      define compo 2
      define compo 3

      some other tasks

      copy compo 1 to instance .....
      copy compo 2 to instance .....
      copy compo 3 to instance .....

      end

      BUT

      when i try to make the component definition within a LOOP and define the compnent'S name as ARRAY,
      the components are again created, ok, but the creation of INSTANCES report, that they cannot find a deleted master component.

      so it seems, that the components made within a loop are not visible for the rest of the program.

      i am not sure, if i could explain my problem. in one other topics i was said, i should post the parts of programm code in some special way: could someone tell me, how to do it?

      until then, here is the loop for the component - creation:

       # edit 131107
      require 'sketchup.rb'
      
      Sketchup.send_action "showRubyPanel:"
      
      #Add a menu item to launch our plugin.
      UI.menu("PlugIns").add_item("01-ZF"){
      
      # Call
      programm
      }
      
      def_programm
      
      some code
      
      #******************************************************
        #DEF PFOSTEN MASTER COMPONENTS AT 0,0,0 AS LOOP
           
                  
      model = Sketchup.active_model
      entities = model.active_entities 
                    
               
               for pfosten in 0..2
               pfostenname=[]
               pfostenname[0] = "$masterpfostenl"
               pfostenname[1] = "$masterpfostenr"
               pfostenname[2] = "$mittelpfostenm"
                      
               pfm1 = [0, 0, 0+thickness]
               pfm2 = [0, 0, 0+thickness+sl-((5/faktor)/100)]
               pfm3 = [0, 0, 0+thickness+sl-((5/faktor)/100)]
               pfm4 = [0, 0, 0+thickness+sl]        
                       
               group = entities.add_group
               group.name = pfostenname[pfosten]
               entities2 = group.entities
               
               unless pradius<0
               
               #puts "tube-pole"
               
               new_line = entities2.add_line pfm1, pfm2
                            length = new_line.length
                            centerpoint = pfm1
      # Create a circle perpendicular to the normal or Z axis
                            vector = pfm2
                            vector = vector.normalize!
                             
                            edges = entities2.add_circle centerpoint, vector, pradius
                            kreis = entities2.add_face edges
                            kreis.pushpull length
                 
                 
               new_line2 = entities2.add_line pfm3, pfm4
                            length2 = new_line2.length 
                            centerpoint2 = pfm4
               	      vector2 = pfm3
        	              vector2 = vector.normalize!
        			                     
        		      edges2 = entities2.add_circle centerpoint2, vector2, pradius/3
        		      kreis2 = entities2.add_face edges2
                            kreis2.pushpull -length2            
                 
        else  
        
                           #puts "square-pole"
                           side = pradius.abs
                           #puts pradius
        
                           new_line = entities2.add_line pfm1, pfm2
                           length = new_line.length
                           centerpoint = pfm1
                           vector = pfm2
                           vector = vector.normalize!
                           pts = []
                           pts[0] = [-side/2, -side/2, 0+thickness]
                           pts[1] = [-side/2, +side/2, 0+thickness]
                           pts[2] = [+side/2, +side/2, 0+thickness]
                           pts[3] = [+side/2, -side/2, 0+thickness]
                           square = entities2.add_face pts
                           square.pushpull -length
        
        
                           new_line2 = entities2.add_line pfm3, pfm4
                           length2 = new_line2.length 
                           centerpoint2 = pfm4
                           vector2 = pfm3
                           vector2 = vector.normalize!
        
                           edges2 = entities2.add_circle centerpoint2, vector2, pradius/3
                           kreis2 = entities2.add_face edges2
                           kreis2.pushpull -length2              
        
        end    
        
        puts "creating component in loop"
        puts pfostenname[pfosten]
        puts "Gruppenname " , group.name
        pfostenname[pfosten] = group.to_component  
      
      # BUT THIS COMPONENT IS NOT REACHABLE FOR THE REST OF THE PROGRAMM
          
      end    # end of loop
      
      
      #here the code for copying of components (which works, when the compnents are created oe after each oother within programm
      
      #********************************************************************************************       
      # PFOSTEN SETZEN
      
      #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
      if $putswitch == 1
      puts "positioning posts"  
      end
      #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++    
       
      
          pf1 =  [x1+rol-$lwidthplus*(step+multistep-2),  y1-run+ro.abs+multistep*run, z-rise] #links
          pf3 =  [x2-ror+$rwidthplus*(step+multistep-2),  y1-run+ro.abs+multistep*run, z-rise] #rechts
          pf5 =  [0, y1-run+ro.abs+multistep*run, z-rise] #mitte
          
          pf1p =  [x1+rol-$lwidthplus*(step+multistep-1),  y1-run+ro.abs+multistep*run+run, z] #links
          pf3p =  [x2-ror+$rwidthplus*(step+multistep-1),  y1-run+ro.abs+multistep*run+run, z] #rechts
          pf5p =  [0, y1+ro.abs+multistep*run, z] #mitte
              
               
              if (step) / il == podestnr #posledni horni
             
                      stepcount = 0 
                      
              	componentinstance =
      		entities.add_instance($masterpfostenl.definition, pf1p)
      		componentinstance =
      		entities.add_instance($masterpfostenr.definition, pf3p)
      		componentinstance =
          		entities.add_instance($mittelpfostenm.definition, pf5p)
          		 		
          		elsif   pmod == 1 # na kazdy schod
                      
          			componentinstance =
          			entities.add_instance($masterpfostenl.definition, pf1)
          			componentinstance =
          			entities.add_instance($masterpfostenr.definition, pf3)
          			componentinstance =
          			entities.add_instance($mittelpfostenm.definition, pf5)	
          			
          			
          			if (step+1) / il == podestnr
          			componentinstance =
      			entities.add_instance($masterpfostenl.definition, pf1p)
      			componentinstance =
      			entities.add_instance($masterpfostenr.definition, pf3p)
      			componentinstance =
          		        entities.add_instance($mittelpfostenm.definition, pf5p)
          			end
          			
          			
                      elsif
                              
                              #pmodoffset =   2*((il / pmod).ceil+1)*pmod-il
                              
          			(step + (((il / pmod).ceil+1)*pmod-il)*(podestnr-1)) % pmod == 1      
                              # verteiler ab modulo 2
          			componentinstance =
          			entities.add_instance($masterpfostenl.definition, pf1)
          			componentinstance =
          			entities.add_instance($masterpfostenr.definition, pf3)
          			componentinstance =
          			entities.add_instance($mittelpfostenm.definition, pf5)
               end
      
      end     # end of PROGRAMM
      
       
      

      edit 131107, thanx to dan

      THANK YOU VERY MUCH FOR HELPING.
      stan

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

        See the BBcode help page

        You need to use the [ code ] ... [/code ] bbCodes around your code example and it will look like this:
        [code:11ffgczd]

        Some example code;

        a = [1,2,3]

        last = a.pop

        puts last[/code:11ffgczd]

        I'm not here much anymore.

        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