Getting global coordinates
-
Hi,
I woul like to write a script to get the midpoint global coordinate from the line current selected.
The task is store this global coordinates (x,y,z) inside variables, and retrive them as reference point to insert objects or draw lines from them.
I search on Sketchup ruby API Documentation but I didn't found.
Someone knows how can I do this?
Thanks!
Sergio Bizello -
An Edge has two Vertices, or you can use its Start and End Vertices.
You can then find the Positions of these (==Points).
You use the Point Offset method on a Point towards the other Point,
by half the Distance between these two Points
That is the mid-point you want...edge=ss[0] ### say, or however you set/get the 'edge' point0=edge.start.position point1=edge.end.position vector=point1-point0 distance=point0.distance(point1) halfway=distance/2 point_mid=point0.offset(vector,halfway)
I've made it with perhaps more variables and steps than you need, for clarity...
To store these use...
model.set_attribute("MyLines",edge.to_s,point_mid.to_a)
To recover these use
### assuming 'edge' is selected somehow... mid_edge=model.get_attribute("MyLines",edge.to_s,[]) if mid_edge[2]###=z ### do stuff with this point [x,y,z] else puts edge.to_s+" mid-point not available." end#if
-
Thank you TIG,
But I'm begginer on Sketchup API and Ruby programming, so I would like to know how can I apply this code on a selected line.
I will explained better, my task has two parts:- I select a line, part of a wall, and store this line to do the second part;
- To detect and store the midpoint global coordinate to use as reference point (You already did this, very grateful again).
So, if you tell me if there is a good book about Ruby and Sketchup API programming I woul be very been thankful.
I 've seen a lot of sites on internet, all of them very good but I like books a lot and would like to have one.
Grateful
Sérgio Bizello
-
To get the line info let's assume you first select it and then run the tool [the other way - when you run the tool and then select the line is more complicated!]
Here is a def to do what you want - I have tried to explain what each bit does...
def getlinemidpoint() model=Sketchup.active_model ss=model.selection if ss[0] and ss[0].class== Sketchup;;Edge edge= ss[0] else UI.messagebox("You Must Select an Edge.") return nil end#if point0= edge.start.position point1= edge.end.position vector= point1 - point0 distance= point0.distance(point1) halfway= distance/2 ### could also use; halfway= edge.length / 2 point_mid= point0.offset(vector,halfway) UI.messagebox("The Selected Edge Mid-Point is ["+point_mid.x.to_s+", "+point_mid.y.to_s+", "+point_mid.z.to_s+"].") ### now do what you want with this info... e.g. model.set_attribute("MyLines", edge.to_s, point_mid.to_a) end#def
-
TIG, thanks a lot!
The script worked very well. So, I will include it inside the main script.
I wont forget to speak folds its aid.
Really very thanks
Sérgio
Advertisement