Saving Objects to FilesCognex VisionPro

This topic shows you how to use the VisionPro persistence system to save the current state of a VisionPro object to a file and how to restore saved object from the file. You can save any VisionPro object and restore it and its state at a later time. For example, you may want to save several different configurations of a PatMax tool for different applications.

Saving an Object to a File

The following code saves a configured PatMax tool to the binary file PatArchive.vpp.

Cognex.VisionPro.CogSerializer.SaveObjectToFile(mPatMaxTool,"c:\PatArchive.vpp")

This version of SaveObjectToFile saves all of the data in mPatMaxTool in binary formats.

Loading an Object

The following code uses LoadObjectFromFile to load an object from a file:

[VB.NET]
mPatMaxTool = ctype(CogSerializer.LoadObjectFromFile( "c:\PatArchive.vpp"),Cognex.VisionPro.PMAlign.CogPMAlignTool)
[C#]
mPatMaxTool = CogSerializer.LoadObjectFromFile("c:\PatArchive.vpp") as  Cognex.VisionPro.PMAlign.CogPMAlignTool;

LoadObjectFromFile returns a generic object that you need to cast to the type of the saved object.

Safely Reusing a CogJob Object

If your application repeatedly calls LoadObjectFromFile to load an object into a CogJob object, you must take care to properly clean up the CogJob object before reusing it. In particular, make sure that you call Shutdown on the job before you call LoadObjectFromFile. If you do not call Shutdown, your application may experience memory leaks or other problems.