-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstartup.py
117 lines (87 loc) · 3.99 KB
/
startup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""Example of IronPython script to be executed by pyRevit on extension load
The script filename must end in startup.py
To Test:
- rename file to startup.py
- reload pyRevit: pyRevit will run this script after successfully
created the DLL for the extension.
pyRevit runs the startup script in a dedicated IronPython engine and output
window. Thus the startup script is isolated and can not hurt the load process.
All errors will be printed to the dedicated output window similar to the way
errors are printed from pyRevit commands.
"""
# with open(r'C:\Temp\test.txt', 'w') as f:
# f.write('test')
from pyrevit.framework import clr
import time
import sqlite3
import os
from os import path
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
clr.AddReference('AdWindows')
clr.AddReference('UIFramework')
clr.AddReference('UIFrameworkServices')
clr.AddReference('Newtonsoft.Json')
import Autodesk.Revit.DB.Events as Event
import Autodesk.Revit.DB as DB
from Autodesk.Revit.DB import *
import Autodesk.Revit.UI as UI
app = __revit__.Application
def getElementsProperties(AddedElementsIds, connection):
''' '''
output= []
date = int(time.time())
doc = __revit__.ActiveUIDocument.Document
docName = doc.Title or ""
userName = app.Username or ""
action = "Added"
uniqueIds = list(set(AddedElementsIds))
for id in uniqueIds:
element= doc.GetElement(id)
category = element.Category
categoryName = ''
try:
categoryName = category.Name
if "Pipes" in categoryName or "Ducts" in categoryName :
length = element.LookupParameter('Length').AsDouble()
comment = ""
SystemType = element.get_Parameter(DB.BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM).AsValueString()
#SystemTypeName = SystemType.LookupParameter('Name')
s ="INSERT INTO elements VALUES ('%s','%s','%s','%s',%s,'%s',%s,'%s,'%s')" % (date, userName, docName, action, id, categoryName, length, comment, SystemType)
output.append(s)
if "Pipe Fitting" in categoryName or "Duct Fitting" in categoryName:
length = 0
comment = element.LookupParameter('Size').AsString()
try:
SystemType = element.get_Parameter(DB.BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM).AsValueString()
except:
SystemType = element.get_Parameter(DB.BuiltInParameter.RBS_DUCT_SYSTEM_TYPE_PARAM).AsValueString()
#SystemTypeName = SystemType.LookupParameter('Name')
s ="INSERT INTO elements VALUES ('%s','%s','%s','%s',%s,'%s',%s,'%s','%s')" % (date, userName, docName, action, id, categoryName, length, comment, SystemType)
output.append(s)
except Exception as ex:
erroLog = os.path.expanduser(r'~\error.log')
with open(erroLog, 'a') as file:
file.write(str(ex)+'\n')
return output
def SaveChangeJournal(sender, event):
'''Save journal of elements changed during document edition'''
DBpath = os.path.expanduser(r'~\log.sqlite')
conn = sqlite3.connect(DBpath)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS elements
(date text, username text, document text, action text, id INTEGER, category text, length real, comment text, system text )''')
try:
#Look for elements created in last event and cleaning from duplicated values
AddedElementsIds = list(set(event.GetAddedElementIds()))
queries = getElementsProperties(AddedElementsIds,c)
for q in queries:
c.execute(q)
conn.commit()
except Exception as ex:
erroLog = os.path.expanduser(r'~\error.log')
with open(erroLog, 'a') as file:
file.write(str(ex)+'\n')
#Closing the connections in case something fails
conn.close()
app.DocumentChanged += SaveChangeJournal