This topic contains the following sections.
Persistence is the ability to save the state of an object into some storage format or to load a previously saved state into an object. VisionPro has persistence features at the component level and within the vision tool edit controls.
The CogSerializer class provides methods that load and save objects to files, strings, or streams:
| Storage type | Saving method | Loading method |
| File | SaveObjectToFile | LoadObjectFromFile |
| String | SaveObjectToString | LoadObjectFromString |
| Stream | SaveObjectToStream | LoadObjectFromStream |
By default, VisionPro saves object data in binary format. Overloads of the methods listed in the preceding table let you specify other formatters as well.
When you save an object, you can specify the types of object properties to save. The save options are in CogSerializationOptionsConstants. The default is to save all of an object's properties.
Similarly when you load an object, you can specify which of the saved properties to load.
The following code fragments show some examples of how to use these methods.
ICogTool myTool; myTool = CogSerializer.LoadObjectFromFile(filename) as ICogTool; CogSerializer.SaveObjectToFile(myTool, @"c:\foo.vpp", typeof(SoapFormatter),CogSerializationOptionsConstants.All); myTool = null; myTool = CogSerializer.LoadObjectFromFile(@"c:\foo.vpp") as ICogTool; CogSerializer.SaveObjectToFile(myTool, @"c:\foo.vpp", typeof(BinaryFormatter)); myTool = null; myTool = CogSerializer.LoadObjectFromFile(@"c:\foo.vpp") as ICogTool;
You can use CogSerializer to save and load objects that you define by deriving them from CogToolBase.
Each vision tool edit control has buttons that allow you to save, load or reset the state of the underlying tool. The edit control can save to and load from VisionPro persistence files; these files have a .vpp extension and are in XML format. When you save a vision tool object state, you have the option to save either all of the tool's properties or the tool without input images or results. See the documentation for each vision tool edit control for descriptions of the control buttons.
When you load a saved tool that was created with an earlier version of VisionPro, VisionPro always uses the most recent version of the tool. This is unlike the default .NET behavior which allows for side-by-side versioning.
In fact, any object that you persist from the Cognex.VisionPro.* namespace, uses the most recent version of its corresponding assembly.
In your own custom tools you can specify whether to use the most recent version of the tool or side-by-side versioning.
To specify most-recent-version versioning include the following attribute in your assembly:
[assembly:CogSerializationBinderAttribute];
To specify side-by-side versioning, you ca either omit the attribute or use the following:
[assembly:CogSerializationBinderAttribute(null)];
Additionally you can specify custom serialization by defining a custom binder. You may want to do this when you want to handle the case where a type has been moved from one assembly to another, a type has been renamed, or when you want to selectively upgrade. To specify a custom binder, use something like the following attribute in your assembly:
[assembly:CogSerializationBinderAttribute("MyCompany.MyCustomBinderType, MyCompany.MyToolAssemblyName, Version=2.0.1.0")];In this example, MyCompany.MyCustomBinderType is the binder type name. This class must derive from the .NET base class SerializationBinder. MyCompany.MyToolAssemblyName, Version=2.0.1.0 is the name of the assembly. This can be as brief as simply the name of the assembly, or you can include the assembly version number, a public key, and so on.