How do I correctly serialize a derived class into the Eyeshot proprietary file format?

Listed below is the code that shows methods to override in order to correctly serialize your custom entity.

public class MyPic : Picture
{
	public MyPic(Picture pic) : base(pic)
	{

	}

	public Point3D StartPoint;
	public Point3D EndPoint;

	public override EntitySurrogate ConvertToSurrogate()
	{
		return new MyPictureSurrogate(this);
	}
}

public class MyPictureSurrogate : PictureSurrogate
{
	public MyPictureSurrogate(MyPic pic) : base(pic)
	{

	}
	public Point3D StartPoint;
	public Point3D EndPoint;
	protected override void CopyDataToObject(Entity entity)
	{
		MyPic pic=entity as MyPic;
		pic.StartPoint = StartPoint;
		pic.EndPoint = EndPoint;
		base.CopyDataToObject(entity);
	}

	protected override void CopyDataFromObject(Entity entity)
	{
		MyPic pic = entity as MyPic;
		StartPoint = pic.StartPoint;
		EndPoint = pic.EndPoint;
		base.CopyDataFromObject(entity);
	}

	protected override Entity ConvertToObject()
	{
		MyPic pic = new MyPic(new Picture(Plane, Width, Height, Image, Tiling));
		CopyDataToObject(pic);
		return pic;
	}
}

public class MyFileSerializer : FileSerializer
                           // : FileSerializerEx // use this if you need to include also Autodesk objects.
{
	public MyFileSerializer()
	{
	}
	public MyFileSerializer(contentType contentType) : base(contentType)
	{
	}
	protected override void FillModel()
	{
		base.FillModel();

		// Adds MyPic as sub-type of Picture
		// When you add a sub-type to an Eyeshot object you have to use an id > 1000.
		Model[typeof(Picture)]
			.AddSubType(1001, typeof(MyPic));

		// Adds MyPictureSurrogate as sub-type of PictureSurrogate
		Model[typeof(PictureSurrogate)]
			.AddSubType(1001, typeof(MyPictureSurrogate));

		// Defines properties for MyPictureSurrogate
		Model[typeof(MyPictureSurrogate)]
			.Add(1, "StartPoint")
			.Add(2, "EndPoint")
			.UseConstructor = false;
		
	}
	protected override Type GetTypeForObject(string typeName)
	{
		// this override is available since build 12.0.518
        
		Type t =  Type.GetType(typeName, false, true);
		if (t != null)
			return t;

		return base.GetTypeForObject(typeName);
	}
} 

For a complete source code sample, you can have a look at the FileFormatExtension sample, whereas, for a detailed description of how the Eyeshot proprietary file format works, you can check the related article.

UNIT TESTING

We perform a huge number of tests to verify backward reading compatibility for previous versions.
If you choose to extend it, please consider doing the same and feel free to send us your unit test project to be notified of any changes that will affect your extended format.

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.