找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1863|回复: 0

[分享] Return nil to indicate an error condition in a .NET LispFunction

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

发表于 2013-5-27 23:35:13 | 显示全部楼层 |阅读模式

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

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

×
Return nil to indicate an error condition in a .NET LispFunction                                                        By Wayne Brill
  Issue
  I am implementing functionality in .NET that I am using from AutoLISP. How can I return an error condition from the .NET function?
  
  Solution
  One suggestion is to return nil from the .NET function using the LispDataType. If the function does return nil you could then handle that appropriately. (Or you could return an integer and respond to that value in the LISP function)
  Here is an excerpt from the AutoCAD .NET Managed Class Reference guide for the LispDataType:
  1. Public Enum LispDataType
  2.   Angle = &H138c
  3.   DottedPair = &H139a
  4.   Double = &H1389
  5.   Int16 = &H138b
  6.   Int32 = &H1392
  7.   ListBegin = &H1398
  8.   ListEnd = &H1399
  9.   Nil = &H139b
  10.   None = &H1388
  11.   ObjectId = &H138e
  12.   Orientation = &H1390
  13.   Point2d = &H138a
  14.   Point3d = &H1391
  15.   SelectionSet = &H138f
  16.   T_atom = &H139d
  17.   Text = &H138d
  18.   Void = &H1396
  19. End Enum
复制代码
Here is a VB example. Notice that if the entity is not selected ok, the routine returns nil.
  1. <LispFunction("getId")>
  2. Public Function test _
  3.   (ByVal myLispArgs As ResultBuffer) As ResultBuffer
  4.     ' This example asks the user to select
  5.     ' an(entity) The object id of the entity is
  6.     ' returned to the calling lisp function
  7.     Dim rbfResult As ResultBuffer
  8.     Dim ed As Autodesk.AutoCAD.EditorInput.Editor
  9.     ed = Autodesk.AutoCAD.ApplicationServices. _
  10.                   Application.DocumentManager. _
  11.                         MdiActiveDocument.Editor
  12.     Dim prmptEntOpts As New PromptEntityOptions _
  13.                                   ("Select Entity")
  14.     Dim prmptEntRes As PromptEntityResult
  15.     ' Get the entity from the users selection
  16.     prmptEntRes = ed.GetEntity(prmptEntOpts)
  17.     ' Make sure the entity was selected ok
  18.     If prmptEntRes.Status <> PromptStatus.OK Then
  19.         rbfResult = New ResultBuffer _
  20.               (New TypedValue(LispDataType.Nil))
  21.         Return rbfResult
  22.     End If
  23.     ' Return the ObjectId to the Lisp function
  24.     Dim objId As ObjectId
  25.     objId = prmptEntRes.ObjectId
  26.     rbfResult = New ResultBuffer _
  27.       (New TypedValue(LispDataType.ObjectId, objId))
  28.     Return rbfResult
  29. End Function

Here is a LISP function that uses the .NET function:
  1. (defun c:testID ( / objId nilTest ent)
  2.     (setq objId (getId))
  3.     (setq nilTest (nth 0 objId))
  4.     (if (= nilTest nil)
  5.         (princ "Entity not selected")
  6.       )
  7.      (if (/= nilTest nil)
  8.      (progn
  9.           (vl-load-com);ensure COM API is available
  10.         (setq ent (vlax-ename->vla-object (nth 0 objId)));the 0 element is an ename
  11.       (vla-highlight ent :vlax-true); highlight the entity using COM call
  12.      );progn
  13.     );if
  14.     (princ)
  15.     )
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-11-17 22:16 , Processed in 0.176842 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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