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 #ifndef __VCGLIB_EXPORT_DXF
00029 #define __VCGLIB_EXPORT_DXF
00030
00031 namespace vcg {
00032 namespace tri {
00033 namespace io {
00034
00035 template <class SaveMeshType>
00039 class ExporterDXF
00040 {
00041 typedef typename SaveMeshType::CoordType CoordType;
00042 public:
00044 static int Save(SaveMeshType &m, const char * filename)
00045 {
00046 if(m.fn==0 && m.en != 0) return SaveEdge(m,filename);
00047
00048 FILE * o = fopen(filename,"w");
00049 if(o==NULL) return 1;
00050
00051 writeHeader(o, m);
00052
00053 fprintf(o,"0\n");
00054 fprintf(o,"SECTION\n");
00055 fprintf(o,"2\n");
00056 fprintf(o,"ENTITIES\n");
00057
00058 typename SaveMeshType::FaceIterator fi;
00059 for(fi=m.face.begin(); fi!=m.face.end(); ++fi)
00060 {
00061 if (!fi->IsD())
00062 {
00063 typename SaveMeshType::CoordType v0 = (*fi).V(0)->P();
00064 typename SaveMeshType::CoordType v1 = (*fi).V(1)->P();
00065 typename SaveMeshType::CoordType v2 = (*fi).V(2)->P();
00066 fprintf(o,"0\n"); fprintf(o,"3DFACE\n"); fprintf(o,"8\n"); fprintf(o,"0\n");
00067 fprintf(o,"10\n"); fprintf(o,"%f\n", v0[0]);
00068 fprintf(o,"20\n"); fprintf(o,"%f\n", v0[1]);
00069 fprintf(o,"30\n"); fprintf(o,"%f\n", v0[2]);
00070
00071 fprintf(o,"11\n"); fprintf(o,"%f\n", v1[0]);
00072 fprintf(o,"21\n"); fprintf(o,"%f\n", v1[1]);
00073 fprintf(o,"31\n"); fprintf(o,"%f\n", v1[2]);
00074
00075 fprintf(o,"12\n"); fprintf(o,"%f\n", v2[0]);
00076 fprintf(o,"22\n"); fprintf(o,"%f\n", v2[1]);
00077 fprintf(o,"32\n"); fprintf(o,"%f\n", v2[2]);
00078
00079 fprintf(o,"13\n"); fprintf(o,"%f\n", v2[0]);
00080 fprintf(o,"23\n"); fprintf(o,"%f\n", v2[1]);
00081 fprintf(o,"33\n"); fprintf(o,"%f\n", v2[2]);
00082 }
00083 }
00084
00085 fprintf(o,"0\n");
00086 fprintf(o,"ENDSEC\n");
00087 fprintf(o,"0\n");
00088 fprintf(o,"EOF\n");
00089 fclose(o);
00090 return 0;
00091 }
00093 static const char *ErrorMsg(int error)
00094 {
00095 static std::vector<std::string> dxf_error_msg;
00096 if(dxf_error_msg.empty())
00097 {
00098 dxf_error_msg.resize(2 );
00099 dxf_error_msg[0]="No errors";
00100 dxf_error_msg[1]="Can't open file";
00101 }
00102
00103 if(error>1 || error<0) return "Unknown error";
00104 else return dxf_error_msg[error].c_str();
00105 }
00106
00107
00108 static bool SaveEdge(SaveMeshType &m, const char * filename)
00109 {
00110 FILE * o = fopen(filename,"w");
00111 if(o==NULL) return 1;
00112
00113 writeHeader(o, m);
00114
00115 fprintf(o,"0\n");
00116 fprintf(o,"SECTION\n");
00117 fprintf(o,"2\n");
00118 fprintf(o,"ENTITIES\n");
00119
00120 typename SaveMeshType::EdgeIterator ei;
00121 for(ei=m.edge.begin(); ei!=m.edge.end();++ei)
00122 {
00123 CoordType p1 = (*ei).V(0)->P();
00124 CoordType p2 = (*ei).V(1)->P();
00125
00126 fprintf(o,"0\n");
00127 fprintf(o,"LINE\n");
00128 fprintf(o,"8\n");
00129 fprintf(o,"0\n");
00130 fprintf(o,"10\n");
00131
00132 fprintf(o,"%f\n", p1[0]);
00133 fprintf(o,"20\n");
00134 fprintf(o,"%f\n", p1[1]);
00135 fprintf(o,"30\n");
00136 fprintf(o,"%f\n", p1[2]);
00137
00138 fprintf(o,"11\n");
00139 fprintf(o,"%f\n", p2[0]);
00140 fprintf(o,"21\n");
00141 fprintf(o,"%f\n", p2[1]);
00142 fprintf(o,"31\n");
00143 fprintf(o,"%f\n", p2[2]);
00144 }
00145
00146 fprintf(o,"0\n");
00147 fprintf(o,"ENDSEC\n");
00148 fprintf(o,"0\n");
00149 fprintf(o,"EOF\n");
00150 fclose(o);
00151 return true;
00152 }
00153
00154 static bool writeHeader(FILE* o, SaveMeshType &mp)
00155 {
00156
00157
00158 fprintf(o, "999\n");
00159 fprintf(o, "DXF created by VCGLib\n");
00160 fprintf(o, "0\n");
00161 fprintf(o, "SECTION\n");
00162 fprintf(o, "2\n");
00163 fprintf(o, "HEADER\n");
00164
00165
00166 fprintf(o, "9\n");
00167 fprintf(o, "$ACADVER\n");
00168 fprintf(o, "1\n");
00169 fprintf(o, "AC1009\n");
00170
00171
00172 fprintf(o, "9\n");
00173 fprintf(o, "$INSBASE\n");
00174 fprintf(o, "10\n");
00175 fprintf(o, "0.0\n");
00176 fprintf(o, "20\n");
00177 fprintf(o, "0.0\n");
00178 fprintf(o, "30\n");
00179 fprintf(o, "0.0\n");
00180
00181
00182
00183 double emin = std::min(mp.bbox.min[0], std::min(mp.bbox.min[1], mp.bbox.min[2]));
00184 double emax = std::max(mp.bbox.max[0], std::max(mp.bbox.max[1], mp.bbox.max[2]));
00185
00186 fprintf(o, "9\n");
00187 fprintf(o, "$EXTMIN\n");
00188 fprintf(o, "10\n");
00189 fprintf(o, "%f\n",emin);
00190 fprintf(o, "20\n");
00191 fprintf(o, "%f\n",emin);
00192
00193 fprintf(o, "9\n");
00194 fprintf(o, "$EXTMAX\n");
00195 fprintf(o, "10\n");
00196 fprintf(o, "%f\n", emax);
00197 fprintf(o, "20\n");
00198 fprintf(o, "%f\n", emax);
00199
00200 fprintf(o, "9\n");
00201 fprintf(o, "$LINMIN\n");
00202 fprintf(o, "10\n");
00203 fprintf(o, "%f\n", emin);
00204 fprintf(o, "20\n");
00205 fprintf(o, "%f\n", emin);
00206
00207 fprintf(o, "9\n");
00208 fprintf(o, "$LINMAX\n");
00209 fprintf(o, "10\n");
00210 fprintf(o, "%f\n", emax);
00211 fprintf(o, "20\n");
00212 fprintf(o, "%f\n", emax);
00213
00214 fprintf(o, "0\n");
00215 fprintf(o, "ENDSEC\n");
00216
00217 return true;
00218 }
00219
00220
00221 };
00222
00223 }
00224 }
00225 }
00226
00227 #endif