Message 1 of 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I've organised my project using a master document and several derived documents, also derived from derived.
I've a script with looping the following:
- change parameter in master document
- save master document
- update derived documents
- export step and jpg of components
- save derived documents
PROBLEM
Using the following save function, each saving command becomes slower and slower (from 1 second to thousands seconds).
# save document and wait until finished
def saveDocument(doc):
if doc.isModified:
doc.save('')
# Spin until the save is complete
startTime = time.time()
log("SAVING: "+format(doc.dataFile.name))
while not doc.dataFile.isComplete:
time.sleep(0.5)
adsk.doEvents()
log(".... saved in "+ format(int(time.time() - startTime)) +" seconds")
Here below the log showing the progressive degradation of saving performances
20:57:44 > ******************************************** PROCESS STARTED
FILES
20:57:44 > 00_door_master v274
20:57:44 > 02_00_BRI_OGL v47
20:57:44 > 02_01_FLU_OGL v40
************
20:57:49 > processing document: 00_door_master v274
20:57:59 > SAVING: 00_door_master
20:58:01 > .... saved in 1 seconds
20:58:01 > processing document: 02_00_BRI_OGL v47
20:58:23 > SAVING: 02_00_BRI_OGL
20:58:48 > .... saved in 25 seconds
20:58:48 > processing document: 02_01_FLU_OGL v40
20:59:03 > SAVING: 02_01_FLU_OGL
21:00:20 > .... saved in 77 seconds
21:00:24 > processing document: 00_door_master v275
21:00:35 > SAVING: 00_door_master
21:03:14 > .... saved in 158 seconds
21:03:14 > processing document: 02_00_BRI_OGL v48
21:03:36 > SAVING: 02_00_BRI_OGL
21:08:20 > .... saved in 283 seconds
21:08:20 > processing document: 02_01_FLU_OGL v41
21:08:33 > SAVING: 02_01_FLU_OGL
21:15:27 > .... saved in 414 seconds
21:15:31 > processing document: 00_door_master v276
21:15:40 > SAVING: 00_door_master
21:24:32 > .... saved in 531 seconds
21:24:36 > processing document: 02_00_BRI_OGL v49
21:24:59 > SAVING: 02_00_BRI_OGL
21:35:16 > .... saved in 617 seconds
21:35:19 > processing document: 02_01_FLU_OGL v42
21:35:33 > SAVING: 02_01_FLU_OGL
21:47:57 > .... saved in 744 seconds
21:48:04 > processing document: 00_door_master v277
21:48:14 > SAVING: 00_door_master
22:02:26 > .... saved in 852 seconds
22:02:27 > processing document: 02_00_BRI_OGL v50
22:02:48 > SAVING: 02_00_BRI_OGL
22:18:36 > .... saved in 948 seconds
22:18:36 > processing document: 02_01_FLU_OGL v43
22:18:48 > SAVING: 02_01_FLU_OGL
22:37:14 > .... saved in 1106 seconds
22:37:19 > processing document: 00_door_master v278
22:37:30 > SAVING: 00_door_master
Below the complete script, apologise me for the poor syntax (I'm not a developer)
import adsk.core, adsk.fusion, adsk.cam, traceback
import os.path, sys
import platform
import os
import re
import time
class FileLogger:
def __init__(self, filePath):
try:
open(filePath, 'a').close()
self.filePath = filePath
except:
raise Exception("Could not open/create file = " + filePath)
def print(self, text):
with open(self.filePath, 'a') as txtFile:
txtFile.writelines( get_time() + ' > ' + text + '\r\n' )
def get_time():
current_time = time.time()
gmt_plus_1_offset = 3600*2 # Time zone offset for GMT+1 in seconds
gmt_plus_1_time = current_time + gmt_plus_1_offset
formatted_time = time.strftime("%H:%M:%S", time.gmtime(gmt_plus_1_time))
return formatted_time
def run(context):
try:
# set up logging
logPath = os.path.join(os.path.expanduser('~'), 'Desktop/out/__log.txt')
logF = FileLogger(logPath)
def log(msg):
logF.print(msg)
app.log(msg)
app = adsk.core.Application.get()
ui = app.userInterface
exported_components = set()
log("******************************************** PROCESS STARTED")
def hideAllBodies(comp):
for body in comp.bRepBodies:
body.isVisible=False
def isolate(comp,body):
comp.isIsolated = True
hideAllBodies(comp)
body.isVisible=True
def unisolate(comp):
comp.isIsolated = False
#export all componentes of active document as step and make jpeg
def export_step_jpg():
try:
# get active design
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
# get all components in this design
allComps = design.allComponents
# set export locations
folderPath = os.path.join(os.path.expanduser('~'), 'Desktop/out')
# create a single exportManager instance
exportMgr = design.exportManager
# remove visibility
for component in allComps:
for body in component.bRepBodies:
body.isVisible=False
# export the component one by one with a specified format
for component in allComps:
if "§" in component.name:
fileName = component.name.lstrip("§") # remove § at the beginning if present
fileName = fileName.replace("W§", _W)
fileName = fileName.replace("D§", _D)
fileName = fileName.replace("N§", _N)
if fileName not in exported_components:
app.log('...exporting component: ' + fileName)
fileName = os.path.join(folderPath, fileName)
stpOptions = exportMgr.createSTEPExportOptions(fileName, component)
fileNameThumb = fileName + ".jpg"
# export step + jpg
for body in component.bRepBodies:
body.isVisible = True
vp.refresh
returnValue = vp.fit()
time.sleep(2)
exportMgr.execute(stpOptions)
vp.saveAsImageFile(fileNameThumb, 600, 600)
body.isVisible = False
exported_components.add(fileName)
else:
log('******************Component already exported: ' + fileName)
# restore visibility
for component in allComps:
if "§" in component.name:
for body in component.bRepBodies:
body.isVisible=True
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
# save document and wait until finished
def saveDocument(doc):
if doc.isModified:
doc.save('')
# Spin until the save is complete, up to 10 seconds and then abort.
startTime = time.time()
log("SAVING: "+format(doc.dataFile.name))
while not doc.dataFile.isComplete:
time.sleep(0.5)
adsk.doEvents()
log(".... saved in "+ format(int(time.time() - startTime)) +" seconds")
else:
log("ALREADY SAVED: "+format(doc.dataFile.name))
# Store open documents in an array sorted by document.name (the first document, e.g. 00_???, is the master document with parameters to be changed)
documents = app.documents
_open_documents = sorted(
(document for document in documents if len(document.name) >= 3 and document.name[:2].isdigit() and document.name[2] == "_"),
key=lambda doc: doc.name
)
for document in _open_documents:
log(document.name)
_master_document = _open_documents[0]
_master_document.activate()
_master_design = adsk.fusion.Design.cast(app.activeProduct)
_master_params = _master_design.userParameters
for W in ["1","2","3"]:
for D in ["1"]:
for N in ["3","4","5","6","8"]:
caseW = {
"0": "W0",
"1": "W1",
"2": "W2",
"3": "W3",
"3": "W4"
}
_W = caseW[W]
caseD = {
"1": "D1",
"2": "D2",
"3": "D3"
}
_D = caseD[D]
caseN = {
"3": "N3",
"4": "N4",
"5": "N5",
"6": "N6",
"8": "N8",
}
_N = caseN[N]
# Set parameters in the loop
_master_params.itemByName("SET_CABINET_W").expression = W
_master_params.itemByName("SET_CABINET_DPT").expression = D
_master_params.itemByName("SET_LOCKERS_NUM").expression = N
for document in _open_documents:
document.activate()
vp = app.activeViewport
log("processing document: " + document.name)
app.executeTextCommand(u'Commands.Start PLM360DeepRefreshDocumentCommand')
export_step_jpg()
saveDocument(document)
log("******************************************** PROCESS FINISHED")
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Due to performance degradation, the script is not usable. Any idea on how to make saving faster?
Solved! Go to Solution.