1 : /******************************************************************************
2 : * $Id: ogr_dxf.h 22527 2011-06-13 03:58:34Z 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 "ogr_autocad_services.h"
35 : #include "cpl_conv.h"
36 : #include <vector>
37 : #include <map>
38 : #include <set>
39 : #include <queue>
40 :
41 : class OGRDXFDataSource;
42 :
43 : /************************************************************************/
44 : /* DXFBlockDefinition */
45 : /* */
46 : /* Container for info about a block. */
47 : /************************************************************************/
48 :
49 : class DXFBlockDefinition
50 40 : {
51 : public:
52 20 : DXFBlockDefinition() : poGeometry(NULL) {}
53 : ~DXFBlockDefinition();
54 :
55 : OGRGeometry *poGeometry;
56 : std::vector<OGRFeature *> apoFeatures;
57 : };
58 :
59 : /************************************************************************/
60 : /* OGRDXFBlocksLayer() */
61 : /************************************************************************/
62 :
63 : class OGRDXFBlocksLayer : public OGRLayer
64 : {
65 : OGRDXFDataSource *poDS;
66 :
67 : OGRFeatureDefn *poFeatureDefn;
68 :
69 : int iNextFID;
70 : unsigned int iNextSubFeature;
71 :
72 : std::map<CPLString,DXFBlockDefinition>::iterator oIt;
73 :
74 : public:
75 : OGRDXFBlocksLayer( OGRDXFDataSource *poDS );
76 : ~OGRDXFBlocksLayer();
77 :
78 : void ResetReading();
79 : OGRFeature * GetNextFeature();
80 :
81 6 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
82 :
83 : int TestCapability( const char * );
84 :
85 : OGRFeature * GetNextUnfilteredFeature();
86 : };
87 :
88 : /************************************************************************/
89 : /* OGRDXFLayer */
90 : /************************************************************************/
91 : class OGRDXFLayer : public OGRLayer
92 : {
93 : OGRDXFDataSource *poDS;
94 :
95 : OGRFeatureDefn *poFeatureDefn;
96 : int iNextFID;
97 :
98 : std::set<CPLString> oIgnoredEntities;
99 :
100 : std::queue<OGRFeature*> apoPendingFeatures;
101 : void ClearPendingFeatures();
102 :
103 : std::map<CPLString,CPLString> oStyleProperties;
104 :
105 : void TranslateGenericProperty( OGRFeature *poFeature,
106 : int nCode, char *pszValue );
107 : void PrepareLineStyle( OGRFeature *poFeature );
108 : void ApplyOCSTransformer( OGRGeometry * );
109 :
110 : OGRFeature * TranslatePOINT();
111 : OGRFeature * TranslateLINE();
112 : OGRFeature * TranslatePOLYLINE();
113 : OGRFeature * TranslateLWPOLYLINE();
114 : OGRFeature * TranslateCIRCLE();
115 : OGRFeature * TranslateELLIPSE();
116 : OGRFeature * TranslateARC();
117 : OGRFeature * TranslateSPLINE();
118 : OGRFeature * TranslateINSERT();
119 : OGRFeature * TranslateMTEXT();
120 : OGRFeature * TranslateTEXT();
121 : OGRFeature * TranslateDIMENSION();
122 : OGRFeature * TranslateHATCH();
123 :
124 : void FormatDimension( CPLString &osText, double dfValue );
125 : OGRErr CollectBoundaryPath( OGRGeometryCollection * );
126 : OGRErr CollectPolylinePath( OGRGeometryCollection * );
127 :
128 : CPLString TextUnescape( const char * );
129 :
130 : public:
131 : OGRDXFLayer( OGRDXFDataSource *poDS );
132 : ~OGRDXFLayer();
133 :
134 : void ResetReading();
135 : OGRFeature * GetNextFeature();
136 :
137 58 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
138 :
139 : int TestCapability( const char * );
140 :
141 : OGRFeature * GetNextUnfilteredFeature();
142 : };
143 :
144 : /************************************************************************/
145 : /* OGRDXFReader */
146 : /* */
147 : /* A class for very low level DXF reading without interpretation. */
148 : /************************************************************************/
149 :
150 : class OGRDXFReader
151 : {
152 : public:
153 : OGRDXFReader();
154 : ~OGRDXFReader();
155 :
156 : void Initialize( VSILFILE * fp );
157 :
158 : VSILFILE *fp;
159 :
160 : int iSrcBufferOffset;
161 : int nSrcBufferBytes;
162 : int iSrcBufferFileOffset;
163 : char achSrcBuffer[1025];
164 :
165 : int nLastValueSize;
166 :
167 : int ReadValue( char *pszValueBuffer,
168 : int nValueBufferSize = 81 );
169 : void UnreadValue();
170 : void LoadDiskChunk();
171 : void ResetReadPointer( int iNewOffset );
172 : };
173 :
174 :
175 : /************************************************************************/
176 : /* OGRDXFDataSource */
177 : /************************************************************************/
178 :
179 : class OGRDXFDataSource : public OGRDataSource
180 : {
181 : VSILFILE *fp;
182 :
183 : CPLString osName;
184 : std::vector<OGRLayer*> apoLayers;
185 :
186 : int iEntitiesSectionOffset;
187 :
188 : std::map<CPLString,DXFBlockDefinition> oBlockMap;
189 : std::map<CPLString,CPLString> oHeaderVariables;
190 :
191 : CPLString osEncoding;
192 :
193 : // indexed by layer name, then by property name.
194 : std::map< CPLString, std::map<CPLString,CPLString> >
195 : oLayerTable;
196 :
197 : std::map<CPLString,CPLString> oLineTypeTable;
198 :
199 : int bInlineBlocks;
200 :
201 : OGRDXFReader oReader;
202 :
203 : public:
204 : OGRDXFDataSource();
205 : ~OGRDXFDataSource();
206 :
207 : int Open( const char * pszFilename, int bHeaderOnly=FALSE );
208 :
209 22 : const char *GetName() { return osName; }
210 :
211 86 : int GetLayerCount() { return apoLayers.size(); }
212 : OGRLayer *GetLayer( int );
213 :
214 : int TestCapability( const char * );
215 :
216 : // The following is only used by OGRDXFLayer
217 :
218 46 : int InlineBlocks() { return bInlineBlocks; }
219 : void AddStandardFields( OGRFeatureDefn *poDef );
220 :
221 : // Implemented in ogrdxf_blockmap.cpp
222 : void ReadBlocksSection();
223 : OGRGeometry *SimplifyBlockGeometry( OGRGeometryCollection * );
224 : DXFBlockDefinition *LookupBlock( const char *pszName );
225 10 : std::map<CPLString,DXFBlockDefinition> &GetBlockMap() { return oBlockMap; }
226 :
227 : // Layer and other Table Handling (ogrdatasource.cpp)
228 : void ReadTablesSection();
229 : void ReadLayerDefinition();
230 : void ReadLineTypeDefinition();
231 : const char *LookupLayerProperty( const char *pszLayer,
232 : const char *pszProperty );
233 : const char *LookupLineType( const char *pszName );
234 :
235 : // Header variables.
236 : void ReadHeaderSection();
237 : const char *GetVariable(const char *pszName,
238 : const char *pszDefault=NULL );
239 :
240 604 : const char *GetEncoding() { return osEncoding; }
241 :
242 : // reader related.
243 31946 : int ReadValue( char *pszValueBuffer, int nValueBufferSize = 81 )
244 31946 : { return oReader.ReadValue( pszValueBuffer, nValueBufferSize ); }
245 26 : void RestartEntities()
246 26 : { oReader.ResetReadPointer(iEntitiesSectionOffset); }
247 474 : void UnreadValue()
248 474 : { oReader.UnreadValue(); }
249 10 : void ResetReadPointer( int iNewOffset )
250 10 : { oReader.ResetReadPointer( iNewOffset ); }
251 : };
252 :
253 : /************************************************************************/
254 : /* OGRDXFWriterLayer */
255 : /************************************************************************/
256 :
257 : class OGRDXFWriterDS;
258 :
259 : class OGRDXFWriterLayer : public OGRLayer
260 : {
261 : VSILFILE *fp;
262 : OGRFeatureDefn *poFeatureDefn;
263 :
264 : OGRDXFWriterDS *poDS;
265 :
266 : int WriteValue( int nCode, const char *pszValue );
267 : int WriteValue( int nCode, int nValue );
268 : int WriteValue( int nCode, double dfValue );
269 :
270 : OGRErr WriteCore( OGRFeature* );
271 : OGRErr WritePOINT( OGRFeature* );
272 : OGRErr WriteTEXT( OGRFeature* );
273 : OGRErr WritePOLYLINE( OGRFeature*, OGRGeometry* = NULL );
274 : OGRErr WriteHATCH( OGRFeature*, OGRGeometry* = NULL );
275 : OGRErr WriteINSERT( OGRFeature* );
276 :
277 : static CPLString TextEscape( const char * );
278 : int ColorStringToDXFColor( const char * );
279 : CPLString PrepareLineTypeDefinition( OGRFeature*, OGRStyleTool* );
280 :
281 : std::map<CPLString,CPLString> oNewLineTypes;
282 : int nNextAutoID;
283 :
284 : public:
285 : OGRDXFWriterLayer( OGRDXFWriterDS *poDS, VSILFILE *fp );
286 : ~OGRDXFWriterLayer();
287 :
288 0 : void ResetReading() {}
289 0 : OGRFeature *GetNextFeature() { return NULL; }
290 :
291 26 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
292 :
293 : int TestCapability( const char * );
294 : OGRErr CreateFeature( OGRFeature *poFeature );
295 : OGRErr CreateField( OGRFieldDefn *poField,
296 : int bApproxOK = TRUE );
297 :
298 : void ResetFP( VSILFILE * );
299 :
300 10 : std::map<CPLString,CPLString>& GetNewLineTypeMap() { return oNewLineTypes;}
301 : };
302 :
303 : /************************************************************************/
304 : /* OGRDXFBlocksWriterLayer */
305 : /************************************************************************/
306 :
307 : class OGRDXFBlocksWriterLayer : public OGRLayer
308 : {
309 : OGRFeatureDefn *poFeatureDefn;
310 :
311 : public:
312 : OGRDXFBlocksWriterLayer( OGRDXFWriterDS *poDS );
313 : ~OGRDXFBlocksWriterLayer();
314 :
315 0 : void ResetReading() {}
316 0 : OGRFeature *GetNextFeature() { return NULL; }
317 :
318 10 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
319 :
320 : int TestCapability( const char * );
321 : OGRErr CreateFeature( OGRFeature *poFeature );
322 : OGRErr CreateField( OGRFieldDefn *poField,
323 : int bApproxOK = TRUE );
324 :
325 : std::vector<OGRFeature*> apoBlocks;
326 : OGRFeature *FindBlock( const char * );
327 : };
328 :
329 : /************************************************************************/
330 : /* OGRDXFWriterDS */
331 : /************************************************************************/
332 :
333 : class OGRDXFWriterDS : public OGRDataSource
334 : {
335 : friend class OGRDXFWriterLayer;
336 :
337 : int nNextFID;
338 :
339 : CPLString osName;
340 : OGRDXFWriterLayer *poLayer;
341 : OGRDXFBlocksWriterLayer *poBlocksLayer;
342 : VSILFILE *fp;
343 : CPLString osTrailerFile;
344 :
345 : CPLString osTempFilename;
346 : VSILFILE *fpTemp;
347 :
348 : CPLString osHeaderFile;
349 : OGRDXFDataSource oHeaderDS;
350 : char **papszLayersToCreate;
351 :
352 : vsi_l_offset nHANDSEEDOffset;
353 :
354 : std::vector<int> anDefaultLayerCode;
355 : std::vector<CPLString> aosDefaultLayerText;
356 :
357 : std::set<CPLString> aosUsedEntities;
358 : void ScanForEntities( const char *pszFilename,
359 : const char *pszTarget );
360 :
361 : int WriteNewLineTypeRecords( VSILFILE *fp );
362 : int WriteNewBlockRecords( VSILFILE * );
363 : int WriteNewBlockDefinitions( VSILFILE * );
364 : int WriteNewLayerDefinitions( VSILFILE * );
365 : int TransferUpdateHeader( VSILFILE * );
366 : int TransferUpdateTrailer( VSILFILE * );
367 : int FixupHANDSEED( VSILFILE * );
368 :
369 : public:
370 : OGRDXFWriterDS();
371 : ~OGRDXFWriterDS();
372 :
373 : int Open( const char * pszFilename,
374 : char **papszOptions );
375 :
376 10 : const char *GetName() { return osName; }
377 :
378 : int GetLayerCount();
379 : OGRLayer *GetLayer( int );
380 :
381 : int TestCapability( const char * );
382 :
383 : OGRLayer *CreateLayer( const char *pszName,
384 : OGRSpatialReference *poSpatialRef = NULL,
385 : OGRwkbGeometryType eGType = wkbUnknown,
386 : char ** papszOptions = NULL );
387 :
388 : int CheckEntityID( const char *pszEntityID );
389 : long WriteEntityID( VSILFILE * fp,
390 : long nPreferredFID = OGRNullFID );
391 : };
392 :
393 : /************************************************************************/
394 : /* OGRDXFDriver */
395 : /************************************************************************/
396 :
397 : class OGRDXFDriver : public OGRSFDriver
398 389 : {
399 : public:
400 : ~OGRDXFDriver();
401 :
402 : const char *GetName();
403 : OGRDataSource *Open( const char *, int );
404 : int TestCapability( const char * );
405 :
406 : OGRDataSource *CreateDataSource( const char *pszName,
407 : char ** = NULL );
408 : };
409 :
410 :
411 : #endif /* ndef _OGR_DXF_H_INCLUDED */
|