Macro Align Object BoundBox Center
Description |
---|
Aligns 2 (or more) objects by the center of their bounding boxes. Macro version: 0.1 Last modified: 2024-10-23 Author: heda |
Author |
heda |
Download |
None |
Links |
Macros recipes How to install macros How to customize toolbars |
Macro Version |
0.1 |
Date last modified |
2024-10-23 |
FreeCAD Version(s) |
Default shortcut |
None |
See also |
None |
Description
This macro aligns 2 (or more) objects by the center of their bounding boxes.
Macro Align Object BoundBox Center
Usage
Select and launch.
Install
Through the Addon manager.
Version
v0.1 2024-10-23: First release.
Code
Macro_Align_Object_BoundBox_Center.FCMacro
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # *************************************************************************** # * Copyright (c) 2024 heda <heda @ freecad forum> * # * * # * This file is part of the FreeCAD CAx development system. * # * * # * This program is free software; you can redistribute it and/or modify * # * it under the terms of the GNU Lesser General Public License (LGPL) * # * as published by the Free Software Foundation; either version 2 of * # * the License, or (at your option) any later version. * # * for detail see the LICENCE text file. * # * * # * This program is distributed in the hope that it will be useful, * # * but WITHOUT ANY WARRANTY; without even the implied warranty of * # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * # * GNU Library General Public License for more details. * # * * # * You should have received a copy of the GNU Library General Public * # * License along with this program; if not, write to the Free Software * # * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * # * USA * # * * # *************************************************************************** __Name__ = 'Macro_Align_Object_BoundBox_Center' __Comment__ = 'Aligns 2 (or more) objects by bounding box' __Author__ = 'heda @ fc-forum' __Version__ = '0.1' __Date__ = '2024-10-23' __License__ = 'LGPL-2.0-or-later' __Web__ = '' __Wiki__ = 'https://wiki.freecad.org/Macro_Align_Object_BoundBox_Center' __Icon__ = '' __Help__ = 'Select and launch.' __Status__ = 'functional' __Requires__ = 'tested on FreeCAD v0.21' __Communication__ = '' __Files__ = '' __doc__ = """ select 2 or more objects and run macro. first object is reference, and all subsequent selections will get a placement that has the centre at same location as the reference object. undo transaction is commented out, since it moves the object back twice... """ import FreeCAD as App import FreeCADGui as Gui doc = App.ActiveDocument sel = Gui.Selection.getSelection() if len(sel) > 1: ref, *moves = sel # doc.openTransaction('BoundBox align objects') for move in moves: vec = ref.Shape.BoundBox.Center - move.Shape.BoundBox.Center move.Placement.Base = move.Placement.Base + vec # doc.commitTransaction() doc.recompute() txt = [obj.Label for obj in moves] print('moved {} to {}'.format(', '.join(txt), ref.Label))