diff --git a/bluemira/codes/_freecadapi.py b/bluemira/codes/_freecadapi.py index fdc9e73b89..d90d5a65d9 100644 --- a/bluemira/codes/_freecadapi.py +++ b/bluemira/codes/_freecadapi.py @@ -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 @@ -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 ------ @@ -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) @@ -1691,6 +1702,7 @@ def save_cad( filename: str, cad_format: str | CADFileType = "stp", labels: Iterable[str] | None = None, + unit_scale: str = "metre", **kwargs, ): """ @@ -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 @@ -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( f"Unable to save to {cad_format.value} please try through the main" diff --git a/bluemira/codes/_freecadconfig.py b/bluemira/codes/_freecadconfig.py index 7dca9ea142..71b89a3111 100644 --- a/bluemira/codes/_freecadconfig.py +++ b/bluemira/codes/_freecadconfig.py @@ -14,7 +14,7 @@ import FreeCAD -class FCUnit(enum.IntEnum): +class _Unit(enum.IntEnum): """Available units in FreeCAD""" MM = 0 # mmKS @@ -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( @@ -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" diff --git a/bluemira/geometry/tools.py b/bluemira/geometry/tools.py index 64d13e7231..8059b1b83c 100644 --- a/bluemira/geometry/tools.py +++ b/bluemira/geometry/tools.py @@ -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, ): """ @@ -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( diff --git a/examples/geometry/geometry_tutorial.ex.py b/examples/geometry/geometry_tutorial.ex.py index ee44b66e1d..f84d4e08a0 100644 --- a/examples/geometry/geometry_tutorial.ex.py +++ b/examples/geometry/geometry_tutorial.ex.py @@ -403,4 +403,5 @@ filename=Path( get_bluemira_path("", subfolder="generated_data"), my_file_path ).as_posix(), + unit_scale="metre", )