[原创]用C#体会自定义实体的强大(2010年6月21日更新)
本帖最后由 作者 于 2010-6-21 8:16:49 编辑自定义实体一直是ARX专利(现在还是),但我们还是可以在C#中体会它的强大。运行前请引用附件中的DLL,运行环境:AutoCAD 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
{
// 创建自定义实体
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);
}
// 编辑自定义实体
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, OpenMode.ForWrite);
entId = btr.AppendEntity(ent);
trans.AddNewlyCreatedDBObject(ent, true);
trans.Commit();
}
return entId;
}
}
} ahlzl 发表于 2010-5-23 20:28
这是学习SDK(\entity\SimpleSquare)的成果,以前没解决的问题是:Mgtriangle ent = (Mgtriangle)trans.Ge ...
因为获取的是数据库文档,类型不统一,所以需要强制转换类型 不错支持一下 为什么没有后续的更新了 <p>老卢,牛啊!</p><p>重定义要做的像自定义一样强大就好了</p> 好东西,下来学习一下. <p>这是一个“子实体”例子,各部分颜色不同,拖动夹点时的效果很爽。</p><p>另,感觉重定义也能搞出这效果。</p> <p>重定义确实可以实现,不过属性框里显示的还是原实体</p><p>想办法把Cad的属性框用自己的偷换掉,应该就差不多了,呵呵</p> 这是学习SDK(\entity\SimpleSquare)的成果,以前没解决的问题是:Mgtriangle ent = (Mgtriangle)trans.GetObject(res.ObjectId, OpenMode.ForWrite);转换会失败(小狼也碰到过类似的情况)。 <p>老兄,能提供自定义实体的代码吗。</p> 代码,请看SDK的例子:\samples\entity\SimpleSquare 运行出错。提示<div><div><font class="Apple-style-span" color="#FF0000">有关调用实时(JIT)调试而不是此对话框的详细信息,</font></div><div><font class="Apple-style-span" color="#FF0000">请参见此消息的结尾。</font></div><div><font class="Apple-style-span" color="#FF0000"><br/></font></div><div><font class="Apple-style-span" color="#FF0000">************** 异常文本 **************</font></div><div><font class="Apple-style-span" color="#FF0000">System.Reflection.TargetInvocationException: 调用的目标发生了异常。 ---> System.MissingFieldException: 找不到字段:“Autodesk.AutoCAD.DatabaseServices.BlockTableRecord.ModelSpace”。</font></div><div><font class="Apple-style-span" color="#FF0000"> 在 SimpleSquareUI.SquareCommands.JigSquare()</font></div><div><font class="Apple-style-span" color="#FF0000"> --- 内部异常堆栈跟踪的结尾 ---</font></div><div><font class="Apple-style-span" color="#FF0000"> 在 System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)</font></div><div><font class="Apple-style-span" color="#FF0000"> 在 System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)</font></div><div><font class="Apple-style-span" color="#FF0000"> 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)</font></div><div><font class="Apple-style-span" color="#FF0000"> 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)</font></div><div><font class="Apple-style-span" color="#FF0000"> 在 AcMgPerDocumentCommandClass.Invoke(AcMgPerDocumentCommandClass* , gcroot<System::Reflection::MethodInfo __gc \*>* mi)</font></div><div><font class="Apple-style-span" color="#FF0000"> 在 Autodesk.AutoCAD.Runtime.SEHExceptionFilter.InvokeWorker()</font></div><div><font class="Apple-style-span" color="#FF0000"> 在 Autodesk.AutoCAD.Runtime.ExceptionFilter.Invoke()</font></div><div><font class="Apple-style-span" color="#FF0000"><br/></font></div><div><font class="Apple-style-span" color="#FF0000"><br/></font></div><div><font class="Apple-style-span" color="#FF0000">************** 已加载的程序集 **************</font></div><div><font class="Apple-style-span" color="#FF0000">mscorlib</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 2.0.0.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 2.0.50727.3603 (GDR.050727-3600)</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">AcDbMgdHost</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///D:/Program%20Files/AutoCAD%202006/AcdbMgdHost.DLL</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">System</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 2.0.0.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">System.Xml</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 2.0.0.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">System.Drawing</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 2.0.0.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">System.Windows.Forms</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 2.0.0.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">acmgd</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///D:/Program%20Files/AutoCAD%202006/acmgd.DLL</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">acdbmgd</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///D:/Program%20Files/AutoCAD%202006/acdbmgd.DLL</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">AcDxUi</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///D:/Program%20Files/AutoCAD%202006/AcDxUi.DLL</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">AcDx</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///D:/Program%20Files/AutoCAD%202006/AcDx.DLL</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">AcDxUi.resources</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///D:/Program%20Files/AutoCAD%202006/zh-CN/AcDxUi.resources.DLL</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">AcMgdShared</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///D:/Program%20Files/AutoCAD%202006/AcMgdShared.DLL</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">AcLayer</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///D:/Program%20Files/AutoCAD%202006/AcLayer.DLL</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">AcLayer.resources</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///D:/Program%20Files/AutoCAD%202006/zh-CN/AcLayer.resources.DLL</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">Microsoft.VisualC</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 8.0.0.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 8.00.50727.3053</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualC/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualC.dll</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">BzLocation</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 16.2.54.0</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///D:/Program%20Files/AutoCAD%202006/BzLocation.DLL</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">PROJECTPOINTCLIENTLib</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 1.0.0.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 6.1.1158.0</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///D:/Program%20Files/AutoCAD%202006/PROJECTPOINTCLIENTLib.DLL</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">SimpleSquareUI</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 0.0.0.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 0.0.0.0</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///F:/资料/ObjectARX_2008_32Bit/samples/entity/SimpleSquare/SimpleSquareUI/bin/Debug/SimpleSquareUI.dll</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">Microsoft.VisualBasic</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 8.0.0.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 8.0.50727.3053 (netfxsp.050727-3000)</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">mscorlib.resources</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 2.0.0.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 2.0.50727.3603 (GDR.050727-3600)</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000">System.Windows.Forms.resources</font></div><div><font class="Apple-style-span" color="#FF0000"> 程序集版本: 2.0.0.0</font></div><div><font class="Apple-style-span" color="#FF0000"> Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)</font></div><div><font class="Apple-style-span" color="#FF0000"> 基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms.resources/2.0.0.0_zh-CHS_b77a5c561934e089/System.Windows.Forms.resources.dll</font></div><div><font class="Apple-style-span" color="#FF0000">----------------------------------------</font></div><div><font class="Apple-style-span" color="#FF0000"><br/></font></div><div><font class="Apple-style-span" color="#FF0000">************** JIT 调试 **************</font></div><div><font class="Apple-style-span" color="#FF0000">要启用实时(JIT)调试,</font></div><div><font class="Apple-style-span" color="#FF0000">该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置</font></div><div><font class="Apple-style-span" color="#FF0000">jitDebugging 值。</font></div><div><font class="Apple-style-span" color="#FF0000">编译应用程序时还必须启用</font></div><div><font class="Apple-style-span" color="#FF0000">调试。</font></div><div><font class="Apple-style-span" color="#FF0000"><br/></font></div><div><font class="Apple-style-span" color="#FF0000">例如: </font></div><div><font class="Apple-style-span" color="#FF0000"><br/></font></div><div><font class="Apple-style-span" color="#FF0000"><configuration></font></div><div><font class="Apple-style-span" color="#FF0000"> <system.windows.forms jitDebugging="true" /></font></div><div><font class="Apple-style-span" color="#FF0000"></configuration></font></div><div><font class="Apple-style-span" color="#FF0000"><br/></font></div><div><font class="Apple-style-span" color="#FF0000">启用 JIT 调试后,任何无法处理的异常</font></div><div><font class="Apple-style-span" color="#FF0000">都将被发送到在此计算机上注册的 JIT 调试器,</font></div><div><font class="Apple-style-span" color="#FF0000">而不是由此对话框处理。</font></div></div><div><font class="Apple-style-span" color="#FF0000"><br/></font></div><div><font class="Apple-style-span" color="#333333">我用的2008sdk中的例子。用vs2008+cad2006</font></div> <p>CAD2006,不行。</p>
<p>要VS2005 + CAD07、08、09或VS2008SP1 + CAD10、11</p>