Panda3D
dxfFile.h
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 dxfFile.h
10  * @author drose
11  * @date 2004-05-04
12  */
13 
14 #ifndef DXFFILE_H
15 #define DXFFILE_H
16 
17 #include "pandatoolbase.h"
18 
19 #include "dxfLayer.h"
20 #include "dxfLayerMap.h"
21 #include "dxfVertex.h"
22 
23 #include "luse.h"
24 #include "filename.h"
25 
26 
27 static const int DXF_max_line = 256;
28 static const int DXF_num_colors = 256;
29 
30 /**
31  * A generic DXF-reading class. This class can read a DXF file but doesn't
32  * actually do anything with the data; it's intended to be inherited from and
33  * the appropriate functions overridden (particularly DoneEntity()).
34  */
35 class DXFFile : public MemoryBase {
36 public:
37  DXFFile();
38  virtual ~DXFFile();
39 
40  void process(Filename filename);
41  void process(std::istream *in, bool owns_in);
42 
43  // These functions are called as the file is processed. These are the main
44  // hooks for redefining how the class should dispense its data. As each
45  // function is called, the state stored in the DXFFile class reflects the
46  // data that was most recently read.
47 
48  virtual void begin_file();
49  virtual void begin_section();
50  virtual void done_vertex();
51  virtual void done_entity();
52  virtual void end_section();
53  virtual void end_file();
54  virtual void error();
55 
56  // new_layer() is called whenever the DXFFile class encounters a new Layer
57  // definition, and must allocate a DXFLayer instance. This function is
58  // provided so that user code may force allocate of a specialized DXFLayer
59  // instance instead.
60  virtual DXFLayer *new_layer(const std::string &name) {
61  return new DXFLayer(name);
62  }
63 
64  enum State {
65  ST_top,
66  ST_section,
67  ST_entity,
68  ST_verts,
69  ST_error,
70  ST_done,
71  };
72  enum Section {
73  SE_unknown,
74  SE_header,
75  SE_tables,
76  SE_blocks,
77  SE_entities,
78  SE_objects,
79  };
80  enum Entity {
81  EN_unknown,
82  EN_3dface,
83  EN_point,
84  EN_insert,
85  EN_vertex,
86  EN_polyline,
87  };
88  enum PolylineFlags {
89  PF_closed = 0x01,
90  PF_curve_fit = 0x02,
91  PF_spline_fit = 0x04,
92  PF_3d = 0x08,
93  PF_3d_mesh = 0x10,
94  PF_closed_n = 0x20,
95  PF_polyface = 0x40,
96  PF_continuous_linetype = 0x80,
97  };
98 
99  // This is a table of standard Autocad colors. DXF files can store only a
100  // limited range of colors; specifically, the 255 colors defined by Autocad.
101  struct Color {
102  double r, g, b;
103  };
104  static Color _colors[DXF_num_colors];
105 
106  // find_color() returns the index of the closest matching AutoCAD color to
107  // the indicated r, g, b.
108  static int find_color(double r, double g, double b);
109 
110  // get_color() returns the r,g,b of the current entity. It is valid at the
111  // time done_entity() is called.
112  const Color &get_color() const;
113 
114  // Some entities are defined in world coordinates, in 3-d space; other
115  // entities are inherently 2-d in nature and are defined in planar
116  // coordinates and must be converted to 3-d space. Call this function from
117  // done_entity() to convert a 2-d entity to 3-d world coordinates.
118  void ocs_2_wcs();
119 
120  // These members indicate the current state and describe properties of the
121  // current thing being processed. They are valid at done_entity(), and at
122  // other times.
123  int _flags;
124  Section _section;
125  Entity _entity;
126  LPoint3d _p, _q, _r, _s;
127  LVector3d _z;
128  int _color_index;
129  DXFLayer *_layer;
130 
131  // _verts is the list of vertices associated with the current entity. It is
132  // valid at the time done_entity() is called.
133  DXFVertices _verts;
134 
135  // This is the set of layers encountered within the DXF file.
136  DXFLayerMap _layers;
137 
138 protected:
139  State _state;
140  bool _vertices_follow;
141  LMatrix4d _ocs2wcs;
142 
143  std::istream *_in;
144  bool _owns_in;
145 
146  int _code;
147  std::string _string;
148 
149  void compute_ocs();
150 
151  bool get_group();
152  void change_state(State new_state);
153  void change_section(Section new_section);
154  void change_layer(const std::string &layer_name);
155  void change_entity(Entity new_entity);
156  void reset_entity();
157 
158  void state_top();
159  void state_section();
160  void state_entity();
161  void state_verts();
162 };
163 
164 std::ostream &operator << (std::ostream &out, const DXFFile::State &state);
165 std::ostream &operator << (std::ostream &out, const DXFFile::Section &section);
166 std::ostream &operator << (std::ostream &out, const DXFFile::Entity &entity);
167 
168 #endif
DXFFile::done_entity
virtual void done_entity()
This is the primary hook for user code.
Definition: dxfFile.cxx:406
DXFLayerMap
A map of string (layer name) to DXFLayer: that is, the layers of a file ordered by name.
Definition: dxfLayerMap.h:28
pvector< DXFVertex >
DXFFile::error
virtual void error()
A hook for user code, if desired.
Definition: dxfFile.cxx:433
dxfLayer.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
dxfVertex.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
dxfLayerMap.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
DXFFile::ocs_2_wcs
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:482
filename.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
DXFFile::done_vertex
virtual void done_vertex()
A hook for user code, if desired.
Definition: dxfFile.cxx:391
DXFFile::end_section
virtual void end_section()
A hook for user code, if desired.
Definition: dxfFile.cxx:415
DXFFile::find_color
static int find_color(double r, double g, double b)
Returns the index of the closest matching AutoCAD color to the indicated r, g, b.
Definition: dxfFile.cxx:444
DXFFile::end_file
virtual void end_file()
A hook for user code, if desired.
Definition: dxfFile.cxx:424
MemoryBase
This class is intended to be the base class of all objects in Panda that might be allocated and delet...
Definition: memoryBase.h:65
DXFFile
A generic DXF-reading class.
Definition: dxfFile.h:35
DXFFile::begin_section
virtual void begin_section()
A hook for user code, if desired.
Definition: dxfFile.cxx:380
luse.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
DXFLayer
This represents a "layer" as read from the DXF file.
Definition: dxfLayer.h:28
DXFFile::begin_file
virtual void begin_file()
A hook for user code, if desired.
Definition: dxfFile.cxx:371
DXFFile::Color
Definition: dxfFile.h:101
pandatoolbase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
DXFFile::get_color
const Color & get_color() const
This is a convenience function to return the r,g,b color of the current entity (at the time of done_e...
Definition: dxfFile.cxx:467
DXFFile::process
void process(Filename filename)
Opens the indicated filename and reads it as a DXF file.
Definition: dxfFile.cxx:310
Filename
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39