From ff1ec122996be480c4e2c6a321d9792c2ca2a85c Mon Sep 17 00:00:00 2001 From: Jeremy Wright Date: Fri, 27 Oct 2023 17:16:36 -0400 Subject: [PATCH 1/2] Added DXF codec --- src/cq_cli/cqcodecs/cq_codec_dxf.py | 24 ++++++++++++++++++++++++ src/cq_cli/cqcodecs/cq_codec_gltf.py | 2 -- src/cq_cli/cqcodecs/cq_codec_svg.py | 1 - tests/test_dxf_codec.py | 20 ++++++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 src/cq_cli/cqcodecs/cq_codec_dxf.py create mode 100644 tests/test_dxf_codec.py diff --git a/src/cq_cli/cqcodecs/cq_codec_dxf.py b/src/cq_cli/cqcodecs/cq_codec_dxf.py new file mode 100644 index 0000000..722e135 --- /dev/null +++ b/src/cq_cli/cqcodecs/cq_codec_dxf.py @@ -0,0 +1,24 @@ +import os, tempfile +from cadquery import exporters +import cq_cli.cqcodecs.codec_helpers as helpers + +def convert(build_result, output_file=None, error_file=None, output_opts=None): + # Create a temporary file to put the STL output into + temp_dir = tempfile.gettempdir() + temp_file = os.path.join(temp_dir, "temp_dxf.dxf") + + # The exporters will add extra output that we do not want, so suppress it + with helpers.suppress_stdout_stderr(): + # Put the DXF output into the temp file + exporters.export( + build_result.results[0].shape, + temp_file, + exporters.ExportTypes.DXF, + opt=output_opts, + ) + + # Read the DXF output back in + with open(temp_file, "r") as file: + dxf_str = file.read() + + return dxf_str diff --git a/src/cq_cli/cqcodecs/cq_codec_gltf.py b/src/cq_cli/cqcodecs/cq_codec_gltf.py index 867c737..3274be9 100644 --- a/src/cq_cli/cqcodecs/cq_codec_gltf.py +++ b/src/cq_cli/cqcodecs/cq_codec_gltf.py @@ -1,6 +1,4 @@ import os, tempfile -from cadquery import exporters -import cadquery as cq import cq_cli.cqcodecs.codec_helpers as helpers diff --git a/src/cq_cli/cqcodecs/cq_codec_svg.py b/src/cq_cli/cqcodecs/cq_codec_svg.py index e9b84a0..b01017c 100644 --- a/src/cq_cli/cqcodecs/cq_codec_svg.py +++ b/src/cq_cli/cqcodecs/cq_codec_svg.py @@ -1,6 +1,5 @@ import os, tempfile from cadquery import exporters -import cadquery as cq import cq_cli.cqcodecs.codec_helpers as helpers diff --git a/tests/test_dxf_codec.py b/tests/test_dxf_codec.py new file mode 100644 index 0000000..ffb4ad9 --- /dev/null +++ b/tests/test_dxf_codec.py @@ -0,0 +1,20 @@ +import tests.test_helpers as helpers + + +def test_dxf_codec(): + """ + Basic test of the DXF codec plugin. + """ + test_file = helpers.get_test_file_location("cube.py") + + command = [ + "python", + "src/cq_cli/main.py", + "--codec", + "dxf", + "--infile", + test_file, + ] + out, err, exitcode = helpers.cli_call(command) + + assert out.decode().split("\n")[1].replace("\r", "") == "SECTION" \ No newline at end of file From d084f8a9339586bf2cc8104fcb8adb0e06569b3f Mon Sep 17 00:00:00 2001 From: Jeremy Wright Date: Fri, 27 Oct 2023 17:25:06 -0400 Subject: [PATCH 2/2] Black formatting fix --- src/cq_cli/cqcodecs/cq_codec_dxf.py | 1 + tests/test_dxf_codec.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cq_cli/cqcodecs/cq_codec_dxf.py b/src/cq_cli/cqcodecs/cq_codec_dxf.py index 722e135..84e4f11 100644 --- a/src/cq_cli/cqcodecs/cq_codec_dxf.py +++ b/src/cq_cli/cqcodecs/cq_codec_dxf.py @@ -2,6 +2,7 @@ from cadquery import exporters import cq_cli.cqcodecs.codec_helpers as helpers + def convert(build_result, output_file=None, error_file=None, output_opts=None): # Create a temporary file to put the STL output into temp_dir = tempfile.gettempdir() diff --git a/tests/test_dxf_codec.py b/tests/test_dxf_codec.py index ffb4ad9..8992957 100644 --- a/tests/test_dxf_codec.py +++ b/tests/test_dxf_codec.py @@ -17,4 +17,4 @@ def test_dxf_codec(): ] out, err, exitcode = helpers.cli_call(command) - assert out.decode().split("\n")[1].replace("\r", "") == "SECTION" \ No newline at end of file + assert out.decode().split("\n")[1].replace("\r", "") == "SECTION"