明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3860|回复: 13

.net Api怎么实现Lisp里面(textscr)函数的功能

  [复制链接]
发表于 2009-9-3 15:18 | 显示全部楼层 |阅读模式

有的时候需要用.net Api处理数据并在cad的文本屏幕上显示出来,但是找了半天也没有找到怎么实现这么一个简单的功能,版主能不能帮忙解决一下?

发表于 2009-9-3 15:44 | 显示全部楼层
2010版本有提供
Application.DisplayTextScreen = true;

之前的版本估计只有发送命令了
  1.         [DllImport("acad.exe")]
  2.         private static extern int acedCmd(IntPtr vlist);
  3.         public static void Command(params object[] args)
  4.         {
  5.             ResultBuffer rb = new ResultBuffer();
  6.             foreach (object val in args)
  7.             {
  8.                 if (val == null)
  9.                 {
  10.                     rb.Add(new TypedValue((int)LispDataType.Text, ""));
  11.                 }
  12.                 else
  13.                 {
  14.                     switch (val.GetType().Name)
  15.                     {
  16.                         case "String":
  17.                             rb.Add(new TypedValue((int)LispDataType.Text, val));
  18.                             break;
  19.                         case "Point2d":
  20.                             rb.Add(new TypedValue((int)LispDataType.Point2d, val));
  21.                             break;
  22.                         case "Point3d":
  23.                             rb.Add(new TypedValue((int)LispDataType.Point3d, val));
  24.                             break;
  25.                         case "ObjectId":
  26.                             rb.Add(new TypedValue((int)LispDataType.ObjectId, val));
  27.                             break;
  28.                         case "SelectionSet":
  29.                             rb.Add(new TypedValue((int)LispDataType.SelectionSet, val));
  30.                             break;
  31.                         case "Double":
  32.                             rb.Add(new TypedValue((int)LispDataType.Double, val));
  33.                             break;
  34.                         case "Int16":
  35.                             rb.Add(new TypedValue((int)LispDataType.Int16, val));
  36.                             break;
  37.                         case "Int32":
  38.                             rb.Add(new TypedValue((int)LispDataType.Int32, val));
  39.                             break;
  40.                         case "TypedValue":
  41.                             rb.Add(val);
  42.                             break;
  43.                     }
  44.                 }
  45.             }
  46.             try
  47.             {
  48.                 acedCmd(rb.UnmanagedObject);
  49.             }
  50.             catch
  51.             { }
  52.             finally
  53.             {
  54.                 rb.Dispose();
  55.             }
  56.         }
  57.         [CommandMethod("tscr")]
  58.         public static void TextScr()
  59.         {
  60.             Command("TextScr");
  61.             Document doc = Application.DocumentManager.MdiActiveDocument;
  62.             doc.Editor.WriteMessage("\nHi");
  63.         }
 楼主| 发表于 2009-9-3 16:07 | 显示全部楼层

我是用的vb.net,如果靠发送命令的话,我是这样实现的,

   Public Sub ShowTextScreen()
        Dim Doc As Document = Application.DocumentManager.MdiActiveDocument
        Doc.SendStringToExecute("textscr ", False, False, False)
    End Sub

对c#应该是

public static void ShowTextScreen()

{

        Document Doc = Application.DocumentManager.MdiActiveDocument;

        Doc.SendStringToExecute("textscr ", False, False, False);

}
这个是可以实现的,但是用了这个函数之后,cad默认的命令就是textscr了,很不方便命令的重复使用

版主有什么办法解决嘛?

发表于 2009-9-3 16:17 | 显示全部楼层

你没有试过二楼的代码?这个可以的
SendStringToExecute不要用

另外注意代码以前最好设置系统变量cmdecho=0

 楼主| 发表于 2009-9-3 16:41 | 显示全部楼层

SendStringToExecute有什么问题嘛?二楼的代码我还要整成vb.net的试下

发表于 2009-9-3 16:54 | 显示全部楼层

SendStringToExecute和VBA的SendCommand一样,

应该都是调用Com接口发送,发了就不管:)

 楼主| 发表于 2009-9-3 18:40 | 显示全部楼层
不行啊,这是vb代码:
这是trycommand 里的模块文件内容
  1. Imports System
  2. Imports System.Runtime.InteropServices
  3. Imports Autodesk.AutoCAD.Runtime
  4. Imports Autodesk.AutoCAD.DatabaseServices
  5. Imports Autodesk.AutoCAD.EditorInput
  6. Imports Autodesk.AutoCAD.ApplicationServices
  7. Module Example
  8.     <DllImport("acad.exe")> Public Function acedCmd(ByVal vlist As IntPtr) As Integer
  9.     End Function
  10.     Public Sub Command(ByVal args As Object)
  11.         Dim rb As ResultBuffer = New ResultBuffer()
  12.         Dim val As Object
  13.         For Each val In args
  14.             If (val = Nothing) Then
  15.                 rb.Add(New TypedValue(CInt(LispDataType.Text), ""))
  16.             Else
  17.                 Select Case val.GetType().Name
  18.                     Case "String"
  19.                         rb.Add(New TypedValue(CInt(LispDataType.Text), val))
  20.                         Exit Select
  21.                     Case "Point2d"
  22.                         rb.Add(New TypedValue(CInt(LispDataType.Point2d), val))
  23.                         Exit Select
  24.                     Case "Point3d"
  25.                         rb.Add(New TypedValue(CInt(LispDataType.Point3d), val))
  26.                         Exit Select
  27.                     Case "ObjectId"
  28.                         rb.Add(New TypedValue(CInt(LispDataType.ObjectId), val))
  29.                         Exit Select
  30.                     Case "SelectionSet"
  31.                         rb.Add(New TypedValue(CInt(LispDataType.SelectionSet), val))
  32.                         Exit Select
  33.                     Case "Double"
  34.                         rb.Add(New TypedValue(CInt(LispDataType.Double), val))
  35.                         Exit Select
  36.                     Case "Int16"
  37.                         rb.Add(New TypedValue(CInt(LispDataType.Int16), val))
  38.                         Exit Select
  39.                     Case "Int32"
  40.                         rb.Add(New TypedValue(CInt(LispDataType.Int32), val))
  41.                         Exit Select
  42.                     Case "TypedValue"
  43.                         rb.Add(val)
  44.                         Exit Select
  45.                 End Select
  46.             End If
  47.             Try
  48.                 acedCmd(rb.UnmanagedObject)
  49.             Catch
  50.             Finally
  51.                 rb.Dispose()
  52.             End Try
  53.         Next val
  54.     End Sub
  55. End Module
这是trycommand里的类内容
  1. Imports Autodesk.AutoCAD.Runtime
  2. Imports Autodesk.AutoCAD.ApplicationServices
  3. Imports Application = Autodesk.AutoCAD.ApplicationServices.Application
  4. Imports Autodesk.AutoCAD.EditorInput
  5. Imports Autodesk.AutoCAD.Geometry
  6. Imports Autodesk.AutoCAD.DatabaseServices
  7. Imports System
  8. Imports System.Runtime.InteropServices
  9. Imports System.Windows.Forms
  10. Imports SysApplication = System.Windows.Forms.Application
  11. Public Class Class1
  12.     <CommandMethod("tscr")> Sub TextScr()
  13.         Command("TextScr")
  14.         Dim doc As Document = Application.DocumentManager.MdiActiveDocument
  15.         doc.Editor.WriteMessage("\nHi")
  16.     End Sub
  17. End Class
在vb.net里用行不通啊
 楼主| 发表于 2009-9-3 18:48 | 显示全部楼层

这是源文件,帮我看下,vs2008,怎么都没有效果,运行之后就只有这个:

命令: netload
命令: tscr
命令:
命令:
命令:
命令:
命令:
命令:
命令: \nHi

我数了一下,刚好是六个空命令加一个空格("\nhi"前面有一个空格),对应textscr七个字符,如果是Command("Line"),运行结果就是这样的

命令: tscr
命令:
命令:
命令:
命令: \nHi

 楼主| 发表于 2009-9-3 18:57 | 显示全部楼层

我知道了,这样改了一下:

把 Public Sub Command(ByVal args As Object)改成

 ublic Sub Command(ByVal args() As Object)

然后把 Command("TextScr")改成       

     Dim CmdStr(0) As Object
        CmdStr(0) = "TextScr"
        Command(CmdStr)

 楼主| 发表于 2009-9-3 19:17 | 显示全部楼层

lzh741206版主:好像Command函数就只能运行单一的文本命令,我这样做程序就会出错:

    <CommandMethod("tscr")> Sub TextScr()
        Dim CmdStr() As Object = {"RECTANG", New Point2d(0, 0), New Point2d(1, 1)}

        Command(CmdStr)
        'Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        'doc.Editor.WriteMessage("\nHi")
    End Sub

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

本版积分规则

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

GMT+8, 2024-5-10 09:16 , Processed in 0.163643 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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