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.
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 Licensedn");
return 1;
case LI_NWC_API_INTERNAL_ERROR:
default:
printf("Internal Errorn");
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 geometryn");
return;
}
// Open geometry stream
LtNwcGeometryStream stream = LiNwcGeometryOpenStream(geom);
if (!stream)
{
printf("Failed to open geometry streamn");
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 filen");
}
// Destroy scene to free memory
LiNwcSceneDestroy(scene);
}


Leave a Reply