Bulk Save as Central

I need a way to open multiple files and save them as central worksharing models. I am able to create a list of file paths in excel for all the files I want to do this with.

The reason: We are moving to a different file storage system which means all the workshared Revit files will still point to the old location. This means that I would have to open all the Revit files individually and save as central model and close them.
It seems like this is something that could be done with a python script, but I am not skilled enough to make it happen.

1 Like

You’ll need access to the API through Python. This is the API you’ll probably need to start with – http://www.revitapidocs.com/2016/5db03f57-b7fa-a1fa-e8b2-289c80678e75.htm

Find a Python node that opens files and modify it from there. You gotta start somewhere!

1 Like

Did you manage this? I have the same issue and I’d love to stand on the shoulders of giants rather than start from scratch.

Thanks!

well, i believe this will do the trick. i personally used it for model migration and model upgrades.

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

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

import clr
clr.AddReference("RevitAPIUI")
from  Autodesk.Revit.UI import *

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

from System.Collections.Generic import *

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

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

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

worksharingOptions = WorksharingSaveAsOptions()
worksharingOptions.SaveAsCentral = True

SaveOptions = SaveAsOptions()
SaveOptions.MaximumBackups = 50
SaveOptions.SetWorksharingOptions(worksharingOptions)
SaveOptions.OverwriteExistingFile = True
SaveOptions.Compact = 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

tOptions = TransactWithCentralOptions()

TransactionManager.Instance.ForceCloseTransaction()    
filepaths = IN[0]

RVer = "R" + app.VersionNumber[-2:]

docpath = []

if IN[1] == True:
	try:
		for filepath in filepaths:
			file = FileInfo(filepath)
			filein = file.FullName
			modelpath = FilePath(filein)
			filename = filepath.Split("\\")[-1].Replace(".rvt","")
			newdoc = app.OpenDocumentFile(filepath)			
			if RVer not in filename:
				if "Central" in filename:
					x = filepath.split("_")
					x.insert(len(filepath.split("_"))-1,RVer)
					y = "_".join(x)
				else:
					x = filepath[:-4]
					y = x + "_" + RVer + ".rvt"
			else:
				y = filepath
			newdoc.SaveAs(y,SaveOptions)			
			newdoc.SynchronizeWithCentral(tOptions,sOptions)
			newdoc.Close(True)
			docpath.append(y)	
		OUT = [filepaths,docpath]
	except Exception,e:
		OUT = str(e)
else:
	OUT = "Please set it to true"
8 Likes

Hi !
Can you explain what are the inputs for this Python script ?

Hello, IN[0] will be the list of filepaths you wanna open, and IN[1] is just a boolean (true or false)

Thank you !

Here is what I add to the script to make it works !

it does not work fo me, it says something like the file path does not exist, but there is already a file in there
image

I guess because script is editing the file path name, splitting string by “_” and something more, and my file name contains that symbol.

ilename = filepath.Split("\\")[-1].Replace(".rvt","")
			newdoc = app.OpenDocumentFile(filepath)			
			if RVer not in filename:
				if "Central" in filename:
					x = filepath.split("_")
					x.insert(len(filepath.split("_"))-1,RVer)
					y = "_".join(x)
				else:
					x = filepath[:-4]
					y = x + "_" + RVer + ".rvt"
			else:
				y = filepath
			newdoc.SaveAs(y,SaveOptions)			

yea it does that, the script’s intention was to also change the name to the respective version. you can comment away the if statement to make it work as you need.