Const MMCCONFIGNAME As String = "MMC"
Const LMCCONFIGNAME As String = "LMC"
Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.ModelDoc2
Dim swFeatMgr As SldWorks.FeatureManager
Dim aAllFeatures As Variant
Dim swFeature As SldWorks.Feature
Dim swDispDim As SldWorks.DisplayDimension
Dim swDim As SldWorks.Dimension
Dim swTol As SldWorks.DimensionTolerance
Dim aDimsWithTol() As SldWorks.Dimension
Dim swDocExt As SldWorks.ModelDocExtension
Dim swMassProps As SldWorks.MassProperty
Dim swCfgMMC As SldWorks.Configuration
Dim swCfgLMC As SldWorks.Configuration
Dim swCfgBase As SldWorks.Configuration
Sub CreateMCconfigs()
Dim i As Long
Dim UpperVal As Double
Dim LowerVal As Double
Dim NominalVal As Double
Dim UpperValMass As Double
Dim LowerValMass As Double
Dim Temp As Variant
Dim DummyVar As Variant
Dim ValChgErr As Long
Dim Status As Long
Dim boolStatus As Boolean
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set swDocExt = swDoc.Extension
Set swCfgBase = swDoc.GetActiveConfiguration
If swDoc.GetType <> swDocPART Then
MsgBox "This macro only works for part files."
Exit Sub
End If
If MsgBox("Please be sure that your base configuration is active!!", vbOKCancel) <> vbOK Then
Exit Sub
End If
'Create the two configs if they do not exist
Call CreateOrConfirmConfigs
'First do MMC
swDoc.ShowConfiguration2 swCfgMMC.Name
Call GetDimsWithTol 'Loads the array "aDimsWithTol" with all
'toleranced dims (eliminates duplicates)
For i = 0 To UBound(aDimsWithTol)
GetDimLimits aDimsWithTol(i), UpperVal, LowerVal
Temp = aDimsWithTol(i).GetSystemValue3(swThisConfiguration, DummyVar)
NominalVal = Temp(0)
'Set to upper value
ValChgErr = aDimsWithTol(i).SetSystemValue3(UpperVal, swSetValue_InThisConfiguration, Empty)
boolStatus = swDoc.ForceRebuild3(False)
Set swMassProps = swDocExt.CreateMassProperty
UpperValMass = swMassProps.Mass
'Set to lower value
ValChgErr = aDimsWithTol(i).SetSystemValue3(LowerVal, swSetValue_InThisConfiguration, Empty)
boolStatus = swDoc.ForceRebuild3(False)
Set swMassProps = swDocExt.CreateMassProperty
LowerValMass = swMassProps.Mass
'Change dim value to whichever gave higher mass
If UpperValMass > LowerValMass Then
ValChgErr = aDimsWithTol(i).SetSystemValue3(UpperVal, swSetValue_InThisConfiguration, Empty)
Else
ValChgErr = aDimsWithTol(i).SetSystemValue3(LowerVal, swSetValue_InThisConfiguration, Empty)
End If
aDimsWithTol(i).Tolerance.Type = swTolNONE
boolStatus = swDoc.ForceRebuild3(False)
Next i
'Now do MMC
swDoc.ShowConfiguration2 swCfgLMC.Name
Call GetDimsWithTol 'Loads the array "aDimsWithTol" with all
'toleranced dims (eliminates duplicates)
For i = 0 To UBound(aDimsWithTol)
GetDimLimits aDimsWithTol(i), UpperVal, LowerVal
Temp = aDimsWithTol(i).GetSystemValue3(swThisConfiguration, DummyVar)
NominalVal = Temp(0)
'Set to upper value
ValChgErr = aDimsWithTol(i).SetSystemValue3(UpperVal, swSetValue_InThisConfiguration, Empty)
boolStatus = swDoc.ForceRebuild3(False)
Set swMassProps = swDocExt.CreateMassProperty
UpperValMass = swMassProps.Mass
'Set to lower value
ValChgErr = aDimsWithTol(i).SetSystemValue3(LowerVal, swSetValue_InThisConfiguration, Empty)
boolStatus = swDoc.ForceRebuild3(False)
Set swMassProps = swDocExt.CreateMassProperty
LowerValMass = swMassProps.Mass
'Change dim value to whichever gave lower mass
If UpperValMass > LowerValMass Then
ValChgErr = aDimsWithTol(i).SetSystemValue3(LowerVal, swSetValue_InThisConfiguration, Empty)
Else
ValChgErr = aDimsWithTol(i).SetSystemValue3(UpperVal, swSetValue_InThisConfiguration, Empty)
End If
aDimsWithTol(i).Tolerance.Type = swTolNONE
boolStatus = swDoc.ForceRebuild3(False)
Next i
swDoc.ShowConfiguration2 swCfgBase.Name
End Sub
Sub GetDimsWithTol()
Dim bMatch As Boolean
Dim i As Long
Dim j As Long
Dim TolCount As Long
ReDim aDimsWithTol(0)
Set aDimsWithTol(0) = Nothing
Set swFeatMgr = swDoc.FeatureManager
aAllFeatures = swFeatMgr.GetFeatures(True)
For i = 0 To UBound(aAllFeatures)
Set swFeature = aAllFeatures(i)
Set swDispDim = swFeature.GetFirstDisplayDimension
While Not (swDispDim Is Nothing)
Set swDim = swDispDim.GetDimension
Set swTol = swDim.Tolerance
dimcount = dimcount + 1
DimNameStr = DimNameStr & swDim.FullName & vbCrLf
If TolAnalysisPossible(swTol.Type) Then
bMatch = False
If Not (aDimsWithTol(0) Is Nothing) Then
For j = 0 To UBound(aDimsWithTol)
If aDimsWithTol(j).FullName = swDim.FullName Then
bMatch = True
'MsgBox "matched " & aDimsWithTol(j).FullName & " with " & swDim.FullName
Exit For
End If
Next j
End If
If Not bMatch Then
TolCount = TolCount + 1
ReDim Preserve aDimsWithTol(TolCount - 1)
Set aDimsWithTol(TolCount - 1) = swDim
End If
End If
Set swDispDim = swFeature.GetNextDisplayDimension(swDispDim)
Wend
Next i
End Sub
Sub CreateOrConfirmConfigs()
Dim boolStatus As Boolean
Set swCfgMMC = swDoc.GetConfigurationByName(MMCCONFIGNAME)
Set swCfgLMC = swDoc.GetConfigurationByName(LMCCONFIGNAME)
If Not swCfgMMC Is Nothing Then 'destroy config
boolStatus = swDoc.DeleteConfiguration2(swCfgMMC.Name)
If Not boolStatus Then
MsgBox "Unable to delete configuration: " & swCfgMMC.Name, vbCritical
End
End If
End If
If Not swCfgLMC Is Nothing Then 'destroy config
boolStatus = swDoc.DeleteConfiguration2(swCfgLMC.Name)
If Not boolStatus Then
MsgBox "Unable to delete configuration: " & swCfgLMC.Name, vbCritical
End
End If
End If
Set swCfgMMC = swDoc.AddConfiguration3(MMCCONFIGNAME, "Max Mat'l Config", "Max Mat'l Config", 0)
Set swCfgLMC = swDoc.AddConfiguration3(LMCCONFIGNAME, "Least Mat'l Config", "Least Mat'l Config", 0)
swDoc.ShowConfiguration2 swCfgBase.Name
End Sub
Sub GetDimLimits(ByVal myDim As SldWorks.Dimension, ByRef HiVal As Double, ByRef LoVal As Double)
Dim myTol As SldWorks.DimensionTolerance
Set myTol = myDim.Tolerance
Dim dummy As Variant
Select Case myTol.Type
Case swTolBILAT
dummy = myDim.GetSystemValue3(swThisConfiguration, Empty)
HiVal = dummy(0) + myTol.GetMaxValue
LoVal = dummy(0) + myTol.GetMinValue
Case swTolLIMIT
dummy = myDim.GetSystemValue3(swThisConfiguration, Empty)
HiVal = dummy(0) + myTol.GetMaxValue
LoVal = dummy(0) + myTol.GetMinValue
Case swTolSYMMETRIC
dummy = myDim.GetSystemValue3(swThisConfiguration, Empty)
HiVal = dummy(0) + myTol.GetMaxValue
LoVal = dummy(0) - myTol.GetMaxValue
Case swTolFITWITHTOL
dummy = myDim.GetSystemValue3(swThisConfiguration, Empty)
HiVal = dummy(0) + myTol.GetMaxValue
LoVal = dummy(0) + myTol.GetMinValue
Case swTolFITTOLONLY
dummy = myDim.GetSystemValue3(swThisConfiguration, Empty)
HiVal = dummy(0) + myTol.GetMaxValue
LoVal = dummy(0) + myTol.GetMinValue
End Select
End Sub
Function TolAnalysisPossible(ByVal DimTolType As Long) As Boolean
TolAnalysisPossible = False
Select Case DimTolType
Case swTolBILAT
TolAnalysisPossible = True
Case swTolLIMIT
TolAnalysisPossible = True
Case swTolSYMMETRIC
TolAnalysisPossible = True
Case swTolMETRIC
'TolAnalysisPossible = True 'Don't know what this is at this time
Case swTolFITWITHTOL
TolAnalysisPossible = True
Case swTolFITTOLONLY
TolAnalysisPossible = True
Case swTolBLOCK
'TolAnalysisPossible = True 'Don't know what this is at this time
End Select
End Function