3 Replies Latest reply on Dec 28, 2015 1:38 AM by Vincent Li

    Get the value of all types of the dimension without the unit ?

    Vincent Li

      Hi Every one,

       

      When I get the value of the dimension, it would be normal when the unit is mm/cm/m, the linear dimension.

      but for the dimension , like angle, k fator, they are the dimension, but the unit is degree or no unit, Then I  got Zero by using :GetSystemValue2("")

       

      like a angle dimemsion in a revolve feature, it is 360 degree, I want to get "360" part,  k factor , 0.5, then get a value=0.5.

       

      How to handle above ? pls help to advise.

       

      regards

      Vincent

        • Re: Get the value of all types of the dimension without the unit ?
          Amen Allah Jlili

          Well, you should use a switch case statement for Feature.GetTypeName when you traverse you part's features so when you hit a revolve feature or a sheet metal feature, you'd know how to handle them by adding "value=" or "degrees" to your output? I hope I understood you correctly.

           

          By definition K factor is the ratio between the sheet thickness and the neutral fiber position so the dimensions of the last two cancels each other out so it makes sense you're getting a zero. Likewise, all angels are (technically) measured in degrees in mechanical design.

            • Re: Get the value of all types of the dimension without the unit ?
              Vincent Li

              Hi Amen,

               

              I found a sample which can solve this problem.

              it still use idimension.GetSystemValue2("")  to get the value, but with a nDimensionfactor to calculate the final value.

               

              just come back to share .

               

              regards

              Vincent

                • Re: Get the value of all types of the dimension without the unit ?
                  Vincent Li

                  Option Explicit

                  Function GetDimFactor(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, swDim As SldWorks.Dimension) As Double

                      Const PI                    As Double = 3.14159265

                      Const LEN_FACTOR            As Double = 1000#

                      Const ANG_FACTOR            As Double = 180# / PI

                     

                      Select Case swDim.GetType

                          Case swDimensionParamTypeDoubleLinear

                              GetDimFactor = LEN_FACTOR

                             

                          Case swDimensionParamTypeDoubleAngular

                              GetDimFactor = ANG_FACTOR

                             

                          Case Else

                              Debug.Assert False

                      End Select

                  End Function

                  Function GetDimString _
                  (swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, swDim As SldWorks.Dimension) As String

                      Const LEN_STR               As String = " mm"

                      Const ANG_STR               As String = " deg"

                     

                      Select Case swDim.GetType

                          Case swDimensionParamTypeDoubleLinear

                              GetDimString = LEN_STR

                             

                          Case swDimensionParamTypeDoubleAngular

                              GetDimString = ANG_STR

                             

                          Case Else

                              Debug.Assert False

                      End Select

                  End Function

                  Sub SetDimensionToMidTolerance _
                  (swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, swDim As SldWorks.Dimension)

                      Dim nRetval                 As Long

                      Dim nDimFactor              As Double

                      Dim sDimStr                 As String

                      Dim vTolVal                 As Variant

                      Dim nOldVal                 As Double

                      Dim nNewVal                 As Double

                      Dim bRet                    As Boolean

                      If swDimensionDriving <> swDim.DrivenState Or swDim.ReadOnly Then

                          Exit Sub

                      End If

                     

                      ' Might have tolerance values stored from

                      ' previous tolerance type, so check current

                      ' tolerance type first

                      If swTolNONE = swDim.GetToleranceType Then

                          Exit Sub

                      End If

                      ' Mid-tolerance cannot make sense for a MIN/MAX tolerance

                      If swTolMIN = swDim.GetToleranceType Or swTolMAX = swDim.GetToleranceType Then

                          Exit Sub

                      End If

                      vTolVal = swDim.GetToleranceValues

                      If IsEmpty(vTolVal) Then

                          Exit Sub

                      End If

                     

                      nDimFactor = GetDimFactor(swApp, swModel, swDim)

                      sDimStr = GetDimString(swApp, swModel, swDim)

                     

                      nOldVal = swDim.GetSystemValue2("")

                      nNewVal = nOldVal + (vTolVal(0) + vTolVal(1)) / 2#

                      Debug.Print "    -->"

                      Debug.Print "      Old Value    = " & nOldVal * nDimFactor & sDimStr

                      Debug.Print "      New Value    = " & nNewVal * nDimFactor & sDimStr

                      nRetval = swDim.SetSystemValue3(nNewVal, swSetValue_InThisConfiguration, Empty): Debug.Assert swSetValue_Successful = nRetval

                     

                      ' Changed to mid tolerance, so remove tolerance;

                      ' otherwise, will change dimension when run again

                      bRet = swDim.SetToleranceType(swTolNONE): Debug.Assert bRet

                  End Sub

                  Sub ProcessDimension _
                  (swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, swDim As SldWorks.Dimension)

                      Dim nDimFactor              As Double

                      Dim sDimStr                 As String

                      Dim vTolVal                 As Variant

                      Dim bRet                    As Boolean

                     

                      nDimFactor = GetDimFactor(swApp, swModel, swDim)

                      sDimStr = GetDimString(swApp, swModel, swDim)

                     

                      Debug.Print "    " & swDim.FullName

                      Debug.Print "      Value        = " & swDim.GetSystemValue2("") * nDimFactor & sDimStr

                      Debug.Print "      Driven       = " & swDim.DrivenState

                      Debug.Print "      ReadOnly     = " & swDim.ReadOnly
                     
                     

                     

                      vTolVal = swDim.GetToleranceValues

                      If IsEmpty(vTolVal) Then

                          Debug.Print "      No tolerance info"

                          Exit Sub

                      End If

                     

                      Debug.Print "      TolType      = " & swDim.GetToleranceType

                      Debug.Print "      FitType      = " & swDim.GetToleranceFitValues

                      Debug.Print "      MaxTol       = " & vTolVal(1) * nDimFactor & sDimStr

                      Debug.Print "      MinTol       = " & vTolVal(0) * nDimFactor & sDimStr

                  End Sub

                  Sub ProcessMassProps(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2)

                      Dim swDocExt                As SldWorks.ModelDocExtension

                      Dim swMass                  As SldWorks.MassProperty

                      Set swDocExt = swModel.Extension

                      Set swMass = swDocExt.CreateMassProperty

                      Debug.Print "    Mass             = " & swMass.Mass * 1000# & " g"

                      Debug.Print "    Surface Area     = " & swMass.SurfaceArea * 1000000# & " mm^2"

                      Debug.Print "    Volume           = " & swMass.Volume * 1000000000# & " mm^3"

                      Debug.Print "    Density          = " & swMass.Density & " kg/m^3"

                      Debug.Print "    CenterOfMass     = (" & swMass.CenterOfMass(0) * 1000# & ", " & swMass.CenterOfMass(1) * 1000# & ", " & swMass.CenterOfMass(2) * 1000# & ") mm"

                  End Sub

                  Sub main()

                      Dim swApp                   As SldWorks.SldWorks

                      Dim swModel                 As SldWorks.ModelDoc2

                      Dim swConf                  As SldWorks.Configuration

                      Dim swMidConf               As SldWorks.Configuration

                      Dim swConfMgr               As SldWorks.ConfigurationManager

                      Dim swFeat                  As SldWorks.Feature

                      Dim swDispDim               As SldWorks.DisplayDimension

                      Dim swDim                   As SldWorks.Dimension

                      Dim bRet                    As Boolean

                     

                      Set swApp = Application.SldWorks

                      Set swModel = swApp.ActiveDoc

                      Set swConf = swModel.GetActiveConfiguration

                      Set swConfMgr = swModel.ConfigurationManager

                      Set swMidConf = swConfMgr.AddConfiguration(swConf.Name & " - mid tolerance", "mid tolerance", "mid tolerance", 1, swConf.Name, "mid tolerance"): Debug.Assert Not swMidConf Is Nothing

                      Set swFeat = swModel.FirstFeature

                     

                     

                      Debug.Print "File = " & swModel.GetPathName

                     

                      Debug.Print "  Nominal Tolerance:"

                      ProcessMassProps swApp, swModel

                      Debug.Print "  -----------------------------"

                     

                      Do While Not swFeat Is Nothing

                          Debug.Print "  " & swFeat.Name

                         

                          Set swDispDim = swFeat.GetFirstDisplayDimension

                          Do While Not swDispDim Is Nothing

                              Set swDim = swDispDim.GetDimension

                             

                              ProcessDimension swApp, swModel, swDim

                              SetDimensionToMidTolerance swApp, swModel, swDim

                             

                              Set swDispDim = swFeat.GetNextDisplayDimension(swDispDim)

                          Loop

                          Set swFeat = swFeat.GetNextFeature

                      Loop

                     

                      bRet = swModel.ForceRebuild3(False): Debug.Assert bRet

                      Debug.Print "  Middle Tolerance:"

                      ProcessMassProps swApp, swModel

                      Debug.Print "  -----------------------------"

                  End Sub