Open Document in background with Python

Hey everyone,

I am trying to convert Rhythm’s Open Document zero touch node to Python so that I can add an option to discard worksets. I am getting an EOF error at the moment, I am probably missing something obvious. We won’t have to worry about adding a for loop or anything since I am putting it into a .dyf

# Enable Python support and load DesignScript library
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import *
from RevitServices.Transactions import TransactionManager

# The inputs to this node will be stored as a list in the IN variables.
filePath = IN[0]
audit = IN[1]
detachFromCentral = IN[2]
preserveWorksets = IN[3]

# Place your code below this line
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
#instantiate open options for user to pick to audit or not
openOpts =  OpenOptions()
openOpts.Audit = audit
#tOpt = TransmittedModelOptions.SaveAsNewCentral
if detachFromCentral == False:
	openOpts.DetachFromCentralOption = DetachFromCentralOption.DoNotDetach       
else:
	if preserveWorksets:
		openOpts.DetachFromCentralOption = DetachFromCentralOption.DetachAndPreserveWorksets
	else:
		openOpts.DetachFromCentralOption = DetachFromCentralOption.DetachAndDiscardWorksets
#convert string to model path for open
modelPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(filePath)

try:
	docTitle = DocumentUtils.OpenDocument(modelPath, openOpts)
	OUT = docTitle
#don't remember how to handle these, it throws it everytime so is off at the moment 
#except:
#	OUT = 'error'

Here’s the the “File Management” dyf it is going to end up in btw.

Any ideas?
-Thanks

Can you expand your input (file path) so we can see what is going into it?

I’m just testing it against a single workshared file that is linked to a central model at the moment. It will typically extract the files out a directory and push those into the dyf.

Although not a direct answer, perhaps this set of python code could get you to be able to read out the error report at least, or see how the files are being read in.

EOF means “end of file”. It is being raised when you’re code cannot be read to the end. Always look for syntax mistakes, such as missing brackets, if you get it.

In this case your try statement is missing an exception. You can either comment out the try and indent the rest accordingly, or add except: pass to ignore all exceptions.

Not sure, but it looks as though you may be using the wrong Method to try and open the file.

OpenDocumentFile(path,options)

See here for more:

1 Like

Does anyone know where the DocumentUtils class is coming from? As noted in the post Sean has linked to, documents are usually opened using the current Application.

2 Likes

That was a helper class in my code a while back to attempt to fix this issue.

It was an internal class that I didn’t have open sourced as it was in test mode. I abandoned that method after some discussion on that github issue.

My github has my newest code for it. @L.Cunningham, you might need to update your fork.

5 Likes

Ah, interesting. Mystery solved!

1 Like