Naveen Kumar

Navisworks API: Resolving Exceptions in LiNwcApiTerminate() When Using NwCreate API

By Naveen Kumar

When working with the Navisworks NwCreate API, you may encounter an exception at LiNwcApiTerminate(). This issue occurs in multiple versions, including NwCreate 2022, 2023, and 2024. This post explains why this happens and how to fix it.

Issue Overview: Following the "Get Started with NwCreate - Part 1" article from the Autodesk Developer Network (ADN) blog, I created a sample project that successfully generates a .nwc file. However, an exception occurs when calling LiNwcApiTerminate(), causing the program to crash.

NW_Error

Cause of the Issue: The main reason for this exception is not properly releasing all handles before calling LiNwcApiTerminate(). If any handles remain open, the API does not shut down cleanly, leading to an error.

Solution: Avoid this issue by ensuring all handles are properly cleaned up. Call LiNwcSceneDestroy(scene); for cleanup

#include <iostream>
#include <tchar.h>
#include "nwcreate/LiNwcAll.h"

// Forward declaration for doExport function
void doExport();

int main()
{
    // Initialise low-level API first.
    LiNwcApiErrorInitialise();
    // Then initialise the rest of the API.
    switch (LiNwcApiInitialise())
    {
    case LI_NWC_API_OK:
        doExport();
        break;
    case LI_NWC_API_NOT_LICENSED:
        printf("Not Licensed\n");
        return 1;
    case LI_NWC_API_INTERNAL_ERROR:
    default:
        printf("Internal Error\n");
        return 1;
    }

    // Terminate API after use
    LiNwcApiTerminate();
    return 0;
}

void doExport()
{
    LtWideString wfilename = L"C:\\test.nwc";
    

    // Create scene and geometry
    LtNwcScene scene = LiNwcSceneCreate();
    LtNwcGeometry geom = LiNwcGeometryCreate();

    if (!scene || !geom)
    {
        printf("Failed to create scene or geometry\n");
        return;
    }

    // Open geometry stream
    LtNwcGeometryStream stream = LiNwcGeometryOpenStream(geom);
    if (!stream)
    {
        printf("Failed to open geometry stream\n");
        LiNwcGeometryDestroy(geom);
        LiNwcSceneDestroy(scene);
        return;
    }

    LiNwcGeometryStreamBegin(stream, 0);
    LiNwcGeometryStreamTriangleVertex(stream, 1, 0, 0);
    LiNwcGeometryStreamTriangleVertex(stream, 2, 0, 10);
    LiNwcGeometryStreamTriangleVertex(stream, 3, 10, 10);
    LiNwcGeometryStreamEnd(stream);
    LiNwcGeometryStreamColor(stream, 0, 1, 0, 1);

    // Close geometry stream
    LiNwcGeometryCloseStream(geom, stream);

    // Add geometry to scene
    LiNwcSceneAddNode(scene, geom);

    // Cleanup geometry
    LiNwcGeometryDestroy(geom);

    // Write NWC file
    if (LiNwcSceneWriteCacheEx(scene, wfilename, wfilename, 0, 0) != LI_NWC_API_OK)
    {
        printf("Failed to write NWC file\n");
    }

    // Destroy scene to free memory
    LiNwcSceneDestroy(scene);
}


03/05/2025

10/26/2024

10/24/2024

10/20/2024

06/13/2024

04/05/2024

06/13/2023

10/04/2022

10/03/2022

05/04/2022

March 2025

Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          

Autodesk Blogs

Share