导出CAD文件的几种方法

1. 直接导入COM组件里的AutoCAD的dll。

private void Write()
        {
            a = new AcadApplicationClass();
            a.Visible = true;

            var p = a.ActiveDocument.ModelSpace;
            
            p.AddBox(new double[] { 0, 0, 0 }, 200, 200, 1);
            p.AddCircle(new double[] { 100, 100, 1 }, 100);
            var pts = VirtualPoints();


            for (int i = 0; i < pts.Count; i++)
            {
                bool hasException;
                do
                {
                    try
                    {
                        p.AddBox(new double[] { pts.ElementAt(i).X, pts.ElementAt(i).Y, 0 }, 10, 10, 1);
                        hasException = false;
                    }
                    catch (System.Runtime.InteropServices.COMException e)
                    {
                        if (e.ErrorCode == -2147418111)
                        {
                            hasException = true;
                        }
                        else
                        {
                            throw;
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                } while (hasException);
            }
            if (a.ActiveDocument.Saved == false)
            {
                a.ActiveDocument.SaveAs("F:\\Wafer_Sort\\image.dwg", AcSaveAsType.ac2018_dwg, null);

            }



        }

2. 利用netdxf的库进行开发。

public static void Main()
{
	// your DXF file name
	string file = "sample.dxf";

	// create a new document, by default it will create an AutoCad2000 DXF version
	DxfDocument doc = new DxfDocument();
	// an entity
	Line entity = new Line(new Vector2(5, 5), new Vector2(10, 5));
	// add your entities here
	doc.Entities.Add(entity);
	// save to file
	doc.Save(file);

	// this check is optional but recommended before loading a DXF file
	DxfVersion dxfVersion = DxfDocument.CheckDxfFileVersion(file);
	// netDxf is only compatible with AutoCad2000 and higher DXF versions
	if (dxfVersion < DxfVersion.AutoCad2000) return;
	// load file
	DxfDocument loaded = DxfDocument.Load(file);
}

3. 收集资料得到(未尝试)

private void ExportCad()
        {
            using (Services svcs = new Services())
            {
                Database db = new Database();
                Circle cirl = new Circle();
                cirl.Center = new Point3d(100, 100, 0);
                cirl.Radius = 14;
                BlockTableRecord btr = (BlockTableRecord)db.CurrentSpaceId.Open(OpenMode.ForWrite);

                btr.AppendEntity(cirl);
                db.SaveAs("C:\\test.dwg", DwgVersion.Current);

            }




        }

4. 利用CAD二次开发。

第一种方式比较直接,但对版本号有要求。

第二种也是笔者采用的方法,比较简单,官网也有教程,通俗易懂。

第三种看似简单,但是找不到匹配的库,未采用。

第四种与我的需求不符,但是是最常用的方法,网上资料也最多。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值