CAD .NET是一款.NET环境下的对AutoCAD® DWG/ DXF, PLT和其他CAD文件进行处理的类库,该产品分了Import、Export、Enterprise三个版本们可以对导入CAD文件、光栅图片、创建新的绘制,存取数据结构、通过代码添加和编辑绘制数据,可视化地添加和编辑绘制数据(企业版具备),还可以对CAD文件进行打印和控制,这篇文章主要介绍怎么利用代码对CAD文件中的各种实体进行添加、删除和修改操作,具体如下:
using CADImport;
using CADImport.DWG;
using CADImport.DXF;
using CADImport.RasterImage;
private void Add_change_delete_Click(object sender, EventArgs e)
{
//CADImage.CreateImageByExtension detects format by the extension specified in the argument.
//The correct class for any supported format will be used automatically. We recommend to
//create a new drawing object with CADImage.CreateImageByExtension if import from the existed
//file/stream is required.
CADImage vDrawing = CADImage.CreateImageByExtension(@"..\..\..\Files\Entities.dxf");
vDrawing.LoadFromFile(@"..\..\..\Files\Entities.dxf");
// Changes color
vDrawing.CurrentLayout.Entities[1].Color = Color.Blue;
vDrawing.CurrentLayout.Entities[1].LineWeight = 2;
vDrawing.Converter.Loads(vDrawing.CurrentLayout.Entities[1]);
//Removes the circle entity from the Entities.dxf drawing
vDrawing.CurrentLayout.Entities.RemoveAt(2);
// Creating a new entity - line
CADLine vLine = new CADLine();
vLine.Point = new DPoint(50, 0, 0);
vLine.Point1 = new DPoint(50, 70, 10);
vLine.LineWeight = 1;
vDrawing.Converter.Loads(vLine);
vDrawing.CurrentLayout.AddEntity(vLine);
// Recalculates the extents of the drawing
vDrawing.GetExtents();
// Adjusting visualization sizes to the control area:
RectangleF vRect;
double vRatio = (double)(vDrawing.AbsHeight * Image1.ClientSize.Width) / (vDrawing.AbsWidth *
Image1.ClientSize.Height);
if (vRatio > 1)
vRect = new RectangleF(0, 0, (float)(Image1.ClientSize.Width / vRatio), (float)Image1.ClientSize.Height);
else
vRect = new RectangleF(0, 0, (float)Image1.ClientSize.Width, (float)(Image1.ClientSize.Height * vRatio));
//-----------------------------------------------
vDrawing.Draw(Image1.CreateGraphics(), vRect);
}