2005-02-03 12:38:31 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
2016-06-13 07:29:25 +02:00
|
|
|
* Copyright(C) 2004-2016 \/)\/ *
|
2005-02-03 12:38:31 +01:00
|
|
|
* Visual Computing Lab /\/| *
|
|
|
|
* ISTI - Italian National Research Council | *
|
|
|
|
* \ *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
2013-07-26 16:01:17 +02:00
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
2005-02-03 12:38:31 +01:00
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
|
|
|
* for more details. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
2006-11-21 20:23:50 +01:00
|
|
|
/****************************************************************************
|
|
|
|
History
|
|
|
|
$Log: not supported by cvs2svn $
|
|
|
|
****************************************************************************/
|
|
|
|
|
2005-02-03 12:38:31 +01:00
|
|
|
#ifndef __VCGLIB_EXPORT_DXF
|
|
|
|
#define __VCGLIB_EXPORT_DXF
|
|
|
|
|
|
|
|
namespace vcg {
|
|
|
|
namespace tri {
|
|
|
|
namespace io {
|
|
|
|
|
|
|
|
template <class SaveMeshType>
|
2013-07-26 16:01:17 +02:00
|
|
|
/**
|
2006-11-21 20:23:50 +01:00
|
|
|
This class encapsulate a filter for save dxf meshes.
|
|
|
|
*/
|
2005-02-03 12:38:31 +01:00
|
|
|
class ExporterDXF
|
|
|
|
{
|
2014-06-18 12:55:09 +02:00
|
|
|
typedef typename SaveMeshType::CoordType CoordType;
|
2005-02-03 12:38:31 +01:00
|
|
|
public:
|
2013-07-26 16:01:17 +02:00
|
|
|
///Standard call for saving a mesh
|
2021-03-19 12:58:38 +01:00
|
|
|
static int Save(const SaveMeshType &m, const char * filename)
|
2013-07-26 16:01:17 +02:00
|
|
|
{
|
|
|
|
if(m.fn==0 && m.en != 0) return SaveEdge(m,filename);
|
|
|
|
|
|
|
|
FILE * o = fopen(filename,"w");
|
|
|
|
if(o==NULL) return 1;
|
2016-01-05 11:09:02 +01:00
|
|
|
|
|
|
|
writeHeader(o, m);
|
|
|
|
|
2013-07-26 16:01:17 +02:00
|
|
|
fprintf(o,"0\n");
|
|
|
|
fprintf(o,"SECTION\n");
|
|
|
|
fprintf(o,"2\n");
|
|
|
|
fprintf(o,"ENTITIES\n");
|
|
|
|
|
2021-03-19 12:58:38 +01:00
|
|
|
typename SaveMeshType::ConstFaceIterator fi;
|
2013-07-26 16:01:17 +02:00
|
|
|
for(fi=m.face.begin(); fi!=m.face.end(); ++fi)
|
|
|
|
{
|
|
|
|
if (!fi->IsD())
|
|
|
|
{
|
|
|
|
typename SaveMeshType::CoordType v0 = (*fi).V(0)->P();
|
|
|
|
typename SaveMeshType::CoordType v1 = (*fi).V(1)->P();
|
|
|
|
typename SaveMeshType::CoordType v2 = (*fi).V(2)->P();
|
|
|
|
fprintf(o,"0\n"); fprintf(o,"3DFACE\n"); fprintf(o,"8\n"); fprintf(o,"0\n");
|
|
|
|
fprintf(o,"10\n"); fprintf(o,"%f\n", v0[0]); //X
|
|
|
|
fprintf(o,"20\n"); fprintf(o,"%f\n", v0[1]); //Y
|
|
|
|
fprintf(o,"30\n"); fprintf(o,"%f\n", v0[2]); //Z
|
|
|
|
|
|
|
|
fprintf(o,"11\n"); fprintf(o,"%f\n", v1[0]); //X
|
|
|
|
fprintf(o,"21\n"); fprintf(o,"%f\n", v1[1]); //Y
|
|
|
|
fprintf(o,"31\n"); fprintf(o,"%f\n", v1[2]); //Z
|
|
|
|
|
|
|
|
fprintf(o,"12\n"); fprintf(o,"%f\n", v2[0]); //X
|
|
|
|
fprintf(o,"22\n"); fprintf(o,"%f\n", v2[1]); //Y
|
|
|
|
fprintf(o,"32\n"); fprintf(o,"%f\n", v2[2]); //Z
|
|
|
|
|
|
|
|
fprintf(o,"13\n"); fprintf(o,"%f\n", v2[0]); //X
|
|
|
|
fprintf(o,"23\n"); fprintf(o,"%f\n", v2[1]); //Y
|
|
|
|
fprintf(o,"33\n"); fprintf(o,"%f\n", v2[2]); //Z
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(o,"0\n");
|
|
|
|
fprintf(o,"ENDSEC\n");
|
|
|
|
fprintf(o,"0\n");
|
|
|
|
fprintf(o,"EOF\n");
|
2018-05-09 14:06:32 +02:00
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
if (ferror(o)) result = 2;
|
|
|
|
fclose(o);
|
|
|
|
return result;
|
2013-07-26 16:01:17 +02:00
|
|
|
}
|
|
|
|
/// Standard call for knowing the meaning of an error code
|
|
|
|
static const char *ErrorMsg(int error)
|
|
|
|
{
|
2018-05-09 14:06:32 +02:00
|
|
|
static std::vector<std::string> dxf_error_msg;
|
|
|
|
if (dxf_error_msg.empty())
|
|
|
|
{
|
|
|
|
dxf_error_msg.resize(3);
|
|
|
|
dxf_error_msg[0] = "No errors";
|
|
|
|
dxf_error_msg[1] = "Can't open file";
|
|
|
|
dxf_error_msg[2] = "Output Stream Error";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error>2 || error<0) return "Unknown error";
|
|
|
|
else return dxf_error_msg[error].c_str();
|
2013-07-26 16:01:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-19 12:58:38 +01:00
|
|
|
static bool SaveEdge(const SaveMeshType &m, const char * filename)
|
2013-07-26 16:01:17 +02:00
|
|
|
{
|
|
|
|
FILE * o = fopen(filename,"w");
|
|
|
|
if(o==NULL) return 1;
|
2016-01-05 11:09:02 +01:00
|
|
|
|
|
|
|
writeHeader(o, m);
|
|
|
|
|
2013-07-26 16:01:17 +02:00
|
|
|
fprintf(o,"0\n");
|
|
|
|
fprintf(o,"SECTION\n");
|
|
|
|
fprintf(o,"2\n");
|
|
|
|
fprintf(o,"ENTITIES\n");
|
|
|
|
|
2021-03-19 12:58:38 +01:00
|
|
|
typename SaveMeshType::ConstEdgeIterator ei;
|
2016-01-05 11:09:02 +01:00
|
|
|
for(ei=m.edge.begin(); ei!=m.edge.end();++ei)
|
2013-07-26 16:01:17 +02:00
|
|
|
{
|
2014-06-18 12:55:09 +02:00
|
|
|
CoordType p1 = (*ei).V(0)->P();
|
|
|
|
CoordType p2 = (*ei).V(1)->P();
|
2013-07-26 16:01:17 +02:00
|
|
|
|
|
|
|
fprintf(o,"0\n");
|
|
|
|
fprintf(o,"LINE\n");
|
|
|
|
fprintf(o,"8\n");
|
|
|
|
fprintf(o,"0\n");
|
|
|
|
fprintf(o,"10\n");
|
|
|
|
|
|
|
|
fprintf(o,"%f\n", p1[0]); //X
|
|
|
|
fprintf(o,"20\n");
|
|
|
|
fprintf(o,"%f\n", p1[1]); //Y
|
|
|
|
fprintf(o,"30\n");
|
|
|
|
fprintf(o,"%f\n", p1[2]); //Z
|
|
|
|
|
|
|
|
fprintf(o,"11\n");
|
|
|
|
fprintf(o,"%f\n", p2[0]); //X
|
|
|
|
fprintf(o,"21\n");
|
|
|
|
fprintf(o,"%f\n", p2[1]); //Y
|
|
|
|
fprintf(o,"31\n");
|
|
|
|
fprintf(o,"%f\n", p2[2]); //Z
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(o,"0\n");
|
|
|
|
fprintf(o,"ENDSEC\n");
|
|
|
|
fprintf(o,"0\n");
|
|
|
|
fprintf(o,"EOF\n");
|
|
|
|
fclose(o);
|
|
|
|
return true;
|
|
|
|
}
|
2006-01-13 16:47:43 +01:00
|
|
|
|
2021-03-19 12:58:38 +01:00
|
|
|
static bool writeHeader(FILE* o, const SaveMeshType &mp)
|
2016-01-05 11:09:02 +01:00
|
|
|
{
|
|
|
|
// standard DXF header
|
|
|
|
// most of data is meaningless, but required by a lot of importers
|
|
|
|
fprintf(o, "999\n");
|
2016-06-13 07:29:25 +02:00
|
|
|
fprintf(o, "DXF created by VCGLib\n");
|
|
|
|
fprintf(o, "0\n");
|
|
|
|
fprintf(o, "SECTION\n");
|
|
|
|
fprintf(o, "2\n");
|
|
|
|
fprintf(o, "HEADER\n");
|
|
|
|
|
|
|
|
// Version of the dxf specs, most reader need version 12 or above (AC1009)
|
|
|
|
fprintf(o, "9\n");
|
|
|
|
fprintf(o, "$ACADVER\n");
|
|
|
|
fprintf(o, "1\n");
|
|
|
|
fprintf(o, "AC1009\n");
|
|
|
|
|
|
|
|
// Insertion base set by BASE command (in WCS)
|
|
|
|
fprintf(o, "9\n");
|
|
|
|
fprintf(o, "$INSBASE\n");
|
|
|
|
fprintf(o, "10\n");
|
|
|
|
fprintf(o, "0.0\n");
|
|
|
|
fprintf(o, "20\n");
|
|
|
|
fprintf(o, "0.0\n");
|
|
|
|
fprintf(o, "30\n");
|
|
|
|
fprintf(o, "0.0\n");
|
|
|
|
|
|
|
|
// extents for draw space and line drawing...
|
|
|
|
// I will just use the data from the boundingbox (largest bbox value in all directions)
|
|
|
|
double emin = std::min(mp.bbox.min[0], std::min(mp.bbox.min[1], mp.bbox.min[2]));
|
|
|
|
double emax = std::max(mp.bbox.max[0], std::max(mp.bbox.max[1], mp.bbox.max[2]));
|
|
|
|
|
|
|
|
fprintf(o, "9\n");
|
|
|
|
fprintf(o, "$EXTMIN\n");
|
|
|
|
fprintf(o, "10\n");
|
|
|
|
fprintf(o, "%f\n",emin);
|
|
|
|
fprintf(o, "20\n");
|
|
|
|
fprintf(o, "%f\n",emin);
|
|
|
|
|
|
|
|
fprintf(o, "9\n");
|
|
|
|
fprintf(o, "$EXTMAX\n");
|
|
|
|
fprintf(o, "10\n");
|
|
|
|
fprintf(o, "%f\n", emax);
|
|
|
|
fprintf(o, "20\n");
|
|
|
|
fprintf(o, "%f\n", emax);
|
|
|
|
|
|
|
|
fprintf(o, "9\n");
|
|
|
|
fprintf(o, "$LINMIN\n");
|
|
|
|
fprintf(o, "10\n");
|
|
|
|
fprintf(o, "%f\n", emin);
|
|
|
|
fprintf(o, "20\n");
|
|
|
|
fprintf(o, "%f\n", emin);
|
|
|
|
|
|
|
|
fprintf(o, "9\n");
|
|
|
|
fprintf(o, "$LINMAX\n");
|
|
|
|
fprintf(o, "10\n");
|
|
|
|
fprintf(o, "%f\n", emax);
|
|
|
|
fprintf(o, "20\n");
|
|
|
|
fprintf(o, "%f\n", emax);
|
|
|
|
|
|
|
|
fprintf(o, "0\n");
|
2016-01-05 11:09:02 +01:00
|
|
|
fprintf(o, "ENDSEC\n");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-02-03 12:38:31 +01:00
|
|
|
|
|
|
|
}; // end class
|
|
|
|
|
|
|
|
} // end Namespace io
|
2013-07-26 16:01:17 +02:00
|
|
|
} // end Namespace tri
|
2005-02-03 12:38:31 +01:00
|
|
|
} // end Namespace vcg
|
|
|
|
|
2005-12-01 01:58:56 +01:00
|
|
|
#endif
|