6 Replies Latest reply on Jul 25, 2017 8:41 AM by Ales Svoboda

    EPDM Task: Set Part Material & Configuration Variables.

    Donald Grunloh

      I would like to set up an EPDM task using a custom macro that performs the following functions:

      1) Check-out Files

      2) Open File

      3) Set part material to <insert material>

      4) Add variable to each configuration and set value

      5) Rebuild/Save

      6) Check-in File

       

      I have items 2,3,4,5 figured out using a macro based on work done by Deepak Gupta (how to change material in multiple parts ).

       

      Rather than use the "Browse Folder" function in this macro I would like to run this macro as a task.  Trouble is I cannot figure out how to check-out/check-in the files properly.  Any input is appreciated.

       

      I have attached the macro I currently use (outside of EPDM) to perform the task.

        • Re: EPDM Task: Set Part Material & Configuration Variables.
          Brian McEwen

          Donald,

           

          I'm not sure if this applies given you are doing something a little different than my experience, but since no else replied yet...

          "Rather than use the "Browse Folder" function in this macro I would like to run this macro as a task."

          Do you mean you want to use the SolidWorks Task Scheduler to run the EPDM Task?  If so the Task Scheduler is a pain when it comes to what you need to check-out.

           

          I found that I had to check out all referenced files, even when I just wanted to run it on a certain set of files (I wanted to update drawing data cards). To update the drawing I think the Task Scheduler tries to open and do something to all the referenced files, even though I picked "None" in the Load Referenced Documents part of the Convert Options dialog. It seems that unless I check out the drawing and everything it references the task will not work. It is not easy to check out all references since they are scattered. The log error it gives is "Referenced files read-only". 

           

          Also discovered that I could not add a top level folder to the scheduled task, I had  to add each sub folder as a separate step - otherwise the path came out wrong.   

          • Re: EPDM Task: Set Part Material & Configuration Variables.
            Jeff Sweeney

            This macro is a nice start, but yeah, you have some PDM API calls you'll need to add.

             

            Too much to cover here, but I recommend starting with "C:\Program Files\SOLIDWORKS Enterprise PDM\API_GB.chm". It is a great document to get you started with the PDM API. The checkout functions you are looking for are called "Lock" and "Unlock"

             

            If you want to get this done quick, as a one time thing, here is what I would do.

             

            1. Manually check out the files you want to update (this will get them in your local cache)
            2. Run the macro
            3. Manually check the files back in.

             

            If you really want to run this as a PDM task, you have quite a bit of work in front of you.

            • Re: EPDM Task: Set Part Material & Configuration Variables.
              Leonardo Magno

              I have a Task that does exactly that. Hope this helps.

               

              ' Description: Macro to add special properties to all configurations by configuration.
              '
              ' Precondition: An Active part / assembly.
              '
              ' Postcondition: New properties are added to file.
              '
              ' This macro is provided as is.  No claims, support, refund, safety net, or
              ' warranties are expressed or implied.  By using this macro and/or its code in
              ' any way whatsoever, the user and any entities which the user represents,
              ' agree to hold the authors free of any and all liability.  Free distribution
              ' and use of this code in other free works is welcome.  If any portion of
              ' this code is used in other works, credit to the authors must be placed in
              ' that work within a user viewable location (e.g., macro header).  All other
              ' forms of distribution (i.e., not free, fee for delivery, etc) are prohibited
              ' without the expressed written consent by the authors.  Use at your own risk!
              ' ------------------------------------------------------------------------------
              ' Written by: Leonardo Magno
              ' ------------------------------------------------------------------------------
              Option Explicit
              Dim swApp As Object
              Dim swModel As SldWorks.ModelDoc2
              Dim swDrawing As SldWorks.DrawingDoc
              Dim swAssembly As SldWorks.AssemblyDoc
              Dim swExtension As SldWorks.ModelDocExtension
              Dim swConfigMgr As SldWorks.ConfigurationManager
              Dim swConfig As SldWorks.Configuration
              Dim swCustomPropertyManager As SldWorks.CustomPropertyManager
              Dim swDocSpecification As SldWorks.DocumentSpecification
              Dim swModelDocExt As SldWorks.ModelDocExtension
              Dim swSelMgr As SldWorks.SelectionMgr
              Dim swFeat As SldWorks.Feature
              Dim swWeldBead As SldWorks.CosmeticWeldBeadFeatureData
              Dim swWeldBeadFolder As SldWorks.CosmeticWeldBeadFolder
              Dim FileSystemObj As Object
              Dim errors As Long
              Dim warnings As Long
              Dim docType As Integer
              Function bIsSupportedExtension(oExtension) As Boolean
                  oExtension = LCase(oExtension)
                  If oExtension = "prt" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "asm" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "drw" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "dxf" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "dwg" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "psd" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "ai" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "lfp" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "sldlfp" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "prtdot" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "asmdot" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "drwdot" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "x_t" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "x_b" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "xmt_txt" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "xmt_bin" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "igs" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "iges" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "step" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "stp" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "sat" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "vda" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "wrl" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "stl" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "cgr" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "wrl" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "xpr" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "xas" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "ipt" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "iam" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "par" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "psm" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "ckd" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "emn" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "brd" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "bdf" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "idb" Then
                     bIsSupportedExtension = True
                  ElseIf oExtension = "3dm" Then
                     bIsSupportedExtension = True
                  Else
                      bIsSupportedExtension = False
                  End If
              End Function
              Sub main()
                  Dim lRetVal As Integer
                  Dim mRetVal As Long
                  Dim nRetVal As Long
                  Dim strFilePath As String
                  Dim strFileName As String
                  Dim vConfigurations As Variant
                  Dim strMaterial As String
                  Dim strWeight As String
                  Dim strThickness As String
                  Dim strProjectDescription As String
                  Dim blnResolved As Boolean
                  Dim strResolved As String
                  Dim i As Long
                  Dim blnStatus As Boolean
                  Dim Vault As Object
                  Dim File As Object
                  Dim Folder As Object
                  'Dim Vault As EdmVault5
                  'Dim File As IEdmFile5
                  'Dim Folder As IEdmFolder5
                  Dim strVaultName As String
                  Dim blnIsLocked As Boolean
                  
                  Set FileSystemObj = CreateObject("Scripting.FileSystemObject")
                  Set swApp = Application.SldWorks
                  swApp.Visible = True
                  strFilePath = "<Filepath>"
                  Set Vault = CreateObject("ConisioLib.EdmVault") 'New EdmVault5
                  strVaultName = Vault.GetVaultNameFromPath(strFilePath)
                  Vault.LoginAuto strVaultName, 0
                  
                  Set File = Vault.GetFileFromPath(strFilePath, Folder)
                  'MsgBox "Vault: " & strVaultName & vbNewLine & _
                  '    "File: " & File.Name & vbNewLine & _
                  '    "Folder: " & Folder.LocalPath
                  
                  ' If file is checked in, check out the file for editing
                  If File.IsLocked = False Then
                      File.LockFile Folder.ID, 0
                      blnIsLocked = True
                  Else
                      blnIsLocked = False
                  End If
                  
                  ' Look for the file type
                  If LCase(Right(strFilePath, 7)) = ".sldprt" Or LCase(Right(strFilePath, 4)) = ".prt" Then
                      docType = swDocPART
                  ElseIf LCase(Right(strFilePath, 7)) = ".sldasm" Or LCase(Right(strFilePath, 4)) = ".asm" Then
                      docType = swDocASSEMBLY
                  ElseIf LCase(Right(strFilePath, 7)) = ".slddrw" Or LCase(Right(strFilePath, 4)) = ".drw" Then
                      docType = swDocDRAWING
                  Else
                      docType = swDocNONE
                       If bIsSupportedExtension(Mid(strFilePath, InStrRev(strFilePath, ".") + 1)) = False Then
                           MsgBox "The file extension """ & Mid(strFilePath, InStrRev(strFilePath, ".") + 1) & """ is not supported."
                           Exit Sub
                       End If
                  End If
                     
                  ' Open the document
                  If docType = swDocNONE Then
                      Set swModel = swApp.LoadFile4(strFilePath, "", Nothing, errors)
                      docType = swModel.GetType
                  Else
                      Set swDocSpecification = swApp.GetOpenDocSpec(strFilePath)
                      swDocSpecification.DocumentType = docType
                      swDocSpecification.ReadOnly = False
                      swDocSpecification.Silent = True
                      swDocSpecification.ConfigurationName = ""
                      swDocSpecification.DisplayState = ""
                      swDocSpecification.IgnoreHiddenComponents = True 'SPR 682792, 538578, 651998
                      Set swModel = swApp.OpenDoc7(swDocSpecification)
                      errors = swDocSpecification.Error
                  End If
                  Set swConfigMgr = swModel.ConfigurationManager
                  Set swConfig = swConfigMgr.ActiveConfiguration
                  ' Set default units
                  lRetVal = swModel.SetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swUnitSystem, swUnitSystem_e.swUnitSystem_MMGS)
                  
                  strFileName = swModel.GetTitle
                  ' Get the names of the configurations
                  vConfigurations = swApp.GetConfigurationNames(strFilePath)
                  For i = 0 To UBound(vConfigurations)
                      Set swCustomPropertyManager = swModel.Extension.CustomPropertyManager(vConfigurations(i))
                      
                      ' Material
                      If docType = swDocPART Then
                          strMaterial = """SW-Material@@" & vConfigurations(i) & "@" & strFileName & """"
                      Else
                          strMaterial = "N/A"
                      End If
                      lRetVal = swCustomPropertyManager.Add3("Material", swCustomInfoType_e.swCustomInfoText, strMaterial, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
                      
                      ' Weight
                      strWeight = """SW-Mass@@" & vConfigurations(i) & "@" & strFileName & """"
                      lRetVal = swCustomPropertyManager.Add3("Weight", swCustomInfoType_e.swCustomInfoText, strWeight, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
                      
                      ' Thickness
                      If docType = swDocPART Then
                          strThickness = """Thickness@" & vConfigurations(i) & "@" & strFileName & """"
                      Else
                          strThickness = "N/A"
                      End If
                      lRetVal = swCustomPropertyManager.Add3("Thickness", swCustomInfoType_e.swCustomInfoText, strThickness, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
                      
                      ' Project description
                      lRetVal = swCustomPropertyManager.Get5("Description", False, strProjectDescription, strResolved, blnResolved)
                      If strProjectDescription <> "" Then
                          If InStr(strProjectDescription, "File:") = False Then
                              strProjectDescription = strProjectDescription & vbNewLine & "File: " & strFileName
                          End If
                      Else
                          strProjectDescription = "File: " & strFileName
                      End If
                      lRetVal = swCustomPropertyManager.Add3("Description", swCustomInfoType_e.swCustomInfoText, strProjectDescription, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
                      
                      ' Bend count
                      ' Get the 1st feature in part
                      Dim Feature As SldWorks.Feature
                      Set Feature = swModel.FirstFeature
                      Dim FlatPatternCount As Integer
                      Dim FlattenBendsCount As Integer
                      Dim BendsCount As Integer
                      FlatPatternCount = 0
                      FlattenBendsCount = 0
                      ' While we have a valid feature
                      While Not Feature Is Nothing
                          ' Get the name of the feature
                          Dim FeatureName As String
                          Dim FeatureType As String
                          Dim SubFeat As SldWorks.Feature
                          FeatureType = Feature.GetTypeName2
                          If FeatureType = "FlatPattern" Or FeatureType = "FlattenBends" Then
                              Set SubFeat = Feature.GetFirstSubFeature
                              ' While we have a valid Sub-feature
                              While Not SubFeat Is Nothing
                                  ' Get the type of the Sub-feature
                                  If SubFeat.GetTypeName2 = "UiBend" Then
                                      FlatPatternCount = FlatPatternCount + 1
                                  ElseIf SubFeat.GetTypeName2 = "OneBend" Then
                                      FlattenBendsCount = FlattenBendsCount + 1
                                  End If
                                  Set SubFeat = SubFeat.GetNextSubFeature
                                  ' Continue until the last Sub-feature is done
                              Wend
                              End If
                          ' Get the next feature
                          Set Feature = Feature.GetNextFeature
                      Wend
                      If FlatPatternCount > 0 Then
                          BendsCount = FlatPatternCount
                      Else
                          BendsCount = FlattenBendsCount
                      End If
                      ' Add the custom property
                      Set swCustomPropertyManager = swModel.Extension.CustomPropertyManager(vConfigurations(i))
                      lRetVal = swCustomPropertyManager.Add3("Number of Bends", swCustomInfoType_e.swCustomInfoText, BendsCount, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
                      
                      ' Weld lenght
                      Dim dbTotalBeadLength As Double
                      Dim dbTotalBeadCount As Integer
                      Dim swWeldTypeCount As Integer
                      Dim j As Long
                      Set swModelDocExt = swModel.Extension
                      j = 1
                      swWeldTypeCount = 0
                      dbTotalBeadLength = 0
                      dbTotalBeadCount = 0
                      blnStatus = swModelDocExt.SelectByID2("Weld Bead" & j, "COSMETIC_WELD", 0, 0, 0, False, 0, Nothing, 0)
                      While blnStatus
                          Set swSelMgr = swModel.SelectionManager
                          Set swFeat = swSelMgr.GetSelectedObject6(1, 0)
                          Set swWeldBead = swFeat.GetDefinition
                          Set swWeldBeadFolder = swWeldBead.GetWeldBeadFolder()
                          dbTotalBeadLength = dbTotalBeadLength + swWeldBeadFolder.TotalLength
                          dbTotalBeadCount = dbTotalBeadCount + swWeldBeadFolder.TotalNumber
                          swWeldTypeCount = swWeldTypeCount + 1
                          j = j + swWeldBeadFolder.TotalNumber
                          blnStatus = swModelDocExt.SelectByID2("Weld Bead" & j, "COSMETIC_WELD", 0, 0, 0, False, 0, Nothing, 0)
                      Wend
                      ' Add the custom property
                      Set swCustomPropertyManager = swModel.Extension.CustomPropertyManager(vConfigurations(i))
                      lRetVal = swCustomPropertyManager.Add3("Lenght of weld bead", swCustomInfoType_e.swCustomInfoText, CStr(dbTotalBeadLength), swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
                      'MsgBox "Material: " & strMaterial & vbNewLine & _
                      '    "Weight: " & strWeight & vbNewLine & _
                      '    "Thickness: " & strThickness & vbNewLine & _
                      '    "Description: " & strProjectDescription
                  Next
                  ' Save the changes made
                  lRetVal = swModel.Save3(swSaveAsOptions_e.swSaveAsOptions_Silent, errors, warnings)
                  ' Close the model before check in
                  swApp.CloseDoc swModel.GetTitle
                  ' Do check in of file
                  If File.IsLocked = True And blnIsLocked = True Then
                      Call File.UnlockFile(0, "Task updated properties")
                  End If
              End Sub
              
              • Re: EPDM Task: Set Part Material & Configuration Variables.
                Ales Svoboda

                If you use PDM Task, you do not have to check out / in. Task automatically do that for you.

                Just do:

                 

                2) Open File

                3) Set part material to <insert material>

                4) Add variable to each configuration and set value

                5) Rebuild/Save

                 

                and put script into new task in PDM administration.

                Hope this helps.