Does anyone know why my function for exploding empty groups is not working properly?

Does anyone know why my function for exploding empty groups is not working properly?

mtrainottiGHXJH
Enthusiast Enthusiast
714 Views
5 Replies
Message 1 of 6

Does anyone know why my function for exploding empty groups is not working properly?

mtrainottiGHXJH
Enthusiast
Enthusiast

Below is my function for exploding empty groups and removing an item from a group if it is the only item in a group. This function works as expected with the caveat that I have to run it multiple times to remove all the groups. I have tried several solutions including rerunning the test recursively every time a group is removed until it does not return but this still does not work. I think it may have to do with do with items moving around the testdata in the same step(idk). Does anybody have any ideas for a solution or a method to await the testdata refreshing after an item is moved?

 

 

        void explode_empty_groups()
        {
            var tests_data = doc_cd.TestsData.Tests;
            foreach (ClashTest ctest in tests_data)

            {
                    foreach (SavedItem _item in ctest.Children)
                    {

                        if(_item.IsGroup)
                        {
                            ClashResultGroup clash_group = _item as ClashResultGroup;
                            if (clash_group == null) 
                            {
                                //isgroup & null
                                doc_cd.TestsData.TestsRemove(ctest, _item);
                                continue;
                            }


                            //move child out
                            if (clash_group.Children.Count == 1)
                            {
                                doc_cd.TestsData.TestsMove(clash_group, 0, ctest, 0);
                                doc_cd.TestsData.TestsRemove(ctest, clash_group);
                                continue;
                            }

                            //remove group
                            if (clash_group.Children.Count == 0)
                            {
                                doc_cd.TestsData.TestsRemove(ctest, clash_group);
                                continue;
                            }


                        }

                    }


                return;
            }
        }

 

0 Likes
Accepted solutions (1)
715 Views
5 Replies
Replies (5)
Message 2 of 6

mtrainottiGHXJH
Enthusiast
Enthusiast

Also the return after a single clash test intentional as I am testing this over 1 clash test currently.

0 Likes
Message 3 of 6

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @mtrainottiGHXJH ,

 

I am able to reproduce the issue.
I have raised your issue to the Navisworks Engineering team.
But I haven't received any response from the engineering team yet.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 4 of 6

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @mtrainottiGHXJH ,

 

I still have not received any response from the NW Engineering team.
So I have logged the issue( NW-58887 ) with the Navisworks Engineering team.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 5 of 6

naveen.kumar.t
Autodesk Support
Autodesk Support
Accepted solution

Hi @mtrainottiGHXJH ,

 

I had a discussion with my team.
The below sample code will help you.

 private void Code(Document doc)
        {
            DocumentClash docClash = doc.GetClash();
            DocumentClashTests tests = docClash.TestsData;
            foreach (ClashTest test in tests.Tests)
            {
                var groupToDelete = new List<ClashResultGroup>();

                foreach (SavedItem savedItem in test.Children)
                {
                    if (savedItem is ClashResultGroup)
                    {
                        ClashResultGroup resultGroup = savedItem as ClashResultGroup;
                        string resultGroupName = resultGroup.DisplayName;

                        //If the group does not have any clash, the group will be deleted
                        if (resultGroup.Children.Count() == 0)
                        {
                            groupToDelete.Add(resultGroup);
                        }
                    }
                }
                foreach (ClashResultGroup resultGroup in groupToDelete)
                {
                    docClash.TestsData.TestsRemove(resultGroup.Parent, resultGroup);

                }
            }
        }

Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

Message 6 of 6

mtrainottiGHXJH
Enthusiast
Enthusiast

I appreciate your help! I wasn’t aware you could remove this way.

0 Likes