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