Issue
I wanted to manipulate Selection Set in .NET. e.g. get some Selection Sets and add them to current selection.
Solution
From 2012, .NET API provides the object SelectionSet. Before 2012, the related objects are exposed in COM API.
The code below assumes you open the SDK sample gatehouse.nwd and want to highlight the first two selection sets in first level of the Selection Sets tree.
1) InwOpSelectionSet: Selection Set
2) InwSelectionSetFolder: folder of selection sets
3) state.SelectionSetsEx lists the selection sets and folders in first level of the Selection Sets tree
4) InwSelectionSetFolder. SelectionSets returns the selection sets and folders within this folder
// access selection set
private void test()
{
ComApi.InwOpState10 state;
state = ComApiBridge.ComApiBridge.State;
// model items collection to store the
// items of selection set
ModelItemCollection modelItemCollectionIn =
new ModelItemCollection();
// Selection Set Collection
ComApi.InwSelectionSetExColl oSSExColl =
state.SelectionSetsEx();
for (int i = 1; i <= oSSExColl.Count; i++)
{
// if this is a selection set
if (oSSExColl[i] is ComApi.InwOpSelectionSet)
{
ComApi.InwOpSelectionSet oSet =
(ComApi.InwOpSelectionSet)oSSExColl[i];
state.CurrentSelection =
oSet.selection;
ComApi.InwOpSelection oCopySelection =
(ComApi.InwOpSelection)oSet.selection.Copy();
foreach (ModelItem oItem in
ComApiBridge.ComApiBridge.
ToModelItemCollection(oSet.selection))
{
// store each item
modelItemCollectionIn.Add(oItem);
}
//this is just an example on gatehouse.nwd.
// we checked two sets only
if (i > 2)
break;
}
else if (oSSExColl[i] is ComApi.InwSelectionSetFolder)
{
//if this is folder.
ComApi.InwSelectionSetFolder oSSFolder =
(ComApi.InwSelectionSetFolder)oSSExColl[i];
// recurse the folder
recurseFolderSS(oSSFolder);
}
else
{
}
}
//set the current selection
// highlight the selection of the set
Autodesk.Navisworks.Api.Application.ActiveDocument.
CurrentSelection.CopyFrom(modelItemCollectionIn);
}
// recurse a selection set folder
private void recurseFolderSS(ComApi.InwSelectionSetFolder folder)
{
ComApi.InwOpState10 state;
state = ComApiBridge.ComApiBridge.State;
ComApi.InwSelectionSetExColl oSSExColl =
folder.SelectionSets();
for (int i = 1; i <= oSSExColl.Count; i++)
{
if (oSSExColl[i] is
ComApi.InwOpSelectionSet)
{
ComApi.InwOpSelectionSet oSet =
(ComApi.InwOpSelectionSet)oSSExColl[i];
//you can collect the sets from the folder.
// .....
}
else if (oSSExColl[i] is ComApi.InwSelectionSetFolder)
{
// if this is a folder, recurse
ComApi.InwSelectionSetFolder oSSFolder =
(ComApi.InwSelectionSetFolder)oSSExColl[i];
recurseFolderSS(oSSFolder);
}
else
{
}
}
}