Question

I'm trying to use the SketchUp Ruby API to remove vertical elements from an imported STL file.

I've adapted a snippet to give the following code which selects non-horizontal faces.

s=Sketchup.active_model.selection;
a=s.to_a;
s.clear;
a.grep(Sketchup::Face).each{ |f| s.add( f )if f.normal.z > -0.1 and f.normal.z < 0.1}

I've then tried to adapt this to select non-horizontal edges, using the fact that any such edges will have one vertex with a z-position different to the other.

s=Sketchup.active_model.selection;
a=s.to_a;
s.clear;
a.grep( Sketchup::Edge ).each {
    |edge| s.add(edge) if (edge.end.position.z - edge.other_vertex(edge.end).position.z)}

However this is selecting all the edges in the model. Can anyone point out where I'm going wrong?

Was it helpful?

Solution

In the end, instead of trying to write a one-liner, I wrote the following function which does what I need.

def find_plans()
    model = Sketchup.active_model()
    s = model.selection()
    s.clear()

    ents = model.entities
    non_horizontal_edges = []
    for e in ents
        non_horizontal_edges.push( e ) if e.typename() == 'Edge' and (e.end.position.z - e.other_vertex(e.end).position.z).abs > 1
    end
    ents.erase_entities( non_horizontal_edges )
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top