Open model in background and change view

Hi all,

I have a problem with an script that should make an IFC export from a list of documents. The script works almost fine , the only problem is that it won’t change the view to make an different export. Sometimes we have 2 or more views where we want to make an export from.
This my script.

Here is my pythoncode. except the part for ifc export that part works for me.

import clr
from Autodesk.DesignScript.Geometry import *
import Revit
# Import geometry conversion extension methods
# Import DocumentManager and TransactionManager
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *
# Import RevitAPI
import Autodesk
from Autodesk.Revit.DB import *

uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application

#The inputs to this node will be stored as a list in the IN variable.
docs = IN[0]
UDPS_pad = IN[3] #UserDefinedProppertySet
EMT_pad = IN[5] #Exportmappingtable
waarden = IN[6]
i = 0
views = []
naam = "naam"
vnaam = "a"
tekst = []
fileversion = waarden[21]
# instantiate error log
error_log = None

# default document set to DocumentManager.Instance.CurrentDBDocument
if docs == 'Current.Document':
    docs = DocumentManager.Instance.CurrentDBDocument

# wrap input inside a list (if not a list)
if not isinstance(docs, list):
    docs = [docs]
# core data processing
for doc in docs:
	typelist = list()
		for tl in typelist:
			collector = FilteredElementCollector(tl).OfClass(View)
			for view in collector:
				vnaam = view.Name
				if view.Name == vws[i]:
					views.append (view)
					for v in views:
						total_export = []
						options = []
						if fileversion == "IFC4":
							options.FileVersion = IFCVersion.IFC4
						if fileversion == "IFC2x2":
							options.FileVersion = IFCVersion.IFC2x2
						if fileversion == "IFC2x3":
							options.FileVersion = IFCVersion.IFC2x3
						if fileversion == "IFC2x3":
							options.FileVersion = IFCVersion.IFC2x3CV2
						if fileversion == "IFCBCA":
							options.FileVersion = IFCVersion.IFCBCA
						if fileversion == "IFCCOBIE":
							options.FileVersion = IFCVersion.IFCCOBIE
						if fileversion == "":
							options.FileVersion = IFCVersion.Default
						naam = name[i] + "_" + vnaam
						c=tl.Export(folder[i], naam, options)
		# End Transaction
			type = list()
		for t in typelist:
				tekst.append ("niet gesloten")
		tekst.append ("niet geopend")
	i = i+1
OUT= tekst

I hope someone could help me with this.
greetings marc

Could anyone help me?

I’m not quite sure if I understood your problem but It seems that you are checking the name of the view against a certain string and if it matches you add at to a list, then iterate over a list that always contains a single item. I reckon you need to do this:

            for view in collector:
				if view.Name == vws[i]:
					views.append (view)
			for v in views:
                vnaam = view.Name
			    total_export = []
			    options = []

… and so on to make sure all the views that match get added to the list.

Thank you Hoss,

What you said in your reply will not set the view activ.
Maybe i did not explain good enough. What i want is as follow.

  1. i open model in background.
  2. i want to make an view the active view (view from model that is open in background)
  3. make ifc export from view.

Now it makes an export from the whole model/standard view.

You can set the active view of the document using the ActiveView property:

So you should say:

doc.ActiveView = view

I tried that but got an error.
My Code :

as you can see i made it tl.Activeview = view

Now i get an error like :

I think it’s because the model isn’t open actually but in background.
If i am wrong i hope that you will explain what i am doing wrong and that you will help me further.

The error is on a different line than in the Python? It says Activeview on line 66 but its line 50 in Python. Anyways, it should be ActiveView, not Activeview. Methods are type sensitive, although I don’t know if that will solve the error you have.

I’m sorry my bad. That was the wrong picture… But i changed what you said and it heled a little. Now i’m getting an other error.

this error belongs with the python code from above.

So the ActiveView is read-only. Is the entire document opened in read-only mode while in the script? Sorry, I am not that proficient in API/doc work.

I don’t think so. Because at the end of the script it close the model and it will save the model.
But i have no idea why it gives me this error.

Sorry I sent you to the wrong path. There are at least three issues here:

  • ActiveView property of the document that you open is get only. Unless you activate the document and get the ActiveView property from the UIDocument which is get and set. There is a method for opening and activating the document called OpenAndActivateDocument() which returns the UIDocument. However, as Dynamo is using the idling event Revit doesn’t allow to activate another document. Therefore you can’t set the ActiveView with this method as well. I don’t know of any method to set the ActiveView for a document that is open in the background at the moment using Dynamo. Although you can get the OpenAndActivateDocument method to work with a macro for instance. However if you want to stick to Dynamo:

  • There’s another way you can try, which might give you what you want. In Revit API we have another class called StartingViewSettings you can get the starting view of the document by calling its get method in this way:

startingViewSettings = GetStartingViewSettings(doc)

then set the ViewId property this way:

startingViewSettings.ViewId = view

then if you save & close the document and open it again you should get ActiveView of the doc to the view which you have set previously.

Thank you very much for your reply Hoss.
That is an very clear answer and i have to think about the options we’ve got now. Maybe we’ll go for the option to change the startingview , but we’ve got models with 5 to 10 views that we want to export. So i don’t know if that’s going to work. If not i have to make it as an macro and then i’ve got a lot to learn for.

For now i want to thank you both.


I have a node to export IFC from documents. You can choose the views to feed in the node.
The node accepts multiple views per document.
For several documents, the lacing must be set to the longest.


1 Like


Nice node but it doesn’t work on multiple documents. The python code in the node gives an error on a list of multiple documents.

I am absolutely certain that the node works with several documents.
If you show the error and your graph, I can help you solve it.

I wonder what i did wrong. here is my script en the error.

The explanation is quite simple.
You can’t set the lacing on longest with the Python script on the contrary of the node.
You must edit the Python script to accept several documents or create a custom node with your desired inputs and use lacing.

The python is not myn but from the custom node. Node from the package Genius Loci as you told me. When i use that node nothing happens even when i put lacing to Longest. That’s why i put it all in my script ( outside the custom node).

I can’t see all the previews under your nodes so I can’t know where the error is.
For example in hte picture, there are 2 documents: the first with a selected view and the second with 2 selected views.
I got 3 IFC files.


this is what i get with the node “Export IFC from document”

I will put the script and excell file as download so you can see what i’m doing and hopefully you can tell me what i’m doing wrong.

ULC_IFC-export(2018-08-27).dyn (48.4 KB)
IFC_projecten_ULC_R17.xlsx (19.8 KB)