找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 826|回复: 5

[每日一码] 如何创建自己的NEW命令

[复制链接]

已领礼包: 13个

财富等级: 恭喜发财

发表于 2016-9-11 09:15:01 | 显示全部楼层 |阅读模式

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

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

×
Question
How can I write my own NEW command without using acedCommand()?

Answer
There is no API exposed directly for the NEW command, but you can simulate it
fairly closely. The idea is to copy the DWT file you want to use as a template
into your destination file (for example, "Drawing.dwg"), and then open it using
acedSyncFileOpen().

The disadvantage of this approach is that the drawing will have a file name
(if you create a NEW drawing normally, it will actually perform a SAVEAS when
you first have to SAVE it - this new way of defining your NEW command means
the drawing has a name when it is opened).


[it618postdisplay>0]
  1. // NewDwg.cpp : Initialization functions
  2. #include "StdAfx.h"
  3. #include "StdArx.h"
  4. #include "resource.h"HINSTANCE _hdllInstance =NULL ;// This command registers an ARX command.
  5. void AddCommand(const char* cmdGroup, const char* cmdInt, const char* cmdLoc,
  6.     const int cmdFlags, const AcRxFunctionPtr cmdProc, const int idLocal = -1);// NOTE: DO NOT edit the following lines.
  7. //{{AFX_ARX_MSG
  8. void InitApplication();
  9. void UnloadApplication();
  10. //}}AFX_ARX_MSG// NOTE: DO NOT edit the following lines.
  11. //{{AFX_ARX_ADDIN_FUNCS
  12. //}}AFX_ARX_ADDIN_FUNCS
  13. /////////////////////////////////////////////////////////////////////////////
  14. // DLL Entry Point
  15. extern "C"
  16. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
  17. {
  18.    if (dwReason == DLL_PROCESS_ATTACH){
  19.       _hdllInstance = hInstance;
  20.    }
  21.    else if (dwReason == DLL_PROCESS_DETACH) {
  22.    }
  23.    return TRUE;    // ok
  24. } /////////////////////////////////////////////////////////////////////////////
  25. // ObjectARX EntryPoint
  26. extern "C" AcRx::AppRetCode
  27. acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt)
  28. {
  29.    switch (msg) {
  30.       case AcRx::kInitAppMsg:
  31.          // Comment out the following line if your
  32.          // application should be locked into memory
  33.          acrxDynamicLinker->unlockApplication(pkt);
  34.          acrxDynamicLinker->registerAppMDIAware(pkt);
  35.          InitApplication();
  36.          break;
  37.       case AcRx::kUnloadAppMsg:
  38.          UnloadApplication();
  39.          break;
  40.    }
  41.    return AcRx::kRetOK;
  42. }// Init this application. Register your
  43. // commands, reactors...
  44. void InitApplication()
  45. {
  46.    // NOTE: DO NOT edit the following lines.
  47.    //{{AFX_ARX_INIT
  48.    AddCommand("ASDKMYNEW", "MYNEW", "MYNEW", ACRX_CMD_MODAL, mynewmynew);
  49.    //}}AFX_ARX_INIT   // TODO: add your initialization functions}// Unload this application. Unregister all objects
  50. // registered in InitApplication.
  51. void UnloadApplication()
  52. {
  53.    // NOTE: DO NOT edit the following lines.
  54.    //{{AFX_ARX_EXIT
  55.    acedRegCmds->removeGroup("ASDKMYNEW");
  56.    //}}AFX_ARX_EXIT   // TODO: clean up your application
  57. }// This functions registers an ARX command.
  58. // It can be used to read the localized command name
  59. // from a string table stored in the resources.
  60. void AddCommand(const char* cmdGroup, const char* cmdInt, const char* cmdLoc,
  61.   const int cmdFlags, const AcRxFunctionPtr cmdProc, const int idLocal)
  62. {
  63.    char cmdLocRes[65];   // If idLocal is not -1, it's treated as an ID for
  64.    // a string stored in the resources.
  65.    if (idLocal != -1) {      // Load strings from the string table and register the command.
  66.       ::LoadString(_hdllInstance, idLocal, cmdLocRes, 64);
  67.       acedRegCmds->addCommand(cmdGroup, cmdInt, cmdLocRes, cmdFlags, cmdProc);   }
  68.    else
  69.       // idLocal is -1, so the 'hard coded'
  70.       // localized function name is used.
  71.       acedRegCmds->addCommand(cmdGroup, cmdInt, cmdLoc, cmdFlags, cmdProc);
  72. } // NewDwgCommands.cpp
  73. // ObjectARX defined command#include "StdAfx.h"
  74. #include "StdArx.h"// This is command 'MYNEW'

[/it618postdisplay]
  1. void mynewmynew()
  2. {
  3.    // TODO: Implement the command
  4.    const TCHAR newFile[] = "Drawing.dwg";
  5.    const TCHAR dwtFile[] = "Template\\Acad.dwt";
  6.    TCHAR dwtPath [ MAX_PATH ], *pLastSlash = NULL;
  7.    HMODULE hAcad = NULL;
  8.    // Get the Template file/pathname by:
  9.    //     get the pathname of the AutoCAD executable
  10.    //     find the final backslash
  11.    //     copy the template directory name and the template filename (e.g."Template\\Acad.dwt")
  12.    //       onto this path after the final backslash
  13.    // Copy this file to the destination path
  14.    // Open this file in the AutoCAD editor
  15.    if ( NULL == ( hAcad = ::GetModuleHandle (_T("acad.exe"))))
  16.       acutPrintf( "\nCannot get handle to AutoCAD's executable!" );
  17.    else if ( !::GetModuleFileName( hAcad, dwtPath, MAX_PATH ))
  18.       acutPrintf( "\nCannot get AutoCAD's location!" );
  19.    else if ( NULL == ( pLastSlash = strrchr( dwtPath, '\\' )))
  20.       acutPrintf( "\nCannot extract AutoCAD's path!" );
  21.    else if ( (pLastSlash - dwtPath) + strlen( dwtFile ) > MAX_PATH )
  22.       acutPrintf( "\nPath too long to get template path!" );
  23.    else {
  24.       strcpy( pLastSlash + 1, dwtFile );
  25.       // Do not overwrite file if it already exists (could do further checking here)
  26.       if ( !::CopyFile( dwtPath, newFile, TRUE )){
  27.          // Cannot copy file for one reason or another (if error 80 then file exists)
  28.          DWORD dwErr = GetLastError();
  29.          acutPrintf( "\nCannot copy template file, failed with error %d",dwErr );
  30.          return;
  31.       }
  32.       // See solution 3615 if you need to clear the DBMOD system variable
  33.       if ( /*Adesk::kFalse == */acedSyncFileOpen( newFile ))
  34.          // Opening the newly copied file has failed/been cancelled
  35.          ::DeleteFile( newFile );
  36.    }
  37. }




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

已领礼包: 1957个

财富等级: 堆金积玉

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

使用道具 举报

已领礼包: 35个

财富等级: 招财进宝

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

使用道具 举报

已领礼包: 8个

财富等级: 恭喜发财

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

使用道具 举报

已领礼包: 2963个

财富等级: 家财万贯

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

使用道具 举报

已领礼包: 60个

财富等级: 招财进宝

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-24 20:22 , Processed in 0.189559 second(s), 38 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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