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

Added argument handling for ASCII export of assemblies to GLTF and STL #1418

Merged
merged 16 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ nested.wrl
nested.xml
nested.zip
nested.stl
nested_ascii.bin
nested_ascii.gltf
nested_ascii.stl
out1.3mf
out2.3mf
out3.3mf
Expand Down
19 changes: 16 additions & 3 deletions cadquery/assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,10 @@ def save(
:param exportType: export format (default: None, results in format being inferred form the path)
:param tolerance: the deflection tolerance, in model units. Only used for GLTF, VRML. Default 0.1.
:param angularTolerance: the angular tolerance, in radians. Only used for GLTF, VRML. Default 0.1.
:param \**kwargs: Additional keyword arguments. Only used for STEP.
:param \**kwargs: Additional keyword arguments. Only used for STEP, GLTF and STL.
jmwright marked this conversation as resolved.
Show resolved Hide resolved
See :meth:`~cadquery.occ_impl.exporters.assembly.exportAssembly`.
:param ascii: STL only - Sets whether or not STL export should be text or binary
:type ascii: bool
"""

# Make sure the export mode setting is correct
Expand All @@ -488,11 +490,22 @@ def save(
elif exportType == "VRML":
exportVRML(self, path, tolerance, angularTolerance)
elif exportType == "GLTF":
exportGLTF(self, path, True, tolerance, angularTolerance)
# Handle the binary option for GLTF export
jmwright marked this conversation as resolved.
Show resolved Hide resolved
binary = True
path_parts = path.split(".")
if len(path_parts) > 0 and path_parts[-1] == "gltf":
binary = False
jmwright marked this conversation as resolved.
Show resolved Hide resolved

exportGLTF(self, path, binary, tolerance, angularTolerance)
elif exportType == "VTKJS":
exportVTKJS(self, path)
elif exportType == "STL":
self.toCompound().exportStl(path, tolerance, angularTolerance)
# Handle the ascii setting for STL export
export_ascii = False
if "ascii" in kwargs:
export_ascii = bool(kwargs.get("ascii"))

self.toCompound().exportStl(path, tolerance, angularTolerance, export_ascii)
else:
raise ValueError(f"Unknown format: {exportType}")

Expand Down
2 changes: 2 additions & 0 deletions doc/importexport.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Export Formats
* VRML
* VTP
* 3MF
* GLTF
jmwright marked this conversation as resolved.
Show resolved Hide resolved

Notes on the Formats
---------------------
Expand All @@ -42,6 +43,7 @@ Notes on the Formats
* TJS is short for ThreeJS, and is a JSON mesh format that is useful for displaying 3D models in web browsers. The TJS format is used to display embedded 3D examples within the CadQuery documentation.
* VRML is a mesh-based format for representing interactive 3D objects in a web browser.
* VTP is a mesh-based format used by the VTK library.
* GLTF is a mesh-based format useful for viewing models on the web. Whether the resulting GLTF file is binary (.glb) or text (.gltf) is set by the file extension.
jmwright marked this conversation as resolved.
Show resolved Hide resolved

Importing DXF
##############
Expand Down
29 changes: 28 additions & 1 deletion tests/test_assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,11 +647,38 @@ def test_save(extension, args, nested_assy, nested_assy_sphere):
assert os.path.exists(filename)


def test_save_stl_formats(nested_assy_sphere):

# Binary export
nested_assy_sphere.save("nested.stl", "STL", ascii=False)
assert os.path.exists("nested.stl")

# Trying to read a binary file as UTF-8/ASCII should throw an error
with pytest.raises(UnicodeDecodeError) as info:
with open("nested.stl", "r") as file:
file.read()

# ASCII export
nested_assy_sphere.save("nested_ascii.stl", ascii=True)
assert os.path.exists("nested_ascii.stl")
assert os.path.getsize("nested_ascii.stl") > 3960 * 1024


def test_save_gltf(nested_assy_sphere):

# Binary export
nested_assy_sphere.save("nested.glb", "GLTF")
assert os.path.exists("nested.glb")
assert os.path.getsize("nested.glb") > 50 * 1024

# Trying to read a binary file as UTF-8/ASCII should throw an error
with pytest.raises(UnicodeDecodeError) as info:
with open("nested.glb", "r") as file:
file.read()

# ASCII export
nested_assy_sphere.save("nested_ascii.gltf")
assert os.path.exists("nested_ascii.gltf")
assert os.path.getsize("nested_ascii.gltf") > 5 * 1024


def test_save_gltf_boxes2(boxes2_assy, tmpdir, capfd):
Expand Down