Skip to content

Commit

Permalink
Merge pull request #27 from CadQuery/assembly_compounds
Browse files Browse the repository at this point in the history
Adds Assembly Handling to SVG and STL Export
  • Loading branch information
jmwright authored Dec 7, 2023
2 parents 583da30 + fa28edf commit 04cda27
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
13 changes: 10 additions & 3 deletions src/cq_cli/cqcodecs/cq_codec_stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ def convert(build_result, output_file=None, error_file=None, output_opts=None):

# The exporters will add extra output that we do not want, so suppress it
with helpers.suppress_stdout_stderr():
# There should be a shape in the build results
result = build_result.results[0].shape

# If the build result is an assembly, we have to make it a compound before trying to export it as SVG
if type(result).__name__ == "Assembly":
result = result.toCompound()
else:
result = result.val()

# Put the STL output into the temp file
build_result.results[0].shape.val().exportStl(
temp_file, linearDeflection, angularDeflection, True
)
result.exportStl(temp_file, linearDeflection, angularDeflection, True)

# Read the STL output back in
with open(temp_file, "r") as file:
Expand Down
12 changes: 8 additions & 4 deletions src/cq_cli/cqcodecs/cq_codec_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ def convert(build_result, output_file=None, error_file=None, output_opts=None):

# The exporters will add extra output that we do not want, so suppress it
with helpers.suppress_stdout_stderr():
# There should be a shape in the build results
result = build_result.results[0].shape

# If the build result is an assembly, we have to make it a compound before trying to export it as SVG
if type(result).__name__ == "Assembly":
result = result.toCompound()

# Put the STEP output into the temp file
exporters.export(
build_result.results[0].shape,
temp_file,
exporters.ExportTypes.SVG,
opt=output_opts,
result, temp_file, exporters.ExportTypes.SVG, opt=output_opts,
)

# Read the STEP output back in
Expand Down
19 changes: 19 additions & 0 deletions tests/test_stl_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,22 @@ def test_stl_codec_quality():
low_detail = len(out2.decode().split("\n"))

assert low_detail < high_detail


def test_stl_codec_with_assembly():
"""
Test of the STL codec plugin with a CadQuery assembly.
"""
test_file = helpers.get_test_file_location("cube_assy.py")

command = [
"python",
"src/cq_cli/main.py",
"--codec",
"stl",
"--infile",
test_file,
]
out, err, exitcode = helpers.cli_call(command)

assert out.decode().split("\n")[0].replace("\r", "") == "solid "
24 changes: 24 additions & 0 deletions tests/test_svg_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,27 @@ def test_svg_codec():
out.decode().split("\n")[0].replace("\r", "")
== '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'
)


def test_svg_codec_with_assembly():
"""
Test of the SVG codec plugin with a CadQuery assembly.
"""
test_file = helpers.get_test_file_location("cube_assy.py")

command = [
"python",
"src/cq_cli/main.py",
"--codec",
"svg",
"--infile",
test_file,
"--outputopts",
"width:100;height:100;marginLeft:12;marginTop:12;showAxes:False;projectionDir:(0.5,0.5,0.5);strokeWidth:0.25;strokeColor:(255,0,0);hiddenColor:(0,0,255);showHidden:True;",
]
out, err, exitcode = helpers.cli_call(command)

assert (
out.decode().split("\n")[0].replace("\r", "")
== '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'
)

0 comments on commit 04cda27

Please sign in to comment.