-
Notifications
You must be signed in to change notification settings - Fork 9
/
lattice2ProjectArray.py
249 lines (200 loc) · 12.3 KB
/
lattice2ProjectArray.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
#***************************************************************************
#* *
#* Copyright (c) 2015 - Victor Titov (DeepSOIC) *
#* <[email protected]> *
#* *
#* 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 as App
import Part
from lattice2Common import *
import lattice2BaseFeature
import lattice2CompoundExplorer as LCE
import lattice2Executer
import lattice2GeomUtils as Utils
__title__="Lattice ProjectArray module for FreeCAD"
__author__ = "DeepSOIC"
__url__ = ""
# -------------------------- common stuff --------------------------------------------------
def makeProjectArray(name):
'''makeProjectArray(name): makes a Lattice ProjectArray object.'''
return lattice2BaseFeature.makeLatticeFeature(name, LatticeProjectArray, ViewProviderProjectArray)
class LatticeProjectArray(lattice2BaseFeature.LatticeFeature):
"The Lattice ProjectArray object"
def derivedInit(self,obj):
self.Type = "LatticeProjectArray"
obj.addProperty("App::PropertyLink","Base","Lattice ProjectArray","Array to be altered")
obj.addProperty("App::PropertyLink","Tool","Lattice ProjectArray","Shape to project onto")
obj.addProperty("App::PropertyEnumeration","TranslateMode","Lattice ProjectArray","")
obj.TranslateMode = ['keep','projected','mixed']
obj.TranslateMode = 'projected'
obj.addProperty("App::PropertyFloat","PosMixFraction","Lattice ProjectArray","Value controls mixing of positioning between the shape and the placement. 0 is on shape, 1 is at placement.")
obj.addProperty("App::PropertyDistance","PosElevation","Lattice ProjectArray","Extra displacement along normal to face or along gap, away from the shape.")
obj.addProperty("App::PropertyEnumeration","OrientMode","Lattice ProjectArray","")
obj.OrientMode = ['keep','along gap','tangent plane','along u', 'along v']
obj.OrientMode = 'along u'
obj.addProperty("App::PropertyEnumeration","Multisolution","Lattice ProjectArray","Specify the way of dealing with multiple solutions of projection")
obj.Multisolution = ['use first','use all']
def derivedExecute(self,obj):
#validity check
if not lattice2BaseFeature.isObjectLattice(screen(obj.Base)):
lattice2Executer.warning(obj,"A lattice object is expected as Base, but a generic shape was provided. It will be treated as a lattice object; results may be unexpected.")
toolShape = screen(obj.Tool).Shape
if lattice2BaseFeature.isObjectLattice(screen(obj.Tool)):
lattice2Executer.warning(obj,"A lattice object was provided as Tool. It will be converted into points; orientations will be ignored.")
leaves = LCE.AllLeaves(toolShape)
points = [Part.Vertex(leaf.Placement.Base) for leaf in leaves]
toolShape = Part.makeCompound(points)
leaves = LCE.AllLeaves(screen(obj.Base).Shape)
input = [leaf.Placement for leaf in leaves]
output = [] #variable to receive the final list of placements
#cache settings
elev = float(obj.PosElevation)
posIsKeep = obj.TranslateMode == 'keep'
posIsProjected = obj.TranslateMode == 'projected'
posIsMixed = obj.TranslateMode == 'mixed'
mixF = float(obj.PosMixFraction)
oriIsKeep = obj.OrientMode == 'keep'
oriIsAlongGap = obj.OrientMode == 'along gap'
oriIsTangentPlane = obj.OrientMode == 'tangent plane'
oriIsAlongU = obj.OrientMode == 'along u'
oriIsAlongV = obj.OrientMode == 'along v'
isMultiSol = obj.Multisolution == 'use all'
for plm in input:
v = Part.Vertex(plm.Base)
projection = v.distToShape(toolShape)
(dist, gaps, infos) = projection
for iSol in range(0,len(gaps)):
(posKeep, posPrj) = gaps[iSol]
(dummy, dummy, dummy, el_topo, el_index, el_params) = infos[iSol]
# Fetch all possible parameters (some may not be required, depending on modes)
normal = posKeep - posPrj
if normal.Length < DistConfusion:
normal = None
tangU = None
tangV = None
if el_topo == 'Face':
face = toolShape.Faces[el_index]
if normal is None:
normal = face.normalAt(*el_params)
(tangU, tangV) = face.tangentAt(*el_params)
elif el_topo == "Edge":
edge = toolShape.Edges[el_index]
tangU = edge.tangentAt(el_params)
if normal is not None:
normal.normalize()
#mode logic - compute new placement
if posIsKeep:
pos = plm.Base
elif posIsProjected:
pos = posPrj
elif posIsMixed:
pos = posKeep*mixF + posPrj*(1-mixF)
else:
raise ValueError("Positioning mode not implemented: " + obj.TranslateMode )
if abs(elev) > DistConfusion:
if normal is None:
raise ValueError("Normal vector not available for a placement resting on " + el_topo +". Normal vector is required for nonzero position elevation.")
pos += normal * elev
if oriIsKeep:
ori = plm.Rotation
elif oriIsAlongGap:
if normal is None:
raise ValueError("Normal vector not available for a placement resting on " + el_topo +". Normal vector is required for orientation mode '"+obj.OrientMode+"'")
ori = Utils.makeOrientationFromLocalAxesUni("X",XAx= normal*(-1.0))
elif oriIsTangentPlane:
if normal is None:
raise ValueError("Normal vector not available for a placement resting on " + el_topo +". Normal vector is required for orientation mode '"+obj.OrientMode+"'")
ori = Utils.makeOrientationFromLocalAxesUni("Z",ZAx= normal)
elif oriIsAlongU:
if normal is None:
raise ValueError("Normal vector not available for a placement resting on " + el_topo +". Normal vector is required for orientation mode '"+obj.OrientMode+"'")
if tangU is None:
raise ValueError("TangentU vector not available for point on " + el_topo +". TangentU vector is required for orientation mode '"+obj.OrientMode+"'")
ori = Utils.makeOrientationFromLocalAxesUni("ZX",ZAx= normal, XAx= tangU)
elif oriIsAlongV:
if normal is None:
raise ValueError("Normal vector not available for a placement resting on " + el_topo +". Normal vector is required for orientation mode '"+obj.OrientMode+"'")
if tangV is None:
raise ValueError("TangentV vector not available for point on " + el_topo +". TangentV vector is required for orientation mode '"+obj.OrientMode+"'")
ori = Utils.makeOrientationFromLocalAxesUni("ZX",ZAx= normal, XAx= tangV)
else:
raise ValueError("Orientation mode not implemented: " + obj.OrientMode )
output.append(App.Placement(pos,ori))
if not isMultiSol:
break
return output
class ViewProviderProjectArray(lattice2BaseFeature.ViewProviderLatticeFeature):
"A View Provider for the Lattice ProjectArray object"
def getIcon(self):
return getIconPath("Lattice2_ProjectArray.svg")
def claimChildren(self):
weakparenting = App.ParamGet("User parameter:BaseApp/Preferences/Mod/Lattice2").GetBool("WeakParenting", True)
if weakparenting:
return []
return [screen(self.Object.Base)]
def CreateLatticeProjectArray(name):
sel = FreeCADGui.Selection.getSelectionEx()
# selection order independence logic (lattice object and generic shape stencil can be told apart)
iLtc = 0 #index of lattice object in selection
iStc = 1 #index of stencil object in selection
for i in range(0,len(sel)):
if lattice2BaseFeature.isObjectLattice(sel[i]):
iLtc = i
iStc = i-1 #this may give negative index, but python accepts negative indexes
break
FreeCAD.ActiveDocument.openTransaction("Create ProjectArray")
FreeCADGui.addModule("lattice2ProjectArray")
FreeCADGui.addModule("lattice2Executer")
FreeCADGui.doCommand("sel = Gui.Selection.getSelectionEx()")
FreeCADGui.doCommand("f = lattice2ProjectArray.makeProjectArray(name = '"+name+"')")
FreeCADGui.doCommand("f.Base = App.ActiveDocument."+sel[iLtc].ObjectName)
FreeCADGui.doCommand("f.Tool = App.ActiveDocument."+sel[iStc].ObjectName)
FreeCADGui.doCommand("for child in f.ViewObject.Proxy.claimChildren():\n"+
" child.ViewObject.hide()")
FreeCADGui.doCommand("lattice2Executer.executeFeature(f)")
FreeCADGui.doCommand("f = None")
FreeCAD.ActiveDocument.commitTransaction()
# -------------------------- /common stuff --------------------------------------------------
# -------------------------- Gui command --------------------------------------------------
class _CommandProjectArray:
"Command to create Lattice ProjectArray feature"
def GetResources(self):
return {'Pixmap' : getIconPath("Lattice2_ProjectArray.svg"),
'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_ProjectArray","Project Array"),
'Accel': "",
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_ProjectArray","Project Array: alter placements based on their proximity to a shape.")}
def Activated(self):
sel = FreeCADGui.Selection.getSelectionEx()
if len(sel) == 2:
CreateLatticeProjectArray(name= "Project")
else:
mb = QtGui.QMessageBox()
mb.setIcon(mb.Icon.Warning)
mb.setText(translate("Lattice2_ProjectArray", "Select one lattice object to be projected, and one shape to project onto, first!", None))
mb.setWindowTitle(translate("Lattice2_ProjectArray","Bad selection", None))
mb.exec_()
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
if FreeCAD.GuiUp:
FreeCADGui.addCommand('Lattice2_ProjectArray', _CommandProjectArray())
exportedCommands = ['Lattice2_ProjectArray']
# -------------------------- /Gui command --------------------------------------------------