How to create a CAD drawing?
Let's create a new drawing from scratch with CAD .NET.
- Add the
using
directive with theCADImport
andCADImport.FaceModule
namespaces.
using CADImport;
using CADImport.FaceModule;
More information about CADPictureBox
The CADPictureBox
class is the basic implementation of the control element for displaying vector drawings. Visually CADPictureBox
includes only area for drawing visualization and can be extended by the required control elements in the project under development.
To get more information about the CAD .NET controls, see What controls does CAD .NET have?
- Use the control element of the
CADPictureBox
class:- Set the
Location
property asnew Point(10, 30)
. - Set the
BackColor
property asColor.Black
. - Set the
Size
property asnew Size(995, 500)
. - Finally, add it to the form.
- Set the
...
CADPictureBox pictureBox1 = new CADPictureBox(){
Location = new Point(10, 30),
BackColor = Color.Black,
Size = new Size(995, 500),
};
public Form1()
{
Controls.Add(pictureBox1);
InitializeComponent();
}
- Add a new button. Name it
CreateCADDrawing
. Then create theCreateCADDrawing_Click
function.
private void CreateCADDrawing(object sender, EventArgs e){
- Create a new instance of the
CADImage
class. CallInitializeSection
to create the CAD object manually:- Set the
Background
property asColor.Azure
. - Set the
CurrentLayout
property asvDrawing.Layouts[0]
.
- Set the
CADImage vDrawing = new CADImage();
vDrawing.Converter.InitializeSections();
vDrawing.BackgroundColor = Color.Azure;
vDrawing.CurrentLayout = vDrawing.Layouts[0];
- Create a new instance of the
CADLine
class:- Add
vLine
to the current layout ofvDrawing
using theAddEntity
method. - Set the
Point
property asnew DPoint(45, 200, 0)
. - Set the
Point1
property asnew DPoint(450,200, 0)
. - Set the
LineWeight
property as1
.
- Add
CADLine vLine = new CADLine();
vDrawing.CurrentLayout.AddEntity(vLine);
vLine.Point = new DPoint(45, 200, 0);
vLine.Point1 = new DPoint(450, 200, 0);
vLine.LineWeight = 1;
- Use the
Loads
method to fill the internal data of the entity to prepare it for drawing.
vDrawing.Converter.Loads(vLine);
- Create a new instance of the
CADCircle
class:- Add
vCircle
to the current layout ofvDrawing
using theAddEntity
method. - Set the
vPoint
property asnew DPoint(250, 200, 0)
. - Set the
Radius
property as100
. - Set the
Color
property asColor.Red
.
- Add
CADCircle vCircle = new CADCircle();
vDrawing.CurrentLayout.AddEntity(vCircle);
vCircle.Point = new DPoint(250, 200, 0);
vCircle.Radius = 100;
vCircle.Color = Color.Red;
- Use the
Loads
method to fill the internal data of the entity to prepare it for drawing.
vDrawing.Converter.Loads(vCircle);
- Use the
GetExtents
method to recalculate drawing extents.
vDrawing.GetExtents();
- Declare the local variable
vRect
and specifyRectangleF
as its type. This variable stores four floating values that represent the location and size of a CAD file. Use the following code to fit the CAD file topictureBox1
. Finally, render the result with theDraw
method.
vRect = new RectangleF(0, 0, (float)pictureBox1.ClientSize.Width, (float)(pictureBox1.ClientSize.Height));
vDrawing.Draw(pictureBox1.CreateGraphics(), vRect);
The following picture illustrates the result.
You have created a function to create a CAD image.
The full code listing.
...
using CADImport;
using CADImport.FaceModule;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
CADPictureBox pictureBox1 = new CADPictureBox()
{
Location = new Point(10, 30),
TabIndex = 10,
BackColor = Color.Black,
Size = new Size(995, 500)
};
{
Controls.Add(pictureBox1);
InitializeComponent();
}
private void CreateCADDrawing(object sender, EventArgs e)
{
CADImage vDrawing = new CADImage();
vDrawing.Converter.InitializeSections();
vDrawing.BackgroundColor = Color.Azure;
vDrawing.CurrentLayout = vDrawing.Layouts[0];
// Adding a line
CADLine vLine = new CADLine();
vDrawing.CurrentLayout.AddEntity(vLine);
vLine.Point = new DPoint(45, 200, 0);
vLine.Point1 = new DPoint(450, 200, 0);
vLine.LineWeight = 1;
vDrawing.Converter.Loads(vLine);
// Adding a circle
CADCircle vCircle = new CADCircle();
vDrawing.CurrentLayout.AddEntity(vCircle);
vCircle.Point = new DPoint(250, 200, 0);
vCircle.Radius = 100;
vCircle.Color = Color.Red;
vDrawing.Converter.Loads(vCircle);
vDrawing.GetExtents();
RectangleF vRect;
double vRatio = (double)(vDrawing.AbsHeight * pictureBox1.ClientSize.Width) / (vDrawing.AbsWidth * pictureBox1.ClientSize.Height);
if (vRatio > 1)
vRect = new RectangleF(0, 0, (float)(pictureBox1.ClientSize.Width / vRatio), (float)pictureBox1.ClientSize.Height);
else
vRect = new RectangleF(0, 0, (float)pictureBox1.ClientSize.Width, (float)(pictureBox1.ClientSize.Height * vRatio));
vDrawing.Draw(pictureBox1.CreateGraphics(), vRect);
}
}
}