Panda3D

dxfFile.h

00001 // Filename: dxfFile.h
00002 // Created by:  drose (04May04)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef DXFFILE_H
00016 #define DXFFILE_H
00017 
00018 #include "pandatoolbase.h"
00019 
00020 #include "dxfLayer.h"
00021 #include "dxfLayerMap.h"
00022 #include "dxfVertex.h"
00023 
00024 #include "luse.h"
00025 #include "filename.h"
00026 
00027 
00028 static const int DXF_max_line = 256;
00029 static const int DXF_num_colors = 256;
00030 
00031 ////////////////////////////////////////////////////////////////////
00032 //       Class : DXFFile
00033 // Description : A generic DXF-reading class.  This class can read a
00034 //               DXF file but doesn't actually do anything with the
00035 //               data; it's intended to be inherited from and the
00036 //               appropriate functions overridden (particularly
00037 //               DoneEntity()).
00038 ////////////////////////////////////////////////////////////////////
00039 class DXFFile : public MemoryBase {
00040 public:
00041   DXFFile();
00042   virtual ~DXFFile();
00043 
00044   void process(Filename filename);
00045   void process(istream *in, bool owns_in);
00046 
00047   // These functions are called as the file is processed.  These are
00048   // the main hooks for redefining how the class should dispense its
00049   // data.  As each function is called, the state stored in the
00050   // DXFFile class reflects the data that was most recently read.
00051 
00052   virtual void begin_file();
00053   virtual void begin_section();
00054   virtual void done_vertex();
00055   virtual void done_entity();
00056   virtual void end_section();
00057   virtual void end_file();
00058   virtual void error();
00059 
00060   // new_layer() is called whenever the DXFFile class encounters a new
00061   // Layer definition, and must allocate a DXFLayer instance.  This
00062   // function is provided so that user code may force allocate of a
00063   // specialized DXFLayer instance instead.
00064   virtual DXFLayer *new_layer(const string &name) {
00065     return new DXFLayer(name);
00066   }
00067 
00068   enum State {
00069     ST_top,
00070     ST_section,
00071     ST_entity,
00072     ST_verts,
00073     ST_error,
00074     ST_done,
00075   };
00076   enum Section {
00077     SE_unknown,
00078     SE_header,
00079     SE_tables,
00080     SE_blocks,
00081     SE_entities,
00082     SE_objects,
00083   };
00084   enum Entity {
00085     EN_unknown,
00086     EN_3dface,
00087     EN_point,
00088     EN_insert,
00089     EN_vertex,
00090     EN_polyline,
00091   };
00092   enum PolylineFlags {
00093     PF_closed              = 0x01,
00094     PF_curve_fit           = 0x02,
00095     PF_spline_fit          = 0x04,
00096     PF_3d                  = 0x08,
00097     PF_3d_mesh             = 0x10,
00098     PF_closed_n            = 0x20,
00099     PF_polyface            = 0x40,
00100     PF_continuous_linetype = 0x80,
00101   };
00102 
00103   // This is a table of standard Autocad colors.  DXF files can store
00104   // only a limited range of colors; specifically, the 255 colors
00105   // defined by Autocad.
00106   struct Color {
00107     double r, g, b;
00108   };
00109   static Color _colors[DXF_num_colors];
00110 
00111   // find_color() returns the index of the closest matching AutoCAD
00112   // color to the indicated r, g, b.
00113   static int find_color(double r, double g, double b);
00114 
00115   // get_color() returns the r,g,b of the current entity.  It is valid
00116   // at the time done_entity() is called.
00117   const Color &get_color() const;
00118 
00119   // Some entities are defined in world coordinates, in 3-d space;
00120   // other entities are inherently 2-d in nature and are defined in
00121   // planar coordinates and must be converted to 3-d space.  Call this
00122   // function from done_entity() to convert a 2-d entity to 3-d world
00123   // coordinates.
00124   void ocs_2_wcs();
00125 
00126   // These members indicate the current state and describe properties
00127   // of the current thing being processed.  They are valid at
00128   // done_entity(), and at other times.
00129   int _flags;
00130   Section _section;
00131   Entity _entity;
00132   LPoint3d _p, _q, _r, _s;
00133   LVector3d _z;
00134   int _color_index;
00135   DXFLayer *_layer;
00136 
00137   // _verts is the list of vertices associated with the current
00138   // entity.  It is valid at the time done_entity() is called.
00139   DXFVertices _verts;
00140 
00141   // This is the set of layers encountered within the DXF file.
00142   DXFLayerMap _layers;
00143 
00144 protected:
00145   State _state;
00146   bool _vertices_follow;
00147   LMatrix4d _ocs2wcs;
00148 
00149   istream *_in;
00150   bool _owns_in;
00151 
00152   int _code;
00153   string _string;
00154 
00155   void compute_ocs();
00156 
00157   bool get_group();
00158   void change_state(State new_state);
00159   void change_section(Section new_section);
00160   void change_layer(const string &layer_name);
00161   void change_entity(Entity new_entity);
00162   void reset_entity();
00163 
00164   void state_top();
00165   void state_section();
00166   void state_entity();
00167   void state_verts();
00168 };
00169 
00170 ostream &operator << (ostream &out, const DXFFile::State &state);
00171 ostream &operator << (ostream &out, const DXFFile::Section &section);
00172 ostream &operator << (ostream &out, const DXFFile::Entity &entity);
00173 
00174 #endif
 All Classes Functions Variables Enumerations