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

📄 highlight.cpp

📁 solidworks二次开发中
💻 CPP
字号:
// -------------------------------------------------------------------
//
//     Filename: Highlight.cpp
//  Description: This defines the interface to SolidWorks and the
//               initialization routines for the DLL.
//
// -------------------------------------------------------------------

// pre-compiled headers
#include "stdafx.h"

// MFC includes
#include <afxdllx.h>
#include <math.h>

#include "highlight.h"
#include "SldWorks.h"
#include "ExampleApp.h"
#include <tchar.h>
#include "sldworks_i.c"		// The SolidWorks interface IIDs
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// the application object
CHighlightApp* TheApplication = NULL;
CDynLinkLibrary *pDynLinkLibrary = NULL;

//The instance of this DLL
HINSTANCE g_hinstDll;

// the array that contains the registry information
const char *g_RegTable[][3] = {
//Format is {key, value name, value}
	{"SOFTWARE\\SolidWorks\\Applications\\highlight", 0, "{be4dbf4e-3ce2-444a-966c-a469b5c3fff1}"},
	{"SOFTWARE\\SolidWorks\\Applications\\highlight\\CLSID", 0, "{be4dbf4e-3ce2-444a-966c-a469b5c3fff1}"},
	{"CLSID\\{be4dbf4e-3ce2-444a-966c-a469b5c3fff1}", 0, "highlight"},
	{"CLSID\\{be4dbf4e-3ce2-444a-966c-a469b5c3fff1}\\InprocServer32", 0, (const char*)-1}, //indicates file path
};

static AFX_EXTENSION_MODULE HighlightDLL = { NULL, NULL };

extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
	if (dwReason == DLL_PROCESS_ATTACH)
	{
		//Get an instance of this DLL for Self-Registration
		g_hinstDll = hInstance;
		TRACE0("Highlight.DLL Initializing!\n");
		
		// Extension DLL one-time initialization
		AfxInitExtensionModule(HighlightDLL, hInstance);

		// Insert this DLL into the resource chain
		new CDynLinkLibrary(HighlightDLL);
		pDynLinkLibrary = new CDynLinkLibrary(HighlightDLL);

	}
	else if (dwReason == DLL_PROCESS_DETACH)
	{
		TRACE0("Highlight.DLL Terminating!\n");

		// shut down the sample application
		TheApplication->TerminateApplication();
		delete TheApplication;

		// Terminate the library before destructors are called
		delete pDynLinkLibrary;
		AfxTermExtensionModule(HighlightDLL);
	}
	return 1;   // ok
}

// --------------------------------
//  connections to SolidWorks
// -------------------------------- 

// this is the function SolidWorks calls to connect
// to the add-in application

long InitUserDLL3(LPSLDWORKS pSldWorks)
{
	// this function should be called once per session
	if ( TheApplication != NULL )
	{
		ASSERT ( FALSE );
		return 0;
	}

	// start the application
	TheApplication = new CHighlightApp(pSldWorks);
	if (!TheApplication->StartApplication())
		return 0;

	// connected successfully
	return 1;
}

// document callbacks

void MenuItemCB(void)
{
	LPMODELDOC pModelDoc;
	TheApplication->GetSWApp()->get_IActiveDoc(&pModelDoc);
	LPPARTDOC pPartDoc;
	pModelDoc->QueryInterface(IID_IPartDoc,(LPVOID*)&pPartDoc);
	VARIANT_BOOL retval;
	pModelDoc->SelectByID(auT("前视"),auT("PLANE"),0,0,0,&retval);
	pModelDoc->InsertSketch();
	double p1[3];
	p1[0]=0;
	p1[1]=0;
	p1[2]=0;
	double p2[3];
	p2[0]=0;
	p2[1]=-0.1;
	p2[2]=0;
	pModelDoc->ICreateCenterLine(p1,p2);
	pModelDoc->ClearSelection();
	LPSKETCHSEGMENT retval2;
	pModelDoc->ICreateLine2(0,0,0,   0.03,-0.09,0,  &retval2);
	pModelDoc->ICreateLine2(0.03,-0.09,0,  0,-0.09,0,  &retval2);
	pModelDoc->ICreateLine2(0,-0.09,0,  0,0,0,  &retval2);
	pModelDoc->InsertSketch();
	VARIANT_BOOL retvala;
	VARIANT_BOOL retvalb;
	VARIANT_BOOL retvalc;
	VARIANT_BOOL retvald;
	pModelDoc->SelectByID(auT("直线2"),auT("SKETCHSEGMENT"),0.01953083815029,-0.05859251445087,0,&retvala);
	pModelDoc->SelectByID(auT("Line3"), auT("SKETCHSEGMENT"),0.02, -0.09,0,&retvalb); 
	pModelDoc->SelectByID(auT("Line4"), auT("SKETCHSEGMENT"),0,-0.06,0,&retvalc); 
	pModelDoc->SelectByID(auT("Point2"), auT("SKETCHPOINT"),0,-0.1,0,&retvald);
	pPartDoc->FeatureRevolve (6.28318530718, 0, 0, 0);
	pModelDoc->BlankSketch ( );//清空所选的草图
	pModelDoc->ShowNamedView2(auT("*Isometric"),7);//显示等轴测视区
	pModelDoc->ViewZoomtofit2();//自动缩放以整屏显示全图
	pModelDoc->Release();//释放pModelDoc指针
	pPartDoc->Release();//释放pPartDoc指针	
		



return;
}


//Toolbar callbacks
void ToolbarCB0(void)
{
	long retval;
	TheApplication->GetSWApp()->SendMsgToUser2(auT("Button 0 Pressed"), swMbInformation, swMbOk, &retval);
	return;
}

void ToolbarCB1(void)
{
	long retval;
	TheApplication->GetSWApp()->SendMsgToUser2(auT("Button 1 Pressed"), swMbInformation, swMbOk, &retval);
	return;
}

void ToolbarCB2(void)
{
	long retval;
	TheApplication->GetSWApp()->SendMsgToUser2(auT("Button 2 Pressed"), swMbInformation, swMbOk, &retval);
	return;
}

long updateButton(void)
{
	long retval = 1;
	return retval;
}

void DllExport SaveAsCB(LPTSTR FileName, LPSLDWORKS pSolidWorks)
{
    return;
}

//Register the DLL

STDAPI DllRegisterServer(void) {
	HRESULT hr = S_OK;
	//Look up the server's file name
	char szFileName[MAX_PATH];
	GetModuleFileNameA(g_hinstDll, szFileName, MAX_PATH);
	//register entries from table
	int nEntries = sizeof(g_RegTable)/sizeof(*g_RegTable);
	for (int i = 0; SUCCEEDED(hr) && i < nEntries; i++) {
		const char *pszKeyName = g_RegTable[i][0];
		const char *pszValueName = g_RegTable[i][1];
		const char *pszValue = g_RegTable[i][2];
		//map rogue value to module file name
		if (pszValue == (const char*)-1)
			pszValue = szFileName;
		HKEY hkey;

		//create the key
		long err = ERROR_SUCCESS;
		if (i <= 1) { // the first keys are in HKEY_LOCAL_MACHINE
			 err = RegCreateKeyA(HKEY_LOCAL_MACHINE, pszKeyName, &hkey);
		} else { //The rest go into HKEY_CLASSES_ROOT
			 err = RegCreateKeyA(HKEY_CLASSES_ROOT, pszKeyName, &hkey);
		}

		if (err == ERROR_SUCCESS) {
			//set the value
			err = RegSetValueExA(hkey, pszValueName, 0, REG_SZ, (const BYTE*)pszValue, (strlen(pszValue)+1));
			RegCloseKey(hkey);
		}
		if (err != ERROR_SUCCESS) {
			//if cannot add key or value, back out and fail
			DllUnregisterServer();
			hr = SELFREG_E_CLASS;
		}
	}
	return hr;
}

//Unregister the DLL
STDAPI DllUnregisterServer(void) {
	HRESULT hr = S_OK;
	int nEntries = sizeof(g_RegTable)/sizeof(*g_RegTable);
	for (int i = nEntries - 1; i >= 0; i--) {

		const char *pszKeyName = g_RegTable[i][0];
		long err = ERROR_SUCCESS;

		if (i <= 1) { //The last keys are in HKEY_LOCAL_MACHINE
			err = RegDeleteKeyA(HKEY_LOCAL_MACHINE, pszKeyName);
		} else { //Remove the rest from HKEY_CLASSES_ROOT
			err = RegDeleteKeyA(HKEY_CLASSES_ROOT, pszKeyName);
		}

		if (err != ERROR_SUCCESS)
			hr = S_FALSE;
	}
	return hr;
}


// --------------------------------
//  End of Highlight.cpp
// --------------------------------

⌨️ 快捷键说明

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