-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
72 lines (59 loc) · 2.87 KB
/
app.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
import datetime
import json
from io import BytesIO
import viktor as vkt
from viktor.external.generic import GenericAnalysis
class Parametrization(vkt.ViktorParametrization):
intro = vkt.Text(
"## Grasshopper Analysis app \n This app parametrically generates and analyses a "
"3D model of a tower using a Grasshopper script. "
"The sun hour analysis is carried out using the Ladybug plugin for Grasshopper. "
"Geometry and resulting values are sent back and forth to the Grasshopper script in real-time."
"\n\n Please fill in the following parameters:"
)
# Input fields
floorplan_width = vkt.NumberField(
"Floorplan width", default=15, min=10, max=18, suffix="m", flex=100, variant='slider', step=1
)
twist_top = vkt.NumberField(
"Twist top", default=0.65, min=0.20, max=1.00, variant='slider', flex=100, step=0.01
)
floor_height = vkt.NumberField(
"Floor height", default=3.5, min=2.5, max=5.0, suffix="m", variant='slider', flex=100, step=0.1
)
tower_height = vkt.NumberField(
"Tower height", default=75, min=20, max=100, suffix="m", flex=100, variant='slider', step=1
)
rotation = vkt.NumberField(
"Rotation", default=60, min=0, max=90, suffix="°", flex=100, variant='slider', step=1
)
date = vkt.DateField(
'Date for the sun hour analysis', default=datetime.date.today(), flex=100
)
class Controller(vkt.ViktorController):
label = 'My Entity Type'
parametrization = Parametrization(width=30)
@vkt.GeometryAndDataView("Geometry", duration_guess=0, update_label='Run Grasshopper', x_axis_to_right=True)
def run_grasshopper(self, params, **kwargs):
# Replace datetime object with month and day
date: datetime.date = params["date"]
params["month"] = date.month
params["day"] = date.day
params.pop("date")
# Create a JSON file from the input parameters
input_json = json.dumps(params)
# Generate the input files
files = [('input.json', BytesIO(bytes(input_json, 'utf8')))]
# Run the Grasshopper analysis and obtain the output files
generic_analysis = GenericAnalysis(files=files, executable_key="run_grasshopper", output_filenames=[
"geometry.3dm", "output.json"
])
generic_analysis.execute(timeout=60)
rhino_3dm_file = generic_analysis.get_output_file("geometry.3dm", as_file=True)
output_values: vkt.File = generic_analysis.get_output_file("output.json", as_file=True)
# Create a DataGroup object to display output data
output_dict = json.loads(output_values.getvalue())
data_group = vkt.DataGroup(
*[vkt.DataItem(key.replace("_", " "), val) for key, val in output_dict.items()]
)
return vkt.GeometryAndDataResult(geometry=rhino_3dm_file, geometry_type="3dm", data=data_group)