Interface IvdProxyFigure
- Namespace
- VectorDraw.Serialize
- Assembly
- VectorDraw.Serialize.dll
Interface representing that the object is a custom proxy figure.
public interface IvdProxyFigure : IvdProxySerializer, IVDSerialise
- Inherited Members
Examples
A form with a vdFramedControl that contains a command line event handler to execute a custom command "test" and
a custom figure MyLine with two custom properties StartPoint and EndPoint of type gPoint,
and the code to serialize/deserialize the custom properties, draw the line, add grip points and osnap points to it, and also add a static method to draw the line by command.
using vdControls;
using VectorDraw.Geometry;
using VectorDraw.Professional.vdCollections;
using VectorDraw.Professional.vdFigures;
using VectorDraw.Professional.vdObjects;
using VectorDraw.Professional.vdPrimaries;
using VectorDraw.Render;
using VectorDraw.Serialize;
namespace VDCAD_TEST
{
public partial class Form1 : Form
{
vdFramedControl vdraw = new vdFramedControl();
vdDocument doc { get { return vdraw.BaseControl.ActiveDocument; } }
public Form1()
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
this.Controls.Add(vdraw);
vdraw.Dock = DockStyle.Fill;
vdraw.CommandLine.CommandExecute += CommandLine_CommandExecute;
}
//this method is called when the user execute any command in the command line,
//you can use it to execute your code when a specific command is executed by the user,
private void CommandLine_CommandExecute(string commandname, bool isDefaultImplemented, ref bool success)
{
if (isDefaultImplemented) return;
doc.Prompt("\r\nCommand:" + commandname); doc.Prompt(null);
if (commandname.Equals("test", StringComparison.OrdinalIgnoreCase))
{
success = true;
MyLine.Commmand_MyLine(doc);
}
}
}
//example of simple custom line figure with two custom properties StartPoint and EndPoint of type gPoint,
//you can add any number of custom properties of any type
//in this example we will add code to serialize/deserialize the custom properties,
//draw the line, add grip points and osnap points to it, and also add a static method to draw the line by command.
//this class example contains the basic overrides that you need to write for any custom figure
public class MyLine : vdFigure, IvdProxyFigure
{
gPoint mStartPoint = new gPoint();
gPoint mEndPoint = new gPoint();
//a default public empty contructor is required for the deserialization to work, a
//and you can add another constructor that takes document as parameter to set the document for the object when you create it by code, but it is not required.
public MyLine()
{
}
public MyLine(vdDocument doc)
{
SetUnRegisterDocument(doc);
setDocumentDefaults();
}
public gPoint StartPoint
{
get { return mStartPoint; }
set
{
//add history for the property to enable undo/redo for it,
//you can skip this if you do not want to support undo/redo for this property,
//but it is recommended to add it for any property that you want to be able to undo/redo changes on it.
AddHistory("StartPoint", value);
mStartPoint = value;
}
}
public gPoint EndPoint
{
get { return mEndPoint; }
set
{
//add history for the property to enable undo/redo for it,
//you can skip this if you do not want to support undo/redo for this property,
//but it is recommended to add it for any property that you want to be able to undo/redo changes on it.
AddHistory("EndPoint", value);
mEndPoint = value;
}
}
//write the object properties to the serializer,
//you need to write code for all custom properties that you want to be serialized in this method,
//and you need to call the base.Serialize at the beginning of the method to serialize the base class properties.
public override void Serialize(Serializer serializer)
{
base.Serialize(serializer);
serializer.Serialize(StartPoint, "StartPoint");
serializer.Serialize(EndPoint, "EndPoint");
}
//read the object properties from the deserializer,
public override bool DeSerialize(DeSerializer deserializer, string fieldname, object value)
{
if (base.DeSerialize(deserializer, fieldname, value)) return true;
else if (fieldname == "StartPoint") StartPoint = (gPoint)value;
else if (fieldname == "EndPoint") EndPoint = (gPoint)value;
else return false;
return true;
}
//copy the properties from another object of the same type to this object,
//this is used when you want to copy an object or create a new object by copying an existing one,
//you need to write code for all custom properties that you want to be copied in this method,
//and you need to call the base.MatchProperties at the beginning of the method to copy the base class properties.
public override void MatchProperties(vdPrimary _from, vdDocument thisdocument)
{
base.MatchProperties(_from, thisdocument);
MyLine from = _from as MyLine;
if (from == null) return;
StartPoint = from.StartPoint;
EndPoint = from.EndPoint;
}
//override the bounding box property to return the bounding box of the line based on the start and end points,
public override Box BoundingBox
{
get
{
if (mBoundBox.IsEmpty)
{
lock (this)
{
mBoundBox.AddPoint(StartPoint);
mBoundBox.AddPoint(EndPoint);
}
}
return mBoundBox;
}
}
//override the draw method to draw the line on the screen,
//and also used to add the line to the selection when the user select it by mouse,
//you need to call the base.Draw at the beginning of the method to draw the base class properties
// and then add your drawing code after that.
public override vdRender.DrawStatus Draw(vdRender render)
{
lock (this)
{
//use ListStart and ListEnd to support the case of drawing the line in a list,
//this will help in improving the performance by reducing the number of times the base.Draw is called and also to reduce the number of times the draw code is executed .
//Note ListStart and ListEnd works if Draw3DFlag property of base vdFigure is set to Default
if (ListStart(render)) return render.StatusDraw;
vdRender.DrawStatus doDraw = base.Draw(render);
if (doDraw == vdRender.DrawStatus.Successed)
{
render.DrawLine(this, StartPoint, EndPoint);
}
AfterDraw(render);//always call AfterDraw at the end in combination with base.Draw
if (ListEnd(render)) doDraw = render.StatusDraw;
return render.StatusDraw;
}
}
//override the Transformby method to transform the line by a given matrix,
//this is used when you want to move, rotate, scale or mirror the line by code or by grip points,
public override void Transformby(Matrix mat)
{
if (mat.IsUnitMatrix()) return;
StartPoint = mat.Transform(StartPoint);
EndPoint = mat.Transform(EndPoint);
base.Transformby(mat);
}
public override gPoints GetGripPoints()
{
//mGripPoints is a protected member in the base class vdFigure that is used to store the grip points of the figure,
//it is cleared by the Update method of the figure when the figure is modified or when the grip points are moved by the user,
if (mGipPoints != null) return mGipPoints;
mGipPoints = new gPoints();
mGipPoints.Add((gPoint)StartPoint.Clone());
mGipPoints.Add((gPoint)EndPoint.Clone());
return mGipPoints;
}
//override the MoveGripPointsAt method to move the grip points of the figure when the user move them by mouse,
public override void MoveGripPointsAt(Int32Array Indexes, double dx, double dy, double dz)
{
if (Indexes == null || Indexes.Count == 0 || (dx == 0.0 && dy == 0.0 && dz == 0.0)) return;
gPoints grips = GetGripPoints();
if (Indexes.Count == grips.Count)
{
Matrix mat = new Matrix();
mat.TranslateMatrix(dx, dy, dz);
Transformby(mat);
}
else
{
foreach (int index in Indexes)
{
switch (index)
{
case 0:
StartPoint += new gPoint(dx, dy, dz);
break;
case 1:
EndPoint += new gPoint(dx, dy, dz);
break;
default:
break;
}
}
}
Update();
}
//override the getOsnapPoints method to add osnap points to the line when the user hover over it with osnap mode on,
public override bool getOsnapPoints(Matrix object2viewcs, OsnapMode mode, gPoint pickPoi, gPoint LastPoi, int SegCount, OsnapPoints osnaps)
{
if (mode == OsnapMode.NONE) return false;
int count = osnaps.Count;
switch (mode)
{
case OsnapMode.END:
osnaps.AddItem(new OsnapPoint(StartPoint, mode, object2viewcs, ECSMatrix, OsnapPoint.PointCoordSystem.World, this, osnaps.RenderingObject as IvdRenderViewProperties));
osnaps.AddItem(new OsnapPoint(EndPoint, mode, object2viewcs, ECSMatrix, OsnapPoint.PointCoordSystem.World, this, osnaps.RenderingObject as IvdRenderViewProperties));
break;
case OsnapMode.MID:
osnaps.AddItem(new OsnapPoint(gPoint.MidPoint(StartPoint, EndPoint), mode, object2viewcs, ECSMatrix, OsnapPoint.PointCoordSystem.World, this, osnaps.RenderingObject as IvdRenderViewProperties));//6018 60001001
break;
case OsnapMode.NEA:
{
Matrix world2objectcs = ECSMatrix.GetInvertion();
Matrix viewcs2objectcs = object2viewcs.GetInvertion();
gPoint pt = Globals.LineNearestTo(world2objectcs.Transform(StartPoint), world2objectcs.Transform(EndPoint), viewcs2objectcs.Transform(pickPoi), true);
if (pt == null) break;
osnaps.AddItem(new OsnapPoint(pt, mode, object2viewcs, ECSMatrix, OsnapPoint.PointCoordSystem.ECS, this, osnaps.RenderingObject as IvdRenderViewProperties));//6018 60001001
}
break;
case OsnapMode.PER:
{
if (LastPoi == null) break;
Matrix world2objectcs = ECSMatrix.GetInvertion();
Matrix viewcs2objectcs = object2viewcs.GetInvertion();
gPoint pt = Globals.LineNearestTo(world2objectcs.Transform(StartPoint), world2objectcs.Transform(EndPoint), viewcs2objectcs.Transform(LastPoi), true);
if (pt == null) break;
osnaps.AddItem(new OsnapPoint(pt, mode, object2viewcs, ECSMatrix, OsnapPoint.PointCoordSystem.ECS, this, osnaps.RenderingObject as IvdRenderViewProperties));//6018 60001001
}
break;
default:
break;
}
return (osnaps.Count != count);
}
//override the Explode method to return the entities that represent the line when it is exploded,
public override vdEntities Explode()
{
vdEntities Entities = new vdEntities();
Entities.SetUnRegisterDocument(Document);
vdLine line = new vdLine();
line.MatchProperties(this, Document);
line.StartPoint = StartPoint;
line.EndPoint = EndPoint;
Entities.AddItem(line);
return Entities;
}
//a static method to draw the line by command, this method is called by this example
//from the command line event handler in the form when the user execute the command "test",
public static bool Commmand_MyLine(vdDocument doc)
{
doc.Prompt("Pick start point");
gPoint startpoint = doc.ActionUtility.getUserPoint() as gPoint;
doc.Prompt(null);
if (startpoint == null) return false;
doc.Prompt("Pick end point");
gPoint endpoint = doc.ActionUtility.getUserRefPoint(startpoint) as gPoint;
doc.Prompt(null);
if (endpoint == null) return false;
MyLine line = new MyLine(doc);
line.StartPoint = startpoint;
line.EndPoint = endpoint;
doc.ActiveLayOut.Entities.AddItem(line);
doc.ActionDrawFigure(line);
return true;
}
}
}