How to programatically draw a line and rectangle

Discuss and ask questions about CAD .NET library.

Moderators: SDS, support, admin

Post Reply
odenlou
Posts: 11
Joined: 16 Jul 2014, 23:57

How to programatically draw a line and rectangle

Post by odenlou » 17 Jul 2014, 14:44

How to programmatically draw a line from one point to another?

How to programmatically draw a rectangle whose center is at a particular point?

support
Posts: 3272
Joined: 30 Mar 2005, 11:36
Contact:

Re: How to programatically draw a line and rectangle

Post by support » 17 Jul 2014, 16:13

Hello,

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();
        }
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:

Code: Select all

cadImage.Draw(pictureBox1.CreateGraphics(), new RectangleF(0, 0, pictureBox1.Width, pictureBox1.Height));
The following example shows how to add a rectangle with a specified center point, width and 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();
        }
As you can see, a rectangle is drawn as POLYLINE entity which is closed and has four vertexes.

Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

Post Reply