Only valid for ModelItem in a Document

Only valid for ModelItem in a Document

lanneauolivier
Enthusiast Enthusiast
300 Views
5 Replies
Message 1 of 6

Only valid for ModelItem in a Document

lanneauolivier
Enthusiast
Enthusiast

Hey,

 

When I create a new SelectionSet from a ModelItemCollection, I cannot access to properties :

 

Document doc = Autodesk.Navisworks.Api.Application.ActiveDocument;

ModelItemCollection items = ...

SelectionSet selectionSet = new SelectionSet(items);

ModelItemCollection itemsFromSet = selectionSet.GetSelectedItems(doc);

itemsFromSet .First().DisplayName; // ok

itemsFromSet .First().PropertyCategories; // exception here

 

Is there something wrong here ? Thanks

Accepted solutions (1)
301 Views
5 Replies
Replies (5)
Message 2 of 6

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @lanneauolivier ,


Could you please try using the below code?

 Document doc = Autodesk.Navisworks.Api.Application.ActiveDocument;

 string selectionSetName;
 ModelItemCollection selectedItems = doc.CurrentSelection.SelectedItems;
 SelectionSet selectionSet = new SelectionSet(selectedItems);
 selectionSet.DisplayName = selectionSetName;
 doc.SelectionSets.InsertCopy(0,selectionSet);

 SelectionSet ss1 = doc.SelectionSets.ToSavedItemCollection().Where(e => e.DisplayName == selectionSetName).First() as SelectionSet;
 ModelItemCollection itemsFromSet = ss1.GetSelectedItems();
 string itemDisplayName= itemsFromSet.First().DisplayName;
 MessageBox.Show(itemDisplayName);
 ModelItem item = itemsFromSet.First();
 if(item!=null)
 {
     PropertyCategoryCollection collection = item.PropertyCategories;               
 }

This code worked fine at my end.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 6

lanneauolivier
Enthusiast
Enthusiast

Hi @naveen.kumar.t 

This works for me as well because you add the SelectionSet to the document.

In my case, I do not want to add the SelectionSet in the document (I also do not want to add it then delete it).

 

It seems confusing because I give the Document as an argument to selectionSet.GetSelectedItems(doc);

It should works

0 Likes
Message 4 of 6

naveen.kumar.t
Autodesk Support
Autodesk Support

HI @lanneauolivier ,

 

Could you please explain your workflow in more detail?

From the code you attached, I see that while the selection set with the list of model items is ready to be created, it hasn’t actually been added to the project yet.


Because the selection set isn’t part of the project, trying to access its elements results in an exception.


If your goal is to use GetSelectedItems from the selection set, why not use the ModelItemCollection directly, since you’re already passing it into new SelectionSet()?

In my opinion, the behavior you’re seeing in Navisworks seems correct.


Could you please walk me through your workflow step by step? That will help me fully understand your scenario and assist you more effectively.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 5 of 6

lanneauolivier
Enthusiast
Enthusiast

@naveen.kumar.t 

 

The aim of my algo is to create severals SelectionSet from user inputs. (like : finding pipes where radius is x, inside boundingBox from other pipes, where properties are ...)

The logic is defined within an external xml I am parsing on the fly and could be quite complicated, like using adding, substracting, iterating over properties, items, ...

I have a List<SelectionSet> to store every SelectionSet defined by the user. I would like to manage everything as a SelectionSet, some on them are added to the document (the Sets the user is looking for), and other are working SelectionSet used by the user to acheived the search.

I am asking it because it makes my code more generic, but I could change for ModelItemCollection if no solution

 

Thanks 🙂

Message 6 of 6

naveen.kumar.t
Autodesk Support
Autodesk Support
Accepted solution

Hi @lanneauolivier ,

 

Yes, I also believe that to resolve this issue, it would be better to use ModelItemCollection.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes