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

From string to not a string

Scheduled Pinned Locked Moved Developers' Forum
9 Posts 2 Posters 2.1k 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.
  • R Offline
    renderiza
    last edited by renderiza 19 Oct 2013, 00:00

    Hi,

    I have this problem where I have a string that I want to use as a line of code(not sure what to call it).

    Here is my example...

    sumx = "v[0].position.x + v[1].position.x + v[2].position.x"

    I want sumx to solve that math equation...Is this possible

    
    model = Sketchup.active_model
    ents = model.active_entities
    sel = model.selection
    faces = sel.grep(Sketchup;;Face)
    @addx = ["v[0].position.x"]
    @addy = ["v[0].position.y"]
    @addz = ["v[0].position.z"]
    
    model.start_operation('csplit')
    
      faces.each do |face|
        v = face.vertices
        num = v.length
        i = 1
        
        while i < num  do
          @addx << " + v[#{i}].position.x"
          @addy << " + v[#{i}].position.y"
          @addz << " + v[#{i}].position.z"
          i += 1
        end
        
        ax = @addx * ","
        sumx = ax.delete ","
        
        ay = @addy * ","
        sumy = ay.delete ","
        
        az = @addz * ","
        sumz = az.delete ","
    
        cx = sumx # v[0].position.x + v[1].position.x + v[2].position.x ... 
        cy = sumy # v[0].position.y + v[1].position.y + v[2].position.y ...
        cz = sumz # v[0].position.z + v[1].position.z + v[2].position.z ...
        
        px = cx / num
        py = cy / num
        pz = cz / num
    
        v.each do |vertex|
          p1 = [px, py, pz]
          p2 = vertex
          line = ents.add_line p1,p2
        end
       
      end
      sel.clear
    model.commit_operation
    

    Thanks!

    [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

    1 Reply Last reply Reply Quote 0
    • R Offline
      renderiza
      last edited by 19 Oct 2013, 00:16

      Yay! ๐Ÿ˜

      Here is updated code...

      model = Sketchup.active_model
      ents = model.active_entities
      sel = model.selection
      faces = sel.grep(Sketchup;;Face)
      @addx = ["v[0].position.x"]
      @addy = ["v[0].position.y"]
      @addz = ["v[0].position.z"]
      
      model.start_operation('csplit')
      
        faces.each do |face|
          @addx.clear
          @addy.clear
          @addz.clear
          @addx = ["v[0].position.x"]
          @addy = ["v[0].position.y"]
          @addz = ["v[0].position.z"]
       
          v = face.vertices
          num = v.length
          i = 1
         
          while i < num  do
            @addx << " + v[#{i}].position.x"
            @addy << " + v[#{i}].position.y"
            @addz << " + v[#{i}].position.z"
            i += 1
          end
         
          ax = @addx * ","
          sumx = ax.delete ","
         
          ay = @addy * ","
          sumy = ay.delete ","
         
          az = @addz * ","
          sumz = az.delete ","
         
          cx = eval("#{sumx}") # v[0].position.x + v[1].position.x + v[2].position.x
          cy = eval("#{sumy}") # v[0].position.y + v[1].position.y + v[2].position.y
          cz = eval("#{sumz}") # v[0].position.z + v[1].position.z + v[2].position.z
         
          px = cx / num
          py = cy / num
          pz = cz / num
      
          v.each do |vertex|
            p1 = [px, py, pz]
            p2 = vertex
            line = ents.add_line p1,p2
          end
      
        end
        sel.clear
      model.commit_operation
      

      If you think the code can improve please let me know! ๐Ÿ‘

      [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by 19 Oct 2013, 01:14

        Lines 5,6,7,15,16 & 17 refer to array reference v before the reference is made.

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • R Offline
          renderiza
          last edited by 19 Oct 2013, 01:21

          Will this cause problem even if v on lines 5,6,7,15,16 & 17 is part of a string and not a variable?

          Thanks!

          [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by 19 Oct 2013, 01:23

            When you ned to have an index var,... replace:

                v = face.vertices
                num = v.length
                i = 1
               
                while i < num  do
                  @addx << " + v[#{i}].position.x"
                  @addy << " + v[#{i}].position.y"
                  @addz << " + v[#{i}].position.z"
                  i += 1
                end
            

            ... with this (remember that module Enumerable is mixed into class Array ) :

                v = face.vertices
                v.each_with_index {|vert,i|
                  @addx << " + v[#{i}].position.x"
                  @addy << " + v[#{i}].position.y"
                  @addz << " + v[#{i}].position.z"
                }
            

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • Dan RathbunD Offline
              Dan Rathbun
              last edited by 19 Oct 2013, 01:25

              @renderiza said:

              Will this cause problem even if v on lines 5,6,7,15,16 & 17 is part of a string and not a variable?

              NO.. you are correct. They are just a string char at that point.

              So my mistake. (you got me) ๐Ÿ˜ฎ

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • R Offline
                renderiza
                last edited by 19 Oct 2013, 01:36

                @dan rathbun said:

                NO.. you are correct. They are just a string char at that point.

                So my mistake. (you got me) ๐Ÿ˜ฎ

                My life is complete!!! ๐Ÿคฃ

                @dan rathbun said:

                ... with this (remember that module Enumerable is mixed into class Array ) :

                    v = face.vertices
                >     v.each_with_index {|vert,i|
                >       @addx << " + v[#{i}].position.x"
                >       @addy << " + v[#{i}].position.y"
                >       @addz << " + v[#{i}].position.z"
                >     }
                

                That is great! With your suggestion I now don't need to start with an array that has a value!

                Here is updated code...

                model = Sketchup.active_model
                ents = model.active_entities
                sel = model.selection
                faces = sel.grep(Sketchup;;Face)
                @addx = []
                @addy = []
                @addz = []
                
                model.start_operation('csplit')
                
                  faces.each do |face|
                    @addx.clear
                    @addy.clear
                    @addz.clear
                
                    v = face.vertices
                    num = v.length
                  
                    v.each_with_index {|vert,i|
                      @addx << " + v[#{i}].position.x"
                      @addy << " + v[#{i}].position.y"
                      @addz << " + v[#{i}].position.z"
                    }
                   
                    ax = @addx * ","
                    sumx = ax.delete ","
                   
                    ay = @addy * ","
                    sumy = ay.delete ","
                   
                    az = @addz * ","
                    sumz = az.delete ","
                   
                    cx = eval("#{sumx}")
                    cy = eval("#{sumy}")
                    cz = eval("#{sumz}")
                   
                    px = cx / num
                    py = cy / num
                    pz = cz / num
                
                    v.each do |vertex|
                      p1 = [px, py, pz]
                      p2 = vertex
                      line = ents.add_line p1,p2
                    end
                
                  end
                  sel.clear
                model.commit_operation
                

                I appreciate your help as always Dan!!! ๐Ÿ’š

                [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by 19 Oct 2013, 02:30

                  String manipulation is quite slow in Ruby.

                  How about doing it with no strings?

                    def csplit()
                      #
                      model = Sketchup.active_model
                      ents = model.active_entities
                      sel = model.selection
                      faces = sel.grep(Sketchup;;Face)
                      #
                      model.start_operation('csplit')
                        #
                        faces.each do |face|
                          #
                          sum = Geom;;Point3d.new(0,0,0)
                          #
                          verts = face.vertices
                          n = verts.size.to_f
                          # use API Geom;;Point3d.+ instance method;
                          verts.each {|v| sum += ORIGIN.vector_to(v.position) }
                          #
                          avg = Geom;;Point3d.new( sum.x/n, sum.y/n, sum.z/n )
                          #
                          verts.each {|v| ents.add_line( avg, v ) }
                          #
                        end # each face
                        #
                      model.commit_operation
                      #
                    rescue => e
                      #
                      puts "Error <#{e.class.name}; #{e.message} >"
                      puts e.backtrace if $VERBOSE
                      #
                      model.abort_operation
                      #
                    else # when no errors;
                      #
                      sel.clear
                      #
                    end # csplit()
                  

                  FIXED: (line 17) Geom::Point3d#+ cannot take a Geom::Point3d as an arg, even tho the API says it can. Replaced sum += v.position with: sum += ORIGIN.vector_to(v.position)

                  ๐Ÿ’ญ

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • R Offline
                    renderiza
                    last edited by 19 Oct 2013, 03:41

                    Dan Rathbun I can't thank you enough you sir are brilliant!

                    [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

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

                    Advertisement