-
Notifications
You must be signed in to change notification settings - Fork 75
/
newDatumCmd.py
258 lines (227 loc) · 10.6 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python3
# coding: utf-8
#
# LGPL
# Copyright HUBERT Zoltán
#
# newDatumCmd.py
import os
from PySide import QtGui, QtCore
import FreeCADGui as Gui
import FreeCAD as App
from FreeCAD import Console as FCC
import Asm4_libs as Asm4
"""
+-----------------------------------------------+
| a class to create all Datum objects |
+-----------------------------------------------+
"""
class newDatum:
"My tool object"
def __init__(self, datumName):
self.datumName = datumName
# recognised containers (not the same as Asm4.containerTypes !)
self.containers = [ 'App::Part', 'PartDesign::Body', 'App::DocumentObjectGroup']
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 = []
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
selType = selectedObj.TypeId
if selType in self.containers or selType in Asm4.datumTypes or selType=='Sketcher::SketchObject':
return(selectedObj)
# or of nothing is selected ...
elif Asm4.getAssembly():
# ... but there is as assembly:
return Asm4.getAssembly()
# 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
selectedObj = self.checkSelection()
# default name increments the datum type's end numeral
proposedName = Asm4.nextInstance( self.datumName, startAtOne=True )
parentContainer = None
# check whether we have selected a container
if selectedObj.TypeId in self.containers:
parentContainer = selectedObj
# if a datum object is selected
elif selectedObj.TypeId in Asm4.datumTypes or selectedObj.TypeId=='Sketcher::SketchObject':
# see whether it's in a container
parent = selectedObj.getParentGeoFeatureGroup()
if parent.TypeId in self.containers:
parentContainer = parent
# if there is an assembly
elif Asm4.getAssembly():
parentContainer = Asm4.getAssembly()
# 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 parentContainer:
# input dialog to ask the user the name of the Sketch:
#proposedName = Asm4.nextInstance( self.datumName + '_' + selectedObj.Label, startAtOne=True )
text,ok = QtGui.QInputDialog.getText(None,'Create new '+self.datumName,
'Enter '+self.datumName+' name :'+' '*40, text = proposedName)
if ok and text:
# App.activeDocument().getObject('Model').newObject( 'Sketcher::SketchObject', text )
createdDatum = App.ActiveDocument.addObject( self.datumType, text )
parentContainer.addObject( createdDatum )
createdDatum.Label = 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
elif self.datumType=='PartDesign::Line':
createdDatum.ResizeMode = 'Manual'
createdDatum.Length = 200
# 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, parentContainer.Name, createdDatum.Name+'.' )
Gui.runCommand('Part_EditAttachment')
"""
+-----------------------------------------------+
| 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.getSelectedEdges()
if selection is None:
return False
else:
return True
"""
+-----------------------------------------------+
| the real stuff |
+-----------------------------------------------+
"""
def getSelectedEdges(self):
# check that we have selected only circular edges
selection = None
parent = None
edges = []
# 1 selection means a single parent
if App.ActiveDocument and len(Gui.Selection.getSelection()) == 1:
parent = Gui.Selection.getSelection()[0]
# parse all sub-elemets of the selection
for i in range(len(Gui.Selection.getSelectionEx()[0].SubObjects)):
edgeObj = Gui.Selection.getSelectionEx()[0].SubObjects[i]
edgeName = Gui.Selection.getSelectionEx()[0].SubElementNames[i]
# if the edge is circular
if Asm4.isCircle(edgeObj):
edges.append( [edgeObj,edgeName] )
# if we found circular edges
if len(edges) > 0:
selection = ( parent, edges )
return selection
def Activated(self):
( selectedObj, edges ) = self.getSelectedEdges()
for i in range(len(edges)):
edgeObj = edges[i][0]
edgeName = edges[i][1]
parentPart = selectedObj.getParentGeoFeatureGroup()
# we can create a datum only in a container
if parentPart:
parentDoc = parentPart.Document
# 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 parentDoc.getObject( 'HoleAxis_'+str(instanceNum) ):
instanceNum += 1
axis = Asm4.newLCS(parentPart, 'PartDesign::Line', 'HoleAxis_'+str(instanceNum), [( selectedObj, (edgeName,) )])
axis.MapMode = 'AxisOfCurvature'
axis.MapReversed = False
axis.ResizeMode = 'Manual'
axis.Length = edgeObj.BoundBox.DiagonalLength
axis.ViewObject.ShapeColor = (0.0,0.0,1.0)
axis.ViewObject.Transparency = 50
axis.recompute()
parentPart.recompute()
#
else:
FCC.PrintMessage('Datum objects can only be created inside Part or Body containers')
"""
+-----------------------------------------------+
| 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() )
# defines the drop-down button for Datum objects
createDatumList = [ 'Asm4_newLCS',
'Asm4_newPlane',
'Asm4_newAxis',
'Asm4_newPoint',
'Asm4_newHole' ]
Gui.addCommand( 'Asm4_createDatum', Asm4.dropDownCmd( createDatumList, 'Create Datum Object'))