Skip to content

Commit

Permalink
added overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Robinson committed Jun 25, 2020
1 parent 72e31cc commit 6532f9b
Show file tree
Hide file tree
Showing 5 changed files with 9,594 additions and 5 deletions.
111 changes: 111 additions & 0 deletions dash_building_heatmap/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import base64
import datetime
import io
import os
import time
import copy

import pyiges
from geomdl import NURBS, BSpline, utilities
import numpy as np

import dash_html_components as html
import plotly.graph_objects as go




UPLOAD_DIRECTORY = os.path.join(os.getcwd(),'temp_iges')

if not os.path.exists(UPLOAD_DIRECTORY):
os.makedirs(UPLOAD_DIRECTORY)


def save_temp_file(name, content) -> str:
"""
Decode and store a file uploaded with Plotly Dash.
saves as filename + epoch timestamp
Remember to delete it!
t
Returns:
full path as str
"""
data = content.encode("utf8").split(b";base64,")[1]
filepath = os.path.join(UPLOAD_DIRECTORY, name + str(time.time()))

with open(filepath, "wb") as file:
file.write(base64.decodebytes(data))
return filepath


def _clean_iges(filepath, from_rhino = True):
"""
this is based on export of file from rhino.
overwrites file at filepath so create a new file (eg save_temp_file)
"""
if not from_rhino:
raise NotImplementedError('Good luck')

new_lines = []

with open(filepath, "rb") as file:
lines = file.readlines()
for line in lines:
line = line.replace(b'D0',b' ')
line = line.replace(b'D-10',b' ')
line = line.replace(b'D-11',b' ')
line = line.replace(b'D-12',b' ')
line = line.replace(b'D-13',b' ')
new_lines.append(line) # iges in expects certain things at certain chars on each line, hence replace

with open(filepath, "wb") as file:
file.writelines(new_lines)



def eval_bspline(b : pyiges.curves_surfaces.BSpline, delta=0.001, n = 10):
"""
Return:
numpy array of sampled points on bspline
"""

# Create a geomdl 3-dimensional B-spline Curve from incoming pyiges spline
curve = NURBS.Curve()
curve.degree = b.M
curve.ctrlpts = b.control_points
curve.weights = b.W + [1]
curve.knotvector = b.T
curve.delta = delta # TODO sampling - this could get out of hand depending on model dims and scale

#TODO conditional delta: min length, n and check for straight lines

return np.array(curve.evalpts)


def add_graphtrace_from_iges(fig : go.Figure(), filename : str = None):

default_file = os.path.join('data', 'linework.igs')

f = filename or default_file
#f = save_temp_file(filename, contents)
_clean_iges(f)
iges = pyiges.read(f)

for b in iges.bsplines:
xyz = eval_bspline(b)
xy = list([x[:-1] for x in xyz]) #remove 3d data
x_data = []
y_data = []
for c in xy:
x_data.append(c[0])
y_data.append(c[1])

fig.add_trace(
go.Scatter(
x= x_data,
y= y_data,
mode='lines',
line =dict(color='black',width=1)
)
)
return fig
76 changes: 76 additions & 0 deletions dash_building_heatmap/iges.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import base64
import datetime
import io
import os
import time


import pyiges
from geomdl import NURBS, BSpline, utilities
import numpy as np

import dash_html_components as html



UPLOAD_DIRECTORY = os.path.join(os.getcwd(),'temp_iges')

if not os.path.exists(UPLOAD_DIRECTORY):
os.makedirs(UPLOAD_DIRECTORY)


def save_temp_file(name, content) -> str:
"""
Decode and store a file uploaded with Plotly Dash.
saves as filename + epoch timestamp
Remember to delete it!
Returns:
full path as str
"""
data = content.encode("utf8").split(b";base64,")[1]
filepath = os.path.join(UPLOAD_DIRECTORY, name + str(time.time()))

with open(filepath, "wb") as file:
file.write(base64.decodebytes(data))
return filepath


def clean_iges(filepath):
new_lines = []

with open(filepath, "rb") as file:
lines = file.readlines()
print ('hello')
for line in lines:
s = line.split()
if len>2 and s[-2][-1]=='P':
n = len(line):



for c in line
line = line.replace('D0',' ')
line = line.replace('D-10',' ')
line = line.replace('D-11',' ')
line = line.replace('D-12',' ')
line = line.replace('D-13',' ')
new_lines.append(line)

with open(filepath, "wb") as file:
file.writelines(new_lines)



def eval_bspline(b : pyiges.curves_surfaces.BSpline, delta=0.1):
# Create a 3-dimensional B-spline Curve
curve = NURBS.Curve()
curve.degree = b.M
curve.ctrlpts = b.control_points # Set control points (weights vector will be 1 by default)
curve.weights = b.W + [1]
curve.knotvector = b.T # Set knot vector
curve.delta = delta

return np.array(curve.evalpts)


29 changes: 24 additions & 5 deletions dash_building_heatmap/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,47 @@
import dash_html_components as html

import pandas as pd
import numpy as np

import helpers

DATA_PATH = os.path.join(os.getcwd(),'data')


def layout(filename = None):
#TODO load multiple csv into a hidden div in the background as cache.
#TODO ouline of building, stair core from cad file.
file = filename or 'a_p8w_h818l_s0.75d_b_s2sh0.csv'
file = filename or 'a_p0w_h024sh0.csv'
csv_path = os.path.join(DATA_PATH,file)

df = pd.read_csv(csv_path,skip_blank_lines=True)

def clean(i : float):
def clean(i : float, b: bool, gridsize : float =0.5):
"""
removes weird 0.0000000001 from coord data.
would be better removed at source (in GH) for performance
"""
if b:
i=-i


if i <0.01:
return 0
return i


i = 1000* int(i/gridsize) * gridsize
return i

def mirror(l):
m = max(l)
return l.apply(lambda x: m-x)

rad_data = df['radiation']
x_data = df['x'].apply(lambda x:clean(x))
y_data = df['y'].apply(lambda x:clean(x))
x_data = df['x'].apply(lambda x:clean(x,True))
y_data = df['y'].apply(lambda x:clean(x,False))

y_data = mirror(y_data)


#TODO do colorbar units and title.
#TODO hover unit and title.
Expand All @@ -51,4 +67,7 @@ def clean(i : float):
yaxis=dict(scaleanchor="x", scaleratio=1)
)


fig = helpers.add_graphtrace_from_iges(fig)

return dcc.Graph(figure=fig)
Loading

0 comments on commit 6532f9b

Please sign in to comment.