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 &, CallBackPos *)
00079 {
00080 return Save(m,filename,true);
00081 }
00082
00083 static int Save(SaveMeshType &m, const char * filename , bool binary =true, const char *objectname=0, CallBackSTLFaceAttribute *faceAttributeCallback = NULL)
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 fwrite(header,80,1,fp);
00098
00099 fwrite(&m.fn,1,sizeof(int),fp);
00100 Point3f p;
00101 unsigned short attributes=0;
00102
00103 FaceIterator fi;
00104 for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() )
00105 {
00106
00107 p.Import(vcg::NormalizedNormal(*fi));
00108 fwrite(p.V(),3,sizeof(float),fp);
00109
00110 for(int k=0;k<3;++k){
00111 p.Import((*fi).V(k)->P());
00112 fwrite(p.V(),3,sizeof(float),fp);
00113 }
00114 if (faceAttributeCallback != NULL) {
00115 attributes = (*faceAttributeCallback)(m, *fi);
00116 }
00117 fwrite(&attributes,1,sizeof(short),fp);
00118 }
00119 }
00120 else
00121 {
00122 if(objectname) fprintf(fp,"solid %s\n",objectname);
00123 else fprintf(fp,"solid vcg\n");
00124
00125 Point3f p;
00126 FaceIterator fi;
00127 for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() )
00128 {
00129
00130 p.Import(vcg::NormalizedNormal(*fi));
00131 fprintf(fp," facet normal %13e %13e %13e\n",p[0],p[1],p[2]);
00132 fprintf(fp," outer loop\n");
00133 for(int k=0;k<3;++k){
00134 p.Import((*fi).V(k)->P());
00135 fprintf(fp," vertex %13e %13e %13e\n",p[0],p[1],p[2]);
00136 }
00137 fprintf(fp," endloop\n");
00138 fprintf(fp," endfacet\n");
00139 }
00140 fprintf(fp,"endsolid vcg\n");
00141 }
00142 fclose(fp);
00143 return 0;
00144 }
00145 static const char *ErrorMsg(int error)
00146 {
00147 static std::vector<std::string> stl_error_msg;
00148 if(stl_error_msg.empty())
00149 {
00150 stl_error_msg.resize(2 );
00151 stl_error_msg[0]="No errors";
00152 stl_error_msg[1]="Can't open file";
00153 }
00154
00155 if(error>1 || error<0) return "Unknown error";
00156 else return stl_error_msg[error].c_str();
00157 };
00158
00159
00160
00161
00162 static int GetExportMaskCapability()
00163 {
00164 int capability = 0;
00165 capability |= vcg::tri::io::Mask::IOM_VERTCOORD;
00166 capability |= vcg::tri::io::Mask::IOM_FACEINDEX;
00167 return capability;
00168 }
00169
00170
00171 };
00172
00173 }
00174 }
00175 }
00176
00177
00178
00179 #endif