Giter Site home page Giter Site logo

Generate .stl file about butterfly HOT 7 CLOSED

ladybug-tools avatar ladybug-tools commented on June 27, 2024
Generate .stl file

from butterfly.

Comments (7)

mostaphaRoudsari avatar mostaphaRoudsari commented on June 27, 2024

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.

antonszilasi avatar antonszilasi commented on June 27, 2024

@mostaphaRoudsari Do you want me to test it?

from butterfly.

mostaphaRoudsari avatar mostaphaRoudsari commented on June 27, 2024

@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.

stefan-buildSCI avatar stefan-buildSCI commented on June 27, 2024

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.

antonszilasi avatar antonszilasi commented on June 27, 2024

Note: There is also some sample code here

https://github.com/mcneel/rhinoscript/blob/master/ExportCustomSTL.rvb

from butterfly.

antonszilasi avatar antonszilasi commented on June 27, 2024

@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.

mostaphaRoudsari avatar mostaphaRoudsari commented on June 27, 2024

Done!
c5d2cb3

from butterfly.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.