1. 安装 solidworks sdk api (按默认安装,编程工具应在 c 盘)
2. 在 vc6.0 中化建立 atl com app wizard
3. 添加 new atl object
4. 选择 solidworks swaddin
设置并起名
5. 得到
6. 其中
添加自定义方法 method
添加自定义命令
7. 添加 string 资源
其中 IDS_FIRST_message_METHOD 函数名与自定义 method 名应相同
8. 添加按钮
编译,会出错,更改 ------>>
在 swobj.h 中
改为,指定路径
在 part.h 中,也同样加上路径。
Rebuildall
完成,启动solidworks,在工具,插件中加载。
可以使用
9. 使用对话框
添加对话框资源
添加对话框类
在 swobj.cpp 中添加包含对话框 #include "FIRSTDLG.h"
添加 method 使用对话框
以下是代码(注意红色部分):
/* NOTE: You will need to add the -GX flag to your Project Options in Project->Settings->C/C++*/
// swobj.cpp : Implementation of Cswobj
#include "stdafx.h"
#include "Planet gear1.h"
#include "swobj.h"
#include "Modelobj.h"
#include "FIRSTDLG.h"
/
// Cswobj
void Cswobj::AddUserInterface()
{
AddToolbar();
AddMenus();
}
void Cswobj::RemoveUserInterface()
{
RemoveMenus();
RemoveToolbar();
}
void Cswobj::AddMenus()
{
long retval = 0;
VARIANT_BOOL ok;
long type;
long position;
CComBSTR menu;
CComBSTR method;
CComBSTR update;
CComBSTR hint;
// Add menu for main frame
type = swDocNONE;
position = 3;
menu.LoadString(IDS_PLANETGEAR1_MENU);
m_iSldWorks->AddMenu(type, menu, position, &retval);
position = -1;
menu.LoadString(IDS_PLANETGEAR1_START_NOTEPAD_ITEM);
method.LoadString(IDS_PLANETGEAR1_START_NOTEPAD_METHOD);
hint.LoadString(IDS_PLANETGEAR1_START_NOTEPAD_HINT);
m_iSldWorks->AddMenuItem2(type, m_swCookie, menu, position, method, update, hint, &ok);
// Add menu for part frame
type = swDocPART;
position = 5;
menu.LoadString(IDS_PLANETGEAR1_MENU);
m_iSldWorks->AddMenu(type, menu, position, &retval);
position = -1;
menu.LoadString(IDS_PLANETGEAR1_START_NOTEPAD_ITEM);
method.LoadString(IDS_PLANETGEAR1_START_NOTEPAD_METHOD);
hint.LoadString(IDS_PLANETGEAR1_START_NOTEPAD_HINT);
m_iSldWorks->AddMenuItem2(type, m_swCookie, menu, position, method, update, hint, &ok);
position = -1;
menu.LoadString(IDS_STRING_MENU_PLANETARY_ITEM);
method.LoadString(IDS_STRING_MENU_METHOD);
hint.LoadString(IDS_STRING_MENU_PLANETARY_HINT);
m_iSldWorks->AddMenuItem2(type, m_swCookie, menu, position, method, update, hint, &ok);
position = -1;
menu.LoadString(IDS_DLG_ITEM);
method.LoadString(IDS_DLG_METHOD);
hint.LoadString(IDS_DLG_HINT);
m_iSldWorks->AddMenuItem2(type, m_swCookie, menu, position, method, update, hint, &ok);
// Add menu for assembly frame
type = swDocASSEMBLY;
position = 5;
menu.LoadString(IDS_PLANETGEAR1_MENU);
m_iSldWorks->AddMenu(type, menu, position, &retval);
position = -1;
menu.LoadString(IDS_PLANETGEAR1_START_NOTEPAD_ITEM);
method.LoadString(IDS_PLANETGEAR1_START_NOTEPAD_METHOD);
hint.LoadString(IDS_PLANETGEAR1_START_NOTEPAD_HINT);
m_iSldWorks->AddMenuItem2(type, m_swCookie, menu, position, method, update, hint, &ok);
// Add menu for drawing frame
type = swDocDRAWING;
position = 5;
menu.LoadString(IDS_PLANETGEAR1_MENU);
m_iSldWorks->AddMenu(type, menu, position, &retval);
position = -1;
menu.LoadString(IDS_PLANETGEAR1_START_NOTEPAD_ITEM);
method.LoadString(IDS_PLANETGEAR1_START_NOTEPAD_METHOD);
hint.LoadString(IDS_PLANETGEAR1_START_NOTEPAD_HINT);
m_iSldWorks->AddMenuItem2(type, m_swCookie, menu, position, method, update, hint, &ok);
}
void Cswobj::RemoveMenus()
{
VARIANT_BOOL ok;
CComBSTR menu;
menu.LoadString(IDS_PLANETGEAR1_MENU);
m_iSldWorks->RemoveMenu(swDocNONE, menu, NULL, &ok);
m_iSldWorks->RemoveMenu(swDocPART, menu, NULL, &ok);
m_iSldWorks->RemoveMenu(swDocASSEMBLY, menu, NULL, &ok);
m_iSldWorks->RemoveMenu(swDocDRAWING, menu, NULL, &ok);
}
void Cswobj::AddToolbar()
{
VARIANT_BOOL stat = VARIANT_FALSE;
VARIANT_BOOL ok;
HRESULT hres;
CComBSTR title;
CComBSTR callback;
CComBSTR update;
CComBSTR tip;
CComBSTR hint;
if (m_lToolbarID == 0)
{
title.LoadString(IDS_PLANETGEAR1_TOOLBAR_TITLE);
callback.LoadString(IDS_PLANETGEAR1_TOOLBAR_CALLBACK);
update.LoadString(IDS_PLANETGEAR1_TOOLBAR_UPDATE);
tip.LoadString(IDS_PLANETGEAR1_TOOLBAR_TIP);
hint.LoadString(IDS_PLANETGEAR1_TOOLBAR_HINT);
hres = m_iSldWorks->AddToolbar3(m_swCookie, title, IDR_TOOLBAR_SMALL, IDR_TOOLBAR_LARGE, -1,
swDocTemplateTypeNONE|swDocTemplateTypePART|swDocTemplateTypeASSEMBLY|swDocTemplateTypeDRAWING, &m_lToolbarID);
m_iSldWorks->AddToolbarCommand2(m_swCookie, m_lToolbarID, 0, callback, update, tip, hint, &ok);
m_iSldWorks->AddToolbarCommand2(m_swCookie, m_lToolbarID, 1, callback, update, tip, hint, &ok);
m_iSldWorks->AddToolbarCommand2(m_swCookie, m_lToolbarID, 2, callback, update, tip, hint, &ok);
}
}
void Cswobj::RemoveToolbar()
{
if (m_lToolbarID != 0)
{
VARIANT_BOOL stat;
HRESULT hres = m_iSldWorks->RemoveToolbar2( m_swCookie, m_lToolbarID, &stat);
m_lToolbarID = 0;
}
}
/
// ISwAddin implementation
STDMETHODIMP Cswobj::ConnectToSW(IDispatch * ThisSW, LONG Cookie, VARIANT_BOOL * IsConnected)
{
VARIANT_BOOL status;
if (IsConnected == NULL)
return E_POINTER;
m_swCookie = Cookie;
m_iSldWorks = CComQIPtr
if (m_iSldWorks != NULL)
{
m_iSldWorks->SetAddinCallbackInfo((long)_Module.GetModuleInstance(), static_cast
AddUserInterface();
}
return S_OK;
}
STDMETHODIMP Cswobj::DisconnectFromSW(VARIANT_BOOL * IsDisconnected)
{
if (IsDisconnected == NULL)
return E_POINTER;
if (m_iSldWorks != NULL)
{
RemoveUserInterface();
m_iSldWorks.Release();
}
return E_NOTIMPL;
}
/
// DSldWorksEvents implementation
/
// Iswobj implementation
STDMETHODIMP Cswobj::StartNotepad()
{
// TODO: Add your implementation code here
::WinExec("Notepad.exe", SW_SHOW);
return S_OK;
}
STDMETHODIMP Cswobj::ToolbarUpdate(long *status)
{
HRESULT retval = S_OK;
*status = 1;
return S_OK;
}
STDMETHODIMP Cswobj::modeling(int)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
// TODO: Add your implementation code here
AfxMessageBox("first-hello");
return S_OK;
}
STDMETHODIMP Cswobj::model()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
// TODO: Add your implementation code here
VARIANT_BOOL False = VARIANT_FALSE;
CComPtr
CComPtr
CComPtr
CComPtr
CComPtr
m_iSldWorks->get_IActiveDoc2 (&m_pModelDoc);
m_pModelDoc->IGetActiveSketch2(&swSketch);
m_pModelDoc->CreateCircle2(0,0,0,0.02,0.05,0,&swSkSeg);
m_pFeature->FeatureExtrusion2(TRUE, FALSE, FALSE, 0, 0, 0.5, 0.01, FALSE, FALSE, FALSE, FALSE, 0.01745329251994, 0.01745329251994, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, 0, 0, FALSE, &pfeat);
return S_OK;
}
STDMETHODIMP Cswobj::FIRST()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
// TODO: Add your implementation code here
FIRSTDLG dlg;
if (dlg.DoModal()==IDOK)
{
AfxMessageBox(" 对话框 ");
}
return S_OK;
}
主要参考
[1] 王文波,涂海宁,熊星君 .Solidworks2008 二次开发基础与实例(VC++ ) . 北京: 清华大学出版社,2009.8
[2] 曹岩,方舟 .Solidworks 开发篇 . 北京: 化学工业出版社,2010.5