找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1228|回复: 0

[分享] Iterating AutoCAD system variables using .NET – Part 1

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-7-30 21:53:56 来自手机 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 csharp 于 2014-7-31 07:46 编辑

http://through-the-interface.typepad.com/through_the_interface/2014/06/iterating-autocad-system-variables-using-net-part-1.html

On my flight across to Toronto, I took another look at the “What’s New in AutoCAD 2015” section of the .NET API documentation and decided to dig into the new SystemVariableEnumerator implementation.
I was particularly curious about this feature as there is already a way to get access to system variables via the Autodesk.AutoCAD.Runtime.SystemObjects.Variables collection. As an initial look at these two classes, I decided to put together a couple of simple variable iterator commands that use the same function to print out some information concerning the system variables exposed by each collection.
Here’s the C# code implementing two commands, ESV and ESV2, which use the two mechanisms for getting the list of AutoCAD system variables:


We won’t look at the output to the command-line in this post, as there’s a lot there. Hopefully the format of the data printed to the command-line is obvious from the code.
A big chunk of this code actually “decodes” the type information, returning a System.Type object when it can make sense of the number passed in (which may be a 50xx number or a shorter enumeration, depending on which mechanism has been used).
It’s also worth noting that it wasn’t quite as simple as doing a “foreach” on the new API: it seems there’s a GetEnumerator() method missing which means we can’t simplify our code in that way, unfortunately (hence the long-hand approach using MoveNext() etc.). Hopefully someone will tell me I’ve made a mistake somewhere in my code and that it is, in fact, possible. :-)
The newer enumerator skips over “hidden” system variables – those with an asterisk (“*”) prefix on their name – which do get returned as part of the SystemObjects collection.
The SystemObjects collection does allow you to set the value of a variable, which is pretty interesting. In a future post we’ll take a look at this capability as well as comparing the performance of the two mechanisms, to see which is most efficient

  1. using Autodesk.AutoCAD.ApplicationServices;

  2. using Autodesk.AutoCAD.ApplicationServices.Core;

  3. using Autodesk.AutoCAD.EditorInput;

  4. using Autodesk.AutoCAD.Geometry;

  5. using Autodesk.AutoCAD.Runtime;

  6. using System;



  7. namespace SystemVariableEnumeration

  8. {

  9.   public class Commands

  10.   {

  11.     [CommandMethod("ESV")]

  12.     public static void EnumerateSysVars()

  13.     {

  14.       var doc = Application.DocumentManager.MdiActiveDocument;

  15.       if (doc == null)

  16.         return;

  17.       var ed = doc.Editor;



  18.       // Use the existing SystemObjects iteration mechanism



  19.       foreach (var v in SystemObjects.Variables)

  20.       {

  21.         PrintVariable(ed, v);

  22.       }

  23.     }



  24.     [CommandMethod("ESV2")]

  25.     public static void EnumerateSysVars2()

  26.     {

  27.       var doc = Application.DocumentManager.MdiActiveDocument;

  28.       if (doc == null)

  29.         return;

  30.       var ed = doc.Editor;



  31.       // Use the new system variable enumerator



  32.       var sve = new SystemVariableEnumerator();



  33.       while (sve.MoveNext())

  34.       {

  35.         var v = sve.Current;

  36.         if (v != null)

  37.         {

  38.           PrintVariable(ed, v);

  39.         }

  40.       }

  41.     }



  42.     // Helper function to print out the information about

  43.     // a particular variable



  44.     private static void PrintVariable(Editor ed, Variable v)

  45.     {

  46.       var t = GetType(v.PrimaryType);



  47.       ed.WriteMessage(

  48.         "\n{0} ({1}, {2} - {3}): {4}",

  49.         v.Name,

  50.         t == null ? "null" : t.Name,

  51.         v.PrimaryType, v.SecondaryType, v.TypeFlags

  52.       );



  53.       if (v.Range != null)

  54.       {

  55.         ed.WriteMessage(

  56.           " [{0}...{1}]",

  57.           v.Range.LowerBound, v.Range.UpperBound

  58.         );

  59.       }

  60.     }



  61.     // Determine the type of a system variable based on

  62.     // the internal representation



  63.     private static System.Type GetType(short v)

  64.     {

  65.       Type ret = null;



  66.       switch (v)

  67.       {

  68.         case 1:

  69.         case 5001: // RTREAL real number

  70.           {

  71.             ret = typeof(Double);

  72.             break;

  73.           }

  74.         case 2:

  75.         case 5002: // RTPOINT: 2D point X and Y only

  76.           {

  77.             ret = typeof(Point2d);

  78.             break;

  79.           }

  80.         case 3:

  81.         case 5003: // RTSHORT: short integer

  82.           {

  83.             ret = typeof(Int16);

  84.             break;

  85.           }

  86.         case 4:

  87.         case 5004: // RTANG: angle

  88.           {

  89.             ret = null; // Angle

  90.             break;

  91.           }

  92.         case 5:

  93.         case 5005: // RTSTR: string

  94.           {

  95.             ret = typeof(String);

  96.             break;

  97.           }

  98.         case 6:

  99.         case 5006: // RTENAME: entity name

  100.           {

  101.             ret = null;

  102.             break;

  103.           }

  104.         case 7:

  105.         case 5007: // RTPICKS: pick set

  106.           {

  107.             ret = null;

  108.             break;

  109.           }

  110.         case 8:

  111.         case 5008: // RTORIENT: orientation

  112.           {

  113.             ret = null; // Orientation

  114.             break;

  115.           }

  116.         case 9:

  117.         case 5009: // RT3DPOINT: 3D point - X, Y and Z

  118.           {

  119.             ret = typeof(Point3d);

  120.             break;

  121.           }

  122.         case 10:

  123.         case 5010: // RTLONG: long integer

  124.           {

  125.             ret = typeof(Int32);

  126.             break;

  127.           }

  128.         case 11:

  129.         case 5011: // 2D extents of some kind

  130.           {

  131.             ret = typeof(Point2d);

  132.             break;

  133.           }

  134.       }

  135.       return ret;

  136.     }

  137.   }

  138. }
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-12-19 01:39 , Processed in 0.370116 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表