9 Replies Latest reply on Dec 4, 2018 9:39 AM by Fifi Riri

    Get the dimension of part

    khadouja hami


      i'm trying to determine the dimension of part ( length and diameter) in SolidWorks using VB.net. but, i don't find the right algorithm.

      can you help me please? i'm waiting and thank you.

        • Re: Get the dimension of part
          Dave Paul

          Khadouja -

          Are you looking for the volume of the part, using the length and the diameter?

          If so, the equations are:

          • V = Volume
          • L = Length
          • ø = Diameter
          • r = Radius
          • π = Pi = 3.14159…


          V = L · π · r²

          V = L · π · (ø/2)²

          • Re: Get the dimension of part
            Fifi Riri

            It depends on the part.

            You could loop through the dimensions to look for what you want.

            2018 SOLIDWORKS API Help - Iterate Through Dimensions in Model Example (VBA)

            or if the dimensions are the maximum ones, you could use bounding box

            2018 SOLIDWORKS API Help - Get Part Bounding Box Example (VBA)

            • Re: Get the dimension of part
              khadouja hami

              hello and thank you for your answers,

              i tried this algorithm :

              Dim SelectionManager As Object


                      Dim SketchSegment As Object


                      Dim Length As Double


                      SelectionManager = Part.SelectionManager()


                      Part.SelectByID("part-1@assembly@Révolution1@Esquisse1", "EXTSKETCHSEGMENT", 0, 0, 0)


                      SketchSegment = SelectionManager.GetSelectedObject2(1)


                      Length = SketchSegment.GetLength()      " result = not defined bloc in this line "


              but i don't get a reslut

                • Re: Get the dimension of part
                  Fifi Riri

                  Not sure what are you trying to accomplish with this.

                  If you're trying to get the length of all the lines in the sketch "Esquisse1", use this:


                  'Open a part and select a sketch

                  Option Explicit

                  Sub main()

                      Dim swApp As SldWorks.SldWorks

                      Dim swModel As SldWorks.ModelDoc2

                      Dim swSelMgr As SldWorks.SelectionMgr

                      Dim swFeat As SldWorks.Feature

                      Dim swSketch As SldWorks.Sketch

                      Dim vSketchSegs As Variant

                      Dim vSketchSeg As Variant

                      Dim swSketchSeg As SldWorks.SketchSegment

                      Dim boolstatus As Boolean

                      Set swApp = Application.SldWorks

                      Set swModel = swApp.ActiveDoc

                      Set swSelMgr = swModel.SelectionManager

                      'boolstatus = Part.Extension.SelectByID2("Esquisse1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

                      Set swFeat = swSelMgr.GetSelectedObject6(1, -1)

                      Set swSketch = swFeat.GetSpecificFeature2

                      vSketchSegs = swSketch.GetSketchSegments

                      For Each vSketchSeg In vSketchSegs

                          Set swSketchSeg = vSketchSeg

                          If swSketchSeg.GetType <> swSketchSegments_e.swSketchTEXT Then

                              MsgBox swSketchSeg.GetLength

                          End If


                  End Sub


                  if you're trying to get the length of 1 selected line in the sketch "Esquisse1", use:


                  'Open a sketch and select a line

                  Option Explicit

                  Dim swApp As SldWorks.SldWorks

                  Dim Part As SldWorks.ModelDoc2

                  Dim SelectionManager As SldWorks.SelectionMgr

                  Dim SketchSegment As SldWorks.SketchSegment

                  Dim length As Double

                  Dim boolstatus As Boolean

                  Sub main()

                  Set swApp = Application.SldWorks

                  Set Part = swApp.ActiveDoc

                  'boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)

                  Set SelectionManager = Part.SelectionManager

                  Set SketchSegment = SelectionManager.GetSelectedObject6(1, -1)

                  Length = SketchSegment.GetLength

                  MsgBox (Length)

                  End Sub

                • Re: Get the dimension of part
                  John Stoltzfus

                  Bounding Box ????