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
这两个函数来自动更新。