forked from Zolko-123/FreeCAD_Assembly4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewDatumCmd.py
218 lines (189 loc) · 8.94 KB
/
newDatumCmd.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python3
# coding: utf-8
#
# newDatumCmd.py
import os
from PySide import QtGui, QtCore
import FreeCADGui as Gui
import FreeCAD as App
import libAsm4 as Asm4
"""
+-----------------------------------------------+
| a class to create all Datum objects |
+-----------------------------------------------+
"""
class newDatum:
"My tool object"
def __init__(self, datumName):
self.datumName = datumName
if self.datumName == 'Point':
self.datumType = 'PartDesign::Point'
self.menutext = "New Point"
self.tooltip = "Create a new Datum Point in a Part"
self.icon = os.path.join( Asm4.iconPath , 'Asm4_Point.svg')
self.datumColor = (0.00,0.00,0.00)
self.datumAlpha = []
elif self.datumName == 'Axis':
self.datumType = 'PartDesign::Line'
self.menutext = "New Axis"
self.tooltip = "Create a new Datum Axis in a Part"
self.icon = os.path.join( Asm4.iconPath , 'Asm4_Axis.svg')
self.datumColor = (0.00,0.00,0.50)
self.datumAlpha = []
elif self.datumName == 'Plane':
self.datumType = 'PartDesign::Plane'
self.menutext = "New Plane"
self.tooltip = "Create a new Datum Plane in a Part"
self.icon = os.path.join( Asm4.iconPath , 'Asm4_Plane.svg')
self.datumColor = (0.50,0.50,0.50)
self.datumAlpha = 80
elif self.datumName == 'LCS':
self.datumType = 'PartDesign::CoordinateSystem'
self.menutext = "New Coordinate System"
self.tooltip = "Create a new Coordinate System in a Part"
self.icon = os.path.join( Asm4.iconPath , 'Asm4_CoordinateSystem.svg')
self.datumColor = []
self.datumAlpha = []
elif self.datumName == 'Sketch':
self.datumType = 'Sketcher::SketchObject'
self.menutext = "New Sketch"
self.tooltip = "Create a new Sketch in a Part"
self.icon = os.path.join( Asm4.iconPath , 'Asm4_Sketch.svg')
self.datumColor = []
self.datumAlpha = []
self.datumTypes = ['PartDesign::Point','PartDesign::Line','PartDesign::Plane','PartDesign::CoordinateSystem']
def GetResources(self):
return {"MenuText": self.menutext,
"ToolTip": self.tooltip,
"Pixmap" : self.icon }
def IsActive(self):
if App.ActiveDocument:
# is something correct selected ?
if self.checkSelection():
return(True)
return(False)
def checkSelection(self):
# if something is selected ...
if Gui.Selection.getSelection():
selectedObj = Gui.Selection.getSelection()[0]
# ... and it's an App::Part or an datum object
if selectedObj.TypeId=='App::Part' or selectedObj.TypeId=='PartDesign::Body'or selectedObj.TypeId in self.datumTypes:
return(selectedObj)
# or of nothing is selected ...
elif App.ActiveDocument.getObject('Model'):
# ... but there is a Model:
return App.ActiveDocument.getObject('Model')
# if we're here it's because we didn't find a good reason to not be here
return None
"""
+-----------------------------------------------+
| the real stuff |
+-----------------------------------------------+
"""
def Activated(self):
# check that we have somewhere to put our stuff (an App::Part or PartDesign::Body or Asm4 Model)
selectedObj = self.checkSelection()
if selectedObj.TypeId=='App::Part' or selectedObj.TypeId=='PartDesign::Body':
partChecked = selectedObj
# if a datum object is selected we try to find the parent App::Part or PartDesign::Body
elif selectedObj.TypeId in self.datumTypes:
parent = selectedObj.getParentGeoFeatureGroup()
if parent.TypeId=='App::Part' or parent.TypeId=='PartDesign::Body':
partChecked = parent
# something went wrong
else:
Asm4.warningBox("I can't create a "+self.datumType+" with the current selections")
# check whether there is already a similar datum, and increment the instance number
instanceNum = 1
while App.ActiveDocument.getObject( self.datumName+'_'+str(instanceNum) ):
instanceNum += 1
datumName = self.datumName+'_'+str(instanceNum)
if partChecked:
# input dialog to ask the user the name of the Sketch:
text,ok = QtGui.QInputDialog.getText(None,'Create new '+self.datumName,
'Enter '+self.datumName+' name : ', text = datumName)
if ok and text:
# App.activeDocument().getObject('Model').newObject( 'Sketcher::SketchObject', text )
createdDatum = partChecked.newObject( self.datumType, text )
# automatic resizing of datum Plane sucks, so we set it to manual
if self.datumType=='PartDesign::Plane':
createdDatum.ResizeMode = 'Manual'
createdDatum.Length = 100
createdDatum.Width = 100
# if color or transparency is specified for this datum type
if self.datumColor:
Gui.ActiveDocument.getObject(createdDatum.Name).ShapeColor = self.datumColor
if self.datumAlpha:
Gui.ActiveDocument.getObject(createdDatum.Name).Transparency = self.datumAlpha
# highlight the created datum object
Gui.Selection.clearSelection()
Gui.Selection.addSelection( App.ActiveDocument.Name, partChecked.Name, createdDatum.Name+'.' )
"""
+-----------------------------------------------+
| a class to create an LCS on a hole |
+-----------------------------------------------+
"""
class newHole:
def GetResources(self):
return {"MenuText": "New Hole Axis",
"ToolTip": "Create a Datum Axis attached to a hole",
"Pixmap" : os.path.join( Asm4.iconPath , 'Asm4_Hole.svg')
}
def IsActive(self):
selection = self.getSelection()
if selection == None:
return False
else:
return True
def getSelection(self):
# check that we have selected a circular edge
selection = None
if App.ActiveDocument:
# 1 thing is selected:
if len(Gui.Selection.getSelection()) == 1:
# check whether it's a circular edge:
edge = Gui.Selection.getSelectionEx()[0]
if len(edge.SubObjects) == 1:
edgeObj = edge.SubObjects[0]
# if the edge is circular
if hasattr(edgeObj,"Curve") and hasattr(edgeObj.Curve,"centerOfCurvature"):
# find the feature on which the edge is located
parentObj = Gui.Selection.getSelection()[0]
edgeName = edge.SubElementNames[0]
selection = ( parentObj, edgeName )
return selection
"""
+-----------------------------------------------+
| the real stuff |
+-----------------------------------------------+
"""
def Activated(self):
( selectedObj, edge ) = self.getSelection()
parentPart = selectedObj.getParentGeoFeatureGroup()
# if the solid having the edge is indeed in an App::Part
if parentPart and (parentPart.TypeId=='App::Part' or parentPart.TypeId=='PartDesign::Body'):
# check whether there is already a similar datum, and increment the instance number
instanceNum = 1
while App.ActiveDocument.getObject( 'HoleAxis_'+str(instanceNum) ):
instanceNum += 1
axis = parentPart.newObject('PartDesign::Line','HoleAxis_'+str(instanceNum))
axis.Support = [( selectedObj, (edge,) )]
axis.MapMode = 'AxisOfCurvature'
axis.MapReversed = False
axis.ResizeMode = 'Manual'
axis.Length = 40
Gui.ActiveDocument.getObject(axis.Name).ShapeColor = (0.0,1.0,1.0)
Gui.ActiveDocument.getObject(axis.Name).Transparency = 0
axis.recompute()
parentPart.recompute()
"""
+-----------------------------------------------+
| add the commands to the workbench |
+-----------------------------------------------+
"""
Gui.addCommand( 'Asm4_newPoint', newDatum('Point') )
Gui.addCommand( 'Asm4_newAxis', newDatum('Axis') )
Gui.addCommand( 'Asm4_newPlane', newDatum('Plane') )
Gui.addCommand( 'Asm4_newLCS', newDatum('LCS') )
Gui.addCommand( 'Asm4_newSketch',newDatum('Sketch'))
Gui.addCommand( 'Asm4_newHole', newHole() )