Search name constant contain string

Search name constant contain string

Anonymous
Not applicable
3,645 Views
23 Replies
Message 1 of 24

Search name constant contain string

Anonymous
Not applicable

Dear all,

 

I want to search the elements that has a name constant property.

For example, 

Category: Element

Property: Phase Created

Value: Phase "New Construction", #86961

 

How can I search this element with only a keyword "New"?

 

I tried to search through below code, but nothing returns.

SearchCondition.HasPropertyByDisplayName("Element""Phase Created").DisplayStringContains("New").IgnoreStringValueCase()

 

Best Regards,

John

0 Likes
Accepted solutions (1)
3,646 Views
23 Replies
Replies (23)
Message 2 of 24

augusto.goncalves
Alumni
Alumni

Sorry, which API is this SearchCondition that you're using?

 

UPDATE: sorry, I got confused, this is a Navisworks API

Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
0 Likes
Message 3 of 24

ulski1
Collaborator
Collaborator

hi,

I recommend that you open and compile the dll for the Autodesk provided plugin tool called appinfo (see C:\Program Files\Autodesk\Navisworks Simulate 2016\api\NET\examples\Tools\AppInfo)

After you get this tool up and running you select the item manually you want to seach for and start the appinfo tool  - in the tool you can browse the object tree (look for selected item) and see where you search fails - that said the most likely problem is that you have to use the internal category name not the displayed category name

 

br

Ulrik

0 Likes
Message 4 of 24

Anonymous
Not applicable

Hi Augusto Goncalves,

 

Thanks for your interest. It is Navisworks API.

Autodesk.Navisworks.Api.Search.Conditions.

 

Hi Ulrik,

 

Thanks for your answer.

I tried to use internal name but it still doesn't work.

The problem I faced is that the value of the property is NameConstant, instead of DisplayString.

 

Here are one more example.

Category: Element

Property: Id

Value: 123456

 

How can I search this element with part of it, says '234'?

 

Below code returns me nothing.

SearchCondition.HasPropertyByDisplayName("Element""Id").DisplayStringContains("234").IgnoreStringValueCase()

The reason behind is the value of Element Id is Int, instead of DisplayString.

 

Best Regards,

John

0 Likes
Message 5 of 24

ulski1
Collaborator
Collaborator

hi again John,

when navisworks read data from other CAD systems Revit,AutoCad etc. it will automatically try to guess if a value is a string, integer, double or date. The number 123 is most likely a int and not a string. The values are accessed via a class  "VariantData" (look into this)

 

You can only use "displaystring" on things that are a string. If you want to have a search method that can handle both integer and string you should try to use ".value" instead of "displaystring". I also recommend that you read in the documentation about "PruneBelowMatch", "StartGroup" and  "Locations"

 

 

Here is a code snippet to get you started

 

using (VariantData MyValue = DPC.FindPropertyByName(NWDataPropertyMyProp).Value)

{
if (MyValue.IsDisplayString)
{
Result = MyValue.ToDisplayString();
}
else
{
Result = MyValue.ToString();
}
}

 

 

Ulrik

0 Likes
Message 6 of 24

Anonymous
Not applicable

Hi again Ulrik,

 

Thanks again for your detailed reply.

 

The main problem I faced is to search the element with part of the value.

I read the document, but there is only method 'DisplayStringContains', which works with DisplayString only, but not Int and NamedConstant.

 

Best Regards,

John

0 Likes
Message 7 of 24

ulski1
Collaborator
Collaborator

try

SearchCondition.HasPropertyByName(NWPropertyCategory, NWDataPropertyTag).StartGroup();

 

0 Likes
Message 8 of 24

ulski1
Collaborator
Collaborator

full line is something like:

searchObject.SearchConditions.Add(SearchCondition.HasPropertyByName(NWPropertyCategory, NWDataPropertyTag).StartGroup());

0 Likes
Message 9 of 24

Anonymous
Not applicable

This will search all the elements that have that properties.

However, I just want to search the elements with part of the value.

0 Likes
Message 10 of 24

xiaodongliang
Contributor
Contributor

Hi John,

 

you can use SearchConditionComparison.DisplayStringContains, orSearchConditionComparison.DisplayStringWildcard

 

 

 Search search = new Search();
search.Selection.SelectAll();
search.SearchConditions.Add(new SearchCondition(
                            new NamedConstant(PropertyCategoryNames.AutoCadEntityHandle,"Element"),
                            new NamedConstant(DataPropertyNames.AutoCadEntityHandleValue, "Phase Created"),
                            SearchConditionOptions.IgnoreNames,
                            SearchConditionComparison.DisplayStringContains,
                            VariantData.FromDisplayString("New"))); 

ModelItemCollection items = search.FindAll(Nw.Application.ActiveDocument, false);
Nw.Application.ActiveDocument.CurrentSelection.CopyFrom(items);

 

Or, you could also build LINQ clause 

 

   IEnumerable<ModelItem> oMC = 
             Nw.Application.ActiveDocument.Models[0].RootItem.DescendantsAndSelf.Where<ModelItem>
                (x=>x.PropertyCategories.FindPropertyByDisplayName("Element","Phase Created").Value.ToDisplayString().Contains("New"));

 

0 Likes
Message 11 of 24

xiaodong_liang
Autodesk Support
Autodesk Support
again, posted by my personal account. Do not be surprised 🙂
0 Likes
Message 12 of 24

Anonymous
Not applicable

Hi Xiao Dong,

 

Long time no chat.

 

I tried your codes but there is nothing returned.

I guess the problem is the the Property 'Phase Created' is a NamedConstant instead of DisplayString.

 

However, I cannot find a method that has the 'Contain' function for other types, such as Int32 and NamedConstant.

 

Best Regards,

John

0 Likes
Message 13 of 24

xiaodong_liang
Autodesk Support
Autodesk Support
Hi John,

will it be fine if you toString on the value and use Contain function?
0 Likes
Message 14 of 24

Anonymous
Not applicable

Hi XiaoDong,

 

It cannot be done in the Search API.

Would you mean I search all elements containing this property and use For loop to compare all items with the keyword?

 

Best Regards,

John

0 Likes
Message 15 of 24

xiaodong_liang
Autodesk Support
Autodesk Support
I meant the second way LINQ which sorts out the items from rootitem.
0 Likes
Message 16 of 24

Anonymous
Not applicable

Hi XiaoDong,

 

I tried your code, but it doesn't work.

 

IEnumerable<ModelItem> oMC = 
             Nw.Application.ActiveDocument.Models[0].RootItem.DescendantsAndSelf.Where<ModelItem>
                (x=>x.PropertyCategories.FindPropertyByDisplayName("Element","Phase Created").Value.ToDisplayString().Contains("New"))

 

Best Regards,

John

0 Likes
Message 17 of 24

ulski1
Collaborator
Collaborator

hi John,

try this:

Int32 myi = 0;

if (int.TryParse(item, out myi) == false)
{

//display error

}

else

{

SearchObject.SearchConditions.Add(SearchCondition.HasPropertyByName(NWPropertyCategory,NWDataProperty).EqualValue(VariantData.FromInt32(myi)).StartGroup());

}

 

br

Ulrik

0 Likes
Message 18 of 24

Anonymous
Not applicable

Hi Ulrik,

 

Thanks for your suggestion. However, this method need to search with the exact value.

For example, I want to find an element that has value '1234567',but I search nothing with keyword '3456' by your method.

 

Best Regards,

John

0 Likes
Message 19 of 24

xiaodong_liang
Autodesk Support
Autodesk Support
Hi John,

could you share a test model? I can give a test at my side.
0 Likes
Message 20 of 24

ulski1
Collaborator
Collaborator

I see the problem - Navisworks does not offer "contains" or "wildcard" as search criteria for numbers. You can only use "smaller than"/"larger than" etc.

I agree with Xiaodong Liang that you will have to select all objects with this number attribute and then write you own loop / filter

br

Ulrik

0 Likes