From string to not a string
-
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!
-
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!
-
Lines 5,6,7,15,16 & 17 refer to array reference
v
before the reference is made. -
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!
-
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" }
-
@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)
-
@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!!!
-
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 aGeom::Point3d
as an arg, even tho the API says it can. Replacedsum += v.position
with:sum += ORIGIN.vector_to(v.position)
-
Dan Rathbun I can't thank you enough you sir are brilliant!
Advertisement