~freecad-community/freecad-extras/assembly2

114 by hamish2014
Command added for quickly bolting multiple circular edges
1
from assembly2lib import *
2
from assembly2solver import solveConstraints
3
4
class CircularEdgeSelectionGate:
5
    def allow(self, doc, obj, sub):
6
        return CircularEdgeSelected( SelectionExObject(doc, obj, sub) )
7
8
class boltMultipleCircularEdgesCommand:
9
    def Activated(self):
10
        selection = FreeCADGui.Selection.getSelectionEx()
11
        if len(selection) < 2:
12
            FreeCADGui.Selection.clearSelection()
13
            self.taskDialog = RapidBoltingTaskDialog()
14
            FreeCADGui.Control.showDialog( self.taskDialog )
15
            FreeCADGui.Selection.removeSelectionGate()
16
            FreeCADGui.Selection.addSelectionGate( CircularEdgeSelectionGate() )
17
        else:
18
            valid = True
19
            for s in selection:
20
                for se_name in s.SubElementNames:
21
                    if not CircularEdgeSelected( SelectionExObject(FreeCAD.ActiveDocument, s.Object, se_name) ):
22
                        valid = False
23
                        break
24
            if valid:
25
                boltSelection()
26
            else:
186.1.1 by ChrisGallagher
Updated obsolete API: QtGui.qApp.activeWindow() to QtGui.QApplication.activeWindow()
27
                QtGui.QMessageBox.information(  QtGui.QApplication.activeWindow(), "Incorrect Usage", 'Select only circular edges')
28
    def GetResources(self):
171 by BPLRFE
Typos in tooltips and readme correct
29
        msg = 'Bolt multiple circular edges'
114 by hamish2014
Command added for quickly bolting multiple circular edges
30
        return {
186.1.1 by ChrisGallagher
Updated obsolete API: QtGui.qApp.activeWindow() to QtGui.QApplication.activeWindow()
31
            'Pixmap' : ':/assembly2/icons/boltMultipleCircularEdges.svg',
32
            'MenuText': msg,
114 by hamish2014
Command added for quickly bolting multiple circular edges
33
            'ToolTip': msg
186.1.1 by ChrisGallagher
Updated obsolete API: QtGui.qApp.activeWindow() to QtGui.QApplication.activeWindow()
34
            }
114 by hamish2014
Command added for quickly bolting multiple circular edges
35
36
FreeCADGui.addCommand('boltMultipleCircularEdges', boltMultipleCircularEdgesCommand())
37
38
class RapidBoltingTaskDialog:
39
    def __init__(self):
40
        self.form = RapidBoltingForm('''Instructions:
41
42
1) select mating edge on Bolt
186.1.1 by ChrisGallagher
Updated obsolete API: QtGui.qApp.activeWindow() to QtGui.QApplication.activeWindow()
43
2) add to the Selection the edges
114 by hamish2014
Command added for quickly bolting multiple circular edges
44
to which the bolt is to be mated
45
3) press Ok ''' )
186.1.1 by ChrisGallagher
Updated obsolete API: QtGui.qApp.activeWindow() to QtGui.QApplication.activeWindow()
46
        self.form.setWindowTitle( 'Bolt multiple circular edges' )
128.2.1 by hamish2014
Merged https://github.com/hamish2014/FreeCAD_assembly2/pull/47/files QRC updates
47
        self.form.setWindowIcon( QtGui.QIcon( ':/assembly2/icons/boltMultipleCircularEdges.svg' ) )
114 by hamish2014
Command added for quickly bolting multiple circular edges
48
    def reject(self):
49
        FreeCADGui.Selection.removeSelectionGate()
50
        FreeCADGui.Control.closeDialog()
51
    def accept(self):
52
        FreeCADGui.Selection.removeSelectionGate()
53
        FreeCADGui.Control.closeDialog()
54
        boltSelection()
186.1.1 by ChrisGallagher
Updated obsolete API: QtGui.qApp.activeWindow() to QtGui.QApplication.activeWindow()
55
class RapidBoltingForm(QtGui.QWidget):
114 by hamish2014
Command added for quickly bolting multiple circular edges
56
    def __init__(self, textLines ):
57
        super(RapidBoltingForm, self).__init__()
186.1.1 by ChrisGallagher
Updated obsolete API: QtGui.qApp.activeWindow() to QtGui.QApplication.activeWindow()
58
        self.textLines = textLines
114 by hamish2014
Command added for quickly bolting multiple circular edges
59
        self.initUI()
60
    def initUI(self):
61
        vbox = QtGui.QVBoxLayout()
62
        for line in self.textLines.split('\n'):
63
            vbox.addWidget( QtGui.QLabel(line) )
64
        self.setLayout(vbox)
65
66
67
from circularEdgeConstraint import parseSelection
68
from importPart import duplicateImportedPart
69
70
def boltSelection():
71
    doc = FreeCAD.ActiveDocument
115 by hamish2014
Bolt Multiple Circular Edges Undo Functionality Added
72
    doc.openTransaction('Bolt Multiple Circular Edges')
114 by hamish2014
Command added for quickly bolting multiple circular edges
73
    selection = FreeCADGui.Selection.getSelectionEx()
74
    bolt = selection[0].Object
75
    bolt_se_name = selection[0].SubElementNames[0]
186.1.1 by ChrisGallagher
Updated obsolete API: QtGui.qApp.activeWindow() to QtGui.QApplication.activeWindow()
76
    S = [] #edgesToConstrainTo
114 by hamish2014
Command added for quickly bolting multiple circular edges
77
    for s in selection[1:]:
78
        for se_name in s.SubElementNames:
79
            S.append( SelectionExObject(doc, s.Object, se_name) )
80
    for s2 in S:
81
        newBolt = duplicateImportedPart(bolt)
82
        s1 = SelectionExObject(doc, newBolt, bolt_se_name)
83
        debugPrint(3,'s1 %s' % [s1, s2])
84
        parseSelection(
186.1.1 by ChrisGallagher
Updated obsolete API: QtGui.qApp.activeWindow() to QtGui.QApplication.activeWindow()
85
            [s1, s2 ],
86
            callSolveConstraints= False, lockRotation = True
114 by hamish2014
Command added for quickly bolting multiple circular edges
87
            )
88
    solveConstraints( doc )
115 by hamish2014
Bolt Multiple Circular Edges Undo Functionality Added
89
    FreeCAD.ActiveDocument.commitTransaction()
163 by hamish2014
repair_tree_view added to help with grouping constraints under objects
90
    repair_tree_view()