Skip to content

Commit 0f32de9

Browse files
authored
Merge pull request #668 from CadQuery/docs-update
Adding dedicated import-export doc
2 parents dc9b0e7 + 45227c9 commit 0f32de9

File tree

6 files changed

+351
-0
lines changed

6 files changed

+351
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Don't display text diffs for files that are not human readable
22
# Git should not modify line endings in these test files
33
tests/testdata/* -diff -text
4+
*.svg -diff -text
45

56
# Jupyter notebooks should be classified as documentation
67
*.ipynb linguist-documentation -diff -text

cadquery/occ_impl/shapes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,16 @@ def cast(
399399
def exportStl(
400400
self, fileName: str, tolerance: float = 1e-3, angularTolerance: float = 0.1
401401
) -> bool:
402+
"""
403+
Exports a shape to a specified STL file.
404+
405+
:param fileName: The path and file name to write the STL output to.
406+
:type fileName: fileName
407+
:param tolerance: A linear deflection setting which limits the distance between a curve and its tessellation. Setting this value too low will result in large meshes that can consume computing resources. Setting the value too high can result in meshes with a level of detail that is too low. Default is 0.1, which is good starting point for a range of cases.
408+
:type tolerance: float
409+
:param angularTolerance: - Angular deflection setting which limits the angle between subsequent segments in a polyline. Default is 0.1.
410+
:type angularTolerance: float
411+
"""
402412

403413
mesh = BRepMesh_IncrementalMesh(self.wrapped, tolerance, True, angularTolerance)
404414
mesh.Perform()
Lines changed: 33 additions & 0 deletions
Loading
Lines changed: 46 additions & 0 deletions
Loading

doc/importexport.rst

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
.. _importexport:
2+
3+
******************************
4+
Importing and Exporting Files
5+
******************************
6+
7+
Introduction
8+
============
9+
10+
The purpose of this section is to explain how to import external file formats into CadQuery, and export files from
11+
it as well. While the external file formats can be used to interchange CAD model data with other software, CadQuery
12+
does not support any formats that carry parametric data with them at this time. The only format that is fully
13+
parametric is CadQuery's own Python format. Below are lists of the import and export file formats that CadQuery
14+
supports.
15+
16+
Import Formats
17+
###############
18+
19+
* DXF
20+
* STEP
21+
22+
Export Formats
23+
###############
24+
25+
* DXF
26+
* SVG
27+
* STEP
28+
* STL
29+
* AMF
30+
* TJS
31+
* VRML
32+
33+
Notes on the Formats
34+
#######################
35+
36+
* DXF is useful for importing complex 2D profiles that would be tedious to create using CadQuery's 2D operations. An example is that the 2D profiles of aluminum extrusion are often provided in DXF format. These can be imported and extruded to create the length of extrusion that is needed in a design.
37+
* STEP files are useful for interchanging model data with other CAD and analysis systems, such as FreeCAD. Many parts such as screws have STEP files available, which can be imported and used in CadQuery assemblies.
38+
* STL and AMF files are mesh-based formats which are typically used in additive manufacturing (i.e. 3D printing). AMF files support more features, but are not as universally supported as STL files.
39+
* TJS is short for ThreeJS, and is a JSON mesh format that is useful for displaying 3D models in web browsers. The TJS format is used to display embedded 3D examples within the CadQuery documentation.
40+
* VRML is a mesh-based format for representing interactive 3D objects in a web browser.
41+
42+
Importing DXF
43+
##############
44+
45+
DXF files can be imported using the :py:meth:`importers.importDXF` method.
46+
47+
.. automethod::
48+
cadquery.importers.importDXF
49+
50+
Importing a DXF profile with default settings and using it within a CadQuery script is shown in the following code.
51+
52+
.. code-block:: python
53+
54+
import cadquery as cq
55+
56+
result = (
57+
cq.importers.importDXF('/path/to/dxf/circle.dxf')
58+
.wires().toPending()
59+
.extrude(10)
60+
)
61+
62+
Note the use of the :py:meth:`Workplane.wires` and :py:meth:`Workplane.toPending` methods to make the DXF profile
63+
ready for use during subsequent operations. Calling ``toPending()`` tells CadQuery to make the edges/wires available
64+
to the next modelling operation that is called in the chain.
65+
66+
Importing STEP
67+
###############
68+
69+
STEP files can be imported using the :py:meth:`importers.importStep` method (note the capitalization of "Step").
70+
There are no parameters for this method other than the file path to import.
71+
72+
.. code-block:: python
73+
74+
import cadquery as cq
75+
76+
result = cq.importers.importStep('/path/to/step/block.stp')
77+
78+
Exporting SVG
79+
##############
80+
81+
The SVG exporter has several options which can be useful for achieving the desired final output. Those
82+
options are as follows.
83+
84+
* *width* - Document width of the resulting image.
85+
* *height* - Document height of the resulting image.
86+
* *marginLeft* - Inset margin from the left side of the document.
87+
* *marginTop* - Inset margin from the top side of the document.
88+
* *projectionDir* - Direction the camera will view the shape from.
89+
* *showAxes* - Whether or not to show the axes indicator, which will only be visible when the projectionDir is also at the default.
90+
* *strokeWidth* - Width of the line that visible edges are drawn with.
91+
* *strokeColor* - Color of the line that visible edges are drawn with.
92+
* *hiddenColor* - Color of the line that hidden edges are drawn with.
93+
* *showHidden* - Whether or not to show hidden lines.
94+
95+
The options are passed to the exporter in a dictionary, and can be left out to force the SVG to be created with default options.
96+
Below are examples with and without options set.
97+
98+
Without options:
99+
100+
.. code-block:: python
101+
102+
import cadquery as cq
103+
from cadquery import exporters
104+
105+
result = cq.Workplane().box(10, 10, 10)
106+
107+
exporters.export(result, '/path/to/file/box.svg')
108+
109+
Results in:
110+
111+
.. image:: _static/importexport/box_default_options.svg
112+
113+
Note that the exporters API figured out the format type from the file extension. The format
114+
type can be set explicitly by using :py:class:`exporters.ExportTypes`.
115+
116+
The following is an example of using options to alter the resulting SVG output by passing in the ``opt`` parameter.
117+
118+
.. code-block:: python
119+
120+
import cadquery as cq
121+
from cadquery import exporters
122+
123+
result = cq.Workplane().box(10, 10, 10)
124+
125+
exporters.export(
126+
result,
127+
'/path/to/file/box_custom_options.svg',
128+
opt={
129+
"width": 300,
130+
"height": 300,
131+
"marginLeft": 10,
132+
"marginTop": 10,
133+
"showAxes": False,
134+
"projectionDir": (0.5, 0.5, 0.5),
135+
"strokeWidth": 0.25,
136+
"strokeColor": (255, 0, 0),
137+
"hiddenColor": (0, 0, 255),
138+
"showHidden": True,
139+
},
140+
)
141+
142+
Which results in the following image:
143+
144+
.. image:: _static/importexport/box_custom_options.svg
145+
146+
Exporting STL
147+
##############
148+
149+
The STL exporter is capable of adjusting the quality of the resulting mesh, and accepts the following parameters.
150+
151+
.. automethod::
152+
cadquery.occ_impl.shapes.Shape.exportStl
153+
154+
For more complex objects, some experimentation with ``tolerance`` and ``angularTolerance`` may be required to find the
155+
optimum values that will produce an acceptable mesh.
156+
157+
.. code-block:: python
158+
159+
import cadquery as cq
160+
from cadquery import exporters
161+
162+
result = cq.Workplane().box(10, 10, 10)
163+
164+
exporters.export(result, '/path/to/file/mesh.stl')
165+
166+
Exporting AMF
167+
##############
168+
169+
The AMF exporter is capable of adjusting the quality of the resulting mesh, and accepts the following parameters.
170+
171+
* ``fileName`` - The path and file name to write the AMF output to.
172+
* ``tolerance`` - A linear deflection setting which limits the distance between a curve and its tessellation. Setting this value too low will result in large meshes that can consume computing resources. Setting the value too high can result in meshes with a level of detail that is too low. Default is 0.1, which is good starting point for a range of cases.
173+
* ``angularTolerance`` - Angular deflection setting which limits the angle between subsequent segments in a polyline. Default is 0.1.
174+
175+
For more complex objects, some experimentation with ``tolerance`` and ``angularTolerance`` may be required to find the
176+
optimum values that will produce an acceptable mesh. Note that parameters for AMF color and material are absent.
177+
178+
.. code-block:: python
179+
180+
import cadquery as cq
181+
from cadquery import exporters
182+
183+
result = cq.Workplane().box(10, 10, 10)
184+
185+
exporters.export(result, '/path/to/file/mesh.amf', tolerance=0.01, angularTolerance=0.1)
186+
187+
188+
Exporting TJS
189+
##############
190+
191+
The TJS (ThreeJS) exporter produces a file in JSON format that describes a scene for the ThreeJS WebGL renderer. The objects in the first argument are converted into a mesh and then form the ThreeJS geometry for the scene. The mesh can be adjusted with the following parameters.
192+
193+
* ``fileName`` - The path and file name to write the ThreeJS output to.
194+
* ``tolerance`` - A linear deflection setting which limits the distance between a curve and its tessellation. Setting this value too low will result in large meshes that can consume computing resources. Setting the value too high can result in meshes with a level of detail that is too low. Default is 0.1, which is good starting point for a range of cases.
195+
* ``angularTolerance`` - Angular deflection setting which limits the angle between subsequent segments in a polyline. Default is 0.1.
196+
197+
For more complex objects, some experimentation with ``tolerance`` and ``angularTolerance`` may be required to find the
198+
optimum values that will produce an acceptable mesh.
199+
200+
.. code-block:: python
201+
202+
import cadquery as cq
203+
from cadquery import exporters
204+
205+
result = cq.Workplane().box(10, 10, 10)
206+
207+
exporters.export(result, '/path/to/file/mesh.json', tolerance=0.01, angularTolerance=0.1, exportType=exporters.ExportTypes.TJS)
208+
209+
Note that the export type was explicitly specified as ``TJS`` because the extension that was used for the file name was ``.json``. If the extension ``.tjs``
210+
had been used, CadQuery would have understood to use the ``TJS`` export format.
211+
212+
Exporting VRML
213+
###############
214+
215+
The VRML exporter is capable of adjusting the quality of the resulting mesh, and accepts the following parameters.
216+
217+
* ``fileName`` - The path and file name to write the VRML output to.
218+
* ``tolerance`` - A linear deflection setting which limits the distance between a curve and its tessellation. Setting this value too low will result in large meshes that can consume computing resources. Setting the value too high can result in meshes with a level of detail that is too low. Default is 0.1, which is good starting point for a range of cases.
219+
* ``angularTolerance`` - Angular deflection setting which limits the angle between subsequent segments in a polyline. Default is 0.1.
220+
221+
For more complex objects, some experimentation with ``tolerance`` and ``angularTolerance`` may be required to find the
222+
optimum values that will produce an acceptable mesh.
223+
224+
.. code-block:: python
225+
226+
import cadquery as cq
227+
from cadquery import exporters
228+
229+
result = cq.Workplane().box(10, 10, 10)
230+
231+
exporters.export(result, '/path/to/file/mesh.vrml', tolerance=0.01, angularTolerance=0.1)
232+
233+
Exporting Other Formats
234+
########################
235+
236+
The remaining export formats do not accept any additional parameters other than file name, and can be exported
237+
using the following structure.
238+
239+
.. code-block:: python
240+
241+
import cadquery as cq
242+
from cadquery import exporters
243+
244+
result = cq.Workplane().box(10, 10, 10)
245+
246+
exporters.export(result, '/path/to/file/object.[file_extension]')
247+
248+
Be sure to use the correct file extension so that CadQuery can determine the export format. If in doubt, fall
249+
back to setting the type explicitly by using :py:class:`exporters.ExportTypes`.
250+
251+
For example:
252+
253+
.. code-block:: python
254+
255+
import cadquery as cq
256+
from cadquery import exporters
257+
258+
result = cq.Workplane().box(10, 10, 10)
259+
260+
exporters.export(result, '/path/to/file/object.dxf', exporters.ExportTypes.DXF)

doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Table Of Contents
4444
apireference.rst
4545
selectors.rst
4646
classreference.rst
47+
importexport.rst
4748
cqgi.rst
4849
extending.rst
4950
roadmap.rst

0 commit comments

Comments
 (0)