Listed below is the code that shows methods to override in order to correctly serialize your custom entity.
public class MyPicture : Picture
{
// custom data
public Point3D Point { get; set; }
public Vector3D Direction { get; set; }
public MyPicture(Plane pln, double width, double height, byte[] image)
: base(pln, width, height, image)
{
}
public override EntitySurrogate ConvertToSurrogate()
{
return new MyPictureSurrogate(this);
}
}
public class MyPictureSurrogate : PictureSurrogate
{
public Point3D Point;
public Vector3D Direction;
public MyPictureSurrogate(MyPicture picture) : base(picture)
{
}
protected override void CopyDataToObject(Entity entity)
{
MyPicture picture = entity as MyPicture;
picture.Point = Point;
picture.Direction = Direction;
base.CopyDataToObject(entity);
}
protected override void CopyDataFromObject(Entity entity)
{
MyPicture picture = (MyPicture) entity;
Point = picture.Point;
Direction = picture.Direction;
base.CopyDataFromObject(entity);
}
protected override Entity ConvertToObject()
{
MyPicture picture = new MyPicture(Plane, Width, Height, Image?.Data);
CopyDataToObject(picture);
return picture;
}
}
public class MyFileSerializer : FileSerializer
{
public MyFileSerializer()
{
}
public MyFileSerializer(contentType contentType) : base(contentType)
{
}
protected override void FillModel()
{
if (ModelIsCompiled()) return;
base.FillModel();
// Adds MyPicture 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(MyPicture));
// Adds MyPictureSurrogate as sub-type of PictureSurrogate
Model[typeof(PictureSurrogate)]
.AddSubType(1001, typeof(MyPictureSurrogate));
// Defines properties for MyPictureSurrogate
Model[typeof(MyPictureSurrogate)]
.Add(1, "Point")
.Add(2, "Direction")
.UseConstructor = false;
}
protected override Type GetTypeForObject(string typeName)
{
Type t = Type.GetType(typeName, false, true);
if (t != null)
return t;
return base.GetTypeForObject(typeName);
}
}
Usage:
MyPicture mp = new MyPicture(Plane.XY, 100, 80, Smile.ToByteArray())
{
Point = new Point3D(100, 200, 300),
Direction = new Vector3D(1, 0, 0)
};
design1.Entities.Add(mp);
MyFileSerializer mfs = new MyFileSerializer();
design1.SaveFile("test.eye", null, contentType.GeometryAndTessellation, mfs);
design1.Clear();
design1.OpenFile("test.eye", null, mfs);
For a complete source code sample, you can have a look at the FileFormatExtension code 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.
Comments
Is there a sample on how to serialize Eyeshot entities like Curve, Mesh...?
Hi Yaahov,
You can check the FileFormatExtension sample code.
i just want add curve or any other eyhost entity to my object to serialize to save as xml
is there more simple way do it?in yours sample seem need create several classes ,derive,overide....
Hi Yaahov,
Our proprietary file format is not based on XML, more details can be found here.
If you need to export entities in XML format you should implement it by yourself, sorry.
Please sign in to leave a comment.