Assembly recursive update, Assembly 2

Discussion about the development of the Assembly workbench.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
engelant
Posts: 2
Joined: Sat Jun 03, 2017 9:27 pm

Re: Assembly recursive update

Post by engelant »

So, since there was no response at all I had to dig in myself.
I definitly need to figure out ab better IDE setup and maybe actually read functions exposed by FreeCADs API.
Anyhow, a little Macro:

Code: Select all

import FreeCAD
from PySide import QtGui

def _is_a_part(element):
	return (element is not None) and hasattr(element,'sourceFile')

def _update_parts(parts, name):
	App = FreeCAD
	for part in parts:
		was_open = False
		if part.sourceFile in [ d.FileName for d in FreeCAD.listDocuments().values() ]:
			doc = [ d for d in FreeCAD.listDocuments().values() if d.FileName == part.sourceFile][0]
			App.setActiveDocument(doc.Label)
			was_open = True
		else:
			doc = App.openDocument(part.sourceFile)
			App.setActiveDocument(doc.Label)
		App.ActiveDocument=App.getDocument(doc.Label)
		_update_parts(filter(_is_a_part, App.ActiveDocument.Objects), doc.Label)
		App.ActiveDocument.save()
		if not was_open:
			App.closeDocument(doc.Label)
	App.setActiveDocument(name)
	App.ActiveDocument=App.getDocument(name)
	for obj in App.ActiveDocument.Objects:
 		obj.touch()
	App.ActiveDocument.recompute()

App.ActiveDocument.save()
_update_parts(filter(_is_a_part, FreeCAD.ActiveDocument.Objects),  FreeCAD.ActiveDocument.Label)
What it does, it gets all objects inside the current document, which have the attribute sourceFile, thereby identifying the assembly parts.
For each of those parts, it's checked (and preserved), if they are already opened or must be opened. These parts are then opened/switched to (recursive), and done the same thing. After that all Objects are marked "dirty", the document gets recomputed, saved and then closed/switched.
After running this Macro it is required to use the reload function of the assembly2 bench to reload the parts.

Background:
For my small projects I like to be able to switch materials like they are availible, without having to hussle calculating new dimensions due to different thicknesses. Creating an Assembly file inside my project folder, with a spreadsheet next to the assembly provides me with that freedom, but updating the sub parts, which rely on the Assembly is a pain by hand.
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: Assembly recursive update

Post by triplus »

engelant wrote: Mon Jun 12, 2017 8:20 pm Anyhow, a little Macro:
Thanks for sharing.
User avatar
easyw-fc
Veteran
Posts: 3630
Joined: Thu Jul 09, 2015 9:34 am

Re: Assembly recursive update, Assembly 2

Post by easyw-fc »

nice trick indeed! :)
User avatar
silopolis
Posts: 74
Joined: Thu Oct 20, 2016 10:06 pm

Re: Assembly recursive update

Post by silopolis »

engelant wrote: Mon Jun 12, 2017 8:20 pm So, since there was no response at all I had to dig in myself.
I definitly need to figure out ab better IDE setup and maybe actually read functions exposed by FreeCADs API.
Anyhow, a little Macro:

Code: Select all

...
Hey but you did it !
Hadn't seen this message when posted my previous one... This is a great time saver I just installed as a macro button in my Assembly 2 toolbars :)

Now just wish I knew how to call the FreeCADGui updateImportedPartsCommand or UpdateImportedPartsCommand() at the end for final automation...

Oh and for those like me, don't forget to close and re-open your assembly file before trying this macro if you're testing from a new file !
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: Assembly recursive update, Assembly 2

Post by triplus »

Try out:

Code: Select all

import importPart
importPart.UpdateImportedPartsCommand().Activated()
Post Reply