Export a list of Revit 3D views into NWC through Python

python
navisworks

#1

Hey everybody!

Following this post Export NWC By Python I’m trying to do a similar thing but instead of exporting only the Active View, I’d like to extract a list of 3D views in different files.

I tweaked a bit the code in that post above but I’m still pretty new to Python so I guess there must be something pretty basic I’m missing there

This is the Dynamo graph:

And this is the script I’m using (modified from the script above)

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

Import ToDSType(bool) extension method
clr.AddReference(“RevitNodes”)
import Revit
clr.ImportExtensions(Revit.Elements)

Import geometry conversion extension methods
clr.ImportExtensions(Revit.GeometryConversion)

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 RevitAPI
clr.AddReference(“RevitAPI”)
import Autodesk
from Autodesk.Revit.DB import *

import System

import math

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

coll = FilteredElementCollector(doc).OfClass(ProjectLocation)

output = []
#views = [UnwrapElement(IN[0])]
views = (UnwrapElement(IN[0]))
path = IN[1]
name = IN[2]
#path = Autodesk.Revit.DB.FilePath(pathName)
OUT = views
for i in views:
Options = Autodesk.Revit.DB.NavisworksExportOptions()
Options.ExportScope = NavisworksExportScope.View
Options.ViewId = i.Id
#Expects ElementId (above)
Options.ExportLinks = True
doc.Export(path, name + i.Name , Options)
TransactionManager.Instance.EnsureInTransaction(doc)

TransactionManager.Instance.TransactionTaskDone()
doc.Regenerate()
#OUT = dir(Autodesk.Revit.DB.NavisworksExportOptions.ExportScope)
#OUT = 0

I think now the script is getting the list of views in the input, but I’m still creating one file, now called “try1+the name of the view”, while I actually want to create as many files as the views in the list that I input.

How do I make it create 1 file for each view I’m inputting?

Thanks a lot!

PS thanks @4bimfercesp for suggesting to create the new post!


Export 3D view from a List to NWC
Export to Navisworks
#2

Looking at the code I think you are overwriting the same file every time. You need to find a way to get a different filepath name for every iteration of the loop.


#3

@andreatas

Thanks for creating a new topic.

Please find below dyn (python). This exports all 3D views with the word “Navisworks” on its name. You can edit the python to suit your needs regarding the view (name) you want to export. You can tagged your views with “Navis” instead of Navisworks etc.

This is already tested and ready to go.

Let me know if you have issues.

4BimFerCEsp_Nwc3Dexport.dyn (2.2 KB)

PS.
I assume that one should have the Navisworks exporter installed before trying to use the graph.
Your current view should be either a 3D view or a Sheet before running the graph.


Export NWC By Python
#4

Thanks @4bimfercesp !

That’s great! How can implement the part of the saving in multiple files in my script?
Is it the line that says
doc.Export(folder, “Export-” + view.Name + “.nwc”, options)

Because I tried that in my script but it still exports one file with the name of the Revit view + Export- at the beginning


#5

Sorry forgot to say its an alternative approach rather than using your python on the top. It will achieved the same thing at the end. Just try it first rather than fixing your one. Just open a revit model from the sample files and create some 3d views and named it anything your like but with “navisworks” word on its name and run the graph.

You will see.


Export Selected 3d Views to NWC
#6

Hey @4bimfercesp

I tried and it actually doesn’t work…the output of the node is “Success” but the result is that nothing happens.


#7

Did you check the folder path you placed or did you use what i have c:\test?
i’ve been using that dyn all the time and it works. Are you sure your current view is a sheet or 3d view when you run the graph?

Dont really know whst to say apart from it works for me and any pc i’ve used.


#8

@4bimfercesp

SOrry my bad, it is working! It was just because I was trying to export linked geometries…I just added the option options.ExportLinks = True and now it works!

I see the value of your workflow of taking the views that contains “Navisworks” in their names but I was trying to make it more flexible for my purpose so inputting the views (that I may filter by name or by parameter or by whatever I need) and export them in different files. One file for each view.

Now, if you see above, our lines of the doc.Export are the same. So I don’t understand why my script is not exporting multiple views.

Any idea?


#9

Hey @andreatas

You could use keywords for your view names like this:

ViewKeyWords=["Navis","navis","AnythingYouWant"]

for view in collector:
    if (view.ViewType == ViewType.ThreeD and any(word in view.Name for word in ViewKeyWords)):
        options.ViewId = view.Id
        doc.Export(folder, "Export-" + view.Name + ".nwc", options)

#10

Ciao @salvatoredragotta

Yes that would be possible, but what I’m trying to do is input a list of views that I collected elsewhere with whatever rule and I want (or would like… :smiley: ) the script to create one export for each view.

Now it looks a bit weird because the script sees the 2 views I’m inputting but for some reason it exports just the first one. Also, when, in line 42, I output the name of the view with i.Name it outputs the name of the second view…which makes it super confusing!!

Here a screenshot of the graph and of the file that comes out


And here is the code:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
# Import ToDSType(bool) extension method
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
# Import geometry conversion extension methods
clr.ImportExtensions(Revit.GeometryConversion)
# 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 RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

import System

import math

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

coll = FilteredElementCollector(doc).OfClass(ProjectLocation)

output = []
#views = [UnwrapElement(IN[0])]
views = (UnwrapElement(IN[0]))
#views = [UnwrapElement(i) for i in IN[0]]
path = IN[1]
name = IN[2]
#path = Autodesk.Revit.DB.FilePath(pathName)
#OUT = views

for i in views:
	OUT = i.Name
	Options = Autodesk.Revit.DB.NavisworksExportOptions()
	Options.ExportScope = NavisworksExportScope.View
	Options.ViewId = i.Id
	Options.ExportLinks = True
	doc.Export(path, name + "_" + i.Name, Options)
	TransactionManager.Instance.EnsureInTransaction(doc)

#OUT = i.Name

TransactionManager.Instance.TransactionTaskDone()
doc.Regenerate()
#OUT = dir(Autodesk.Revit.DB.NavisworksExportOptions.ExportScope)
#OUT = 0

#11

change to:

OUT=[]
for i in views:
    OUT.append(i.Name)

#12

Not sure why, but your code runs just once.

Try this:

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

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

doc = DocumentManager.Instance.CurrentDBDocument


views = UnwrapElement(IN[0])
path = IN[1]
name = IN[2]


OUT = []
if IN[3]:
	for i in views:
		OUT.append(i.Name)
		Options = Autodesk.Revit.DB.NavisworksExportOptions()
		Options.ExportScope = NavisworksExportScope.View
		Options.ViewId = i.Id
		Options.ExportLinks = True
		doc.Export(path, name + "_" + i.Name, Options)
		
	TransactionManager.Instance.EnsureInTransaction(doc)
	doc.Regenerate()
	TransactionManager.Instance.TransactionTaskDone()


#13

@salvatoredragotta it works!!! Sei un grande!!!

What was wrong in the other script and why is this one working??

Thank you so much mate!!


#14

Regarding your output, you had this wrong

Then it was running just once. I had to close Revit to re-run it. Not sure why. Maybe someone else can answer this.


#15

@andreatas
I see you got your solution.

In addition to this i think your problem with only running once is due to the location of this line

TransactionManager.Instance.EnsureInTransaction(doc)

If you place it i think above this line

for i in views:

In your code it will do the trick. You may need to rem out your doc.regenerate temporary to test or tab it to match the indentation.

It may not work. I am not on a pc to test. Goodluck. At least you have a working solution above.


#16

@4bimfercesp , the Script is great ,Big Thanks for it; however , I had a problem . when it exports it doesn’t keep the visibility / graphics for each view. In another words, I used it to export nwc for each floor , so I created different views with different section box location for each floor. but when It exported , it exported multiple NWC files and all of them has all the floors in it , Do you have any idea that could make it keep visibility and graphics for each view ??