注册 登录
明经CAD社区 返回首页

鱼与熊掌的个人空间 http://www.mjtd.com/?7304311 [收藏] [复制] [分享] [RSS]

日志

C#中定义Lisp函数,并且用调用lisp函数.

热度 2已有 982 次阅读2015-4-15 13:52 |系统分类:应用

using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System.Runtime.InteropServices;

namespace ClassLibrary2
{

    public class Class1
    {

        [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl)]
        extern static private int acedInvoke(IntPtr args, out IntPtr result);
        public static ResultBuffer CallLispFunction(ResultBuffer args)
        {
            IntPtr ip = IntPtr.Zero;
            int st = acedInvoke(args.UnmanagedObject, out ip);
            if (ip != IntPtr.Zero)
            {
                ResultBuffer rbRes = ResultBuffer.Create(ip, true);
                return rbRes;
            }
            return null;
        }

        private static void AddValueToResultBuffer(ref ResultBuffer rb, object obj)
        {
            if (obj == null)
            {
                rb.Add(new TypedValue((int)LispDataType.Text, ""));
            }
            else
            {
                if (obj is string)
                {
                    rb.Add(new TypedValue((int)LispDataType.Text, obj));
                }
                else if (obj is Point2d)
                {
                    rb.Add(new TypedValue((int)LispDataType.Text, "_non"));
                    rb.Add(new TypedValue((int)LispDataType.Point2d, obj));
                }
                else if (obj is Point3d)
                {
                    rb.Add(new TypedValue((int)LispDataType.Text, "_non"));
                    rb.Add(new TypedValue((int)LispDataType.Point3d, obj));
                }
                else if (obj is ObjectId)
                {
                    rb.Add(new TypedValue((int)LispDataType.ObjectId, obj));
                }
                else if (obj is SelectionSet)
                {
                    rb.Add(new TypedValue((int)LispDataType.SelectionSet, obj));
                }
                else if (obj is double)
                {
                    rb.Add(new TypedValue((int)LispDataType.Double, obj));
                }
                else if (obj is short)
                {
                    rb.Add(new TypedValue((int)LispDataType.Int16, obj));
                }
                else if (obj is int)
                {
                    rb.Add(new TypedValue((int)LispDataType.Int32, obj));
                }
                else if (obj is TypedValue)
                {
                    rb.Add(obj);
                }

            }
        }


        public static ResultBuffer CallLispFunction(string name, params object[] args)
        {
            ResultBuffer rbArgs = new ResultBuffer();
            rbArgs.Add(new TypedValue((int)LispDataType.Text, name));
            foreach (object val in args)
            {
                AddValueToResultBuffer(ref rbArgs, val);
            }
            return CallLispFunction(rbArgs);
        }


        [LispFunction("GetArr")]
        public static ResultBuffer GetArr(ResultBuffer resBufIn)
        {
            ResultBuffer resBufOut = new ResultBuffer();
            resBufOut.Add(new TypedValue((int)LispDataType.ListBegin));
            resBufOut.Add(
              new TypedValue((int)LispDataType.Int16, 700));
            resBufOut.Add(
              new TypedValue((int)LispDataType.Int16, 800));
            resBufOut.Add(
              new TypedValue((int)LispDataType.Int16, 18));
            resBufOut.Add(
              new TypedValue((int)LispDataType.Text, "a+b"));
            resBufOut.Add(new TypedValue((int)LispDataType.ListEnd));
            resBufOut.Add(new TypedValue((int)LispDataType.ListBegin));
            resBufOut.Add(
              new TypedValue((int)LispDataType.Int16, 700));
            resBufOut.Add(
              new TypedValue((int)LispDataType.Int16, 800));
            resBufOut.Add(
              new TypedValue((int)LispDataType.Int16, 18));
            resBufOut.Add(
              new TypedValue((int)LispDataType.Text, "a+b"));
            resBufOut.Add(new TypedValue((int)LispDataType.ListEnd));
           Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
           ed.WriteMessage("test");
            return resBufOut;
        }
        [LispFunction("tt")]
        public static ResultBuffer tt(ResultBuffer resBufIn)
        {
            ResultBuffer resBufOut = new ResultBuffer();
            resBufOut = CallLispFunction("getarr");
            resBufOut.Add(
              new TypedValue((int)LispDataType.Int16, 700));
            return resBufOut;
            //return resBufOut;
        }
    }
}


路过

雷人

握手

鲜花

鸡蛋

发表评论 评论 (3 个评论)

回复 Real_King 2015-4-16 21:56
混合编程,好
回复 鱼与熊掌 2015-4-16 23:22
Real_King: 混合编程,好
  
回复 yarp 2015-5-6 09:57
好,太好的资料了。

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-19 04:58 , Processed in 0.082427 second(s), 16 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部