from OCC.BRep import *
from OCC.BRepMesh import *
from OCC.BRepBuilderAPI import *
from OCC.TopoDS import *
from OCC.gp import *
from OCC.BRep import BRep_Tool
from OCC.BRepOffsetAPI import BRepOffsetAPI_MakePipe
from OCC.Geom import Geom_Circle
from OCC.Utils import *
from OCC.TopLoc import *
from OCC.BRepGProp import *



def mesh_shape(shape):
    if shape.IsNull():
        return
    print 'got shape'
    BRepMesh_Mesh(shape, 0.01)
    t = Topo(shape)
    numTriangles = 0
    triangulations = []
    faces = []
    for  aFace in t.faces():
        loc = TopLoc_Location()
        triangulation = BRep_Tool_Triangulation(aFace, loc)
        faces.append(aFace)
        if triangulation.IsNull() == False:
            numTriangles += triangulation.GetObject().NbTriangles()
            triangulations.append(triangulation)
    
    normals = [0.] * numTriangles * 3 * 3
    triangles = [0.] * numTriangles * 3 * 3
    
    t = Topo(shape)
    index = 0
    for i in range(len(triangulations)):
        triangulation = triangulations[i]
        tris = triangulation.GetObject().Triangles()
        nodes = triangulation.GetObject().Nodes()
        prop = BRepGProp_Face(faces[i])
        for j in range(1, triangulation.GetObject().NbTriangles() + 1):
            
            N1, N2, N3 = tris.Value(j).Get()
            V1 = nodes.Value(N1)
            V2 = nodes.Value(N2)
            V3 = nodes.Value(N3)
            triangles[index * 9] = V1.X();
            triangles[index * 9 + 1] = V1.Y();
            triangles[index * 9 + 2] = V1.Z();
            triangles[index * 9 + 3] = V2.X();
            triangles[index * 9 + 4] = V2.Y();
            triangles[index * 9 + 5] = V2.Z();
            triangles[index * 9 + 6] = V3.X();
            triangles[index * 9 + 7] = V3.Y();
            triangles[index * 9 + 8] = V3.Z();
            
            uv = triangulation.GetObject().UVNodes().Value(N1);
            tmp = gp_Pnt()
            norm = gp_Vec()
            prop.Normal(uv.X(), uv.Y(), tmp, norm);
            norm.Normalize();
            normals[index * 9] = norm.X();
            normals[index * 9 + 1] = norm.Y();
            normals[index * 9 + 2] = norm.Z();
            
            norm.Normalize();
            normals[index * 9 + 3] = norm.X();
            normals[index * 9 + 4] = norm.Y();
            normals[index * 9 + 5] = norm.Z();
            
            uv = triangulation.GetObject().UVNodes().Value(N3)
            prop.Normal(uv.X(), uv.Y(), tmp, norm)
            norm.Normalize()
            normals[index * 9 + 6] = norm.X()
            normals[index * 9 + 7] = norm.Y()
            normals[index * 9 + 8] = norm.Z() 
            index = index + 1
    print 'finished meshing,creating arrays',len(triangles)

p1 = gp_Pnt(0, 0, 0)
p2 = gp_Pnt(0, 0, 10)
v1 = BRepBuilderAPI_MakeVertex(p1).Vertex()
v2 = BRepBuilderAPI_MakeVertex(p2).Vertex()
edge = BRepBuilderAPI_MakeEdge(v1,v2).Edge()
circle = BRepBuilderAPI_MakeEdge(gp_Circ(gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), 10.)).Edge()
edge = BRepBuilderAPI_MakeWire(edge).Wire()
shape = BRepOffsetAPI_MakePipe(edge,circle).Shape()
mesh_shape(shape)
GarbageCollector.garbage.smart_purge()
