Navisworks Export through Python

Hi all,
I have been attempting to export objects to Navisworks using python. The script runs without errors, but I am not seeing an exported .nwc at the specified location.

Files attached
Direct Shape Python test.dyn (14.5 KB)
DirectShape Python.rvt (388 KB)

Python below:

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

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

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

clr.AddReference('System')
from System.Collections.Generic import List
#The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

doc = DocumentManager.Instance.CurrentDBDocument

solid = IN[0]
category = UnwrapElement(IN[1])
solidList = []
solidIds = []
catId = category.Id

TransactionManager.Instance.EnsureInTransaction(doc)

for i in range(len(solid)):
	ds = DirectShape.CreateElement(doc, catId)
	ds.SetShape(solid[i].ToRevitType())
	ds.Name = "This is the Name"
	solidList.append(ds)
	solidIds.append(ds.Id)

TransactionManager.Instance.ForceCloseTransaction()

fileLoc = "C:\Users\proberson\Desktop\dyn test"
fileName = "Test"

TransactionManager.Instance.EnsureInTransaction(doc)

navOp = NavisworksExportOptions()
col1 = List[ElementId](solidIds)
navOp.SetSelectedElementIds(col1)
navOp.ExportScope = navOp.ExportScope.SelectedElements
navOp.ExportRoomGeometry = False
doc.Export(fileLoc, fileName, navOp)

TransactionManager.Instance.TransactionTaskDone()


#Assign your output to the OUT variable.
OUT = solidIds

I figured this one out. I just moved the TransactionManager.Instance.EnsureInTransaction(doc) line to below the doc.Export line. I am not sure why this changed the outcome…any thoughts on that would be helpful. Hope this helps someone in the future.

There are a few additional lines in the code that are disassociated with solving this issue.

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

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager


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

clr.AddReference('System')
from System.Collections.Generic import List
#The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

doc = DocumentManager.Instance.CurrentDBDocument

solid = IN[0]
category = UnwrapElement(IN[1])
names = IN[2]
catId = category.Id
dsId = []

TransactionManager.Instance.EnsureInTransaction(doc)

for i in range(len(solid)):
	
	ds = DirectShape.CreateElement(doc, catId)
	ds.SetShape(solid[i].ToRevitType())
	ds.Name = names[i]
	dsId.append(UnwrapElement(ds.Id))

TransactionManager.Instance.ForceCloseTransaction()

fileLoc = IN[3]
fileName = IN[4]

navOp = NavisworksExportOptions()
col1 = List[ElementId](dsId)
navOp.SetSelectedElementIds(col1)
navOp.ExportScope = navOp.ExportScope.SelectedElements
navOp.ExportRoomGeometry = False
doc.Export(fileLoc, fileName, navOp)
TransactionManager.Instance.EnsureInTransaction(doc)
TransactionManager.Instance.TransactionTaskDone()

doc.Regenerate()

TransactionManager.Instance.EnsureInTransaction(doc)

doc.Delete(col1)
TransactionManager.Instance.TransactionTaskDone()

doc.Regenerate()

#Assign your output to the OUT variable.
OUT = 0

Do you know any way to export NWC from Multiple Sites existing in revit?
I change this in a loop and then try to export NWC, sites changes but no export file is done.

import clr
clr.AddReference(‚ÄėRevitNodes‚Äô)
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference(‚ÄėProtoGeometry‚Äô)
from Autodesk.DesignScript.Geometry import *

clr.AddReference(‚ÄúRevitAPI‚ÄĚ)
import Autodesk.Revit.DB as db

clr.AddReference(‚ÄúRevitServices‚ÄĚ)
from RevitServices.Transactions import TransactionManager
from RevitServices.Persistence import DocumentManager

doc = DocumentManager.Instance.CurrentDBDocument
allLoc = doc.ProjectLocations

allLocNumber = allLoc.Size
enum = allLoc.GetEnumerator()
enum.MoveNext()
sol = enum.Current
folderpath = IN[0]

viewEx = UnwrapElement(IN[1])

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 )

allLocations = [sol]

for loc in range(allLocNumber)[:10]:
enum.MoveNext()
sol = enum.Current
allLocations.append(sol)

options = db.NavisworksExportOptions()
options.ViewId=viewEx.Id
#All,Elements, or None
#options.NavisworksParameters = Enumeration
options.ExportScope = db.NavisworksExportScope.View
options.ExportLinks=False
options.Coordinates= db.NavisworksCoordinates.Shared
options.ExportParts = False
options.ExportElementIds = True
options.ConvertElementProperties = True
options.ExportRoomAsAttribute = True
options.ExportRoomGeometry = False
options.ExportUrls = True
options.DivideFileIntoLevels = False
options.FindMissingMaterials = True

def moveAndExport(loc):
TransactionManager.Instance.EnsureInTransaction(doc)
fileName = doc.Title + ‚Äú_‚ÄĚ + loc.Name
doc.ActiveProjectLocation = loc
doc.Regenerate()
TransactionManager.Instance.TransactionTaskDone()
result = doc.Export(folderpath, fileName, options)
return fileName

ProcessParallelLists(moveAndExport, allLocations)
OUT = [x.Name for x in allLocations]

1 Like