-
-
Notifications
You must be signed in to change notification settings - Fork 341
/
startup.py
154 lines (117 loc) · 4.02 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""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.
"""
#pylint: disable=import-error,invalid-name,broad-except,superfluous-parens
#pylint: disable=unused-import,wrong-import-position,unused-argument
#pylint: disable=missing-docstring
import sys
import time
import os.path as op
from pyrevit import HOST_APP, framework
from pyrevit import revit, DB, UI
from pyrevit import forms
from pyrevit import routes
# add your module paths to the sys.path here
# sys.path.append(r'path/to/your/module')
print('Startup script execution test.')
print('\n'.join(sys.path))
# test imports from same directory and exensions lib
import startuplibimport
print('lib/ import works in startup.py')
# test code for creating event handlers =======================================
# define event handler
def docopen_eventhandler(sender, args):
forms.alert('Document Opened: {}'.format(args.PathName))
# add to DocumentOpening
# type is EventHandler[DocumentOpeningEventArgs] so create that correctly
HOST_APP.app.DocumentOpening += \
framework.EventHandler[DB.Events.DocumentOpeningEventArgs](
docopen_eventhandler
)
# test code routes module =====================================================
api = routes.API("pyrevit-dev")
@api.route('/forms-block', methods=['POST'])
def forms_blocking(doc):
"""Test blocking GUI"""
forms.alert("Routes works!")
return 'Routes works!'
@api.route('/doc')
def get_doc(doc):
"""Test API access: get active document title"""
return {
"title": doc.Title if doc else ""
}
@api.route('/doors/')
def get_doors(uiapp):
"""Test API access: find doors in active model"""
time.sleep(3)
doors = revit.query.get_elements_by_categories(
[DB.BuiltInCategory.OST_Doors]
)
doors_data = [x.Id.IntegerValue for x in doors]
return routes.make_response(
data=doors_data,
headers={"pyRevit": "v4.6.7"}
)
@api.route('/except')
def raise_except():
"""Test handler exception"""
m = 12 / 0 #pylint: disable=unused-variable
@api.route('/reflect', methods=['POST'])
def reflect_request(request):
return {
"path": request.path,
"method": request.method,
"data": request.data
}
@api.route('/posts/<int:uiapp>')
def invalid_pattern():
# this must throw an error in routes
pass
@api.route('/posts/<int:pid>')
def post_id(request, pid):
return {
"path": request.path,
"method": request.method,
"data": {
"post_id": pid,
"post_id_type": type(pid).__name__
}
}
@api.route('/posts/<uuid:pid>')
def post_uuid(request, pid):
return {
"path": request.path,
"method": request.method,
"data": {
"post_id": str(pid),
"post_id_type": type(pid).__name__
}
}
@api.route('/archive/<int:year>/<int:month>/<int:day>/posts/<int:pid>')
def post_date_id(request, year, month, day, pid):
return {
"path": request.path,
"method": request.method,
"data": {
"date": '{}/{}/{}'.format(year, month, day),
"post_id": pid,
"post_id_type": type(pid).__name__
}
}
# test dockable panel =========================================================
class DockableExample(forms.WPFPanel):
panel_title = "pyRevit Dockable Panel Title"
panel_id = "3110e336-f81c-4927-87da-4e0d30d4d64a"
panel_source = op.join(op.dirname(__file__), "DockableExample.xaml")
def do_something(self, sender, args):
forms.alert("Voila!!!")
forms.register_dockable_panel(DockableExample)