Validating your Exchange Model File

We ran into a problematic CAD file recently. Importing the model with Exchange was successful and the model rendered and worked correctly. We later serialized the loaded model to a byte array for embedding in our application file (via A3DAsmModelFileExportToPrcStream). When later loading the file and de-serializing the byte array back to a model file (via A3DAsmModelFileLoadFromPrcStream) it seemed to work and returned a non-null model file pointer, however when attempting to render the model in Visualize the app crashed.

A deeper look showed that the serialized byte array for the model was only ~200 bytes. So clearly the serialize didn’t work and when de-serializing we ended up with a non-valid but not-null model file pointer.

We’ve updated our code so that now, after de-serializing, we validate the model file. If this fails, we mark the CAD as invalid in the tree. This is probably a good check to do in general when receiving a model file pointer, before attempting to use it. The below is C++/CLI called from C# to do the check.

        static bool IsValidModelFile(IntPtr modelFilePtr)
            if (modelFilePtr == IntPtr::Zero)
                return false;
            auto mfp = modelFilePtr.ToPointer();

            A3DAsmModelFileData modelFileData;
            auto iRet = A3DAsmModelFileGet(mfp, &modelFileData);
            if (iRet != A3D_SUCCESS)
                return false;
            A3DAsmModelFileGet(nullptr, &modelFileData);

            return true;

1 Like

Thanks for sharing Dave! This seems like it will be really helpful for those working in exchange. :slight_smile: