Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

How to merge SelectionSets with the same name.

rampseeker
Enthusiast

How to merge SelectionSets with the same name.

rampseeker
Enthusiast
Enthusiast

If a SelectionSet with the same name as the Set to be added exists within the Folderitem, I would like to merge that Set with a new Set.

 

 

//for save with selectionSet type
ModelItemCollection _modelItemCollection = new ModelItemCollection();                
_modelItemCollection.Add(modelItem);
int _isExistSet = currentFolder.Children.IndexOfDisplayName(_modelSet.DisplayName);
//IF same name selectionSet exist
if (_isExistSet != -1)
{
	SelectionSet _existingSet = currentFolder.Children[_isExistSet] as SelectionSet;	
	//below code just insert same name set into currentFolder...			
	NavisworksApp.ActiveDocument.SelectionSets.InsertCopy(currentFolder, _isExistSet, _modelSet);                    
	//Also I tried this
_existingSet.ExplicitModelItems.Add(modelItem);
//or like this
foreach (ModelItem item in _existingSet.ExplicitModelItems)
{
    _models_temp.Add(item);
}
_models_temp.Add(modelItem);
SelectionSet _mergetSet = new SelectionSet(_models_temp)
{
    DisplayName = _modelSet.DisplayName,
};
NavisworksApp.ActiveDocument.SelectionSets.AddCopy(currentFolder, _mergetSet);
NavisworksApp.ActiveDocument.SelectionSets.RemoveAt(_isExistSet);
}

 

 

 

 

0 Likes
Reply
Accepted solutions (2)
747 Views
6 Replies
Replies (6)

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @rampseeker ,

 

I am having difficulty understanding your issue clearly.

Could you please provide more details along with screenshots?


From what I gather, you have two selection sets (A and B) with the same name inside a folder. You want to merge these two selection sets into a single selection set (C), place it in the same folder, and then delete the original selection sets (A and B).


Is my understanding correct?


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

rampseeker
Enthusiast
Enthusiast

Yes, you understood correctly.duplicate Set.PNG

0 Likes

alexisDVJML
Collaborator
Collaborator

No specific solution below, just some thoughts.

A/ I can identify 2 different "features" in your 2 posts in this thread:
1) your first post: adding items in an existing set instead of creating a new one if the name already exists in this folder
2) your second post: merging multiple Selection Sets based on their name.

While some of the code for these 2 scenarios will probably be shared or at least similar, I would still have 2 different methods to handle these.

B/ Explicit vs Search SavedSets
In addition, you will have to handle differently explicit (easier case) and search (hum what to do? ignored? convert to explicit) sets.

Main Scientist, Full Stack Developer & When Time Permits Director of IDIGO ► On your marks, Set, Go

rampseeker
Enthusiast
Enthusiast

For the feature I wrote, I tried several methods because I couldn't find a way to insert Modelitems into an existing SelectionSet no matter how much I searched in forum. One of them came from the process of creating Modelitem->ModelitemCollection->SelectionSet and adding that Set to the current folder if a set with the same name does not exist, and attempting to merge if a Set with the same name exists. Thank you for sharing your ideas.

rampseeker
Enthusiast
Enthusiast
Accepted solution

I couldn't find a way to merge sets directly, so I had to create a new set in this way.

 

int _isExistSet = currentFolder.Children.IndexOfDisplayName(_modelSet.DisplayName);
if (_isExistSet != -1)
{
    try
    {
        SelectionSet _existingSet = currentFolder.Children[_isExistSet] as SelectionSet;
        ModelItemCollection _models_temp = new ModelItemCollection();
        _existingSet.GetSelectedItems().CopyTo(_models_temp);
        _models_temp.Add(modelItem);
        _modelSet = new SelectionSet(_models_temp)
        {
            DisplayName = _modelSet.DisplayName
        };
        NavisworksApp.ActiveDocument.SelectionSets.Remove(currentFolder, _existingSet);
        NavisworksApp.ActiveDocument.SelectionSets.AddCopy(currentFolder, _modelSet);

 

alexisDVJML
Collaborator
Collaborator
Accepted solution
Neat!
Could probably:
- check the existing one is a SelectionSet and not a folder, etc.
- check if the existing one is a search, and decide what to do in this case?
- reinsert the new one at same position as original one
- loop while have others with the same name

But as long as it fits your requirements, KISS is fine 😉
Main Scientist, Full Stack Developer & When Time Permits Director of IDIGO ► On your marks, Set, Go