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