source: main/branches/3D/openPLM/document3D/aficher.py @ 592

Revision 592, 4.6 KB checked in by agalech, 9 years ago (diff)

add document3d app

Line 
1
2from openPLM.document3D.STP_converter_JSS import *
3from openPLM.document3D.models import *
4from OCC.MSH.Mesh import QuickTriangleMesh
5from kjbuckets import kjGraph
6#llevar el stp_to_jss a models
7# si sale mal de mes3js borrar el new_stp_js para que no se muestre   
8
9
10
11   
12def convertir_STP_to_JSS(doc_file):
13
14
15   
16
17    my_step_importer = NEW_STEP_Import(doc_file.file.path.encode())   #simple
18   
19   
20    if not my_step_importer.read_file():
21        return False
22
23   
24   
25
26    my_shape = my_step_importer.get_shapes() 
27
28
29    graph=kjGraph()
30    fileName, fileExtension = os.path.splitext(doc_file.filename)
31    return parcours_recursif(my_shape,fileName,doc_file,graph)   
32   
33     
34   
35    return True   
36   
37   
38def parcours_recursif(my_shape,fileName,doc_file,graph):
39     
40    for i in range(len(my_shape)):
41     
42        if  len(my_shape[i])==1:
43            my_mesh = mesh_shape(my_shape[i][0])
44            new_stp_jss= stp_to_jss()
45            new_stp_jss.stp = doc_file
46            new_stp_jss.save()
47            name = new_stp_jss.js.storage.get_available_name(fileName+".js")
48            path = os.path.join(new_stp_jss.js.storage.location, name)
49            if(not mesh_to_3js(my_mesh,path.encode(),"_"+str(new_stp_jss.id))):
50                new_stp_jss.delete()
51                return False   
52                #exception
53            else:
54
55                new_stp_jss.js = name
56                new_stp_jss.save()
57
58        else:
59            return parcours_recursif(my_shape[i],fileName,doc_file,graph)
60           
61    return True   
62    """
63    for i in range(len(my_shape)):
64       
65        for t in range(len(my_shape[i])):
66            print "estoy aqui adentro"
67        #print "aqui tambien con nombre ",my_shape[i]     
68            my_mesh = mesh_shape(my_shape[i][t])
69            #mesh_to_3js(my_mesh,output,aux,aux2)
70            fileName, fileExtension = os.path.splitext(doc_file.filename)
71            new_stp_jss= stp_to_jss()
72            new_stp_jss.stp = doc_file
73            new_stp_jss.save()
74            name = new_stp_jss.js.storage.get_available_name(fileName+".js")
75            path = os.path.join(new_stp_jss.js.storage.location, name)
76            ###
77            if(not mesh_to_3js(my_mesh,path.encode(),"_"+str(new_stp_jss.id))):
78                print "se borro el fichero"
79                new_stp_jss.delete()   
80            ###
81            else:
82                print "entra"
83                new_stp_jss.js = name
84                new_stp_jss.save()
85    """
86
87
88   
89def mesh_shape(shape):
90    """ Take a topods_shape instance, returns the tesselated object"""
91    a_mesh = QuickTriangleMesh(DISPLAY_LIST=True)
92    a_mesh.set_shape(shape)
93    # define precision for the mesh
94    a_mesh.set_precision(a_mesh.get_precision()/2.)
95    # then compute the mesh
96    a_mesh.compute()
97    return a_mesh       
98
99    return True
100
101
102
103   
104def mesh_to_3js(mesh,filename,name):
105    """ Take a mesh, exports to a three.js javascript file"""
106
107
108   
109    try:
110        directory = os.path.dirname(filename)
111       
112        if not os.path.exists(directory):
113            os.makedirs(directory)
114
115        output = open(filename,"w")
116        output.write("""
117    var %s = function () {
118
119    var scope = this;
120    THREE.Geometry.call( this );
121        """%name)
122
123        # export vertices
124        for vertex in mesh.get_vertices():
125
126           output.write('v(%.4f,%.4f,%.4f);\n'%(vertex[0],vertex[1],vertex[2]))
127        # export faces
128        index = 0
129        faces = mesh.get_faces()
130        normals = mesh.get_normals()
131        while index<len(faces)-2:
132            n1 = normals[index]
133            n2 = normals[index+1]
134            n3 = normals[index+2]
135            output.write('f3(%i,%i,%i,%.4f,%.4f,%.4f,%.4f,%.4f,%.4f,%.4f,%.4f,%.4f);\n'%(faces[index],faces[index+1],faces[index+2],n1[0],n1[1],n1[2],n2[0],n2[1],n2[2],n3[0],n3[1],n3[2]))
136           
137            index += 3
138       
139        #closing file
140        #this.sortFacesByMaterial();
141        output.write("""
142   
143    function v( x, y, z ) {
144        scope.vertices.push( new THREE.Vertex( new THREE.Vector3( x, y, z )  ) );
145        }
146    function f3( a, b, c, n1_x,n1_y,n1_z,n2_x,n2_y,n2_z,n3_x,n3_y,n3_z ) {
147        scope.faces.push( new THREE.Face3( a, b, c, [ new THREE.Vector3( n1_x, n1_y, n1_z ), new THREE.Vector3( n2_x, n2_y, n2_z ), new THREE.Vector3( n3_x, n3_y, n3_z ) ]  ) );
148        }
149        }
150
151    %s.prototype = new THREE.Geometry();
152    %s.prototype.constructor = %s;
153        """%(name,name,name))
154        output.close()
155        return True
156    except IOError as (errno, strerror):
157        print "I/O error({0}): {1}".format(errno, strerror)
158        return False
Note: See TracBrowser for help on using the repository browser.