Navisworks .NET API 2013 new feature – Clash 2

By Xiaodong Liang

Continue with last post, we will look further on the API of Clash Detective. 

 

Update Clash

To add or modify clash, we cannot manipulate the properties of the existing objects directly. We need to use the document part: DocumentClashTests. It provides various methods to update the Clash. If there is no corresponding method to update one property such as, say,  selection of a test, we need to create a copy of the existing object, modify the copied instance and update the existing object with the copied one. The code below changes the display name and SelectionA of one ClashTest, also changes the status of one ClashResult.

private void modifyClash()    {        Document document = Application.ActiveDocument;        DocumentClash documentClash = document.GetClash();        DocumentClashTests oDCT = documentClash.TestsData;        ClashTest t = oDCT.Tests[0] as ClashTest;        if (t != null)        {            //change the name of one ClashTest            oDCT.TestsEditDisplayName(t, "NewName" +                t.DisplayName);            ClashResult rt = t.Children[0] as ClashResult;            //change the status of one result to Approved            if (rt != null)                oDCT.TestsEditResultStatus(rt,                     ClashResultStatus.Approved);        }        //change the SelectionA of the Clash Test.        ClashTest oCopyt = t.CreateCopy() as ClashTest;        //modify SelectionA with the current selection        oCopyt.SelectionA.Selection.CopyFrom(            document.CurrentSelection.SelectedItems);        //modify SelectionA with the current selection         if (document.CurrentSelection.SelectedItems.Count > 0)        {            oCopyt.SelectionA.Selection.CopyFrom(                document.CurrentSelection.SelectedItems);            //update the existing test            oDCT.TestsEditTestFromCopy(t, oCopyt);        }    }

Similarly to modifying clash tests, we can add new clash tests by either creating a new test, or copying from an existing test, and adding it using the methods of DocumentClashTests.

//Sample: Add Clash Test    private void addClashTest()    {        Document document = Application.ActiveDocument;        DocumentClash documentClash = document.GetClash();        DocumentClashTests oDCT = documentClash.TestsData;        //create a ClashTest        ClashTest oNewTest = new ClashTest();        oNewTest.DisplayName = "myTest";        //Set SelectionA and SelectionB of the ClashTest        ModelItemCollection oSelA = new ModelItemCollection();        ModelItemCollection oSelB = new ModelItemCollection();        //assume the first and second model item are not null.        ModelItemEnumerableCollection oModelCollect =                       document.Models[0].RootItem.Children;        if(oModelCollect.Count() > 1)        {        oSelA.Add( oModelCollect .ElementAt(0));           oSelB.Add( oModelCollect .ElementAt(1));        oNewTest.SelectionA.Selection.CopyFrom(oSelA);        oNewTest.SelectionB.Selection.CopyFrom(oSelB);        //set other properties of the test …-}        oDCT.TestsAddCopy(oNewTest);    }

The following sample illustrates how we can reorganize the Clash Results by grouping them with ClashResultGroup

 

// Sample:  Add Clash Result Group. Moves all 'NEW' top level results into a group named "Grouped:NEW",             private void addResultsGroup()    {        DocumentClash documentClash = Application.ActiveDocument.GetClash();        DocumentClashTests oDCT = documentClash.TestsData;        ClashTest t = oDCT.Tests[0] as ClashTest;        //add new group if required        ClashResultGroup group;        int groupNdx = t.Children.IndexOfDisplayName("Grouped:NEW");        if (-1 == groupNdx)        {            
ClashResultGroup newGroup = new ClashResultGroup();            newGroup.DisplayName = "Grouped:NEW";            oDCT.TestsInsertCopy(t, 0, newGroup);            group = (ClashResultGroup)t.Children[0];        }        else        {            group = (ClashResultGroup)t.Children[groupNdx];        }        //moves all 'NEW' top level issues into the group        int resultsCount = t.Children.Count;        for (int i = resultsCount - 1; i >= 0; i--)        {            SavedItem issue = (SavedItem)t.Children[i];            ClashResult rt = issue as ClashResult;            if (null != rt)            {                if (ClashResultStatus.New == rt.Status)                    oDCT.TestsMove(t, i, group, 0);            }        }    }

The content in this article should help you get started with the Clash API. Please refer to API help document for more details. All the codes have been tested with the SDK model clashtest.nwd.

In the next post, we will introduce viewpoints and saved viewpoints.


Comments

3 responses to “Navisworks .NET API 2013 new feature – Clash 2”

  1. Thanks for this and other examples. I am also looking for a means of returning the Revit family names of both items in the clash. Could you provide an example of how these might be found. Thanks again.

  2. Hi Xiaodong Liang,
    i want to get the selected clash result name from clash detective window and also want to select clash result in clash detective window,
    could you please confirm whether it is posible through Net API.

  3. This code is very useful, but the TestsInsertCopy() function is throwing a stack overflow exception due to the deep recursion of the inner functions. Could you suggest anything to insert a clash result group?

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading