5 Replies Latest reply on Jun 21, 2018 9:03 AM by Frederick Law

    Auto Run Macro

    Patrick Krug

      Hello everyone,

      is it possible to run a macro that change the appearance of a part

      when i press the apply button to save the custom properties?

       

      Thanks in advance

      Patrick

        • Re: Auto Run Macro
          Deepak Gupta

          Do you mean apply button on the properties window or property builder ?

            • Re: Auto Run Macro
              Patrick Krug

              properties.JPG

              I mean the button in this window.

               

              I want to type a "RAL Code" in  the blue marked text field "Farbgebung/Oberfläche" save it with the button and

              then the macro should run one time.

                • Re: Auto Run Macro
                  Michael Spens

                  Hi Patrick,

                  The Custom Property Tab controls don't have event handlers, so unfortunately no.  Alternatively you could add a macro button that reads the property and sets your color or trigger the color setting at the FileSaveNotifyEventHandler (or rebuild, but that's probably too often).

                  2018 SOLIDWORKS API Help - DPartDocEvents_FileSaveNotifyEventHandler Delegate (SolidWorks.Interop.sldworks)

                   

                  Mike

                    • Re: Auto Run Macro
                      Patrick Krug

                      Hey Mike,

                      Thank you for that...

                      The problem is, I do not even know event handlers. Can you maybe do that for me if i post my code here?

                       

                      Dim swApp As SldWorks.SldWorks

                      Dim swModelDocExt As SldWorks.ModelDocExtension

                      Dim swAppearance As SldWorks.RenderMaterial

                       

                      Dim Part As Object

                      Dim swModel As ModelDoc2

                      Dim SWmoddoc As ModelDoc2

                      Dim boolstatus As Boolean

                      Dim propColor As String

                      Dim vColor As String

                      Dim vColorPath As String

                      Dim errColor As String

                      Dim fso As Object

                      Dim oFile As Object

                      Dim txt As String

                       

                      Sub main()

                       

                          txt = "G:\Konstruktion\_Ablagen\Ablage_Patrick Krug\_Solidworks\Erscheinungsbilder\add.txt"

                          errColor = "G:\Konstruktion\_Ablagen\Ablage_Patrick Krug\_Solidworks\Erscheinungsbilder\err.p2m"

                          Debug.Print (errColor)

                       

                          Set swApp = Application.SldWorks

                          Set Part = swApp.ActiveDoc

                          Set SWmoddoc = swApp.ActiveDoc

                          Set swModelDocExt = Part.Extension

                         

                          Part.ClearSelection2 True

                       

                          If Part Is Nothing Then Exit Sub

                          If (Part.GetType() = swDocumentTypes_e.swDocDRAWING) Then Exit Sub

                         

                          Debug.Print ""

                          Debug.Print "---------------------------------------------------------------------------------------------------------------------------------------"

                         

                          propColor = SWmoddoc.GetCustomInfoValue(Standard, "Farbe")

                          Debug.Print "propColor: " & propColor

                         

                          If propColor = "" Then Exit Sub

                         

                          vColor = propColor + ".p2m"

                          Debug.Print "vColor: " & vColor

                         

                          vColorPath = "G:\Konstruktion\_Ablagen\Ablage_Patrick Krug\_Solidworks\Erscheinungsbilder\" & vColor

                          Debug.Print "vColorPath: " & vColorPath

                         

                          If FileExists(vColorPath) Then

                              ' Erscheinunsbild festlegen

                              Set swAppearance = swModelDocExt.CreateRenderMaterial(vColorPath)

                              Debug.Print "Set Appearance: " & vColor

                              'swModel.Extension.ShowSmartMessage "Set Appearance: " & vColor, 4000, False, False

                              boolstatus = swAppearance.AddEntity(Part)

                              boolstatus = swModelDocExt.AddRenderMaterial(swAppearance, 0)

                          Else

                              Set swAppearance = swModelDocExt.CreateRenderMaterial(errColor)

                              boolstatus = swAppearance.AddEntity(Part)

                              boolstatus = swModelDocExt.AddRenderMaterial(swAppearance, 0)

                             

                              MsgBox "Erscheinungsbild noch nicht vorhanden!", vbExclamation + vbOKOnly, "Fehler"

                              Set fso = CreateObject("Scripting.FileSystemObject")

                              Set oFile = fso.OpenTextFile(txt, ForAppending, True)

                              oFile.WriteLine propColor '& vbCrLf            'Fehlende Farbe in add.txt schreiben!

                              oFile.Close

                              Set fso = Nothing

                              Set oFile = Nothing

                              Exit Sub

                          End If

                       

                      End Sub

                       

                      Function FileExists(ByVal Dateipfad As String) As Boolean

                       

                      'Quelle: www.dbwiki.net oder www.dbwiki.de

                       

                      FileExists = CreateObject("Scripting.FileSystemObject").FileExists(Dateipfad)

                       

                      End Function

                      • Re: Auto Run Macro
                        Frederick Law

                        Maybe use the CustomProperty change event:

                        DAssemblyDocEvents_ChangeCustomPropertyNotifyEventHandler Delegate (SolidWorks.Interop.sldworks)

                        DPartDocEvents_ChangeCustomPropertyNotifyEventHandler Delegate (SolidWorks.Interop.sldworks)