Min/Max Material Models
Min/Max Material Models
(OP)
Hello All,
I did a quick search but couldn't find anything on this, so.
Is there a macro/plugin that will create new model configurations for the MMC and LMC of a model using the assigned tolerances?
I did a quick search but couldn't find anything on this, so.
Is there a macro/plugin that will create new model configurations for the MMC and LMC of a model using the assigned tolerances?






RE: Min/Max Material Models
I ended up realizing that nothing exists that is cheap. There is however a slick (or seemingly slick) piece of software that does all sorts of tolerance analysis within the solidworks environment.
http://www.varatech.com/
-Shaggy
RE: Min/Max Material Models
Chris
Systems Analyst, I.S.
SolidWorks 06 4.1/PDMWorks 06
AutoCAD 06
ctopher's home (updated 06-21-06)
RE: Min/Max Material Models
-Shaggy
RE: Min/Max Material Models
SW 2007 has a few new features along these lines, but I have not tried it yet.
Chris
Systems Analyst, I.S.
SolidWorks 06 4.1/PDMWorks 06
AutoCAD 06
ctopher's home (updated 06-21-06)
RE: Min/Max Material Models
1. Save 2 copies called [original model]LMC and [original model]MMC.
2. Traverse each dimension of each feature in the feature tree.
- Check for dimensions with a tolerance
- Change dimension to nominal plus upper tolerance
- Rebuild and check mass
- Change dimension to nominal minus lower tolerance
- Rebuild and check mass, comparing to upper tolerance value
- Keep whichever dim gave a higher/lower mass value for MMC/LMC
If you need to do tolerance analysis in the assembly (I assume this would be the case) you would have to do "Replace Component" rather than just changing configs. However, unless your tolerances are really bad wrong any faces/edges etc should remain the same so mates reattach.
RE: Min/Max Material Models
I suppose the trick is in knowing whether you're specifying a positive or negative feature? But even that shouldn't be an issue because SWx knows that when you make the feature in the first place!
Handleman seems to be on the right track, but I'm kind of surprised this isn't something that's already out there (aside from the apparently questionable varatech)
RE: Min/Max Material Models
(Don't worry, CBL, it's clean.
Preconditions:
1. A part model is open whose dimensions have tolerances
2. No configurations active named "MMC" or "LMC" (names changeable with string constants at top). Macro will attempt to delete configurations with these names.
3. "Nominal" configuration is active.
Postconditions:
Two new configurations named "MMC" and "LMC" showing Maximum and Least Material Conditions based on tolerances. These configurations will have their dimension tolerance set to "None" for clarity.
Methodology:
It was desired to not alter the original configuration at all to minimize the possibility of inadvertent model dimension change should the macro crash. Therefore, new configurations LMC and MMC are created if they do not exist. If they do exist the macro will attempt to delete them. If either deletion fails the macro will quit. This is to ensure that both new configurations start out identical to the nominal configuration.
The features are iterated through and an array is built of dimensions that have tolerances with values (basic, max, min, etc are ignored). Then, for each toleranced dimension the dimension value itself is set to the upper limit and lower limit, mass is compared to determine which is MMC and LMC.
CODE
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
RE: Min/Max Material Models
Neat, is there something I can add to change the solid body color of the config upon creation?
Somewhat related to the MMC/LMC.. is there a macro floating around to export all configurations as an .IGES (or .STEP, etc.) with the configuration name (not part name)? Currently I'm using a macro to export to those file types one-by-one but then I have to rename them to their config name as they get saved with the part name.
RE: Min/Max Material Models
RE: Min/Max Material Models
I've also fixed it to make LMC and MMC configs a different color. You can change the color by editing the constants at the top. The COLORQUERYTOGGLE constant is currently "False". If you change it to "True" then it will show you the RGB values of the base configuration in a message box. You can use these to home in on the color you want for each of the other configs. I set them up initially to be Magenta and Lemon
Hate to have to post such a long code block again, but here goes:
CODE
Const MMCCONFIGNAME As String = "MMC"
Const LMCCONFIGNAME As String = "LMC"
'Magenta for Max matl
Const MMCRED As Double = 1
Const MMCGREEN As Double = 0.753
Const MMCBLUE As Double = 1
'Lemon for Least matl
Const LMCRED As Double = 1
Const LMCGREEN As Double = 1
Const LMCBLUE As Double = 0
'This boolean turns on/off color query for base config
Const COLORQUERYTOGGLE As Boolean = False
'This boolean turns on/off hiding tolerances on new configs
Const HIDETOLS As Boolean = True
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
Dim MatlProps As Variant
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
If COLORQUERYTOGGLE Then
MatlProps = swDoc.MaterialPropertyValues
MsgBox "Red: " & MatlProps(0) & vbCrLf & "Green: " & MatlProps(1) & vbCrLf & "Blue: " & MatlProps(2)
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
If HIDETOLS Then aDimsWithTol(i).Tolerance.Type = swTolNONE
boolStatus = swDoc.ForceRebuild3(False)
Next i
MatlProps = swDoc.MaterialPropertyValues
MatlProps(0) = MMCRED
MatlProps(1) = MMCGREEN
MatlProps(2) = MMCBLUE
swDoc.MaterialPropertyValues = MatlProps
'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
If HIDETOLS Then aDimsWithTol(i).Tolerance.Type = swTolNONE
boolStatus = swDoc.ForceRebuild3(False)
Next i
MatlProps = swDoc.MaterialPropertyValues
MatlProps(0) = LMCRED
MatlProps(1) = LMCGREEN
MatlProps(2) = LMCBLUE
swDoc.MaterialPropertyValues = MatlProps
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, dummy)
HiVal = dummy(0) + myTol.GetMaxValue
LoVal = dummy(0) + myTol.GetMinValue
Case swTolLIMIT
dummy = myDim.GetSystemValue3(swThisConfiguration, dummy)
HiVal = dummy(0) + myTol.GetMaxValue
LoVal = dummy(0) + myTol.GetMinValue
Case swTolSYMMETRIC
dummy = myDim.GetSystemValue3(swThisConfiguration, dummy)
HiVal = dummy(0) + myTol.GetMaxValue
LoVal = dummy(0) - myTol.GetMaxValue
Case swTolFITWITHTOL
dummy = myDim.GetSystemValue3(swThisConfiguration, dummy)
HiVal = dummy(0) + myTol.GetMaxValue
LoVal = dummy(0) + myTol.GetMinValue
Case swTolFITTOLONLY
dummy = myDim.GetSystemValue3(swThisConfiguration, dummy)
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
RE: Min/Max Material Models