Opening & closing of multiple models in background while completing a set of tasks

revit
python
dynamo

#1

Hi all, just start dynamo scripting not long ago. Any help is much appreciated.
I am trying to achieve a series of actions in sequence without opening the actual Revit model.
The actions are:

  1. Open(detach from central & Audit) model while preserve all workset
  2. Unload all type of links
  3. Save as new central to another location with same name
  4. Relinquish all attributes

I have a bulk of models that required for these actions to be done sequentially, opening 1 model at a time and complete the actions stated above.
I had manage to complete action 1,3 & 4 but could not have any luck with 2. Picture below is the graph that i had come up with from various custom packages and the python script was drafted up by me with multiple reference, awesome learning platform here, but it has a warning of
“Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
The iterator cannot proceed due to changes made to the Element table in Revit’s database (typically, This can be the result of an Element deletion).”

Below is the script

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# Import python library
import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)
import os

# Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Analysis import *

# Import Revit Nodes
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)


tOptions = TransactWithCentralOptions()

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

if isinstance(IN[0], list):
	file = IN[0]
	filename = IN[2]
else:
	file = [IN[0]]
	filename = [IN[2]]

newpath = IN[1]
Unloadedlinks = []
newdocpath = []
booleans = list()

oOptions = OpenOptions()
oOptions.DetachFromCentralOption = DetachFromCentralOption.DetachAndPreserveWorksets
oOptions.Audit = True

worksharingOptions = WorksharingSaveAsOptions()
worksharingOptions.SaveAsCentral = True

SaveOptions = SaveAsOptions()
SaveOptions.MaximumBackups = 50
SaveOptions.SetWorksharingOptions(worksharingOptions)
SaveOptions.OverwriteExistingFile = True

rOptions = RelinquishOptions(False)
rOptions.StandardWorksets = True
rOptions.ViewWorksets = True
rOptions.FamilyWorksets = True
rOptions.UserWorksets = True
rOptions.CheckedOutElements = True

sOptions = SynchronizeWithCentralOptions()
sOptions.SetRelinquishOptions(rOptions)
sOptions.Compact = True
sOptions.SaveLocalBefore = True
sOptions.SaveLocalAfter = True

TransactionManager.Instance.ForceCloseTransaction()
	
try:
	for files,filenames in zip(file,filename):
		modelpath = FilePath(files)
		newdoc = app.OpenDocumentFile(modelpath,oOptions)
		newfilepath = newpath + "\\" + filenames
		newdoc.SaveAs(newfilepath,SaveOptions)
		links = FilteredElementCollector(newdoc).OfClass(Autodesk.Revit.DB.RevitLinkType)
		for link in links:
			linkelement = newdoc.GetElement(link.GetTypeId())
			try:
				linkelement.Unload(None)
				booleans.append(True)
			except:
				booleans.append(False)
		newdoc.SynchronizeWithCentral(tOptions,sOptions)
		newdoc.Close(True)
		Unloadedlinks.append(links)
		newdocpath.append(newfilepath)
		OUT=[newdocpath,Unloadedlinks,booleans]
except Exception,e:
	OUT = str(e)

Thanks in advance :slight_smile:


#2

@Kulkul hey, sorry to disturb you but why is my post still hidden when my account is no longer on hold?

Thanks for getting back to me


#3

@stillgotme Its on now :slight_smile:


#4

Hey guys, any ideas?


#5

hey guys, anyone know why i get this error “attribute ‘RevitLinkInstances’ of ‘Namespace#’ object is read-only” when i change the RevitLinkType to RevitLinkInstances in the collection line?


#6

Well, i had solved my own issues. Thanks :slight_smile:


#7

any thoughts on why this would happen?


#8

using the script above


#9

@AdamHamilton try this instead
Detach_Unload_Save.dyn (13.8 KB)
If it still doesnt work then you will need to show me what is your input or what version of revit and dynamo you are using


#10

trick is i dont want to detach…i want to Open Local - Sync - and saveas a central in new location


#11

maybe you can start a new thread and link this post as reference since it is on different topic. then i will see what i can do to help you :slight_smile: