Accessing ModelItems from SelectionSets in Navisworks

The client needs a Synchro 4D animation of his building do to detect possible issues in constructions and also a video to show to the rest of the stakeholders.

Synchro4D is a software for 4D visualization and construction coordination.It allows you to animate and program any 3D object exported from Autocad, Revit, Infraworks, sketchup, and such.
http://xkcd.com/1739/

The only input I have right now is a federated navisworks model and a mpp – Ms Project – file with tasks and a Gantt diagram.

Synchro (as Navisworks) has the ability of read any parameter on the Revit model and relate it to the right task from the project.*

In order to save time while waiting for Revit models I had the idea of creating a Selection Set per task containing Model items related to each one. This Selection Sets will be named after the task’s name.

The only thing left  to do is export a csv file with the sets’ content, displaying set’s name, source of the file (revit’s file name where were exported) and element’s Id. The file should look like this:

SelectionSet,SourceName,ElementId

This can be achieved using the Navisworks API to access all the sets, and then all elements on each one.

Here is the code I’ve used. You may want to test it using the add-in CodeRun included with Naviswork’s API.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using Autodesk.Navisworks.Api;
using Autodesk.Navisworks.Api.DocumentParts;
using System.IO;

namespace CScript
{
    public class CScript
    {
        static public void Main()
        {
            var csv = new StringBuilder();
            var output = new List<String>();
            Document oDoc = Autodesk.Navisworks.Api.Application.ActiveDocument;
            //All selection sets in model
            Autodesk.Navisworks.Api.DocumentParts.DocumentSelectionSets oCurSets = oDoc.SelectionSets;
            //Convert to SavedItemCollectin
            SavedItemCollection coll = oCurSets.ToSavedItemCollection();
            //Path of the current file to save our csv later
            var path = (oDoc.CurrentFileName).Replace(oDoc.Title, "");

            //Iterate over all selections sets to create a current selection
            foreach (SavedItem i in coll)
            {
                //Create a selection source with all objects in selection
                SelectionSource oS = Autodesk.Navisworks.Api.Application.ActiveDocument.SelectionSets.CreateSelectionSource(i);
                //Get a list of ModelItems in Selection Set
                ModelItemCollection list = oS.TryGetSelectedItems(oDoc);

                Selection a = new Selection(list);
                var set = i.DisplayName;

                Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.AddRange(list);
                foreach (ModelItem m in oDoc.CurrentSelection.SelectedItems)

                {
                    var Name = m.DisplayName;
                    var FC = m.PropertyCategories.FindPropertyByDisplayName("Item", "Source File").Value.ToDisplayString();
                    var SC = m.PropertyCategories.FindPropertyByDisplayName("Element ID", "Value").Value.ToDisplayString();

                    var newLine = string.Format("{0},{1},{2},{3}", set, Name, FC, SC);
                    output.Add(newLine);
                    csv.AppendLine(newLine);
                }
                Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.Clear();
            }
            //Check if document is saved and use its path to save the csv
            if (oDoc.FileName != null)
            {
                File.WriteAllText(path + oDoc.Title + ".csv", csv.ToString());
            }
            else

            {
                //if file is not saved (untitled) use MyDocuments path instade 
                path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                File.WriteAllText(path + oDoc.Title + ".csv", csv.ToString());
            }
            //test

            foreach (string s in output)
            {
                Console.WriteLine(s);
            }
        }
    }
}

Notice it will export the csv file to the same folder of the NWD or NWF, if the file was not saved it will be saved in MyDocuments.

Later we will input this information to each Revit model using Dynamo and then export it to Synchro4D.

Hope you find it useful! Let me know if you have any ideas to share or a different workflow to work with.

*We don’t use Navisworks for this work because Synchro4D has a better platform to animate the actions on each thask – Demo, Temporary, New construction – and the final outcome needs to show how it actually has to be built.

5 thoughts on “Accessing ModelItems from SelectionSets in Navisworks”

  1. Just to better understand:
    -I select the project in Navisworks
    -I import a gantt diagram from “Data Source” (either csv or Microsoft Project)
    -I attach tasksfrom Timeliner to selection Tree – Standard
    Then, how I have to go head to see your idea above please?

    1. Ridi, sorry. Seeing this article after several months made realize it is quite cryptic.

      So, in a nutshell. What I’m trying to achieve is to create a cvs file from existing Navisworks selection sets (one per task).
      The expected result is an excel like file showing “SelectionSet”,”SourceName”,”ElementId” one item per row. After I use this to import to Revit or Synchro and build my 4D.
      Is important to execute the script when you have all your selection sets.
      Hope it clarifies everything.
      Cheers!

      1. Thanks for answering. I’m not able to get the cvs file from existing Navisworks seletions sets. Could you tell me what are the step that you do please?

        1. Sure! Can you give background about the navis version you are using? Are you running the script in the code run addin from the API example? Can you poste the error is giving you?

  2. I’m working with Navisworks 2018,
    I created a new Framework.net from scratch, then I copied your code and I added the references.
    I ran Navisworks, import a Gantt diagram from Microsoft Project and then I attached all the tasks. But your code doesn’t start. Maybe I do something wrong. But I don’t get any error from C#.
    Could you tell me what are the steps that you do please?

Leave a Reply

Your email address will not be published. Required fields are marked *