# Validating your Exchange Model File

**URL:** <https://forum.techsoft3d.com/t/validating-your-exchange-model-file/985>\
**Category:** HOOPS Exchange\
**Tags:** exchange\
**Created:** [September 2, 2022, 3:45pm UTC](https://forum.techsoft3d.com/t/validating-your-exchange-model-file/985 "2022-09-02T15:45:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dave.calkins](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.techsoft3d.com/dave.calkins/32/156_2.png) [@dave.calkins](https://forum.techsoft3d.com/u/dave.calkins)\
**Post date:** [September 2, 2022, 3:45pm UTC](https://forum.techsoft3d.com/t/validating-your-exchange-model-file/985/1 "2022-09-02T15:45:58Z")

</div>

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.

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

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

            return true;
        }

```

---

<div class="post-metadata">

**Author:** ![gabriel.peragine](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.techsoft3d.com/gabriel.peragine/32/1535_2.png) [@gabriel.peragine](https://forum.techsoft3d.com/u/gabriel.peragine)\
**Post date:** [September 2, 2022, 9:19pm UTC](https://forum.techsoft3d.com/t/validating-your-exchange-model-file/985/2 "2022-09-02T21:19:06Z")

</div>

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