00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 #ifndef __VCGLIB_EXPORT_STL
00059 #define __VCGLIB_EXPORT_STL
00060
00061 #include <stdio.h>
00062
00063 namespace vcg {
00064 namespace tri {
00065 namespace io {
00066
00071 template <class SaveMeshType>
00072 class ExporterSTL
00073 {
00074 public:
00075 typedef typename SaveMeshType::FaceType FaceType;
00076 typedef unsigned short CallBackSTLFaceAttribute(const SaveMeshType &m, const FaceType &f);
00077
00078 static int Save(SaveMeshType &m, const char * filename, const int &mask, CallBackPos *)
00079 {
00080 return Save(m,filename,true,mask);
00081 }
00082
00083 static int Save(SaveMeshType &m, const char * filename , bool binary =true, int mask=0, const char *objectname=0, bool magicsMode=0)
00084 {
00085 typedef typename SaveMeshType::FaceIterator FaceIterator;
00086 FILE *fp;
00087
00088 fp = fopen(filename,"wb");
00089 if(fp==0)
00090 return 1;
00091
00092 if(binary)
00093 {
00094
00095 char header[128]="VCG ";
00096 if(objectname) strncpy(header,objectname,80);
00097 if(magicsMode)
00098 {
00099 strncpy(header,"COLOR=XXX MATERIAL=AAA BBB CCC ",80);
00100 for(int i=0;i<3;++i)
00101 {
00102 header[0x06+i]=0x7f;
00103 header[0x13+i]=0x7f;
00104 header[0x17+i]=0x7f;
00105 header[0x1b+i]=0x7f;
00106 }
00107 }
00108 fwrite(header,80,1,fp);
00109
00110 fwrite(&m.fn,1,sizeof(int),fp);
00111 Point3f p;
00112 unsigned short attributes=0;
00113 for(FaceIterator fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() )
00114 {
00115
00116 p.Import(vcg::TriangleNormal(*fi).Normalize());
00117 fwrite(p.V(),3,sizeof(float),fp);
00118
00119 for(int k=0;k<3;++k){
00120 p.Import((*fi).V(k)->P());
00121 fwrite(p.V(),3,sizeof(float),fp);
00122 }
00123 if ((mask & Mask::IOM_FACECOLOR) && tri::HasPerFaceColor(m))
00124 {
00125 if(magicsMode) attributes = 32768 | vcg::Color4b::ToUnsignedR5G5B5(fi->C());
00126 else attributes = 32768 | vcg::Color4b::ToUnsignedB5G5R5(fi->C());
00127 }
00128 fwrite(&attributes,1,sizeof(short),fp);
00129 }
00130 }
00131 else
00132 {
00133 if(objectname) fprintf(fp,"solid %s\n",objectname);
00134 else fprintf(fp,"solid vcg\n");
00135
00136 Point3f p;
00137 FaceIterator fi;
00138 for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() )
00139 {
00140
00141 p.Import(TriangleNormal(*fi).Normalize());
00142 fprintf(fp," facet normal %13e %13e %13e\n",p[0],p[1],p[2]);
00143 fprintf(fp," outer loop\n");
00144 for(int k=0;k<3;++k){
00145 p.Import((*fi).V(k)->P());
00146 fprintf(fp," vertex %13e %13e %13e\n",p[0],p[1],p[2]);
00147 }
00148 fprintf(fp," endloop\n");
00149 fprintf(fp," endfacet\n");
00150 }
00151 fprintf(fp,"endsolid vcg\n");
00152 }
00153 fclose(fp);
00154 return 0;
00155 }
00156 static const char *ErrorMsg(int error)
00157 {
00158 static std::vector<std::string> stl_error_msg;
00159 if(stl_error_msg.empty())
00160 {
00161 stl_error_msg.resize(2 );
00162 stl_error_msg[0]="No errors";
00163 stl_error_msg[1]="Can't open file";
00164 }
00165
00166 if(error>1 || error<0) return "Unknown error";
00167 else return stl_error_msg[error].c_str();
00168 };
00169
00170
00171
00172
00173 static int GetExportMaskCapability()
00174 {
00175 int capability = 0;
00176 capability |= vcg::tri::io::Mask::IOM_VERTCOORD;
00177 capability |= vcg::tri::io::Mask::IOM_FACEINDEX;
00178 capability |= vcg::tri::io::Mask::IOM_FACECOLOR;
00179 return capability;
00180 }
00181
00182
00183 };
00184
00185 }
00186 }
00187 }
00188
00189
00190
00191 #endif