6 Replies Latest reply on Feb 23, 2015 5:50 PM by Jon Proffitt

    Get Dimension Type

    Jon Proffitt

      Hello,

       

      New to SW API, long time user on the NX API boards , new company has SW which I haven't used in a few years.

       

      Im trying to use GetSelectedObject6 to cycle thru dimensions and split them into linear dimensions, radius, diameter, angle etc.

      Sofar I have. My Issue is that swDisplayDimension.Type2 is giving a value of 2 for linear dimensions and also diameters....

      I am basing this code off a similar API instructions to get dimension types.  Thanks!

      Dim swApp As SldWorks.SldWorks
      Dim swModel As SldWorks.ModelDoc2
      Dim swSelMgr As SldWorks.SelectionMgr
      Dim swDisplayDimension As SldWorks.DisplayDimension
      Dim swDimension As SldWorks.Dimension
      Dim swDimensionTolerance As SldWorks.DimensionTolerance
      Dim status As Boolean
      Dim fontHeight As Double
      Dim fontScale As Double
      Dim swView As SldWorks.View
      Dim swAnn As SldWorks.Annotation
      Dim swPart As SldWorks.PartDoc

       

       

      Sub main()

      Set swApp = Application.SldWorks
      Set swModel = swApp.ActiveDoc
      Set swSelMgr = swModel.SelectionManager

      Dim DimVal As Double
      Dim index As Long
      Dim Tol As String
      Dim DimType As Integer
      Dim DimTypeSel As String
      Dim Post As String
      Dim CalcNum As Double
      CalcNum = 0.0254

        

      For index = 1 To swSelMgr.GetSelectedObjectCount

          'Get the selection
          Set swDisplayDimension = swSelMgr.GetSelectedObject6(index, 0)
          'Set swAnn = swSelMgr.GetSelectedObject6(index, 0)
         
          Set swDimension = swDisplayDimension.GetDimension
          DimVal = swDimension.Value
         
          Set swDimensionTolerance = swDimension.Tolerance
         
          Set swAnn = swDisplayDimension.GetAnnotation
          'Debug.Print swAnn.GetName
          'Debug.Print swAnn.GetType
         
          Set swDisplayDimension = swAnn.GetSpecificAnnotation
          DimType = swDisplayDimension.Type2

          Debug.Print DimType

          ' CASE SELECTION

          Select Case (DimType)

         

          Case 1

          DimTypeSel = ""

          'Debug.Print DimTypeSel

          Case 2

          DimTypeSel = "Ø" ' Dia

          'Debug.Print DimTypeSel

          Case 3

          DimTypeSel = "" ' Angle

          'Debug.Print DimTypeSel

          CalcNum = 0.01745329

          Case 4

          DimTypeSel = ""

          'Debug.Print DimTypeSel

          Case 5

          DimTypeSel = "R" ' R

          'Debug.Print DimTypeSel

          Case Else

          DimTypeSel = ""

          'Debug.Print DimTypeSel

         

          End Select

          ' END SELECTION

       

      blah blah some stuff to export to Excel

        • Re: Get Dimension Type
          Keith Rice

          Jon,

           

          Next time, please include all information necessary to repeat the problem, like what document type you're using and what your dimensions look like. If you could upload a model, that would be best. Moreover, as a courtesy, please 1) remove irrelevant code from your snippet before you post it, 2) test the snippet to make sure it works (this snippet doesn't since you left out End Sub).

           

          Nevertheless, if I had to guess, you're selecting a double distance dimension, which does record a diametric value but does so using a linear dimension. If that's the case I can show you how to test for whether a dimension is double distance, and then you can mark it as diametric.

           

          Keith

          SolidWorks API Video Tutorials

            • Re: Get Dimension Type
              Jon Proffitt

              Keith,

               

              You are correct in the issue as I am trying to select a diameter and it also records as a linear dimension. I am trying to differentiate between the linear and diameter records. What is the proper method for testing between the two types as you mentioned? This is the code now and the value returned in debug.print is the same for both diameters and linear dimensions unfortunately.

               

              Option Explicit

               

              Dim swApp As SldWorks.SldWorks
              Dim swModel As SldWorks.ModelDoc2
              Dim swSelMgr As SldWorks.SelectionMgr
              Dim swDisplayDimension As SldWorks.DisplayDimension
              Dim swDimension As SldWorks.Dimension
              Dim swDimensionTolerance As SldWorks.DimensionTolerance
              Dim status As Boolean
              Dim fontHeight As Double
              Dim fontScale As Double
              Dim swView As SldWorks.View
              Dim swAnn As SldWorks.Annotation
              Dim swPart As SldWorks.PartDoc

               

               

              Sub main()

               

              Set swApp = Application.SldWorks
              Set swModel = swApp.ActiveDoc
              Set swSelMgr = swModel.SelectionManager

               

              Dim index As Integer
              Dim DimType As Double

                
              For index = 1 To swSelMgr.GetSelectedObjectCount

                 
                  'Get the selection
                  Set swDisplayDimension = swSelMgr.GetSelectedObject6(index, 0)
                  'Set swAnn = swSelMgr.GetSelectedObject6(index, 0)
                 
                   'If selection is not a dimension, then exit
                  If swSelMgr.GetSelectedObjectType3(1, -1) <> swSelDIMENSIONS Then Exit Sub

               

                ' Selection is a dimension, so get the dimension tolerance object
                  Set swDimension = swDisplayDimension.GetDimension
                  Set swDimensionTolerance = swDimension.Tolerance
                 
                  Set swAnn = swDisplayDimension.GetAnnotation
               
                  Set swDisplayDimension = swAnn.GetSpecificAnnotation
                  DimType = swDisplayDimension.Type2
                  Debug.Print "Dimension Type is: " & DimType

               

              Next index

               

              End Sub

                • Re: Get Dimension Type
                  Keith Rice

                  Jon,

                   

                  Artem wrote a function here for how to determine if a linear dimension is diametric. The criteria for whether a linear dimension is diametric are:

                   

                  1. The dimension must have a DOUBLEDISTANCE constraint type associated with it.

                  2. The dimension must be attached to two sketch segments.

                       a. One of these sketch segments must be construction.

                       b. The other sketch segment must not be construction.

                   

                  Keith

                  SolidWorks API Tutorials

                  1 person found this helpful
              • Re: Get Dimension Type
                JOHN GEORGE

                Jon,

                There is another discussion on the same subject

                I am not a code expert, still hope this will help you to solve the issue

                See this

                  • Re: Get Dimension Type
                    Jon Proffitt

                    John,

                     

                    I was using that post as a guideline in which it was very helpful. My only issue is, when I use the GetSelectedObject6 function ( since I am only trying to run this macro for a selected group of dimensions) and use the swDisplayDimension.Type2 , it returns the same value 2 for linear and diameter. I'm unsure how to differentiate these two type, however that approach works for arcs (value of 4) , radii (value of 5) , and angles (Value of 3) perfectly. Diameter should be value of 6 ... but being recorded as 2

                     

                    I feel like I am misunderstanding how the .Type2 interacts with objects in a GetSelecedObject6.

                  • Re: Get Dimension Type
                    Jon Proffitt

                    Hey, just resolved the issue I was having awhile ago.

                     

                    I used

                    Dim strDimensionTextCalloutAboveDefinition As String = swDisplayDimension.GetText(swDimensionTextParts_e.swDimensionTextCalloutAboveDefinition)

                    Dim strDimensionTextPrefixDefinition As String = swDisplayDimension.GetText(swDimensionTextParts_e.swDimensionTextPrefixDefinition)

                     

                    Then Solidworks uses a <MOD-DIAM> as a return on that

                    I then used Replace(strDimensionTextPrefixDefinition, "<MOD-DIAM>, "ø ") and now it knows its a diameter....sorry for long fix! Hope this helps someone.