3 Replies Latest reply on Apr 4, 2018 12:03 PM by Scott Werner

    Macro to create a Custom Property from cutlist folder name

    Scott Werner

      Hello,

       

      I have a macro that I found here that I am trying to modify to create a Property with the Cutlist Folder Name as its value like the MK# shown here that I manually inserted:

       

      I've modified the macro and it runs but unfortunately doesn't actually do anything.  Any help would be appreciated and if you could point me to some resources to learn Solidworks specific VBA I would love that as well.  Thank you in advance!

       

      Here is the macro:

       

      Option Explicit

          Dim swApp               As SldWorks.SldWorks

          Dim swModel             As SldWorks.ModelDoc2

          Dim swFeat              As SldWorks.Feature

          Dim swCustPropMgr       As SldWorks.CustomPropertyManager

          Dim MemberMKnumber      As String

          Dim BodyFolder          As SldWorks.BodyFolder

          Dim doc As SldWorks.ModelDoc2: Set doc = swApp.ActiveDoc

         

         

         

         

      Sub main()

          On Error Resume Next

           

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")

          swCustPropMgr.Get4 "Mark No. Part", False, MemberMKnumber, ""

             

          Set swFeat = swModel.FirstFeature

              Do While Not swFeat Is Nothing

                  If swFeat.GetTypeName() = "CutListFolder" Then

                              Set swCustPropMgr = swFeat.CustomPropertyManager

                              swCustPropMgr.Add3 "MK#", swCustomInfoText, "SW-CutListItemName@@@" + BodyFolder + "@" + doc.GetTitle + ".SLDPRT"

                  End If

              Set swFeat = swFeat.GetNextFeature

              Loop

      End Sub

        • Re: Macro to create a Custom Property from cutlist folder name
          Michael Spens

          Hi Scott,

          You had a few minor syntax errors.  First, you don't need the declaration for doc in your global variables.  The syntax for the folder name can be simplifed to "SW-CutListItemName@PartName.SLDPRT" so you don't need the folder's name.   Though you could get that by calling swFeat.Name.

           

          Watch out for adding the file extension in your syntax.  You only need to do that when you don't display file extensions in Windows Explorer.  If you turn on file extensions, you'll be getting something like "SW-CutListItemName@2008 table.SLDPRT.SLDPRT" which won't resolve to a value.

           

          Many resources for learning the API.  This post has a few suggestions.

          Macro, scan feature tree of assembly, rename parts

           

          Try replacing some of your loop code with this for better results.

           

                      If swFeat.GetTypeName() = "CutListFolder" Then

                                  Set swCustPropMgr = swFeat.CustomPropertyManager

                                  Dim propVal As String

                                  propVal = """SW-CutListItemName@" & swModel.GetTitle + ".SLDPRT"""

                                  swCustPropMgr.Add3 "MK#", swCustomInfoText, propVal, swCustomPropertyDeleteAndAdd

                      End If

           

          Mike

            • Re: Macro to create a Custom Property from cutlist folder name
              Scott Werner

              Thanks so much for your reply Michael!

               

              I've tried to implement you code into my loop but I end up with the syntax instead of the folder name.  I think that's because you forgot to add the folder name in your syntax.  I tried to add that in but still cannot get it to work.  This is what I get with your loop:

              This is the way I tried to add the folder name in using the BodyFolder from the original macro:

               

                        If swFeat.GetTypeName() = "CutListFolder" Then

               

                                      Set swCustPropMgr = swFeat.CustomPropertyManager

               

                                      Dim propVal As String

               

                                      propVal = """SW-CutListItemName@@@" & BodyFolder + "@" & swModel.GetTitle + ".SLDPRT"""

               

                                      swCustPropMgr.Add3 "MK#", swCustomInfoText, propVal, swCustomPropertyDeleteAndAdd

               

                          End If

              • Re: Macro to create a Custom Property from cutlist folder name
                Scott Werner

                Michael,

                 

                Thanks so much!!  Please disregard my previous reply,  I re-read your statement and was able to edit your loop to make it work, I've re-attached the macro if its any use to anyone else.

                 

                Thanks again!!

                 

                Edited to add the code, couldn't attach:

                 

                Option Explicit

                    Dim swApp               As SldWorks.SldWorks

                    Dim swModel             As SldWorks.ModelDoc2

                    Dim swFeat              As SldWorks.Feature

                    Dim swCustPropMgr       As SldWorks.CustomPropertyManager

                    Dim MemberMKnumber      As String

                   

                 

                   

                   

                   

                   

                Sub main()

                    On Error Resume Next

                     

                    Set swApp = Application.SldWorks

                    Set swModel = swApp.ActiveDoc

                    Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")

                    swCustPropMgr.Get4 "Mark No. Part", False, MemberMKnumber, ""

                       

                    Set swFeat = swModel.FirstFeature

                        Do While Not swFeat Is Nothing

                             If swFeat.GetTypeName() = "CutListFolder" Then

                 

                                        Set swCustPropMgr = swFeat.CustomPropertyManager

                 

                                        Dim propVal As String

                 

                                        propVal = """SW-CutListItemName@@@" & swFeat.Name + "@" & swModel.GetTitle + ".SLDPRT"""

                 

                                        swCustPropMgr.Add3 "MK#", swCustomInfoText, propVal, swCustomPropertyDeleteAndAdd

                 

                            End If

                        Set swFeat = swFeat.GetNextFeature

                        Loop

                End Sub