Move Face (cube remains parametric)

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
clarkitect
Posts: 39
Joined: Sat Sep 19, 2015 1:57 pm

Move Face (cube remains parametric)

Post by clarkitect »

Parametric Move Face.gif
Parametric Move Face.gif (409.19 KiB) Viewed 1514 times
A bit of a fusion between direct modelling and parametric modelling here... I frequently need to move faces of cubes to line up with other faces in simple ways (because I like to use part cubes as a base object for multiple sketches). I often found myself taking measurements from one face to another, then manually updating placement/dimensions accordingly, which is a bit too much like hard work for me. This macro does the job in a click.

Code: Select all

Selection = FreeCADGui.Selection.getSelectionEx()
TargetReferencePoint = Selection[1].SubObjects[0].CenterOfMass
BasePoint = Selection[0].SubObjects[0].CenterOfMass
BaseNormal = Selection[0].SubObjects[0].normalAt(0,0)
TargetPoint = Selection[0].SubObjects[0].CenterOfMass.projectToPlane(TargetReferencePoint,BaseNormal)
Difference = TargetReferencePoint.distanceToPlane(BasePoint,BaseNormal)
TargetVector = TargetPoint.sub(BasePoint)

if Selection[0].SubElementNames[0] == 'Face1':
    Selection[0].Object.Length = Selection[0].Object.Length.Value + Difference
    Selection[0].Object.Placement.Base = Selection[0].Object.Placement.Base.add(TargetVector)
elif Selection[0].SubElementNames[0] == 'Face2':
    Selection[0].Object.Length = Selection[0].Object.Length.Value + Difference
elif Selection[0].SubElementNames[0] == 'Face3':
    Selection[0].Object.Width = Selection[0].Object.Width.Value + Difference
    Selection[0].Object.Placement.Base = Selection[0].Object.Placement.Base.add(TargetVector)
elif Selection[0].SubElementNames[0] == 'Face4':
    Selection[0].Object.Width = Selection[0].Object.Width.Value + Difference
elif Selection[0].SubElementNames[0] == 'Face5':
    Selection[0].Object.Height = Selection[0].Object.Height.Value + Difference
    Selection[0].Object.Placement.Base = Selection[0].Object.Placement.Base.add(TargetVector)
else: Selection[0].Object.Height = Selection[0].Object.Height.Value + Difference

Usage:
1. Select BOTH the face you want to change, THEN the target thing
2. Run the macro

Limitations:
-Only works on standard cubes
-First object selected must be a face. The second object can be a face or an edge, but not a vertex
Post Reply