Skip to content

Commit

Permalink
Added assembly handling for SVG and STL export
Browse files Browse the repository at this point in the history
  • Loading branch information
jmwright committed Dec 5, 2023
1 parent 583da30 commit eb72128
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/cq_cli/cqcodecs/cq_codec_stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +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(
result.exportStl(
temp_file, linearDeflection, angularDeflection, True
)

Expand Down
9 changes: 8 additions & 1 deletion src/cq_cli/cqcodecs/cq_codec_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +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,
result,
temp_file,
exporters.ExportTypes.SVG,
opt=output_opts,
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 eb72128

Please sign in to comment.