sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    [plugin/code] get global coordinates, the brute force way

    Scheduled Pinned Locked Moved Developers' Forum
    4 Posts 4 Posters 1.1k Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • liquid98L Offline
      liquid98
      last edited by

      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


      get_global coordinates.rb

      Things that flourish fall into decay. This is not-Tao, And what is not-Tao soon ends ~ Lao tse

      1 Reply Last reply Reply Quote 0
      • S Offline
        shannonnovus
        last edited by

        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?

        1 Reply Last reply Reply Quote 0
        • tt_suT Offline
          tt_su
          last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • A Offline
            Anton_S
            last edited by

            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
            
            
            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Buy SketchPlus
            Buy SUbD
            Buy WrapR
            Buy eBook
            Buy Modelur
            Buy Vertex Tools
            Buy SketchCuisine
            Buy FormFonts

            Advertisement