diff --git a/src/cq_cli/cqcodecs/cq_codec_stl.py b/src/cq_cli/cqcodecs/cq_codec_stl.py index 401ff11..3f67e64 100644 --- a/src/cq_cli/cqcodecs/cq_codec_stl.py +++ b/src/cq_cli/cqcodecs/cq_codec_stl.py @@ -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: diff --git a/src/cq_cli/cqcodecs/cq_codec_svg.py b/src/cq_cli/cqcodecs/cq_codec_svg.py index b01017c..bc7d6be 100644 --- a/src/cq_cli/cqcodecs/cq_codec_svg.py +++ b/src/cq_cli/cqcodecs/cq_codec_svg.py @@ -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 diff --git a/tests/test_stl_codec.py b/tests/test_stl_codec.py index 45ceab5..d6f6648 100644 --- a/tests/test_stl_codec.py +++ b/tests/test_stl_codec.py @@ -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 " diff --git a/tests/test_svg_codec.py b/tests/test_svg_codec.py index ade12a6..1e3561b 100644 --- a/tests/test_svg_codec.py +++ b/tests/test_svg_codec.py @@ -23,3 +23,27 @@ def test_svg_codec(): out.decode().split("\n")[0].replace("\r", "") == '' ) + + +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", "") + == '' + )