Panda3D

eggToDXF.cxx

00001 // Filename: eggToDXF.cxx
00002 // Created by:  drose (04May04)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "eggToDXF.h"
00016 #include "eggPolygon.h"
00017 #include "dcast.h"
00018 #include "pystub.h"
00019   
00020 ////////////////////////////////////////////////////////////////////
00021 //     Function: EggToDXF::Constructor
00022 //       Access: Public
00023 //  Description: 
00024 ////////////////////////////////////////////////////////////////////
00025 EggToDXF::
00026 EggToDXF() :
00027   EggToSomething("DXF", ".dxf", true, false)
00028 {
00029   set_binary_output(true);
00030   set_program_description
00031     ("This program converts files from egg format to AutoCAD DXF format.  "
00032      "Since DXF does not support nested hierarchies, vertex normals, or any "
00033      "fancy stuff you are probably used to, there is some information lost "
00034      "in the conversion");
00035 
00036   add_option
00037     ("p", "", 0,
00038      "Use POLYLINE to represent polygons instead of the default, 3DFACE.",
00039      &EggToDXF::dispatch_none, &_use_polyline);
00040 
00041   _coordinate_system = CS_zup_right;
00042   _got_coordinate_system = true;
00043 }
00044 
00045 ////////////////////////////////////////////////////////////////////
00046 //     Function: EggToDXF::run
00047 //       Access: Public
00048 //  Description:
00049 ////////////////////////////////////////////////////////////////////
00050 void EggToDXF::
00051 run() {
00052   get_layers(_data);
00053   if (_layers.empty()) {
00054     nout << "Egg file contains no polygons.  Output file not written.\n";
00055     exit(1);
00056   }
00057 
00058   //  uniquify_names("layer", _layers.begin(), _layers.end());
00059   
00060   ostream &out = get_output();
00061 
00062   // Autodesk says we don't need the header, but some DXF-reading
00063   // programs might get confused if it's missing.  We'll write an
00064   // empty header.
00065   out << "0\nSECTION\n"
00066       << "2\nHEADER\n"
00067       << "0\nENDSEC\n";
00068 
00069   write_tables(out);
00070   write_entities(out);
00071   out << "0\nEOF\n";   // Mark end of file.
00072 
00073   if (!out) {
00074     nout << "An error occurred while writing.\n";
00075     exit(1);
00076   }
00077 }
00078 
00079 ////////////////////////////////////////////////////////////////////
00080 //     Function: get_layers
00081 //       Access: Private
00082 //  Description: Traverses the hierarchy, looking for groups that
00083 //               contain polygons.  Any such groups are deemed to be
00084 //               layers, and are added to the layers set.
00085 ////////////////////////////////////////////////////////////////////
00086 void EggToDXF::
00087 get_layers(EggGroupNode *group) {
00088   bool has_polys = false;
00089 
00090   EggToDXFLayer layer(this, group);
00091 
00092   EggGroupNode::iterator ci;
00093   for (ci = group->begin(); ci != group->end(); ++ci) {
00094     EggNode *child = (*ci);
00095     if (child->is_of_type(EggPolygon::get_class_type())) {
00096       EggPolygon *poly = DCAST(EggPolygon, child);
00097       has_polys = true;
00098 
00099       layer.add_color(poly->get_color());
00100 
00101     } else if (child->is_of_type(EggGroupNode::get_class_type())) {
00102       get_layers(DCAST(EggGroupNode, child));
00103     }
00104   }
00105 
00106   if (has_polys) {
00107     layer.choose_overall_color();
00108     _layers.push_back(layer);
00109   }
00110 }
00111 
00112 
00113 ////////////////////////////////////////////////////////////////////
00114 //     Function: write_tables 
00115 //       Access: Private
00116 //  Description: Writes out the "layers", e.g. groups.  This is just
00117 //               the layers definition in the tables section at the
00118 //               beginning of the file; the actual geometry gets
00119 //               written later, in write_entities().
00120 ////////////////////////////////////////////////////////////////////
00121 void EggToDXF::
00122 write_tables(ostream &out) {
00123   out << "0\nSECTION\n"
00124       << "2\nTABLES\n"  // Begin TABLES section.
00125       << "0\nTABLE\n"
00126       << "2\nLAYER\n"   // Define LAYERS.
00127       << "70\n" << _layers.size() << "\n";
00128 
00129   EggToDXFLayers::iterator li;
00130   for (li = _layers.begin(); li != _layers.end(); ++li) {
00131     (*li).write_layer(out);
00132   }
00133 
00134   out << "0\nENDTAB\n"    // End LAYERS definition.
00135       << "0\nENDSEC\n";   // End TABLES section.
00136 }
00137 
00138 ////////////////////////////////////////////////////////////////////
00139 //     Function: write_entities 
00140 //       Access: Private
00141 //  Description: Writes out the "entities", e.g. polygons, defined for
00142 //               all layers.
00143 ////////////////////////////////////////////////////////////////////
00144 void EggToDXF::
00145 write_entities(ostream &out) {
00146   out << "0\nSECTION\n"
00147       << "2\nENTITIES\n";  // Begin ENTITIES section.
00148 
00149   EggToDXFLayers::iterator li;
00150   for (li = _layers.begin(); li != _layers.end(); ++li) {
00151     (*li).write_entities(out);
00152   }
00153 
00154   out << "0\nENDSEC\n";   // End ENTITIES section.
00155 }
00156 
00157 
00158 
00159 int main(int argc, char *argv[]) {
00160   // A call to pystub() to force libpystub.so to be linked in.
00161   pystub();
00162 
00163   EggToDXF prog;
00164   prog.parse_command_line(argc, argv);
00165   prog.run();
00166   return 0;
00167 }
 All Classes Functions Variables Enumerations