forked from kbwbe/A2plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
a2p_constraintServices.py
110 lines (102 loc) · 5.8 KB
/
a2p_constraintServices.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#***************************************************************************
#* *
#* Copyright (c) 2020 kbwbe *
#* *
#* Portions of code based on hamish's assembly 2 *
#* *
#* 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 *
#* *
#***************************************************************************
import FreeCAD
import FreeCADGui
from PySide import QtGui
import a2plib
import a2p_constraints
#==============================================================================
def redAdjustConstraintDirections(doc):
'''
recalculate value of property 'direction' and the sign of property 'offset' of all
a2p-constraints of a document, in order to reach a solvable state if
possible, especially used after updating of imported parts.
'''
unknown_constraints = []
constraints = [ obj for obj in doc.Objects if 'ConstraintInfo' in obj.Content]
for c in constraints:
try: #process as much constraints as possible
if c.Type == 'pointIdentity':
a2p_constraints.PointIdentityConstraint.recalculateMatingDirection(c)
elif c.Type == 'pointOnLine':
a2p_constraints.PointOnLineConstraint.recalculateMatingDirection(c)
elif c.Type == 'pointOnPlane':
a2p_constraints.PointOnPlaneConstraint.recalculateMatingDirection(c)
elif c.Type == 'circularEdge':
a2p_constraints.CircularEdgeConstraint.recalculateMatingDirection(c)
elif c.Type == 'axial':
a2p_constraints.AxialConstraint.recalculateMatingDirection(c)
elif c.Type == 'axisParallel':
a2p_constraints.AxisParallelConstraint.recalculateMatingDirection(c)
elif c.Type == 'axisPlaneParallel':
a2p_constraints.AxisPlaneParallelConstraint.recalculateMatingDirection(c)
elif c.Type == 'axisPlaneAngle':
a2p_constraints.AxisPlaneAngleConstraint.recalculateMatingDirection(c)
elif c.Type == 'axisPlaneNormal':
a2p_constraints.AxisPlaneNormalConstraint.recalculateMatingDirection(c)
elif c.Type == 'planesParallel':
a2p_constraints.PlanesParallelConstraint.recalculateMatingDirection(c)
elif c.Type == 'plane':
a2p_constraints.PlaneConstraint.recalculateMatingDirection(c)
elif c.Type == 'angledPlanes':
a2p_constraints.AngledPlanesConstraint.recalculateMatingDirection(c)
elif c.Type == 'sphereCenterIdent':
a2p_constraints.SphericalConstraint.recalculateMatingDirection(c)
elif c.Type == 'CenterOfMass':
a2p_constraints.CenterOfMassConstraint.recalculateMatingDirection(c)
else:
unknown_constraints.append(c.Type)
except:
print("Errors occured during processing of {}".format(c.Label))
if len(unknown_constraints) > 0:
print("redefineConstraintDirections(): Found unknown constraints: {}".format(
set(unknown_constraints)
)
)
#==============================================================================
class a2p_reAdjustConstraintDirectionsCommand:
def Activated(self):
flags = QtGui.QMessageBox.StandardButton.Yes | QtGui.QMessageBox.StandardButton.No
response = QtGui.QMessageBox.information(
QtGui.QApplication.activeWindow(),
u'Recalculate direction of constraints',
u'Do you really want to recalculate\nthe directions of all constraints?',
flags
)
if response == QtGui.QMessageBox.Yes:
doc = FreeCAD.activeDocument()
doc.openTransaction("Readjust constraint's directions")
redAdjustConstraintDirections(doc)
doc.commitTransaction()
def IsActive(self):
if FreeCAD.activeDocument() is None: return False
return True
def GetResources(self):
return {
'Pixmap' : a2plib.pathOfModule()+'/icons/a2p_ReAdjustConstraints.svg',
'MenuText': 'Readjust directions of all constraints',
'ToolTip': 'Readjust directions of all constraints to fit best'
}
FreeCADGui.addCommand('a2p_reAdjustConstraintDirectionsCommand', a2p_reAdjustConstraintDirectionsCommand())
#==============================================================================