今天我们来了解一下怎么用VB.NET来对CAD进行开发!
环境搭建
版本兼容性:
首先,先来了解一下objectarx或者.net DLL 与AutoCAD版本兼容问题!
AutoCAD 版本 | 受支持的.NET 和 ObjectARX SDK | .NET Framework |
---|---|---|
AutoCAD 2020 | AutoCAD 2020、AutoCAD 2019 | 4.7 |
AutoCAD 2019 | AutoCAD 2019 | 4.7 |
AutoCAD 2018 | AutoCAD 2018 | 4.6 |
AutoCAD 2017 | AutoCAD 2017 | 4.6 |
AutoCAD 2016 | AutoCAD 2015, AutoCAD 2016 | 4.5 |
AutoCAD 2015 | AutoCAD 2015 | 4.5 |
AutoCAD 2014 | AutoCAD 2013, AutoCAD 2014 | 4.0 |
AutoCAD 2013 | AutoCAD 2013 | 4.0 |
AutoCAD 2012 | AutoCAD2010,AutoCAD2011,AutoCAD 2012 | 3.51 SP1 |
AutoCAD 2011 | AutoCAD 2010, 2011 | 3.51 SP1 |
AutoCAD 2010 | AutoCAD 2010 | 3.51 SP1 |
AutoCAD 2009 | AutoCAD2007,AutoCAD2008,AutoCAD 2009 | 3.0 |
AutoCAD 2008 | AutoCAD 2007, AutoCAD 2008 | 2.0 |
AutoCAD 2007 | AutoCAD 2007 | 2.0 |
AutoCAD 2006 | AutoCAD2004,AutoCAD2005,AutoCAD 2006 | 1.1 SP1 |
AutoCAD 2005 | AutoCAD 2004, AutoCAD 2005 | 1.1 |
AutoCAD 2004 | AutoCAD 2004 | 无 |
1.同一个.net Framework框架版本autoCAD高版本兼容低版本。不同的.Net框架往往涉及到函数或者参数的改变,一般不兼容。
2.版本兼容和系统(32位和64位)有关,如用了com的两个DLL引用, Autodesk.AutoCAD.Interop和 Autodesk.AutoCAD.Interop.Common。或者用了相交函数 IntersectWith()等情况
3.从基于 AutoCAD 2014的产品开始,当SECURELOAD系统变量设置为1或2时,自定义应用程序必须在安全模式下工作。当在安全模式下进行操作时,基于AutoCAD的产品限制为从受信任的位置加载和执行包含代码的文件;受信任的位置由TRUSTEDPATHS系统变量指定。
环境及dll引用:
1.创建一个类库项目(VS版本:VS2019,CAD版本:CAD2019),选择"类库(.NET Framework)"做开发项目!
2.引用CAD .NET 开发DLL;引用-->添加引用-->浏览-->打开CAD安装目录-->找到"acmgd.dll" "accoremgd.dll" "acdbmgd.dll" 这三个文件-->添加到引用-->一定要把三个dll文件"复制到本地"属性改为false
3.调试环境配置
3.1.打开项目属性
3.2.打开项目属性-->应用程序设置-->把.net框架调为4.7(根据上表选择对应框架,这里用的是CAD2019,所以选择.net4.7)
3.3.打开项目属性-->编译设置-->目标CPU调为X64
3.4.打开项目属性-->调试设置-->启动操作-->选择启动外部程序-->浏览-->打开CAD安装目录-->选择acad.exe文件
3.5.OK到目前为止,环境已经搭建完毕!
4.下面展示一下小项目代码及运行调试
4.1运行
4.2.加载开发项目CAD命令:netload-->选择开发生成的DLL文件-->输入我们设定的CAD命令:VBXYM-->生成图形VB
4.3.示例代码
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime
Public Class Class1
<CommandMethod("VBXYM")> Public Sub Test()
Dim V As New Polyline, B As New Polyline '’画多段线
V.AddVertexAt(0, New Point2d(0, 2), 0, 0, 0)
V.AddVertexAt(1, New Point2d(0.42, 0), 0, 0, 0)
V.AddVertexAt(2, New Point2d(0.84, 2), 0, 0, 0)
''------------------------------------------------
B.AddVertexAt(0, New Point2d(1.51, 2), 0, 0, 0)
B.AddVertexAt(1, New Point2d(1.51, 0), 0, 0, 0)
B.AddVertexAt(2, New Point2d(2.08, 0), 0, 0, 0)
B.AddVertexAt(3, New Point2d(2.25, 0.17), 0, 0, 0)
B.AddVertexAt(4, New Point2d(2.34, 0.35), 0, 0, 0)
B.AddVertexAt(5, New Point2d(2.34, 0.68), 0, 0, 0)
B.AddVertexAt(6, New Point2d(2.09, 1), 0, 0, 0)
B.AddVertexAt(7, New Point2d(1.51, 1), 0, 0, 0)
B.AddVertexAt(8, New Point2d(2.09, 1), 0, 0, 0)
B.AddVertexAt(9, New Point2d(2.25, 1.17), 0, 0, 0)
B.AddVertexAt(10, New Point2d(2.34, 1.35), 0, 0, 0)
B.AddVertexAt(11, New Point2d(2.34, 1.68), 0, 0, 0)
B.AddVertexAt(12, New Point2d(2.25, 1.84), 0, 0, 0)
B.AddVertexAt(13, New Point2d(2.09, 2), 0, 0, 0)
B.AddVertexAt(14, New Point2d(1.51, 2), 0, 0, 0)
''-----------------------------------------------
Dim entIdV As ObjectId = AppendEntity(V)
Dim entIdB As ObjectId = AppendEntity(B)
Dim Doc As Document = Core.Application.DocumentManager.MdiActiveDocument
Doc.SendStringToExecute("_.ZOOM _E ", True, False, False)
End Sub
''' <summary>
''' 把图行形添加到CAD空间
''' </summary>
''' <param name="ent"></param>
''' <returns></returns>
Public Function AppendEntity(ByVal ent As Entity) As ObjectId
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim entId As ObjectId
Using trans As Transaction = db.TransactionManager.StartTransaction
Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim btr As BlockTableRecord = trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
entId = btr.AppendEntity(ent)
trans.AddNewlyCreatedDBObject(ent, True)
trans.Commit()
End Using
Return entId
End Function
End Class
今天教程到此结束啦!谢谢阅读!