Macro Center Align Objects with Faces or Edges

From FreeCAD Documentation
Revision as of 19:07, 14 September 2017 by Renatorivo (talk | contribs)

File:Macro Center Align Objects with Faces or Edges Center Faces of Parts

Description
This macro Aligns Objects through Faces or Edges constraints

Macro version: 0.32 (11.2016)
Author: easyw-fc
Author
easyw-fc
Download
None
Links
Macro Version
0.32 (11.2016)
Date last modified
None
FreeCAD Version(s)
None
Default shortcut
None
See also
None

Description

This macro covers the following constraints:
- Concentric constraint among non cylindrical parts;
- Constraint on center Faces and/or Edges.

Aligning tool video tutorial

Use

Faces or Edges constraint among non cylindrical parts: Just open a FC document, launch the Macro and select two or more Faces/Edges to be aligned. Click on Align button and that's it!

Script

The icon for your toolbar

Macro Center Faces of Parts.FCMacro

After downloading the file here
GitHub page
https://github.com/easyw/FreeCAD_Macros/blob/master/Align%20Objects/CenterAlignObjectswFacesEdges.py
code:
https://github.com/easyw/FreeCAD_Macros/raw/master/Align%20Objects/CenterAlignObjectswFacesEdges.py
you need to copy the file to your macro directory.
How to install macros

Link

Forum : Faces or Edges constraint among non cylindrical parts Macro

Macro_Center_Align_Objects_with_Faces_or_Edges

# -*- coding: utf-8 -*-

# evolution of Macro_CenterFace
# some part of Macro WorkFeature
# and assembly2
#
# center objs faces/closed_edges to first obj face/closed_edge
#
 
__title__   = "Center Faces of Parts"
__author__  = "maurice"
__url__     = "kicad stepup"
__version__ = "0.38" #undo alignement
__date__    = "09.2017"

## todo 
## align edges when are just a line
## use faces when available?
## make Undo doing opposite Moved, Rotated on objs (prob it is enough the Rotation plus actual undo)
## align PartDN and Body using simplecopy plcm of faces

# * (C) Maurice easyw-fc 2016
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Library 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.                                 *

## done case: invert normal and standard when already aligned planes
## done works for Bodys on FC 0.17

import FreeCAD, FreeCADGui, Draft, Part, DraftTools
from FreeCAD import Base
import sys
testing=False #true for showing helpers

# Form implementation generated from reading ui file 'C:\Cad\Progetti_K\3D-FreeCad-tools\CenterAlignObjectswFacesEdges.ui'
#
# Created: Wed Sep 13 22:20:26 2017
#      by: pyside-uic 0.2.15 running on PySide 1.2.2
#
# WARNING! All changes made in this file will be lost!

from PySide import QtCore, QtGui

class Ui_CenterAlignObjectsFacesEdges(object):
    def setupUi(self, CenterAlignObjectsFacesEdges):
        CenterAlignObjectsFacesEdges.setObjectName("CenterAlignObjectsFacesEdges")
        CenterAlignObjectsFacesEdges.setWindowModality(QtCore.Qt.NonModal)
        CenterAlignObjectsFacesEdges.resize(475, 351)
        CenterAlignObjectsFacesEdges.setFocusPolicy(QtCore.Qt.NoFocus)
        self.verticalLayout = QtGui.QVBoxLayout(CenterAlignObjectsFacesEdges)
        self.verticalLayout.setObjectName("verticalLayout")
        self.verticalLayout_2 = QtGui.QVBoxLayout()
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.lbl_info = QtGui.QLabel(CenterAlignObjectsFacesEdges)
        font = QtGui.QFont()
        font.setWeight(75)
        font.setBold(True)
        self.lbl_info.setFont(font)
        self.lbl_info.setObjectName("lbl_info")
        self.verticalLayout_2.addWidget(self.lbl_info)
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.groupBox = QtGui.QGroupBox(CenterAlignObjectsFacesEdges)
        self.groupBox.setObjectName("groupBox")
        self.rb_bb = QtGui.QRadioButton(self.groupBox)
        self.rb_bb.setGeometry(QtCore.QRect(11, 20, 241, 20))
        self.rb_bb.setChecked(True)
        self.rb_bb.setObjectName("rb_bb")
        self.rb_mass = QtGui.QRadioButton(self.groupBox)
        self.rb_mass.setGeometry(QtCore.QRect(300, 20, 181, 20))
        self.rb_mass.setObjectName("rb_mass")
        self.horizontalLayout.addWidget(self.groupBox)
        self.verticalLayout_2.addLayout(self.horizontalLayout)
        self.verticalLayout.addLayout(self.verticalLayout_2)
        self.hLayout2 = QtGui.QHBoxLayout()
        self.hLayout2.setObjectName("hLayout2")
        self.groupBox_2 = QtGui.QGroupBox(CenterAlignObjectsFacesEdges)
        self.groupBox_2.setObjectName("groupBox_2")
        self.rb_centers = QtGui.QRadioButton(self.groupBox_2)
        self.rb_centers.setGeometry(QtCore.QRect(20, 20, 261, 20))
        self.rb_centers.setObjectName("rb_centers")
        self.rb_planes = QtGui.QRadioButton(self.groupBox_2)
        self.rb_planes.setGeometry(QtCore.QRect(20, 50, 261, 20))
        self.rb_planes.setObjectName("rb_planes")
        self.rb_centers_planes = QtGui.QRadioButton(self.groupBox_2)
        self.rb_centers_planes.setGeometry(QtCore.QRect(20, 80, 301, 20))
        self.rb_centers_planes.setChecked(True)
        self.rb_centers_planes.setObjectName("rb_centers_planes")
        self.cb_x = QtGui.QCheckBox(self.groupBox_2)
        self.cb_x.setGeometry(QtCore.QRect(370, 40, 41, 20))
        self.cb_x.setChecked(True)
        self.cb_x.setObjectName("cb_x")
        self.cb_y = QtGui.QCheckBox(self.groupBox_2)
        self.cb_y.setGeometry(QtCore.QRect(370, 60, 41, 20))
        self.cb_y.setChecked(True)
        self.cb_y.setObjectName("cb_y")
        self.cb_z = QtGui.QCheckBox(self.groupBox_2)
        self.cb_z.setGeometry(QtCore.QRect(370, 80, 41, 20))
        self.cb_z.setChecked(True)
        self.cb_z.setObjectName("cb_z")
        self.label_2 = QtGui.QLabel(self.groupBox_2)
        self.label_2.setGeometry(QtCore.QRect(350, 20, 61, 16))
        self.label_2.setObjectName("label_2")
        self.hLayout2.addWidget(self.groupBox_2)
        self.verticalLayout.addLayout(self.hLayout2)
        self.verticalLayout_3 = QtGui.QVBoxLayout()
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.cb_inv_normals = QtGui.QCheckBox(CenterAlignObjectsFacesEdges)
        self.cb_inv_normals.setObjectName("cb_inv_normals")
        self.verticalLayout_3.addWidget(self.cb_inv_normals)
        self.line = QtGui.QFrame(CenterAlignObjectsFacesEdges)
        self.line.setFrameShape(QtGui.QFrame.HLine)
        self.line.setFrameShadow(QtGui.QFrame.Sunken)
        self.line.setObjectName("line")
        self.verticalLayout_3.addWidget(self.line)
        self.label = QtGui.QLabel(CenterAlignObjectsFacesEdges)
        self.label.setObjectName("label")
        self.verticalLayout_3.addWidget(self.label)
        self.hLayout1 = QtGui.QHBoxLayout()
        self.hLayout1.setObjectName("hLayout1")
        self.btnAlign = QtGui.QPushButton(CenterAlignObjectsFacesEdges)
        self.btnAlign.setObjectName("btnAlign")
        self.hLayout1.addWidget(self.btnAlign)
        self.btnMove = QtGui.QPushButton(CenterAlignObjectsFacesEdges)
        self.btnMove.setObjectName("btnMove")
        self.hLayout1.addWidget(self.btnMove)
        self.btnUndoMove = QtGui.QPushButton(CenterAlignObjectsFacesEdges)
        self.btnUndoMove.setObjectName("btnUndoMove")
        self.hLayout1.addWidget(self.btnUndoMove)
        self.verticalLayout_3.addLayout(self.hLayout1)
        self.verticalLayout.addLayout(self.verticalLayout_3)

        self.retranslateUi(CenterAlignObjectsFacesEdges)
        QtCore.QMetaObject.connectSlotsByName(CenterAlignObjectsFacesEdges)

    def retranslateUi(self, CenterAlignObjectsFacesEdges):
        CenterAlignObjectsFacesEdges.setWindowTitle(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "Center Align Faces/Edges", None, QtGui.QApplication.UnicodeUTF8))
        self.lbl_info.setText(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "<html><head/><body><p>Select [Ctrl+Click] multiple face(s) or closed Edges and click Align</p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox.setTitle(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "reference", None, QtGui.QApplication.UnicodeUTF8))
        self.rb_bb.setText(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "Center of Bounding Box", None, QtGui.QApplication.UnicodeUTF8))
        self.rb_mass.setText(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "Center of Mass", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox_2.setTitle(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "aligning", None, QtGui.QApplication.UnicodeUTF8))
        self.rb_centers.setText(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "Align Faces/Edges Centers", None, QtGui.QApplication.UnicodeUTF8))
        self.rb_planes.setText(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "Align Faces/Edges Planes", None, QtGui.QApplication.UnicodeUTF8))
        self.rb_centers_planes.setText(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "Align Faces/Edges Centers and Planes", None, QtGui.QApplication.UnicodeUTF8))
        self.cb_x.setText(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "x", None, QtGui.QApplication.UnicodeUTF8))
        self.cb_y.setText(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "y", None, QtGui.QApplication.UnicodeUTF8))
        self.cb_z.setText(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "z", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "center on:", None, QtGui.QApplication.UnicodeUTF8))
        self.cb_inv_normals.setText(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "invert Normal for Plane", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "<html><b>First Face/Edge is the Reference for alignment</b>&nbsp;&nbsp;&nbsp;<u>vers. 0.38</u>", None, QtGui.QApplication.UnicodeUTF8))
        self.btnAlign.setToolTip(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "select Faces or Edges (Ctrl+LBM) and click button to Apply", None, QtGui.QApplication.UnicodeUTF8))
        self.btnAlign.setText(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "Align", None, QtGui.QApplication.UnicodeUTF8))
        self.btnMove.setToolTip(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "select an Object and click button to Move it", None, QtGui.QApplication.UnicodeUTF8))
        self.btnMove.setText(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "Move", None, QtGui.QApplication.UnicodeUTF8))
        self.btnUndoMove.setToolTip(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "select Faces / Edges and click to show Normals", None, QtGui.QApplication.UnicodeUTF8))
        self.btnUndoMove.setText(QtGui.QApplication.translate("CenterAlignObjectsFacesEdges", "undo", None, QtGui.QApplication.UnicodeUTF8))

### ------------------------------------------------------------------------------------ ###
### ---------code to be inserted and remove from new generation------------------------- ###
### ------------------------------------------------------------------------------------ ###
        self.btnAlign.clicked.connect(self.onAlign)
        self.btnMove.clicked.connect(self.onMove)
        self.btnUndoMove.clicked.connect(self.onUndo)
        
    def onAlign(self):
        say("Align clicked")
        normal=0;type=0;mode=0
        if self.cb_inv_normals.isChecked():
            say("Align Normal Inverted")
            normal=1
        if self.rb_bb.isChecked():
            say("centering on Bounding Boxes")
        else:
            say("centering on Center of Mass")
            type=1
        if self.rb_centers_planes.isChecked():
            say("Centering and aligning Planes")
        elif self.rb_centers.isChecked():
            say("Centering Faces/Edges")
            mode=1
        else:
            say("Aligning Planes")
            mode=2
        cx=0;cy=0;cz=0
        if self.cb_x.isChecked():
            cx=1
        if self.cb_y.isChecked():
            cy=1
        if self.cb_z.isChecked():
            cz=1
        Align(normal,type,mode,cx,cy,cz)

    def onMove(self):
        say("Move clicked")
        Move()
        say("Move clicked2")

    def onUndo(self):
        say("Undo clicked")
        Undo()
        say("Undo clicked2")

##############################################################
global initial_placement, last_selection
global moving, rotating
    
#init
initial_placement = FreeCAD.Placement(App.Vector(0,0,0), App.Rotation(0,0,0), App.Vector(0,0,0)) #Placement [Pos=(0,0,0), Yaw-Pitch-Roll=(0,0,0)]
moving = [] #[App.Vector(0,0,0)]
rotating = [] #[0, App.Vector(0,0,0), App.Vector(0,0,0)]
#Draft.rotate(objs[j],-rot_angle,rot_center,rot_axis)
#rotating=[rot_angle,rot_center,rot_axis]

last_selection = []      

def say(msg):
    FreeCAD.Console.PrintMessage(msg)
    FreeCAD.Console.PrintMessage('\n')

def sayw(msg):
    FreeCAD.Console.PrintWarning(msg)
    FreeCAD.Console.PrintWarning('\n')

def sayerr(msg):
    FreeCAD.Console.PrintError(msg)
    FreeCAD.Console.PrintWarning('\n')

def make_string(input):
    if (sys.version_info > (3, 0)):  #py3
        if isinstance(input, str):
            return input
        else:
            input =  input.encode('utf-8')
            return input
    else:  #py2
        if type(input) == unicode:
            input =  input.encode('utf-8')
            return input
        else:
            return input

def singleInstance():
    app = QtGui.qApp

    for i in app.topLevelWidgets():
        if i.objectName() == "CenterAlignObjectsFacesEdges":
            i.deleteLater()
        else:
            pass
#    t=FreeCADGui.getMainWindow()
#    dw=t.findChildren(QtGui.QDockWidget)
#    #print str(dw)
#    for i in dw:
#        print str(i.objectName())
#        if str(i.objectName()) == "CenterAlignObjectsFacesEdges": #"kicad StepUp 3D tools":
#            i.deleteLater()
#        else:
#            pass

## assigning DisplayModeBody to Tip to attach Facebinder to Body
doc=FreeCAD.ActiveDocument
#Init        
   
    
singleInstance()

CenterAlignObjectsFacesEdges = QtGui.QWidget()
ui = Ui_CenterAlignObjectsFacesEdges()
ui.setupUi(CenterAlignObjectsFacesEdges)
#CenterAlignObjectsFacesEdges.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
CenterAlignObjectsFacesEdges.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint & QtCore.Qt.WindowTitleHint & QtCore.Qt.WindowMinimizeButtonHint & QtCore.Qt.WindowSystemMenuHint & QtCore.Qt.CustomizeWindowHint


Other languages: