Skip to content

Commit

Permalink
Revert "🐛 Fix save cad unit"
Browse files Browse the repository at this point in the history
This reverts commit a48ed9f.
Apparently this is post V1...
  • Loading branch information
je-cook committed Nov 21, 2024
1 parent 8180ad8 commit 2e465e0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
24 changes: 22 additions & 2 deletions bluemira/codes/_freecadapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,9 @@ def wrapper(objs: Part.Feature, filename: str, *, tessellate: float = 0.5, **kwa
return wrapper # noqa: DOC201


def save_as_STP(shapes: list[apiShape], filename: str = "test"):
def save_as_STP(
shapes: list[apiShape], filename: str = "test", unit_scale: str = "metre"
):
"""
Saves a series of Shape objects as a STEP assembly
Expand All @@ -1644,6 +1646,8 @@ def save_as_STP(shapes: list[apiShape], filename: str = "test"):
Iterable of shape objects to be saved
filename:
Full path filename of the STP assembly
unit_scale:
The scale in which to save the Shape objects
Raises
------
Expand All @@ -1667,6 +1671,13 @@ def save_as_STP(shapes: list[apiShape], filename: str = "test"):
raise FreeCADError("Shape is null.")

compound = make_compound(shapes)
scale = raw_uc(1, unit_scale, "mm")

if scale != 1:
# scale the compound. Since the scale function modifies directly the shape,
# a copy of the compound is made to avoid modification of the original shapes.
compound = scale_shape(compound.copy(), scale)

compound.exportStep(filename)


Expand All @@ -1691,6 +1702,7 @@ def save_cad(
filename: str,
cad_format: str | CADFileType = "stp",
labels: Iterable[str] | None = None,
unit_scale: str = "metre",
**kwargs,
):
"""
Expand All @@ -1706,6 +1718,8 @@ def save_cad(
file cad_format
labels:
shape labels
unit_scale:
unit to save the objects as.
kwargs:
passed to freecad preferences configuration
Expand Down Expand Up @@ -1735,8 +1749,14 @@ def save_cad(
})

with Document() as doc:
objs = list(doc.setup(shapes, labels))

# Part is always built in mm but some formats are unitless
if cad_format not in CADFileType.unitless_formats():
_scale_obj(objs, scale=raw_uc(1, unit_scale, "mm"))

try:
cad_format.exporter(list(doc.setup(shapes, labels)), filename, **kwargs)
cad_format.exporter(objs, filename, **kwargs)
except ImportError as imp_err:
raise FreeCADError(

Check warning on line 1761 in bluemira/codes/_freecadapi.py

View check run for this annotation

Codecov / codecov/patch

bluemira/codes/_freecadapi.py#L1760-L1761

Added lines #L1760 - L1761 were not covered by tests
f"Unable to save to {cad_format.value} please try through the main"
Expand Down
8 changes: 5 additions & 3 deletions bluemira/codes/_freecadconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import FreeCAD


class FCUnit(enum.IntEnum):
class _Unit(enum.IntEnum):
"""Available units in FreeCAD"""

MM = 0 # mmKS
Expand Down Expand Up @@ -50,7 +50,9 @@ def _freecad_save_config(
This must be run before Part is imported for legacy exporters
"""
unit_prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Units")
unit_prefs.SetInt("UserSchema", FCUnit[unit].value)
# Seems to have little effect on anything but its an option to set
# does effect the GUI be apparently not the base unit of the built part...
unit_prefs.SetInt("UserSchema", _Unit[unit].value)
unit_prefs.SetInt("Decimals", no_dp) # 100th mm

part_step_prefs = FreeCAD.ParamGet(
Expand All @@ -60,7 +62,7 @@ def _freecad_save_config(
part_step_prefs.SetString("Author", author)
part_step_prefs.SetString("Company", "Bluemira")
# Seems to have little effect on anything but its an option to set
part_step_prefs.SetInt("Unit", FCUnit[unit].value)
part_step_prefs.SetInt("Unit", _Unit[unit].value)

part_step_2_prefs = FreeCAD.ParamGet(
"User parameter:BaseApp/Preferences/Mod/Import/hSTEP"
Expand Down
5 changes: 4 additions & 1 deletion bluemira/geometry/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,7 @@ def is_convex(points: npt.NDArray):
def save_as_STP(
shapes: BluemiraGeoT | Iterable[BluemiraGeoT],
filename: str,
unit_scale: str = "metre",
**kwargs,
):
"""
Expand All @@ -1583,13 +1584,15 @@ def save_as_STP(
List of shape objects to be saved
filename:
Full path filename of the STP assembly
unit_scale:
The scale in which to save the Shape objects
"""
filename = force_file_extension(filename, [".stp", ".step"])

if not isinstance(shapes, list):
shapes = list(shapes) if isinstance(shapes, Iterable) else [shapes]

cadapi.save_as_STP([s.shape for s in shapes], filename, **kwargs)
cadapi.save_as_STP([s.shape for s in shapes], filename, unit_scale, **kwargs)


def save_cad(
Expand Down
1 change: 1 addition & 0 deletions examples/geometry/geometry_tutorial.ex.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,5 @@
filename=Path(
get_bluemira_path("", subfolder="generated_data"), my_file_path
).as_posix(),
unit_scale="metre",
)

0 comments on commit 2e465e0

Please sign in to comment.