Insert Common SelectionSet to Folder

By Xiaodong Liang

SelectionSets is a collection. It can contain Folder and common SelectionSet. A Folder is a GroupItem that can include common SelectionSet as well. The code below is a demo on how to create a common SelectionSet and a Folder, and how to move the common set to the folder.

public void testSelectionSet()

{

    Document oDoc =

       Autodesk.Navisworks.Api.Application.ActiveDocument;

 

    // get the selection sets

    DocumentSelectionSets oSets =

            oDoc.SelectionSets;

 

     // create a search and conditions

    Search s = new Search();

 

    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;

 

    SavedItem newItem = new SelectionSet
(s);

    newItem.DisplayName = "MySearchSet";

    //add to the end

    oSets.AddCopy(newItem);

 

    //folder

    FolderItem oFolder = new FolderItem();

    oFolder.DisplayName = "MyFolder";

    // add to the end

    oSets.AddCopy(oFolder);

 

    try

    {               

        //index of the common selection set        

        int indexOfSaveditem = oSets.Value.Count – 2;      

            

        // move the  selection set  to the folder         

        oSets.Move(oSets.RootItem, indexOfSaveditem,

            (GroupItem)oSets.Value.Last<SavedItem>(), 0);

    }

    catch (Exception ex)

    {

        MessageBox.Show(ex.ToString());

    }

}

image


Comments

One response to “Insert Common SelectionSet to Folder”

  1. Glenn Cai Avatar
    Glenn Cai

    Hi, Xiaodong
    Do you have any examples that show how to move multiple selection sets under a folder?

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading