Add Search or Selection Set to Timeliner Task

By Xiaodong Liang

In the post, we demoed how to bind an explicit selection to a timeliner task. The API also supports to attach a selection set (SelectionSet) to the task.

The TimelinerTask.Selection (TimelinerSelection) could be:

Explicit selection
Tthe typical code snippet is:

Selection current = main_doc.CurrentSelection;

task.Selection.CopyFrom(current);

the post above shows in detail. With an Explicit selection, TimelinerSelection.HasExplicitSelection will be true.

Search selection

The workflow is to get/create a Search, and TimelinerSelection.CopyFrom this Search. With an Search selection, TimelinerSelection. HasSearch will be true.

public void attachSearchToTask()

       {  

           Document main_doc =

Autodesk.Navisworks.Api.Application.ActiveDocument;

 

 

           // create a search and conditions

           Search s = new Search();

           //search the items whose properties:  Element >> Category = Wall

           SearchCondition sc =

               SearchCondition.HasPropertyByDisplayName("Element",

                                                       "Category");

           s.SearchConditions.Add(

               sc.EqualValue(VariantData.FromDisplayString("Walls")));

 

           //set the selection to everything

           s.Selection.SelectAll();

           s.Locations =

               SearchLocations.DescendantsAndSelf;

 

           // get the Timeliner document part from an active document

         

           Tl.DocumentTimeliner doc_tl =

Tl.TimelinerDocumentExtensions.GetTimeliner(main_doc);

           // copy the current set of tasks. get the current selection.

           GroupItem root_copy =                      

                                  doc_tl.TasksRoot.CreateCopy() as GroupItem;

          

           // attach  search  to one task

           Tl.TimelinerTask task1 = root_copy.Children[0] as Tl.TimelinerTask;

           task1.Selection.CopyFrom(s);

         

           // replace the timeliner tasks

           doc_tl.TasksCopyFrom(root_copy.Children);         

 

         return 0;

      }

Selection Set

It is a bit tricky to attach a SelectionSet. In Navisworks API, SelectionSet is a kind of SelectionSource. You will need to create a SelectionSourceCollection which contains the SelectionSet, and bind it to the task. Then, TimelinerSelection. HasSelectionSources will be true.

public   void attachSelectionSetToTask()

       {        

<

p style=”line-height: normal;margin: 0cm 0cm 0pt” class=”MsoNormal” align=”left”> 

           Document main_doc =Autodesk.Navisworks.Api.Application.ActiveDocument; 

 

           // create a selection set (from explicitly selection)

           // assume some objects are selected already

           ModelItemCollection oCurrentSelection = main_doc.CurrentSelection.SelectedItems;

           // create a selectionset with the search condition

           SavedItem newExplicitlySetItem =

               new SelectionSet(oCurrentSelection);

           newExplicitlySetItem.DisplayName = "My Selection";

 

           // add this selection set to selection set collection

           //Note: InsertCopy makes a copy from newExplicitlySetItem

          // instead of adding  it directly.

           Autodesk.Navisworks.Api.Application.ActiveDocument.

               SelectionSets.InsertCopy(0, newExplicitlySetItem);

 

          //get the SelectionSet added just now 

           IEnumerable<SavedItem> tempCollection = Autodesk.Navisworks.Api.Application.ActiveDocument.

                SelectionSets.Value.Where<SavedItem>(x => x.DisplayName == "My Selection") ;

 

           SelectionSet newSelectionSet = null; ;

           if (tempCollection.Count<SavedItem>() > 0)

           {

               newSelectionSet = tempCollection.ElementAt<SavedItem>(0) as SelectionSet;

           }

           else

           {

               // should not happen
               return;

           } 

 

           // get the Timeliner document part from an active document         

           Tl.DocumentTimeliner doc_tl = Tl.TimelinerDocumentExtensions.GetTimeliner(main_doc);

 

           // copy the current set of tasks. get the current selection.

           GroupItem root_copy = doc_tl.TasksRoot.CreateCopy() as GroupItem;         

          

           // attach selection set (search set) to one task

           Tl.TimelinerTask task1 = root_copy.Children[0] as Tl.TimelinerTask;

           //create a SelectionSourceCollection

           SelectionSourceCollection oSC = new SelectionSourceCollection();

           //create a SelectionSource from the selection set

           SelectionSource oS = Autodesk.Navisworks.Api.Application.ActiveDocument.

           SelectionSets.CreateSelectionSource(newSelectionSet);

           //add the SelectionSource to SelectionSourceCollection

           oSC.Add(oS);

           //attach the SelectionSourceCollection to the task.

           task1.Selection.CopyFrom(oSC);

         

           // replace the timeliner tasks

           doc_tl.TasksCopyFrom(root_copy.Children);

         

 

         return 0;

      }


Comments

5 responses to “Add Search or Selection Set to Timeliner Task”

  1. savafa Avatar
    savafa

    Hey,
    How did you define “Tl”? This line give error in Navisworks 2014:
    using Tl = Autodesk.Navisworks.Api.Timeliner;
    Error:
    Error 1 The type or namespace name ‘Timeliner’ does not exist in the namespace ‘Autodesk.Navisworks.Api’ (are you missing an assembly reference?)

  2. savafa Avatar
    savafa

    Any help?

  3. savafa Avatar
    savafa

    I’ve also tested 2015 version, but still the same error in defining Timeliner. Isn’t this API supported by Navisworks anymore?

  4. Have you ever figured this out? i have the same issue with ModelItem: i.e. The type of namspace name ‘ModelItem’ does not exist in the namespace ‘Autodesk.Navisworks.Api’ (are you missing an assembly reference?) we’re porting a project that has worked just fine in 2012 and 2013.

  5. Ok. Note to self: NavisWorks went up to .Net 4.5 framework. That fixes it.
    Cheers,
    Gregor

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading