找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1690|回复: 6

[密技]:如何不使用sendcommand在vba中对lisp求值

[复制链接]
发表于 2002-9-26 10:21:07 | 显示全部楼层 |阅读模式

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

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

×

  1. <body>

  2. <table cellSpacing="0" cellPadding="2" width="666" bgColor="#e0e0e0" border="0">
  3.   <tbody>
  4.     <tr>
  5.       <td width="660">寄件者:<font face="arial,sans-serif"><a href="http://groups.google.com/groups?hl=zh-CN&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;q=author:holderm%40atscorporation.com+" target="_top">Mark
  6.         Holder</a> (<a href="mailto:holderm@atscorporation.com">holderm@atscorporation.com</a>)<br>
  7.         </font>主旨:<font face="arial,sans-serif">Re: Calling Autolisp
  8.         Programs From VB - Map 2000<br>
  9.         </font>新闻群组:<font face="arial,sans-serif"><a href="http://groups.google.com/groups?hl=zh-CN&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;group=autodesk.autocad.customization.vba" target="_top">autodesk.autocad.customization.vba</a><br>
  10.         <table cellSpacing="0" cellPadding="0" align="right" border="0">
  11.           <tbody>
  12.             <tr>
  13.               <td><font face="arial,sans-serif"><a href="http://groups.google.com/groups?hl=zh-CN&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;selm=375FBB76.FE6C2EDB%40atscorporation.com" target="_top">View
  14.                 this article only</a></font></td>
  15.             </tr>
  16.           </tbody>
  17.         </table>
  18.         </font>日期:<font face="arial,sans-serif">1999/06/10<br>
  19.         </font></td>
  20.     </tr>
  21.   </tbody>
  22. </table>
  23. <pre><font face="Courier New">Tony,

  24. Thanks once again for the info. For others who may be interested, I went looking and found the
  25. following link:

  26. <a href="http://www.autodesk.com/products/acadr14/files/avlisp.exe" target="_top">[url]http://www.autodesk.com/products/acadr14/files/avlisp.exe[/url]</a>

  27. The following is an excerpt:

  28. &lt;&lt;snip&gt;&gt;
  29. Visual LISP as an ActiveX Server

  30. Visual LISP can function as an ActiveX server.  This is useful when using VBA dialogs to control
  31. Visual LISP applications.  For instance, a programmer can create dialogs using the VBA visual
  32. tools for layout. They can then have the actions invoke Visual LISP.

  33. Any Visual LISP function may be invoked and any symbol may have its value referenced or set.
  34. Parameters and results are all variants.  In the following example

  35. <font color="#000080">Sub Hello()
  36.     Set vla = CreateObject(&quot;VL.Application.1&quot;)
  37.     Set vld = vla.ActiveDocument
  38.     Set vl_read = vld.Functions.Item(&quot;read&quot;)
  39.     Set vl_eval = vld.Functions.Item(&quot;eval&quot;)
  40.     Set vl_hello = vl_read.funcall(&quot;(defun hello (x)(print x))&quot;)
  41.     Set vl_hello = vl_eval.funcall(vl_hello)
  42.     Set vl_hello = vld.Functions.Item(&quot;hello&quot;)
  43.     ret = vl_hello.funcall(&quot;world&quot;)
  44.     MsgBox ret, vbOKOnly
  45. End Sub
  46. </font>
  47. we obtain access to Visual LISPs read and eval functions.  We then use those to define a hello
  48. function, which we then call, displaying its return value in a message box.
  49. &lt;&lt;snip&gt;&gt;

  50. Of interest in it is the phrase &quot;Parameters and results are all variants&quot;. We may get better
  51. results by declaring our variables as variants (which can contain an object) rather than declaring
  52. them directly as objects.

  53. I'm more optimistic about calling VLisp from VB after seeing Autodesk endorse the idea.

  54. Tony Tanzillo wrote:
  55. &lt;&lt;snip&gt;&gt;The Visual LISP white paper on Autodesk's web page has an example in VBA that should work.
  56. &lt;&lt;snip&gt;&gt;</font></pre>

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

  1. <body>

  2. <table cellPadding="2" width="581" bgColor="#ffcc33" border="0">
  3.   <tbody>
  4.     <tr>
  5.       <td bgColor="#ffcc33" width="571">所有留言及回应</td>
  6.     </tr>
  7.   </tbody>
  8. </table>
  9. <a name="link1"></a><a name="s"></a>
  10. <table cellSpacing="0" cellPadding="2" width="581" bgColor="#e0e0e0" border="0">
  11.   <tbody>
  12.     <tr>
  13.       <td width="575">
  14.         <table cellSpacing="0" cellPadding="0" align="right" border="0">
  15.           <tbody>
  16.             <tr>
  17.               <td><font face="Courier New" size="2">Message 1 in thread</font></td>
  18.             </tr>
  19.           </tbody>
  20.         </table>
  21.         <font size="2">寄件者:</font><font face="Courier New" size="2"><a href="http://groups.google.com/groups?hl=zh-CN&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;q=author:holderm%40atscorporation.com+" target="_top">Mark
  22.         Holder</a> (<a href="mailto:holderm@atscorporation.com">holderm@atscorporation.com</a>)<br>
  23.         </font><font size="2">主旨:</font><font face="Courier New" size="2">VLisp
  24.         from VB - Episode IV: The Variant Menace<br>
  25.         </font><font size="2">新闻群组:</font><font face="Courier New" size="2"><a href="http://groups.google.com/groups?hl=zh-CN&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;group=autodesk.autocad.customization.vba" target="_top">autodesk.autocad.customization.vba</a><br>
  26.         </font>
  27.         <table cellSpacing="0" cellPadding="0" align="right" border="0">
  28.           <tbody>
  29.             <tr>
  30.               <td><a href="http://groups.google.com/groups?hl=zh-CN&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;selm=37C5B503.8A7A0E88%40atscorporation.com" target="_top"><font face="Courier New" size="2">View
  31.                 this article only</font></a></td>
  32.             </tr>
  33.           </tbody>
  34.         </table>
  35.         <font size="2">日期:</font><font face="Courier New" size="2">1999/08/26<br>
  36.         </font></td>
  37.     </tr>
  38.   </tbody>
  39. </table>
  40. <pre><font face="Courier New">A recent thread in this NG brought to my attention a new (to me, at
  41. least) group of VLisp functions. I refer specifically to the very
  42. powerful vlax-Curve-xxx functions, though there are probably many more
  43. that could be useful. I posted a request for information on how to use
  44. these functions from VB(A).

  45. After much trial and error, I managed to get what I was after. The
  46. snippet of code below demonstrates obtaining the length of a polyline
  47. using some of these functions.

  48. To save others time and frustration, I offer the following observations
  49. along with the working code:

  50. 1) Variables to be passed as arguments to VLisp must be declared as
  51. Variant, though constants are coerced ok.

  52. 2) Those same arguments must be converted back to Lisp values with
  53. (vlax-variant-value &lt;v&gt;) before being used in &quot;standard&quot; lisp function
  54. calls (like (handent), or ones you might write) in the Lisp code you
  55. will be calling from VB.

  56. 3) You can only call Lisp defined functions (as opposed to internal ones
  57. like vlax-curve-xxx) directly, because internal functions cannot be
  58. found by vld.Functions.Item(fName).

  59. 4) I call (vl-load-com) in my AcadDoc.LSP file, to ensure that it is
  60. always loaded, and to avoid calling it in every routine I write. If you
  61. don't, make sure it is called before you do anything that depends on it.

  62. 5) The Lisp code can be defined in a standard text or vlx file, in which
  63. case it need not be built into your code. By putting it in your code you
  64. can be sure it is available, and not visible to others.

  65. 6) I chose to prefix my ActiveX Lisp function names with &quot;vb&quot; to
  66. indicate that their arguments have to be made into ActiveX variants
  67. (with vlax-make-variant) if they are to be called from regular Lisp
  68. code.

  69. <font color="#000080">Private Sub GetEnd(ByVal anEnt As AutoCAD.AcadEntity)
  70.    Dim vl_Read As Variant, vl_Eval As Variant, vl_Defun As Variant, ret
  71. As Variant
  72.    Dim vld As Variant
  73.    Dim arg As Variant
  74.    Dim fName As String
  75.    Dim NoFunc As String

  76.    On Error GoTo Failed

  77. </font><font color="#008000">    ' Your code to Set vld will call GetInterfaceObject or CreateObject.
  78. </font><font color="#000080">
  79.    Set vld = myAcadSession.VLisp.ActiveDocument

  80. </font><font color="#008000">    ' An arbitrary function name is used.
  81. </font><font color="#000080">   fName = &quot;vbEntityLength&quot;
  82.    Set vl_Defun = vld.Functions.Item(fName)

  83. </font><font color="#008000">   ' One might think that the value of vl_Defun or Err would indicate
  84. whether function fName
  85.    '     was already available, but NOOOO! It is always an Object.
  86. </font><font color="#000080">
  87. </font><font color="#008000">   ' An error will be raised when we try to use the function...
  88. </font><font color="#000080">   On Error Resume Next

  89. </font><font color="#008000">    ' Note: Passing anEnt.Handle directly does NOT work. Parameters MUST
  90. be variants
  91. </font><font color="#000080">   arg = anEnt.Handle
  92.    ret = vl_Defun.funcall(arg)
  93.    If Err Then
  94. </font><font color="#008000">      ' Err.Number=2000, whether the reason is no function definition or
  95. the entity is not a curve
  96. </font><font color="#000080">      NoFunc = &quot;no function definition:&quot;
  97.       If UCase(Left$(Err.Description, Len(NoFunc))) = UCase(NoFunc) Then

  98. </font><font color="#008000">         ' MsgBox Err.Description &amp; &quot; (&quot; &amp; Err.Number &amp; &quot;)&quot;,
  99. vbInformation, &quot;Defining function &quot; &amp; fName &amp; &quot;:&quot;
  100. </font><font color="#000080">         Err.Clear

  101.          Set vl_Read = vld.Functions.Item(&quot;read&quot;)
  102.          Set vl_Eval = vld.Functions.Item(&quot;eval&quot;)

  103. </font><font color="#008000">        ' Note: a mopre legible version of the Lisp code appears below,
  104. after the 'End Sub' statement
  105. </font><font color="#000080">         Set vl_Defun = vl_Read.funcall(&quot;(defun &quot; &amp; fName &amp; &quot; (x /
  106. picked rc lgth) (setq picked (handent (vlax-variant-value x)))  (setq rc
  107. (Vlax-Curve-GetEndPoint picked)) (setq lgth (vlax-curve-GetDistAtPoint
  108. picked rc)))&quot;)
  109.          Set vl_Defun = vl_Eval.funcall(vl_Defun)

  110.          Set vl_Defun = vld.Functions.Item(fName)

  111.          arg = anEnt.Handle
  112.          ret = vl_Defun.funcall(arg)
  113.       Else
  114.          MsgBox Err.Description &amp; &quot; (&quot; &amp; Err.Number &amp; &quot;)&quot;,
  115. vbInformation, &quot;Error in &quot; &amp; fName &amp; &quot;:&quot;
  116.       End If
  117.    Else
  118.       MsgBox &quot;Function &quot; &amp; fName &amp; &quot; was already defined:&quot;
  119.    End If

  120.    MsgBox &quot;Length = &quot; &amp; ret &amp; &quot;.&quot;

  121.    Exit Sub

  122. Failed:
  123.    MsgBox Err.Description, vbInformation, &quot;Error:&quot;
  124.    Err.Clear
  125.    Exit Sub

  126. End Sub

  127. </font>A more legible version of the Lisp code is:
  128. <font color="#000080">(defun vbEntityLength ( x /
  129.    picked rc lgth
  130.    )
  131. </font><font color="#008000">    ; The argument passed in is an ActiveX variant, which must be
  132. converted into a plain Lisp value
  133.     ; before Handent can use it.
  134. </font><font color="#000080">   (setq picked (handent (vlax-variant-value x)))

  135. </font><font color="#008000">    ; Now, we can call the VLisp function to get the end point
  136. </font><font color="#000080">   (setq rc (Vlax-Curve-GetEndPoint picked))

  137. </font><font color="#008000">    ; And the length is the distance at the end point
  138. </font><font color="#000080">   (setq lgth (vlax-curve-GetDistAtPoint picked rc))
  139. )</font></font></pre>
  140. <a name="link2"></a>
  141. <table cellSpacing="0" cellPadding="2" width="558" bgColor="#e0e0e0" border="0">
  142.   <tbody>
  143.     <tr>
  144.       <td width="552">
  145.         <table cellSpacing="0" cellPadding="0" align="right" border="0">
  146.           <tbody>
  147.             <tr>
  148.               <td><font face="Courier New" size="2">Message 2 in thread</font></td>
  149.             </tr>
  150.           </tbody>
  151.         </table>
  152.         <font size="2">寄件者:</font><font face="Courier New" size="2"><a href="http://groups.google.com/groups?hl=zh-CN&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;q=author:degag%40videotron.ca+" target="_top">Denis
  153.         Gagne</a> (<a href="mailto:degag@videotron.ca">degag@videotron.ca</a>)<br>
  154.         </font><font size="2">主旨:</font><font face="Courier New" size="2">Re:
  155.         VLisp from VB - Episode IV: The Variant Menace<br>
  156.         </font><font size="2">新闻群组:</font><font face="Courier New" size="2"><a href="http://groups.google.com/groups?hl=zh-CN&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;group=autodesk.autocad.customization.vba" target="_top">autodesk.autocad.customization.vba</a><br>
  157.         </font>
  158.         <table cellSpacing="0" cellPadding="0" align="right" border="0">
  159.           <tbody>
  160.             <tr>
  161.               <td><a href="http://groups.google.com/groups?hl=zh-CN&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;selm=7q7eoo%24miu32%40adesknews2.autodesk.com" target="_top"><font face="Courier New" size="2">View
  162.                 this article only</font></a></td>
  163.             </tr>
  164.           </tbody>
  165.         </table>
  166.         <font size="2">日期:</font><font face="Courier New" size="2">1999/08/27<br>
  167.         </font></td>
  168.     </tr>
  169.   </tbody>
  170. </table>
  171. <pre><font face="Courier New">Not enough for a marriage consumption
  172. between Lisp and Vba but at least it's the
  173. beginning of a more solid frequentation.

  174. Very well done, Mark

  175. Denis</font></pre>
  176. <a name="link3"></a>
  177. <table cellSpacing="0" cellPadding="2" width="557" bgColor="#e0e0e0" border="0">
  178.   <tbody>
  179.     <tr>
  180.       <td width="551">
  181.         <table cellSpacing="0" cellPadding="0" align="right" border="0">
  182.           <tbody>
  183.             <tr>
  184.               <td><font face="Courier New" size="2">Message 3 in thread</font></td>
  185.             </tr>
  186.           </tbody>
  187.         </table>
  188.         <font size="2">寄件者:</font><font face="Courier New" size="2"><a href="http://groups.google.com/groups?hl=zh-CN&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;q=author:holderm%40atscorporation.com+" target="_top">Mark
  189.         Holder</a> (<a href="mailto:holderm@atscorporation.com">holderm@atscorporation.com</a>)<br>
  190.         </font><font size="2">主旨:</font><font face="Courier New" size="2">Re:
  191.         VLisp from VB - Episode IV: The Variant Menace<br>
  192.         </font><font size="2">新闻群组:</font><font face="Courier New" size="2"><a href="http://groups.google.com/groups?hl=zh-CN&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;group=autodesk.autocad.customization.vba" target="_top">autodesk.autocad.customization.vba</a><br>
  193.         </font>
  194.         <table cellSpacing="0" cellPadding="0" align="right" border="0">
  195.           <tbody>
  196.             <tr>
  197.               <td><a href="http://groups.google.com/groups?hl=zh-CN&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;selm=37C7D6C2.85F9EB3E%40atscorporation.com" target="_top"><font face="Courier New" size="2">View
  198.                 this article only</font></a></td>
  199.             </tr>
  200.           </tbody>
  201.         </table>
  202.         <font size="2">日期:</font><font face="Courier New" size="2">1999/08/28</font><font face="Courier New"><br>
  203.         </font></td>
  204.     </tr>
  205.   </tbody>
  206. </table>
  207. <pre><font face="Courier New">Denis,

  208. Clearly, a better approach would be to expose functions like these
  209. through an AcitveX DLL like DbConnect does for database connectivity.
  210. Perhaps an adesker like Jorge or Albert or an intrepid 3rd party ARX
  211. developer will take on the task. Or maybe we just haven't yet found out
  212. what DLL we need to reference.

  213. Mark Holder

  214. Denis Gagne wrote:

  215. <font color="#660066">&gt; Not enough for a marriage consumption
  216. &gt; between Lisp and Vba but at least it's the
  217. &gt; beginning of a more solid frequentation.
  218. &gt;
  219. &gt; Very well done, Mark
  220. &gt;
  221. &gt; Denis</font></font></pre>
  222. <table cellSpacing="0" cellPadding="0" width="100%" border="0">
  223.   <tbody>
  224.     <tr>
  225.       <td bgColor="#ffcc33"><font face="Courier New"><img height="3" alt src="http://groups.google.com/images/cleardot.gif"></font></td>
  226.     </tr>
  227.   </tbody>
  228. </table>
  229. <font face="Courier New"><br>
  230. <center><font size="-1">&copy;2002 Google</font></center></font>

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

使用道具 举报

发表于 2002-10-19 16:52:36 | 显示全部楼层

请教Quelea

请教Quelea, 你做过测试吗?
为什么我老是不能得到或创建“VL.Application.1”对象(vl.arx已加载,我也已引用了vl.tlb,vlide能正常运行)
报运行时错误800700c1.
即“类型未注册”的错误?
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2002-10-28 15:06:31 | 显示全部楼层

-

I see.

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

使用道具 举报

发表于 2002-11-19 20:36:09 | 显示全部楼层

Re: -

最初由 rave 发布
[B]I see.

(vl-load-com) [/B]

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

使用道具 举报

已领礼包: 181个

财富等级: 日进斗金

发表于 2002-11-19 20:40:19 | 显示全部楼层
用SendCommand "(vl-load-com)"加载或者将(vl-load-com)放入LSP程序中,使CAD启动时自动加载。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2002-11-19 20:47:31 | 显示全部楼层

谢谢老板,我下了

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-22 06:35 , Processed in 0.330689 second(s), 43 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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