Macro Connect And Sweep

From FreeCAD Documentation
Revision as of 11:44, 7 June 2016 by Mario52 (talk | contribs) (create page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

File:Macro Connect And Sweep Macro_Connect_And_Sweep

Description
This macro easily creates a connection between two objects.

Author: Mario52
Author
Mario52
Download
None
Links
Macro Version
1.0
Date last modified
None
FreeCAD Version(s)
None
Default shortcut
None
See also
None

Description

This macro easily creates a connection between two objects, an object and a point or between two points (the center of the objects are the starting and ending points of the sweep) can be selected form a configurable ellipse polygon circle

Macro_Connect_And_Sweep
Macro_Connect_And_Sweep

Use

Run the macro select your objects or points choice the form and parameters and click Create.

Macro_Connect_And_Sweep
Macro_Connect_And_Sweep

Script

Macro_Connect_And_Sweep the icon for the toolbar icon for the button

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
"""
***************************************************************************
*   Copyright (c) 2016 <mario52>                                          *
*                                                                         *
*   This file is a supplement to 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 software 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 macro; if not, write to the Free Software     *
*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
*   USA                                                                   *
***************************************************************************
*           WARNING! All changes in this file will be lost and            *  
*                  may cause malfunction of the program                   *
***************************************************************************
"""
#Macro_Connect_And_Sweep
#
#OS: Windows 10
#Word size of OS: 64-bit
#Word size of FreeCAD: 64-bit
#Version: 0.16.6700 (Git)
#Build type: Release
#Branch: releases/FreeCAD-0-16
#Hash: 7b925d11aa69ac405b423635adb1e2833f18a817
#Python version: 2.7.8
#Qt version: 4.8.6
#Coin version: 4.0.0a
#OCC version: 6.8.0.oce-0.17
#
__title__   = "Macro_Connect_And_Sweep"
__author__  = "Mario52"
__url__     = "http://www.freecadweb.org/index-fr.html"
__version__ = "00.00"
__date__    = "05/06/2016"

__Comment__ = "This macro connect 2 objects (boundingBox center) or one object (boundingBox center) and one point or twoo points"
__Help__    = "Start the macro select 2 objects choice the form and validate"

try:
    import PyQt4
    from PyQt4 import QtGui ,QtCore
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
except Exception:
    import PySide
    from PySide import QtGui ,QtCore
    from PySide.QtGui import *
    from PySide.QtCore import *
 
import Draft, Part, FreeCAD, math, PartGui, FreeCADGui
from math import sqrt, pi, sin, cos, asin
from FreeCAD import Base
App = FreeCAD

global path
path = FreeCAD.ConfigGet("AppHomePath")
#path = FreeCAD.ConfigGet("UserAppData")

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

def errorDialog(msg):
    diag = QtGui.QMessageBox(QtGui.QMessageBox.Critical,u"Error Message",msg )
    try:
        diag.setWindowFlags(PyQt4.QtCore.Qt.WindowStaysOnTopHint) # PyQt4 # cette fonction met la fenetre en avant
    except Exception:    
        diag.setWindowFlags(PySide.QtCore.Qt.WindowStaysOnTopHint) # PySide #cette fonction met la fenetre en avant
#    diag.setWindowModality(QtCore.Qt.ApplicationModal)       # la fonction a ete desactivee pour favoriser "WindowStaysOnTopHint"
    diag.exec_()
        
class Ui_MainWindow(object):

    def __init__(self):
        global path
        self.Polyline_Number_Faces = 3
        self.Polyline_CirCon       = 5.0
        self.Rectangle_X           = 5.0
        self.Rectangle_Y           = 3.0
        self.Circle_Radius         = 1.0

    def setupUi(self, MainWindow):
        self.window = MainWindow
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(332, 252)
        MainWindow.setMinimumSize(QtCore.QSize(332, 252))
        MainWindow.setMaximumSize(QtCore.QSize(332, 252))
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))

        self.PB_Quit = QtGui.QPushButton(self.centralwidget)
        self.PB_Quit.setGeometry(QtCore.QRect(20, 220, 141, 23))
        self.PB_Quit.setObjectName(_fromUtf8("PB_Quit"))
        self.PB_Quit.clicked.connect(self.on_PB_Quit) ###

        self.PB_Create = QtGui.QPushButton(self.centralwidget)
        self.PB_Create.setGeometry(QtCore.QRect(170, 220, 141, 23))
        self.PB_Create.setObjectName(_fromUtf8("PB_Create"))
        self.PB_Create.clicked.connect(self.on_PB_Create) ###

        self.GBox_02_ = QtGui.QGroupBox(self.centralwidget)
        self.GBox_02_.setGeometry(QtCore.QRect(20, 40, 291, 171))
        self.GBox_02_.setObjectName(_fromUtf8("GBox_02_"))

        self.SB_Polyline_Number_Faces = QtGui.QSpinBox(self.GBox_02_)
        self.SB_Polyline_Number_Faces.setGeometry(QtCore.QRect(210, 20, 71, 22))
        self.SB_Polyline_Number_Faces.setPrefix(_fromUtf8(""))
        self.SB_Polyline_Number_Faces.setObjectName(_fromUtf8("SB_Polyline_Number_Faces"))
        self.SB_Polyline_Number_Faces.setToolTip(_fromUtf8("Enter number faces"))
        self.SB_Polyline_Number_Faces.setMinimum(3)
        self.SB_Polyline_Number_Faces.setMaximum(999999)
        self.SB_Polyline_Number_Faces.setValue(self.Polyline_Number_Faces )
        self.SB_Polyline_Number_Faces.valueChanged.connect(self.on_SB_Polyline_Number_Faces) ###

        self.CB_Insc_Circon = QtGui.QCheckBox(self.GBox_02_)
        self.CB_Insc_Circon.setGeometry(QtCore.QRect(190, 20, 21, 21))
        self.CB_Insc_Circon.setText(_fromUtf8(""))
        self.CB_Insc_Circon.setToolTip(_fromUtf8("Circumscribed by default, if checked then Inscribed"))
        self.CB_Insc_Circon.setObjectName(_fromUtf8("CB_Insc_Circon"))
        self.CB_Insc_Circon.clicked.connect(self.on_CB_Insc_Circon_clicked)

        self.DS_Polyline_CirCon = QtGui.QDoubleSpinBox(self.GBox_02_)
        self.DS_Polyline_CirCon.setGeometry(QtCore.QRect(90, 20, 91, 22))
        self.DS_Polyline_CirCon.setObjectName(_fromUtf8("DS_Polyline_CirCon"))
        self.DS_Polyline_CirCon.setToolTip(_fromUtf8("Radius polygon"))
        self.DS_Polyline_CirCon.setMinimum(0.00001)
        self.DS_Polyline_CirCon.setMaximum(999999.999999)
        self.DS_Polyline_CirCon.setValue(self.Polyline_CirCon)
        self.DS_Polyline_CirCon.valueChanged.connect(self.on_DS_Polyline_CirCon) ###

        self.DS_Circle_Radius = QtGui.QDoubleSpinBox(self.GBox_02_)
        self.DS_Circle_Radius.setGeometry(QtCore.QRect(90, 110, 91, 22))
        self.DS_Circle_Radius.setObjectName(_fromUtf8("DS_Circle_Radius"))
        self.DS_Circle_Radius.setToolTip(_fromUtf8("Radius of circle"))
        self.DS_Circle_Radius.setMinimum(0.00001)
        self.DS_Circle_Radius.setMaximum(999999.999999)
        self.DS_Circle_Radius.setValue(self.Circle_Radius)
        self.DS_Circle_Radius.valueChanged.connect(self.on_DS_Circle_Radius) ###

        self.DS_Rectangle_X = QtGui.QDoubleSpinBox(self.GBox_02_)
        self.DS_Rectangle_X.setGeometry(QtCore.QRect(90, 50, 91, 22))
        self.DS_Rectangle_X.setObjectName(_fromUtf8("DS_Rectangle_X"))
        self.DS_Rectangle_X.setToolTip(_fromUtf8("Rectangle Length"))
        self.DS_Rectangle_X.setMinimum(0.00001)
        self.DS_Rectangle_X.setMaximum(999999.999999)
        self.DS_Rectangle_X.setValue(self.Rectangle_X)
        self.DS_Rectangle_X.valueChanged.connect(self.on_DS_Rectangle_X) ###

        self.DS_Rectangle_Y = QtGui.QDoubleSpinBox(self.GBox_02_)
        self.DS_Rectangle_Y.setGeometry(QtCore.QRect(191, 50, 91, 22))
        self.DS_Rectangle_Y.setObjectName(_fromUtf8("DS_Rectangle_Y"))
        self.DS_Rectangle_Y.setToolTip(_fromUtf8("Rectangle Height"))
        self.DS_Rectangle_Y.setMinimum(0.00001)
        self.DS_Rectangle_Y.setMaximum(999999.999999)
        self.DS_Rectangle_Y.setValue(self.Rectangle_Y)
        self.DS_Rectangle_Y.valueChanged.connect(self.on_DS_Rectangle_Y) ###

        self.RB_Polyline = QtGui.QRadioButton(self.GBox_02_)
        self.RB_Polyline.setGeometry(QtCore.QRect(20, 20, 82, 17))
        self.RB_Polyline.setChecked(True)
        self.RB_Polyline.setObjectName(_fromUtf8("RB_Polyline"))

        self.RB_Rectangle = QtGui.QRadioButton(self.GBox_02_)
        self.RB_Rectangle.setGeometry(QtCore.QRect(20, 50, 82, 17))
        self.RB_Rectangle.setObjectName(_fromUtf8("RB_Rectangle"))
        self.RB_Rectangle.clicked.connect(self.on_RB_Rectangle_clicked)

        self.RB_Ellipse = QtGui.QRadioButton(self.GBox_02_)
        self.RB_Ellipse.setGeometry(QtCore.QRect(20, 80, 82, 17))
        self.RB_Ellipse.setObjectName(_fromUtf8("RB_Ellipse"))
        self.RB_Ellipse.clicked.connect(self.on_RB_Ellipse_clicked)

        self.RB_Circle = QtGui.QRadioButton(self.GBox_02_)
        self.RB_Circle.setGeometry(QtCore.QRect(20, 110, 82, 17))
        self.RB_Circle.setObjectName(_fromUtf8("RB_Circle"))

        self.RB_Line = QtGui.QRadioButton(self.GBox_02_)
        self.RB_Line.setGeometry(QtCore.QRect(20, 140, 82, 17))
        self.RB_Line.setObjectName(_fromUtf8("RB_Line"))

        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(20, 15, 181, 21))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.label.setFont(font)
        self.label.setObjectName(_fromUtf8("label"))

        self.Info_01 = QtGui.QLabel(self.GBox_02_)
        self.Info_01.setGeometry(QtCore.QRect(90, 4, 181, 21))
        font = QtGui.QFont()
        font.setPointSize(8)
        self.Info_01.setFont(font)
        self.Info_01.setObjectName(_fromUtf8("Info_01"))
        MainWindow.setCentralWidget(self.centralwidget)

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

    def retranslateUi(self, MainWindow):
        try:
            MainWindow.setWindowFlags(PyQt4.QtCore.Qt.WindowStaysOnTopHint)        # PyQt4 cette fonction met la fenetre en avant
        except Exception:
            MainWindow.setWindowFlags(PySide.QtCore.Qt.WindowStaysOnTopHint)       # PySide cette fonction met la fenetre en avant
        MainWindow.setWindowTitle("Macro Connect And Sweep")
        self.PB_Quit.setText("Quit")
        self.PB_Create.setText("Create")
        self.GBox_02_.setTitle("Sweep Forme")
        self.SB_Polyline_Number_Faces.setSuffix(" num")
        self.DS_Circle_Radius.setSuffix(" r mm")
        self.RB_Circle.setText("Circle")
        self.DS_Polyline_CirCon.setSuffix(" Cir mm")
        self.RB_Polyline.setText("Polygone")
        self.RB_Line.setText("Line")
        self.RB_Rectangle.setText("Rectangle")
        self.RB_Ellipse.setText("Ellipse")
        self.DS_Rectangle_X.setSuffix(" L mm")
        self.DS_Rectangle_Y.setSuffix(" H mm")
        self.label.setText("Connect And Sweep")
        self.Info_01.setText(" Circumscribed")

    def on_SB_Polyline_Number_Faces(self,value):
        self.Polyline_Number_Faces = value
#        App.Console.PrintMessage(str("on_SB_Polyline_Number_Faces ")+str(self.Polyline_Number_Faces)+"\n")
    def on_DS_Circle_Radius(self,value):
        self.Circle_Radius = value
#        App.Console.PrintMessage(str("on_DS_Circle_Radius ")+str(self.Circle_Radius)+"\n")
    def on_DS_Polyline_CirCon(self,value):
        self.Polyline_CirCon = value
#        App.Console.PrintMessage(str("on_DS_Polyline_CirCon ")+str(self.Polyline_CirCon)+"\n")
    def on_DS_Rectangle_X(self,value):
        self.Rectangle_X = value
#        App.Console.PrintMessage(str("on_DS_Rectangle_X ")+str(self.Rectangle_X)+"\n")
    def on_DS_Rectangle_Y(self,value):
        self.Rectangle_Y = value
#        App.Console.PrintMessage(str("on_DS_Rectangle_Y ")+str(self.Rectangle_Y)+"\n")

    def on_CB_Insc_Circon_clicked(self):
        if self.CB_Insc_Circon.isChecked():
            self.DS_Polyline_CirCon.setSuffix(" Ins mm")
            self.Info_01.setText(" Inscribed")
        else:
            self.DS_Polyline_CirCon.setSuffix(" Cir mm")
            self.Info_01.setText(" Circumscribed")

    def on_RB_Rectangle_clicked(self):
        self.DS_Rectangle_X.setGeometry(QtCore.QRect(90, 50, 91, 22))
        self.DS_Rectangle_Y.setGeometry(QtCore.QRect(191, 50, 91, 22))
        self.DS_Rectangle_X.setSuffix(" L mm")
        self.DS_Rectangle_Y.setSuffix(" H mm")
        self.DS_Rectangle_X.setToolTip(_fromUtf8("Rectangle Length"))
        self.DS_Rectangle_Y.setToolTip(_fromUtf8("Rectangle Height"))
    def on_RB_Ellipse_clicked(self):
        self.DS_Rectangle_X.setGeometry(QtCore.QRect(90, 80, 91, 22))
        self.DS_Rectangle_Y.setGeometry(QtCore.QRect(191, 80, 91, 22))
        self.DS_Rectangle_X.setSuffix(" R mm")
        self.DS_Rectangle_Y.setSuffix(" r mm")
        self.DS_Rectangle_X.setToolTip(_fromUtf8("Ellipse MajorRadius"))
        self.DS_Rectangle_Y.setToolTip(_fromUtf8("Ellipse MinorRadius"))

    def on_PB_Quit(self):                        # Quit
        App.Console.PrintMessage(str("Fin Connect_And_Sweep ")+"\n")
        self.window.hide()

    def on_PB_Create(self):                      # Create

        if len(Gui.Selection.getSelection()) >= 2:
            try:                                                                        # Section Object 1 ###
                boundBox_0 = Gui.Selection.getSelectionEx()[0].SubObjects[0].Point      # Object 1 is SubObject 1
            except Exception:
                boundBox_0= Gui.Selection.getSelection()[0].Shape.BoundBox.Center       # Object 1 is Object

            try:                                                                        # Section Object 2 ###
                boundBox_1 = Gui.Selection.getSelectionEx()[1].SubObjects[0].Point      # Object 2 is SubObject other object
            except Exception:
                try:
                    boundBox_1 = Gui.Selection.getSelectionEx()[0].SubObjects[1].Point  # Object 2 is SubObject 2
                except Exception:
                    boundBox_1= Gui.Selection.getSelection()[1].Shape.BoundBox.Center   # Object 2 is Object
            
            points=[FreeCAD.Vector(boundBox_0),FreeCAD.Vector(boundBox_1)]              # coordinates
            ligne = Draft.makeWire(points,closed=False,face=False,support=None)         # create line directrice
                        
            App.Console.PrintMessage(str(boundBox_0)+" : "+str(boundBox_1)+"\n")        # coordinates displayed

            ##### create Line
            if self.RB_Line.isChecked():
                None
            else:
                ##### section camera view get origin
                OriginOrientation = FreeCADGui.ActiveDocument.ActiveView.getCameraOrientation()
                ##### section camera alignement to points
                v=boundBox_1.sub(boundBox_0)
                r=App.Rotation(App.Vector(0,0,1),v)
                FreeCADGui.ActiveDocument.ActiveView.setCameraOrientation(r.Q)
            
                ##### create Placement
                pl = FreeCAD.Placement()
                pl.Rotation.Q = (r.Q[0],r.Q[1],r.Q[2],r.Q[3])
                pl.Base = FreeCAD.Vector(boundBox_0[0],boundBox_0[1],boundBox_0[2])
    
                ##### create Polyline
                if self.RB_Polyline.isChecked():
                    if self.Polyline_CirCon == 0: self.Polyline_CirCon = 1.0
                    if self.CB_Insc_Circon.isChecked():
                        Forme = Draft.makePolygon(self.Polyline_Number_Faces ,radius=self.Polyline_CirCon,inscribed=True,placement=pl,face=False,support=None)    # Draft
                    else:
                        Forme = Draft.makePolygon(self.Polyline_Number_Faces ,radius=self.Polyline_CirCon,inscribed=False,placement=pl,face=False,support=None)   # Draft
                    Forme.Placement = pl

                ##### create Rectangle
                elif self.RB_Rectangle.isChecked():
                    if self.Rectangle_X == 0: self.Rectangle_X = 1.0
                    if self.Rectangle_Y == 0: self.Rectangle_Y = 1.0
                    Forme = Draft.makeRectangle(length=self.Rectangle_X,height=self.Rectangle_Y,placement=App.Placement(App.Vector(-(self.Rectangle_X/2),-(self.Rectangle_Y/2),0.0), App.Rotation(0.0,0.0,0.0), App.Vector(0.0,0.0,0.0)),face=False,support=None)
                    Forme.Placement = pl.multiply(Forme.Placement) # increment and centrage
        
                ##### create Ellipse
                elif self.RB_Ellipse.isChecked():
                    if self.Rectangle_X == 0: self.Rectangle_X = 1.0
                    if self.Rectangle_Y == 0: self.Rectangle_Y = 1.0
                    Forme = Draft.makeEllipse(self.Rectangle_X,self.Rectangle_Y,placement=pl,face=False,support=None)    
                    Forme.Placement = pl
    
                ##### create Circle
                elif self.RB_Circle.isChecked():
                    if self.Circle_Radius == 0: self.Circle_Radius = 1.0
                    Forme = Draft.makeCircle(radius=self.Circle_Radius,placement=pl,face=False,support=None)
                    Forme.Placement = pl
     
                ##### create sweep Forme to line
                sweepForme = App.ActiveDocument.addObject('Part::Sweep','Sweep')
                sweepForme.Sections=[App.ActiveDocument.getObject(Forme.Name), ]
                sweepForme.Spine=(App.ActiveDocument.getObject(ligne.Name))
                sweepForme.Solid=True
                sweepForme.Frenet=True

                ##### Contener
                FcSweep = App.ActiveDocument.addObject("App::DocumentObjectGroup","FcSweep")    
                FcSweep.addObject(sweepForme)
                FcSweep.addObject(ligne)
                FcSweep.addObject(Forme)
                FcSweep.Label = sweepForme.Name

                ##### renamme
                sweepForme.Label = sweepForme.Label + "_" + Forme.Name
                Forme.Label = App.ActiveDocument.ActiveObject.Name + "_" + Forme.Name
                ligne.Label = App.ActiveDocument.ActiveObject.Name + "_" + ligne.Name
                FreeCADGui.ActiveDocument.getObject(ligne.Name).Visibility = False
                FreeCADGui.ActiveDocument.getObject(Forme.Name).Visibility = False

                App.ActiveDocument.recompute()
                
                ##### section camera origin view restitute
                FreeCADGui.ActiveDocument.ActiveView.setCameraOrientation(OriginOrientation)

        else:
            App.Console.PrintError("Select twoo objects ("+str(len(Gui.Selection.getSelection()))+" object(s) selected)"+"\n")
#        App.Console.PrintMessage(str("on_PB_Create")+"\n")


MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()

Version

00.00 05/06/2016 :

Links

The forum discussion Scripting point to point tubing

Other languages: