找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2389|回复: 4

[分享] AutoCAD .NET: Define Lisp Functions – Input and Output

[复制链接]

已领礼包: 1268个

财富等级: 财源广进

发表于 2013-7-25 11:20:30 | 显示全部楼层 |阅读模式

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

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

×
本帖最后由 st788796 于 2013-7-25 11:22 编辑

AutoCAD .NET: Define Lisp Functions – Input and OutputAutoCAD .NET API can also define lisp functions. Just like creating commands through specifying the CommandMethod attribute for a method, we can specify the LispFunction attribute for a special kind of methods to define LISP functions.
The method needs to accept a ResultBuffer argument and generally returns a common Object. By the way, the method should be local. It cannot be global or static. It must be public, certainly.
[LispFunction("DoubleMe")]
public Object DoubleMe_LispFunction(ResultBuffer rb)
{
    Object ret = null;
    if (rb != null)
    {
        TypedValue[] tvArr = rb.AsArray();
        if (tvArr.Count() > 0)
        {
            if (tvArr.Count() == 1)   // a single value
            {
                ret = DoubleValue(tvArr[0]);
            }
            else    // a list
            {
                ret = rb;
            }
        }
    }
    return ret;
}
Object DoubleValue(TypedValue tv)
{
    Object ret = null;
    if (tv.TypeCode == (int)LispDataType.T_atom)
    {
        ret = true;
    }
    else if (tv.TypeCode == (int)LispDataType.Int16)
    {
        ret = ((Int16)tv.Value) * 2;
    }
    else if (tv.TypeCode == (int)LispDataType.Int32)
    {
        ret = ((Int32)tv.Value) * 2;
    }
    else if (tv.TypeCode == (int)LispDataType.Double
        || tv.TypeCode == (int)LispDataType.Angle)
    {
        ret = ((double)tv.Value) * 2.0;
    }
    else if (tv.TypeCode == (int)LispDataType.None
        || tv.TypeCode == (int)LispDataType.Nil
        || tv.TypeCode == (int)LispDataType.Void)
    {
        ret = null;
    }
    else if (tv.TypeCode == (int)LispDataType.Text)
    {
        ret = (tv.Value as String) + (tv.Value as String);
    }
    else if (tv.TypeCode == (int)LispDataType.Point2d)
    {
        Point2d p2d = (Point2d)tv.Value * 2.0;
        ret = p2d;
    }
    else if (tv.TypeCode == (int)LispDataType.Point3d)
    {
        Point3d p3d = (Point3d)tv.Value * 2.0;
        ret = p3d;
    }
    else if (tv.TypeCode == (int)LispDataType.ObjectId)
    {
        ret = tv.Value;
    }
    else if (tv.TypeCode == (int)LispDataType.SelectionSet)
    {
        ret = tv.Value;
    }
    else if (tv.TypeCode == (int)LispDataType.ListBegin)
    {
        ret = tv.Value;
        Debug.WriteLine(string.Format("TypeCode: {0}/ListBegin\t\tValue: {1}", tv.TypeCode, tv.Value));
    }
    else if (tv.TypeCode == (int)LispDataType.ListEnd)
    {
        ret = tv.Value;  
        Debug.WriteLine(string.Format("TypeCode: {0}/ListEnd\t\tValue: {1}", tv.TypeCode, tv.Value));
    }
    else if (tv.TypeCode == (int)LispDataType.Orientation)
    {
        ret = tv.Value;   
        Debug.WriteLine(string.Format("TypeCode: {0}/Orientation\t\tValue: {1}", tv.TypeCode, tv.Value));
    }
    else if (tv.TypeCode == (int)LispDataType.DottedPair)
    {
        ret = tv.Value;   
        Debug.WriteLine(string.Format("TypeCode: {0}/DottedPair\t\tValue: {1}", tv.TypeCode, tv.Value));
    }
    else
    {
        ret = tv;
    }
    return ret;
}
There are totally only around a couple of dozens of lines of code above, but various input types and return types can be exercised by the single same LISP function definition. In fact, the return/output types of the LISP function depend on its input. The function tries to double its input value and return it if possible and convenient.
The following are some example outputs in the AutoCAD command line window when the LISP function is exercised:
Command: (doubleme 1.1 )
2.2
Command: (doubleme 0 )
0
Command: (doubleme 1.5)
3.0
Command: (doubleme 3)
6
Command: (doubleme pi)
6.28319
Command: (doubleme (sin (/ pi 6)))
1.0
Command: (doubleme '( 1 2 3) )
(2.0 4.0 6.0)
Command: (doubleme '( 1 ) )
(1)
Command: (doubleme T )
T
Command: (doubleme nil)
nil
Command: (doubleme )
nil
Command: (doubleme (> 0 1))
nil
Command: (doubleme (> 2 1))
T
Command: (doubleme "ab" "cd" )
("ab" "cd")
Command: (doubleme '(8 . "aabb"))
(8 . "aabb")
Command: (doubleme 1.1 2.2 3.3)
(1.1 2.2 3.3)
Command: (doubleme 1.1 2.2 )
(1.1 2.2)
Command: (setq id (car (entsel)))
Select object: <Entity name: 7ffffb09f10>
Command: (doubleme id)
<Entity name: 7ffffb09f10>
Command: (setq entlst (entget id))
((-1 . <Entity name: 7ffffb09f10>) (0 . "CIRCLE") (330 . <Entity name:
7ffffb069f0>) (5 . "231") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 .
"0") (100 . "AcDbCircle") (10 -16.1102 8.8777 0.0) (40 . 9.78317) (210 0.0 0.0
1.0))
Command: (doubleme entlst)
((-1 . <Entity name: 7ffffb09f10>) (0 . "CIRCLE") (330 . <Entity name:
7ffffb069f0>) (5 . "231") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 .
"0") (100 . "AcDbCircle") (10 -16.1102 8.8777 0.0) (40 . 9.78317) (210 0.0 0.0
1.0))
Command: (setq pnt (getpoint))
(65.4005 11.0181 0.0)
Command: (doubleme pnt)
(130.801 22.0363 0.0)
Command: (setq angle (getangle))
Specify second point: 0.701371
Command: (doubleme angle)
1.40274
Command: (setq ss (ssget))
11 found
<Selection set: 1e>
Command: (doubleme ss)
<Selection set: 1e>
Command: (doubleme "abcd")
"abcdabcd"

评分

参与人数 1D豆 +5 收起 理由
ScmTools + 5 很给力!经验;技术要点;资料分享奖!

查看全部评分

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

点评

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

使用道具 举报

已领礼包: 1268个

财富等级: 财源广进

 楼主| 发表于 2013-7-25 17:20:55 | 显示全部楼层
snsj 发表于 2013-7-25 17:08
给NET和LISP间搭桥

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

使用道具 举报

已领礼包: 1632个

财富等级: 堆金积玉

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

使用道具 举报

已领礼包: 347个

财富等级: 日进斗金

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-13 07:44 , Processed in 0.188874 second(s), 40 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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