Is there any performance optimized solution to collect object vertices without crashing when the number of vertices is large, please?

# code here
def collect_vertices(container, cumulative_transformation = Geom::Transformation.new)
    vertices = []
    entities_to_process = [[container, cumulative_transformation]]

    while !entities_to_process.empty?
        current_container, current_transformation = entities_to_process.pop

        # Skip if the object is hidden
        next unless current_container.visible?

        # If it is a ComponentInstance, convert it to its ComponentDefinition
        if current_container.is_a?(Sketchup::ComponentInstance)
            current_container = current_container.definition
        end

        # If it is a ComponentDefinition or Group, collect the vertices
        if current_container.is_a?(Sketchup::ComponentDefinition) || current_container.is_a?(Sketchup::Group)
            # Filter all Edges (skip hidden edges)
            edges = current_container.entities.grep(Sketchup::Edge).select(&:visible?)
            
            # Collect vertices from each Edge
            edges.each do |edge|
                edge.vertices.each do |vertex|
                    vertices << vertex.position.transform(current_transformation)
                end
            end

            # Filter Groups and ComponentInstances (skip hidden objects)
            groups_and_instances = current_container.entities.grep(Sketchup::Group).select(&:visible?) +
                                   current_container.entities.grep(Sketchup::ComponentInstance).select(&:visible?)

            # Add Groups and ComponentInstances to the processing list
            groups_and_instances.each do |entity|
                new_ct = current_transformation * entity.transformation
                entities_to_process << [entity, new_ct]
            end
        end
    end

    vertices
end

Post code correctly in the forum and please edit your previous post.

1 Like

Similar topic:
https://forums.sketchup.com/t/fastest-way-to-get-all-vertices/300652

1 Like

thank you, i have read the article, i will make adjustments to my source code