How to load a file with CAD .NET?
The CADImage
class defines the drawing object. All available drawing data can be received via properties of this class.
The library includes the CADImage
derived classes that must be used only to import drawings of the corresponding format:
Classes | File format |
---|---|
DWGImage | DWG |
CGMImage | CGM |
HPGLImage | HPGL/2 |
GBRImage | Extended Gerber (RS-274X) |
CADRasterImage | Raster images and metafiles |
CADImage | DXF |
Let's open a CAD file 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 LoadCADFile_Click(object sender, EventArgs e){
- Use the
ShowDialog
method to load a CAD file from the dialog window. Create a new instance of theCADImage
class.
{
if ((openFileDialog1.ShowDialog() != DialogResult.OK)) return;
CADImage vDrawing = CADImage.CreateImageByExtension(openFileDialog1.FileName);
vDrawing.LoadFromFile(openFileDialog1.FileName);
We recommend creating a new drawing object using CreateImageByExtension
in case of importing from an existing file or stream.
- 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.
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);
You have created the function to load CAD files.
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)
};
public Form1()
{
Controls.Add(pictureBox1);
InitializeComponent();
}
private void LoadCADFile_Click(object sender, EventArgs e)
{
if ((openFileDialog1.ShowDialog() != DialogResult.OK)) return;
CADImage vDrawing = CADImage.CreateImageByExtension(openFileDialog1.FileName);
vDrawing.LoadFromFile(openFileDialog1.FileName);
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);
}
}
}