12 Replies Latest reply on Jul 31, 2018 1:26 PM by Deepak Gupta

    Add custom property to all assembly components using macro and Input Box

    Mike Landro

      Hi All,

      I am attempting to modify this macro Re: Add custom property to all assembly components using macro  from Ivana Kolin to get a work order number from a text input box then add a custom property Label1 to the assembly and all of the parts in a specific configuration. Right now the input box is popping up once for each part in the assembly how can I get it to pop up once and apply the work order number to the assembly and all of the parts in the assembly? I am new to Solidworks Is there a better/easier way to do this?

      Thanks, Mike

      Solidworks 2017

       

        • Re: Add custom property to all assembly components using macro and Input Box
          Ivana Kolin

          I hate code pictures ...........

           

          move inputbox to Sub Main

           

          and pass wo_num as parameter to UpdateProperty

            • Re: Add custom property to all assembly components using macro and Input Box
              Deepak Gupta

              Ivana Kolin wrote:

               

              I hate code pictures ...........

               

              Me too

              • Re: Add custom property to all assembly components using macro and Input Box
                Mike Landro

                Rookie move on the code picture...won't do that again

                 

                I had tried moving the input box to the sub but could not figure out how to pass wo_num where would I put that in the code?

                 

                Option Explicit
                Dim swApp As SldWorks.SldWorks
                Dim swModel As ModelDoc2
                Dim vComps As Variant
                Dim swComp As SldWorks.Component2
                Dim swAssy As SldWorks.AssemblyDoc
                Dim i As Integer
                Dim wo_num As String

                Sub main()

                Set swApp = Application.SldWorks
                Set swModel = swApp.ActiveDoc

                wo_num = InputBox("Enter Work Order Number")

                updateProperty swModel

                If swModel.GetType = swDocASSEMBLY Then
                    Set swAssy = swModel
                    vComps = swAssy.GetComponents(False)
                    For i = 0 To UBound(vComps)
                        Set swComp = vComps(i)
                        If swComp.GetSuppression = swComponentFullyResolved Then
                            Set swModel = swComp.GetModelDoc2
                            updateProperty swModel
                        Else
                            MsgBox "warn user lightweight or suppressed"
                        End If
                       
                    Next i

                End If

                 

                End Sub

                 

                Function updateProperty(swModel As SldWorks.ModelDoc2) As Boolean
                Dim cpm As CustomPropertyManager
                Dim path As String, wo_num As String

                wo_num = InputBox("Enter Work Order Number")

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

                cpm.Delete "Label1"

                cpm.Add2 "Label1", swCustomInfoText, wo_num

                End Function

                  • Re: Add custom property to all assembly components using macro and Input Box
                    Deepak Gupta

                    Try this

                     

                    Option Explicit
                    Dim swApp As SldWorks.SldWorks
                    Dim swModel As ModelDoc2
                    Dim vComps As Variant
                    Dim swComp As SldWorks.Component2
                    Dim swAssy As SldWorks.AssemblyDoc
                    Dim i As Integer
                    Dim wo_num As String
                    Sub main()
                    Set swApp = Application.SldWorks
                    Set swModel = swApp.ActiveDoc
                    wo_num = InputBox("Enter Work Order Number")
                    
                    updateProperty swModel, wo_num
                    
                    If swModel.GetType = swDocASSEMBLY Then
                        Set swAssy = swModel
                        vComps = swAssy.GetComponents(False)
                        For i = 0 To UBound(vComps)
                            Set swComp = vComps(i)
                            If swComp.GetSuppression = swComponentFullyResolved Then
                                Set swModel = swComp.GetModelDoc2
                                updateProperty swModel, wo_num
                            Else
                                MsgBox "warn user lightweight or suppressed"
                            End If
                            
                        Next i
                    End If 
                    End Sub
                    
                    Function updateProperty(swModel As SldWorks.ModelDoc2, mValue As String) As Boolean
                    Dim cpm As CustomPropertyManager
                    Set cpm = swModel.Extension.CustomPropertyManager("")
                    cpm.Add3 "Label1", swCustomInfoText, mValue, 1
                    End Function
                    
                      • Re: Add custom property to all assembly components using macro and Input Box
                        Mike Landro

                        That's exactly what I was trying to do, thanks all for the help

                        • Re: Add custom property to all assembly components using macro and Input Box
                          Mathew Bush

                          Deepak,

                          This macro is a great start to what I am trying to accomplish. Could you guide me in adding a counter to this. The end result I am trying to achieve is add a consecutive number in all my parts of an assembly.

                          An example would be if I added 12345 it would add 001 to give me 12345001 on the first part and 12345002 and so forth.

                           

                          Thanks

                          Matt

                            • Re: Add custom property to all assembly components using macro and Input Box
                              Deepak Gupta

                              Mathew, try the following codes (have not tested them with multiple instance of same component)

                               

                              Option Explicit
                              Dim swApp As SldWorks.SldWorks
                              Dim swModel As ModelDoc2
                              Dim vComps As Variant
                              Dim swComp As SldWorks.Component2
                              Dim swAssy As SldWorks.AssemblyDoc
                              Dim i As Integer
                              Dim wo_num As String
                              Dim counter As Integer
                              
                              Sub main()
                              counter = 1
                              
                              Set swApp = Application.SldWorks
                              Set swModel = swApp.ActiveDoc
                              
                              wo_num = InputBox("Enter Work Order Number")
                                
                              updateProperty swModel, wo_num
                                
                              If swModel.GetType = swDocASSEMBLY Then
                                  Set swAssy = swModel
                                  vComps = swAssy.GetComponents(False)
                                  For i = 0 To UBound(vComps)
                                      Set swComp = vComps(i)
                                      If swComp.GetSuppression = swComponentFullyResolved Then
                                          Set swModel = swComp.GetModelDoc2
                                              If counter <= 9 Then
                                                  updateProperty swModel, wo_num & "00" & counter
                                              ElseIf counter >= 9 Then
                                                  updateProperty swModel, wo_num & "0" & counter
                                              End If
                                      Else
                                          MsgBox "warn user lightweight or suppressed"
                                      End If
                                        counter = counter + 1
                                  Next i
                              End If
                              End Sub
                                
                              Function updateProperty(swModel As SldWorks.ModelDoc2, mValue As String) As Boolean
                              Dim cpm As CustomPropertyManager
                              Set cpm = swModel.Extension.CustomPropertyManager("")
                              cpm.Add3 "Label1", swCustomInfoText, mValue, 1
                              End Function
                              
                      • Re: Add custom property to all assembly components using macro and Input Box
                        John Stoltzfus

                        Mike Landro wrote:

                        I am new to Solidworks Is there a better/easier way to do this?

                        Thanks, Mike

                        Solidworks 2017

                        Yes - Custom Property Tab Builder