Macro PlacementAbsolufy
Description |
---|
This macro resets position of all part containers to document origin while keeping the absolute object positions Macro version: 0.2 Last modified: 2019-06-10 FreeCAD version: 0.17+ Author: OpenBrain |
Author |
OpenBrain |
Download |
None |
Links |
Macros recipes How to install macros How to customize toolbars |
Macro Version |
0.2 |
Date last modified |
2019-06-10 |
FreeCAD Version(s) |
0.17+ |
Default shortcut |
None |
See also |
None |
Description
Context
This macro has been written mainly to circumvent unfinished Part container implementation that can lead to absolute position shift, mainly when exporting parts. This is due to fact that Part containers creates a local coordinate system that can be shifted from global one. This local referential is then used by subsequent objects but isn't correctly handled by several functions (eg. export).
Usage
Functionally, the macro will reset the Part containers placement to global origin while preserving the objects absolute position. Notice that PlacementAbsolufy macro applies to the whole active document.
To use the macro, just run it when the document on which it shall be applied is open.
Installation
At the moment, the macro isn't available from the addon manager => PR submitted : https://github.com/FreeCAD/FreeCAD-macros/pull/50 So you have to copy the below code and paste it in FreeCAD macro editor.
For more detailed explanations, see the How to install macros page.
Script
Limitations
- Process the whole open document
Forum discussion
For any feedback (bug, feature request, comments, ...), please use this forum thread : https://forum.freecadweb.org/viewtopic.php?f=3&t=36869
Code
#!/usr/bin/python
#####################################
# Copyright (c) openBrain 2019
# Licensed under LGPL v2
#
# This macro will reset position of all part containers to document origin while keeping the absolute object positions
#
# Version history :
# *0.1 : alpha release, almost no test performed
# *0.2 : some typo improvement + commenting for official PR
#
#####################################
__Name__ = 'PlacementAbsolufy'
__Comment__ = 'Reset part containers to global origin while keeping object positions'
__Author__ = 'openBrain'
__Version__ = '0.2'
__Date__ = '2019-06-10'
__License__ = 'LGPL v2'
__Web__ = 'https://www.freecadweb.org/wiki/Macro_PlacementAbsolufy'
__Wiki__ = 'https://www.freecadweb.org/wiki/Macro_PlacementAbsolufy'
__Icon__ = ''
__Help__ = 'Run the macro with model active in the GUI'
__Status__ = 'Alpha'
__Requires__ = 'FreeCAD >= 0.17'
__Communication__ = 'https://forum.freecadweb.org/viewtopic.php?f=3&t=36869'
__Files__ = ''
currState = {} #initialize a dictionary to store current object placements
for obj in App.ActiveDocument.Objects: #going through active document objects
if "Placement" in obj.PropertiesList: #if object has a Placement property
currState[obj] = obj.getGlobalPlacement() #store the object pointer with its global placement
App.ActiveDocument.openTransaction("Absolufy") #open a transaction for undo management
for obj, plac in currState.items(): #going through all moveable objects
if obj.isDerivedFrom("App::Part"): #if object is a part container
obj.Placement = App.Placement(App.Vector(0,0,0),App.Rotation(0,0,0)) #reset its placement to global document origin
elif obj.TypeId[:5] == "App::": #if object is another App type (typically an origin axis or plane)
None #do nothing
else: #for all other objects
obj.Placement = plac #replace them at their global (absolute) placement
App.ActiveDocument.commitTransaction() #commit transaction