-
Notifications
You must be signed in to change notification settings - Fork 9
/
lattice2JoinArrays.py
149 lines (119 loc) · 6.6 KB
/
lattice2JoinArrays.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
#***************************************************************************
#* *
#* 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__="Lattice JoinArrays object: combine elements of two lattices"
__author__ = "DeepSOIC"
__url__ = ""
import FreeCAD as App
import Part
from lattice2Common import *
import lattice2BaseFeature
import lattice2CompoundExplorer as LCE
import lattice2Executer
# -------------------------- document object --------------------------------------------------
def makeJoinArrays(name):
'''makeJoinArrays(name): makes a JoinArrays object.'''
return lattice2BaseFeature.makeLatticeFeature(name, JoinArrays, ViewProviderJoinArrays)
class JoinArrays(lattice2BaseFeature.LatticeFeature):
"The Lattice JoinArrays object"
def derivedInit(self,obj):
self.Type = "LatticeJoinArrays"
obj.addProperty("App::PropertyLinkList","Links","Lattice JoinArrays","Links to arrays to be joined")
obj.addProperty("App::PropertyBool","Interleave","Lattice JoinArrays","If false, first go all elements of array 1, nect go all elements of array 2, so on. If true, first go all first elements from each array, then all second elements, and so on.")
def derivedExecute(self,obj):
#validity check
nonLattices = []
for iArr in range(0, len(obj.Links)):
link = obj.Links[iArr]
if not lattice2BaseFeature.isObjectLattice(link):
nonLattices.append(link.Label)
if len(nonLattices) > 0:
lattice2Executer.warning(obj, "Only lattice objects are expected to be linked as arrays in JoinArrays. There are "
+len(nonLattices)+" objects which are not lattice objects. Results may me unexpected.")
#extract placements
listlistPlms = []
lengths = []
for link in obj.Links:
leaves = LCE.AllLeaves(link.Shape)
listlistPlms.append([child.Placement for child in leaves])
lengths.append(len(leaves))
#processing
output = [] #list of placements
if obj.Interleave:
for l in lengths[1:]:
if l != lengths[0]:
lattice2Executer.warning(obj,"Array lengths are unequal: "+repr(lengths)+". Interleaving will be inconsistent.")
break
for iItem in range(0,max(lengths)):
for list in listlistPlms:
if iItem < len(list):
output.append(list[iItem])
else:
for list in listlistPlms:
output.extend(list)
return output
class ViewProviderJoinArrays(lattice2BaseFeature.ViewProviderLatticeFeature):
def getIcon(self):
return getIconPath('Lattice2_JoinArrays.svg')
def claimChildren(self):
return self.Object.Links
# -------------------------- /document object --------------------------------------------------
# -------------------------- Gui command --------------------------------------------------
def CreateJoinArrays(name):
sel = FreeCADGui.Selection.getSelection()
FreeCAD.ActiveDocument.openTransaction("Create JoinArrays")
FreeCADGui.addModule("lattice2JoinArrays")
FreeCADGui.addModule("lattice2Executer")
FreeCADGui.doCommand("f = lattice2JoinArrays.makeJoinArrays(name='"+name+"')")
FreeCADGui.doCommand("f.Links = []")
for s in sel:
FreeCADGui.doCommand("f.Links = f.Links + [App.ActiveDocument."+s.Name+"]")
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()
class _CommandJoinArrays:
"Command to create JoinArrays feature"
def GetResources(self):
return {'Pixmap' : getIconPath("Lattice2_JoinArrays.svg"),
'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_JoinArrays","Join arrays"),
'Accel': "",
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_JoinArrays","Lattice JoinArrays: concatenate or interleave two or more arrays.")}
def Activated(self):
if len(FreeCADGui.Selection.getSelection()) > 1 :
CreateJoinArrays(name = "Join")
else:
mb = QtGui.QMessageBox()
mb.setIcon(mb.Icon.Warning)
mb.setText(translate("Lattice2_JoinArrays", "Please select at least two lattice objects. Selected lattice objects will be concatenated or interleaved into one array.", None))
mb.setWindowTitle(translate("Lattice2_JoinArrays","Bad selection", None))
mb.exec_()
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
if FreeCAD.GuiUp:
FreeCADGui.addCommand('Lattice2_JoinArrays', _CommandJoinArrays())
exportedCommands = ['Lattice2_JoinArrays']
# -------------------------- /Gui command --------------------------------------------------