1 : /******************************************************************************
2 : * $Id: ogr_dxf.h 18409 2009-12-29 01:10:00Z warmerdam $
3 : *
4 : * Project: DXF Translator
5 : * Purpose: Definition of classes for OGR .dxf driver.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2009, Frank Warmerdam
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #ifndef _OGR_DXF_H_INCLUDED
31 : #define _OGR_DXF_H_INCLUDED
32 :
33 : #include "ogrsf_frmts.h"
34 : #include "cpl_conv.h"
35 : #include <vector>
36 : #include <map>
37 : #include <stack>
38 :
39 : class OGRDXFDataSource;
40 :
41 : /************************************************************************/
42 : /* DXFBlockDefinition */
43 : /* */
44 : /* Container for info about a block. */
45 : /************************************************************************/
46 :
47 : class DXFBlockDefinition
48 0 : {
49 : public:
50 0 : DXFBlockDefinition() : poGeometry(NULL) {}
51 : ~DXFBlockDefinition();
52 :
53 : OGRGeometry *poGeometry;
54 : std::vector<OGRFeature *> apoFeatures;
55 : };
56 :
57 : /************************************************************************/
58 : /* OGRDXFLayer */
59 : /************************************************************************/
60 : class OGRDXFDataSource;
61 :
62 : class OGRDXFLayer : public OGRLayer
63 : {
64 : OGRDXFDataSource *poDS;
65 :
66 : OGRFeatureDefn *poFeatureDefn;
67 : int iNextFID;
68 :
69 : std::stack<OGRFeature*> apoPendingFeatures;
70 : void ClearPendingFeatures();
71 :
72 : std::map<CPLString,CPLString> oStyleProperties;
73 :
74 : void TranslateGenericProperty( OGRFeature *poFeature,
75 : int nCode, char *pszValue );
76 : void PrepareLineStyle( OGRFeature *poFeature );
77 :
78 : OGRFeature * TranslatePOINT();
79 : OGRFeature * TranslateLINE();
80 : OGRFeature * TranslatePOLYLINE();
81 : OGRFeature * TranslateLWPOLYLINE();
82 : OGRFeature * TranslateCIRCLE();
83 : OGRFeature * TranslateELLIPSE();
84 : OGRFeature * TranslateARC();
85 : OGRFeature * TranslateSPLINE();
86 : OGRFeature * TranslateINSERT();
87 : OGRFeature * TranslateMTEXT();
88 : OGRFeature * TranslateTEXT();
89 : OGRFeature * TranslateDIMENSION();
90 :
91 : void FormatDimension( CPLString &osText, double dfValue );
92 :
93 : public:
94 : OGRDXFLayer( OGRDXFDataSource *poDS );
95 : ~OGRDXFLayer();
96 :
97 : void ResetReading();
98 : OGRFeature * GetNextFeature();
99 :
100 2 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
101 :
102 : int TestCapability( const char * );
103 :
104 : OGRFeature * GetNextUnfilteredFeature();
105 : };
106 :
107 : /************************************************************************/
108 : /* OGRDXFDataSource */
109 : /************************************************************************/
110 :
111 : class OGRDXFDataSource : public OGRDataSource
112 : {
113 : CPLString osName;
114 : std::vector<OGRDXFLayer*> apoLayers;
115 :
116 : FILE *fp;
117 :
118 : int iEntitiesSectionOffset;
119 :
120 : int iSrcBufferOffset;
121 : int nSrcBufferBytes;
122 : int iSrcBufferFileOffset;
123 : char achSrcBuffer[1025];
124 :
125 : int nLastValueSize;
126 :
127 : std::map<CPLString,DXFBlockDefinition> oBlockMap;
128 : std::map<CPLString,CPLString> oHeaderVariables;
129 :
130 : // indexed by layer name, then by property name.
131 : std::map< CPLString, std::map<CPLString,CPLString> >
132 : oLayerTable;
133 :
134 : public:
135 : OGRDXFDataSource();
136 : ~OGRDXFDataSource();
137 :
138 : int Open( const char * pszFilename );
139 :
140 1 : const char *GetName() { return osName; }
141 :
142 1 : int GetLayerCount() { return apoLayers.size(); }
143 : OGRLayer *GetLayer( int );
144 :
145 : int TestCapability( const char * );
146 :
147 : // The following is only used by OGRDXFLayer
148 :
149 : // Implemented in ogrdxf_diskio.cpp
150 : int ReadValue( char *pszValueBuffer,
151 : int nValueBufferSize = 81 );
152 : void UnreadValue();
153 : void LoadDiskChunk();
154 : void ResetReadPointer( int iNewOffset );
155 4 : void RestartEntities()
156 4 : { ResetReadPointer(iEntitiesSectionOffset); }
157 :
158 : // Implemented in ogrdxf_blockmap.cpp
159 : void ReadBlocksSection();
160 : OGRGeometry *SimplifyBlockGeometry( OGRGeometryCollection * );
161 : DXFBlockDefinition *LookupBlock( const char *pszName );
162 :
163 : // Layer Table Handling (ogrdxf_tables.cpp)
164 : void ReadTablesSection();
165 : void ReadLayerDefinition();
166 : const char *LookupLayerProperty( const char *pszLayer,
167 : const char *pszProperty );
168 :
169 : // Header variables.
170 : void ReadHeaderSection();
171 : const char *GetVariable(const char *pszName,
172 : const char *pszDefault=NULL );
173 : };
174 :
175 : /************************************************************************/
176 : /* OGRDXFWriterLayer */
177 : /************************************************************************/
178 :
179 : class OGRDXFWriterDS;
180 :
181 : class OGRDXFWriterLayer : public OGRLayer
182 : {
183 : FILE *fp;
184 : OGRFeatureDefn *poFeatureDefn;
185 : int nNextFID;
186 :
187 : int WriteValue( int nCode, const char *pszValue );
188 : int WriteValue( int nCode, int nValue );
189 : int WriteValue( int nCode, double dfValue );
190 :
191 : OGRErr WriteCore( OGRFeature* );
192 : OGRErr WritePOINT( OGRFeature* );
193 : OGRErr WriteTEXT( OGRFeature* );
194 : OGRErr WritePOLYLINE( OGRFeature*, OGRGeometry* = NULL );
195 :
196 : int ColorStringToDXFColor( const char * );
197 :
198 : public:
199 : OGRDXFWriterLayer( FILE *fp );
200 : ~OGRDXFWriterLayer();
201 :
202 0 : void ResetReading() {}
203 0 : OGRFeature *GetNextFeature() { return NULL; }
204 :
205 0 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
206 :
207 : int TestCapability( const char * );
208 : OGRErr CreateFeature( OGRFeature *poFeature );
209 : OGRErr CreateField( OGRFieldDefn *poField,
210 : int bApproxOK = TRUE );
211 : };
212 :
213 : /************************************************************************/
214 : /* OGRDXFWriterDS */
215 : /************************************************************************/
216 :
217 : class OGRDXFWriterDS : public OGRDataSource
218 : {
219 : CPLString osName;
220 : OGRDXFWriterLayer *poLayer;
221 : FILE *fp;
222 : CPLString osTrailerFile;
223 :
224 : public:
225 : OGRDXFWriterDS();
226 : ~OGRDXFWriterDS();
227 :
228 : int Open( const char * pszFilename,
229 : char **papszOptions );
230 :
231 0 : const char *GetName() { return osName; }
232 :
233 : int GetLayerCount();
234 : OGRLayer *GetLayer( int );
235 :
236 : int TestCapability( const char * );
237 :
238 : OGRLayer *CreateLayer( const char *pszName,
239 : OGRSpatialReference *poSpatialRef = NULL,
240 : OGRwkbGeometryType eGType = wkbUnknown,
241 : char ** papszOptions = NULL );
242 :
243 : };
244 :
245 : /************************************************************************/
246 : /* OGRDXFDriver */
247 : /************************************************************************/
248 :
249 : class OGRDXFDriver : public OGRSFDriver
250 64 : {
251 : public:
252 : ~OGRDXFDriver();
253 :
254 : static const unsigned char *GetDXFColorTable();
255 :
256 : const char *GetName();
257 : OGRDataSource *Open( const char *, int );
258 : int TestCapability( const char * );
259 :
260 : OGRDataSource *CreateDataSource( const char *pszName,
261 : char ** = NULL );
262 : };
263 :
264 : #endif /* ndef _OGR_DXF_H_INCLUDED */
|