[AutoCAD2008新功能介绍4]利用.NET使AutoCAD对象可缩放

    技术2022-05-11  27

    欢迎转载,但请注明出处,谢谢! 原文: http://through-the-interface.typepad.com 翻译:才鸟( http://www.objectarx.net) 在上一篇帖子中,我们看到了如何为AutoCAD图形添加一个新的缩放比例。 在这一篇帖子中,我们来看一下如何使一个对象可缩放——当然前提条件是它必须是一个支持缩放比例的类型。重复一下,这一篇帖子也是基于AutoCAD 2008中引入的功能。 对于本贴的问题,我想了很久,尽我的最大努力来把AnnotationScaling这个ObjectARX示例(它使用接口扩展来访问存储在缩放实体的扩展字典中的对象)转换成.NET。我最后询问了我们的团队才解决了这个问题:Ravi Pothineni提供了一些代码 ,他使用了一个内部的组件(但它是随AutoCAD发布的)。显然这会成为下一个版本AutoCAD的标准功能——你可以直接从DBObject访问到它而不是使用一个单独的"辅助类"——但你必须使用一些如下所示的稍微复杂的代码,并添加一个AcMgdInternal.dll引用来让它运行。从它的名字中你可以看出来了,这个功能没有被正式支持,使用它你得冒一些风险。 下面是C#代码——"ADS"命令基本上是上一篇中提到的(原名为"AS"),"ATS"函数是新的命令,它使一个对象可缩放并把缩放比例附加到对象上: using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.Internal; namespace AnnotationScaling {   public class Commands   {     [CommandMethod("ADS")]     static public void addScale()     {       Document doc =         Application.DocumentManager.MdiActiveDocument;       Database db = doc.Database;       Editor ed = doc.Editor;       try       {         ObjectContextManager ocm =           db.ObjectContextManager;         if (ocm != null)         {           // 现在获取缩放比例内容集合           // (名字为ACDB_ANNOTATIONSCALES_COLLECTION)           ObjectContextCollection occ =             ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");           if (occ != null)           {             // 创建一个新的比例内容             AnnotationScale asc = new AnnotationScale();             asc.Name = "MyScale 1:28";             asc.PaperUnits = 1;             asc.DrawingUnits = 28;             // 把它加入到图形的比例内容集合             occ.AddContext(asc);           }         }       }       catch (System.Exception ex)       {         ed.WriteMessage(ex.ToString());       }     }     [CommandMethod("ATS")]     static public void attachScale()     {       Document doc =         Application.DocumentManager.MdiActiveDocument;       Database db = doc.Database;       Editor ed = doc.Editor;       ObjectContextManager ocm =         db.ObjectContextManager;       ObjectContextCollection occ =         ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");       Transaction tr =         doc.TransactionManager.StartTransaction();       using (tr)       {         PromptEntityOptions opts =           new PromptEntityOptions("/nSelect entity: ");         opts.SetRejectMessage(           "/nEntity must support annotation scaling."         );         opts.AddAllowedClass(typeof(DBText), false);         opts.AddAllowedClass(typeof(MText), false);         opts.AddAllowedClass(typeof(Dimension), false);         opts.AddAllowedClass(typeof(Leader), false);         opts.AddAllowedClass(typeof(Hatch), false);         PromptEntityResult per = ed.GetEntity(opts);         if (per.ObjectId != ObjectId.Null)         {           DBObject obj =             tr.GetObject(per.ObjectId, OpenMode.ForRead);           if (obj != null)           {             obj.UpgradeOpen();             obj.Annotative = AnnotativeStates.True;             ObjectContexts.AddContext(obj, occ.GetContext("1:1"));             ObjectContexts.AddContext(obj, occ.GetContext("1:2"));             ObjectContexts.AddContext(obj, occ.GetContext("1:10"));             ObjectContext oc = occ.GetContext("MyScale 1:28");             if (oc != null)             {               ObjectContexts.AddContext(obj, oc);             }           }         }         tr.Commit();       }     }   } } 你会注意到如果你在ATS命令前运行ADS命令,你也会在所选择的对象列表中得到新加入的缩放比例: attached_annotation_scale.png (30.47 KB) 2007-5-10 09:01 AM  

    最新回复(0)