solidworks二次开发全教程系列.docx

上传人:b****6 文档编号:6080232 上传时间:2023-01-03 格式:DOCX 页数:35 大小:30.33KB
下载 相关 举报
solidworks二次开发全教程系列.docx_第1页
第1页 / 共35页
solidworks二次开发全教程系列.docx_第2页
第2页 / 共35页
solidworks二次开发全教程系列.docx_第3页
第3页 / 共35页
solidworks二次开发全教程系列.docx_第4页
第4页 / 共35页
solidworks二次开发全教程系列.docx_第5页
第5页 / 共35页
点击查看更多>>
下载资源
资源描述

solidworks二次开发全教程系列.docx

《solidworks二次开发全教程系列.docx》由会员分享,可在线阅读,更多相关《solidworks二次开发全教程系列.docx(35页珍藏版)》请在冰豆网上搜索。

solidworks二次开发全教程系列.docx

solidworks二次开发全教程系列

solidworks二次开发全教程系列

solidworks二次开发-01-录制一个宏

第一步:

我们需要自己录制一个宏,然后看看程序产生了什么代码。

当初学习excel时候就是这么干的。

只是,solidworks要复杂一些,直接录制的宏不能使用,需要做一些调整。

在没有经验的时候我们最好按照下面的建议来做。

EditorDebugSolidWorksMacro

EditordebugSolidWorksmacrosusingMicrosoftVBA.使用MicrosoftVBA编辑或调试宏

ToeditordebugaSolidWorksmacro:

ClickEditMacroontheMacrotoolbar,orclickTools,Macro,Edit.

NOTES:

注意:

Toautomaticallyeditamacroafterrecordingit,clickTools,Options,SystemsOptions.OntheGeneraltab,selectAutomaticallyeditmacroafterrecordingandclickOK.ThissettingispersistentacrossSolidWorkssessions.

此选项Automaticallyeditmacroafterrecording顾名思义是在记录宏完毕后自动打开编辑界面。

Ifyourecentlyeditedthemacro,youcanselectitfromthemenuwhenyouclickTools,Macro.Thismenuliststhelastninemacrosthatyouedited.

已经编辑了宏,菜单中会有最近的9个宏程序列表供选择。

Inthedialogbox,selectamacrofile(.swp)andclickOpen.选择一个宏swp文件

NOTE:

Youcanalsoedit.swbfiles,whichareolder-styleSolidWorksmacrofiles.Whenyourunoredita.swbfile,itisautomaticallyconvertedtoa.swpfile.旧的宏文件后缀为swb,你也可以打开swb,那么会自动保存为swp。

Editordebugthemacro.Ifitisanewmacro,besureto:

如果是新的宏

Deleteextralinesofcode:

删除一些多余的代码:

ThefollowingvariablesaredeclaredautomaticallyinaSolidWorksmacro.Deleteanyvariablesnotusedinthemacro.这些对象的声明是自动产生的,可以将没用的删除DimswAppAsObject

DimPartAsObject

DimboolstatusAsBoolean

DimlongstatusAsLong,longwarningsAsLong

DimFeatureDataAsObject

DimFeatureAsObject

DimComponentAsObject

Deletealllinesofcodethatchangetheview.删除切换试图的代码

译者注:

像这样的Part.ActiveView().RotateAboutCenter0.0662574,0.0346621无情的删掉吧

DeleteallModelDocExtension:

:

SelectByID2callsappearingimmediatelybeforeModelDoc2:

:

ClearSelection2calls.However,donotdeleteModelDocExtension:

:

SelectByID2callsappearingimmediatelyafterModelDoc2:

:

ClearSelection2calls.

DeleteallModelDoc2:

:

ClearSelection2callsappearingimmediatelybeforeModelDocExtension:

:

SelectByID2.

solidworks二次开发-02-用来访问特征的两个API

来学习两个api:

SelectByID2和GetSelectedObject5。

这两个函数,第一个通过给出对象的name选择对象。

第二个通过启用程序前已经选择的索引得到对象。

看下面程序:

OptionExplicit

DimswAppAsSldWorks.SldWorks

DimModelAsModelDoc2

DimfeatureAsfeature

DimboolstatusAsVariant

Submain()

SetswApp=Application.SldWorks

SetModel=swApp.ActiveDoc

'选择叫"拉伸1"的特征

boolstatus=Model.Extension.SelectByID2("拉伸1","BODYFEATURE",0,0,0,False,0,Nothing,swSelectOptionDefault)

'主要就是这一句话,在写OptionExplicit后函数的最后一个参数swSelectOptionDefault可以使用0来代替

'Iftheselectionwassuccessful,thatis,"Extrude1"was

'selectedanditisa"BODYFEATURE",thengetthatfeature;otherwise,

'indicatefailure

Ifboolstatus=TrueThen'如果有“拉伸1”这个特征下面的代码将其选中

DimSelMgrAsSelectionMgr

SetSelMgr=Model.SelectionManager

Setfeature=SelMgr.GetSelectedObject5

(1)'此处使用一个索引来得到特征

Debug.Printfeature.Name

Else

Debug.Print"Error"

EndIf

EndSub

最后列出这两个函数的VB语法:

ModelDocExtension:

:

SelectByID2

Description

Thismethodselectsthespecifiedentity.

Syntax(OLEAutomation)

retval=ModelDocExtension.SelectByID2(Name,Type,X,Y,Z,Append,Mark,Callout.SelectOption)

Input:

(BSTR)Name

Nameofobjecttoselectoranemptystring

Input:

(BSTR)Type

Typeofobject(uppercase)asdefinedinswSelectType_eoranemptystring

Input:

(double)X

Xselectionlocationor0

Input:

(double)Y

Yselectionlocationor0

Input:

(double)Z

Zselectionlocationor0

Input:

(VARIANT_BOOL)Append

If...

Andifentityis...

Then...

TRUE

Notalreadyselected

Theentityisappendedtothecurrentselectionlist

Alreadyselected

Theentityisremovedfromthecurrentselectionlist

FALSE

Notalreadyselected

Thecurrentselectionlistiscleared,andthentheentityisputonthelist

Alreadyselected

Thecurrentselectionlistremainsthesame

Input:

(long)Mark

Valuethatyouwanttouseasamark;thisvalueisusedbyotherfunctionsthatrequireorderedselection

Input:

(LPCALLOUT)Callout

Pointertotheassociatedcallout

Input:

(long)SelectOption

SelectionoptionasdefinedinswSelectOption_e(seeRemarks)

Output:

(VARIANT_BOOL)retval

TRUEifitemwassuccessfullyselected,FALSEifnot

SelectionMgr:

:

GetSelectedObject5

Description

Thismethodgetstheselectedobject.

Syntax(OLEAutomation)

retval=SelectionMgr.GetSelectedObject5(AtIndex)

Input:

(long)AtIndex

Indexpositionwithinthecurrentlistofselecteditems,whereAtIndexrangesfrom1toSelectionMgr:

:

GetSelectedObjectCount

Output:

(LPDISPATCH)retval

PointertotheDispatchobjectasdefinedinswSelType_e;NULLmaybereturnediftypeisnotsupportedorifnothingisselected

solidworks二次开发-03-访问特征数据

'coderarden

'filename:

getchoosed.swp

'date:

2005-03-22

'usedtogetthesimpleholeinfomationdep&dia

'finishedlucky!

!

'------------------------------------------------------------

OptionExplicit

DimswAppAsSldWorks.SldWorks

DimModelAsModelDoc2

DimcurfeatureAsfeature

DimboolstatusAsBoolean

DimfeatdataAsSimpleHoleFeatureData2'声明一个简单直孔对象

DimcomponentAsComponent2

DimdepAsDouble

DimdiaAsDouble

DimSelMgrAsSelectionMgr

DimncountAsInteger

Subgetselected()

SetswApp=Application.SldWorks

SetModel=swApp.ActiveDoc

SetSelMgr=Model.SelectionManager

Setcurfeature=SelMgr.GetSelectedObject5

(1)'得到当前选中的第一个特征

MsgBoxcurfeature.Name

Setfeatdata=curfeature.GetDefinition'得到特征的定义

boolstatus=featdata.AccessSelections(Model,component)'可以对数据进行访问了

ncount=featdata.GetFeatureScopeBodiesCount

MsgBoxncount

dep=featdata.Depth

dia=featdata.Diameter

MsgBoxdia&"*"&dep

'MsgBox"errorarden"'在solidworks中可以使用swAPP.sendmsgtouser2

'featdata.ReleaseSelectionAccess

Model.Save

Model.EditRebuild

EndSub

**********************************************

上面程序运行前,假设你选择了一个简单直孔特征。

然后得到这个孔德一些参数。

孔深、直径等。

solidworks的API虽然是e文的。

介绍的还算详细,并且有很多的example。

大家可以多看看代码。

要访问一个特征,需要经历这样的步骤:

定义一个特征对象:

dim....as...

得到这个特征:

比如使用GetSelectedObject5还有SelectebyID等...

得到定义:

GetDefinition

进行访问:

AccessSelections

上面的程序没有if选择的容错机制,需要添加上。

solidworks二次开发-04-修改数据

上次已经可以访问特征的各参数了,今天我们来修改它:

要修改前面的步骤不能少,当我们已经可以读取一些特征时,我们就可以给他设定一些值。

当然有时需要调用特定的参数。

solidworks是ole和com的,所以要习惯这样。

在修改完特征后需要调用函数modifydefinition()来实现变化。

我们给一个例子,这个例子比前面的都要全面,它有很好的容错引导机制,可以直接拿来成为一个稳定的宏程序。

Thisexampledoublesthelengthofthebaseextrude.这个例子将拉伸凸台的长度增加一倍

DimswAppAsSldWorks.SldWorks

DimModelAsModelDoc2

DimComponentAsComponent2

DimCurFeatureAsfeature

DimisGoodAsBoolean

'WillbecomeanExtrudeFeatureDataObject

DimFeatDataAsObject

DimDepthAsDouble

DimSelMgrAsSelectionMgr

SubdoubleBE()

}}-->}}-->SetswApp=CreateObject("sldWorks.application")

}}-->}}-->SetModel=swApp.ActiveDoc

}}-->}}-->'Makesurethattheactivedocumentisapart

}}-->}}-->IfModel.GetType<>swDocPARTAndModel.GetType<>swDocASSEMBLYThen

‘这里的swDocPART、swDocASSEMBLY我的环境没有通过。

我使用msgboxModel.GetType的笨办法得到整数为1和2

}}-->}}-->Msg="OnlyAllowedonPartsorAssemblies"'Definemessage

}}-->}}-->Style=vbOKOnly'OKButtononly

}}-->}}-->Title="Error"'Definetitle

}}-->}}-->CallMsgBox(Msg,Style,Title)'Displayerrormessage

}}-->}}-->ExitSub'Exitthisprogram

}}-->}}-->EndIf

}}-->

}}-->

}}-->}}-->'GettheSelectionManager

}}-->}}-->SetSelMgr=Model.SelectionManager

}}-->

}}-->

}}-->}}-->'Gettheselectedobject(firstinthegroupiftherearemorethanone)

}}-->}}-->'NotethatatthispointCurFeatureisjustaFeatureObject

}}-->}}-->SetCurFeature=SelMgr.GetSelectedObject3

(1)

}}-->}}-->IfCurFeatureIsNothingThen

}}-->}}-->'Telltheuserthatnothingisselected

}}-->}}-->swApp.SendMsgToUser2"PleaseselecttheBase-Extrude",swMbWarning,swMbOk

}}-->}}-->ExitSub

}}-->}}-->EndIf

}}-->}}-->'Checkthefeature'stypename

}}-->}}-->'Makesureitisanextrusion

}}-->}}-->IfNotCurFeature.GetTypeName=swTnExtrusionThen

’在这里使用swTnExtrusion我的环境没有通过,我改成了Extrusion才ok

}}-->}}-->swApp.SendMsgToUser2"PleaseselecttheBase-Extrude",swMbWarning,swMbOk

}}-->}}-->ExitSub

}}-->}}-->EndIf

}}-->}}-->'GettheExtrusion'sFeatureData

}}-->}}-->SetFeatData=CurFeature.GetDefinition

}}-->

}}-->

}}-->}}-->'Gettheaccessselectionsforthefeaturedata

}}-->}}-->'NotethatComponentisNULLwhenaccessingtheselectionsofastandalonepart.}}-->}}-->IfwewerecallingAccessSelectionsfromwithinanassembly,thenmodelwouldrefertothetop-leveldocumentintheassemblyandcomponentwouldrefertotheactualpart.

}}-->}}-->isGood=FeatData.AccessSelections(Model,Component)

}}-->

}}-->

}}-->}}-->'Informtheuserofanerror

}}-->}}-->IfNotisGoodThen

}}-->}}-->swApp.SendMsgToUser2"Unabletoobtainaccessselections",swMbWarning,swMbOk

}}-->}}-->ExitSub

}}-->}}-->EndIf

}}-->

}}-->

}}-->}}-->'Makesuretheuserhasselectedthebaseextrude

}}-->}}-->IfNotFeatData.IsBaseExtrudeThen

}}-->}}-->swApp.SendMsgToUser2"PleaseselecttheBase-Extrude",swMbWarning,swMbOk

}}-->}}-->FeatData.ReleaseSelectionAccess

}}-->}}-->ExitSub

}}-->}}-->EndIf

}}-->

}}-->

}}-->}}-->'Changethedepthofthisextrusiontodoubleitspreviousdepth

}}-->}}-->Depth=FeatData.GetDepth(True)

}}-->}}-->FeatData.SetDepthTrue,Depth*2

}}-->

}}-->

}}-->}}-->'Implementthechangestothefeature

}}-->}}-->isGood=CurFeature.ModifyDefinition(FeatData,Model,Component)

}}-->

}}-->

}}-->}}-->'Ifthemodifydefinitionfailed

}}-->}}-->IfNotisGoodThen

}}-->}}-->swApp.SendMsgToUser2"Unabletomodifyfeaturedata",swMbWarning,swMbOk

}}-->}}-->'ReleasetheAccessSelections

}}-->}}-->FeatData.ReleaseSelectionAccess

}}-->}}-->EndIf

}}-->

}}-->

EndSub

如果出现特征出现“退回”状态,我现在还没有找到问题的原因,只能在代码执行到最后调用

Model.Save

Model.Rebuild

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 冶金矿山地质

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1