| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #ifndef DXF_ENTITY |
|---|
| 15 | #define DXF_ENTITY 1 |
|---|
| 16 | |
|---|
| 17 | #include <vector> |
|---|
| 18 | #include <string> |
|---|
| 19 | #include <iostream> |
|---|
| 20 | |
|---|
| 21 | #include <osg/Referenced> |
|---|
| 22 | #include <osg/ref_ptr> |
|---|
| 23 | #include <osg/Array> |
|---|
| 24 | #include <osg/Vec3d> |
|---|
| 25 | #include <osg/Node> |
|---|
| 26 | #include <osg/Matrixd> |
|---|
| 27 | #include <osgText/Text> |
|---|
| 28 | |
|---|
| 29 | #include "dxfBlock.h" |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | class scene; |
|---|
| 33 | class codeValue; |
|---|
| 34 | class dxfFile; |
|---|
| 35 | |
|---|
| 36 | static inline void |
|---|
| 37 | getOCSMatrix(const osg::Vec3d& ocs, osg::Matrixd& m) |
|---|
| 38 | { |
|---|
| 39 | static const double one_64th = 1.0/64.0; |
|---|
| 40 | m.makeIdentity(); |
|---|
| 41 | if (ocs == osg::Vec3d(0,0,1)) return; |
|---|
| 42 | osg::Vec3d ax(1,0,0), ay(0,1,0), az(0,0,1); |
|---|
| 43 | osg::Vec3d ocsaxis(ocs); |
|---|
| 44 | ocsaxis.normalize(); |
|---|
| 45 | if (fabs(ocsaxis.x()) < one_64th && fabs(ocsaxis.y()) < one_64th) { |
|---|
| 46 | ax = ay ^ ocsaxis; |
|---|
| 47 | } else { |
|---|
| 48 | ax = az ^ ocsaxis; |
|---|
| 49 | } |
|---|
| 50 | ax.normalize(); |
|---|
| 51 | ay = ocsaxis ^ ax; |
|---|
| 52 | ay.normalize(); |
|---|
| 53 | m = osg::Matrixd( ax.x(), ax.y(), ax.z(), 0, |
|---|
| 54 | ay.x(), ay.y(), ay.z(), 0, |
|---|
| 55 | ocsaxis.x(), ocsaxis.y(), ocsaxis.z(), 0, |
|---|
| 56 | 0,0,0,1); |
|---|
| 57 | |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | class dxfBasicEntity : public osg::Referenced |
|---|
| 61 | { |
|---|
| 62 | public: |
|---|
| 63 | dxfBasicEntity() : _color(0), _useAccuracy(false), _maxError(0.01), _improveAccuracyOnly(false) {} |
|---|
| 64 | virtual ~dxfBasicEntity() {} |
|---|
| 65 | virtual dxfBasicEntity* create() = 0; |
|---|
| 66 | virtual const char* name() = 0; |
|---|
| 67 | virtual void assign(dxfFile* dxf, codeValue& cv); |
|---|
| 68 | virtual void drawScene(scene*) {} |
|---|
| 69 | const std::string getLayer() const { return _layer; } |
|---|
| 70 | |
|---|
| 71 | void setAccuracy(bool useAccuracy,double maxError,bool improveAccuracyOnly) { |
|---|
| 72 | _useAccuracy=useAccuracy; |
|---|
| 73 | _maxError=maxError; |
|---|
| 74 | _improveAccuracyOnly=improveAccuracyOnly; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | protected: |
|---|
| 79 | std::string _layer; |
|---|
| 80 | unsigned short _color; |
|---|
| 81 | |
|---|
| 82 | bool _useAccuracy; |
|---|
| 83 | double _maxError; |
|---|
| 84 | bool _improveAccuracyOnly; |
|---|
| 85 | |
|---|
| 86 | }; |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | class dxfCircle : public dxfBasicEntity |
|---|
| 90 | { |
|---|
| 91 | public: |
|---|
| 92 | dxfCircle() : _radius(0), _ocs(0,0,1) {} |
|---|
| 93 | virtual ~dxfCircle() {} |
|---|
| 94 | virtual dxfBasicEntity* create() { |
|---|
| 95 | dxfBasicEntity* circle=new dxfCircle; |
|---|
| 96 | circle->setAccuracy(_useAccuracy,_maxError,_improveAccuracyOnly); |
|---|
| 97 | return circle; |
|---|
| 98 | } |
|---|
| 99 | virtual const char* name() { return "CIRCLE"; } |
|---|
| 100 | virtual void assign(dxfFile* dxf, codeValue& cv); |
|---|
| 101 | virtual void drawScene(scene* sc); |
|---|
| 102 | protected: |
|---|
| 103 | osg::Vec3d _center; |
|---|
| 104 | double _radius; |
|---|
| 105 | osg::Vec3d _ocs; |
|---|
| 106 | }; |
|---|
| 107 | |
|---|
| 108 | class dxfArc : public dxfBasicEntity |
|---|
| 109 | { |
|---|
| 110 | public: |
|---|
| 111 | dxfArc() : _radius(0), _startAngle(0), _endAngle(360), _ocs(0,0,1) {} |
|---|
| 112 | virtual ~dxfArc() {} |
|---|
| 113 | virtual dxfBasicEntity* create() { |
|---|
| 114 | dxfBasicEntity* arc=new dxfArc; |
|---|
| 115 | arc->setAccuracy(_useAccuracy,_maxError,_improveAccuracyOnly); |
|---|
| 116 | |
|---|
| 117 | return arc; |
|---|
| 118 | } |
|---|
| 119 | virtual const char* name() { return "ARC"; } |
|---|
| 120 | virtual void assign(dxfFile* dxf, codeValue& cv); |
|---|
| 121 | virtual void drawScene(scene* sc); |
|---|
| 122 | protected: |
|---|
| 123 | osg::Vec3d _center; |
|---|
| 124 | double _radius; |
|---|
| 125 | double _startAngle; |
|---|
| 126 | double _endAngle; |
|---|
| 127 | osg::Vec3d _ocs; |
|---|
| 128 | }; |
|---|
| 129 | |
|---|
| 130 | class dxfPoint : public dxfBasicEntity |
|---|
| 131 | { |
|---|
| 132 | public: |
|---|
| 133 | dxfPoint() : _ocs(0,0,1) {} |
|---|
| 134 | virtual ~dxfPoint() {} |
|---|
| 135 | virtual dxfBasicEntity* create() { return new dxfPoint; } |
|---|
| 136 | virtual const char* name() { return "POINT"; } |
|---|
| 137 | virtual void assign(dxfFile* dxf, codeValue& cv); |
|---|
| 138 | virtual void drawScene(scene* sc); |
|---|
| 139 | protected: |
|---|
| 140 | osg::Vec3d _a; |
|---|
| 141 | |
|---|
| 142 | osg::Vec3d _ocs; |
|---|
| 143 | }; |
|---|
| 144 | |
|---|
| 145 | class dxfLine : public dxfBasicEntity |
|---|
| 146 | { |
|---|
| 147 | public: |
|---|
| 148 | dxfLine() : _ocs(0,0,1) {} |
|---|
| 149 | virtual ~dxfLine() {} |
|---|
| 150 | virtual dxfBasicEntity* create() { return new dxfLine; } |
|---|
| 151 | virtual const char* name() { return "LINE"; } |
|---|
| 152 | virtual void assign(dxfFile* dxf, codeValue& cv); |
|---|
| 153 | virtual void drawScene(scene* sc); |
|---|
| 154 | protected: |
|---|
| 155 | osg::Vec3d _a; |
|---|
| 156 | osg::Vec3d _b; |
|---|
| 157 | osg::Vec3d _ocs; |
|---|
| 158 | }; |
|---|
| 159 | |
|---|
| 160 | class dxf3DFace : public dxfBasicEntity |
|---|
| 161 | { |
|---|
| 162 | public: |
|---|
| 163 | dxf3DFace() |
|---|
| 164 | { |
|---|
| 165 | _vertices[0] = osg::Vec3d(0,0,0); |
|---|
| 166 | _vertices[1] = osg::Vec3d(0,0,0); |
|---|
| 167 | _vertices[2] = osg::Vec3d(0,0,0); |
|---|
| 168 | _vertices[3] = osg::Vec3d(0,0,0); |
|---|
| 169 | } |
|---|
| 170 | virtual ~dxf3DFace() {} |
|---|
| 171 | virtual dxfBasicEntity* create() { return new dxf3DFace; } |
|---|
| 172 | virtual const char* name() { return "3DFACE"; } |
|---|
| 173 | virtual void assign(dxfFile* dxf, codeValue& cv); |
|---|
| 174 | virtual void drawScene(scene* sc); |
|---|
| 175 | protected: |
|---|
| 176 | osg::Vec3d _vertices[4]; |
|---|
| 177 | }; |
|---|
| 178 | |
|---|
| 179 | class dxfVertex : public dxfBasicEntity |
|---|
| 180 | { |
|---|
| 181 | public: |
|---|
| 182 | dxfVertex() : _vertex(osg::Vec3d(0,0,0)), _indice1(0), _indice2(0), _indice3(0), _indice4(0) {} |
|---|
| 183 | virtual ~dxfVertex() {} |
|---|
| 184 | virtual dxfBasicEntity* create() { return new dxfVertex; } |
|---|
| 185 | virtual const char* name() { return "VERTEX"; } |
|---|
| 186 | virtual void assign(dxfFile* dxf, codeValue& cv); |
|---|
| 187 | void getVertex(double &x, double &y, double &z) { x=_vertex.x();y=_vertex.y();z=_vertex.z(); } |
|---|
| 188 | const osg::Vec3d& getVertex() const { return _vertex; } |
|---|
| 189 | const unsigned int getIndice1() const { return _indice1; } |
|---|
| 190 | const unsigned int getIndice2() const { return _indice2; } |
|---|
| 191 | const unsigned int getIndice3() const { return _indice3; } |
|---|
| 192 | const unsigned int getIndice4() const { return _indice4; } |
|---|
| 193 | |
|---|
| 194 | protected: |
|---|
| 195 | osg::Vec3d _vertex; |
|---|
| 196 | unsigned int _indice1, _indice2, _indice3, _indice4; |
|---|
| 197 | }; |
|---|
| 198 | |
|---|
| 199 | class dxfPolyline : public dxfBasicEntity |
|---|
| 200 | { |
|---|
| 201 | public: |
|---|
| 202 | dxfPolyline() : _currentVertex(NULL), |
|---|
| 203 | _elevation(0.0), |
|---|
| 204 | _flag(0), |
|---|
| 205 | _mcount(0), |
|---|
| 206 | _ncount(0), |
|---|
| 207 | _nstart(0), |
|---|
| 208 | _nend(0), |
|---|
| 209 | _ocs(osg::Vec3d(0,0,1)), |
|---|
| 210 | _mdensity(0), |
|---|
| 211 | _ndensity(0), |
|---|
| 212 | _surfacetype(0) |
|---|
| 213 | {} |
|---|
| 214 | virtual ~dxfPolyline() {} |
|---|
| 215 | virtual dxfBasicEntity* create() { return new dxfPolyline; } |
|---|
| 216 | virtual const char* name() { return "POLYLINE"; } |
|---|
| 217 | virtual void assign(dxfFile* dxf, codeValue& cv); |
|---|
| 218 | virtual int vertexCount() { return _vertices.size(); } |
|---|
| 219 | virtual void drawScene(scene* sc); |
|---|
| 220 | |
|---|
| 221 | protected: |
|---|
| 222 | dxfVertex* _currentVertex; |
|---|
| 223 | std::vector<osg::ref_ptr<dxfVertex> > _vertices; |
|---|
| 224 | std::vector<osg::ref_ptr<dxfVertex> > _indices; |
|---|
| 225 | double _elevation; |
|---|
| 226 | unsigned short _flag; |
|---|
| 227 | unsigned int _mcount; |
|---|
| 228 | unsigned int _ncount; |
|---|
| 229 | unsigned short _nstart; |
|---|
| 230 | unsigned short _nend; |
|---|
| 231 | osg::Vec3d _ocs; |
|---|
| 232 | unsigned short _mdensity; |
|---|
| 233 | unsigned short _ndensity; |
|---|
| 234 | unsigned short _surfacetype; |
|---|
| 235 | |
|---|
| 236 | }; |
|---|
| 237 | |
|---|
| 238 | class dxfLWPolyline : public dxfBasicEntity |
|---|
| 239 | { |
|---|
| 240 | public: |
|---|
| 241 | dxfLWPolyline() : |
|---|
| 242 | _elevation(0.0), |
|---|
| 243 | _flag(0), |
|---|
| 244 | _vcount(0), |
|---|
| 245 | _ocs(osg::Vec3d(0,0,1)), |
|---|
| 246 | _lastv(0,0,0) |
|---|
| 247 | {} |
|---|
| 248 | virtual ~dxfLWPolyline() {} |
|---|
| 249 | virtual dxfBasicEntity* create() { return new dxfLWPolyline; } |
|---|
| 250 | virtual const char* name() { return "LWPOLYLINE"; } |
|---|
| 251 | virtual void assign(dxfFile* dxf, codeValue& cv); |
|---|
| 252 | virtual int vertexCount() { return _vertices.size(); } |
|---|
| 253 | virtual void drawScene(scene* sc); |
|---|
| 254 | |
|---|
| 255 | protected: |
|---|
| 256 | double _elevation; |
|---|
| 257 | unsigned short _flag; |
|---|
| 258 | unsigned short _vcount; |
|---|
| 259 | osg::Vec3d _ocs; |
|---|
| 260 | osg::Vec3d _lastv; |
|---|
| 261 | std::vector< osg::Vec3d > _vertices; |
|---|
| 262 | |
|---|
| 263 | }; |
|---|
| 264 | |
|---|
| 265 | class dxfInsert : public dxfBasicEntity |
|---|
| 266 | { |
|---|
| 267 | public: |
|---|
| 268 | dxfInsert() : _block(NULL), |
|---|
| 269 | _done(false), |
|---|
| 270 | _rotation(0), |
|---|
| 271 | _scale(1,1,1), |
|---|
| 272 | _point(osg::Vec3d(0,0,0)), |
|---|
| 273 | _ocs(osg::Vec3d(0,0,1)) {} |
|---|
| 274 | virtual ~dxfInsert() {} |
|---|
| 275 | virtual dxfBasicEntity* create() { return new dxfInsert; } |
|---|
| 276 | virtual const char* name() { return "INSERT"; } |
|---|
| 277 | virtual void assign(dxfFile* dxf, codeValue& cv); |
|---|
| 278 | virtual void drawScene(scene* sc); |
|---|
| 279 | |
|---|
| 280 | protected: |
|---|
| 281 | std::string _blockName; |
|---|
| 282 | osg::ref_ptr<dxfBlock> _block; |
|---|
| 283 | bool _done; |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | double _rotation; |
|---|
| 287 | osg::Vec3d _scale; |
|---|
| 288 | osg::Vec3d _point; |
|---|
| 289 | osg::Vec3d _ocs; |
|---|
| 290 | }; |
|---|
| 291 | |
|---|
| 292 | class dxfText : public dxfBasicEntity |
|---|
| 293 | { |
|---|
| 294 | public: |
|---|
| 295 | dxfText() : |
|---|
| 296 | _string(""), |
|---|
| 297 | _point1(0,0,0), |
|---|
| 298 | _point2(0,0,0), |
|---|
| 299 | _ocs(0,0,1), |
|---|
| 300 | _height(1), |
|---|
| 301 | _xscale(1), |
|---|
| 302 | _rotation(0), |
|---|
| 303 | _flags(0), |
|---|
| 304 | _hjustify(0), |
|---|
| 305 | _vjustify(0) {} |
|---|
| 306 | |
|---|
| 307 | virtual ~dxfText() {} |
|---|
| 308 | virtual dxfBasicEntity* create() { return new dxfText; } |
|---|
| 309 | virtual const char* name() { return "TEXT"; } |
|---|
| 310 | virtual void assign(dxfFile* dxf, codeValue& cv); |
|---|
| 311 | virtual void drawScene(scene* sc); |
|---|
| 312 | |
|---|
| 313 | protected: |
|---|
| 314 | std::string _string; |
|---|
| 315 | osg::Vec3d _point1; |
|---|
| 316 | osg::Vec3d _point2; |
|---|
| 317 | osg::Vec3d _ocs; |
|---|
| 318 | double _height; |
|---|
| 319 | double _xscale; |
|---|
| 320 | double _rotation; |
|---|
| 321 | int _flags; |
|---|
| 322 | int _hjustify; |
|---|
| 323 | int _vjustify; |
|---|
| 324 | }; |
|---|
| 325 | |
|---|
| 326 | class dxfEntity : public osg::Referenced |
|---|
| 327 | { |
|---|
| 328 | public: |
|---|
| 329 | dxfEntity(std::string s) : _entity(NULL), _seqend(false) |
|---|
| 330 | { |
|---|
| 331 | _entity = findByName(s); |
|---|
| 332 | if (_entity) { |
|---|
| 333 | _entityList.push_back(_entity); |
|---|
| 334 | |
|---|
| 335 | } |
|---|
| 336 | } |
|---|
| 337 | virtual void assign(dxfFile* dxf, codeValue& cv); |
|---|
| 338 | virtual bool done() { return !_seqend; } |
|---|
| 339 | static void registerEntity(dxfBasicEntity*); |
|---|
| 340 | static void unregisterEntity(dxfBasicEntity*); |
|---|
| 341 | static dxfBasicEntity* findByName(std::string s) |
|---|
| 342 | { |
|---|
| 343 | dxfBasicEntity* be = _registry[s].get(); |
|---|
| 344 | if (be) |
|---|
| 345 | return be->create(); |
|---|
| 346 | else { |
|---|
| 347 | std::cout << " no " << s << std::endl; |
|---|
| 348 | return NULL; |
|---|
| 349 | } |
|---|
| 350 | } |
|---|
| 351 | virtual void drawScene(scene* sc); |
|---|
| 352 | dxfBasicEntity* getEntity() { return _entity; } |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | static dxfBasicEntity* getRegistryEntity(std::string s) { |
|---|
| 356 | return _registry[s].get(); |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | protected: |
|---|
| 360 | std::vector<osg::ref_ptr<dxfBasicEntity> > _entityList; |
|---|
| 361 | static std::map<std::string, osg::ref_ptr<dxfBasicEntity> > _registry; |
|---|
| 362 | dxfBasicEntity* _entity; |
|---|
| 363 | bool _seqend; |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | }; |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | template<class T> |
|---|
| 371 | class RegisterEntityProxy |
|---|
| 372 | { |
|---|
| 373 | public: |
|---|
| 374 | RegisterEntityProxy() |
|---|
| 375 | { |
|---|
| 376 | _rw = new T; |
|---|
| 377 | dxfEntity::registerEntity(_rw.get()); |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | ~RegisterEntityProxy() |
|---|
| 381 | { |
|---|
| 382 | dxfEntity::unregisterEntity(_rw.get()); |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | T* get() { return _rw.get(); } |
|---|
| 386 | |
|---|
| 387 | protected: |
|---|
| 388 | osg::ref_ptr<T> _rw; |
|---|
| 389 | }; |
|---|
| 390 | |
|---|
| 391 | #endif |
|---|