Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Saving document gets slower and slower with doc.dataFile.isComplete and adsk.doEvents()

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
luca.giorcelli9CG7C
279 Views, 2 Replies

Saving document gets slower and slower with doc.dataFile.isComplete and adsk.doEvents()

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:

  1. change parameter in master document
  2. save master document
  3. update derived documents
  4. export step and jpg of components
  5. 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?

2 REPLIES 2
Message 2 of 3

Hi @luca.giorcelli9CG7C .

 

I have not been able to fully test or understand it due to the lack of files.
I believe the purpose of this script is to export the step and jpg files.

 

I did not feel the need to wait until the save was complete.
I didn't feel there was any point in saving every time I changed a parameter in the first place.

Message 3 of 3

You are right, I've tested removing the "while not doc.dataFile.isComplete" check and everything seems working.
Thanks!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report