找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1592|回复: 9

[原创] 练习 Lisp定义Net版GetpropertyValue

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-5-9 07:25:40 | 显示全部楼层 |阅读模式

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

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

×
仅仅能用而已,LispFunction可以接受 Point3d,但是转换为 Resultbuffer 输出时就多了一个括号,在Lisp中要 car
  1.         [LispFunction("mGetproperty")]
  2.         public static ResultBuffer mGetproperty(ResultBuffer rb)
  3.         {
  4.             TypedValue[] values = rb.AsArray();
  5.             if (rb != null)
  6.             {
  7.                 Document document = Application.DocumentManager.MdiActiveDocument;
  8.                 Editor ed = document.Editor;
  9.                 String proName = (String)values[1].Value;
  10.                 proName = proName.ToUpper();
  11.                 Transaction transaction = document.TransactionManager.StartTransaction();
  12.                 if (values.Count() == 2 && values[0].TypeCode == (int) LispDataType.ObjectId &&
  13.                     values[1].TypeCode == (int) LispDataType.Text)
  14.                 {
  15.                     using (transaction)
  16.                     {
  17.                         ObjectId id = (ObjectId) values[0].Value;
  18.                         Entity obj = (Entity) id.GetObject(OpenMode.ForRead);
  19.                         PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
  20.                         foreach (PropertyDescriptor prop in props)
  21.                         {
  22.                             String entPropetyName = (String) prop.Name;
  23.                             entPropetyName = entPropetyName.ToUpper();
  24.                             Object valueObj = null;
  25.                             string valueString = string.Empty;
  26.                             if (entPropetyName == proName)
  27.                             {
  28.                                 try
  29.                                 {
  30.                                     valueObj = prop.GetValue(obj);
  31.                                     if (valueObj != null)
  32.                                     {
  33.                                         var proType = prop.PropertyType;
  34.                                         if (valueObj is Point3d)
  35.                                         {
  36.                                             Point3d pt = new Point3d();
  37.                                             pt = (Point3d) valueObj;
  38.                                             TypedValue [] newValues = new TypedValue[1];
  39.                                             newValues [0] = new TypedValue((int)LispDataType .Point3d ,pt);
  40.                                             ResultBuffer ret = new ResultBuffer(newValues);
  41.                                             return ret;
  42.                                         }
  43.                                         else if (valueObj is Vector3d)
  44.                                         {
  45.                                             Vector3d vec = (Vector3d) valueObj;
  46.                                             Point3d pt = new Point3d(vec.X ,vec.Y,vec.Z );
  47.                                             TypedValue[] newValues = new TypedValue[1];
  48.                                             newValues[0] = new TypedValue((int)LispDataType.Point3d, pt);
  49.                                             ResultBuffer ret = new ResultBuffer(newValues);
  50.                                             return ret;

  51.                                         }
  52.                                         else if (valueObj is Matrix3d )
  53.                                         {
  54.                                             Matrix3d mat = (Matrix3d) valueObj;
  55.                                             return Tools.Matrix3dToesultBuffer(mat);
  56.                                         }
  57.                                         else if (valueObj is Boolean )
  58.                                         {
  59.                                             Boolean t = (bool) valueObj;
  60.                                             TypedValue[] newValues = new TypedValue[1];
  61.                                             newValues[0] = new TypedValue((int)LispDataType.T_atom , t);
  62.                                             ResultBuffer ret = new ResultBuffer(newValues);
  63.                                             return ret;
  64.                                         }
  65.                                         else if (valueObj is Double)
  66.                                         {
  67.                                             Double d = (Double) valueObj;
  68.                                             TypedValue[] newValues = new TypedValue[1];
  69.                                             newValues[0] = new TypedValue((int)LispDataType.Double, d);
  70.                                             ResultBuffer ret = new ResultBuffer(newValues);
  71.                                             return ret;
  72.                                         }
  73.                                         else if(valueObj is String )
  74.                                         {
  75.                                             String str = (String) valueObj;
  76.                                             TypedValue[] newValues = new TypedValue[1];
  77.                                             newValues[0] = new TypedValue((int)LispDataType.Text, str);
  78.                                             ResultBuffer ret = new ResultBuffer(newValues);
  79.                                             return ret;
  80.                                         }
  81.                                         else if (valueObj is Extents3d)
  82.                                         {
  83.                                             Extents3d pts = (Extents3d) valueObj;
  84.                                             Point3d p0 = pts.MinPoint ;
  85.                                             Point3d pp = pts.MinPoint ;
  86.                                             TypedValue[] newValues = new TypedValue[2];
  87.                                             newValues[0] = new TypedValue((int)LispDataType.Point3d, p0);
  88.                                             newValues[1] = new TypedValue((int)LispDataType.Point3d, pp);
  89.                                             ResultBuffer ret = new ResultBuffer(newValues);
  90.                                             return ret;
  91.                                         }
  92.                                         else
  93.                                         {
  94.                                             return  null;
  95.                                         }
  96.                                     }
  97.                                     else
  98.                                     {
  99.                                         continue;
  100.                                     }
  101.                                     transaction.Commit();
  102.                                     return null;
  103.                                 }// end try
  104.                                 catch (System.Exception ex)
  105.                                 {
  106.                                     if (ex.InnerException is Autodesk.AutoCAD.Runtime.Exception &&
  107.                                         (ex.InnerException as Autodesk.AutoCAD.Runtime.Exception).ErrorStatus ==
  108.                                         ErrorStatus.NotApplicable)
  109.                                     {
  110.                                         continue;
  111.                                     }
  112.                                     else
  113.                                     {
  114.                                         throw;
  115.                                     }
  116.                                 }
  117.                             }// GetProperTypeName
  118.                             else
  119.                             {
  120.                                 continue;
  121.                             }
  122.                         }// end Foreach
  123.                     }//end using
  124.                 }// end if resultbuffer is avaid
  125.                 else
  126.                 {
  127.                     return null;
  128.                 }
  129.             }// end if rb != null
  130.             else
  131.             {
  132.                 return null;
  133.             }// rb == null
  134.             return null;
  135.         }


命令: (mgetproperty (entlast) "length")
(1469.73)

命令: (mgetproperty (entlast) "ecs")
((1.0 0.0 0.0 0.0) (0.0 1.0 0.0 0.0) (0.0 0.0 1.0 0.0) (0.0 0.0 0.0 1.0))

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

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-5-9 07:46:43 | 显示全部楼层
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-5-9 08:39:57 | 显示全部楼层
本帖最后由 csharp 于 2014-5-9 08:43 编辑

先留存,慢慢学习
http://www.theswamp.org/index.php?topic=25582.0  
  1. //(test1 1 1 "Hello" "Dude")
  2.     [LispFunction("test1")]
  3.     public static ResultBuffer test1(ResultBuffer args)
  4.     {
  5.       ResultBuffer retList = new ResultBuffer();
  6.       try
  7.       {
  8.         Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

  9.         if (args == null)
  10.         {
  11.           retList.AddErr("Null Argument");
  12.           return retList;
  13.         }

  14.         if (args.GetCount() == 0)
  15.         {
  16.           retList.AddErr("To Few Arguments");
  17.           return retList;
  18.         }

  19.         Func<TypedValue, bool>
  20.           filter = x => x.TypeCode == (int)LispDataType.Text;

  21.         var rlist = args.Where(filter);

  22.         foreach (var e in rlist)
  23.           retList.AddString(e.Value.ToString());


  24.         if (retList.GetCount() == 0)
  25.         {
  26.           retList.AddNil();
  27.           return retList;
  28.         }

  29.       }
  30.       catch (System.Exception ex)
  31.       {
  32.         retList.AddErr(ex.Message);
  33.       }
  34.       return retList;
  35.     }


  36.     //(test2 "s" "a" "s" "a" "ad")
  37.     [LispFunction("test2")]
  38.     public static ResultBuffer test2(ResultBuffer args)
  39.     {
  40.       ResultBuffer retList = new ResultBuffer();
  41.       try
  42.       {

  43.         Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

  44.         if (args == null)
  45.         {
  46.           retList.AddErr("Null Argument");
  47.           return retList;
  48.         }

  49.         if (args.GetCount() == 0)
  50.         {
  51.           retList.AddErr("To Few Arguments");
  52.           return retList;
  53.         }

  54.         var query = from f in args
  55.                     where f == new TypedValue((int)LispDataType.Text, "a")
  56.                     select f;

  57.         foreach (var e in query)
  58.           retList.AddString((string)e.Value);

  59.         if (retList.GetCount() == 0)
  60.         {
  61.           retList.AddNil();
  62.           return retList;
  63.         }

  64.       }
  65.       catch (System.Exception ex)
  66.       {
  67.         retList.AddErr(ex.Message);
  68.       }
  69.       return retList;
  70.     }


1 楼的啰嗦了,应该 用linq 查询代替 Foreach,再改改
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-5-9 14:13:26 | 显示全部楼层
本帖最后由 csharp 于 2014-5-9 14:20 编辑

这样筛选
http://blog.csdn.net/q107770540/article/details/5724013
  1.                         var query = from PropertyDescriptor f in props
  2.                             where  (String)f.Name.ToUpper == proName && f.GetValue( obj) != null
  3.                             select f;
  4.                         if (query.Count() > 0)
  5.                         {
  6.                             foreach (PropertyDescriptor descriptor in query)
  7.                             {

  8.                             }
  9.                         }

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

使用道具 举报

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-5-9 16:19:24 | 显示全部楼层
[ 本帖最后由 csharp 于 2014-5-9 16:32 编辑 ]\n\n修改后如下
  1.         [LispFunction("mGetproperty")]
  2.         public static Object mGetproperty(ResultBuffer rb)
  3.         {
  4.             TypedValue[] values = rb.AsArray();
  5.             if (rb != null)
  6.             {
  7.                 Document document = Application.DocumentManager.MdiActiveDocument;
  8.                 Editor ed = document.Editor;
  9.                 String proName = (String) values[1].Value;
  10.                 proName = proName.ToUpper();
  11.                 Transaction transaction = document.TransactionManager.StartTransaction();
  12.                 if (values.Count() == 2 && values[0].TypeCode == (int) LispDataType.ObjectId &&
  13.                     values[1].TypeCode == (int) LispDataType.Text)
  14.                 {
  15.                     using (transaction)
  16.                     {
  17.                         ObjectId id = (ObjectId) values[0].Value;
  18.                         Entity obj = (Entity) id.GetObject(OpenMode.ForRead);
  19.                         PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
  20.                         var query = from PropertyDescriptor f in props
  21.                             where f.Name.ToUpper() == proName && f.GetValue(obj) != null
  22.                             select f;
  23.                         if (query.Count() > 0)
  24.                         {
  25.                             foreach (PropertyDescriptor descriptor in query)
  26.                             {
  27.                                 Object valueObj = null;
  28.                                 valueObj = descriptor.GetValue(obj);
  29.                                 if (valueObj is Point3d)
  30.                                 {
  31.                                     ResultBuffer ret =
  32.                                         new ResultBuffer(new TypedValue((int) LispDataType.Point3d, (Point3d) valueObj));
  33.                                     return ret;
  34.                                 }
  35.                                 else if (valueObj is Vector3d)
  36.                                 {
  37.                                     Vector3d vec = (Vector3d) valueObj;
  38.                                     Point3d pt = new Point3d(vec.X, vec.Y, vec.Z);
  39.                                     ResultBuffer ret = new ResultBuffer(new TypedValue((int) LispDataType.Point3d, pt));
  40.                                     return ret;
  41.                                 }
  42.                                 else if (valueObj is Matrix3d)
  43.                                 {
  44.                                     Matrix3d mat = (Matrix3d) valueObj;
  45.                                     return Tools.Matrix3dToesultBuffer(mat);
  46.                                 }
  47.                                 else if (valueObj is Boolean)
  48.                                 {
  49.                                     return (bool) valueObj;
  50.                                 }
  51.                                 else if (valueObj is Double)
  52.                                 {
  53.                                     return (Double) valueObj;
  54.                                 }
  55.                                 else if (valueObj is String)
  56.                                 {
  57.                                     return (String) valueObj;
  58.                                 }
  59.                                 else if (valueObj is Extents3d)
  60.                                 {
  61.                                     Extents3d pts = (Extents3d) valueObj;
  62.                                     Point3d p0 = pts.MinPoint;
  63.                                     Point3d pp = pts.MinPoint;
  64.                                     TypedValue[] newValues = new TypedValue[2];
  65.                                     newValues[0] = new TypedValue((int) LispDataType.Point3d, p0);
  66.                                     newValues[1] = new TypedValue((int) LispDataType.Point3d, pp);
  67.                                     ResultBuffer ret = new ResultBuffer(newValues);
  68.                                     return ret;
  69.                                 }
  70.                                 else
  71.                                 {
  72.                                     return null;
  73.                                 }
  74.                             }

  75.                         }
  76.                         else
  77.                         {
  78.                             return null;
  79.                         }
  80.                     }
  81.                 } // end if rb != null
  82.                 else
  83.                 {
  84.                     return null;
  85.                 } // rb == null
  86.                 return null;
  87.             }
  88.             else
  89.             {
  90.                 return null;
  91.             }
  92.         }


声明为 Object 类型就可以了
命令: (mgetproperty (entlast) "ecs")
((1.0 0.0 0.0 0.0) (0.0 1.0 0.0 0.0) (0.0 0.0 1.0 0.0) (0.0 0.0 0.0 1.0))

命令: (mgetproperty (entlast) "length")
1040.78
命令: (mgetproperty (entlast) "bounds")
((1909.13 1110.78 0.0) (1909.13 1110.78 0.0))
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-5-9 18:53:57 来自手机 | 显示全部楼层
本帖最后由 csharp 于 2014-5-10 12:24 编辑

有些属性返回还有点问题
Handle 是 Class 要单独判断,Boolean Double String 可以用 | 在一个 if 内
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 19个

财富等级: 恭喜发财

发表于 2020-2-12 18:09:46 | 显示全部楼层
缺少这个类Tools
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 5060个

财富等级: 富甲天下

发表于 2020-2-13 22:22:42 | 显示全部楼层
可以改写成C++版吗?
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 104个

财富等级: 日进斗金

发表于 2020-4-3 23:32:40 | 显示全部楼层
好东西哦,你值得拥有
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2021-2-25 14:27:58 | 显示全部楼层
厉害厉害 这个交互真好 学习 学习
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-3-29 16:39 , Processed in 0.403285 second(s), 45 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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