importOBJ.py

Go to the documentation of this file.
00001 import FreeCAD
00002 from draftlibs import fcgeo
00003 
00004 pythonopen = open
00005 
00006 def findVert(aVertex,aList):
00007     "finds aVertex in aList, returns index"
00008     for i in range(len(aList)):
00009         if (aVertex.X == aList[i].X) and (aVertex.Y == aList[i].Y) and (aVertex.Z == aList[i].Z):
00010             return i
00011 
00012 def getIndices(shape,offset):
00013     "returns a list with 2 lists: vertices and face indexes, offsetted with the given amount"
00014     vlist = []
00015     flist = []
00016     for v in shape.Vertexes:
00017         vlist.append(" "+str(round(v.X,4))+" "+str(round(v.Z,4))+" "+str(round(v.Y,4)))
00018     for f in shape.Faces:
00019         fi = ""
00020         # OCC vertices are unsorted. We need to sort in the right order...
00021         edges = fcgeo.sortEdges(f.Wire.Edges)
00022         print edges
00023         for e in edges:
00024             print e.Vertexes[0].Point,e.Vertexes[1].Point
00025             v = e.Vertexes[0]
00026             fi+=" "+str(findVert(v,shape.Vertexes)+offset)
00027         flist.append(fi)
00028     return vlist,flist
00029 
00030 def export(exportList,filename):
00031     "called when freecad exports a file"
00032     outfile = pythonopen(filename,"wb")
00033     ver = FreeCAD.Version()
00034     outfile.write("# FreeCAD v" + ver[0] + "." + ver[1] + " build" + ver[2] + " Arch module\n")
00035     outfile.write("# http://free-cad.sf.net\n")
00036     offset = 1
00037     for obj in exportList:
00038         if obj.isDerivedFrom("Part::Feature"):
00039             vlist,flist = getIndices(obj.Shape,offset)
00040             offset += len(vlist)
00041             outfile.write("o " + obj.Name + "\n")
00042             for v in vlist:
00043                 outfile.write("v" + v + "\n")
00044             for f in flist:
00045                 outfile.write("f" + f + "\n")
00046     outfile.close()
00047     FreeCAD.Console.PrintMessage("successfully written "+filename)
00048             
00049             
00050             

Generated on Wed Nov 23 19:00:18 2011 for FreeCAD by  doxygen 1.6.1