-
Notifications
You must be signed in to change notification settings - Fork 145
/
Honeybee_Generate Test Points.py
165 lines (138 loc) · 6.58 KB
/
Honeybee_Generate Test Points.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
#
# Honeybee: A Plugin for Environmental Analysis (GPL) started by Mostapha Sadeghipour Roudsari
#
# This file is part of Honeybee.
#
# Copyright (c) 2013-2020, Mostapha Sadeghipour Roudsari <mostapha@ladybug.tools>
# Honeybee is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 3 of the License,
# or (at your option) any later version.
#
# Honeybee is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Honeybee; If not, see <http://www.gnu.org/licenses/>.
#
# @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
"""
Genrate Test Points
-
Provided by Honeybee 0.0.66
Args:
_testSurface: Test surface as a Brep.
_gridSize: Size of the test grid.
_distBaseSrf: Distance from base surface.
moveTestMesh_: Set to 'False' if you want test mesh not to move. Default is 'True'.
Returns:
readMe!: ...
testPoints: Test points
ptsVectors: Vectors
faceArea: Area of each mesh face
mesh: Analysis mesh
"""
ghenv.Component.Name = "Honeybee_Generate Test Points"
ghenv.Component.NickName = 'genTestPts'
ghenv.Component.Message = 'VER 0.0.66\nJUL_07_2020'
ghenv.Component.IconDisplayMode = ghenv.Component.IconDisplayMode.application
ghenv.Component.Category = "HB-Legacy"
ghenv.Component.SubCategory = "03 | Daylight | Recipes"
#compatibleHBVersion = VER 0.0.56\nFEB_01_2015
#compatibleLBVersion = VER 0.0.59\nFEB_01_2015
try: ghenv.Component.AdditionalHelpFromDocStrings = "1"
except: pass
import Rhino as rc
import Grasshopper.Kernel as gh
from itertools import chain
import System.Threading.Tasks as tasks
def createMesh(brep, gridSize):
## mesh breps
def makeMeshFromSrf(i, inputBrep):
try:
mesh[i] = rc.Geometry.Mesh.CreateFromBrep(inputBrep, meshParam)[0]
inputBrep.Dispose()
except:
print 'Error in converting Brep to Mesh...'
pass
# prepare bulk list for each surface
mesh = [None] * len(brep)
# set-up mesh parameters for each surface based on surface size
meshParam = rc.Geometry.MeshingParameters.Default
meshParam.MaximumEdgeLength = gridSize
meshParam.MinimumEdgeLength = gridSize
meshParam.GridAspectRatio = 1
for i in range(len(mesh)): makeMeshFromSrf(i, brep[i])
return mesh
def flattenList(l):return list(chain.from_iterable(l))
def getTestPts(inputMesh, movingDis, moveTestMesh= False, parallel = True):
# preparing bulk lists
testPoint = [[]] * len(inputMesh)
srfNormals = [[]] * len(inputMesh)
meshSrfCen = [[]] * len(inputMesh)
meshSrfArea = [[]] * len(inputMesh)
srfCount = 0
for srf in inputMesh:
testPoint[srfCount] = range(srf.Faces.Count)
srfNormals[srfCount] = range(srf.Faces.Count)
meshSrfCen[srfCount] = range(srf.Faces.Count)
meshSrfArea[srfCount] = range(srf.Faces.Count)
srfCount += 1
try:
def srfPtCalculator(i):
# calculate face normals
inputMesh[i].FaceNormals.ComputeFaceNormals()
inputMesh[i].FaceNormals.UnitizeFaceNormals()
for face in range(inputMesh[i].Faces.Count):
srfNormals[i][face] = (inputMesh[i].FaceNormals)[face] # store face normals
meshSrfCen[i][face] = inputMesh[i].Faces.GetFaceCenter(face) # store face centers
# calculate test points
if srfNormals[i][face]:
movingVec = rc.Geometry.Vector3f.Multiply(movingDis,srfNormals[i][face])
testPoint[i][face] = rc.Geometry.Point3d.Add(rc.Geometry.Point3d(meshSrfCen[i][face]), movingVec)
# make mesh surface, calculate the area, dispose the mesh and mass area calculation
tempMesh = rc.Geometry.Mesh()
tempMesh.Vertices.Add(inputMesh[i].Vertices[inputMesh[i].Faces[face].A]) #0
tempMesh.Vertices.Add(inputMesh[i].Vertices[inputMesh[i].Faces[face].B]) #1
tempMesh.Vertices.Add(inputMesh[i].Vertices[inputMesh[i].Faces[face].C]) #2
tempMesh.Vertices.Add(inputMesh[i].Vertices[inputMesh[i].Faces[face].D]) #3
tempMesh.Faces.AddFace(0, 1, 3, 2)
massData = rc.Geometry.AreaMassProperties.Compute(tempMesh)
meshSrfArea[i][face] = massData.Area
massData.Dispose()
tempMesh.Dispose()
except:
print 'Error in Extracting Test Points'
pass
# calling the function
if parallel:
tasks.Parallel.ForEach(range(len(inputMesh)),srfPtCalculator)
else:
for i in range(len(inputMesh)):
srfPtCalculator(i)
if moveTestMesh:
# find surfaces based on first normal in srfNormals - It is a simplification we can write a better function for this later
for meshCount, mesh in enumerate(inputMesh):
vector = srfNormals[meshCount][0]
vector.Unitize()
vector = rc.Geometry.Vector3d.Multiply(movingDis, vector)
mesh.Translate(vector.X, vector.Y, vector.Z)
return flattenList(testPoint), flattenList(srfNormals), flattenList(meshSrfArea), inputMesh
ghenv.Component.Params.Input[1].Name = '_gridSize'
ghenv.Component.Params.Input[1].NickName = '_gridSize'
if _testGeometry!=None:
inputMesh = []
if type(_testGeometry) == rc.Geometry.Mesh and _distBaseSrf!=None:
ghenv.Component.Params.Input[1].Name = '.'
ghenv.Component.Params.Input[1].NickName = '.'
inputMesh.append(_testGeometry)
elif _gridSize!=None and _distBaseSrf!=None:
initMesh = createMesh([_testGeometry], _gridSize)
for m in initMesh: inputMesh.append(m)
if _distBaseSrf<0:
msg = "Distance from base should be greater than 0. Flip the input surface instead of using a negative number."
ghenv.Component.AddRuntimeMessage(gh.GH_RuntimeMessageLevel.Warning, msg)
else:
testPoints, ptsVectors, facesArea, mesh = getTestPts(inputMesh, _distBaseSrf, moveTestMesh_)