下面的代码是根据自己SolidWorks的二次开发积累的一些公共函数,因为SolidWorks二次开发的圈子太小,所以里面的代码注释写的不是很详细,如果不是很熟悉SolidWorks二次开发的话不是很好看懂。本人在SolidWorks二次开发方面积累点经验,如果有什么需要的话,我们可以一起交流,本人QQ为:1329347242.
'********************************
' Function: 定义和SolidWorks有关的公共函数的类
' Author: 要点理想色彩
' Createtime: 2018/07/11
' Remark: 该类里面的所有函数都是静态的,不需要实例化类就可以使用
'*******************************
Imports SolidWorks.Interop.sldworks
Public Class SWCustomFunction
''' <summary>
''' 获取指定模型里面的尺寸的集合
''' </summary>
''' <param name="swModel">将要获取尺寸的模型对象</param>
''' <returns>返回的是哈希表:哈希表的键是存储尺寸的名称,值是尺寸的数值</returns>
''' <remarks></remarks>
''' <readme>该函数主要思路是通过遍历模型的特征,获取特征里面的尺寸,再将尺寸添加到哈希表,返回哈希表的结果</readme>
Public Shared Function GetDisPalyDimension(ByVal swModel As ModelDoc2) As Hashtable
' 定义函数返回值
Dim hashDimension As New Hashtable
' 判断参数有效性
If swModel Is Nothing Then
MsgBox("函数GetDisPalyDimension的参数'swModel'为Nothing,无法获取模型尺寸集合")
Return hashDimension
End If
' 定义Solidworks有关的对象
Dim swFeat As Feature
Dim swSubFeat As Feature
Dim swDispDim As DisplayDimension
Dim swDim As Dimension
' 获取模型的第一个特征
swFeat = swModel.FirstFeature
'用循环进行遍历
Do While Not swFeat Is Nothing
'获取第一个子特征
swSubFeat = swFeat.GetFirstSubFeature
' 遍历子特征
Do While Not swSubFeat Is Nothing
' 获取子特征里面的第一个尺寸
swDispDim = swSubFeat.GetFirstDisplayDimension
' 遍历特征里面的尺寸
Do While Not swDispDim Is Nothing
' 转化为Dimension对象
swDim = swDispDim.GetDimension
' 将结果添加到函数返回集中
' swDim.FullName: 尺寸的名称(带有特征的层级关系@符号)
' swDim.GetSystemValue2(""):获取尺寸的数值
If hashDimension.ContainsKey(swDim.FullName) = False Then
hashDimension.Add(swDim.FullName, swDim.GetSystemValue2(""))
End If
' 获取下一个显示尺寸
swDispDim = swSubFeat.GetNextDisplayDimension(swDispDim)
Loop
swSubFeat = swSubFeat.GetNextSubFeature
Loop
' 获取下一个显示尺寸
swDispDim = swFeat.GetFirstDisplayDimension
' 遍历特征里面的尺寸
Do While Not swDispDim Is Nothing
' 转化为Dimension对象
swDim = swDispDim.GetDimension
' 将结果添加到函数返回集中
' swDim.FullName: 尺寸的名称(带有特征的层级关系@符号)
' swDim.GetSystemValue2(""):获取尺寸的数值
If hashDimension.ContainsKey(swDim.FullName) = False Then
hashDimension.Add(swDim.FullName, swDim.GetSystemValue2(""))
End If
' 获取下一个显示尺寸
swDispDim = swFeat.GetNextDisplayDimension(swDispDim)
Loop
swFeat = swFeat.GetNextFeature
Loop
'返回函数返回值
Return hashDimension
End Function
''' <summary>
''' 获取用户的自定义属性
''' </summary>
''' <param name="swModel">需要获取属性的模型</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetCustomProperty(ByVal swModel As ModelDoc2) As Hashtable
' 定义函数返回值
Dim hashCustomProperty As New Hashtable
' 判断参数有效性
If swModel Is Nothing Then
MsgBox("函数GetCustomProperty的参数'swModel'为Nothing,无法获取模型尺寸集合")
Return hashCustomProperty
End If
' 获取自定义属性管理器
Dim swCustPropMgr As CustomPropertyManager
swCustPropMgr = swModel.Extension.CustomPropertyManager("")
' 将属性值添加到哈希表中
Dim objPropNames As Object
Dim intPropNum As Integer
Dim valOut As String = ""
Dim resolvedValOut As String = ""
objPropNames = swCustPropMgr.GetNames()
intPropNum = swCustPropMgr.Count
For i = 0 To intPropNum - 1
swCustPropMgr.Get2(objPropNames(i), valOut, resolvedValOut)
If hashCustomProperty.ContainsKey(objPropNames(i)) = False Then
hashCustomProperty.Add(objPropNames(i).ToString, resolvedValOut)
End If
Next
'返回函数返回值
Return hashCustomProperty
End Function
''' <summary>
''' 更新模型的尺寸
''' </summary>
''' <param name="swModel">需要更新的模型</param>
''' <param name="hashNewDimension">类型:哈希表;表的键为将要被赋值的尺寸名称,值为与键对应的尺寸名称的数值大小</param>
''' <param name="hashOldDimension">类型;哈希表;遍历swModel模型里面的尺寸以哈希表的形式存储起来,表的键为尺寸的名称,表的值为对应尺寸的数值大小</param>
''' <remarks></remarks>
Public Shared Sub UpdateDimension(ByVal swModel As ModelDoc2, ByVal hashNewDimension As Hashtable, ByVal hashOldDimension As Hashtable)
' 检查参数有效性
If swModel Is Nothing Then
MsgBox("方法UpdateDimension的参数'swModel'为Nothing,无法更新模型 ")
Exit Sub
End If
' 更新尺寸
For Each NewKey In hashNewDimension.Keys
For Each OldKey In hashOldDimension.Keys
If OldKey.ToString.Contains(NewKey.ToString) Then
swModel.Parameter(OldKey.ToString).SystemValue = Val(hashNewDimension(NewKey)) / 1000
End If
Next
Next
' 重建模型
swModel.EditRebuild3()
End Sub
''' <summary>
''' 更新模型的属性
''' </summary>
''' <param name="swModel">需要更新的模型</param>
''' <param name="hashNewProperty">新的模型属性的集合</param>
''' <param name="hashOldProperty">旧的模型属性的集合</param>
''' <remarks></remarks>
Public Shared Sub UpdateProperty(ByVal swModel As ModelDoc2, ByVal hashNewProperty As Hashtable, ByVal hashOldProperty As Hashtable)
' 检查参数有效性
If swModel Is Nothing Then
MsgBox("方法UpdateProperty的参数'swModel'为Nothing,无法更新模型 ")
Exit Sub
End If
' 获取属性管理器
Dim swCustomPropertyManager As CustomPropertyManager
swCustomPropertyManager = swModel.Extension.CustomPropertyManager("")
' 更新尺寸
For Each NewKey In hashNewProperty.Keys
For Each OldKey In hashOldProperty.Keys
If OldKey.ToString.Contains(NewKey.ToString) Then
swCustomPropertyManager.Set(OldKey.ToString, hashNewProperty(NewKey).ToString)
End If
Next
Next
' 重建模型
swModel.EditRebuild3()
End Sub
End Class