- UID
- 14
- 积分
- 8264
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-1-4
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
C# CAD二次开发入门(VS2005+CAD2008)
原文地址
C#做二次开发显然比C++简单得多。希望公司将来能由C++转向C#
步骤一:
新建C#类库项目HelloArx
找到CAD2008的安装目录,添加acdbmgd.dll和acmgd.dll的引用
修改Class1.cs如下:
 - using System;
- using System.Collections.Generic;
- using System.Text;
- using Autodesk.AutoCAD;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Colors;
- using DBTransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager;
- namespace HelloArx
- {
- public class Class1
- {
- //加载实体到数据库
- public static ObjectId AppendEntity(Entity ent)
- {
- Database db = HostApplicationServices.WorkingDatabase;
- ObjectId entId;
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- entId = btr.AppendEntity(ent);
- trans.AddNewlyCreatedDBObject(ent, true);
- trans.Commit();
- }
- return entId;
- }
- //由两点创建直线
- public static ObjectId AddLine(Point3d startPt, Point3d endPt)
- {
- Line ent = new Line(startPt, endPt);
- ObjectId entId = AppendEntity(ent);
- return entId;
- }
- }
- }
步骤三:
新建一个类NewCmd用来建立Cad命令 -
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.Windows;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Colors;
- using Autodesk.AutoCAD.EditorInput;
- using System.Collections;
- using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
- [assembly: CommandClass(typeof(HelloArx.NewCmd))]
- namespace HelloArx
- {
- class NewCmd
- {
- //新建一个命令
- [CommandMethod("test")]
- public void Test()
- {
- Point3d ptSt = new Point3d(0, 0, 0);
- Point3d ptEnd = new Point3d(10, 20, 54);
- Class1.AddLine(ptSt, ptEnd);
- }
- }
- }
步骤四: 右击项目->属性->调试->启动外部程序输入cad的路径
比如F:\Program Files\AutoCAD 2008\acad.exe
Shift+Ctrl+B编译之后。生成HelloArx.dll
打开Cad2008输入命令netload找到HelloArx.dll然后输入命令test
可以看到效果。
如果看不到,就输入zoom e查看
|
|