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