-
Notifications
You must be signed in to change notification settings - Fork 69
/
a2p_constraintServices.py
133 lines (120 loc) · 6.43 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# ***************************************************************************
# * *
# * 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 *
# * *
# ***************************************************************************
from PySide import QtGui
import FreeCAD
import FreeCADGui
import a2plib
import a2p_constraints
translate = FreeCAD.Qt.translate
def reAdjustConstraintDirections(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.
"""
result = [] # Added for Constraint Diagnostic function
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 - All not Ok, we have errors
print(translate(
"A2plus",
"Errors occurred during processing of {}").format(
c.Label
)
)
result.append(c.Name) # Added for Constraint Diagnostic function
if len(unknown_constraints) > 0:
# Print - All not Ok, we have problem
print(translate(
"A2plus_Constraints",
"reAdjustConstraintDirections(): Found unknown constraints: {}"
).format(
set(unknown_constraints)
)
)
else:
# Print - All Ok
print(translate(
"A2plus_Constraints",
"reAdjustConstraintDirections(): All constraints are recalculated."
))
return(result) # Added for Constraint Diagnostic function
class a2p_reAdjustConstraintDirectionsCommand:
def Activated(self):
flags = QtGui.QMessageBox.StandardButton.Yes | QtGui.QMessageBox.StandardButton.No
response = QtGui.QMessageBox.information(
QtGui.QApplication.activeWindow(),
translate("A2plus_Constraints", "Recalculate direction of constraints"),
translate("A2plus_Constraints", "Do you really want to recalculate the directions of all constraints?"),
flags
)
if response == QtGui.QMessageBox.Yes:
doc = FreeCAD.activeDocument()
doc.openTransaction("Readjust constraint's directions")
reAdjustConstraintDirections(doc)
doc.commitTransaction()
def IsActive(self):
if FreeCAD.activeDocument() is None:
return False
return True
def GetResources(self):
return {
'Pixmap': a2plib.get_module_path()+'/icons/a2p_ReAdjustConstraints.svg',
'MenuText': translate("A2plus_Constraints", "Re-adjust directions of all constraints"),
'ToolTip': translate("A2plus_Constraints", "Re-adjust directions of all constraints to best fit")
}
FreeCADGui.addCommand('a2p_reAdjustConstraintDirectionsCommand', a2p_reAdjustConstraintDirectionsCommand())