forked from kbwbe/A2plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
a2p_convertPart.py
204 lines (171 loc) · 7.75 KB
/
a2p_convertPart.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
#***************************************************************************
#* *
#* Copyright (c) 2018 WandererFan <[email protected]> *
#* *
#* 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 FreeCADGui
import FreeCAD
from PySide import QtGui
import copy
import time
from a2p_translateUtils import *
import a2plib
from a2p_MuxAssembly import createTopoInfo
from a2p_versionmanagement import A2P_VERSION
from a2plib import (
appVersionStr,
AUTOSOLVE_ENABLED
)
from a2p_importedPart_class import Proxy_importPart
from a2p_importedPart_class import Proxy_convertPart # for compat.
from a2p_importedPart_class import ImportedPartViewProviderProxy # for compat.
from a2p_topomapper import TopoMapper
def updateConvertedPart(doc, obj):
obj.timeLastImport = time.time()
baseObject = doc.getObject(obj.localSourceObject)
savedPlacement = obj.Placement
obj.ViewObject.ShapeColor = baseObject.ViewObject.ShapeColor
topoMapper = TopoMapper(doc) # imports the objects and creates toponames if wanted
baseObject.ViewObject.Visibility = True #the topomapper ignores invisible shapes
obj.muxInfo, obj.Shape, obj.ViewObject.DiffuseColor, obj.ViewObject.Transparency = \
topoMapper.createTopoNames(desiredShapeLabel = baseObject.Label)
baseObject.ViewObject.Visibility = False #set baseObject invisible again.
obj.Placement = savedPlacement
for p in baseObject.ViewObject.PropertiesList:
if hasattr(baseObject.ViewObject, p) and p not in [
'DiffuseColor',
'Proxy',
'MappedColors',
'DisplayModeBody'
]:
try:
setattr(obj.ViewObject, p, getattr( baseObject.ViewObject, p))
except:
pass # a lot of attributes related e.g. to sketcher
if not a2plib.getPerFaceTransparency():
# switch of perFaceTransparency
obj.ViewObject.Transparency = 1
obj.ViewObject.Transparency = 0 # default = nontransparent
obj.recompute()
obj.ViewObject.Visibility = True
def convertToImportedPart(doc, obj):
"""
convertToImportedPart(document, documentObject) - changes a regular FreeCAD object into an A2plus
importedPart, adds the importedPart to the document and hides the original object from the
document. Updating the assembly will also update the converted part
"""
partName = a2plib.findUnusedObjectName( obj.Label, document=doc )
partLabel = a2plib.findUnusedObjectLabel( obj.Label, document=doc )
filename = "converted" #or none? if obj is already in this doc, we don't know it's original filename
newObj = doc.addObject("Part::FeaturePython",partName)
newObj.Label = partLabel
Proxy_importPart(newObj)
ImportedPartViewProviderProxy(newObj.ViewObject)
newObj.a2p_Version = A2P_VERSION
newObj.sourceFile = filename
newObj.localSourceObject = obj.Name
#newObj.sourcePart = ""
newObj.setEditorMode("timeLastImport",1)
newObj.timeLastImport = time.time()
newObj.fixedPosition = False
newObj.subassemblyImport = False
newObj.setEditorMode("subassemblyImport",1)
newObj.updateColors = True
newObj.ViewObject.ShapeColor = obj.ViewObject.ShapeColor
#-------------------------------------------
# Initialize the new TopoMapper
#-------------------------------------------
topoMapper = TopoMapper(doc)
newObj.muxInfo, newObj.Shape, newObj.ViewObject.DiffuseColor, newObj.ViewObject.Transparency = \
topoMapper.createTopoNames(desiredShapeLabel = obj.Label)
for p in obj.ViewObject.PropertiesList:
if hasattr(obj.ViewObject, p) and p not in [
'DiffuseColor',
'Proxy',
'MappedColors',
'DisplayModeBody'
]:
try:
setattr(newObj.ViewObject, p, getattr( obj.ViewObject, p))
except: #some sketcher attributes e.g.
pass
if not a2plib.getPerFaceTransparency():
# switch of perFaceTransparency
newObj.ViewObject.Transparency = 1
newObj.ViewObject.Transparency = 0 # default = nontransparent
newObj.Placement.Base = obj.Placement.Base
newObj.Placement.Rotation = obj.Placement.Rotation
obj.ViewObject.Visibility = False
newObj.recompute()
toolTip = \
translate("A2plus_convertPart",
'''
Convert a part, created with
another WB, to a full functional
A2plus part.
After converting, constraints
can be applied. Also you can
duplicate the converted part.
For editing a converted part,
hit the edit button and follow
the instructions shown on screen.
This function is useful, if
you want to use e.g. fasteners
within this workbench.
'''
)
class a2p_ConvertPartCommand():
def GetResources(self):
return {'Pixmap' : a2plib.pathOfModule()+'/icons/a2p_ConvertPart.svg',
# 'Accel' : "Shift+C", # a default shortcut (optional)
'MenuText': translate("A2plus_convertPart", "Convert a part to A2plus"),
'ToolTip' : toolTip
}
def Activated(self):
doc = FreeCAD.activeDocument()
selection = FreeCADGui.Selection.getSelection()
for s in selection:
if s.ViewObject.Visibility == False:
msg = translate("A2plus_convertPart","Please select only visible parts!")
QtGui.QMessageBox.information(
QtGui.QApplication.activeWindow(),
translate("A2plus_convertPart","Conversion Aborted"),
msg
)
return
for s in selection:
doc.openTransaction(u"part converted to A2plus")
convertToImportedPart(doc, s)
doc.commitTransaction()
def IsActive(self):
if FreeCAD.activeDocument() is None:
return False
selection = FreeCADGui.Selection.getSelection()
if not selection: return False
for s in selection:
if a2plib.isA2pPart(s): return False
if (
not s.isDerivedFrom("Part::Feature") and
not s.Name.startswith('Sketch')
):
return False
return True
FreeCADGui.addCommand('a2p_ConvertPart',a2p_ConvertPartCommand())