Hello,
We recommend you try our new diagramming component
NOV Diagram. It contains more features and is a cross platform solution, which makes it easy to target multiple platforms like Windows (WinForms and WPF), Mac (Xamarin.Mac) and in the near future the web (Blazor WebAssembly) from single codebase.
Regarding your request - with
NOV Diagram you can easily make any class serializable and attach an instance of it to the
Tag property of the shape. It will then get properly serialized and deserialized.
For example, the following is a serializable NOV map (dictionary) with string keys and
NPoint values:
public class PointMap : NMap<string, NPoint>,
INDomCustomSerializable,
INDomDeeplyCloneable
{
#region Constructors
public PointMap()
{
}
public PointMap(PointMap source)
: base(source)
{
}
#endregion
#region INDomCustomSerializable
public void Serialize(NDomSerializationContext context, NPropertyBag propertyBag)
{
string[] keyArray = new string[Count];
double[] xArray = new double[Count];
double[] yArray = new double[Count];
int index = 0;
var iter = GetIterator();
while (iter.MoveNext())
{
string key = iter.Current.Key;
NPoint point = iter.Current.Value;
keyArray[index] = key;
xArray[index] = point.X;
yArray[index] = point.Y;
index++;
}
propertyBag.SetStringArrayValue(KeyArrayName, keyArray);
propertyBag.SetDoubleArrayValue(XArrayName, xArray);
propertyBag.SetDoubleArrayValue(YArrayName, yArray);
}
public void Deserialize(NDomDeserializationContext context, NPropertyBag propertyBag)
{
string[] keyArray = propertyBag.GetStringArray(KeyArrayName);
double[] xArray = propertyBag.GetDoubleArray(XArrayName);
double[] yArray = propertyBag.GetDoubleArray(YArrayName);
Clear();
for (int i = 0; i < keyArray.Length; i++)
{
Add(keyArray[i], new NPoint(xArray[i], yArray[i]));
}
}
#endregion
#region INDomDeeplyCloneable
public object DeepClone(NDomDeepCopyContext context)
{
return new PointMap(this);
}
#endregion
#region Constant
private const string KeyArrayName = "K";
private const string XArrayName = "X";
private const string YArrayName = "Y";
#endregion
}
You can then create and attach an instance of that class to the
Tag property of a shape:
NBasicShapeFactory factory = new NBasicShapeFactory();
NShape shape = factory.CreateShape(ENBasicShape.Rectangle);
shape.SetPinPoint(new NPoint(200, 200));
PointMap pointMap = new PointMap();
pointMap.Add("Point 1", new NPoint(0, 0));
pointMap.Add("Point 2", new NPoint(100, 100));
shape.Tag = pointMap;
And then when you save and load your drawing, the Tag property of the shape will be preserved, because it is an instance of a class that implements the INDomCustomSerializable interface:
drawingView.SaveToFile(@"C\test.ndb");
drawingView.Document = new NDrawingDocument();
drawingView.LoadFromFile(@"C:\test.ndb");
Please download and install NOV Diagram and try the code above. Let us know if you have any questions.
Best Regards,
Nevron Support Team