5 Replies Latest reply on Aug 21, 2017 4:55 AM by Deepak Gupta

    Macro Help

    Eric Wicklund

      I was wondering if someone would have a good article or could point me in the right direction to learn how to write effective macro's.

       

      I recently purchased a G13 gaming keypad and would love to have all of my "common tasks" assigned to a macro that would be activated by the hotkey on the keypad. That potion of it i am not concerned with, its the actual writing of the macros.

       

      I would like to have macros

      1. Many times i have a part title that is to long and overflows the title block so i have to go in and edit the sheet format and make the font smaller; 14pt to 10pt.
      2. Insert a pre-made note style from the design library
      3. I have a macro that will add properties IF they do not exist already, it works just fine but i am curious how i can add the weight and material already assigned/calculated. (see picture)

      here is what i have for a macro for line item 3;

       

      Option Explicit

      Public Enum swDocumentTypes_e

          swDocNONE = 0

          swDocPART = 1

          swDocASSEMBLY = 2

      End Enum

       

       

      Sub main()

          Dim swApp As Object

          Dim Part As Object

          Dim tDate As Date

        

          Set swApp = CreateObject("SldWorks.Application")

          Set Part = swApp.ActiveDoc  ' Get currently active document

        

          Select Case Part.GetType

        

              Case swDocPART ' If active file is a part then insert PART custom properties

                   Part.AddCustomInfo "PartNo", "Text", "****" ' Part Number

                   Part.AddCustomInfo "Title", "Text", "****" ' Title

                   Part.AddCustomInfo "Description", "Text", "****" ' Description

                   Part.AddCustomInfo "Revision", "Text", "1" ' Revision

                   Part.AddCustomInfo "DrawnBy", "Text", "EAW" ' Drawn By

                 ' Part.AddCustomInfo "DrawnDate", "Text", tDate ' Drawn Date

                   Part.AddCustomInfo "EngineeringApproval", "Text", "MAW" ' Engineering Approval

                   Part.AddCustomInfo "Finish", "Text", "BLACK ANODIZE" ' Finish

                   Part.AddCustomInfo "Material", "Text", "---" ' Material

                   Part.AddCustomInfo "Weight", "Text", "---" ' Weight

                   Part.AddCustomInfo "Vendor", "Text", "**" ' Vendor

                   Part.AddCustomInfo "VendorNo", "Text", "N/A" ' Vendor P/N

                   Part.AddCustomInfo "Manufacturer", "Text", "N/A" ' Manufacturer

                   Part.AddCustomInfo "Manufacturer PN", "Text", "N/A" ' Manufacturer P/N

                   Part.DeleteCustomInfo "Desc1"   'Removes existing property from downloaded McMaster Carr part

                   Part.DeleteCustomInfo "Desc2"   'Removes existing property from downloaded McMaster Carr part

                   Part.DeleteCustomInfo "Desc3"   'Removes existing property from downloaded McMaster Carr part

        

              Case swDocASSEMBLY ' If active file is an assembly then insert ASSEMBLY custom properties

                   Part.AddCustomInfo "PartNo", "Text", "SEE BOM" ' Part Number

                   Part.AddCustomInfo "Title", "Text", "****" ' Title

                   Part.AddCustomInfo "Description", "Text", "****" ' Description

                   Part.AddCustomInfo "Revision", "Text", "1" ' Revision

                   Part.AddCustomInfo "DrawnBy", "Text", "EAW" ' Drawn By

                 ' Part.AddCustomInfo "DrawnDate", "Text", tDate ' Drawn Date

                   Part.AddCustomInfo "EngineeringApproval", "Text", "MAW" ' Engineering Approval

                   Part.AddCustomInfo "Finish", "Text", "SEE BOM" ' Finish

                   Part.AddCustomInfo "Material", "Text", "SEE BOM" ' Material

                   Part.AddCustomInfo "Weight", "Text", "---" ' Weight

        • Re: Macro Help
          Deepak Gupta

          For Material and Mass use

          Part.AddCustomInfo "Material", "Text", Chr(34) & "SW-Material" & "@" & swModel.GetTitle & ".SLDPRT" & Chr(34) ' Material

          Part.AddCustomInfo "Weight", "Text", Chr(34) & "SW-Mass" & "@" & swModel.GetTitle & ".SLDPRT" & Chr(34) ' Weight

          Make sure you've file extension set to hidden else swModel.GetTitle would have extension included.

           

          Also AddCustomInfo is obsolete now, use swModel.Extension.CustomPropertyManager instead.

            • Re: Macro Help
              Elmar Klammer

              Hello,

               

              Just a little general info. Deepak probably already knows this. You don't have to get the title of the component for SW to resolve the property. The generic form "SW-Material" & "@" & "part.SLDPRT" & Chr(34) will automatically convert to the file name when added.

              That formula works always. It doesn't need to say part. You can try "SW-Material@1.SLDPRT" if you like and see for yourself. For Assemblies replace sldprt with sldasm and it will work too.

               

              For custom props no rebuild is required. With configuration specific props a rebuild all will due the trick.

               

              Elmar

            • Re: Macro Help
              Nilesh Patel

              Hi Eric,

               

              You can create Part or Assembly document template with all custom properties that you need so that you don't have to enter them manually or via macro.

              • Re: Macro Help
                Eric Wicklund

                I know I can adjust the custom properties, but parts are usually pretty late files so it's faster to "save as" and make the adjustments you need so 95% of the time I'm not starting a new part