7 Replies Latest reply on May 30, 2016 9:45 AM by Danilo Martins

    Differences between write code in VBA editor and in the properties

    Danilo Martins

      Hello there,

       

       

      I'm trying to get a simple macro to run under the properties of the file. As example i'll use a macro to calculate the volume of a part in dm³. As i'm using SolidWorks 2012, I don't have the unit cubic decimeters.

       

       

      That's the simpler code that works for me:

       

       

      ------------------------------------------------------------------------------

       

       

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim swApp As SldWorks.SldWorks

      Dim swPart As SldWorks.PartDoc

      Dim swSelMgr As SldWorks.SelectionMgr

      Dim mass As SldWorks.MassProperty

      Dim retval As String

      Dim Volume As String

       

       

      Sub main()

       

       

              Set swApp = CreateObject("SldWorks.Application")

              Set Part = swApp.ActiveDoc

              Set mass = Part.Extension.CreateMassProperty

              boolstatus = mass.AddBodies((compbody))

              mass.UseSystemUnits = False

              Volume = mass.Volume / 1000000

              Part.DeleteCustomInfo ("VOLUME dm3")

              Part.AddCustomInfo3 "", "VOLUME dm3", swCustomInfoText, FormatNumber(Volume, 3, vbUseDefault, vbUseDefault, vbUseDefault)

       

       

      End Sub

       

       

      ------------------------------------------------------------------------------

       

       

      All I want is to put the code under the properties of a part, and call it using Equations. I know that I can't do some things calling the macro under properties, but what is wrong? Can anyone explain more about this differences?

       

       

      Thanks a lot! I hope this informations opens my world!

        • Re: Differences between write code in VBA editor and in the properties
          Danilo Martins

          Just to explain, if my question doesn't make sense for everybody, I wan't to put the code in the properties because I don't wan't to run the macro manually. I need that in every Ctrl+Q or rebuild the volume get updated.

            • Re: Differences between write code in VBA editor and in the properties
              Drew Adams

              I think it sounds like you need an "add-in" and not a macro.  An Add-in can run constantly in the background, but the macro would have to be called to run on its own each time.  You could customize the toolbar/keyboard to create a hotkey to run the macro though, which is what I would recommend should you decide not to go the Add-in route.

               

              Hope that helps.

               

              -Drew

              • Re: Differences between write code in VBA editor and in the properties
                Jim Sculley

                Use a macro feature to run macro code on every rebuild.  Search the API Help for 'macro feature' for details and examples.

                 

                Jim S.

                  • Re: Differences between write code in VBA editor and in the properties
                    Danilo Martins

                    Well, it seems to do what I want.

                    How to create this macro feature?

                      • Re: Differences between write code in VBA editor and in the properties
                        Daniel Andersson

                        You could run the macro on event, rebuild. See the bottom of this page for all events that can be used in SolidWorks.

                        To use events with macros you have to launch the macro at some point. Once launched it will stay in background and respond to the events you trigger.

                         

                        The macro can be launched at the same time as SolidWorks by making a new shortcut. See this post with macro example for events and for how to add a SolidWorks shortcut that launches a macro on start up.

                         

                        Using events will implement this to ALL files that is being opened and rebuilt. IF statements and/or select case could be used to get some filtering done depending on filename or custom properties etc.

                         

                        If you want just for specifc documents from time to time, then you should go for adding a macro icon and add a shortcut to it. It may be possible to change the ctrl+q to run the macro instead. A minor tweak of the macro is then needed, so that the document also rebuilt when the macro is executed.

                         

                        The option with add-in is something to consider if you have multiple users and is even more relevant if the actions should be event based.

                         

                        Please let me know if you need any further assistance.

                         

                        // Daniel

                          • Re: Differences between write code in VBA editor and in the properties
                            Daniel Andersson

                            I had a second thought about this and changed my mind.

                             

                            I think the best would be to have a macro that adds a equation and a custom property that is linked to the equation. Once done you do not have to think of rebuilds etc.

                             

                            If you like I could help you to set up a macro like that.

                              • Re: Differences between write code in VBA editor and in the properties
                                Danilo Martins

                                Daniel, 2 years from there and again I'm in trouble.

                                I created a macro an saved it in the part file, so a equation calls it on every rebuild.

                                But thats my new problem, since it calls my macro on every rebuild, it remains opened! When I try to edit a macro I see a lot of instances of the macro that the equation calls opened, and consuming all my memory.

                                 

                                 

                                Below my current macro. Any help?

                                 

                                ---------------------------------------------

                                 

                                Option Explicit

                                 

                                    Dim swApp               As SldWorks.SldWorks

                                    Dim swModel             As SldWorks.ModelDoc2

                                    Dim swConfigMgr         As SldWorks.ConfigurationManager

                                    Dim swConfig            As SldWorks.Configuration

                                    Dim swCustPropMgr       As SldWorks.CustomPropertyManager

                                 

                                 

                                    Dim valOut              As String

                                    Dim TIPO      As String

                                   

                                    Dim retval As Boolean

                                   

                                    Dim ALTURA As Variant

                                    Dim LARGURA As Variant

                                    Dim COMPRIMENTO As Variant

                                    Dim MEDIDA1 As Variant

                                    Dim MEDIDA2 As Variant

                                    Dim MEDIDA3 As Variant

                                    Dim DIAMETRO As Variant

                                    Dim CANTOS As Variant

                                 

                                Sub main()

                                 

                                 

                                    Set swApp = Application.SldWorks

                                    Set swModel = swApp.ActiveDoc

                                    Set swConfigMgr = swModel.ConfigurationManager

                                    Set swConfig = swConfigMgr.ActiveConfiguration

                                    'Debug.Print "Name of this configuration:" & swConfig.Name

                                 

                                    Set swCustPropMgr = swConfig.CustomPropertyManager

                                 

                                 

                                    swCustPropMgr.Get2 "TIPO", valOut, TIPO

                                    'MsgBox TIPO

                                       

                                If TIPO = "MANUAL" Or TIPO = "PERFIL" Then 'ENCERRAR PREENCHIMENTO AUTOMÁTICO

                                    End

                                End If

                                 

                                 

                                If TIPO <> "TRABALHADO" And TIPO <> "CHAPA" And TIPO <> "EIXO" And TIPO <> "BRUTO" And TIPO <> "TORNEADO" And TIPO <> "BARRA QUADRADA" And TIPO <> "BARRA REDONDA" Then

                                    MsgBox "Tipo de fabricação não conhecido." + vbCrLf + _

                                            "Preencha o TIPO como uma das opções:" + vbCrLf + _

                                            "TRABALHADO, TORNEADO, CHAPA, EIXO, BRUTO, BARRA QUADRADA ou BARRA REDONDA" + vbCrLf + vbCrLf + _

                                            "Ou preecha com MANUAL para escrever manualmente conforme necessário"

                                    End

                                End If

                                 

                                'MsgBox TIPO

                                 

                                'LER MEDIDAS

                                CANTOS = swModel.GetPartBox(True)         'True comes back as system units - meters

                                 

                                ALTURA = Round((Abs(CANTOS(4) - CANTOS(1)) * 1000), 2) ' Z axis

                                LARGURA = Round((Abs(CANTOS(5) - CANTOS(2)) * 1000), 2)  ' Y axis

                                COMPRIMENTO = Round((Abs(CANTOS(3) - CANTOS(0)) * 1000), 2) ' X axis

                                 

                                'ORDENAR AS DIMENSOES

                                If ALTURA > LARGURA Then

                                    If ALTURA > COMPRIMENTO Then

                                        MEDIDA1 = ALTURA

                                        If LARGURA > COMPRIMENTO Then

                                        MEDIDA2 = LARGURA

                                        MEDIDA3 = COMPRIMENTO

                                        Else

                                        MEDIDA2 = COMPRIMENTO

                                        MEDIDA3 = LARGURA

                                        End If

                                    Else

                                        MEDIDA1 = COMPRIMENTO

                                        MEDIDA2 = ALTURA

                                        MEDIDA3 = LARGURA

                                    End If

                                Else

                                    If COMPRIMENTO > LARGURA Then

                                        MEDIDA1 = COMPRIMENTO

                                        MEDIDA2 = LARGURA

                                        MEDIDA3 = ALTURA

                                    Else

                                        MEDIDA1 = LARGURA

                                        MEDIDA2 = ALTURA

                                        MEDIDA3 = COMPRIMENTO

                                    End If

                                End If

                                 

                                'MsgBox MEDIDA1 & " x " & MEDIDA2 & " x " & MEDIDA3 'MOSTRAR MEDIDAS

                                 

                                'If TIPO <> "MANUAL" Then

                                    swCustPropMgr.Delete "DIMENSIONAL"

                                'End If

                                 

                                'PREENCHER DE ACORDO COM TIPO

                                If TIPO = "TRABALHADO" Then

                                    retval = swCustPropMgr.Add2("DIMENSIONAL", swCustomInfoText, _

                                            "t. " & MEDIDA3 & " x " & MEDIDA2 & " x " & MEDIDA1)

                                 

                                ElseIf TIPO = "CHAPA" Then

                                    retval = swCustPropMgr.Add2("DIMENSIONAL", swCustomInfoText, _

                                             "ch. " & MEDIDA3 & " x " & MEDIDA2 & " x " & MEDIDA1)  'ESCREVER VALORES

                                 

                                ElseIf TIPO = "EIXO" Then

                                    If MEDIDA1 = MEDIDA2 Then

                                            DIAMETRO = MEDIDA1

                                            COMPRIMENTO = MEDIDA3

                                        ElseIf MEDIDA2 = MEDIDA3 Then

                                            DIAMETRO = MEDIDA2

                                            COMPRIMENTO = MEDIDA1

                                        ElseIf MEDIDA1 = MEDIDA3 Then

                                            DIAMETRO = MEDIDA3

                                            COMPRIMENTO = MEDIDA2

                                    End If

                                    retval = swCustPropMgr.Add2("DIMENSIONAL", swCustomInfoText, _

                                             "Ø" & DIAMETRO & " x " & COMPRIMENTO)  'ESCREVER VALORES

                                            

                                ElseIf TIPO = "BRUTO" Then

                                    retval = swCustPropMgr.Add2("DIMENSIONAL", swCustomInfoText, _

                                             "Br. " & MEDIDA3 & " x " & MEDIDA2 & " x " & MEDIDA1)  'ESCREVER VALORES

                                            

                                ElseIf TIPO = "BARRA QUADRADA" Then

                                    retval = swCustPropMgr.Add2("DIMENSIONAL", swCustomInfoText, _

                                             "barra " & MEDIDA3 & " x " & MEDIDA2 & " x " & MEDIDA1)  'ESCREVER VALORES

                                            

                                ElseIf TIPO = "BARRA REDONDA" Then

                                    If MEDIDA1 = MEDIDA2 Then

                                            DIAMETRO = MEDIDA1

                                            COMPRIMENTO = MEDIDA3

                                        ElseIf MEDIDA2 = MEDIDA3 Then

                                            DIAMETRO = MEDIDA2

                                            COMPRIMENTO = MEDIDA1

                                        ElseIf MEDIDA1 = MEDIDA3 Then

                                            DIAMETRO = MEDIDA3

                                            COMPRIMENTO = MEDIDA2

                                    End If

                                    retval = swCustPropMgr.Add2("DIMENSIONAL", swCustomInfoText, _

                                             "barra Ø" & DIAMETRO & " x " & COMPRIMENTO)  'ESCREVER VALORES

                                            

                                ElseIf TIPO = "TORNEADO" Then

                                    If MEDIDA1 = MEDIDA2 Then

                                            DIAMETRO = MEDIDA1

                                            COMPRIMENTO = MEDIDA3

                                        ElseIf MEDIDA2 = MEDIDA3 Then

                                            DIAMETRO = MEDIDA2

                                            COMPRIMENTO = MEDIDA1

                                        ElseIf MEDIDA1 = MEDIDA3 Then

                                            DIAMETRO = MEDIDA3

                                            COMPRIMENTO = MEDIDA2

                                    End If

                                    retval = swCustPropMgr.Add2("DIMENSIONAL", swCustomInfoText, _

                                             "t. Ø" & DIAMETRO & " x " & COMPRIMENTO)  'ESCREVER VALORES

                                Else

                                    'MsgBox "TIPO """ & TIPO & """ NÃO CADASTRADO PARA PREENCHIMENTO DO DIMENSIONAL AUTOMÁTICO."

                                 

                                End If

                                      

                                End Sub