|
![]() |
Selection and collections of ModelItem are an important part of the API. The collections returned by various properties give access to the underlying ModelItem data, but not all of these are editable and the information returned by each can vary.
A Selection is either an explicit collection of ModelItems or contains a specification that will generate a collection of ModelItem when applied to a particular document.
Collections
Several types of collections exist in the API, split into two groups:
-
Read only collections accessible for examination and comparison.
-
Read/Write Collections which can be added and removed; copied from and to.
Some examples are:
Collection Class | Read | Write |
---|---|---|
ModelItemCollection | Yes | (*see note below) |
ModelItemEnumerableCollection | Yes | No |
Selection | Yes | (*see note below) |
DocumentCurrentSelection | Yes | Yes |
![]() |
---|
In the DocumentCurrentSelection class, the ToSelection member function and the implicit conversion to the Selection class both return read-only instances of the Selection class. To manipulate the selection use the methods available in DocumentCurrentSelection. |
![]() |
---|
As with all classes derived from NativeHandle, ModelItemCollection and Selection are writable when created by the developer but are read only when returned by part of the document as a property. |
DocumentCurrentSelection class
DocumentCurrentSelection represents the current selection in a given Document object.
It is accessed by the CurrentSelection property of the Document class. Properties in the class include a collection of the selected items and a flag to check IsEmpty status.
There are also editing methods for clearing the selection, 'SelectAll', adding/removing items to/from the selection.
Events are provided for selection changes.
DocumentCurrentSelection exposes the Selection that defines the current selection in the document, adding events and convenience methods for editing.
In order to change the selection you can do one or more of the following:
-
Clear the current selection
-
Add a ModelItem to the current selection
-
Replace the selection with one you stored previously
To better illustrate selections let us use an example where a developer wants to select the ModelItems that are children of the currently selected ModelItems:

//Copy the selection into a new ModelItemCollection ModelItemCollection selectionModelItems = new ModelItemCollection(); Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.SelectedItems.CopyTo( selectionModelItems); //Clear the current selection Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.Clear(); //iterate through the ModelItem's in the selection foreach (ModelItem modelItem in selectionModelItems) { //iterate through the children of the ModelItem foreach (ModelItem childModelItem in modelItem.Children) { //Add the ModelItem to the current selection Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.Add(childModelItem); } }
In the example above, every modification of a DocumentPart (in this case CurrentSelection) raises events and records as separate changes for undo. On larger models this will increase the time of the operation. To increase the speed, and avoid the extra records for undo, the code should instead be written to only set the selection once. This can be done by first building a collection of ModelItems and then applying that as the selection:

//Create a new ModelItemCollection ModelItemCollection newCollection = new ModelItemCollection(); //iterate over the selected Items foreach (ModelItem item in Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.SelectedItems) { //Add the children of the selected item to a new collection newCollection.AddRange(item.Children); } //Assign the ModelItemCollection to the selection Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.CopyFrom(newCollection);
In these examples the code iterates over the Children of ModelItem. However it is important to remember that the Children property of ModelItem will only return the first level of child items. If a developer wants to get a selection containing all the ModelItems that are Children of those children as well, and so on down the chain, then the code above should replace Children with Descendants. For example:

//Create a new ModelItemCollection ModelItemCollection newCollection = new ModelItemCollection(); //iterate over the selected Items foreach (ModelItem item in Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.SelectedItems) { //Add the Descendants of the selected item to a new collection newCollection.AddRange(item.Descendants); } //Assign the ModelItemCollection to the selection Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.CopyFrom(newCollection);
This selects all of the children of ModelItem and their component ModelItems recursively down to the root ModelItems which contain the geometry.
A more extensive list of the ModelItem properties, including these shown here, are available in the Reference Guide.
ApplicationControl.SelectionBehavior
This is the same as what is known as 'Selection resolution' in Navisworks. For more information developers should see the main Navisworks help documentation.
When using the controls API this value can be accessed and altered from ApplicationControl..::..SelectionBehavior.