1+ """
2+ Export DATA to Server for Dynamo Usage Tracking
3+ -
4+ a dynamoPython script, visit the website for more details
5+ https://github.com/Amoursol/dynamoPython
6+ """
7+ __author__ = 'Timon Hazell'
8+ __twitter__ = '@tmnhzll'
9+ __github__ = '@thazell'
10+ __version__ = '1.0.0'
11+
12+ """
13+ #The purpose of this script is to allow users to track usage of dynamo.
14+ #The user needs to set the directory below and then it can be copied to all the scripts in the office.
15+ #It will create a single json or CSV per dynamo run (so expect tons of files)
16+ #The single file per run was selected to limit issues with writing to the same file, which occurred on the creators' network.
17+ #Note if a script runs 30x in one session, each run will be logged, so consider that when working.
18+ """
19+
20+ #
21+ # ------------------Header - Import Section
22+ import sys
23+ import clr
24+
25+
26+ sys .path .append (r'C:\Program Files (x86)\IronPython 2.7\Lib' )
27+ import os
28+
29+ # Adding the DynamoRevitDS.dll module to work with the Dynamo API
30+ clr .AddReference ('DynamoRevitDS' )
31+ import Dynamo
32+
33+ # Import RevitAPI
34+ clr .AddReference ("RevitAPI" )
35+ import Autodesk
36+ from Autodesk .Revit import DB
37+
38+ # Import DocumentManager and TransactionManager
39+ clr .AddReference ("RevitServices" )
40+ import RevitServices
41+ from RevitServices .Persistence import DocumentManager
42+
43+
44+ doc = DocumentManager .Instance .CurrentDBDocument
45+ uiapp = DocumentManager .Instance .CurrentUIApplication
46+ app = uiapp .Application
47+
48+ try :
49+ runscript = IN [0 ]
50+ except :
51+ runscript = True
52+
53+ if runscript :
54+
55+ #----------------Stored Variables Section
56+
57+ # -->>>> update this string below to a network folder that all users have write access to
58+ networkFolder = r'C:\Temp\logreports\DynamoLogReports\''
59+
60+ #Time saveed is assumed based on the following example:
61+ #If you expect that every time you renumber sheets you save 5 minutes, but if you renumber 500 sheets you save 5 minutes + 10 seconds per sheet * 500 sheets)
62+ #edit the following values for each script as an estimate.
63+ estimatedtimesavedeachrun = 10 #put the number of seconds this script will save the office every time it runs
64+ timesavedperelement = 0 #put the number of seconds this script will save for every element that gets edited.
65+
66+
67+ releasedOrTesting = "Testing"
68+ exportAsJson = False #choose whether to record as a json (True) or csv (False)
69+
70+ #try:
71+ import datetime
72+ #add date and time to and central file name subdirectory
73+
74+
75+ #--------------------Processing Section
76+
77+ #get current time of dynamo run
78+ dtstampstring = str (datetime .datetime .today ())
79+
80+ #get computername of run
81+ computername = os .environ ['COMPUTERNAME' ]
82+
83+ #get windows username
84+ username = os .environ ['USERNAME' ]
85+
86+ #get dynamo filename
87+
88+ # access to the current Dynamo instance and workspace
89+ dynamoRevit = Dynamo .Applications .DynamoRevit ()
90+ currentWorkspace = dynamoRevit .RevitDynamoModel .CurrentWorkspace
91+
92+ # access current version of dynamo
93+ version = dynamoRevit .RevitDynamoModel .Version
94+
95+ # checks version of dynamo and adjusts output according to version
96+ if version .StartsWith ("1." ):
97+
98+ # Gets file name which includes full path
99+ filename = currentWorkspace .FileName
100+ # Splits out file path to just file name
101+ dynamoFileName = filename .Split ("\\ " )[- 1 ].Replace (".dyn" ,"" )
102+
103+ elif version .StartsWith ("2." ):
104+ dynamoFileName = currentWorkspace .Name
105+ else :
106+ dynamoFileName = "Unkown"
107+
108+ #get revit projectnumber
109+ projectnumber = doc .ProjectInformation .Number
110+
111+ #get revit projectname
112+ projectname = doc .ProjectInformation .Name
113+
114+ #get revit year
115+ revitYear = app .VersionNumber
116+
117+ #get full revit version
118+ revitfullversion = app .VersionBuild
119+
120+ #get revit saved filename or use
121+ try :
122+ rvtfilename = doc .Title
123+ except :
124+ rvtfilename = "Unkown" # file may not be saved
125+
126+ #calculate time saved per run:
127+ #current time saves per run is just an estimate.
128+ #The user can use the following code if they want to count the number of elements
129+ #that were processed in dynamo
130+ # and use that list to figure out the time savings per element
131+ try :
132+ dynamoelementsupdated = IN [1 ]
133+ if isinstance (dynamoelementsupdated ,list ):
134+ elementsupdated = len (dynamoelementsupdated )
135+ elif isinstance (dynamoelementsupdated , int ):
136+ elementsupdated = dynamoelementsupdated
137+ elif isintance (dynamoelementsupdated , float ):
138+ elementsupdated = dynamoelementsupdated
139+ elif isintance (dynamoelementsupdated , str ):
140+ elementsupdated = int (dynamoelementsupdated )
141+ else :
142+ elementsupdated = 0
143+ except :
144+ elementsupdated = 0
145+
146+ timeSavedPerRun = estimatedtimesavedeachrun + timesavedperelement * elementsupdated
147+ #--------------------Output Section
148+
149+
150+ #create a dictionary which will be sent to a seperate file for each dynamo run
151+ outputdictionary = {}
152+ outputdictionary ["Computer" ] = computername
153+ outputdictionary ["DynamoFile" ] = dynamoFileName
154+ outputdictionary ["Project Name" ] = projectname
155+ outputdictionary ["Project Number" ] = projectnumber
156+ outputdictionary ["Active Revit Project File Name" ] = rvtfilename
157+ outputdictionary ["Released or Testing" ] = releasedOrTesting
158+ outputdictionary ["Revit Version" ] = revitYear
159+ outputdictionary ["Revit Version Full Build" ] = revitfullversion
160+ outputdictionary ["TimeSaved (sec)" ] = timeSavedPerRun
161+ outputdictionary ["TimeStamp" ] = dtstampstring
162+ outputdictionary ["User" ] = username
163+
164+
165+ #set directory, create directory if it doesn't exist:
166+ directory = networkFolder + computername + '\\ '
167+
168+ if not os .path .exists (directory ):
169+ os .mkdir (directory )
170+ newfilename = str (datetime .datetime .today ()).replace (" " ,"-" ).replace (":" ,"-" ).replace ("." ,"-" ) + ""
171+
172+
173+ if exportAsJson :
174+ import json
175+ #export json
176+
177+ newfilename += "-logger.json"
178+ #create json file. a new file will be created every time. This was found to be the most consistent way of getting every run recorded.
179+
180+ with open (directory + newfilename , 'w' ) as fp :
181+ #OUT = outputdictionary
182+ jsonstring = json .dumps (outputdictionary , indent = 4 )
183+ fp .write (jsonstring )
184+
185+ else :
186+ import csv
187+
188+ newfilename += "-logger.csv"
189+ #create json file. a new file will be created every time. This was found to be the most consistent way of getting every run recorded.
190+
191+
192+ with open (directory + newfilename , 'wb' ) as csvfile :
193+ spamwriter = csv .writer (csvfile , delimiter = ',' , quotechar = '|' , quoting = csv .QUOTE_MINIMAL )
194+ fieldnames = outputdictionary .keys ()
195+ writer = csv .DictWriter (csvfile , fieldnames = fieldnames )
196+ writer .writeheader ()
197+ writer .writerow (outputdictionary )
198+
199+
200+ outputpreviewlist = []
201+ outputpreviewlist .append ("The following was recorded to file:" )
202+ outputpreviewlist .append (directory + newfilename )
203+ for title in outputdictionary .keys ():
204+ outputpreviewlist .append ("{0}: {1}" .format (title ,outputdictionary [title ]))
205+ OUT = outputpreviewlist
206+
207+ #if you want to see the output file, uncomment the following lines of code.
208+ #import subprocess
209+ #os.startfile(directory + newfilename)
210+
211+ #-----------------Some code was based on work and questions on the forums by these people, thank you
212+ #John Pierson
213+ #Brendan Cassidy
0 commit comments