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()
{
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;
}