[plugin/code] get global coordinates, the brute force way
-
Hello guys,
This script gives you all real world coordinates of vertices in ((deeply)-nested)entities in the active model.
The script is largely based on a script named 'SketchUp to DXF STL Converter'. For more details please see the attachment.It's brute force, so I'm curious for more elegant solutions....
Liquid
-
I'd also very much like a way to access the global coordinates of points within groups without needing to reference the nesting parents transformation. The brute force method works, but has anyone found a better way to do this yet?
-
Can you describe your scenario in more detail? Got a sample code snippet?
Why don't you want to use the parent transformation?If it's within an open group or component you can use model.edit_transform, but otherwise you have to somehow collect the parent transformations.
-
Here's a piece of code I've written for the new project to get stuff from groups/components. Maybe you'll find it useful:
# ------------------------------------------------------------------------------ # # Anton Synytsia # anton.synytsia@gmail.com # # ------------------------------------------------------------------------------ module MSPhysics module Group # @!visibility private VALID_TYPES = [Sketchup;;Group, Sketchup;;ComponentInstance].freeze # @!visibility private INVALID_TYPE = 'The specified entity is not a group or a component!'.freeze module_function # Get entities of a group/component/definition. # @param [Sketchup;;Group, Sketchup;;ComponentInstance, Sketchup;;ComponentDefinition] ent # @return [Sketchup;;Entities, NilClass] # @since 1.0.0 def get_entities(ent) case ent when Sketchup;;Group, Sketchup;;ComponentDefinition ent.entities when Sketchup;;ComponentInstance ent.definition.entities else nil end end # Get all edges of a group/component. # @param [Sketchup;;Group, Sketchup;;ComponentInstance] ent # @param [Boolean] recursive Whether to include all the child groups and # components. # @param [Boolean] transform Whether to give points in global coordinates. # @return [Array<Array<Geom;;Point3d>>] An array of edges. Each edge # represents an array of two points. # @since 1.0.0 def get_edges(ent, recursive = true, transform = true) unless VALID_TYPES.include?(ent.class) raise INVALID_TYPE end edges = [] get_entities(ent).each { |e| if VALID_TYPES.include?(e.class) and (recursive == true) edges.push *get_edges(e, true, true) next end next unless e.is_a?(Sketchup;;Edge) edge = [] e.vertices.each { |v| edge.push v.position } edges.push edge } if transform == true edges.each { |edge| edge.each { |pt| pt.transform!(ent.transformation) } } end edges end # Get vertices from all edges of a group/component. # @param [Sketchup;;Group, Sketchup;;ComponentInstance] ent # @param [Boolean] recursive Whether to include all the child groups and # components. # @param [Boolean] transform Whether to give points in global coordinates. # @return [Array<Geom;;Point3d>] An array of points. # @since 1.0.0 def get_vertices_from_edges(ent, recursive = true, transform = true) unless VALID_TYPES.include?(ent.class) raise INVALID_TYPE end pts = [] verts = [] get_entities(ent).each { |e| if VALID_TYPES.include?(e.class) and (recursive == true) pts.push *get_vertices_from_edges(e, true, true) next end next unless e.is_a?(Sketchup;;Edge) e.vertices.each { |v| verts.push(v) unless verts.include?(v) } } verts.each { |v| pts.push v.position } if transform == true pts.each { |pt| pt.transform!(ent.transformation) } end pts end # Get all faces of a group/component. # @param [Sketchup;;Group, Sketchup;;ComponentInstance] ent # @param [Boolean] recursive Whether to include all the child groups and # components. # @param [Boolean] transform Whether to give points in global coordinates. # @return [Array<Array<Geom;;Point3d>>] An array of faces. Each face # represents an array of points. # @since 1.0.0 def get_faces(ent, recursive = true, transform = true) unless VALID_TYPES.include?(ent.class) raise INVALID_TYPE end faces = [] get_entities(ent).each { |e| if VALID_TYPES.include?(e.class) and (recursive == true) faces.push *get_faces(e, true, true) next end next unless e.is_a?(Sketchup;;Face) face = [] e.vertices.each { |v| face.push v.position } faces.push face } if transform == true faces.each { |face| face.each { |pt| pt.transform!(ent.transformation) } } end faces end # Get vertices from all faces of a group/component. # @param [Sketchup;;Group, Sketchup;;ComponentInstance] ent # @param [Boolean] recursive Whether to include all the child groups and # components. # @param [Boolean] transform Whether to give points in global coordinates. # @return [Array<Geom;;Point3d>] An array of points. # @since 1.0.0 def get_vertices_from_faces(ent, recursive = true, transform = true) unless VALID_TYPES.include?(ent.class) raise INVALID_TYPE end pts = [] verts = [] get_entities(ent).each { |e| if VALID_TYPES.include?(e.class) and (recursive == true) pts.push *get_vertices_from_faces(e, true, true) next end next unless e.is_a?(Sketchup;;Face) e.vertices.each { |v| verts.push(v) unless verts.include?(v) } } verts.each { |v| pts.push v.position } if transform == true pts.each { |pt| pt.transform!(ent.transformation) } end pts end # Get all construction points and lines of a group/component. # @param [Sketchup;;Group, Sketchup;;ComponentInstance] ent # @param [Boolean] recursive Whether to include all the child groups and # components. # @param [Boolean] transform Whether to give points in global coordinates. # @return [Array<Geom;;Point3d>] An array of points. # @since 1.0.0 def get_construction(ent, recursive = true, transform = true) unless VALID_TYPES.include?(ent.class) raise INVALID_TYPE end pts = [] get_entities(ent).each { |e| if VALID_TYPES.include?(e.class) and (recursive == true) pts.push *get_construction(e, true, true) next end if e.is_a?(Sketchup;;ConstructionPoint) pts.push e.position elsif e.is_a?(Sketchup;;ConstructionLine) pts.push(e.start) unless e.start.nil? pts.push(e.end) unless e.end.nil? end } if transform == true pts.each { |pt| pt.transform!(ent.transformation) } end pts end end # module Group end # module MSPhysics
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