马上注册,结交更多同行朋友,交流,分享,学习。
您需要 登录 才可以下载或查看,没有帐号?注册
x
本帖最后由 wenmk 于 2020-4-11 10:14 编辑
分享一段完整按设计树顺序遍历装配体代码,此代码是VB.NET编写。可以在此代码中添加功能代码后可实现特定功能。
- Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
- Dim swApp As SldWorks
- Dim swModel As ModelDoc2
- Dim swPrOPMgrs As CustomPropertyManager
- Dim swConMgrs As ConfigurationManager
- Dim swConfigs As Configuration
- Dim swGetType As String
- Dim swFeat As Feature
- Dim swChild As String
- RichTextBox1.Text = ""
- swApp = GetObject(, "SldWorks.Application")
- If swApp Is Nothing Then
- MsgBox("请您在运行本程序前先运行SOLIDWORKS!")
- Exit Sub
- End If
- swModel = swApp.ActiveDoc
- If swModel Is Nothing Then
- MsgBox("请您打开模型后再运行!")
- Exit Sub
- End If
- If swModel.GetType <> 2 Then
- MsgBox("请您打开装配模型后再运行!")
- Exit Sub
- End If
- swConMgrs = swModel.ConfigurationManager '获取配置管理器
- swConfigs = swConMgrs.ActiveConfiguration '获取活动配置
- swChild = swConfigs.Name '获取活动配置名
- swPropMgrs = swModel.Extension.CustomPropertyManager("") '获得自定义属性
- 'swPropMgrs = swModel.Extension.CustomPropertyManager(swChild) '获得配置特定属性
- swGetType = swModel.GetPathName '获取此文档的完整路径名,包括文件名
- RichTextBox1.Text &= swGetType & vbNewLine
- swFeat = swModel.FirstFeature
- While IsNothing(swFeat) = False
- Select Case swFeat.GetTypeName2
- Case "Reference", "ReferencePattern" '排除镜像及阵列模型
- Throughs(swModel, swFeat.Name)
- End Select
- swFeat = swFeat.GetNextFeature
- End While
- End Sub
- Private Sub Throughs(ByVal swModels As ModelDoc2, ByVal swPartName As String)
- Dim swComponent As Component2
- Dim swildMode As ModelDoc2
- Dim swPropMgr As CustomPropertyManager
- Dim swGetTypes As String
- Dim swFeature As Feature
- Dim swChild As String
- swComponent = swModels.GetComponentByName(swPartName) '获取指定的顶级程序集组件
- swChild = swComponent.ReferencedConfiguration
- If swComponent.IsSuppressed = False AndAlso swComponent.ExcludeFromBOM = False AndAlso swComponent.IsEnvelope = False Then '排除否被压缩、不包括在材料明细表中或是一个封套
- swildMode = swComponent.GetModelDoc2 '获取此组件的模型文档
- swGetTypes = swildMode.GetPathName '获取此文档的完整路径名,包括文件名
- RichTextBox1.Text &= swGetTypes & vbNewLine
- swPropMgr = swildMode.Extension.CustomPropertyManager("") '获得自定义属性
- 'swPropMgr = swildMode.Extension.CustomPropertyManager(swChild) '获得配置特定属性
- '..............
- '..............
- '..............
- '..............添加你要处理的代码
- If swildMode.GetType = 2 Then '获取文档类型,是否是装配文件
- swFeature = swildMode.FirstFeature '获取此组件中的第一个模型
- Do While Not swFeature Is Nothing '如果模型存在, 模型循环此模型
- Select Case swFeature.GetTypeName2
- Case "Reference", "ReferencePattern"
- Throughs(swildMode, swFeature.Name)
- End Select
- swFeature = swFeature.GetNextFeature
- Loop
- End If
- End If
- End Sub
复制代码
|