找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1690|回复: 2

[分享] ARX定义LISP函数实例(摘自国外)

[复制链接]
发表于 2013-5-22 21:45:11 | 显示全部楼层 |阅读模式

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

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

×
摘自国外某一网站,觉得不错才转来这里,1D豆对新手来说绝对物有所值。
[sell=1]
  1. /**  \file RunShell.cpp
  2. *  \brief
  3. */

  4. /****************************************************************************/
  5. /*  RunShell.cpp                              */
  6. /****************************************************************************/
  7. /*                                                                          */
  8. /*  Copyright 2010 Paul Kohut                                               */
  9. /*  Licensed under the Apache License, Version 2.0 (the "License"); you may */
  10. /*  not use this file except in compliance with the License. You may obtain */
  11. /*  a copy of the License at                                                */
  12. /*                                                                          */
  13. /*  http://www.apache.org/licenses/LICENSE-2.0                              */
  14. /*                                                                          */
  15. /*  Unless required by applicable law or agreed to in writing, software     */
  16. /*  distributed under the License is distributed on an "AS IS" BASIS,       */
  17. /*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or         */
  18. /*  implied. See the License for the specific language governing            */
  19. /*  permissions and limitations under the License.                          */
  20. /*                                                                          */
  21. /****************************************************************************/


  22. #include "stdafx.h"
  23. #include "tchar.h"
  24. #include "DocShells.h"
  25. #include "ConsoleWindow.h"

  26. #if defined(ARX2004) || defined(ARX2005) || defined(ARX2006)
  27. #pragma comment(linker, "/export:_acrxGetApiVersion,PRIVATE")
  28. #pragma comment(linker, "/export:_acrxEntryPoint,PRIVATE")
  29. #endif

  30. #define ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))

  31. struct func_entry { ACHAR *func_name; int (*func) (struct resbuf *); };

  32. // forward references
  33. int OpenShell(resbuf * pRb);
  34. int CloseShell(resbuf * pRb);
  35. int ReadShellData(resbuf * pRb);
  36. int GetLastShellError(resbuf * pRb);
  37. int WriteShellData(resbuf * pRb);

  38. int DoFunc(void);
  39. int FuncLoad(void);

  40. // ADS available commands and function pointers
  41. static struct func_entry func_table[] = {
  42.     {_T("OpenShell"), OpenShell},
  43.     {_T("CloseShell"), CloseShell},
  44.     {_T("ReadShellData"), ReadShellData},
  45.     {_T("WriteShellData"), WriteShellData},
  46.     {_T("GetLastShellError"), GetLastShellError},   
  47. };

  48. extern "C" AcRx::AppRetCode
  49. acrxEntryPoint(AcRx::AppMsgCode msg, void * appId)
  50. {
  51.     switch(msg) {
  52. case AcRx::kInitAppMsg:
  53.     acrxDynamicLinker->unlockApplication(appId);
  54.     acrxDynamicLinker->registerAppMDIAware(appId);
  55.     break;
  56. case AcRx::kUnloadAppMsg:
  57.     // delete the single instance of CConsoleWindow
  58.     // before exiting AutoCAD.
  59.     if(g_pConsole) {
  60.         delete g_pConsole;
  61.         g_pConsole = NULL;
  62.     }
  63.     break;
  64. case AcRx::kInvkSubrMsg:
  65.     DoFunc();
  66.     break;
  67. case AcRx::kLoadDwgMsg:
  68.     FuncLoad();
  69.     }
  70.     return AcRx::kRetOK;
  71. }

  72. static int FuncLoad(void)
  73. {
  74.     for(int i = 0; i < ELEMENTS(func_table); ++i) {
  75.         if(!acedDefun(func_table.func_name, (short) i))
  76.             return RTERROR;
  77.     }
  78.     return RTNORM;
  79. }

  80. static int DoFunc(void)
  81. {
  82.     int val = acedGetFunCode();
  83.     if(val < 0 || val >= ELEMENTS(func_table)) {
  84.         acdbFail(_T("Received nonexistent function code."));
  85.         return RTERROR;
  86.     }

  87.     resbuf * pRb = acedGetArgs();
  88.     val = (*func_table[val].func)(pRb);
  89.     acutRelRb(pRb);
  90.     return val;
  91. }

  92. // Helper function for RESBUF's that contain strings
  93. int GetResBufValue(const resbuf * pRb, TString & sValue)
  94. {
  95.     if(!pRb)
  96.         return RTERROR;
  97.     if(pRb->restype != RTSTR)
  98.         return RTERROR;
  99.     sValue = pRb->resval.rstring;
  100.     return RTNORM;
  101. }

  102. // Helper function for RESBUF's that contains RTSHORT or RTLONG
  103. int GetResBufValue(const resbuf * pRb, int & nValue)
  104. {
  105.     if(!pRb)
  106.         return RTERROR;
  107.     if(pRb->restype == RTLONG) {
  108.         nValue = pRb->resval.rlong;
  109.         return RTNORM;
  110.     }
  111.     if(pRb->restype == RTSHORT) {
  112.         nValue = pRb->resval.rint;
  113.         return RTNORM;
  114.     }
  115.     return RTERROR;
  116. }


  117. /** \brief The gateway function between Autolisp and CShellPipe::OpenShell
  118. *  \param pRb a resbuf that must have 2 link that are strings
  119. *  \returns RTRSLT meaning a result is being returned.
  120. *
  121. *  When this function makes calls to any other function and the return
  122. *  value from those functions are RTERROR, then this function will
  123. *  return RTRSLT with acedRetNil(). The Autolisp function that called
  124. *  this will receive a Nil return.
  125. *
  126. *  Otherwise if every is OK (all functions called return RTNORM) then
  127. *  this function will return RTRSLT with acedRetInt() which is the
  128. *  handle associated with CShellPipe instance.
  129. *  
  130. *  The handle returned to Autolisp is used by readshelldata and
  131. *  closeshell.
  132. */
  133. static int OpenShell(resbuf * pRb)
  134. {
  135.     TCHAR * pcszApplicationName = NULL, * pcszCommandLine = NULL;
  136.     // Get the application name string
  137.     if(!pRb || pRb->restype != RTSTR) {
  138.         acedRetNil();
  139.         return RSRSLT;
  140.     }
  141.     pcszApplicationName = pRb->resval.rstring;

  142.     // get the command line string
  143.     if(!pRb->rbnext || pRb->rbnext->restype != RTSTR) {
  144.         acedRetNil();
  145.         return RSRSLT;
  146.     }
  147.     pcszCommandLine = pRb->rbnext->resval.rstring;

  148.     // Create a new instance of CShellPipe
  149.     CShellPipe * pPipe = new CShellPipe;
  150.     if(pPipe->OpenShell(pRb->resval.rstring, pRb->rbnext->resval.rstring) != RTNORM) {
  151.         delete pPipe;
  152.         acedRetNil();
  153.         return RSRSLT;
  154.     }

  155.     int nHandle = docShells.docData().AddShell(pPipe);
  156.     if(!nHandle)
  157.         acedRetNil();
  158.     else
  159.         acedRetInt(nHandle);
  160.     return RSRSLT;
  161. }


  162. /** \brief Closes a CShellPipe instance
  163. *  \param pRb a resbuf containing the handle value
  164. *  \returns RTRSLT meaning a result is being returned.
  165. *
  166. *  The pRb must be a RTSHORT or RTLONG value that is a handle
  167. *  to an associated CShellPipe to be close.

  168. *  When this function makes calls to any other function and the return
  169. *  value from those functions are RTERROR, then this function will
  170. *  return RTRSLT with acedRetNil(). The Autolisp function that called
  171. *  this will receive a Nil return.
  172. *
  173. *  Otherwise if every is OK (all functions called return RTNORM) then
  174. *  this function will return RTRSLT with acedRetT().
  175. *  
  176. *  The calling Autolisp function will receive T as a returned value.
  177. */
  178. static int CloseShell(resbuf * pRb)
  179. {
  180.     int nHandle = 0;
  181.     // get handle, bail if not a RTLONG or RTSHORT
  182.     if(GetResBufValue(pRb, nHandle) != RTNORM) {
  183.         acedRetNil();
  184.         return RSRSLT;
  185.     }
  186.     // use the handle to delete the CShellPipe associated with it
  187.     if(docShells.docData().DeleteShell(nHandle) == RTNORM)
  188.         acedRetT();
  189.     else
  190.         acedRetNil();
  191.     return RSRSLT;
  192. }


  193. /** \brief Reads data from a CShellPipe instance
  194. *  \param pRb a resbuf containing the handle value
  195. *  \returns RTRSLT meaning a result is being returned.
  196. *
  197. *  The pRb must be a RTSHORT or RTLONG value that is a handle
  198. *  to an associated CShellPipe to be read data from.

  199. *  When this function makes calls to any other function and the return
  200. *  value from those functions are RTERROR, then this function will
  201. *  return RTRSLT with acedRetNil(). The Autolisp function that called
  202. *  this will receive a Nil return.
  203. *
  204. *  Otherwise if everything is OK (all functions called return RTNORM) then
  205. *  this function will return RTRSLT with acedRetStr() that contains
  206. *  the read data.
  207. *  
  208. *  The calling Autolisp function will receive a string as a returned value,
  209. *  which is the read data.
  210. */
  211. static int ReadShellData(resbuf * pRb)
  212. {
  213.     int nHandle = 0;
  214.     // get the handle, bail if pRb is not RTLONG or RTSHORT
  215.     if(GetResBufValue(pRb, nHandle) != RTNORM) {
  216.         acedRetNil();
  217.         return RSRSLT;
  218.     }

  219.     // use the handle to get the associated CShellPipe instance.
  220.     CShellPipe * pShell = docShells.docData().GetShell(nHandle);
  221.     if(!pShell) {
  222.         acedRetNil();
  223.         return RSRSLT;
  224.     }

  225.     // read the data from the CShellPipe instance
  226.     TString sResults;
  227.     if(pShell->ReadShellData(sResults) != RTNORM) {
  228.         acedRetNil();
  229.         return RSRSLT;
  230.     }

  231.     // make the result available to the calling Autolisp function.
  232.     acedRetStr(sResults.c_str());
  233.     return RSRSLT;
  234. }

  235. /** \brief Writes data to the CShellPipe instance
  236. *  \param pRb a resbuf containing the handle value, and string to write
  237. *  \returns RTRSLT meaning a result is being returned. The calling Autolisp
  238. *  function will receive a T as a returned value if the function succeeds,
  239. *  otherwise Nil is returned
  240. *
  241. *  The pRb must be a list containing a RTSHORT or RTLONG for the first value
  242. *  that is the handle associated to a CShellPipe instance.
  243. *  The second value must be a RTSTR for the second value which is the value
  244. *  to be written to Stdin of CPipeShell.
  245. *
  246. *  When this function makes calls to any other function and the return
  247. *  value from those functions are RTERROR, then this function will
  248. *  return RTRSLT with acedRetNil(). The Autolisp function that called
  249. *  this will receive a Nil return.
  250. *
  251. *  Otherwise if everything is OK (all functions called return RTNORM) then
  252. *  this function will return RTRSLT with acedRetStr() that contains
  253. *  the read data.
  254. *  
  255. */
  256. static int WriteShellData(resbuf * pRb)
  257. {
  258.     int nHandle = 0;
  259.     // get the handle, bail if pRb is not RTLONG or RTSHORT
  260.     if(GetResBufValue(pRb, nHandle) != RTNORM) {
  261.         acedRetNil();
  262.         return RSRSLT;
  263.     }

  264.     // Get the string to send to stdio. Bail if it is NULL
  265.     // or not a string
  266.     TCHAR * pcszSendString = NULL;
  267.     if(!pRb->rbnext || pRb->rbnext->restype != RTSTR) {
  268.         acedRetNil();
  269.         return RSRSLT;
  270.     }
  271.     pcszSendString = pRb->rbnext->resval.rstring;


  272.     // use the handle to get the associated CShellPipe instance.
  273.     CShellPipe * pShell = docShells.docData().GetShell(nHandle);
  274.     if(!pShell) {
  275.         acedRetNil();
  276.         return RSRSLT;
  277.     }

  278.     // read the data from the CShellPipe instance
  279.     if(pShell->WriteShellData(pcszSendString) != RTNORM) {
  280.         acedRetNil();
  281.         return RSRSLT;
  282.     }

  283.     acedRetT();
  284.     return RSRSLT;
  285. }

  286. static int GetLastShellError(resbuf * pRb)
  287. {
  288.     TString sResult;
  289.     DWORD dwError = CShellPipe::GetLastShellError(sResult);

  290.     resbuf * pErrorRb = acutBuildList(RTLONG, dwError, RTSTR, sResult.c_str(), 0);
  291.     acedRetList(pErrorRb);
  292.     return RSRSLT;
  293. }

0.jpg 分享到百度分享




[/sell]

评分

参与人数 1D豆 +2 收起 理由
ScmTools + 2 资料分享奖!

查看全部评分

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

已领礼包: 347个

财富等级: 日进斗金

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

使用道具 举报

已领礼包: 22个

财富等级: 恭喜发财

发表于 2014-1-3 03:47:21 | 显示全部楼层
原来是VB编写的程序啊!:'(
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-15 13:28 , Processed in 0.334470 second(s), 38 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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