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

📄 handleevents.cpp

📁 solidworks二次开发中
💻 CPP
字号:
// -------------------------------------------------------------------
//
//     Filename: HandleEvents.cpp
//  Description: Implementation of the event handlers for SolidWorks
//               notifications.
//
// -------------------------------------------------------------------

// To Handle events sent by a SolidWorks Object: 
//  	Derive an object like swx*****Events from CControlItem
//  	Define Functions on this object to handle the events you are interested in
//  	Put appropriate ON_EVENT lines for those events in the object's EVENTSINK map
//
// Syntax for ON_EVENT 
//
//  ON_EVENT (object, -1, swxEventID, handlerFunc, handlerFuncArgs)
//   handlerFuncArgs are specified in the API documentation for that event.
//   It is a space seperated list of arguments with appropriate types
//   defined in mfc\include\afxdisp.h file Some common ones are
//  
//   double			VTS_R8
//   long			VTS_I4
//	 boolean		VTS_BOOL
//   IDispatch*     VTS_DISPATCH
//   LPCOLESTR		VTS_WBSTR
//	 No args		VTS_NONE

#include "stdafx.h"

#include "SldWorks.h"
#include "HandleEvents.h"
#include "ExampleApp.h"

// --------------------------------
//  Application Events
// --------------------------------

BEGIN_EVENTSINK_MAP(swAppEvents, CControlItem)
	ON_EVENT (swAppEvents, (unsigned int)-1, swAppActiveModelDocChangeNotify, OnModelDocChange, VTS_NONE)
	ON_EVENT (swAppEvents, (unsigned int)-1, swAppFileOpenNotify, OnDocumentOpen, VTS_BSTR)
	ON_EVENT (swAppEvents, (unsigned int)-1, swAppFileNewNotify, OnDocumentNew, VTS_DISPATCH VTS_I4)
	ON_EVENT (swAppEvents, (unsigned int)-1, swAppDestroyNotify, OnDestroy, VTS_NONE)
END_EVENTSINK_MAP()


// Ensure that all previously opened documents have event handlers attached
//
BOOL swAppEvents::OnCreate( LPDISPATCH pDisp)
{
	BOOL retval = CControlItem::OnCreate( pDisp);

	if( !retval || pDisp == NULL)
	{
		return FALSE;
	}

	// Assume the worst
	retval = FALSE;

	LPMODELDOC pModelDoc = NULL;
	LPSLDWORKS pSldWorks = NULL;
	pDisp->QueryInterface( IID_ISldWorks, (LPVOID*) &pSldWorks);
	if( pSldWorks)
	{
		pSldWorks->IGetFirstDocument( &pModelDoc);
		pSldWorks->Release();

		retval = TRUE;
	}

	// Cycle through all of the currently open documents and add
	//  the appropriate event handler to each one.
	while( pModelDoc)
	{
		long iDocType = -1;
		pModelDoc->GetType( &iDocType);
		this->OnDocumentNew( pModelDoc, iDocType);

		// Move on to the next in the list
		LPMODELDOC pNextDoc = NULL;
		pModelDoc->IGetNext( &pNextDoc);
		pModelDoc->Release();
		pModelDoc = pNextDoc;
	}

	return retval;
}

// A new document has been actived by the user.
//
HRESULT swAppEvents::OnModelDocChange(void)
{
	// If this is the first time a document of this type has been
	// activated, add the appropriate menus.  This can occur if the
	// application is loaded after SolidWorks is running.
	TheApplication->AddMenus();

	return S_OK;
}

// An existing document has been opened.
//
HRESULT swAppEvents::OnDocumentOpen(BSTR docName)
{
	// make sure a document of this type has menus
	TheApplication->AddMenus();

	HRESULT hres;
	LPSLDWORKS pSldWorks = NULL;
	
	hres = m_lpObject->QueryInterface(IID_ISldWorks, (void**)&pSldWorks);
	if(!pSldWorks)
		return S_OK;

	LPMODELDOC pActiveDoc = NULL;
	hres = pSldWorks->get_IActiveDoc(&pActiveDoc);

	pSldWorks->Release();

	if(!pActiveDoc)
		return S_OK;

	CObList& listEvents = TheApplication->GetEventList();

	BOOL fFound = FALSE;
	CControlItem* pItem = NULL;
	POSITION pos = listEvents.GetHeadPosition();
	while (pos)
	{
		pItem = (CControlItem*)listEvents.GetNext(pos);
		if(pItem && pItem->IsKindOf(RUNTIME_CLASS(swDocumentEvents)))
		{
			if(((swDocumentEvents*)pItem)->FoundInterface(pActiveDoc))
			{
				fFound = TRUE;
				break;
			}
		}
	}

	if(!fFound)
	{
		long DocType;
		pActiveDoc->GetType(&DocType);
		OnDocumentNew(pActiveDoc, DocType);		// Set up events for opened doc.
	}

	pActiveDoc->Release();

	return S_OK;
}

// A new document has been created.
//
HRESULT swAppEvents::OnDocumentNew(LPDISPATCH NewDoc, long DocType)
{
	HRESULT hres;

	LPMODELDOC pModelDoc = NULL;
	hres = NewDoc->QueryInterface(IID_IModelDoc, (void**)&pModelDoc);
	ASSERT(hres == S_OK && pModelDoc != NULL);

	// Create a control item for the new document
	if(DocType == swDocPART)
	{
		swPartEvents* partEvent;
		partEvent = new swPartEvents;
		partEvent->OnCreate(pModelDoc);
	}

	// clean up
	pModelDoc->Release();

	return S_OK;
}

// SolidWorks is shutting down and will destroy this applications
// menus, notification and save-as items
//
HRESULT swAppEvents::OnDestroy(void)
{
	delete  this;
	TheApplication->SetSWApp(NULL);

	return S_OK;
}

// --------------------------------
//  Document Events
// --------------------------------
IMPLEMENT_SERIAL(swDocumentEvents, CCmdTarget, 1)
BEGIN_EVENTSINK_MAP(swDocumentEvents, CControlItem)
END_EVENTSINK_MAP()

swDocumentEvents::~swDocumentEvents()
{
	HRESULT hres;

	// inform CHighlightApp that this document has been destroyed

	LPMODELDOC pModelDoc = NULL;
	hres = m_lpObject->QueryInterface(IID_IModelDoc, (void**)&pModelDoc);

	TheApplication->DestroyDoc(pModelDoc);
	pModelDoc->Release();
}

BOOL swDocumentEvents::FoundInterface(LPMODELDOC pDoc)
{
	LPMODELDOC pModelDoc = NULL;
	m_lpObject->QueryInterface(IID_IModelDoc, (void**)&pModelDoc);
	if(pModelDoc && (pDoc == pModelDoc))
	{
		pModelDoc->Release();
		return TRUE;
	}
	return FALSE;
}

void swDocumentEvents::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

void swDocumentEvents::GetStreamName(CString& strStream)
{
	strStream = _T("HighlightDoc");
}

HRESULT swDocumentEvents::OnLoadFromStorage()
{
	BOOL retval = TRUE;

	if(m_bLoaded != FALSE)
	{
		// This check is required because,
		// solid sends this notification even if
		// have already loaded our document.
		// This inturn will cause components data loss.
		return S_OK;
	}
	m_bLoaded = TRUE;

	HRESULT res;
	LPUNKNOWN pUnk = NULL;
	CString strStorage;
	GetStreamName(strStorage);
	BSTR stringIn = strStorage.AllocSysString();

	LPMODELDOC pModelDoc = NULL;
	res = m_lpObject->QueryInterface(IID_IModelDoc, (void**)&pModelDoc);
	if( res == S_OK && pModelDoc)
	{
		// Obtain the stream for loading data
		res = pModelDoc->IGet3rdPartyStorage(stringIn, FALSE, &pUnk);

		LPSTREAM pStream = NULL;
		if( res == S_OK && pUnk)
		{
			res = pUnk->QueryInterface( IID_IStream, (LPVOID *)&pStream);
			ASSERT( res == S_OK);
			pUnk->Release();
			pUnk = NULL;
		}
		else
		{
			retval = FALSE;
		}

		if( pStream)
		{
			// Read the application data from the stream
			COleStreamFile file(pStream);
			CArchive ar(&file, CArchive::load);

			Serialize(ar);

			pStream->Release();
		}
		else
		{
			retval = FALSE;
		}

		// Release the storage regardless of the success of the Get operation
		pModelDoc->IRelease3rdPartyStorage( stringIn);

		pModelDoc->Release();
	}
	else
	{
		retval = FALSE;
	}

	::SysFreeString( stringIn);

	if( retval)
		return S_OK;
	else
		return E_FAIL;
}

HRESULT swDocumentEvents::OnSaveToStorage()
{
	BOOL retval = TRUE;
	HRESULT res;
	LPUNKNOWN pUnk = NULL;
	CString strStorage;
	GetStreamName(strStorage);
	BSTR stringIn = strStorage.AllocSysString();

	LPMODELDOC pModelDoc = NULL;
	res = m_lpObject->QueryInterface(IID_IModelDoc, (void**)&pModelDoc);
	if( res == S_OK && pModelDoc)
	{
		// Obtain the stream for storing data
		res = pModelDoc->IGet3rdPartyStorage(stringIn, TRUE, &pUnk);

		LPSTREAM pStream = NULL;
		if( res == S_OK && pUnk)
		{
			res = pUnk->QueryInterface(IID_IStream, (LPVOID *)&pStream);
			ASSERT(res == S_OK);
			pUnk->Release();
			pUnk = NULL;
		}
		else
		{
			retval = FALSE;
		}

		if(pStream)
		{
			// Write the application data to the stream
			COleStreamFile file(pStream);
			CArchive ar(&file, CArchive::store);

			Serialize(ar);

			pStream->Release();
		}

		// Release the storage regardless of the success of the Get operation
		pModelDoc->IRelease3rdPartyStorage( stringIn);

		pModelDoc->Release();
	}
	else
	{
		retval = FALSE;
	}

	::SysFreeString( stringIn);

	if( retval)
		return S_OK;
	else
		return E_FAIL;
}


HRESULT swDocumentEvents::OnDestroy(void)
{
	delete this;
	return S_OK;
}

// --------------------------------
//  Part Events
// --------------------------------

IMPLEMENT_SERIAL(swPartEvents, swDocumentEvents, 1)
BEGIN_EVENTSINK_MAP(swPartEvents, swDocumentEvents)
	ON_EVENT (swPartEvents, (unsigned int)-1, swPartDestroyNotify, OnDestroy, VTS_NONE)
	ON_EVENT (swPartEvents, (unsigned int)-1, swPartLoadFromStorageNotify, OnLoadFromStorage, VTS_NONE)
	ON_EVENT (swPartEvents, (unsigned int)-1, swPartSaveToStorageNotify, OnSaveToStorage, VTS_NONE)
END_EVENTSINK_MAP()

void swPartEvents::Serialize(CArchive& ar)
{
	swDocumentEvents::Serialize(ar);

	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

void swPartEvents::GetStreamName(CString& strStream)
{
	strStream = _T("HighlightPart");
}

// --------------------------------
//  End of HandleEvents.cpp
// --------------------------------

⌨️ 快捷键说明

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