Skip to content

Commit 80476cb

Browse files
committed
maintain the option for users to use string representations of paths
1 parent 36eafbc commit 80476cb

File tree

18 files changed

+202
-70
lines changed

18 files changed

+202
-70
lines changed

cadquery/assembly.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848

4949
PATH_DELIM = "/"
5050

51-
5251
# entity selector grammar definition
5352
def _define_grammar():
5453

@@ -509,7 +508,7 @@ def solve(self, verbosity: int = 0) -> Self:
509508
@deprecate()
510509
def save(
511510
self,
512-
path: Path,
511+
path: Path | str,
513512
exportType: Optional[ExportLiterals] = None,
514513
mode: STEPExportModeLiterals = "default",
515514
tolerance: float = 0.1,
@@ -530,13 +529,16 @@ def save(
530529
:type ascii: bool
531530
"""
532531

532+
if isinstance(path, str):
533+
path = Path(path)
534+
533535
return self.export(
534536
path, exportType, mode, tolerance, angularTolerance, **kwargs
535537
)
536538

537539
def export(
538540
self,
539-
path: Path,
541+
path: Path | str,
540542
exportType: Optional[ExportLiterals] = None,
541543
mode: STEPExportModeLiterals = "default",
542544
tolerance: float = 0.1,
@@ -557,6 +559,9 @@ def export(
557559
:type ascii: bool
558560
"""
559561

562+
if isinstance(path, str):
563+
path = Path(path)
564+
560565
# Make sure the export mode setting is correct
561566
if mode not in get_args(STEPExportModeLiterals):
562567
raise ValueError(f"Unknown assembly export mode {mode} for STEP")
@@ -593,22 +598,30 @@ def export(
593598
return self
594599

595600
@classmethod
596-
def importStep(cls, path: Path) -> Self:
601+
def importStep(cls, path: Path | str) -> Self:
597602
"""
598603
Reads an assembly from a STEP file.
599604
600605
:param path: Path and filename for reading.
601606
:return: An Assembly object.
602607
"""
603608

609+
if isinstance(path, str):
610+
path = Path(path)
611+
604612
return cls.load(path, importType="STEP")
605613

606614
@classmethod
607-
def load(cls, path: Path, importType: Optional[ImportLiterals] = None,) -> Self:
615+
def load(
616+
cls, path: Path | str, importType: Optional[ImportLiterals] = None,
617+
) -> Self:
608618
"""
609619
Load step, xbf or xml.
610620
"""
611621

622+
if isinstance(path, str):
623+
path = Path(path)
624+
612625
if importType is None:
613626
t = path.suffix.upper().lstrip(".")
614627
if t in ("STEP", "XML", "XBF"):

cadquery/cq.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4305,7 +4305,7 @@ def text(
43054305
combine: CombineMode = False,
43064306
clean: bool = True,
43074307
font: str = "Arial",
4308-
fontPath: Optional[str] = None,
4308+
fontPath: Optional[Path | str] = None,
43094309
kind: Literal["regular", "bold", "italic"] = "regular",
43104310
halign: Literal["center", "left", "right"] = "center",
43114311
valign: Literal["center", "top", "bottom"] = "center",
@@ -4353,6 +4353,10 @@ def text(
43534353
cq.Workplane().box(8, 8, 8).faces(">Z").workplane().text("Z", 5, -1.0)
43544354
43554355
"""
4356+
4357+
if isinstance(fontPath, str):
4358+
fontPath = Path(fontPath)
4359+
43564360
r = Compound.makeText(
43574361
txt,
43584362
fontsize,
@@ -4583,7 +4587,7 @@ def invoke(
45834587

45844588
def export(
45854589
self: T,
4586-
fname: Path,
4590+
fname: Path | str,
45874591
tolerance: float = 0.1,
45884592
angularTolerance: float = 0.1,
45894593
opt: Optional[Dict[str, Any]] = None,
@@ -4598,6 +4602,9 @@ def export(
45984602
:return: Self.
45994603
"""
46004604

4605+
if isinstance(fname, str):
4606+
fname = Path(fname)
4607+
46014608
export(
46024609
self, fname, tolerance=tolerance, angularTolerance=angularTolerance, opt=opt
46034610
)

cadquery/occ_impl/exporters/__init__.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ExportTypes:
4040

4141
def export(
4242
w: Union[Shape, Iterable[Shape]],
43-
fname: Path,
43+
fname: Path | str,
4444
exportType: Optional[ExportLiterals] = None,
4545
tolerance: float = 0.1,
4646
angularTolerance: float = 0.1,
@@ -57,6 +57,9 @@ def export(
5757
:param opt: additional options passed to the specific exporter. Default None.
5858
"""
5959

60+
if isinstance(fname, str):
61+
fname = Path(fname)
62+
6063
shape: Shape
6164
f: IO
6265

@@ -200,14 +203,15 @@ def tessellate(shape, angularTolerance):
200203
# all these types required writing to a file and then
201204
# re-reading. this is due to the fact that FreeCAD writes these
202205
(h, outFileName) = tempfile.mkstemp()
206+
outFileName = Path(outFileName) # type: ignore
203207
# weird, but we need to close this file. the next step is going to write to
204208
# it from c code, so it needs to be closed.
205209
os.close(h)
206210

207211
if exportType == ExportTypes.STEP:
208-
shape.exportStep(Path(outFileName))
212+
shape.exportStep(outFileName)
209213
elif exportType == ExportTypes.STL:
210-
shape.exportStl(Path(outFileName), tolerance, angularTolerance, True)
214+
shape.exportStl(outFileName, tolerance, angularTolerance, True)
211215
else:
212216
raise ValueError("No idea how i got here")
213217

@@ -216,7 +220,7 @@ def tessellate(shape, angularTolerance):
216220

217221

218222
@deprecate()
219-
def readAndDeleteFile(fileName):
223+
def readAndDeleteFile(fileName: Path):
220224
"""
221225
Read data from file provided, and delete it when done
222226
return the contents as a string
@@ -225,5 +229,5 @@ def readAndDeleteFile(fileName):
225229
with open(fileName, "r") as f:
226230
res = "{}".format(f.read())
227231

228-
os.remove(fileName)
232+
fileName.unlink()
229233
return res

cadquery/occ_impl/exporters/assembly.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class ExportModes:
5050

5151
def exportAssembly(
5252
assy: AssemblyProtocol,
53-
path: Path,
53+
path: Path | str,
5454
mode: STEPExportModeLiterals = "default",
5555
**kwargs,
5656
) -> bool:
@@ -78,6 +78,9 @@ def exportAssembly(
7878
:type precision_mode: int
7979
"""
8080

81+
if isinstance(path, str):
82+
path = Path(path)
83+
8184
# Handle the extra settings for the STEP export
8285
pcurves = 1
8386
if "write_pcurves" in kwargs and not kwargs["write_pcurves"]:
@@ -109,7 +112,7 @@ def exportAssembly(
109112

110113
def exportStepMeta(
111114
assy: AssemblyProtocol,
112-
path: Path,
115+
path: Path | str,
113116
write_pcurves: bool = True,
114117
precision_mode: int = 0,
115118
) -> bool:
@@ -129,6 +132,9 @@ def exportStepMeta(
129132
See OCCT documentation.
130133
"""
131134

135+
if isinstance(path, str):
136+
path = Path(path)
137+
132138
pcurves = 1
133139
if not write_pcurves:
134140
pcurves = 0
@@ -275,11 +281,14 @@ def _process_assembly(
275281
return status == IFSelect_ReturnStatus.IFSelect_RetDone
276282

277283

278-
def exportCAF(assy: AssemblyProtocol, path: Path, binary: bool = False) -> bool:
284+
def exportCAF(assy: AssemblyProtocol, path: Path | str, binary: bool = False) -> bool:
279285
"""
280286
Export an assembly to an XCAF xml or xbf file (internal OCCT formats).
281287
"""
282288

289+
if isinstance(path, str):
290+
path = Path(path)
291+
283292
folder = path.parent
284293
fname = path.name
285294
name = path.stem
@@ -338,11 +347,14 @@ def _vtkRenderWindow(
338347
return renderWindow
339348

340349

341-
def exportVTKJS(assy: AssemblyProtocol, path: Path):
350+
def exportVTKJS(assy: AssemblyProtocol, path: Path | str):
342351
"""
343352
Export an assembly to a zipped vtkjs. NB: .zip extensions is added to path.
344353
"""
345354

355+
if isinstance(path, str):
356+
path = Path(path)
357+
346358
renderWindow = _vtkRenderWindow(assy)
347359

348360
with TemporaryDirectory() as tmpdir:
@@ -356,14 +368,17 @@ def exportVTKJS(assy: AssemblyProtocol, path: Path):
356368

357369
def exportVRML(
358370
assy: AssemblyProtocol,
359-
path: Path,
371+
path: Path | str,
360372
tolerance: float = 1e-3,
361373
angularTolerance: float = 0.1,
362374
):
363375
"""
364376
Export an assembly to a vrml file using vtk.
365377
"""
366378

379+
if isinstance(path, str):
380+
path = Path(path)
381+
367382
exporter = vtkVRMLExporter()
368383
exporter.SetFileName(str(path))
369384
exporter.SetRenderWindow(_vtkRenderWindow(assy, tolerance, angularTolerance))
@@ -372,7 +387,7 @@ def exportVRML(
372387

373388
def exportGLTF(
374389
assy: AssemblyProtocol,
375-
path: Path,
390+
path: Path | str,
376391
binary: Optional[bool] = None,
377392
tolerance: float = 1e-3,
378393
angularTolerance: float = 0.1,
@@ -381,6 +396,9 @@ def exportGLTF(
381396
Export an assembly to a gltf file.
382397
"""
383398

399+
if isinstance(path, str):
400+
path = Path(path)
401+
384402
# If the caller specified the binary option, respect it
385403
if binary is None:
386404
# Handle the binary option for GLTF export based on file extension

cadquery/occ_impl/exporters/dxf.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def _dxf_spline(cls, edge: Edge, plane: Plane) -> DxfEntityAttributes:
367367

368368
def exportDXF(
369369
w: Union[WorkplaneLike, Shape, Iterable[Shape]],
370-
fname: Path,
370+
fname: Path | str,
371371
approx: Optional[ApproxOptions] = None,
372372
tolerance: float = 1e-3,
373373
*,
@@ -385,6 +385,9 @@ def exportDXF(
385385
:param doc_units: ezdxf document/modelspace :doc:`units <ezdxf-stable:concepts/units>` (in. = ``1``, mm = ``4``).
386386
"""
387387

388+
if isinstance(fname, str):
389+
fname = Path(fname)
390+
388391
dxf = DxfDocument(approx=approx, tolerance=tolerance, doc_units=doc_units)
389392

390393
if isinstance(w, (WorkplaneLike, Shape)):

cadquery/occ_impl/exporters/threemf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import datetime
2-
from os import PathLike
2+
from pathlib import Path
33
import xml.etree.cElementTree as ET
44
from typing import IO, List, Literal, Tuple, Union
55
from zipfile import ZipFile, ZIP_DEFLATED, ZIP_STORED
@@ -46,7 +46,7 @@ def __init__(
4646
self.tessellations = [t for t in tessellations if all(t)]
4747

4848
def write3mf(
49-
self, outfile: Union[PathLike, str, IO[bytes]],
49+
self, outfile: Union[Path, str, IO[bytes]],
5050
):
5151
"""
5252
Write to the given file.

cadquery/occ_impl/exporters/vtk.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44

55

66
def exportVTP(
7-
shape: Shape, fname: Path, tolerance: float = 0.1, angularTolerance: float = 0.1
7+
shape: Shape,
8+
fname: Path | str,
9+
tolerance: float = 0.1,
10+
angularTolerance: float = 0.1,
811
):
12+
if isinstance(fname, str):
13+
fname = Path(fname)
914

1015
writer = vtkXMLPolyDataWriter()
1116
writer.SetFileName(str(fname))

0 commit comments

Comments
 (0)