Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add angular precision #424

Merged
merged 6 commits into from
Aug 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions cadquery/occ_impl/exporters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def export(
fname: str,
exportType: Optional[ExportLiterals] = None,
tolerance: float = 0.1,
angularTolerance: float = 0.1,
):

"""
Expand All @@ -41,7 +42,8 @@ def export(
:param w: Shape or Wokrplane to be exported.
:param fname: output filename.
:param exportType: the exportFormat to use. If None will be inferred from the extension. Default: None.
:param tolerance: the tolerance, in model units. Default 0.1.
:param tolerance: the deflection tolerance, in model units. Default 0.1.
:param angularTolerance: the angular tolerance, in radians. Default 0.1.
"""

shape: Shape
Expand All @@ -60,7 +62,7 @@ def export(
raise ValueError("Unknown extensions, specify export type explicitly")

if exportType == ExportTypes.TJS:
tess = shape.tessellate(tolerance)
tess = shape.tessellate(tolerance, angularTolerance)
mesher = JsonMesh()

# add vertices
Expand All @@ -79,7 +81,7 @@ def export(
f.write(getSVG(shape))

elif exportType == ExportTypes.AMF:
tess = shape.tessellate(tolerance)
tess = shape.tessellate(tolerance, angularTolerance)
aw = AmfWriter(tess)
with open(fname, "wb") as f:
aw.writeAmf(f)
Expand All @@ -94,16 +96,16 @@ def export(
shape.exportStep(fname)

elif exportType == ExportTypes.STL:
shape.exportStl(fname, tolerance)
shape.exportStl(fname, tolerance, angularTolerance)

else:
raise ValueError("Unknown export type")


@deprecate()
def toString(shape, exportType, tolerance=0.1):
def toString(shape, exportType, tolerance=0.1, angularTolerance=0.1):
s = StringIO.StringIO()
exportShape(shape, exportType, s, tolerance)
exportShape(shape, exportType, s, tolerance, angularTolerance)
return s.getvalue()


Expand All @@ -113,20 +115,22 @@ def exportShape(
exportType: ExportLiterals,
fileLike: IO,
tolerance: float = 0.1,
angularTolerance: float = 0.1,
):
"""
:param shape: the shape to export. it can be a shape object, or a cadquery object. If a cadquery
object, the first value is exported
:param exportType: the exportFormat to use
:param tolerance: the tolerance, in model units
:param fileLike: a file like object to which the content will be written.
The object should be already open and ready to write. The caller is responsible
for closing the object
:param tolerance: the linear tolerance, in model units. Default 0.1.
:param angularTolerance: the angular tolerance, in radians. Default 0.1.
"""

def tessellate(shape):
def tessellate(shape, angularTolerance):

return shape.tessellate(tolerance)
return shape.tessellate(tolerance, angularTolerance)

shape: Shape
if isinstance(w, Workplane):
Expand All @@ -135,7 +139,7 @@ def tessellate(shape):
shape = w

if exportType == ExportTypes.TJS:
tess = tessellate(shape)
tess = tessellate(shape, angularTolerance)
mesher = JsonMesh()

# add vertices
Expand All @@ -151,7 +155,7 @@ def tessellate(shape):
elif exportType == ExportTypes.SVG:
fileLike.write(getSVG(shape))
elif exportType == ExportTypes.AMF:
tess = tessellate(shape)
tess = tessellate(shape, angularTolerance)
aw = AmfWriter(tess)
aw.writeAmf(fileLike)
else:
Expand All @@ -166,7 +170,7 @@ def tessellate(shape):
if exportType == ExportTypes.STEP:
shape.exportStep(outFileName)
elif exportType == ExportTypes.STL:
shape.exportStl(outFileName, tolerance)
shape.exportStl(outFileName, tolerance, angularTolerance)
else:
raise ValueError("No idea how i got here")

Expand Down
8 changes: 4 additions & 4 deletions cadquery/occ_impl/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,10 @@ def cast(
return tr

def exportStl(
self, fileName: str, precision: float = 1e-3, angularPrecision: float = 0.1
self, fileName: str, tolerance: float = 1e-3, angularTolerance: float = 0.1
) -> bool:

mesh = BRepMesh_IncrementalMesh(self.wrapped, precision, True, angularPrecision)
mesh = BRepMesh_IncrementalMesh(self.wrapped, tolerance, True, angularTolerance)
mesh.Perform()

writer = StlAPI_Writer()
Expand Down Expand Up @@ -798,11 +798,11 @@ def intersect(self, *toIntersect: "Shape") -> "Shape":
return self._bool_op((self,), toIntersect, intersect_op)

def tessellate(
self, tolerance: float
self, tolerance: float, angularTolerance: float = 0.1
) -> Tuple[List[Vector], List[Tuple[int, int, int]]]:

if not BRepTools.Triangulation_s(self.wrapped, tolerance):
BRepMesh_IncrementalMesh(self.wrapped, tolerance, True)
BRepMesh_IncrementalMesh(self.wrapped, tolerance, True, angularTolerance)

vertices: List[Vector] = []
triangles: List[Tuple[int, int, int]] = []
Expand Down
18 changes: 10 additions & 8 deletions tests/test_exporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class TestExporters(BaseTest):
def _exportBox(self, eType, stringsToFind):
def _exportBox(self, eType, stringsToFind, tolerance=0.1, angularTolerance=0.1):
"""
Exports a test object, and then looks for
all of the supplied strings to be in the result
Expand All @@ -25,7 +25,9 @@ def _exportBox(self, eType, stringsToFind):
else:
s = io.StringIO()

exporters.exportShape(p, eType, s, 0.1)
exporters.exportShape(
p, eType, s, tolerance=tolerance, angularTolerance=angularTolerance
)

result = "{}".format(s.getvalue())

Expand Down Expand Up @@ -84,8 +86,8 @@ def testDXF(self):

s1_i = importers.importDXF("res1.dxf")

self.assertAlmostEquals(s1.val().Area(), s1_i.val().Area(), 6)
self.assertAlmostEquals(s1.edges().size(), s1_i.edges().size())
self.assertAlmostEqual(s1.val().Area(), s1_i.val().Area(), 6)
self.assertAlmostEqual(s1.edges().size(), s1_i.edges().size())

pts = [(0, 0), (0, 0.5), (1, 1)]
s2 = (
Expand All @@ -95,8 +97,8 @@ def testDXF(self):

s2_i = importers.importDXF("res2.dxf")

self.assertAlmostEquals(s2.val().Area(), s2_i.val().Area(), 6)
self.assertAlmostEquals(s2.edges().size(), s2_i.edges().size())
self.assertAlmostEqual(s2.val().Area(), s2_i.val().Area(), 6)
self.assertAlmostEqual(s2.edges().size(), s2_i.edges().size())

s3 = (
Workplane("XY")
Expand All @@ -111,8 +113,8 @@ def testDXF(self):

s3_i = importers.importDXF("res3.dxf")

self.assertAlmostEquals(s3.val().Area(), s3_i.val().Area(), 6)
self.assertAlmostEquals(s3.edges().size(), s3_i.edges().size())
self.assertAlmostEqual(s3.val().Area(), s3_i.val().Area(), 6)
self.assertAlmostEqual(s3.edges().size(), s3_i.edges().size())

def testTypeHandling(self):

Expand Down