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

📄 exampleapp.cpp

📁 solidworks二次开发中
💻 CPP
字号:
// -------------------------------------------------------------------
//
//     Filename: ExampleApp.cpp
//  Description: Implements the CHighlightApp class which is the
//               basis for the entire example application.
//
// -------------------------------------------------------------------

#include <stdafx.h>

#include "Resource.h"
#include "ExampleApp.h"
#include "HandleEvents.h"
#include <mbstring.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// --------------------------------
//  Constructor/Destructor
// --------------------------------

// the constructor and destructor are only called once per session
//
CHighlightApp::CHighlightApp(LPSLDWORKS pSldWorks)
{
	// application
	m_pSldWorks = pSldWorks;
	m_pActiveDoc = NULL;

	// menus
	m_bPartMenu = FALSE;
	m_bAssemblyMenu = FALSE;
	m_bDrawingMenu = FALSE;

	m_hSaveInst = NULL;

}

CHighlightApp::~CHighlightApp()
{
	// default destructor
}

// --------------------------------
//  Functionality
// --------------------------------

// --------------------------------
//  Load/Unload
// --------------------------------

bool CHighlightApp::StartApplication(void)
{
	// add menus to the active document
	AddMenus();

	//Add toolbars
	AddToolbars();

	// create a control item to handle application-level events
	swAppEvents* eventApp = new swAppEvents;
	eventApp->OnCreate(m_pSldWorks);

	return TRUE;
}

void CHighlightApp::TerminateApplication(void)
{
	// need this test because SW can terminate the app by
	// shutting down or by unloading it from the Add-In
	if (m_pSldWorks == NULL)
		return;

	// remove all menus
	RemoveMenus();

	//remove the toolbars
	RemoveToolbars();

	// remove events - the event destructor removes
	// the object from the list
	int count = m_EventList.GetCount();
	for (int i=0; i<count; i++)
	{
		 CObject* headEvent = m_EventList.GetHead();
		 delete headEvent;			// destructor will remove it from list with RemoveAt(pos)
	}

	if (m_pActiveDoc != NULL)
		m_pActiveDoc->Release();

	// disconnect from SolidWorks
	m_pSldWorks->Release();
	m_pSldWorks = NULL;
}

// --------------------------------
//  menus
// --------------------------------

#define swMenuPosition  5	// add your menu after Tools and before Window
#define swLastPosition -1

//  Add a menu to the active document frame.  This is to be done
//  only ONCE for each document type.
//  
void CHighlightApp::AddMenus(void)
{
	HRESULT res;

	LPMODELDOC pModelDoc = NULL;
	res = m_pSldWorks->get_IActiveDoc(&pModelDoc);

	if (pModelDoc != NULL)
	{
		// get the document type
		long doctype;
		res = pModelDoc->GetType(&doctype);

		if (doctype == swDocPART && m_bPartMenu == NULL)
		{
			docCreateMenu(doctype);
			m_bPartMenu = TRUE;
		}
		else if (doctype == swDocASSEMBLY && m_bAssemblyMenu == NULL)
		{
			docCreateMenu(doctype);
			m_bAssemblyMenu = TRUE;
		}
		else if (doctype == swDocDRAWING && m_bDrawingMenu == NULL)
		{
			docCreateMenu(doctype);
			m_bDrawingMenu = TRUE;
		}
		else
			return;

		// clean up
		pModelDoc->Release();
	}
}

// adds a menu and save-as item to the currently active document type
//
void CHighlightApp::docCreateMenu(long DocumentType)
{
	HRESULT hres;
	VARIANT_BOOL bres;

	LPFRAME pFrame;
	hres = m_pSldWorks->IFrameObject(&pFrame);

	// add menu
	hres = pFrame->AddMenu(auT("highlight &App"), swMenuPosition, &bres);


	// add menu item
	hres = pFrame->AddMenuItem(
					auT("highlight &App"), auT("&highlight Menu Item"), swLastPosition,
					auT("highlight@MenuItemCB, highlight application menu item"),
					&bres);

	// add file save-as item
	hres = m_pSldWorks->AddFileSaveAsItem(auT("highlight@SaveAsCB"),
					auT("xampl\nhighlight save (*.xampl)"), DocumentType, &bres);

	// clean up
	pFrame->Release();
}

void CHighlightApp::RemoveMenus(void)
{
	// remove menus and save-as items from each document type
	if (m_bPartMenu)
		docRemoveMenu(swDocPART);

	if (m_bAssemblyMenu)
		docRemoveMenu(swDocASSEMBLY);

	if (m_bDrawingMenu)
		docRemoveMenu(swDocDRAWING);
}

// removes a menu and save-as item to the currently active document type
//
void CHighlightApp::docRemoveMenu(long DocumentType)
{
	HRESULT      hres;
	VARIANT_BOOL bres;

	// remove menu
	hres = m_pSldWorks->RemoveMenu(DocumentType, auT("highlight &App"), NULL, &bres);

	// remove save-as item
	hres = m_pSldWorks->RemoveFileSaveAsItem(auT("highlight@SaveAsCB"),
					auT("xampl\nExample save (*.xampl)"), DocumentType, &bres);
}

//Adds a Toolbar to the document
void CHighlightApp::AddToolbars() 
{

	HRESULT hres;
	VARIANT_BOOL retval;

	// Local function to make sure we are using the Add-In resources
	SetResources();

	// Load the Toolbar bitmaps
	m_SmallToolbar.LoadMappedBitmap(IDR_TOOLBAR_SMALL);
	HBITMAP hbmSmallImageWell = (HBITMAP)m_SmallToolbar.GetSafeHandle();

	m_LargeToolbar.LoadMappedBitmap(IDR_TOOLBAR_LARGE);
	HBITMAP hbmLargeImageWell = (HBITMAP)m_LargeToolbar.GetSafeHandle();

	// Local function to reset resources to SolidWorks
	ResetResources();  

	//The type document templates in which this toolbar will be available
	long DocumentType = swDocTemplateTypeNONE |
						swDocTemplateTypePART | 
						swDocTemplateTypeASSEMBLY | 
						swDocTemplateTypeDRAWING;

	//Add the toolbar to SolidWorks
	hres = m_pSldWorks->AddToolbar2(auT("highlight"), auT("Sample Toolbar"), (long)hbmSmallImageWell, 
								    (long)hbmLargeImageWell, -1, DocumentType, &m_ToolbarId);

	//Add Commands
	if(hres==S_OK && m_ToolbarId)
	{
		//First Command
		hres = m_pSldWorks->AddToolbarCommand(auT("highlight"), m_ToolbarId, 0, 
			auT("ToolbarCB0@updateButton,Button 0\nButton0 tooltip"), &retval);
 
		//Second Command
		hres = m_pSldWorks->AddToolbarCommand(auT("highlight"), m_ToolbarId, 1, 
			auT("ToolbarCB1@updateButton,Button 1\nButton1 tooltip"), &retval);

		//Third Command
		hres = m_pSldWorks->AddToolbarCommand(auT("highlight"), m_ToolbarId, 2, 
			auT("ToolbarCB2@updateButton,Button 2\nButton2 tooltip"), &retval);


		m_pSldWorks->ShowToolbar ( auT("highlight"), m_ToolbarId, &retval );
	}
}


void CHighlightApp::RemoveToolbars()
{
	VARIANT_BOOL retval;
	HRESULT hres;

	if( m_ToolbarId) {
		hres = m_pSldWorks->RemoveToolbar( auT("highlight"), m_ToolbarId, &retval );
	}

}

// --------------------------------
//  Events
// --------------------------------

void CHighlightApp::AddEvent(CObject* swEventHandler)
{
	// add to the object list
	m_EventList.AddTail(swEventHandler);
}

bool CHighlightApp::RemoveEvent(CObject* swEventHandler)
{
	POSITION pos;

	// remove from the object list
	pos = m_EventList.Find(swEventHandler);
	if (pos == 0)
		return FALSE;

	m_EventList.RemoveAt(pos);
	return TRUE;
}

// --------------------------------
//  helper functions
// --------------------------------

void CHighlightApp::SetResources()
{
	// Save the original SolidWorks resource instance handle
	// replacing it with the instance handle of the highlight.dll.
	if( m_hSaveInst == NULL)
	{
		m_hSaveInst = ::AfxGetResourceHandle();
		::AfxSetResourceHandle( ::AfxGetInstanceHandle());
	}
}

void CHighlightApp::ResetResources()
{
	// Reset the SolidWorks resource instance handle
	if( m_hSaveInst != NULL)
	{
		::AfxSetResourceHandle( m_hSaveInst);
		m_hSaveInst = NULL;
	}
}

void CHighlightApp::ChangeActiveDoc(void)
{
	// release the refence count on the previously active document
	if (m_pActiveDoc != NULL)
		m_pActiveDoc->Release();

	// Set the new active document
	HRESULT res;
	res = m_pSldWorks->get_IActiveDoc(&m_pActiveDoc);
}

void CHighlightApp::DestroyDoc(LPMODELDOC DocBeingDestroyed)
{
	// Determine if the part being destroyed was the active document.
	// If so, release a reference to it, and set m_pActiveDoc to NULL
	if (DocBeingDestroyed == m_pActiveDoc)
	{
		m_pActiveDoc->Release();
		m_pActiveDoc = NULL;
	}
}

void CHighlightApp::SetSWApp(LPSLDWORKS pSldWorks)
{
	m_pSldWorks = pSldWorks;
}



// --------------------------------
//  End of ExampleApp.cpp
// --------------------------------

⌨️ 快捷键说明

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