We introduced Object Properties and how to Find Item. In this post, we will learn more on Search API.
Search API corresponds to the Find Items functionality in Roamer. It is similar to the product functionality, but with further refined conditions. It is fast because it is happens in native code, but can only do what Roamer can do.
Selection is what to search (i.e. Where to look).
Conditions is what to find. A Condition is basically a test against an object property with quite flexible usage. It also has a few other options that are properties on the Search class.
//1) Create a SearchCondition instance with the condition
VariantData oData =
VariantData.FromDisplayString("16C17");
SearchCondition oSearchCondition =
SearchCondition.HasPropertyByDisplayName("Entity Handle",
"Value");
oSearchCondition =
oSearchCondition.EqualValue(oData);
s.SearchConditions.Add(oSearchCondition);
//2) Create a SearchCondition instance with construction method
s.SearchConditions.Add(
new SearchCondition(
new NamedConstant(PropertyCategoryNames.
AutoCadEntityHandle,
"Entity Handle"),
new NamedConstant(DataPropertyNames.
AutoCadEntityHandleValue,
"Value"),
SearchConditionOptions.IgnoreNames,
SearchConditionComparison.Equal,
VariantData.FromDisplayString("16C17")));
//3) invert a search condition
SearchCondition s1 =
SearchCondition.HasCategoryByName(
PropertyCategoryNames.Transform);
SearchCondition s2 = s1.Negate();
SearchCondition Group
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”.
//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<SearchCondition> oG1 =
new System.Collections.Generic.List<SearchCondition>();
oG1.Add(oGroup1_SC1);
oG1.Add(oGroup1_SC2);
//create group2
System.Collections.Generic.List<SearchCondition> oG2 =
new System.Collections.Generic.List<SearchCondition>();
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);
