forked from kbwbe/A2plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
a2p_recursiveUpdatePlanner.py
223 lines (195 loc) · 8.9 KB
/
a2p_recursiveUpdatePlanner.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
#***************************************************************************
#* *
#* Copyright (c) 2018 kbwbe *
#* *
#* 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 os
import FreeCAD
import FreeCADGui
from PySide import QtGui
from a2p_translateUtils import *
import a2plib
from a2p_importpart import updateImportedParts
from a2p_simpleXMLreader import FCdocumentReader
#==============================================================================
def createUpdateFileList(
importPath,
parentAssemblyDir,
filesToUpdate,
recursive=False,
selectedFiles=[] # only update parts with these sourceFiles
):
# do not update converted parts
print(
"createUpdateFileList importPath = {}".format(
importPath
)
)
if a2plib.to_bytes(importPath) == b'converted':
return False, filesToUpdate
fileNameInProject = a2plib.findSourceFileInProject(
importPath,
parentAssemblyDir
)
workingDir,basicFileName = os.path.split(fileNameInProject)
docReader1 = FCdocumentReader()
docReader1.openDocument(fileNameInProject)
needToUpdate = False
subAsmNeedsUpdate = False
for ob in docReader1.getA2pObjects():
if a2plib.to_bytes(ob.getA2pSource()) == b'converted':
print(
translate("A2plus", "Did not update converted part '{}'").format(
ob.name
)
)
continue
# Only update parts which are selected by the user...
fDir,fName = os.path.split(ob.getA2pSource())
if len(selectedFiles) > 0 and fName not in selectedFiles:
continue
if ob.isSubassembly() and recursive:
subAsmNeedsUpdate, filesToUpdate = createUpdateFileList(
ob.getA2pSource(),
workingDir,
filesToUpdate,
recursive
)
if subAsmNeedsUpdate:
needToUpdate = True
objFileNameInProject = a2plib.findSourceFileInProject(
ob.getA2pSource(),
workingDir
)
mtime = os.path.getmtime(objFileNameInProject)
if ob.getTimeLastImport() < mtime:
needToUpdate = True
if needToUpdate:
if fileNameInProject not in filesToUpdate:
filesToUpdate.append(fileNameInProject)
return needToUpdate, filesToUpdate
#==============================================================================
toolTip = \
translate("A2plus",
"""
Update parts, which have been
imported to the assembly.
(If you modify a part in an
external file, the new shape
is taken to the assembly by
this function.)
This command does this recursively
over all involved subassemblies.
Subassemblies are updated,
if necessary, too.
"""
)
class a2p_recursiveUpdateImportedPartsCommand:
def Activated(self):
a2plib.setAutoSolve(True) # makes no sense without autosolve = ON
doc = FreeCAD.activeDocument()
if doc is None:
QtGui.QMessageBox.information(
QtGui.QApplication.activeWindow(),
translate("A2plus", "No active document found!"),
translate("A2plus", "Before recursive updating parts, you have to open an assembly file.")
)
return
fileName = doc.FileName
workingDir,basicFileName = os.path.split(fileName)
selectedFiles=[]
partial = False
selection = [s for s in FreeCADGui.Selection.getSelection()
if s.Document == FreeCAD.ActiveDocument and
(a2plib.isA2pPart(s) or a2plib.isA2pSketch(s))
]
if selection and len(selection)>0:
flags = QtGui.QMessageBox.StandardButton.Yes | QtGui.QMessageBox.StandardButton.No
msg = translate("A2plus", "Do you want to update only the selected parts?")
response = QtGui.QMessageBox.information(
QtGui.QApplication.activeWindow(),
translate("A2plus", "RECURSIVE UPDATE"),
msg,
flags
)
if response == QtGui.QMessageBox.Yes:
for s in selection:
fDir, fName = os.path.split(s.sourceFile)
selectedFiles.append(fName)
partial = True
filesToUpdate = []
subAsmNeedsUpdate, filesToUpdate = createUpdateFileList(
fileName,
workingDir,
filesToUpdate,
True,
selectedFiles
)
for f in filesToUpdate:
#-------------------------------------------
# update necessary documents
#-------------------------------------------
# look only for filenames, not paths, as there are problems on WIN10 (Address-translation??)
importDoc = None
importDocIsOpen = False
requestedFile = os.path.split(f)[1]
for d in FreeCAD.listDocuments().values():
recentFile = os.path.split(d.FileName)[1]
if requestedFile == recentFile:
importDoc = d # file is already open...
importDocIsOpen = True
break
if not importDocIsOpen:
if f.lower().endswith('.fcstd'):
importDoc = FreeCAD.openDocument(f)
elif f.lower().endswith('.stp') or f.lower().endswith('.step'):
import ImportGui
fname = os.path.splitext(os.path.basename(f))[0]
FreeCAD.newDocument(fname)
newname = FreeCAD.ActiveDocument.Name
FreeCAD.setActiveDocument(newname)
ImportGui.insert(filename,newname)
importDoc = FreeCAD.ActiveDocument
else:
QtGui.QMessageBox.information(
QtGui.QApplication.activeWindow(),
translate("A2plus", "Value Error"),
translate("A2plus", "A part can only be imported from a FreeCAD '*.fcstd' file")
)
return
if importDoc==doc and partial==True:
updateImportedParts(importDoc,True)
else:
updateImportedParts(importDoc)
FreeCADGui.updateGui()
importDoc.save()
FreeCAD.Console.PrintMessage(
translate("A2plus", "===== Assembly '{}' has been updated! =====\n").format(
importDoc.FileName
)
)
if importDoc != doc:
FreeCAD.closeDocument(importDoc.Name)
def GetResources(self):
return {
'Pixmap' : ':/icons/a2p_RecursiveUpdate.svg',
'MenuText': translate("A2plus", "Update imports recursively"),
'ToolTip' : toolTip
}
FreeCADGui.addCommand('a2p_recursiveUpdateImportedPartsCommand', a2p_recursiveUpdateImportedPartsCommand())