solidworks二次開發-04-修改數據
2024-07-21 02:16:14
供稿:網友
solidworks二次開發-04-修改數據
上次已經可以訪問特征的各參數了,今天我們來修改它:
要修改前面的步驟不能少,當我們已經可以讀取一些特征時,我們就可以給他設定一些值。當然有時需要調用特定的參數。solidworks是ole和com的,所以要習慣這樣。
在修改完特征后需要調用函數modifydefinition()來實現變化。
我們給一個例子,這個例子比前面的都要全面,它有很好的容錯引導機制,可以直接拿來成為一個穩定的宏程序。
this example doubles the length of the base extrude.這個例子將拉伸凸臺的長度增加一倍
dim swapp as sldworks.sldworks
dim model as modeldoc2
dim component as component2
dim curfeature as feature
dim isgood as boolean
' will become an extrudefeaturedata object
dim featdata as object
dim depth as double
dim selmgr as selectionmgr
sub doublebe()
}}--> }}-->set swapp = createobject("sldworks.application")
}}--> }}-->set model = swapp.activedoc
}}--> }}-->' make sure that the active document is a part
}}--> }}-->if model.gettype <> swdocpart and model.gettype <> swdocassembly then
‘這里的swdocpart 、swdocassembly 我的環境沒有通過。我使用msgbox model.gettype 的笨辦法得到整數為1和2
}}--> }}-->msg = "only allowed on parts or assemblies" ' define message
}}--> }}-->style = vbokonly ' ok button only
}}--> }}-->title = "error" ' define title
}}--> }}-->call msgbox(msg, style, title) ' display error message
}}--> }}-->exit sub ' exit this program
}}--> }}-->end if
}}-->
}}-->
}}--> }}-->' get the selection manager
}}--> }}-->set selmgr = model.selectionmanager
}}-->
}}-->
}}--> }}-->' get the selected object (first in the group if there are more than one)
}}--> }}-->' note that at this point curfeature is just a feature object
}}--> }}-->set curfeature = selmgr.getselectedobject3(1)
}}--> }}-->if curfeature is nothing then
}}--> }}-->' tell the user that nothing is selected
}}--> }}-->swapp.sendmsgtouser2 "please select the base-extrude", swmbwarning, swmbok
}}--> }}-->exit sub
}}--> }}-->end if
}}--> }}-->' check the feature's type name
}}--> }}-->' make sure it is an extrusion
}}--> }}-->if not curfeature.gettypename = swtnextrusion then
’在這里使用swtnextrusion我的環境沒有通過,我改成了extrusion才ok
}}--> }}-->swapp.sendmsgtouser2 "please select the base-extrude", swmbwarning, swmbok
}}--> }}-->exit sub
}}--> }}-->end if
}}--> }}-->' get the extrusion's feature data
}}--> }}-->set featdata = curfeature.getdefinition
}}-->
}}-->
}}--> }}-->' get the access selections for the feature data
}}--> }}-->' note that component is null when accessing the selections of a standalone part. }}--> }}-->if we were calling accessselections from within an assembly, then model would refer to the top-level document in the assembly and component would refer to the actual part.
}}--> }}-->isgood = featdata.accessselections(model, component)
}}-->
}}-->
}}--> }}-->' inform the user of an error
}}--> }}-->if not isgood then
}}--> }}-->swapp.sendmsgtouser2 "unable to obtain access selections", swmbwarning, swmbok
}}--> }}-->exit sub
}}--> }}-->end if
}}-->
}}-->
}}--> }}-->' make sure the user has selected the base extrude
}}--> }}-->if not featdata.isbaseextrude then
}}--> }}-->swapp.sendmsgtouser2 "please select the base-extrude", swmbwarning, swmbok
}}--> }}-->featdata.releaseselectionaccess
}}--> }}-->exit sub
}}--> }}-->end if
}}-->
}}-->
}}--> }}-->' change the depth of this extrusion to double its previous depth
}}--> }}-->depth = featdata.getdepth(true)
}}--> }}-->featdata.setdepth true, depth * 2
}}-->
}}-->
}}--> }}-->' implement the changes to the feature
}}--> }}-->isgood = curfeature.modifydefinition(featdata, model, component)
}}-->
}}-->
}}--> }}-->' if the modify definition failed
}}--> }}-->if not isgood then
}}--> }}-->swapp.sendmsgtouser2 "unable to modify feature data", swmbwarning, swmbok
}}--> }}-->' release the accessselections
}}--> }}-->featdata.releaseselectionaccess
}}--> }}-->end if
}}-->
}}-->
end sub
如果出現特征出現“退回”狀態,我現在還沒有找到問題的原因,只能在代碼執行到最后調用
model.save
model.rebuild
這兩個函數來自動更新。