Panda3D
dxfToEggConverter.cxx
1 // Filename: dxfToEggConverter.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 "dxfToEggConverter.h"
16 #include "dxfToEggLayer.h"
17 #include "eggData.h"
18 
19 ////////////////////////////////////////////////////////////////////
20 // Function: DXFToEggConverter::Constructor
21 // Access: Public
22 // Description:
23 ////////////////////////////////////////////////////////////////////
24 DXFToEggConverter::
25 DXFToEggConverter() {
26 }
27 
28 ////////////////////////////////////////////////////////////////////
29 // Function: DXFToEggConverter::Copy Constructor
30 // Access: Public
31 // Description:
32 ////////////////////////////////////////////////////////////////////
33 DXFToEggConverter::
34 DXFToEggConverter(const DXFToEggConverter &copy) :
36 {
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: DXFToEggConverter::Destructor
41 // Access: Public
42 // Description:
43 ////////////////////////////////////////////////////////////////////
44 DXFToEggConverter::
45 ~DXFToEggConverter() {
46 }
47 
48 ////////////////////////////////////////////////////////////////////
49 // Function: DXFToEggConverter::make_copy
50 // Access: Public, Virtual
51 // Description: Allocates and returns a new copy of the converter.
52 ////////////////////////////////////////////////////////////////////
55  return new DXFToEggConverter(*this);
56 }
57 
58 
59 ////////////////////////////////////////////////////////////////////
60 // Function: DXFToEggConverter::get_name
61 // Access: Public, Virtual
62 // Description: Returns the English name of the file type this
63 // converter supports.
64 ////////////////////////////////////////////////////////////////////
66 get_name() const {
67  return "DXF";
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: DXFToEggConverter::get_extension
72 // Access: Public, Virtual
73 // Description: Returns the common extension of the file type this
74 // converter supports.
75 ////////////////////////////////////////////////////////////////////
77 get_extension() const {
78  return "dxf";
79 }
80 
81 ////////////////////////////////////////////////////////////////////
82 // Function: DXFToEggConverter::supports_compressed
83 // Access: Published, Virtual
84 // Description: Returns true if this file type can transparently load
85 // compressed files (with a .pz extension), false
86 // otherwise.
87 ////////////////////////////////////////////////////////////////////
90  return true;
91 }
92 
93 ////////////////////////////////////////////////////////////////////
94 // Function: DXFToEggConverter::convert_file
95 // Access: Public, Virtual
96 // Description: Handles the reading of the input file and converting
97 // it to egg. Returns true if successful, false
98 // otherwise.
99 ////////////////////////////////////////////////////////////////////
101 convert_file(const Filename &filename) {
102  clear_error();
103 
104  if (_egg_data->get_coordinate_system() == CS_default) {
105  _egg_data->set_coordinate_system(CS_zup_right);
106  }
107 
108  process(filename);
109  return !had_error();
110 }
111 
112 ////////////////////////////////////////////////////////////////////
113 // Function: DXFToEggConverter::new_layer
114 // Access: Protected, Virtual
115 // Description:
116 ////////////////////////////////////////////////////////////////////
117 DXFLayer *DXFToEggConverter::
118 new_layer(const string &name) {
119  return new DXFToEggLayer(name, get_egg_data());
120 }
121 
122 ////////////////////////////////////////////////////////////////////
123 // Function: DXFToEggConverter::done_entity
124 // Access: Protected, Virtual
125 // Description: If the entity is a polygon, creates the corresponding
126 // egg polygon.
127 ////////////////////////////////////////////////////////////////////
128 void DXFToEggConverter::
129 done_entity() {
130  if (_entity == EN_polyline) {
131  // A Polyline is either an unclosed series of connected line
132  // segments, or a closed polygon of arbitrary complexity.
133 
134  if ((_flags & PF_3d) == 0) {
135  // it's a 2-d polygon; convert it to 3-d coordinates.
136  ocs_2_wcs();
137  }
138 
139  if (_flags & PF_closed) {
140  // it's closed; create a polygon.
141  nassertv(_layer!=NULL);
142  ((DXFToEggLayer *)_layer)->add_polygon(this);
143  } else {
144  // It's open; create a series of line segments.
145  nassertv(_layer!=NULL);
146  ((DXFToEggLayer *)_layer)->add_line(this);
147  }
148 
149  } else if (_entity == EN_3dface) {
150  // DXF can also represent a polygon as a 3DFace. This might be
151  // either a quad or a triangle (if two of the vertices are the
152  // same). We'll add the vertices to our list of vertices and then
153  // define the polygon.
154  _verts.clear();
155  _verts.push_back(DXFVertex(_s));
156  _verts.push_back(DXFVertex(_r));
157  _verts.push_back(DXFVertex(_q));
158  _verts.push_back(DXFVertex(_p));
159 
160  nassertv(_layer!=NULL);
161  ((DXFToEggLayer *)_layer)->add_polygon(this);
162  }
163 }
164 
165 ////////////////////////////////////////////////////////////////////
166 // Function: DXFToEggConverter::done_entity
167 // Access: Protected, Virtual
168 // Description: A hook for user code, if desired. This function is
169 // called when some unexpected error occurs while
170 // reading the DXF file.
171 ////////////////////////////////////////////////////////////////////
172 void DXFToEggConverter::
173 error() {
174  _error = true;
175 }
This represents a "layer" as read from the DXF file.
Definition: dxfLayer.h:31
virtual string get_name() const
Returns the English name of the file type this converter supports.
bool had_error() const
Returns true if an error was detected during the conversion process (unless _allow_errors is true)...
EggData * get_egg_data()
Returns the EggData structure.
virtual bool supports_compressed() const
Returns true if this file type can transparently load compressed files (with a .pz extension)...
The specialization of DXFLayer used by DXFToEggConverter.
Definition: dxfToEggLayer.h:38
void clear_error()
Resets the error flag to the no-error state.
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
virtual string get_extension() const
Returns the common extension of the file type this converter supports.
virtual bool convert_file(const Filename &filename)
Handles the reading of the input file and converting it to egg.
virtual SomethingToEggConverter * make_copy()
Allocates and returns a new copy of the converter.
Stored within DXFFile, this is the basic Vertex data of a DXF file.
Definition: dxfVertex.h:30
void ocs_2_wcs()
Assuming the current entity is a planar-based entity, for instance, a 2-d polygon (as opposed to a 3-...
Definition: dxfFile.cxx:518
void process(Filename filename)
Opens the indicated filename and reads it as a DXF file.
Definition: dxfFile.cxx:314
This is a base class for a family of converter classes that manage a conversion from some file type t...
This class supervises the construction of an EggData structure from a DXF file.