⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 close_delete.cpp

📁 ARX/CAD二次开发 原码集合1
💻 CPP
字号:
// close_delete.cpp : Initialization functions
#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


/////////////////////////////////////////////////////////////////////////////
// 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
	//}}AFX_ARX_INIT

	// TODO: add your initialization functions
	AddCommand("adskTest","test1","test1",ACRX_CMD_SESSION,fTest);

}

// Unload this application. Unregister all objects
// registered in InitApplication.
void UnloadApplication()
{
	// NOTE: DO NOT edit the following lines.
	//{{AFX_ARX_EXIT
	//}}AFX_ARX_EXIT

	// TODO: clean up your application
	acedRegCmds->removeGroup("adskTest");
}

// 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);
}


#include <io.h>
#include <sys/stat.h>
#include <fcntl.h>

///////////////////////////////////////////////////////////////////////////////////////////////////
//no_implementation raw_interfaces_only named_guids no_namespace 
#import "acad.tlb" no_namespace 

///////////////////////////////////////////////////////////////////////////
//function to close a drawing using ActiveX 
void fCloseDocActivex(const char *pFilename)
{

	//Initialize COM library...
	::CoInitialize(NULL);

	CLSID clsid;
	CLSIDFromProgID(L"AutoCAD.Application.15", &clsid);  

	IAcadApplicationPtr pAcadApp = NULL;	
	pAcadApp.GetActiveObject(clsid);

	IAcadDocumentsPtr pDocs = NULL;
	IAcadDocumentPtr pDoc = NULL;
	pAcadApp->get_Documents(&pDocs);
	
	long nCtr = pDocs->Count;

	//get the document we are interested in. We compare the filenames.
	for(--nCtr;nCtr >=0;nCtr--)
	{
		pDoc = pDocs->Item(_variant_t((long) nCtr));
		if (0 == strcmpi(pFilename,(char *)(_bstr_t)pDoc->FullName))break;

	}

	if(NULL != pDoc)
	{
		pDoc->Close();
	}
	
	//Cleanup COM...
	CoUninitialize(); 
}

void fTest()
{
	//get the full path + filename of the document
	char *pFilename = new char[strlen(acDocManager->curDocument()->fileName())];
	strcpy(pFilename,acDocManager->curDocument()->fileName()); 

	//close the current active document, ie. from where the command is invoked.
	fCloseDocActivex(pFilename);

	//if you wish to open a new drawing, this requires the command to be registered in SESSION context
	//acDocManager->appContextNewDocument("acad.dwt");

	//try to delete the closed file
	if(-1 == remove(pFilename))
	{
		acutPrintf("\nunable to delete : ",pFilename);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -