Export View Set To DWG

OK, here’s how this works:

  • First input is a Folder Path that you want to export the DWG to. Use Directory Path node for this.
  • Then you have a list of views to export. For the sake of this example I just grabbed a few of the sections from my model.
  • Then you have to specify a name for each exported view. That can be automated and we can let Revit automatically assign a name for each DWG but I usually want to specify my own so this is how I set this up.
  • lastly we don’t want to export DWG every time something changes so I added a boolean toggle to only export when its set to True. This way you can better control when this time consuming operation happens.

Code itself that was used is really simple:

# Copyright(c) 2016, Konrad K Sobon
# @arch_laboratory, http://archi-lab.net

# Import Element wrapper extension methods
import clr
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument

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

import System
from System.Collections.Generic import *

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

#The inputs to this node will be stored as a list in the IN variable.
dataEnteringNode = IN

def ProcessList(_func, _list):
    return map( lambda x: ProcessList(_func, x) if type(x)==list else _func(x), _list )

def ProcessParallelLists(_func, *lists):
	return map( lambda *xs: ProcessParallelLists(_func, *xs) if all(type(x) is list for x in xs) else _func(*xs), *lists )

def Unwrap(item):
	return UnwrapElement(item)

folderPath = IN[0]

if isinstance(IN[1], list):
	views = ProcessList(Unwrap, IN[1])
else:
	views = list(Unwrap(IN[1]))

if isinstance(IN[2], list):
	names = IN[2]
else:
	names = list(IN[2])

RunIt = IN[3]

def ExportDwg(name, view, folder = folderPath):
	options = DWGExportOptions()
	views = List[ElementId]()
	views.Add(view.Id)
	result = doc.Export(folder, name, views, options)
	return result

if RunIt:
	try:
		errorReport = None
		# run export
		ProcessParallelLists(ExportDwg, names, views)
		
	except:
		# if error accurs anywhere in the process catch it
		import traceback
		errorReport = traceback.format_exc()
else:
	errorReport = "Please set the RunIt to True!"

#Assign your output to the OUT variable
if errorReport == None:
	OUT = 0
else:
	OUT = errorReport

Couple of things that I am doing in code here to make your life easier:

  • I make sure that views and view names inputs can be either a single item or a list. They can also be a nested list so basically as long as those two have matching structure they will all get processed properly. I use the ProcessList() and Unwrap() methods for that. Have a look at those to learn some Python. It will come handy.
  • Once all of the inputs are ready and processed we can just call ProcessParallelLists() to export each DWG. I basically call the method on each provided View. I know that’s slower than calling it once on a viewSet but it allows me to custom name each DWG. I like that flexibility.

Good luck!

Ps. Get All Views node comes from Archi-lab_Grimshaw package.

22 Likes