Skip to content

Instantly share code, notes, and snippets.

@goncalomb
Last active February 15, 2023 23:03
Show Gist options
  • Save goncalomb/132cbb82148f542b1b5e7795907d003d to your computer and use it in GitHub Desktop.
Save goncalomb/132cbb82148f542b1b5e7795907d003d to your computer and use it in GitHub Desktop.
FreeCAD macro to normalize imported faces (tested with svg paths).
# Copyright (c) 2023 Gonçalo Baltazar <[email protected]>
# MIT License
# this macro can be used to manipulate face geometry (scale and origin vertex)
# it was specially made to rescale imported faces (e.g. svg)
# imported geometry may have undesired scale and position, depending on the
# original file (positions, translations, dpi etc.)
# after importing, select a single face vertex and run the macro
# it transforms the geometry of the face to set that vertex as the origin and
# scales it to match the requested size along the selected axis
# TODO: make dialog to configure the behavior
CREATE_COPY = False
SCALE_FINAL_SIZE = 100 # mm
SCALE_AXIS = "X"
ex = Gui.Selection.getSelectionEx()
if ex and ex[0].SubObjects and isinstance(ex[0].SubObjects[0], Part.Vertex):
object = ex[0].Object
vertex = ex[0].SubObjects[0]
label = object.Label
if CREATE_COPY:
object = App.ActiveDocument.copyObject(object, True)
object.Label = "%s_%smm%s" % (label, str(SCALE_FINAL_SIZE), SCALE_AXIS)
m = App.Matrix()
m.move(object.Placement.Base-vertex.Point)
m.scale(SCALE_FINAL_SIZE / getattr(object.Shape.BoundBox, SCALE_AXIS + "Length"))
object.Placement.Base = App.Vector()
object.Shape = object.Shape.transformGeometry(m)
else:
print("no valid selection")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment