Add angularPrecision to export, exportShape and toString

Add tolerance and angularPrecision to TestExporters._exportBox and in
the call to exportShape.
This commit is contained in:
Wink Saville
2020-08-06 12:20:12 -07:00
parent 4f0f333ad8
commit 462dcea6c4
3 changed files with 19 additions and 15 deletions

View File

@ -33,6 +33,7 @@ def export(
fname: str, fname: str,
exportType: Optional[ExportLiterals] = None, exportType: Optional[ExportLiterals] = None,
tolerance: float = 0.1, tolerance: float = 0.1,
angularPrecision: float = 0.1,
): ):
""" """
@ -42,6 +43,7 @@ def export(
:param fname: output filename. :param fname: output filename.
:param exportType: the exportFormat to use. If None will be inferred from the extension. Default: None. :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 tolerance, in model units. Default 0.1.
:param angularPrecision: the angular precision, in radians. Default 0.1 or about 5.7deg.
""" """
shape: Shape shape: Shape
@ -60,7 +62,7 @@ def export(
raise ValueError("Unknown extensions, specify export type explicitly") raise ValueError("Unknown extensions, specify export type explicitly")
if exportType == ExportTypes.TJS: if exportType == ExportTypes.TJS:
tess = shape.tessellate(tolerance) tess = shape.tessellate(tolerance, angularPrecision)
mesher = JsonMesh() mesher = JsonMesh()
# add vertices # add vertices
@ -79,7 +81,7 @@ def export(
f.write(getSVG(shape)) f.write(getSVG(shape))
elif exportType == ExportTypes.AMF: elif exportType == ExportTypes.AMF:
tess = shape.tessellate(tolerance) tess = shape.tessellate(tolerance, angularPrecision)
aw = AmfWriter(tess) aw = AmfWriter(tess)
with open(fname, "wb") as f: with open(fname, "wb") as f:
aw.writeAmf(f) aw.writeAmf(f)
@ -94,16 +96,16 @@ def export(
shape.exportStep(fname) shape.exportStep(fname)
elif exportType == ExportTypes.STL: elif exportType == ExportTypes.STL:
shape.exportStl(fname, tolerance) shape.exportStl(fname, tolerance, angularPrecision)
else: else:
raise ValueError("Unknown export type") raise ValueError("Unknown export type")
@deprecate() @deprecate()
def toString(shape, exportType, tolerance=0.1): def toString(shape, exportType, tolerance=0.1, angularPrecision=0.1):
s = StringIO.StringIO() s = StringIO.StringIO()
exportShape(shape, exportType, s, tolerance) exportShape(shape, exportType, s, tolerance, angularPrecision)
return s.getvalue() return s.getvalue()
@ -113,20 +115,22 @@ def exportShape(
exportType: ExportLiterals, exportType: ExportLiterals,
fileLike: IO, fileLike: IO,
tolerance: float = 0.1, tolerance: float = 0.1,
angularPrecision: float = 0.1,
): ):
""" """
:param shape: the shape to export. it can be a shape object, or a cadquery object. If a cadquery :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 object, the first value is exported
:param exportType: the exportFormat to use :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. :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 The object should be already open and ready to write. The caller is responsible
for closing the object for closing the object
:param tolerance: the tolerance, in model units
:param angularPrecision: the angular precision, in radians. Default 0.1 or about 5.7deg.
""" """
def tessellate(shape): def tessellate(shape, angularPrecision):
return shape.tessellate(tolerance) return shape.tessellate(tolerance, angularPrecision)
shape: Shape shape: Shape
if isinstance(w, Workplane): if isinstance(w, Workplane):
@ -135,7 +139,7 @@ def exportShape(
shape = w shape = w
if exportType == ExportTypes.TJS: if exportType == ExportTypes.TJS:
tess = tessellate(shape) tess = tessellate(shape, angularPrecision)
mesher = JsonMesh() mesher = JsonMesh()
# add vertices # add vertices
@ -151,7 +155,7 @@ def exportShape(
elif exportType == ExportTypes.SVG: elif exportType == ExportTypes.SVG:
fileLike.write(getSVG(shape)) fileLike.write(getSVG(shape))
elif exportType == ExportTypes.AMF: elif exportType == ExportTypes.AMF:
tess = tessellate(shape) tess = tessellate(shape, angularPrecision)
aw = AmfWriter(tess) aw = AmfWriter(tess)
aw.writeAmf(fileLike) aw.writeAmf(fileLike)
else: else:
@ -166,7 +170,7 @@ def exportShape(
if exportType == ExportTypes.STEP: if exportType == ExportTypes.STEP:
shape.exportStep(outFileName) shape.exportStep(outFileName)
elif exportType == ExportTypes.STL: elif exportType == ExportTypes.STL:
shape.exportStl(outFileName, tolerance) shape.exportStl(outFileName, tolerance, angularPrecision)
else: else:
raise ValueError("No idea how i got here") raise ValueError("No idea how i got here")

View File

@ -798,11 +798,11 @@ class Shape(object):
return self._bool_op((self,), toIntersect, intersect_op) return self._bool_op((self,), toIntersect, intersect_op)
def tessellate( def tessellate(
self, tolerance: float self, tolerance: float, angularPrecision: float = 0.1
) -> Tuple[List[Vector], List[Tuple[int, int, int]]]: ) -> Tuple[List[Vector], List[Tuple[int, int, int]]]:
if not BRepTools.Triangulation_s(self.wrapped, tolerance): if not BRepTools.Triangulation_s(self.wrapped, tolerance):
BRepMesh_IncrementalMesh(self.wrapped, tolerance, True) BRepMesh_IncrementalMesh(self.wrapped, tolerance, True, angularPrecision)
vertices: List[Vector] = [] vertices: List[Vector] = []
triangles: List[Tuple[int, int, int]] = [] triangles: List[Tuple[int, int, int]] = []

View File

@ -12,7 +12,7 @@ from tests import BaseTest
class TestExporters(BaseTest): class TestExporters(BaseTest):
def _exportBox(self, eType, stringsToFind): def _exportBox(self, eType, stringsToFind, tolerance=0.1, angularPrecision=0.1):
""" """
Exports a test object, and then looks for Exports a test object, and then looks for
all of the supplied strings to be in the result all of the supplied strings to be in the result
@ -25,7 +25,7 @@ class TestExporters(BaseTest):
else: else:
s = io.StringIO() s = io.StringIO()
exporters.exportShape(p, eType, s, 0.1) exporters.exportShape(p, eType, s, tolerance=tolerance, angularPrecision=angularPrecision)
result = "{}".format(s.getvalue()) result = "{}".format(s.getvalue())