How to get the absolute coordinates of faces?
-
I want to get the absolute center coordinates of faces. But I find the coordinates are affected by the edit mode. Two images in the attachment is an illustration. The first image shows model in edit mode, while the second does not. My goal is to get the absolute coordinate of the center of the red face. However, in the first image, the center is Point3d(-9.0624, -22.0221, 63.6455); in the second image, the center is Point3d(-0.178652, -0.258835, 1.39058).
The first coordnate is what I wanted. But this required me to use mouse to clik the model several times. Could anyeone tell me if there are some methods to get the absolute coordinate without mouse clik?
-
I assume you have somehow got the center of the face inside its 'context' - let's call it
pt
.
You have a reference to the face's 'container' [a group or instance].
You can get the transformation of that - e.g.tr=instance.transformation
.
You can apply a transformation to a point thus:
pt.transform!(tr)
You can also do the inverse - e.g. you have a point outside of the context and want to convert it as if it's inside the context...
pt.transform!(tr.inverse)
I'm unsure about which way round you want to do this, but somewhere in this is your answer...
-
Here's what I've got.
def self.currentPos(parent, child_pos) #Get the position of the point relative to parent's transformation return child_pos unless (parent.class == Sketchup;;Group) and (parent.class == Sketchup;;ComponentInstance) tra = parent.transformation.origin.to_a xaxis = parent.transformation.xaxis.to_a yaxis = parent.transformation.yaxis.to_a zaxis = parent.transformation.zaxis.to_a # my experimented formule external_pos[x] = tra[0] + child_pos[0]*xaxis[0] + child_pos[1]*yaxis[0] + child_pos[2]*zaxis[0] external_pos[y] = tra[1] + child_pos[0]*xaxis[1] + child_pos[1]*yaxis[1] + child_pos[2]*zaxis[1] external_pos[z] = tra[2] + child_pos[0]*xaxis[2] + child_pos[1]*yaxis[2] + child_pos[2]*zaxis[2] #external_pos = child_pos.transform(parent.transformation).to_a #alternative sketchup formula return external_pos end
Copy to editor to unwrap the code lines.
It basically shows how thattransform
formula works
By the way If you have sketchy physics, you can see play with something similiar here: Navigation System -
@tig said:
I assume you have somehow got the center of the face inside its 'context' - let's call it
pt
.
You have a reference to the face's 'container' [a group or instance].
You can get the transformation of that - e.g.tr=instance.transformation
.
You can apply a transformation to a point thus:
pt.transform!(tr)
You can also do the inverse - e.g. you have a point outside of the context and want to convert it as if it's inside the context...
pt.transform!(tr.inverse)
I'm unsure about which way round you want to do this, but somewhere in this is your answer...
I split my 3D car model in several parts, e.g. door, wheel, etc. I want to locate their center no matter the user change the view point.
Use the top of the car as an example, I built a "car_top" group, and used the group as a container, but still did not get the correct coordinate.
-
@anton_s said:
Here's what I've got.
def self.currentPos(parent, child_pos) #Get the position of the point relative to parent's transformation > return child_pos unless (parent.class == Sketchup;;Group) and (parent.class == Sketchup;;ComponentInstance) > tra = parent.transformation.origin.to_a > xaxis = parent.transformation.xaxis.to_a > yaxis = parent.transformation.yaxis.to_a > zaxis = parent.transformation.zaxis.to_a > # my experimented formule > external_pos[x] = tra[0] + child_pos[0]*xaxis[0] + child_pos[1]*yaxis[0] + child_pos[2]*zaxis[0] > external_pos[y] = tra[1] + child_pos[0]*xaxis[1] + child_pos[1]*yaxis[1] + child_pos[2]*zaxis[1] > external_pos[z] = tra[2] + child_pos[0]*xaxis[2] + child_pos[1]*yaxis[2] + child_pos[2]*zaxis[2] > #external_pos = child_pos.transform(parent.transformation).to_a #alternative sketchup formula > return external_pos > end
Copy to editor to unwrap the code lines.
It basically shows how thattransform
formula works
By the way If you have sketchy physics, you can see play with something similiar here: Navigation SystemThanks for your code. But I still did not get the coordinate that I want, the return coordinate is equal to the child's coordinate. The type of the child is Sketchup::Face, and the type of its parent is Sketchup::ComponentDefinition. I made a group like this:
model = Sketchup.active_model; groups = []; model.definitions.each{|d| groups << d if d.group? } entities = model.entities; selection = model.selection; pgrp = []; groups.each{|d| pgrp << d.instances[0] if d.instances[0].name=="car_top" } tmpents = pgrp[0].entities; #print pgrp[0] faces = tmpents.find_all{|entity|entity.class==Sketchup;;Face} ctr = faces[8].bounds.center print ctr pos = self.currentPos(pgrp[0], ctr) print pos
pgrp[0] is a group that contain faces[8]. May be there are some bugs...
-
Sorry, rewritten it with errors. Here's a better version:
def currentPos(parent, child_pos) #Get the position of the point relative to parent's transformation return false if (parent.class != Sketchup;;Group) and (parent.class != Sketchup;;ComponentInstance) tra = parent.transformation.origin.to_a xaxis = parent.transformation.xaxis.to_a yaxis = parent.transformation.yaxis.to_a zaxis = parent.transformation.zaxis.to_a external_pos = Array.new(3) # my experimented formule external_pos[0] = tra[0] + child_pos[0]*xaxis[0] + child_pos[1]*yaxis[0] + child_pos[2]*zaxis[0] external_pos[1] = tra[1] + child_pos[0]*xaxis[1] + child_pos[1]*yaxis[1] + child_pos[2]*zaxis[1] external_pos[2] = tra[2] + child_pos[0]*xaxis[2] + child_pos[1]*yaxis[2] + child_pos[2]*zaxis[2] #external_pos = child_pos.transform(parent.transformation).to_a # alternative sketchup formula return external_pos end
Copy to nowpad++ to unwrap all lines
Note its no moreself.currentPos
, its justcurrentPos
-
Advertisement