Recording Macro Usage On Template Instantiation

Recording Macro Usage On Template Instantiation

Building Catalogs or Library’s for templates is great but unless you know who, when, how, and where those templates are being used its hard to calculate return on investment. The method I like to use is to create a REST API end point either on AWS or AZURE and then on instantiation call the end point and pass some information about that usage. For example in this case were passing:

  • Macro Name
  • Macro Revision
  • User Name
  • Computer Name
  • Part Number
  • Part Revision
  • Collaborative Space
  • Description
  • Comments

The Macro Name and Revision are argument strings being passed into the Action, which is then called on instantiation. Obviously you can pass what ever information you want.

    Dim ioCATIA As Application
    Set ioCATIA = CATIA
    
    Dim ioActiveEditor As Editor
    Set ioActiveEditor = ioCATIA.ActiveEditor
    
    On Error Resume Next
    
        Dim ioActiveObject As VPMRootOccurrence
        Set ioActiveObject = ioActiveEditor.ActiveObject
    
        Dim ioVPMReference As VPMReference
        Set ioVPMReference = ioActiveObject.ReferenceRootOccurrenceOf
        
        Dim ioVPMRepReference As VPMRepReference
        ioVPMRepReference = Nothing
        
        If (Err.Number <> 0) Then
        
            Dim ioPart As Part
            Set ioPart = ioActiveEditor.ActiveObject
            Set ioVPMReference = ioPart.Parent.Father
            
            If IsEmpty(ioVPMReference) Then
                Set ioVPMRepReference = ioPart.Parent
            End If
            
        End If
       
    On Error GoTo 0
    
    Dim Json As String
    
    Dim ioPN, ioRev, ioCollab, ioDesc, ioComm As String
    ioPN = GetPartNumber(ioVPMReference, ioVPMRepReference)
    ioRev = GetRevision(ioVPMReference, ioVPMRepReference)
    ioCollab = GetCollabSpace(ioVPMReference, ioVPMRepReference)
    ioDesc = GetDescription(ioVPMReference, ioVPMRepReference)
    ioComm = GetComments(ioVPMReference, ioVPMRepReference)
    
    Json = "{" & Chr(34) & "macroName" & Chr(34) & " : " & Chr(34) & TemplateName.ValueAsString & Chr(34) & _
            " , " & Chr(34) & "macroRevision" & Chr(34) & " : " & Chr(34) & TemplateRevision.ValueAsString & Chr(34) & _
            " ," & Chr(34) & "userName" & Chr(34) & ": " & Chr(34) & GetUserName() & Chr(34) & _
            " ," & Chr(34) & "computerName" & Chr(34) & ": " & Chr(34) & GetComputerName() & Chr(34) & _
            " ," & Chr(34) & "partNumber" & Chr(34) & ": " & Chr(34) & ioPN & Chr(34) & _
            " ," & Chr(34) & "partRevision" & Chr(34) & ": " & Chr(34) & ioRev & Chr(34) & _
            " ," & Chr(34) & "collaborativeSpace" & Chr(34) & ": " & Chr(34) & ioCollab & Chr(34) & _
            " ," & Chr(34) & "description" & Chr(34) & ": " & Chr(34) & ioDesc & Chr(34) & _
            " ," & Chr(34) & "comments" & Chr(34) & ": " & Chr(34) & ioComm & Chr(34) & _
            "}"
    
    Const APIkey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"  'DO NOT SHARE
    
    Dim httpReq As Object
    Set httpReq = CreateObject("MSXML2.XMLHTTP")

    Dim rootURL As String, EndpointURL As String

    rootURL = "https://macro-usage-function.azurewebsites.net"
    EndpointURL = rootURL & "/api/v1/PostMacroUsageModel"
    
    With httpReq
        .Open "POST", EndpointURL, False
        .setRequestHeader "Content-type", "application/json"
        .setRequestHeader "x-functions-key", APIkey
        .send (Json)
    End With

End Sub

Function GetComputerName() As String
        GetComputerName = CreateObject("WScript.Network").ComputerName
End Function

Function GetUserName() As String
        GetUserName = CreateObject("WScript.Network").username
End Function

Function GetPartNumber(ioVPMReference As VPMReference, ioVPMRepReference As VPMRepReference) As String
    If IsEmpty(ioVPMRepReference) Then
        GetPartNumber = ioVPMReference.GetAttributeValue("PLM_ExternalID")
    Else
        GetPartNumber = ioVPMRepReference.GetAttributeValue("PLM_ExternalID")
    End If
End Function

Function GetRevision(ioVPMReference As VPMReference, ioVPMRepReference As VPMRepReference) As String
     If IsEmpty(ioVPMRepReference) Then
        GetRevision = ioVPMReference.GetAttributeValue("revision")
    Else
        GetRevision = ioVPMRepReference.GetAttributeValue("revision")
    End If
    If (GetRevision = "") Then
        GetRevision = "A.1"
    End If
End Function

Function GetCollabSpace(ioVPMReference As VPMReference, ioVPMRepReference As VPMRepReference) As String
     If IsEmpty(ioVPMRepReference) Then
        GetCollabSpace = ioVPMReference.GetAttributeValue("project")
    Else
        GetCollabSpace = ioVPMRepReference.GetAttributeValue("project")
    End If
    If (GetCollabSpace = "") Then
            GetCollabSpace = "UnKnown"
    End If
End Function

Function GetDescription(ioVPMReference As VPMReference, ioVPMRepReference As VPMRepReference) As String
     If IsEmpty(ioVPMRepReference) Then
        GetDescription = ioVPMReference.GetAttributeValue("V_description")
    Else
        GetDescription = ioVPMRepReference.GetAttributeValue("V_description")
    End If
    If (GetDescription = "") Then
            GetDescription = "None"
    End If
End Function

Function GetComments(ioVPMReference As VPMReference, ioVPMRepReference As VPMRepReference) As String
     If IsEmpty(ioVPMRepReference) Then
        GetComments = ioVPMReference.GetAttributeValue("V_versionComment")
    Else
        GetComments = ioVPMRepReference.GetAttributeValue("V_versionComment")
    End If
    If (GetComments = "") Then
        GetComments = "None"
    End If
End Function