-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathEM_FHEquiv.py
245 lines (213 loc) · 10.3 KB
/
EM_FHEquiv.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
#***************************************************************************
#* *
#* Copyright (c) 2018 *
#* Efficient Power Conversion Corporation, Inc. http://epc-co.com *
#* *
#* Developed by FastFieldSolvers S.R.L. under contract by EPC *
#* http://www.fastfieldsolvers.com *
#* *
#* 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 *
#* *
#***************************************************************************
__title__="FreeCAD E.M. Workbench FastHenry node Equivalence Class"
__author__ = "FastFieldSolvers S.R.L."
__url__ = "http://www.fastfieldsolvers.com"
# defines
#
# tolerance in distance between nodes to define a port
EMFHEQUIV_LENTOL = 1e-8
import FreeCAD, FreeCADGui, Mesh, Part, MeshPart, Draft, DraftGeomUtils, os
from FreeCAD import Vector
if FreeCAD.GuiUp:
import FreeCADGui
from PySide import QtCore, QtGui
from DraftTools import translate
from PySide.QtCore import QT_TRANSLATE_NOOP
else:
# \cond
def translate(ctxt,txt):
return txt
def QT_TRANSLATE_NOOP(ctxt,txt):
return txt
# \endcond
__dir__ = os.path.dirname(__file__)
iconPath = os.path.join( __dir__, 'Resources' )
def makeFHEquiv(node1=None,node2=None,name='FHEquiv'):
'''Creates a FastHenry node equivalence ('.equiv' statement in FastHenry)
'node1' is the first node to short-circuit
'node2' is the second node to short-circuit
'name' is the name of the object
Example:
equiv = makeFHEquiv(node1,node2)
'''
obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", name)
obj.Label = translate("EM", name)
# this adds the relevant properties to the object
#'obj' (e.g. 'Base' property) making it a _FHEquiv
_FHEquiv(obj)
# manage ViewProvider object
if FreeCAD.GuiUp:
_ViewProviderFHEquiv(obj.ViewObject)
# set base ViewObject properties to user-selected values (if any)
# check if 'nodeStart' is a FHNode, and if so, assign it as first node
if node1:
if Draft.getType(node2) == "FHNode":
obj.Node1 = node1
# check if 'nodeEnd' is a FHNode, and if so, assign it as second node
if node2:
if Draft.getType(node2) == "FHNode":
obj.Node2 = node2
# return the newly created Python object
return obj
class _FHEquiv:
'''The EM FastHenry node Equivalence object'''
def __init__(self, obj):
''' Add properties '''
obj.addProperty("App::PropertyLink","Node1","EM",QT_TRANSLATE_NOOP("App::Property","First FHNode to short-circuit"))
obj.addProperty("App::PropertyLink","Node2","EM",QT_TRANSLATE_NOOP("App::Property","Second FHNode to short-circuit"))
obj.Proxy = self
self.Type = "FHEquiv"
# save the object in the class, to store or retrieve specific data from it
# from within the class
self.Object = obj
def execute(self, obj):
''' this method is mandatory. It is called on Document.recompute()
'''
if obj.Node1 == None:
return
elif Draft.getType(obj.Node1) != "FHNode":
FreeCAD.Console.PrintWarning(translate("EM","Node1 is not a FHNode"))
return
if obj.Node2 == None:
return
elif Draft.getType(obj.Node2) != "FHNode":
FreeCAD.Console.PrintWarning(translate("EM","Node2 is not a FHNode"))
return
# and finally, if everything is ok, make and assign the shape
self.assignShape(obj)
def assignShape(self, obj):
''' Compute and assign the shape to the object 'obj' '''
n1 = obj.Node1.Proxy.getAbsCoord()
n2 = obj.Node2.Proxy.getAbsCoord()
shape = self.makeEquivShape(n1,n2)
# shape may be None, e.g. if endpoints coincide. Do not assign in this case.
# FHEquiv is still valid, but not visible.
if shape:
obj.Shape = shape
def makeEquivShape(self,n1,n2):
''' Compute a node equivalence shape given:
'n1': start node position (Vector)
'n2': end node position (Vector)
'''
# do not accept coincident nodes
if (n2-n1).Length < EMFHEQUIV_LENTOL:
return None
line = Part.makeLine(n1, n2)
return line
def onChanged(self, obj, prop):
''' take action if an object property 'prop' changed
'''
#FreeCAD.Console.PrintWarning("\n_FHEquiv onChanged(" + str(prop)+")\n") #debug
if not hasattr(self,"Object"):
# on restore, self.Object is not there anymore (JSON does not serialize complex objects
# members of the class, so __getstate__() and __setstate__() skip them);
# so we must "re-attach" (re-create) the 'self.Object'
self.Object = obj
def serialize(self,fid):
''' Serialize the object to the 'fid' file descriptor
'''
fid.write(".equiv N" + self.Object.Node1.Label + " N" + self.Object.Node2.Label + "\n")
def __getstate__(self):
return self.Type
def __setstate__(self,state):
if state:
self.Type = state
class _ViewProviderFHEquiv:
def __init__(self, obj):
''' Set this object to the proxy object of the actual view provider '''
obj.Proxy = self
self.Object = obj.Object
def attach(self, obj):
''' Setup the scene sub-graph of the view provider, this method is mandatory '''
# on restore, self.Object is not there anymore (JSON does not serialize complex objects
# members of the class, so __getstate__() and __setstate__() skip them);
# so we must "re-attach" (re-create) the 'self.Object'
self.Object = obj.Object
return
def updateData(self, fp, prop):
''' If a property of the handled feature has changed we have the chance to handle this here '''
#FreeCAD.Console.PrintMessage("ViewProvider updateData(), property: " + str(prop) + "\n") # debug
return
def getDefaultDisplayMode(self):
''' Return the name of the default display mode. It must be defined in getDisplayModes. '''
return "Flat Lines"
def onChanged(self, vp, prop):
''' If the 'prop' property changed for the ViewProvider 'vp' '''
#FreeCAD.Console.PrintMessage("ViewProvider onChanged(), property: " + str(prop) + "\n") # debug
def claimChildren(self):
''' Used to place other objects as children in the tree'''
c = []
if hasattr(self,"Object"):
if hasattr(self.Object,"Node1"):
c.append(self.Object.Node1)
if hasattr(self.Object,"Node2"):
c.append(self.Object.Node2)
return c
def getIcon(self):
''' Return the icon which will appear in the tree view. This method is optional
and if not defined a default icon is shown.
'''
return os.path.join(iconPath, 'EM_FHEquiv.svg')
def __getstate__(self):
return None
def __setstate__(self,state):
return None
class _CommandFHEquiv:
''' The EM FastHenry equivalent node (FHEquiv) command definition
'''
def GetResources(self):
return {'Pixmap' : os.path.join(iconPath, 'EM_FHEquiv.svg') ,
'MenuText': QT_TRANSLATE_NOOP("EM_FHEquiv","FHEquiv"),
'Accel': "E, E",
'ToolTip': QT_TRANSLATE_NOOP("EM_FHEquiv","Creates a FastHenry equivalent node object from two FHNodes")}
def IsActive(self):
return not FreeCAD.ActiveDocument is None
def Activated(self):
# preferences
#p = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/EM")
#self.Width = p.GetFloat("Width",200)
# get the selected object(s)
selection = FreeCADGui.Selection.getSelectionEx()
nodes = []
# if selection is not empty
for selobj in selection:
if Draft.getType(selobj.Object) == "FHNode":
nodes.append(selobj.Object)
if len(nodes) <= 1:
FreeCAD.Console.PrintWarning(translate("EM","Less than FHNodes selected when creating a FHEquiv. Nothing created."))
else:
FreeCAD.ActiveDocument.openTransaction(translate("EM","Create FHEquiv"))
FreeCADGui.addModule("EM")
for node_num in range(len(nodes)-1):
FreeCADGui.doCommand('obj=EM.makeFHEquiv(node1=FreeCAD.ActiveDocument.'+nodes[node_num].Name+',node2=FreeCAD.ActiveDocument.'+nodes[node_num+1].Name+')')
# autogrouping, for later on
#FreeCADGui.addModule("Draft")
#FreeCADGui.doCommand("Draft.autogroup(obj)")
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
if FreeCAD.GuiUp:
FreeCADGui.addCommand('EM_FHEquiv',_CommandFHEquiv())