-
Notifications
You must be signed in to change notification settings - Fork 9
/
lattice2LinearArray.py
276 lines (223 loc) · 12.7 KB
/
lattice2LinearArray.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#***************************************************************************
#* *
#* 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 *
#* *
#***************************************************************************
__title__="Linear array feature module for lattice workbench for FreeCAD"
__author__ = "DeepSOIC"
__url__ = ""
import math
import FreeCAD as App
import Part
from lattice2Common import *
import lattice2BaseFeature
from lattice2BaseFeature import assureProperty
import lattice2Executer
import lattice2GeomUtils
from lattice2ValueSeriesGenerator import ValueSeriesGenerator
from lattice2Utils import sublinkFromApart, syncSublinkApart
def makeLinearArray(name):
'''makeLinearArray(name): makes a LinearArray object.'''
return lattice2BaseFeature.makeLatticeFeature(name, LinearArray, ViewProviderLinearArray)
class LinearArray(lattice2BaseFeature.LatticeFeature):
"The Lattice LinearArray object"
def derivedInit(self,obj):
self.Type = "LatticeLinearArray"
obj.addProperty("App::PropertyVector","Dir","Lattice Array","Vector that defines axis direction")
obj.Dir = App.Vector(1,0,0)
obj.addProperty("App::PropertyVector","Point","Lattice Array","Position of base (the point through which the axis passes, and from which positions of elements are measured)")
obj.addProperty("App::PropertyLink","Link","Lattice Array","Link to the axis (Edge1 is used for the axis).")
obj.addProperty("App::PropertyString","LinkSubelement","Lattice Array","subelement to take from axis link shape")
obj.addProperty("App::PropertyBool","Reverse","Lattice Array","Set to true to reverse direction")
obj.addProperty("App::PropertyBool","DirIsDriven","Lattice Array","If True, Dir property is driven by link.")
obj.DirIsDriven = True
obj.addProperty("App::PropertyBool","PointIsDriven","Lattice Array","If True, AxisPoint is not updated based on the link.")
obj.PointIsDriven = True
obj.addProperty("App::PropertyEnumeration","DrivenProperty","Lattice Array","Select, which property is to be driven by length of axis link.")
obj.DrivenProperty = ['None','Span','SpanStart','SpanEnd','Step']
obj.DrivenProperty = 'Span'
obj.addProperty("App::PropertyEnumeration","OrientMode","Lattice Array","Orientation of elements")
obj.OrientMode = ['None','Along axis']
obj.OrientMode = 'Along axis'
self.assureGenerator(obj)
obj.ValuesSource = "Generator"
obj.GeneratorMode = "StepN"
obj.EndInclusive = True
obj.SpanStart = 0.0
obj.SpanEnd = 12.0
obj.Step = 3.0
obj.Count = 5.0
self.assureProperties(obj)
def updateReadonlyness(self, obj):
link = screen(obj.Link)
obj.setEditorMode("Dir", 1 if (link and obj.DirIsDriven) else 0)
obj.setEditorMode("Point", 1 if (link and obj.PointIsDriven) else 0)
obj.setEditorMode("DirIsDriven", 0 if link else 1)
obj.setEditorMode("PointIsDriven", 0 if link else 1)
obj.setEditorMode("DrivenProperty", 0 if link else 1)
self.generator.updateReadonlyness()
def assureGenerator(self, obj):
'''Adds an instance of value series generator, if one doesn't exist yet.'''
if hasattr(self,"generator"):
return
self.generator = ValueSeriesGenerator(obj)
self.generator.addProperties(groupname= "Lattice Array",
groupname_gen= "Lattice Series Generator",
valuesdoc= "List of distances. Distance is measured from Point, along Dir, in millimeters.",
valuestype= "App::PropertyDistance")
self.updateReadonlyness(obj)
def assureProperties(self, selfobj):
assureProperty(selfobj, "App::PropertyLinkSub", "SubLink", sublinkFromApart(screen(selfobj.Link), selfobj.LinkSubelement), "Lattice Array", "Mirror of Object+SubNames properties")
def derivedExecute(self,obj):
self.assureGenerator(obj)
self.assureProperties(obj)
self.updateReadonlyness(obj)
# Apply links
if screen(obj.Link):
if lattice2BaseFeature.isObjectLattice(screen(obj.Link)):
lattice2Executer.warning(obj,"For polar array, axis link is expected to be a regular shape. Lattice objct was supplied instead, it's going to be treated as a generic shape.")
#resolve the link
if len(obj.LinkSubelement) > 0:
linkedShape = screen(obj.Link).Shape.getElement(obj.LinkSubelement)
else:
linkedShape = screen(obj.Link).Shape
#Type check
if linkedShape.ShapeType != 'Edge':
raise ValueError('Axis link must be an edge; it is '+linkedShape.ShapeType+' instead.')
if type(linkedShape.Curve) is not Part.Line:
raise ValueError('Axis link must be a line; it is '+type(linkedShape.Curve)+' instead.')
#obtain
start_point = linkedShape.valueAt(linkedShape.FirstParameter)
end_point = linkedShape.valueAt(linkedShape.LastParameter)
dir = end_point - start_point
point = start_point if not obj.Reverse else end_point
if obj.DirIsDriven:
obj.Dir = dir
if obj.PointIsDriven:
obj.Point = point
if obj.DrivenProperty != 'None':
if obj.DrivenProperty == 'Span':
propname = "SpanEnd"
obj.SpanEnd = obj.SpanStart + App.Units.Quantity('mm')*dir.Length
else:
propname = obj.DrivenProperty
setattr(obj, propname, dir.Length)
if self.generator.isPropertyControlledByGenerator(propname):
lattice2Executer.warning(obj, "Property "+propname+" is driven by both generator and link. Generator has priority.")
# Generate series of values
self.generator.execute()
values = [float(strv) for strv in obj.Values]
#Apply reversal
if obj.Reverse:
obj.Dir = obj.Dir*(-1.0)
if not(obj.DirIsDriven and screen(obj.Link)):
obj.Reverse = False
# precompute orientation
if obj.OrientMode == 'Along axis':
ori = lattice2GeomUtils.makeOrientationFromLocalAxes(ZAx= obj.Dir).multiply(
lattice2GeomUtils.makeOrientationFromLocalAxes(ZAx= App.Vector(1,0,0), XAx= App.Vector(0,0,1)) )
else:
ori = App.Rotation()
dir = obj.Dir
dir.normalize()
# Make the array
output = [] # list of placements
for v in values:
output.append( App.Placement(obj.Point + obj.Dir*v, ori) )
return output
def onChanged(self, selfobj, prop): #prop is a string - name of the property
# synchronize SubLink and Object+SubNames properties
syncSublinkApart(selfobj, prop, 'SubLink', 'Link', 'LinkSubelement')
return lattice2BaseFeature.LatticeFeature.onChanged(self, selfobj, prop)
class ViewProviderLinearArray(lattice2BaseFeature.ViewProviderLatticeFeature):
def getIcon(self):
return getIconPath('Lattice2_LinearArray.svg')
# -------------------------- /document object --------------------------------------------------
# -------------------------- Gui command --------------------------------------------------
def CreateLinearArray(name, mode):
sel = FreeCADGui.Selection.getSelectionEx()
FreeCAD.ActiveDocument.openTransaction("Create LinearArray")
FreeCADGui.addModule("lattice2LinearArray")
FreeCADGui.addModule("lattice2Executer")
FreeCADGui.addModule("lattice2Base.Autosize")
FreeCADGui.doCommand("f = lattice2LinearArray.makeLinearArray(name='"+name+"')")
if len(sel) == 1:
FreeCADGui.doCommand("f.Link = App.ActiveDocument."+sel[0].ObjectName)
if sel[0].HasSubObjects:
FreeCADGui.doCommand("f.LinkSubelement = '"+sel[0].SubElementNames[0]+"'")
FreeCADGui.doCommand("f.GeneratorMode = {mode}".format(mode= repr(mode)))
FreeCADGui.doCommand("f.Placement.Base = lattice2Base.Autosize.convenientPosition()")
FreeCADGui.doCommand("f.SpanEnd = lattice2Base.Autosize.convenientModelSize()")
FreeCADGui.doCommand("f.Step = lattice2Base.Autosize.convenientMarkerSize()")
FreeCADGui.doCommand("lattice2Executer.executeFeature(f)")
FreeCAD.ActiveDocument.commitTransaction()
FreeCADGui.doCommand("Gui.Selection.clearSelection()")
FreeCADGui.doCommand("Gui.Selection.addSelection(f)")
class CommandLinearArray:
"Command to create LinearArray feature"
def __init__(self, mode):
self.mode = mode
def GetResources(self):
mode_tooltips = {
'SpanN': "fit N placements into Span",
'StepN': "make N placements spaced by Step",
'SpanStep': "fill Span with placements spaced by Step",
'Random': "put N placements into Span randomly",
}
return {'Pixmap' : getIconPath("Lattice2_LinearArray_New.svg"),
'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_LinearArray","Linear array: {mode}")
.format(mode= ValueSeriesGenerator.mode_userfriendly_names[self.mode]),
'Accel': "",
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_LinearArray","Make a linear array of placements ({mode_tooltip})")
.format(mode_tooltip= mode_tooltips[self.mode])}
def Activated(self):
if len(FreeCADGui.Selection.getSelection()) < 2 :
try:
CreateLinearArray(name= "LinearArray", mode= self.mode)
except Exception as err:
msgError(err)
else:
infoMessage(translate("Lattice2_LinearArray","Bad selection", None),
translate("Lattice2_LinearArray", "Either don't select anything, or select a linear edge to serve an axis. You have selected more than one object - too much.", None))
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
_listOfSubCommands = []
for m in ValueSeriesGenerator.gen_modes:
cmd_name = 'Lattice2_LinearArray_'+m
_listOfSubCommands.append(cmd_name)
if FreeCAD.GuiUp:
FreeCADGui.addCommand(cmd_name, CommandLinearArray(m))
class GroupCommandLinearArray:
def GetCommands(self):
global _listOfSubCommands
return tuple(_listOfSubCommands) # a tuple of command names that you want to group
def GetDefaultCommand(self): # return the index of the tuple of the default command. This method is optional and when not implemented '0' is used
return 0
def GetResources(self):
return { 'MenuText': 'Linear Array', 'ToolTip': 'Linear Array: array of placements on a line.'}
def IsActive(self): # optional
return FreeCAD.ActiveDocument is not None
if FreeCAD.GuiUp:
FreeCADGui.addCommand('Lattice2_LinearArray_GroupCommand',GroupCommandLinearArray())
exportedCommands = ['Lattice2_LinearArray_GroupCommand']
# -------------------------- /Gui command --------------------------------------------------