Creating Solids with Ruby Api

I’m curious why some objects do not show up as solid when created using the ruby api?

Run this code in the Ruby Console.

def clear_model
  Sketchup.active_model.entities.clear!
  Sketchup.active_model.definitions.purge_unused
end

def test_non_solid
  d = Sketchup.active_model.definitions.add("Non-Solid")
  Sketchup.active_model.entities.add_instance(d, IDENTITY)
  d.entities.add_face([5, 0, 10], [5, 10, 10], [0, 10, 10], [0, 0, 10])
  d.entities.add_face([5, 0, 10], [5, 10, 10], [10, 10, 10], [10, 0, 10])
  d.entities.add_face([0, 0, 10], [0, 10, 10], [0, 10, 0], [0, 0, 0])
  d.entities.add_face([10, 0, 10], [10, 10, 10], [10, 10, 0], [10, 0, 0])
  d.entities.add_face([0, 10, 10], [10, 10, 10], [10, 10, 0], [0, 10, 0])
  d.entities.add_face([0, 0, 10], [10, 0, 10], [10, 0, 0], [0, 0, 0])
  d.entities.add_face([0, 0, 0], [10, 0, 0], [10, 10, 0], [0, 10, 0])
end

def test_solid
  d = Sketchup.active_model.definitions.add("Solid")
  Sketchup.active_model.entities.add_instance(d, Geom::Point3d.new(0, 20, 0))
  d.entities.add_face([0, 0, 10], [0, 10, 10], [0, 10, 0], [0, 0, 0])
  d.entities.add_face([0, 10, 10], [10, 10, 10], [10, 10, 0], [0, 10, 0])
  d.entities.add_face([10, 0, 10], [10, 10, 10], [10, 10, 0], [10, 0, 0])
  d.entities.add_face([0, 0, 10], [10, 0, 10], [10, 0, 0], [0, 0, 0])
  d.entities.add_face([0, 0, 0], [10, 0, 0], [10, 10, 0], [0, 10, 0])
  d.entities.add_face([0, 0, 10], [0, 10, 10], [5, 10, 10], [5, 0, 10])
  d.entities.add_face([5, 0, 10], [5, 10, 10], [10, 10, 10], [10, 0, 10])
end

def do_tests
  clear_model
  test_non_solid
  test_solid
end

Note that in the non solid test the face creates a new long edge rather than reusing the two short lines that already exist.

I know face creation behaves differently depending on the order of the points. In method test_non_solid, the first “add_face” call creates the top westerly face.
The order of the points is creating a face that results in a normal vector that points upwards.
You can see that in the Ruby Console by typing:

entities = Sketchup.active_model.entities
face = entities.add_face( [5, 0, 10], [5, 10, 10], [0, 10, 10], [0, 0, 10] )
face.normal
(-0.0, -0.0, 1.0)

(The rectangle tool on the other hand does the reverse, and creates a face whose normal vector points downwards when editing in the X-Y plane).

After the component is created, the code somehow figures out that one face’s normal vector is off and reverses it, but in doing so it merges the edges that are end to end.

The last line of add_face in test_solid creates the equivalent top westerly face, but its normal points downward, and there are separate edges for each small rectangle, not one long one.

Now, I believe that one of Solid Inspector’s checks is to ensure that each edge is associated with only two faces. The red edges identified by Solid Inspector are touching 3 faces, and that component fails Solid Inspector’s test.

1 Like

Hm… that is strange - and unexpected. add_face should resolve the overlapping edges here.

Can you log this issue in our tracker? Issues · SketchUp/api-issue-tracker (github.com)

1 Like
2 Likes