Algorithm to trim non-solid geometry

I’m wondering if anyone knows of a ‘simple’ formula to trim geometry along two planes.

I’m thinking rather than do all the calcualtions it might be easier to draw the dormer as a square ‘shed’, and then trim it to the roof plane.

Basicly something like @Fredo6 Visuhole.

Found a few clues here:

There is nothing simple here, especially if you deal with multi-level grouping. And anyway, not a formula… You have to deal with the way Sketchup manages entities through operations, noting that the entities will change when you cut them by a plane.

In your case, you need first to make sure that you get all entities resulting from the intersection with the 2 planes. Then, it’s relatively easy to check for each one if they are between the 2 planes so that you can eliminate them.

Now reconstructing and filling the cut section is much more tricky. And there are other odysseys when you have edges right on the cut planes.


OK I think I’m about half way there. I figured out how to create a solid group containing the cut out area, and then using entities.intersect_with to split the faces.

Now I’m trying to use Geom.point_in_polygon2D to remove all the edges that are inside the polygon area of the cut.

Wow! Quite a few minutes per line of code!

def trim_dormer(inst, mid_point, height, depth)
  t =
  Sketchup.active_model.start_operation('Trim Dormer', true)
  ce = inst.definition.entities
  x = ce.grep(Sketchup::Edge).map { |e| [e.end.position.x, e.start.position.x].min }.min - 1
  x1 = ce.grep(Sketchup::Edge).map { |e| [e.end.position.x, e.start.position.x].max }.max + 1
  parent_ent = inst.parent.is_a?(Sketchup::ComponentInstance) ? inst.parent.definition.entities : inst.parent.entities
  g = parent_ent.add_group
  g.transformation = inst.transformation
  ge = g.entities
  points =
    [, depth + 1, -1),, -1, -1),, 0, 0),, mid_point.y, mid_point.z),, depth, height),, depth + 1, height + 1)
  f = ge.add_face(points)
  f.pushpull(0 - (x1 - x))
  ce.intersect_with(false, inst.transformation, ce, inst.transformation, false, g)
  polygon = { |pt|, pt.z, 0) }
  edges = ce.grep(Sketchup::Edge).select do |e|
    pt1 = e.start.position
    pt2 = e.end.position
    (Geom.point_in_polygon_2D(, pt1.z, 0), polygon, false) ||
    Geom.point_in_polygon_2D(, pt2.z, 0), polygon, false))
  p "#{ - t} seconds to trim dormer."

1 Like