自定义实体一直是ARX专利(现在还是),但我们还是可以在C#中体会它的强大。运行前请引用附件中的DLL,运行环境: 2008。
2010年6月21日更新:添加了一个新的DLL,可用于CAD2010及以上版本。
- using System;
- using Autodesk.ahlzl;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Runtime;
- namespace CS_调用
- {
- public class Class1
- {
- [CommandMethod("T1")] // 创建自定义实体
- public void Test1()
- {
- Mgtriangle ent = new Mgtriangle();
- ent.Direction = Vector3d.XAxis;
- ent.CrankAngle = Math.PI / 6;
- ent.CrankCenPt = new Point3d(100.0, 0.0, 0.0);
- ent.CrankRadius = 10.0;
- ent.LinkLength = 30.0;
- ent.PistonSize = 5.0;
- AppendEntity(ent);
- }
- [CommandMethod("T2")] // 编辑自定义实体
- public void Test2()
- {
- Database db = HostApplicationServices.WorkingDatabase;
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- PromptEntityOptions opt = new PromptEntityOptions("\n请选择Mgtriangle自定义实体");
- opt.SetRejectMessage("您选择的不是Mgtriangle自定义实体,请重新选择!");
- opt.AddAllowedClass(typeof(Mgtriangle), true);
- PromptEntityResult res = ed.GetEntity(opt);
- if (res.Status != PromptStatus.OK)
- {
- return;
- }
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- Mgtriangle ent = (Mgtriangle)trans.GetObject(res.ObjectId, OpenMode.ForWrite);
- ed.WriteMessage("\n您选择的对象是:" + ent.GetType().Name + ", 请您添加代码修改自定义实体!");
- }
- }
- private ObjectId AppendEntity(Entity ent)
- {
- ObjectId entId;
- Database db = HostApplicationServices.WorkingDatabase;
- 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;
- }
- }
- }
hrcz3tdahvn.JPG
本帖以下内容被隐藏保护;需要你回复后,才能看到! 游客,如果您要查看本帖隐藏内容请 回复 |