We have some articles on how to add new SelectionSet. I got a question how to update a SelectionSet. The customer said this object (from the collection of DocumentSelectionSets) is read-only.
In general, to modify a .NET object of Navisworks, you would need to make a copy of the original object (CreateCopy method), modify the copy, and copy to the original object (CopyFrom). Normally, a collection object provide the relevant copy methods. In DocumentSelectionSets, they are:
1. ReplaceWithCopy(Int32, SavedItem) Replaces item at given index within Value with a copy of item
2. ReplaceWithCopy(GroupItem, Int32, SavedItem): Replaces item from the specified parent group with a copy of item
I’d recommend the second method as it clearly indicates the relationship of an item and its parent.
So,
1. get the parent item of the SelectionSet you want to modify. In default, the SelectionSet in first level is actually included in a root item which is invisible in UI.
2. get the index the item resides in the group (parent)
3. make a copy of the original item, modify the relevant properties.
4. call ReplaceWithCopy to update the original item.
The following code demos how to update an item named ‘mytestset’. Assume some other objects are selected in advanced. The code will update the SelectionSet with a new name and the current selection.
public override int Execute(params string[] parameters)
{
try
{
string output =
ModifySelectionSetContent(
Autodesk.Navisworks.Api.Application.
ActiveDocument.SelectionSets.RootItem,
Autodesk.Navisworks.Api.Application.
ActiveDocument.Title,
"");
}
catch (Exception ex)
{
// any exception
System.Diagnostics.Debug.Write(ex.ToString());
}
return 0;
}
public string ModifySelectionSetContent(
SavedItem item,
string label,
string lineText)
{
//set the output
string output = lineText + "+ " + label + "\n";
//See if this SavedItem is a GroupItem
if (item.IsGroup)
{
//Indent the lines below this item
lineText += " ";
int index = 0;
//iterate the children and output
foreach (SavedItem childItem in
((GroupItem)item).Children)
{
output +=
ModifySelectionSetContent(childItem,
childItem.DisplayName,
lineText);
if (childItem.DisplayName == "mytestset")
{
SelectionSet oSet =
childItem as SelectionSet;
SelectionSet copyItem =
oSet.CreateCopy() as SelectionSet;
//change name
copyItem.DisplayName += "_updated";
//change selection
copyItem.ExplicitModelItems.CopyFrom(
Autodesk.Navisworks.Api.Application.
ActiveDocument.CurrentSelection.SelectedItems);
DocumentSelectionSets oSets =
Autodesk.Navisworks.Api.Application.
ActiveDocument.SelectionSets;
oSets.ReplaceWithCopy((GroupItem)item,
index, copyItem);
}
index ++;
}
}
//return the node information
return output;
}