How to programatically draw a line and rectangle
Moderators: SDS, support, admin
How to programatically draw a line and rectangle
How to programmatically draw a line from one point to another?
How to programmatically draw a rectangle whose center is at a particular point?
How to programmatically draw a rectangle whose center is at a particular point?
Re: How to programatically draw a line and rectangle
Hello,
The code example below shows how to add a line programmatically:
Each new entity should be loaded into CADConverter and added to some layout (usually, to the current layout). After that, you need to redraw an image:
The following example shows how to add a rectangle with a specified center point, width and height:
As you can see, a rectangle is drawn as POLYLINE entity which is closed and has four vertexes.
Mikhail
The code example below shows how to add a line programmatically:
Code: Select all
private void AddLine(CADImage cadImage, DPoint point1, DPoint point2)
{
if (cadImage == null)
{
return;
}
else
{
if (cadImage.CurrentLayout == null)
return;
}
CADLine line = new CADLine();
line.Color = Color.Black;
line.Point = point1;
line.Point1 = point2;
line.Loaded(cadImage.Converter);
cadImage.Converter.OnCreate(line);
cadImage.CurrentLayout.AddEntity(line);
cadImage.GetExtents();
}
Code: Select all
cadImage.Draw(pictureBox1.CreateGraphics(), new RectangleF(0, 0, pictureBox1.Width, pictureBox1.Height));
Code: Select all
private void AddRectangle(CADImage cadImage, DPoint center, double width, double height)
{
if (cadImage == null)
{
return;
}
else
{
if (cadImage.CurrentLayout == null)
return;
}
CADVertex vertex1, vertex2, vertex3, vertex4;
vertex1 = new CADVertex();
vertex2 = new CADVertex();
vertex3 = new CADVertex();
vertex4 = new CADVertex();
double xLeft = center.X - width / 2;
double xRight = center.X + width / 2;
double yTop = center.Y + height / 2;
double yBottom = center.Y - height / 2;
vertex1.Point = new DPoint(xLeft, yBottom, 0);
vertex2.Point = new DPoint(xLeft, yTop, 0);
vertex3.Point = new DPoint(xRight, yTop, 0);
vertex4.Point = new DPoint(xRight, yBottom, 0);
CADPolyLine polyline = new CADPolyLine();
polyline.Color = Color.Blue;
polyline.Entities.Add(vertex1);
polyline.Entities.Add(vertex2);
polyline.Entities.Add(vertex3);
polyline.Entities.Add(vertex4);
polyline.Closed = true;
polyline.Loaded(cadImage.Converter);
cadImage.Converter.OnCreate(polyline);
cadImage.CurrentLayout.AddEntity(polyline);
cadImage.GetExtents();
}
Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support
Chat support on Skype: cadsofttools.support