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.
VBScript
x
129
129
1
Dim ioCATIA As Application
2
Set ioCATIA = CATIA
3
4
Dim ioActiveEditor As Editor
5
Set ioActiveEditor = ioCATIA.ActiveEditor
6
7
On Error Resume Next
8
9
Dim ioActiveObject As VPMRootOccurrence
10
Set ioActiveObject = ioActiveEditor.ActiveObject
11
12
Dim ioVPMReference As VPMReference
13
Set ioVPMReference = ioActiveObject.ReferenceRootOccurrenceOf
14
15
Dim ioVPMRepReference As VPMRepReference
16
ioVPMRepReference = Nothing
17
18
If (Err.Number <> 0) Then
19
20
Dim ioPart As Part
21
Set ioPart = ioActiveEditor.ActiveObject
22
Set ioVPMReference = ioPart.Parent.Father
23
24
If IsEmpty(ioVPMReference) Then
25
Set ioVPMRepReference = ioPart.Parent
26
End If
27
28
End If
29
30
On Error GoTo 0
31
32
Dim Json As String
33
34
Dim ioPN, ioRev, ioCollab, ioDesc, ioComm As String
35
ioPN = GetPartNumber(ioVPMReference, ioVPMRepReference)
36
ioRev = GetRevision(ioVPMReference, ioVPMRepReference)
37
ioCollab = GetCollabSpace(ioVPMReference, ioVPMRepReference)
38
ioDesc = GetDescription(ioVPMReference, ioVPMRepReference)
39
ioComm = GetComments(ioVPMReference, ioVPMRepReference)
40
41
Json = "{" & Chr(34) & "macroName" & Chr(34) & " : " & Chr(34) & TemplateName.ValueAsString & Chr(34) & _
42
" , " & Chr(34) & "macroRevision" & Chr(34) & " : " & Chr(34) & TemplateRevision.ValueAsString & Chr(34) & _
43
" ," & Chr(34) & "userName" & Chr(34) & ": " & Chr(34) & GetUserName() & Chr(34) & _
44
" ," & Chr(34) & "computerName" & Chr(34) & ": " & Chr(34) & GetComputerName() & Chr(34) & _
45
" ," & Chr(34) & "partNumber" & Chr(34) & ": " & Chr(34) & ioPN & Chr(34) & _
46
" ," & Chr(34) & "partRevision" & Chr(34) & ": " & Chr(34) & ioRev & Chr(34) & _
47
" ," & Chr(34) & "collaborativeSpace" & Chr(34) & ": " & Chr(34) & ioCollab & Chr(34) & _
48
" ," & Chr(34) & "description" & Chr(34) & ": " & Chr(34) & ioDesc & Chr(34) & _
49
" ," & Chr(34) & "comments" & Chr(34) & ": " & Chr(34) & ioComm & Chr(34) & _
50
"}"
51
52
Const APIkey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" 'DO NOT SHARE
53
54
Dim httpReq As Object
55
Set httpReq = CreateObject("MSXML2.XMLHTTP")
56
57
Dim rootURL As String, EndpointURL As String
58
59
rootURL = "https://macro-usage-function.azurewebsites.net"
60
EndpointURL = rootURL & "/api/v1/PostMacroUsageModel"
61
62
With httpReq
63
.Open "POST", EndpointURL, False
64
.setRequestHeader "Content-type", "application/json"
65
.setRequestHeader "x-functions-key", APIkey
66
.send (Json)
67
End With
68
69
End Sub
70
71
Function GetComputerName() As String
72
GetComputerName = CreateObject("WScript.Network").ComputerName
73
End Function
74
75
Function GetUserName() As String
76
GetUserName = CreateObject("WScript.Network").username
77
End Function
78
79
Function GetPartNumber(ioVPMReference As VPMReference, ioVPMRepReference As VPMRepReference) As String
80
If IsEmpty(ioVPMRepReference) Then
81
GetPartNumber = ioVPMReference.GetAttributeValue("PLM_ExternalID")
82
Else
83
GetPartNumber = ioVPMRepReference.GetAttributeValue("PLM_ExternalID")
84
End If
85
End Function
86
87
Function GetRevision(ioVPMReference As VPMReference, ioVPMRepReference As VPMRepReference) As String
88
If IsEmpty(ioVPMRepReference) Then
89
GetRevision = ioVPMReference.GetAttributeValue("revision")
90
Else
91
GetRevision = ioVPMRepReference.GetAttributeValue("revision")
92
End If
93
If (GetRevision = "") Then
94
GetRevision = "A.1"
95
End If
96
End Function
97
98
Function GetCollabSpace(ioVPMReference As VPMReference, ioVPMRepReference As VPMRepReference) As String
99
If IsEmpty(ioVPMRepReference) Then
100
GetCollabSpace = ioVPMReference.GetAttributeValue("project")
101
Else
102
GetCollabSpace = ioVPMRepReference.GetAttributeValue("project")
103
End If
104
If (GetCollabSpace = "") Then
105
GetCollabSpace = "UnKnown"
106
End If
107
End Function
108
109
Function GetDescription(ioVPMReference As VPMReference, ioVPMRepReference As VPMRepReference) As String
110
If IsEmpty(ioVPMRepReference) Then
111
GetDescription = ioVPMReference.GetAttributeValue("V_description")
112
Else
113
GetDescription = ioVPMRepReference.GetAttributeValue("V_description")
114
End If
115
If (GetDescription = "") Then
116
GetDescription = "None"
117
End If
118
End Function
119
120
Function GetComments(ioVPMReference As VPMReference, ioVPMRepReference As VPMRepReference) As String
121
If IsEmpty(ioVPMRepReference) Then
122
GetComments = ioVPMReference.GetAttributeValue("V_versionComment")
123
Else
124
GetComments = ioVPMRepReference.GetAttributeValue("V_versionComment")
125
End If
126
If (GetComments = "") Then
127
GetComments = "None"
128
End If
129
End Function