-
Notifications
You must be signed in to change notification settings - Fork 381
use Path from pathlib everywhere. drop path dep #1909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
greyltc
wants to merge
2
commits into
CadQuery:master
Choose a base branch
from
greyltc:use-pathlib
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
from typish import instance_of | ||
from uuid import uuid1 as uuid | ||
from warnings import warn | ||
from pathlib import Path | ||
|
||
from .cq import Workplane | ||
from .occ_impl.shapes import Shape, Compound, isSubshape | ||
|
@@ -507,7 +508,7 @@ def solve(self, verbosity: int = 0) -> Self: | |
@deprecate() | ||
def save( | ||
self, | ||
path: str, | ||
path: Path | str, | ||
exportType: Optional[ExportLiterals] = None, | ||
mode: STEPExportModeLiterals = "default", | ||
tolerance: float = 0.1, | ||
|
@@ -528,13 +529,16 @@ def save( | |
:type ascii: bool | ||
""" | ||
|
||
if isinstance(path, str): | ||
path = Path(path) | ||
|
||
return self.export( | ||
path, exportType, mode, tolerance, angularTolerance, **kwargs | ||
) | ||
|
||
def export( | ||
self, | ||
path: str, | ||
path: Path | str, | ||
exportType: Optional[ExportLiterals] = None, | ||
mode: STEPExportModeLiterals = "default", | ||
tolerance: float = 0.1, | ||
|
@@ -555,12 +559,15 @@ def export( | |
:type ascii: bool | ||
""" | ||
|
||
if isinstance(path, str): | ||
path = Path(path) | ||
|
||
# Make sure the export mode setting is correct | ||
if mode not in get_args(STEPExportModeLiterals): | ||
raise ValueError(f"Unknown assembly export mode {mode} for STEP") | ||
|
||
if exportType is None: | ||
t = path.split(".")[-1].upper() | ||
t = path.suffix.upper().lstrip(".") | ||
if t in ("STEP", "XML", "XBF", "VRML", "VTKJS", "GLTF", "GLB", "STL"): | ||
exportType = cast(ExportLiterals, t) | ||
else: | ||
|
@@ -591,24 +598,32 @@ def export( | |
return self | ||
|
||
@classmethod | ||
def importStep(cls, path: str) -> Self: | ||
def importStep(cls, path: Path | str) -> Self: | ||
""" | ||
Reads an assembly from a STEP file. | ||
|
||
:param path: Path and filename for reading. | ||
:return: An Assembly object. | ||
""" | ||
|
||
if isinstance(path, str): | ||
path = Path(path) | ||
|
||
return cls.load(path, importType="STEP") | ||
|
||
@classmethod | ||
def load(cls, path: str, importType: Optional[ImportLiterals] = None,) -> Self: | ||
def load( | ||
cls, path: Path | str, importType: Optional[ImportLiterals] = None, | ||
) -> Self: | ||
""" | ||
Load step, xbf or xml. | ||
""" | ||
|
||
if isinstance(path, str): | ||
path = Path(path) | ||
|
||
if importType is None: | ||
t = path.split(".")[-1].upper() | ||
t = path.suffix.upper().lstrip(".") | ||
if t in ("STEP", "XML", "XBF"): | ||
importType = cast(ImportLiterals, t) | ||
else: | ||
|
@@ -680,8 +695,12 @@ def __iter__( | |
color = self.color if self.color else color | ||
|
||
if self.obj: | ||
yield self.obj if isinstance(self.obj, Shape) else Compound.makeCompound( | ||
s for s in self.obj.vals() if isinstance(s, Shape) | ||
yield ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this changed? |
||
self.obj | ||
if isinstance(self.obj, Shape) | ||
else Compound.makeCompound( | ||
s for s in self.obj.vals() if isinstance(s, Shape) | ||
) | ||
), name, loc, color | ||
|
||
for ch in self.children: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import tempfile | ||
import os | ||
import io as StringIO | ||
from pathlib import Path | ||
|
||
from typing import IO, Optional, Union, cast, Dict, Any, Iterable | ||
from typing_extensions import Literal | ||
|
@@ -39,13 +40,12 @@ class ExportTypes: | |
|
||
def export( | ||
w: Union[Shape, Iterable[Shape]], | ||
fname: str, | ||
fname: Path | str, | ||
exportType: Optional[ExportLiterals] = None, | ||
tolerance: float = 0.1, | ||
angularTolerance: float = 0.1, | ||
opt: Optional[Dict[str, Any]] = None, | ||
): | ||
|
||
""" | ||
Export Workplane or Shape to file. Multiple entities are converted to compound. | ||
|
||
|
@@ -57,6 +57,9 @@ def export( | |
:param opt: additional options passed to the specific exporter. Default None. | ||
""" | ||
|
||
if isinstance(fname, str): | ||
fname = Path(fname) | ||
|
||
shape: Shape | ||
f: IO | ||
|
||
|
@@ -69,7 +72,7 @@ def export( | |
shape = compound(*w) | ||
|
||
if exportType is None: | ||
t = fname.split(".")[-1].upper() | ||
t = fname.suffix.upper().lstrip(".") | ||
if t in ExportTypes.__dict__.values(): | ||
exportType = cast(ExportLiterals, t) | ||
else: | ||
|
@@ -121,7 +124,7 @@ def export( | |
|
||
elif exportType == ExportTypes.VRML: | ||
shape.mesh(tolerance, angularTolerance) | ||
VrmlAPI.Write_s(shape.wrapped, fname) | ||
VrmlAPI.Write_s(shape.wrapped, str(fname)) | ||
|
||
elif exportType == ExportTypes.VTP: | ||
exportVTP(shape, fname, tolerance, angularTolerance) | ||
|
@@ -200,6 +203,7 @@ def tessellate(shape, angularTolerance): | |
# all these types required writing to a file and then | ||
# re-reading. this is due to the fact that FreeCAD writes these | ||
(h, outFileName) = tempfile.mkstemp() | ||
outFileName = Path(outFileName) # type: ignore | ||
# weird, but we need to close this file. the next step is going to write to | ||
# it from c code, so it needs to be closed. | ||
os.close(h) | ||
|
@@ -216,7 +220,7 @@ def tessellate(shape, angularTolerance): | |
|
||
|
||
@deprecate() | ||
def readAndDeleteFile(fileName): | ||
def readAndDeleteFile(fileName: Path): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really needed? No changes to legacy functions please. |
||
""" | ||
Read data from file provided, and delete it when done | ||
return the contents as a string | ||
|
@@ -225,5 +229,5 @@ def readAndDeleteFile(fileName): | |
with open(fileName, "r") as f: | ||
res = "{}".format(f.read()) | ||
|
||
os.remove(fileName) | ||
fileName.unlink() | ||
return res |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or