Panda3D
eggToDXF.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file eggToDXF.cxx
10  * @author drose
11  * @date 2004-05-04
12  */
13 
14 #include "eggToDXF.h"
15 #include "eggPolygon.h"
16 #include "dcast.h"
17 
18 /**
19  *
20  */
21 EggToDXF::
22 EggToDXF() :
23  EggToSomething("DXF", ".dxf", true, false)
24 {
25  set_binary_output(true);
26  set_program_brief("convert .egg files to AutoCAD .dxf files");
27  set_program_description
28  ("This program converts files from egg format to AutoCAD DXF format. "
29  "Since DXF does not support nested hierarchies, vertex normals, or any "
30  "fancy stuff you are probably used to, there is some information lost "
31  "in the conversion");
32 
33  add_option
34  ("p", "", 0,
35  "Use POLYLINE to represent polygons instead of the default, 3DFACE.",
36  &EggToDXF::dispatch_none, &_use_polyline);
37 
38  _coordinate_system = CS_zup_right;
39  _got_coordinate_system = true;
40 }
41 
42 /**
43  *
44  */
45 void EggToDXF::
46 run() {
47  get_layers(_data);
48  if (_layers.empty()) {
49  nout << "Egg file contains no polygons. Output file not written.\n";
50  exit(1);
51  }
52 
53  // uniquify_names("layer", _layers.begin(), _layers.end());
54 
55  std::ostream &out = get_output();
56 
57  // Autodesk says we don't need the header, but some DXF-reading programs
58  // might get confused if it's missing. We'll write an empty header.
59  out << "0\nSECTION\n"
60  << "2\nHEADER\n"
61  << "0\nENDSEC\n";
62 
63  write_tables(out);
64  write_entities(out);
65  out << "0\nEOF\n"; // Mark end of file.
66 
67  if (!out) {
68  nout << "An error occurred while writing.\n";
69  exit(1);
70  }
71 }
72 
73 /**
74  * Traverses the hierarchy, looking for groups that contain polygons. Any
75  * such groups are deemed to be layers, and are added to the layers set.
76  */
77 void EggToDXF::
78 get_layers(EggGroupNode *group) {
79  bool has_polys = false;
80 
81  EggToDXFLayer layer(this, group);
82 
83  EggGroupNode::iterator ci;
84  for (ci = group->begin(); ci != group->end(); ++ci) {
85  EggNode *child = (*ci);
86  if (child->is_of_type(EggPolygon::get_class_type())) {
87  EggPolygon *poly = DCAST(EggPolygon, child);
88  has_polys = true;
89 
90  layer.add_color(poly->get_color());
91 
92  } else if (child->is_of_type(EggGroupNode::get_class_type())) {
93  get_layers(DCAST(EggGroupNode, child));
94  }
95  }
96 
97  if (has_polys) {
98  layer.choose_overall_color();
99  _layers.push_back(layer);
100  }
101 }
102 
103 
104 /**
105  * Writes out the "layers", e.g. groups. This is just the layers definition
106  * in the tables section at the beginning of the file; the actual geometry
107  * gets written later, in write_entities().
108  */
109 void EggToDXF::
110 write_tables(std::ostream &out) {
111  out << "0\nSECTION\n"
112  << "2\nTABLES\n" // Begin TABLES section.
113  << "0\nTABLE\n"
114  << "2\nLAYER\n" // Define LAYERS.
115  << "70\n" << _layers.size() << "\n";
116 
117  EggToDXFLayers::iterator li;
118  for (li = _layers.begin(); li != _layers.end(); ++li) {
119  (*li).write_layer(out);
120  }
121 
122  out << "0\nENDTAB\n" // End LAYERS definition.
123  << "0\nENDSEC\n"; // End TABLES section.
124 }
125 
126 /**
127  * Writes out the "entities", e.g. polygons, defined for all layers.
128  */
129 void EggToDXF::
130 write_entities(std::ostream &out) {
131  out << "0\nSECTION\n"
132  << "2\nENTITIES\n"; // Begin ENTITIES section.
133 
134  EggToDXFLayers::iterator li;
135  for (li = _layers.begin(); li != _layers.end(); ++li) {
136  (*li).write_entities(out);
137  }
138 
139  out << "0\nENDSEC\n"; // End ENTITIES section.
140 }
141 
142 
143 
144 int main(int argc, char *argv[]) {
145  EggToDXF prog;
146  prog.parse_command_line(argc, argv);
147  prog.run();
148  return 0;
149 }
virtual void parse_command_line(int argc, char **argv)
Dispatches on each of the options on the command line, and passes the remaining parameters to handle_...
A base class for nodes in the hierarchy that are not leaf nodes.
Definition: eggGroupNode.h:46
LColor get_color() const
Returns the color set on this particular attribute.
Definition: eggAttributes.I:91
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A single polygon.
Definition: eggPolygon.h:24
This is the general base class for a file-converter program that reads some model file format and gen...
A base class for things that may be directly added into the egg hierarchy.
Definition: eggNode.h:35
A program to read an egg file and write a DXF file.
Definition: eggToDXF.h:27
bool is_of_type(TypeHandle handle) const
Returns true if the current object is or derives from the indicated type.
Definition: typedObject.I:28
std::ostream & get_output()
Returns an output stream that corresponds to the user's intended egg file output–either stdout,...
A single layer in the DXF file to be written by EggToDXF.
Definition: eggToDXFLayer.h:29