/
datavis.py
221 lines (144 loc) · 5.01 KB
/
datavis.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import rhinoscriptsyntax as rs
import Rhino
import scriptcontext
import System.Drawing
def exportData():
# Generating sensor points
pts = setSensorLocation()
# Make csv file
file_object = open("points1.csv", "w")
# import sensor value
file_data = open("values.csv", "r")
data = file_data.readlines()
valList = []
for index in range(0, len(data)):
valNum = float(data[index])
valList.append(valNum)
maxNum = max(valList)
minNum = min(valList)
# index initialization
i = 0
# Writing csv file: iteration
for pt in pts:
coord = rs.PointCoordinates(pt)
dataLine = str(coord)
dataLine = dataLine + " ," + data[i]
val = float(data[i])
visLine = makeHeight(pt, val)
pipe = makePipe(visLine)
# Making layers and their colors depending on value
color = mapVal2Color(minNum, maxNum, i, val)
rs.EnableRedraw(False)
# Visualization: assigning layer to point
#rs.ObjectLayer(pt, layername)
#rs.ObjectLayer(visLine, layername)
#rs.ObjectLayer(pipe, layername)
# GUID to Brep
pipeBrep = rs.coercebrep(pipe)
AddMaterial(pipeBrep, i, color)
rs.DeleteObject(pt)
rs.DeleteObject(visLine)
rs.DeleteObject(pipe)
i = i + 1
file_object.writelines(dataLine)
file_object.close()
file_data.close()
print("data export done")
# Making curve corresponding heights
def makeHeight(pt, val):
originalPt = rs.CopyObject(pt)
vector = (0, 0, val)
movedPt = rs.MoveObject(pt, vector)
line = [originalPt, movedPt]
lineID = rs.AddLine(line[0], line[1])
return lineID
# Making pipe based on curves
def makePipe(lineID):
pipe = rs.AddPipe(lineID, 0, 0.1, cap = 1)
return pipe
def mapVal2Color(minNum, maxNum, i, val):
startNum = minNum
endNum = maxNum
length = (endNum - startNum) / 5
val = float(val)
if val > (startNum + 4*length):
Red = 255
Green = 125 - ((val - (startNum + 4*length)) / length) * 125
Blue = 0
color = Red, Green, Blue
elif val > (startNum + 3*length):
Red = 255
Green = 255 - ((val - (startNum + 3*length)) / length) * 125
Blue = 0
color = Red, Green, Blue
elif val > (startNum + 2*length):
Red = ((val - (startNum + 2*length)) / length) * 255
Green = 255
Blue = 0
color = Red, Green, Blue
elif val > (startNum + length):
Red = 0
Green = 255
Blue = 255 - ((val - (startNum + length)) / length) * 125
color = Red, Green, Blue
else:
color = 0, 255, 255
# layername = str(i + 1)
# rs.AddLayer(layername)
# rs.LayerColor(layername, color)
return color
def AddMaterial(obj, index, color):
# materials are stored in the document's material table
index = scriptcontext.doc.Materials.Add()
mat = scriptcontext.doc.Materials[index]
color = System.Drawing.Color.FromArgb(color[0], color[1], color[2])
mat.DiffuseColor = color
mat.Name = str(index)
mat.CommitChanges()
# set up object attributes to say they use a specific material
attr = Rhino.DocObjects.ObjectAttributes()
attr.MaterialIndex = index
attr.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject
scriptcontext.doc.Objects.AddBrep(obj, attr)
def setSensorLocation():
projectedPtList = []
# Select boundary surface
boundarySrf = rs.GetObject("Select surface for making boundary", rs.filter.surface)
# For making location point with grid
direction = (0, 0, 100)
plane = rs.MoveObject(boundarySrf, direction)
# Dividing surface to points
ptList = ArrayPointsOnSurface(plane)
# point projection
projectedSrf = rs.GetObject("Select surface for projection", rs.filter.surface)
for point in ptList:
pointsOnModel = rs.ProjectPointToSurface(point, projectedSrf, (0, 0, 1))
projectedPt = rs.AddPoint(pointsOnModel[0][0], pointsOnModel[0][1], pointsOnModel[0][2])
projectedPtList.append(projectedPt)
rs.DeleteObjects(plane)
rs.DeleteObjects(ptList)
return projectedPtList
def ArrayPointsOnSurface(srf):
ptList = []
# Get the number of rows
rows = 51
# Get the number of columns
cols = 51
# Get the domain of the surface
u, v = rs.SurfaceDomain(srf, 0), rs.SurfaceDomain(srf, 1)
# Turn off redrawing (faster)
rs.EnableRedraw(False)
# Add the points
for i in range(rows):
s = u[0] + ((u[1]-u[0])/(rows-1))*i
for j in range(cols):
t = v[0] + ((v[1]-v[0])/(cols-1))*j
pt = rs.EvaluateSurface(srf, s, t)
obj = rs.AddPoint(pt) # add the point
ptList.append(obj)
return ptList
# Turn on redrawing
rs.EnableRedraw(True)
if __name__ == '__main__':
exportData()
rs.EnableRedraw(True)