Navisworks .NET API 2013 new feature – Clash 1

By Xiaodong Liang

Autodesk® Navisworks® 2013 software has exposed some new .NET APIs. The Clash Detection API is exposed. The TimeLiner API has continued to be enhanced since Navisworks 2013 TimeLiner feature includes Cost and thus provides 5D simulations. On the product side, Navisworks can not only open the file of the native Autodesk® Revit® format, but also import Grid and Level from a Revit file and the relevant API methods have been provided as well.  In the main product, you will also find the ability to drag the Current Selection from the main window and selection tree which we plan to allow third party applications to use in Viewer controls. We also exposed Current Viewpoint, Saved Viewpoints and Camera. NWCreate now supports the creation of Grids and Levels. In addition, if you need to make use of the COM API, you might be glad to see the COM samples migrated from VB6 to C#.

This article series will introduce the new APIs. In the first section, we take a look at Clash Detection. If you are professional with Navisworks API, you could just go ahead to enjoy the SDK sample in <Navisworks Manage 2013>apinetexamplesPlugInsClashDetective.

The Clash API allows you to access Clash Test, Clash Result and relevant information. First, you need to add the assembly <Navisworks Path>Autodesk.Navisworks.Clash.dll to your plug-in and import the namespace: Autodesk.Navisworks.Api.Clash.

Following are the main classes of Clash .NET API:
• DocumentClash: provides access to the various document-parts associated with a clash.
• DocumentClashTests: the document-part that holds the currently defined clash tests to access clash tests also provides the methods to modify tests or results.
• ClashTest: a clash-test, including all its settings and results. 
• ClashResult: represents a single geometry clash detected in a clash test.
• ClashResultGroup: permits grouping of multiple clash results together which can represent different aspects of the same issues

Get Clash

The Document class has a method called GetClash() which provides access to the DocumentClash object. DocumentClash.TestsData in turn, provides access to an object named DocumentClashTests which allows us to access all the clash tests. The Tests property on this DocumentClashTests helps get each test-related information like display name, tolerance, simulation type, etc. The Children property of each ClashTest helps further drill down to ClashResultGroup and further into each ClashResult. The code sample below shows how to get some information of the tests and drill down to the results and write the information to a text file.

using System.IO;
using System.Windows.Forms;
using Autodesk.Navisworks.Api;
using Autodesk.Navisworks.Api.DocumentParts;
using Autodesk.Navisworks.Api.Clash;
 
// Sample: dump clash
private void dumpClash()
{
    // create a stream to dump clash information
    MessageBox.Show("will dump Clash to c:\dumpClash.txt");
    StreamWriter sw = File.CreateText("c:\dumpClash.txt");
    Document document =
        Autodesk.Navisworks.Api.Application.ActiveDocument;
    DocumentClash documentClash = document.GetClash();
    DocumentClashTests oDCT = documentClash.TestsData;
 
    //dump clash tests
    foreach (ClashTest test in oDCT.Tests)
    {
        sw.WriteLine("***Test: " +
            test.DisplayName + "***");
        sw.WriteLine(" Status: " +
            test.Status.ToString());
        sw.WriteLine(" TestType: " +
            test.TestType.ToString());
        if (test.LastRun != null)
            sw.WriteLine(" LastRun: " +
                test.LastRun.Value.ToShortDateString());
        sw.WriteLine(" tolerance: " +
            test.Tolerance);
        sw.WriteLine(" comments: " +
            test.Comments);
        sw.WriteLine(" SimulationType: " +
            test.SimulationType.ToString());
        sw.WriteLine(" SimulationStep: " +
            test.SimulationStep.ToString());
        sw.WriteLine("    ---Results---");
        // dump Clash Result
        foreach (SavedItem issue in test.Children)
        {
            ClashResultGroup group =
                issue as ClashResultGroup;
            if (null != group)
            {
                sw.WriteLine("  test result group: " +
                    group.DisplayName);
                sw.WriteLine("  group status: " +
                    group.Status.ToString());
                foreach (SavedItem issue1 in group.Children)
                {
                    ClashResult rt1 = issue as ClashResult;
                    if (null != rt1)
                        writeClashResult(rt1, sw);
                }
            }
            ClashResult rt = issue as ClashResult;
            if (null != rt)
                writeClashResult(rt, sw);
        }//Result
    } //Test
    MessageBox.Show("done!");
    sw.Close();
}
private void writeClashResult(ClashResult rt,
                            StreamWriter sw)
{
    //dump some information of the result
    sw.WriteLine("      test result: " +
            rt.DisplayName);
    if (rt.CreatedTime != null)
        sw.WriteLine("  CreatedTime: " +
                rt.CreatedTime.ToString());
    sw.WriteLine("  Status: " +
            rt.Status.ToString());
    sw.WriteLine("  ApprovedBy: " +
            rt.ApprovedBy);
    if (rt.Center != null)
        sw.WriteLine(" Center[{0},{1},{2}]: ",
            rt.Center.X, rt.Center.Y, rt.Center.Z);
    sw.WriteLine(" SimulationType: " +
        rt.SimulationType.ToString());
}

ClashTest configures the selections which are involved in a test. ClashTestResult returns the selections which are collisions. The code snippet below gets the selections from one test and one result, and highlights the selection2 of one result. 

//Sample: Get selection information of Clash Test and Clash Result
private void getSelection()
{
    Document document = Application.ActiveDocument;
    DocumentClash documentClash = document.GetClash();
    DocumentClashTests oDCT = documentClash.TestsData;
    ClashTest t = documentClash.TestsData.Tests[0] as ClashTest;
    if (t != null)
    {
        //get SelectionA and SelectionB of one Clash Test
        ModelItemCollection oSelA = t.SelectionA.Selection.GetSelectedItems();
        ModelItemCollection oSelB = t.SelectionB.Selection.GetSelectedItems();
        ClashResult rt = t.Children[0] as ClashResult;
        if (rt != null)
        {
            //get Clash Selections of the result
            ModelItemCollection oSel1 = rt.Selection1;
            ModelItemCollection oSel2 = rt.Selection2;
            //highlight Selection2 of the result
            document.CurrentSelection.CopyFrom(oSel2);
        }
    }
 
}

There are two methods to run all tests or specific test.

DocumentClashTests oDCT = documentClash.TestsData;
    //run all tests
    // oDCT.TestsRunAllTests();
    //or run one test
    ClashTest oFirstTest = oDCT.Tests[0] as ClashTest;
    oDCT.TestsRunTest(oFirstTest);

 

Next, we will look at how to update clash.

(to be continued)


Comments

8 responses to “Navisworks .NET API 2013 new feature – Clash 1”

  1. Hi all
    how to get the Selected clash result item form clash detective window.

  2. Kar Sheng Avatar
    Kar Sheng

    Hi,
    Where can I get the SDK for Naviswork Simulate 2013?

  3. Hello,
    I want to build a Windows Form APP to get clashes in C#. I add reference “Autodesk.Naviswork.Api.dll”. However, when I run the code. It has this error. Is there anything that I missed.
    Exception thrown: ‘System.IO.FileNotFoundException’ in System.Windows.Forms.dll
    An unhandled exception of type ‘System.IO.FileNotFoundException’ occurred in System.Windows.Forms.dll
    Could not load file or assembly ‘Autodesk.Navisworks.Api.dll’ or one of its dependencies. The specified module could not be found.

  4. Xiaodong Liang Avatar
    Xiaodong Liang

    Hi,it is hard to diagnose with the description. Please isolate and make a small demo sample, and post it to Autodesk Navisworks API forum.
    https://forums.autodesk.com/t5/navisworks-api/bd-p/600

  5. Albert Yu Avatar
    Albert Yu

    Hello Mr. Liang,
    While I try to group the ClashResult, the warming indication shows the Argument is Read-Only, that makes me cannot group the ClashResult. Why?

  6. Hello Mr Liang,
    I am trying to reach the clashresultgroup to rename all my grouped clash results. I still can’t figure it out.

  7. Al_the_Builder Avatar
    Al_the_Builder

    Hello Mr. Liang,
    I am trying to write one center point of a clash result I have selected in Clash Detective to a text file. I cant seem to grasp how to do this correctly. I can get all the result center point wrote to a text file but not a specifically selected one (thanks to the help of your code). Is this even Possible? Any help would be greatly appreciated.
    Thanks,
    Al

  8. This article still useful in 2023. Thanks Xiaodong Liang. All three examples can run on my system.

Leave a Reply to Al_the_BuilderCancel reply

Discover more from Autodesk Developer Blog

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

Continue reading