Comments (7)
Here is the code for STL that I wrote while ago. I was hoping to be able to clean it but I didn't get a chance to do so. In case you want to modify this make sure that you keep Grasshopper libraries separate. This class should receive mesh data and generate the .stl file.
class STLMesh:
def __init__(self):
pass
def exportSTL(self):
def calculateAngles(brepList, testVector = rc.Geometry.Vector3d.YAxis):
def getNormal(brep):
MP = rc.Geometry.AreaMassProperties.Compute(brep)
area = None
if MP:
centerPt = MP.Centroid
MP.Dispose()
bool, centerPtU, centerPtV = brep.ClosestPoint(centerPt)
if bool:
normalVector = brep.Faces[0].NormalAt(centerPtU, centerPtV)
else:
normalVector = brep.Faces[0].NormalAt(0,0)
return normalVector
angleCollection = []
for brep in brepList:
thisBrepAngles = []
for brepFace in brep.Faces:
try:
angle = rc.Geometry.Vector3d.VectorAngle(getNormal(brepFace.DuplicateFace(False)), testVector, rc.Geometry.Plane.WorldXY)
except:
print getNormal(brepFace.DuplicateFace(False))
print testVector
try: thisBrepAngles.append("%.0f"%math.degrees(angle))
except: thisBrepAngles.append('NaN')
angleCollection.append(thisBrepAngles)
return angleCollection
def joinMesh(meshList):
joinedMesh = rc.Geometry.Mesh()
for m in meshList: joinedMesh.Append(m)
return joinedMesh
def writeSTLFile(filepath, fileName, meshList, northAngleList, type = 'bldg'):
def extractMeshPts(mesh):
coordinatesList = []
normals = []
mesh.FaceNormals.ComputeFaceNormals()
mesh.FaceNormals.UnitizeFaceNormals()
for face in range(mesh.Faces.Count):
# get each mesh surface vertices
if mesh.Faces.GetFaceVertices(face)[3] != mesh.Faces.GetFaceVertices(face)[4]:
meshVertices = mesh.Faces.GetFaceVertices(face)[1:5]
else:
meshVertices = mesh.Faces.GetFaceVertices(face)[1:4]
coordinatesList.append(list(meshVertices))
normals.append(mesh.FaceNormals[face])
return coordinatesList, normals
stlFile = open(filepath + "\\" + fileName + '.stl', 'w')
# mesh breps
for meshCount, mesh in enumerate(meshList):
for meshFaceCount, meshFace in enumerate(mesh):
# coordinates for a single mesh (multiple faces)
meshCoorLists, normals = extractMeshPts(meshFace)
for faceCount, coordinates in enumerate(meshCoorLists):
# solid bldg'blsgNumber'@'angle2North'N'faceNum'_'cellNumber'
solidStr = 'solid ' + type + `meshCount + 1`+ "@" + \
northAngleList[meshCount][meshFaceCount] + \
'N' + `meshFaceCount + 1` + '_' + \
`faceCount + 1` + '\n'
stlFile.write(solidStr)
if len(coordinates) == 4: verList = [coordinates[:3], [coordinates[0], coordinates[2], coordinates[3]]]
else: verList = [coordinates]
for coorList in verList:
# facet normal 0.400645 -0.916233 0.0
stlFile.write(' facet normal ' + `normals[faceCount].X` + ' '+ `normals[faceCount].Y` + ' ' + `normals[faceCount].Z` + '\n')
# outer loop
stlFile.write(' \touter loop\n')
# vertex 9.59792 -22.1636 4.20010
# vertex 7.65496 -23.0133 4.20010
# vertex 7.65496 -23.0133 2.10005
for ver in coorList: stlFile.write(' \t vertex ' + `ver.X` + ' ' + `ver.Y` + ' ' + `ver.Z` + '\n')
# endloop
stlFile.write(' \tendloop\n')
# endfacet
stlFile.write(' endfacet\n')
# endsolid patch1_1
stlFile.write('end' + solidStr)
stlFile.close()
# write stl file for main bldgs
bldgMesh = self.createMesh(self.mainBldgs, self.gridSize)
srfsAngle2North = calculateAngles(self.mainBldgs)
writeSTLFile(self.workingDir + "/geo", "primary", bldgMesh, srfsAngle2North)
# secondary
contextMesh = []
if len(self.contextBldgs)!=0 and self.contextBldgs[0]!= None:
contextMesh = self.createMesh(self.contextBldgs, 4 * self.gridSize)
srfsAngle2North = calculateAngles(self.contextBldgs)
writeSTLFile(self.workingDir + "/geo", "secondary", contextMesh, srfsAngle2North, 'contextBldg')
# ground
groundMesh = self.createMesh(self.punchedGround, self.gridSize * 200)
if len(self.shadedGround) != 0:
groundShdMesh = self.createMesh(self.shadedGround, self.gridSize)
groundMesh = groundMesh + groundShdMesh
# G angles for ground
# should be replaced with something meaningful later
srfsAngle2North = []
for count, groundFaces in enumerate(groundMesh):
srfsAngle2North.append([])
for face in groundFaces: srfsAngle2North[count].append('G')
writeSTLFile(self.workingDir + "/geo", "ground", groundMesh, srfsAngle2North, 'ground')
self.stlMesh = joinMesh(self.flattenList(bldgMesh) + self.flattenList(contextMesh) + self.flattenList(groundMesh))
from butterfly.
@mostaphaRoudsari Do you want me to test it?
from butterfly.
@antonszilasi, sure!
There only comment that I have about the workflow is that we can handle the naming prices precisely inside the code and export a single clean file.
from butterfly.
Just a note, at some point every EnergyPlus surface and sub-surface object
will need it's own STL label so that boundary conditions can be applied
locally.
On Thu, Nov 5, 2015 at 10:11 AM, Mostapha Sadeghipour Roudsari <
[email protected]> wrote:
@antonszilasi https://github.com/antonszilasi, sure!
There only comment that I have about the workflow is that we can handle
the naming prices precisely inside the code and export a single clean file.—
Reply to this email directly or view it on GitHub
#6 (comment)
.
from butterfly.
Note: There is also some sample code here
https://github.com/mcneel/rhinoscript/blob/master/ExportCustomSTL.rvb
from butterfly.
@TheodoreGalanos Can you test both the blockMeshDict and the STL exporter that I have created in this Grasshopper file. https://github.com/antonszilasi/Butterfly/blob/master/Butterfly%20roadmap%20step1%20and%202%20BlockMeshDict%20STL%20example/createBlockMeshDict%26STL.gh
The two components are grouped in purple at the bottom of the GH file.
from butterfly.
Done!
c5d2cb3
from butterfly.
Related Issues (20)
- Blockmeshcomponent error HOT 4
- Blockmesh Component Error HOT 2
- Butterfly<1.solution exception:windowserror. hep!help! HOT 1
- blockmesh :1.solution exception:windowserror. Who can help me!!! HOT 4
- Solution exception: WindowsError HOT 2
- Running time for Butterfly HOT 3
- Runtime error (UnauthorizedAccessException): Access to the path is denied
- butterfly doesn't seem to wake up blueCFD HOT 1
- Butterfly, Rhino, and BlueCore2020? HOT 6
- 1. Solution exception:WindowsError (wind analysis)
- Butterfly BlueCFD Installation. Followed instructions for both but still receiving error '1. Solution exception: Failed to import butterfly: No module named butterfly_grasshopper.timer'
- Blockmesh Component Error HOT 2
- Blockmesh Component Error HOT 2
- SnappyHexMesh Failed!
- 1. Solution exception:'"C:\Program Files\blueCFD-Core-2020\setvars.bat"' is not recognized as an internal or external command, operable program or batch file HOT 1
- Deleted
- Solution exception:Failed to find points file at C:\Users\An'd's\butterfly\outdoor_airflow\constant\polyMesh
- help,butterfly block mesh is warning HOT 4
- AddLayers output empty with True input in SnappyHexMesh HOT 2
- Moving or dynamic mesh
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from butterfly.