cadquery icon indicating copy to clipboard operation
cadquery copied to clipboard

exportSVG on workplane with Sketch

Open truell20 opened this issue 2 years ago • 3 comments

Can sketches be exported to SVGs? I'm running this program:

import cadquery as cq

result = cq.Workplane("front").sketch().circle(1).parent
result.exportSvg('test.svg')

This is the error I get:

  File "/Users/mntruell/test_cq.py", line 5, in <module>
    result.exportSvg('test.svg')
  File "/usr/local/Caskroom/miniforge/base/envs/myenv/lib/python3.10/site-packages/cadquery/cq.py", line 1079, in exportSvg
    exportSVG(self, fileName)
  File "/usr/local/Caskroom/miniforge/base/envs/myenv/lib/python3.10/site-packages/cadquery/occ_impl/exporters/svg.py", line 290, in exportSVG
    svg = getSVG(shape.val(), opts)
  File "/usr/local/Caskroom/miniforge/base/envs/myenv/lib/python3.10/site-packages/cadquery/occ_impl/exporters/svg.py", line 167, in getSVG
    uom = guessUnitOfMeasure(shape)
  File "/usr/local/Caskroom/miniforge/base/envs/myenv/lib/python3.10/site-packages/cadquery/occ_impl/exporters/svg.py", line 66, in guessUnitOfMeasure
    bb = BoundBox._fromTopoDS(shape.wrapped)
AttributeError: 'Sketch' object has no attribute 'wrapped'

truell20 avatar Mar 05 '22 16:03 truell20

No, currently I don't think you can directly export the Sketch to SVG. You could try exporting the Face.

import cadquery as cq

s = cq.Sketch().circle(1)
cq.exporters.export(s._faces, "out.svg", opt={"projectionDir": (0, 0, 1)})

lorenzncode avatar Mar 05 '22 17:03 lorenzncode

That worked! Thanks.

truell20 avatar Mar 10 '22 17:03 truell20

It works in dxf pretty well. So this works:

cq.exporters.export(cq.Workplane().placeSketch(sketch), op.join(output_path, 'sketch.dxf'))

But not this:

cq.exporters.export(cq.Workplane().placeSketch(sketch), op.join(output_path, 'sketch.svg'))

I think that the ability to export sketches in svg is a common use case and it makes sense to simplify the process. Could this be reopened?

roipoussiere avatar Mar 23 '22 20:03 roipoussiere

Now with the Sketch val method:

import cadquery as cq

s = cq.Sketch().circle(1)
cq.exporters.export(s.faces().val(), "out.svg", opt={"projectionDir": (0, 0, 1)})

lorenzncode avatar Oct 05 '22 00:10 lorenzncode

Perfect! I suppose this can be closed now?

Also, maybe this solution could be mentioned in the export chapter of the documentation.

roipoussiere avatar Oct 06 '22 23:10 roipoussiere