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_operationThanks!
-
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_operationIf you think the code can improve please let me know!

-
Lines 5,6,7,15,16 & 17 refer to array reference
vbefore 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_operationI 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::Point3das an arg, even tho the API says it can. Replacedsum += v.positionwith:sum += ORIGIN.vector_to(v.position)
-
Dan Rathbun I can't thank you enough you sir are brilliant!
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better ๐
Register LoginAdvertisement