Browse Source

add dxf2egg

David Rose 21 years ago
parent
commit
01776f36c2

+ 19 - 0
pandatool/src/dxfegg/Sources.pp

@@ -0,0 +1,19 @@
+#begin ss_lib_target
+  #define TARGET dxfegg
+  #define LOCAL_LIBS converter dxf pandatoolbase
+  #define OTHER_LIBS \
+    egg:c event:c pandaegg:m \
+    mathutil:c linmath:c putil:c express:c panda:m \
+    interrogatedb:c dconfig:c dtoolconfig:m \
+    dtoolutil:c dtoolbase:c dtool:m
+  #define UNIX_SYS_LIBS \
+    m
+
+  #define SOURCES \
+    dxfToEggConverter.cxx dxfToEggConverter.h \
+    dxfToEggLayer.cxx dxfToEggLayer.h
+
+  #define INSTALL_HEADERS \
+    dxfToEggConverter.h dxfToEggLayer.h
+
+#end ss_lib_target

+ 163 - 0
pandatool/src/dxfegg/dxfToEggConverter.cxx

@@ -0,0 +1,163 @@
+// Filename: dxfToEggConverter.cxx
+// Created by:  drose (04May04)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://etc.cmu.edu/panda3d/docs/license/ .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#include "dxfToEggConverter.h"
+#include "dxfToEggLayer.h"
+#include "eggData.h"
+
+////////////////////////////////////////////////////////////////////
+//     Function: DXFToEggConverter::Constructor
+//       Access: Public
+//  Description:
+////////////////////////////////////////////////////////////////////
+DXFToEggConverter::
+DXFToEggConverter() {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: DXFToEggConverter::Copy Constructor
+//       Access: Public
+//  Description:
+////////////////////////////////////////////////////////////////////
+DXFToEggConverter::
+DXFToEggConverter(const DXFToEggConverter &copy) :
+  SomethingToEggConverter(copy)
+{
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: DXFToEggConverter::Destructor
+//       Access: Public
+//  Description:
+////////////////////////////////////////////////////////////////////
+DXFToEggConverter::
+~DXFToEggConverter() {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: DXFToEggConverter::make_copy
+//       Access: Public, Virtual
+//  Description: Allocates and returns a new copy of the converter.
+////////////////////////////////////////////////////////////////////
+SomethingToEggConverter *DXFToEggConverter::
+make_copy() {
+  return new DXFToEggConverter(*this);
+}
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: DXFToEggConverter::get_name
+//       Access: Public, Virtual
+//  Description: Returns the English name of the file type this
+//               converter supports.
+////////////////////////////////////////////////////////////////////
+string DXFToEggConverter::
+get_name() const {
+  return "DXF";
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: DXFToEggConverter::get_extension
+//       Access: Public, Virtual
+//  Description: Returns the common extension of the file type this
+//               converter supports.
+////////////////////////////////////////////////////////////////////
+string DXFToEggConverter::
+get_extension() const {
+  return "dxf";
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: DXFToEggConverter::convert_file
+//       Access: Public, Virtual
+//  Description: Handles the reading of the input file and converting
+//               it to egg.  Returns true if successful, false
+//               otherwise.
+////////////////////////////////////////////////////////////////////
+bool DXFToEggConverter::
+convert_file(const Filename &filename) {
+  _error = false;
+
+  process(filename);
+  return !_error;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: DXFToEggConverter::new_layer
+//       Access: Protected, Virtual
+//  Description: 
+////////////////////////////////////////////////////////////////////
+DXFLayer *DXFToEggConverter::
+new_layer(const string &name) {
+  return new DXFToEggLayer(name, &get_egg_data());
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: DXFToEggConverter::done_entity
+//       Access: Protected, Virtual
+//  Description: If the entity is a polygon, creates the corresponding
+//               egg polygon.
+////////////////////////////////////////////////////////////////////
+void DXFToEggConverter::
+done_entity() {
+  if (_entity == EN_polyline) {
+    // A Polyline is either an unclosed series of connected line
+    // segments, or a closed polygon of arbitrary complexity.
+
+    if ((_flags & PF_3d) == 0) {
+      // it's a 2-d polygon; convert it to 3-d coordinates.
+      ocs_2_wcs();
+    }
+
+    if (_flags & PF_closed) {
+      // it's closed; create a polygon.
+      nassertv(_layer!=NULL);
+      ((DXFToEggLayer *)_layer)->add_polygon(this);
+    } else {
+      // It's open; create a series of line segments.
+      nassertv(_layer!=NULL);
+      ((DXFToEggLayer *)_layer)->add_line(this);
+    }
+
+  } else if (_entity == EN_3dface) {
+    // DXF can also represent a polygon as a 3DFace.  This might be
+    // either a quad or a triangle (if two of the vertices are the
+    // same).  We'll add the vertices to our list of vertices and then
+    // define the polygon.
+    _verts.clear();
+    _verts.push_back(DXFVertex(_p));
+    _verts.push_back(DXFVertex(_q));
+    _verts.push_back(DXFVertex(_r));
+    _verts.push_back(DXFVertex(_s));
+    
+    nassertv(_layer!=NULL);
+    ((DXFToEggLayer *)_layer)->add_polygon(this);
+  }
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: DXFToEggConverter::done_entity
+//       Access: Protected, Virtual
+//  Description: A hook for user code, if desired.  This function is
+//               called when some unexpected error occurs while
+//               reading the DXF file.
+////////////////////////////////////////////////////////////////////
+void DXFToEggConverter::
+error() {
+  _error = true;
+}

+ 55 - 0
pandatool/src/dxfegg/dxfToEggConverter.h

@@ -0,0 +1,55 @@
+// Filename: DXFToEggConverter.h
+// Created by:  drose (04May04)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://etc.cmu.edu/panda3d/docs/license/ .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef DXFTOEGGCONVERTER_H
+#define DXFTOEGGCONVERTER_H
+
+#include "pandatoolbase.h"
+
+#include "somethingToEggConverter.h"
+#include "dxfFile.h"
+
+////////////////////////////////////////////////////////////////////
+//       Class : DXFToEggConverter
+// Description : This class supervises the construction of an EggData
+//               structure from a DXF file.
+////////////////////////////////////////////////////////////////////
+class DXFToEggConverter : public SomethingToEggConverter, public DXFFile {
+public:
+  DXFToEggConverter();
+  DXFToEggConverter(const DXFToEggConverter &copy);
+  ~DXFToEggConverter();
+
+  virtual SomethingToEggConverter *make_copy();
+
+  virtual string get_name() const;
+  virtual string get_extension() const;
+
+  virtual bool convert_file(const Filename &filename);
+
+protected:
+  virtual DXFLayer *new_layer(const string &name);
+  virtual void done_entity();
+  virtual void error();
+
+  bool _error;
+};
+
+#endif
+
+

+ 116 - 0
pandatool/src/dxfegg/dxfToEggLayer.cxx

@@ -0,0 +1,116 @@
+// Filename: dxfToEggLayer.cxx
+// Created by:  drose (04May04)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://etc.cmu.edu/panda3d/docs/license/ .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#include "dxfToEggLayer.h"
+#include "dxfToEggConverter.h"
+
+#include "dxfFile.h"
+#include "eggGroup.h"
+#include "eggPolygon.h"
+#include "eggLine.h"
+#include "eggVertex.h"
+#include "eggVertexPool.h"
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: DXFToEggLayer::Constructor
+//       Access: Public
+//  Description:
+////////////////////////////////////////////////////////////////////
+DXFToEggLayer::
+DXFToEggLayer(const string &name, EggGroupNode *parent) : DXFLayer(name) {
+  _group = new EggGroup(name);
+  parent->add_child(_group);
+  _vpool = new EggVertexPool(name);
+  _group->add_child(_vpool);
+}
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: DXFToEggLayer::add_polygon
+//       Access: Public
+//  Description: Given that done_entity() has just been called and that
+//               the current entity represents a polygon, adds the
+//               corresponding polygon to the layer's EggGroup and
+//               vertex pool.
+////////////////////////////////////////////////////////////////////
+void DXFToEggLayer::
+add_polygon(const DXFToEggConverter *entity) {
+  EggPolygon *poly = new EggPolygon;
+  _group->add_child(poly);
+
+  const DXFFile::Color &color = entity->get_color();
+  poly->set_color(Colorf(color.r, color.g, color.b, 1.0));
+
+  // A polyline's vertices are stored in the attached vector by dxf.C.
+  // They were defined in the DXF file using a series of "VERTEX"
+  // entries.
+
+  // For a 3dface, the vertices are defined explicitly as part of the
+  // entity; but in this case, they were added to the vector before
+  // add_polygon() was called.
+
+  DXFVertices::const_iterator vi;
+  for (vi = entity->_verts.begin();
+       vi != entity->_verts.end();
+       ++vi) {
+    poly->add_vertex(add_vertex(*vi));
+  }
+
+  poly->cleanup();
+}
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: DXFToEggLayer::add_line
+//       Access: Public
+//  Description: Similar to add_polygon(), but adds a set of point
+//               lights instead.
+////////////////////////////////////////////////////////////////////
+void DXFToEggLayer::
+add_line(const DXFToEggConverter *entity) {
+  EggLine *line = new EggLine;
+  _group->add_child(line);
+
+  const DXFFile::Color &color = entity->get_color();
+  line->set_color(Colorf(color.r, color.g, color.b, 1.0));
+
+  DXFVertices::const_iterator vi;
+  for (vi = entity->_verts.begin();
+       vi != entity->_verts.end();
+       ++vi) {
+    line->add_vertex(add_vertex(*vi));
+  }
+}
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: DXFToEggLayer::add_vertex
+//       Access: Public
+//  Description: Adds a unique vertex to the layer's vertex pool and
+//               returns it.  If the vertex was already defined
+//               previously, returns the original definition.  This is
+//               designed to share the common vertices within a layer.
+////////////////////////////////////////////////////////////////////
+EggVertex *DXFToEggLayer::
+add_vertex(const DXFVertex &vert) {
+  EggVertex egg_vert;
+  egg_vert.set_pos(vert._p);
+
+  return _vpool->create_unique_vertex(egg_vert);
+}

+ 55 - 0
pandatool/src/dxfegg/dxfToEggLayer.h

@@ -0,0 +1,55 @@
+// Filename: dxfToEggLayer.h
+// Created by:  drose (04May04)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://etc.cmu.edu/panda3d/docs/license/ .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef DXFTOEGGLAYER_H
+#define DXFTOEGGLAYER_H
+
+#include "pandatoolbase.h"
+
+#include "dxfLayer.h"
+#include "eggVertexPool.h"
+#include "eggGroup.h"
+#include "pointerTo.h"
+
+class EggGroupNode;
+class EggVertex;
+class DXFVertex;
+class DXFToEggConverter;
+
+////////////////////////////////////////////////////////////////////
+// 	 Class : DXFToEggLayer
+// Description : The specialization of DXFLayer used by
+//               DXFToEggConverter.  It contains a pointer to an
+//               EggGroup and a vertex pool; these are used to build
+//               up polygons grouped by layer in the egg file as each
+//               polygon is read from the DXF file.
+////////////////////////////////////////////////////////////////////
+class DXFToEggLayer : public DXFLayer {
+public:
+  DXFToEggLayer(const string &name, EggGroupNode *parent);
+
+  void add_polygon(const DXFToEggConverter *entity);
+  void add_line(const DXFToEggConverter *entity);
+  EggVertex *add_vertex(const DXFVertex &vertex);
+  
+  PT(EggVertexPool) _vpool;
+  PT(EggGroup) _group;
+};
+
+
+#endif