00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 import FreeCAD, collada, Mesh, os, numpy
00025
00026 __title__="FreeCAD Collada importer"
00027 __author__ = "Yorik van Havre"
00028 __url__ = "http://free-cad.sourceforge.net"
00029
00030 DEBUG = True
00031
00032 def open(filename):
00033 "called when freecad wants to open a file"
00034 docname = os.path.splitext(os.path.basename(filename))[0]
00035 doc = FreeCAD.newDocument(docname)
00036 doc.Label = decode(docname)
00037 FreeCAD.ActiveDocument = doc
00038 read(filename)
00039 return doc
00040
00041 def decode(name):
00042 "decodes encoded strings"
00043 try:
00044 decodedName = (name.decode("utf8"))
00045 except UnicodeDecodeError:
00046 try:
00047 decodedName = (name.decode("latin1"))
00048 except UnicodeDecodeError:
00049 print "ifc: error: couldn't determine character encoding"
00050 decodedName = name
00051 return decodedName
00052
00053 def read(filename):
00054 global col
00055 col = collada.Collada(filename, ignore=[collada.DaeUnsupportedError])
00056 for geom in col.scene.objects('geometry'):
00057
00058 for prim in geom.primitives():
00059
00060 print prim, dir(prim)
00061 meshdata = []
00062 if hasattr(prim,"triangles"):
00063 tset = prim.triangles()
00064 elif hasattr(prim,"triangleset"):
00065 tset = prim.triangleset()
00066 for tri in tset:
00067 face = []
00068 for v in tri.vertices:
00069 face.append([v[0],v[1],v[2]])
00070 meshdata.append(face)
00071 print meshdata
00072 newmesh = Mesh.Mesh(meshdata)
00073 print newmesh
00074 obj = FreeCAD.ActiveDocument.addObject("Mesh::Feature","Mesh")
00075 obj.Mesh = newmesh
00076
00077 def export(exportList,filename):
00078 "called when freecad exports a file"
00079 colmesh = collada.Collada()
00080 effect = collada.material.Effect("effect0", [], "phong", diffuse=(.7,.7,.7), specular=(1,1,1))
00081 mat = collada.material.Material("material0", "mymaterial", effect)
00082 colmesh.effects.append(effect)
00083 colmesh.materials.append(mat)
00084 objind = 0
00085 scenenodes = []
00086 for obj in exportList:
00087 if obj.isDerivedFrom("Mesh::Feature"):
00088 print "exporting object ",obj.Name, obj.Mesh
00089 m = obj.Mesh
00090 vindex = []
00091 nindex = []
00092 findex = []
00093
00094 for v in m.Topology[0]:
00095 vindex.extend([v.x,v.y,v.z])
00096
00097 for f in m.Facets:
00098 n = f.Normal
00099 nindex.extend([n.x,n.y,n.z])
00100
00101 for i in range(len(m.Topology[1])):
00102 f = m.Topology[1][i]
00103 findex.extend([f[0],i,f[1],i,f[2],i])
00104 print len(vindex), " vert indices, ", len(nindex), " norm indices, ", len(findex), " face indices."
00105 vert_src = collada.source.FloatSource("cubeverts-array"+str(objind), numpy.array(vindex), ('X', 'Y', 'Z'))
00106 normal_src = collada.source.FloatSource("cubenormals-array"+str(objind), numpy.array(nindex), ('X', 'Y', 'Z'))
00107 geom = collada.geometry.Geometry(colmesh, "geometry"+str(objind), obj.Name, [vert_src, normal_src])
00108 input_list = collada.source.InputList()
00109 input_list.addInput(0, 'VERTEX', "#cubeverts-array"+str(objind))
00110 input_list.addInput(1, 'NORMAL', "#cubenormals-array"+str(objind))
00111 triset = geom.createTriangleSet(numpy.array(findex), input_list, "materialref")
00112 geom.primitives.append(triset)
00113 colmesh.geometries.append(geom)
00114 matnode = collada.scene.MaterialNode("materialref", mat, inputs=[])
00115 geomnode = collada.scene.GeometryNode(geom, [matnode])
00116 node = collada.scene.Node("node"+str(objind), children=[geomnode])
00117 scenenodes.append(node)
00118 objind += 1
00119 myscene = collada.scene.Scene("myscene", scenenodes)
00120 colmesh.scenes.append(myscene)
00121 colmesh.scene = myscene
00122 myscene = collada.scene.Scene("myscene", [node])
00123 colmesh.scenes.append(myscene)
00124 colmesh.scene = myscene
00125 colmesh.write(filename)
00126 print "file ",filename," successfully created."