6 Replies Latest reply on Dec 30, 2014 11:50 PM by Deepak Gupta

    Simple API Help

    Greg Raymond

      Hello I am using a Macro currently that does a bounding box feature, I got this from a user here. I am looking to add a little more to it now, however I don't know how API works. I am somewhat experienced with C and C++ language, and can kind of sort understand the program language, however getting info out of SolidWorks, I don't know what's the protocol.

       

      What I am trying to accomplish is to add a new property that is dependent on the material. So I just need about 6 If's statements. Kinda copying and pasting what I have in the macro, this is what I am trying to achieve.

       

      retval = Part.AddCustomInfo3(ConfigName, "Treatment", swCustomInfoText)                                   // Adds a new property called "Treatment" in the Properties > Configuration Specific

      If getPartMaterial = "A2" Then                                                                                                             // Check for the part material (I don't know what function to use here)

        retval = Part.AddCustomInfo3(ConfigName, "Treatment", swCustomInfoText, "R/C 56 MIN")          // If the material is A2, then it will fill the text with "R/C 56 MIN"

      End if

       

      That's what I am basically trying to achieve, I just need some fine tuning because API is not familiar to me. Thanks.

        • Re: Simple API Help
          Greg Raymond

          Playing around with it more I got some of it down. I just need to retrieve the Material property off the part file.

           

          retval = Part.DeleteCustomInfo2(ConfigName, "Treatment")

           

          If (Need help here!!!)

          retval = Part.AddCustomInfo3(ConfigName, "Treatment", swCustomInfoText, "R/C 56 MIN")

          End If

            • Re: Simple API Help
              Deepak Gupta

              Try adding/using these codes:

               

              Dim sMatName                    As String

              Dim sMatDB                      As String

              Dim swPart                      As SldWorks.PartDoc

               

              Set swPart = swModel

              sMatName = swPart.GetMaterialPropertyName2("Default", sMatDB)

               

                  If sMatName = "A2" Then

                  swModel.CustomInfo2(Configuration, "Treatment") = "R/C 56 MIN"

                  End If

              Though custominfo2  is an obsolete property but works. Using custominfo2 you can change the value of the existing property instead of deleting it and re-adding it.

                • Re: Simple API Help
                  Greg Raymond

                  Thanks! That works like a charm. Now is there any way to add the "Treatment" property in there? It's in the template of the part but just in case someone deletes it or we take an old part with an old template.

                  • Re: Simple API Help
                    Deepak Gupta

                    It would be better to assign a new variable say Value

                     

                    Dim sMatName                    As String

                    Dim sMatDB                      As String

                    Dim swPart                      As SldWorks.PartDoc

                    Dim Value                          As string

                     

                    Set swPart = swModel

                    sMatName = swPart.GetMaterialPropertyName2("Default", sMatDB)

                     

                        Select Case sMatName

                              Case "A2"

                                  Value = "R/C 56 MIN"

                              Case add other checks here

                                  actions for other cases here

                            Case Else

                                    use only if an action is required when no match is found

                        End Select

                      

                        If swModel.CustomInfo2(Configuration, "Treatment") = "" Then

                            retval = swModel.AddCustomInfo3(Configuration, "Treatment", swCustomInfoText, Value)

                          Else

                            swModel.CustomInfo2(Configuration, "Treatment") = Value

                        End If

                • Re: Simple API Help
                  Matt Martens

                  In your original post you mentioned having 6 if statements. Instead of having 6 ifs, use a select case statement.

                   

                  Expanding on Deepak's code it would look like this

                  Dim sMatName                    As String

                  Dim sMatDB                      As String

                  Dim swPart                      As SldWorks.PartDoc

                   

                  Set swPart = swModel

                  sMatName = swPart.GetMaterialPropertyName2("Default", sMatDB)

                   

                      Select Case sMatName

                            Case "A2"

                                swModel.CustomInfo2(Configuration, "Treatment") = "R/C 56 MIN"

                            Case add other checks here

                                 actions for other cases here

                          Case Else

                                  use only if an action is required when no match is found

                       End Select

                      

                      End If

                  You can have as many cases as you like. Just keep adding them in.