From eb7212846cf60fe0d41aa31c26994a6de8cff3b4 Mon Sep 17 00:00:00 2001 From: Jeremy Wright Date: Tue, 5 Dec 2023 11:09:39 -0500 Subject: [PATCH 1/2] Added assembly handling for SVG and STL export --- src/cq_cli/cqcodecs/cq_codec_stl.py | 11 ++++++++++- src/cq_cli/cqcodecs/cq_codec_svg.py | 9 ++++++++- tests/test_stl_codec.py | 19 +++++++++++++++++++ tests/test_svg_codec.py | 24 ++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/cq_cli/cqcodecs/cq_codec_stl.py b/src/cq_cli/cqcodecs/cq_codec_stl.py index 401ff11..681ce15 100644 --- a/src/cq_cli/cqcodecs/cq_codec_stl.py +++ b/src/cq_cli/cqcodecs/cq_codec_stl.py @@ -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 ) diff --git a/src/cq_cli/cqcodecs/cq_codec_svg.py b/src/cq_cli/cqcodecs/cq_codec_svg.py index b01017c..7a9aff4 100644 --- a/src/cq_cli/cqcodecs/cq_codec_svg.py +++ b/src/cq_cli/cqcodecs/cq_codec_svg.py @@ -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, diff --git a/tests/test_stl_codec.py b/tests/test_stl_codec.py index 45ceab5..33fb3ac 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 " \ No newline at end of file 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", "") + == '' + ) From fa28edfbbfb72c182f9b6eeb209ee4c242c8bfe4 Mon Sep 17 00:00:00 2001 From: Jeremy Wright Date: Tue, 5 Dec 2023 11:10:52 -0500 Subject: [PATCH 2/2] Black formatting pass --- src/cq_cli/cqcodecs/cq_codec_stl.py | 4 +--- src/cq_cli/cqcodecs/cq_codec_svg.py | 5 +---- tests/test_stl_codec.py | 2 +- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/cq_cli/cqcodecs/cq_codec_stl.py b/src/cq_cli/cqcodecs/cq_codec_stl.py index 681ce15..3f67e64 100644 --- a/src/cq_cli/cqcodecs/cq_codec_stl.py +++ b/src/cq_cli/cqcodecs/cq_codec_stl.py @@ -30,9 +30,7 @@ def convert(build_result, output_file=None, error_file=None, output_opts=None): result = result.val() # Put the STL output into the temp file - result.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 7a9aff4..bc7d6be 100644 --- a/src/cq_cli/cqcodecs/cq_codec_svg.py +++ b/src/cq_cli/cqcodecs/cq_codec_svg.py @@ -19,10 +19,7 @@ def convert(build_result, output_file=None, error_file=None, output_opts=None): # Put the STEP output into the temp file exporters.export( - result, - 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 33fb3ac..d6f6648 100644 --- a/tests/test_stl_codec.py +++ b/tests/test_stl_codec.py @@ -75,4 +75,4 @@ def test_stl_codec_with_assembly(): ] out, err, exitcode = helpers.cli_call(command) - assert out.decode().split("\n")[0].replace("\r", "") == "solid " \ No newline at end of file + assert out.decode().split("\n")[0].replace("\r", "") == "solid "