forked from kbwbe/A2plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha2p_MuxAssembly.py
241 lines (154 loc) · 6.79 KB
/
a2p_MuxAssembly.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
#***************************************************************************
#* *
#* Copyright (c) 2018 kbwbe *
#* *
#* Portions of code based on hamish's assembly 2 *
#* *
#* 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, FreeCADGui
from a2plib import *
import Part
import os, numpy
from random import random, choice
from FreeCAD import Base
import time
import a2plib
from PySide import QtGui
class Proxy_muxAssemblyObj:
def execute(self, shape):
pass
def createTopoInfo(obj): #deactivated at moment...
return []
def muxObjectsWithKeys(doc, withColor=False):
'''
combines all the imported shape object in doc into one shape,
ist able to import colors
'''
faces = []
faceColors = []
muxInfo = [] # List of keys, not used at moment...
visibleObjects = [ obj for obj in doc.Objects
if hasattr(obj,'ViewObject') and obj.ViewObject.isVisible()
and hasattr(obj,'Shape') and len(obj.Shape.Faces) > 0 and 'Body' not in obj.Name]
for obj in visibleObjects:
# Save Computing time, store this before the for..enumerate loop later...
colorFlag = ( len(obj.ViewObject.DiffuseColor) < len(obj.Shape.Faces) )
shapeCol = obj.ViewObject.ShapeColor
diffuseCol = obj.ViewObject.DiffuseColor
# now start the loop with use of the stored values..(much faster)
for i, face in enumerate(obj.Shape.Faces):
faces.append(face)
if withColor:
if colorFlag:
faceColors.append(shapeCol)
else:
faceColors.append(diffuseCol[i])
shell = Part.makeShell(faces)
if withColor:
return muxInfo, shell, faceColors
else:
return muxInfo, shell
def muxObjects(doc, mode=0):
'combines all the imported shape object in doc into one shape'
faces = []
if mode == 1:
objects = doc.getSelection()
else:
objects = doc.Objects
for obj in objects:
if 'importPart' in obj.Content:
faces = faces + obj.Shape.Faces
shell = Part.makeShell(faces)
return shell
class SimpleAssemblyShape:
def __init__(self, obj):
obj.addProperty("App::PropertyString", "type").type = 'SimpleAssemblyShape'
obj.addProperty("App::PropertyFloat", "timeOfGenerating").timeOfGenerating = time.time()
obj.Proxy = self
def onChanged(self, fp, prop):
pass
def execute(self, fp):
pass
class ViewProviderSimpleAssemblyShape:
def __init__(self,obj):
obj.Proxy = self
def onDelete(self, viewObject, subelements):
return True
def __getstate__(self):
return None
def __setstate__(self, state):
return None
def getIcon(self):
return a2plib.path_a2p + '/icons/simpleAssemblyShape.svg'
def attach(self, obj):
self.object_Name = obj.Object.Name
self.Object = obj.Object
def getDisplayModes(self,obj):
"Return a list of display modes."
modes=[]
modes.append("Shaded")
modes.append("Wireframe")
modes.append("Flat Lines")
return modes
def getDefaultDisplayMode(self):
"Return the name of the default display mode. It must be defined in getDisplayModes."
return "Flat Lines"
def setDisplayMode(self,mode):
return mode
def createOrUpdateSimpleAssemblyShape(doc):
visibleImportObjects = [ obj for obj in doc.Objects
if 'importPart' in obj.Content
and hasattr(obj,'ViewObject')
and obj.ViewObject.isVisible()
and hasattr(obj,'Shape')
and len(obj.Shape.Faces) > 0
]
if len(visibleImportObjects) == 0:
QtGui.QMessageBox.critical( QtGui.QApplication.activeWindow(),
"Cannot create SimpleAssemblyShape",
"No visible ImportParts found"
)
return
sas = doc.getObject('SimpleAssemblyShape')
if sas == None:
sas = doc.addObject("Part::FeaturePython","SimpleAssemblyShape")
SimpleAssemblyShape(sas)
#sas.ViewObject.Proxy = 0
ViewProviderSimpleAssemblyShape(sas.ViewObject)
faces = []
for obj in visibleImportObjects:
faces = faces + obj.Shape.Faces
shell = Part.makeShell(faces)
sas.Shape = shell
sas.ViewObject.Visibility = False
class a2p_SimpleAssemblyShapeCommand():
def GetResources(self):
import a2plib
return {'Pixmap' : a2plib.path_a2p +'/icons/a2p_simpleAssemblyShape.svg',
'MenuText': "create or refresh simple Shape of complete Assembly",
'ToolTip': "create or refresh simple Shape of complete Assembly"
}
def Activated(self):
if FreeCAD.ActiveDocument == None: return
doc = FreeCAD.ActiveDocument
createOrUpdateSimpleAssemblyShape(doc)
doc.recompute()
def IsActive(self):
return True
FreeCADGui.addCommand('a2p_SimpleAssemblyShapeCommand',a2p_SimpleAssemblyShapeCommand())