Search by “OR”

By Xiaodong Liang

SearchConditionCollection::AddGroup adds the items of the specified collection to the SearchConditionCollection as a new group of conditions. The combined set of SearchConditions will match if any group within the set matches. This means it is “OR” condition among the groups. Within a group, the condition is “AND”.

Capture

 

public void searchGroup()
{
    // This code search the items that meet the conditions:
    // Required of Item is true and Entity Handle is 16C17
    // Or Required of Item is false and Entity Handle is 17C2E
    // It is tested with the SDK sample
    // apiCOMexamplesgatehouse.nwd
 
    //Create a new search 
    Search s = new Search();
    s.Selection.SelectAll();
 
    //SearchCondition1 of group1:the item is required 
    SearchCondition oGroup1_SC1 =
          SearchCondition.HasPropertyByDisplayName("Item",
                                                   "Required");
 
    oGroup1_SC1 =
          oGroup1_SC1.EqualValue(VariantData.FromBoolean(true));
 
    //SearchCondition2 of group1:the item's DWG handle is 16C17  
    SearchCondition oGroup1_SC2 =
          SearchCondition.HasPropertyByDisplayName("Entity Handle",
                                                   "Value");
 
    oGroup1_SC2 =oGroup1_SC2.EqualValue(
              VariantData.FromDisplayString("16C17"));
 
    //SearchCondition1 of group2: the item is NOT required 
     SearchCondition oGroup2_SC1 =
          SearchCondition.HasPropertyByDisplayName("Item",
                                                 "Required");
 
    oGroup2_SC1 =
          oGroup2_SC1.EqualValue(VariantData.FromBoolean(false));
 
    //SearchCondition2 of group2: the item's DWG handle is 17C2E 
    SearchCondition oGroup2_SC2 =
          SearchCondition.HasPropertyByDisplayName("Entity Handle",
                                                  "Value");
 
    oGroup2_SC2 =oGroup2_SC2.EqualValue(
                  VariantData.FromDisplayString("17C2E"));
 
    //create group1 
    System.Collections.Generic.List oG1 =
          new System.Collections.Generic.List();
 
    oG1.Add(oGroup1_SC1);
    oG1.Add(oGroup1_SC2);
 
    //create group2 
    System.Collections.Generic.List oG2 =
          new System.Collections.Generic.List();
 
    oG2.Add(oGroup2_SC1);
    oG2.Add(oGroup2_SC2);
 
    //add groups to SearchConditions 
    s.SearchConditions.AddGroup(oG1);
    s.SearchConditions.AddGroup(oG2);
 
    //highlight the items 
    ModelItemCollection searchResults = 
        s.FindAll(Autodesk.Navisworks.Api.Application.ActiveDocument, false);
 
    Autodesk.Navisworks.Api.Application.ActiveDocument.
          CurrentSelection.CopyFrom(searchResults);
}

Comments

One response to “Search by “OR””

  1. Martin Holmberg Avatar
    Martin Holmberg

    Hi Xiaodong,
    Is it possible to nest groups to achieve a search with OR in multiple levels?

Leave a Reply to Martin HolmbergCancel reply

Discover more from Autodesk Developer Blog

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

Continue reading