Skip to content

Commit

Permalink
Fix import of invalid triangles Issue #472
Browse files Browse the repository at this point in the history
  • Loading branch information
gumyr committed Jan 13, 2024
1 parent 047d9cc commit 97088d6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/build123d/mesher.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
# pylint: disable=no-name-in-module, import-error
import copy
import ctypes
import itertools
import math
import os
import sys
Expand All @@ -96,18 +97,21 @@
BRepBuilderAPI_MakeSolid,
BRepBuilderAPI_Sewing,
)
from OCP.BRepGProp import BRepGProp
from OCP.BRepMesh import BRepMesh_IncrementalMesh
from OCP.gp import gp_Pnt
import OCP.TopAbs as ta
from OCP.GProp import GProp_GProps
from OCP.ShapeFix import ShapeFix_Shape
from OCP.TopAbs import TopAbs_ShapeEnum
from OCP.TopExp import TopExp_Explorer
from OCP.TopoDS import TopoDS_Compound
from OCP.TopLoc import TopLoc_Location

from py_lib3mf import Lib3MF
from build123d.build_enums import MeshType, Unit
from build123d.geometry import Color, TOLERANCE
from build123d.topology import Compound, Shape, Shell, Solid, downcast
from build123d.geometry import Color, TOLERANCE, Vector
from build123d.topology import Compound, Face, Shape, Shell, Solid, downcast


class Mesher:
Expand Down Expand Up @@ -455,8 +459,11 @@ def _get_shape(self, mesh_3mf: Lib3MF.MeshObject) -> Shape:
# Create the triangular face using the polygon
polygon_builder = BRepBuilderAPI_MakePolygon(*ocp_vertices, Close=True)
face_builder = BRepBuilderAPI_MakeFace(polygon_builder.Wire())
# Add new Face to Shell
shell_builder.Add(face_builder.Face())
facet = face_builder.Face()
facet_properties = GProp_GProps()
BRepGProp.SurfaceProperties_s(facet, facet_properties)
if facet_properties.Mass() != 0: # Area==0 is an invalid facet
shell_builder.Add(facet)

# Create the Shell(s) - if the object has voids there will be multiple
shell_builder.Perform()
Expand Down
Binary file added tests/cyl_w_rect_hole.stl
Binary file not shown.
11 changes: 11 additions & 0 deletions tests/test_mesher.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,16 @@ def test_hollow_import(self):
self.assertTrue(stl[0].is_valid())


class TestImportDegenerateTriangles(unittest.TestCase):
def test_degenerate_import(self):
"""Some STLs may contain 'triangles' where all three points are on a line"""
importer = Mesher()
stl = importer.read("cyl_w_rect_hole.stl")[0]
self.assertEqual(type(stl), Solid)
self.assertTrue(stl.is_manifold)
self.assertTrue(stl.is_valid())
self.assertEqual(sum(f.area == 0 for f in stl.faces()), 0)


if __name__ == "__main__":
unittest.main()

0 comments on commit 97088d6

Please sign in to comment.