- UID
- 1
- 积分
- 15891
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-1-3
- 最后登录
- 1970-1-1
|
发表于 2002-4-20 03:44:59
|
显示全部楼层
定义你自己的NEW命令
- 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).
- #include "StdAfx.h"
- #include "StdArx.h"
- #include "resource.h"HINSTANCE _hdllInstance =NULL ;// This command registers an ARX command.
- void AddCommand(const char* cmdGroup, const char* cmdInt, const char* cmdLoc,
- const int cmdFlags, const AcRxFunctionPtr cmdProc, const int idLocal = -1);// NOTE: DO NOT edit the following lines.
- //{{AFX_ARX_MSG
- void InitApplication();
- void UnloadApplication();
- //}}AFX_ARX_MSG// NOTE: DO NOT edit the following lines.
- //{{AFX_ARX_ADDIN_FUNCS
- //}}AFX_ARX_ADDIN_FUNCS
- /////////// NewDwg.cpp : Initialization functions
- ////////////////////////////////////////////////////////////////////
- // DLL Entry Point
- extern "C"
- BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
- {
- if (dwReason == DLL_PROCESS_ATTACH){
- _hdllInstance = hInstance;
- }
- else if (dwReason == DLL_PROCESS_DETACH) {
- }
- return TRUE; // ok
- } /////////////////////////////////////////////////////////////////////////////
- // ObjectARX EntryPoint
- extern "C" AcRx::AppRetCode
- acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt)
- {
- switch (msg) {
- case AcRx::kInitAppMsg:
- // Comment out the following line if your
- // application should be locked into memory
- acrxDynamicLinker->unlockApplication(pkt);
- acrxDynamicLinker->registerAppMDIAware(pkt);
- InitApplication();
- break;
- case AcRx::kUnloadAppMsg:
- UnloadApplication();
- break;
- }
- return AcRx::kRetOK;
- }// Init this application. Register your
- // commands, reactors...
- void InitApplication()
- {
- // NOTE: DO NOT edit the following lines.
- //{{AFX_ARX_INIT
- AddCommand("ASDKMYNEW", "MYNEW", "MYNEW", ACRX_CMD_MODAL, mynewmynew);
- //}}AFX_ARX_INIT // TODO: add your initialization functions}// Unload this application. Unregister all objects
- // registered in InitApplication.
- void UnloadApplication()
- {
- // NOTE: DO NOT edit the following lines.
- //{{AFX_ARX_EXIT
- acedRegCmds->removeGroup("ASDKMYNEW");
- //}}AFX_ARX_EXIT // TODO: clean up your application
- }// This functions registers an ARX command.
- // It can be used to read the localized command name
- // from a string table stored in the resources.
- void AddCommand(const char* cmdGroup, const char* cmdInt, const char* cmdLoc,
- const int cmdFlags, const AcRxFunctionPtr cmdProc, const int idLocal)
- {
- char cmdLocRes[65]; // If idLocal is not -1, it's treated as an ID for
- // a string stored in the resources.
- if (idLocal != -1) { // Load strings from the string table and register the command.
- ::LoadString(_hdllInstance, idLocal, cmdLocRes, 64);
- acedRegCmds->addCommand(cmdGroup, cmdInt, cmdLocRes, cmdFlags, cmdProc); }
- else
- // idLocal is -1, so the 'hard coded'
- // localized function name is used.
- acedRegCmds->addCommand(cmdGroup, cmdInt, cmdLoc, cmdFlags, cmdProc);
- } // NewDwgCommands.cpp
- // ObjectARX defined command#include "StdAfx.h"
- #include "StdArx.h"// This is command 'MYNEW'
- void mynewmynew()
- {
- // TODO: Implement the command
- const TCHAR newFile[] = "Drawing.dwg";
- const TCHAR dwtFile[] = "Template\\Acad.dwt";
- TCHAR dwtPath [ MAX_PATH ], *pLastSlash = NULL;
- HMODULE hAcad = NULL;
- // Get the Template file/pathname by:
- // get the pathname of the AutoCAD executable
- // find the final backslash
- // copy the template directory name and the template filename (e.g."Template\\Acad.dwt")
- // onto this path after the final backslash
- // Copy this file to the destination path
- // Open this file in the AutoCAD editor
- if ( NULL == ( hAcad = ::GetModuleHandle (_T("acad.exe"))))
- acutPrintf( "\nCannot get handle to AutoCAD's executable!" );
- else if ( !::GetModuleFileName( hAcad, dwtPath, MAX_PATH ))
- acutPrintf( "\nCannot get AutoCAD's location!" );
- else if ( NULL == ( pLastSlash = strrchr( dwtPath, '\\' )))
- acutPrintf( "\nCannot extract AutoCAD's path!" );
- else if ( (pLastSlash - dwtPath) + strlen( dwtFile ) > MAX_PATH )
- acutPrintf( "\nPath too long to get template path!" );
- else {
- strcpy( pLastSlash + 1, dwtFile );
- // Do not overwrite file if it already exists (could do further checking here)
- if ( !::CopyFile( dwtPath, newFile, TRUE )){
- // Cannot copy file for one reason or another (if error 80 then file exists)
- DWORD dwErr = GetLastError();
- acutPrintf( "\nCannot copy template file, failed with error %d",dwErr );
- return;
- }
- // See solution 3615 if you need to clear the DBMOD system variable
- if ( /*Adesk::kFalse == */acedSyncFileOpen( newFile ))
- // Opening the newly copied file has failed/been cancelled
- ::DeleteFile( newFile );
- }
- }
|
|