5 Replies Latest reply on Jul 1, 2014 2:48 PM by Deepak Gupta

    I need help defining the actions of this code

    Alex Beaulier

      Here's what I have and what i have defined. Everything that I have as #not sure about this is what I don't understand. I want to learn the SolidWorks API code and I don't know what certain definitions and tasks are doing in this macro. The yellow highlighted part is the code. Thanks again for any help.

       

      Option Explicit   #Not sure yet

       

      Dim swApp As SldWorks.SldWorks                            #This defines the application as Solidworks

      Dim swModel As SldWorks.ModelDoc2                  #This defines swModel as the Solidworks model document

      Dim swSketchMgr As SldWorks.SketchManager                                 #This defines swSketchMgr as the sketch manager for creating sketches in SolidWorks

      Dim swExcel As Excel.Application                               #This defines swExcel as the Microsoft Excel application

      Dim exSheet As Excel.Worksheet #This defines the excel spreadsheet as the document to use in the macro for exporting numbers for Solidworks to use

      Dim i As Integer                                #This defines i as an integer to use for various tasks

      Dim xpt As Double                           #Not sure about this part yet

      Dim ypt As Double                           #Not sure about this part yet

      Dim zpt As Double #Not sure about this part yet

      Dim xpt1 As Double #Not sure about this part yet

      Dim ypt1 As Double                        #Not sure about this part yet

      Dim zpt1 As Double                        #Not sure about this part yet

      Sub main() #sets apart the definitions from the actual applications procedures or commands

       

          Set swApp = Application.SldWorks                        #Sets up the application

          Set swModel = swApp.ActiveDoc                          #Prepares to use the active document for the entity to which the properties will act in

         

          If swModel Is Nothing Then                     #Is a double check procedure to make sure an active document is open for editing

              MsgBox "Please activate a part document before using this macro."                                                 #is a warning from the macro application to the user on why it will not function

              Exit Sub                        #Exits the current set of commands in the macro and returns to an offline state until the user changes the error so the next run will execute the macro properly

          End If                                 #Ends the if command loop

          If swModel.GetType <> 1 Then                             #Not sure

              MsgBox "Please activate a part document before using this macro."

              Exit Sub                        #Exits the current set of commands in the macro and returns to an offline state until the user changes the error so the next run will execute the macro properly

          End If                 #Ends the If command loop

         

           If Not swModel.GetActiveSketch2 Is Nothing Then                                    #Not sure yet

              MsgBox "Please exit the sketch before running this macro."                               #is a warning from the macro application to the user on why it will not function

       

              Exit Sub                        #Exits the current set of commands in the macro and returns to an offline state until the user changes the error so the next run will execute the macro properly

          End If                 #Ends the If command loop

         

                                      #Code has passed the initial tests. Will start the next commands for the macro

          Set swExcel = GetObject(, "Excel.Application")                              #Will open up the excel application for the macro

          Set exSheet = swExcel.ActiveSheet                                     #Takes the activesheet for importing data

          Set swSketchMgr = swModel.SketchManager                                #Sets the sketch manager open in Solidworks

       

          swModel.ClearSelection2 True                                              #Clears any selection the mouse is clicked on

          swModel.SketchManager.Insert3DSketch True             #Is activating the 3D sketch for Solidworks

          swModel.SetAddToDB True                                                    #Not sure yet

         

      i = 1                                                                                                        #Defines the integer i as 1

            Do While exSheet.Cells(i, 1).Value <> ""                          #Not sure yet

           

              xpt = exSheet.Cells(i, 1).Value / 1000                              #Takes the excel spreadsheet i-A and divides its value by 1000 for the x1-point

              ypt = exSheet.Cells(i, 2).Value / 1000                              #Takes the excel spreadsheet i-B and divides its value by 1000 for the y1-point

              zpt = exSheet.Cells(i, 3).Value / 1000                              #Takes the excel spreadsheet i-C and divides its value by 1000 for the z1-point

                       

              xpt1 = exSheet.Cells(i, 4).Value / 1000                           #Takes the excel spreadsheet i-D and divides its value by 1000 for the x2-point

              ypt1 = exSheet.Cells(i, 5).Value / 1000                           #Takes the excel spreadsheet i-E and divides its value by 1000 for the y2-point

              zpt1 = exSheet.Cells(i, 6).Value / 1000                            #Takes the excel spreadsheet i-F and divides its value by 1000 for the z2-point

             

              swModel.SketchManager.CreateLine xpt, ypt, zpt, xpt1, ypt1, zpt1                                 #Creates a line based on the values from the above commands

             

      i = i + 1                                  #defines the next i value as i+1 so the next column will be selected as 2-A 2-B ect

       

      Loop                                      #defines the loop so the command starts again at i=current value at end of the loop when 1 is added from the previous step

             

          swModel.ViewZoomtofit2                       #Zooms the Solidwork application to fit the view of all the line segments drawn

          swModel.SetAddToDB False                  #Not sure yet

          swModel.ClearSelection                           #Clears the selection manager from the current operation

          swModel.SketchManager.Insert3DSketch True                             #Not quite sure yet

          swModel.ClearSelection2 True                              #Clears the selection manager

            

         

      End Sub                                #Ends the macro

        • Re: I need help defining the actions of this code
          Jason Kerns

          Hello!

           

          For the data types you can find the Visual Basic Reference or help to understand.

           

          Data type

          Storage size

          Range

          Byte

          1 byte

          0 to 255

          Boolean

          2 bytes

          True or False

          Integer

          2 bytes

          -32,768 to 32,767

          Long
          (long integer)

          4 bytes

          -2,147,483,648 to 2,147,483,647

          LongPtr
          (Long integer on 32-bit systems, LongLong integer on 64-bit systems)

          4-bytes on 32-bit systems, 8-bytes on 64-bit systems.

          -2,147,483,648 to 2,147,483,647 on 32-bit systems, -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 on 64-bit systems.

          LongLong
          (LongLong integer)

          8 bytes

          -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (Valid on 64-bit platforms only.)

          Single
          (single-precision floating-point)

          4 bytes

          -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values

          Double
          (double-precision floating-point)

          8 bytes

          -1.79769313486231E308 to
          -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values

          Currency
          (scaled integer)

          8 bytes

          -922,337,203,685,477.5808 to 922,337,203,685,477.5807

          Decimal

          14 bytes

          +/-79,228,162,514,264,337,593,543,950,335 with no decimal point;
          +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest non-zero number is
          +/-0.0000000000000000000000000001

          Date

          8 bytes

          January 1, 100 to December 31, 9999

          Object

          4 bytes

          Any Object reference

          String
          (variable-length)

          10 bytes + string length

          0 to approximately 2 billion

          String
          (fixed-length)

          Length of string

          1 to approximately 65,400

          Variant
          (with numbers)

          16 bytes

          Any numeric value up to the range of a Double

          Variant
          (with characters)

          22 bytes + string length

          Same range as for variable-length String

          User-defined
          (using Type)

          • Re: I need help defining the actions of this code
            Deepak Gupta

            Option Explicit   Forces explicit declaration of all variables in a file, or allows implicit declarations of variables.

             

               If swModel.GetType <> 1 Then                       Check if the active file is a part file. 1 = Part, 2 = Assembly and 3 = Drawing

             

            swModel.SketchManager.Insert3DSketch True Start a 3d sketch

             

                 If Not swModel.GetActiveSketch2 Is Nothing Then       Checks if there is any active sketch

             

                swModel.SetAddToDB True                                           Sets/Start to add sketch entities to the SolidWorks database, For e.g. if you're making a rectangle then

                                                                                                         you need to have co-ordinates of four corner points

             

                  Do While exSheet.Cells(i, 1).Value <> ""                      will keep  processing the excel row until a cell in the specified row/column is empty


                swModel.SetAddToDB False                                           End the process of adding sketch entities to the SolidWorks database

             

             

                swModel.SketchManager.Insert3DSketch True                Ends the 3d sketch