1 : /******************************************************************************
2 : * $Id: ogr_dxf.h 24991 2012-09-29 10:13:28Z rouault $
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 20 : {
51 : public:
52 10 : 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 3 : 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 39 : 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 16 : const char *GetName() { return osName; }
210 :
211 58 : 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 30 : 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 5 : 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 351 : const char *GetEncoding() { return osEncoding; }
241 :
242 : // reader related.
243 21424 : int ReadValue( char *pszValueBuffer, int nValueBufferSize = 81 )
244 21424 : { return oReader.ReadValue( pszValueBuffer, nValueBufferSize ); }
245 18 : void RestartEntities()
246 18 : { oReader.ResetReadPointer(iEntitiesSectionOffset); }
247 287 : void UnreadValue()
248 287 : { oReader.UnreadValue(); }
249 7 : void ResetReadPointer( int iNewOffset )
250 7 : { 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 : int bWriteHatch;
284 :
285 : public:
286 : OGRDXFWriterLayer( OGRDXFWriterDS *poDS, VSILFILE *fp );
287 : ~OGRDXFWriterLayer();
288 :
289 0 : void ResetReading() {}
290 0 : OGRFeature *GetNextFeature() { return NULL; }
291 :
292 15 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
293 :
294 : int TestCapability( const char * );
295 : OGRErr CreateFeature( OGRFeature *poFeature );
296 : OGRErr CreateField( OGRFieldDefn *poField,
297 : int bApproxOK = TRUE );
298 :
299 : void ResetFP( VSILFILE * );
300 :
301 7 : std::map<CPLString,CPLString>& GetNewLineTypeMap() { return oNewLineTypes;}
302 : };
303 :
304 : /************************************************************************/
305 : /* OGRDXFBlocksWriterLayer */
306 : /************************************************************************/
307 :
308 : class OGRDXFBlocksWriterLayer : public OGRLayer
309 : {
310 : OGRFeatureDefn *poFeatureDefn;
311 :
312 : public:
313 : OGRDXFBlocksWriterLayer( OGRDXFWriterDS *poDS );
314 : ~OGRDXFBlocksWriterLayer();
315 :
316 0 : void ResetReading() {}
317 0 : OGRFeature *GetNextFeature() { return NULL; }
318 :
319 5 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
320 :
321 : int TestCapability( const char * );
322 : OGRErr CreateFeature( OGRFeature *poFeature );
323 : OGRErr CreateField( OGRFieldDefn *poField,
324 : int bApproxOK = TRUE );
325 :
326 : std::vector<OGRFeature*> apoBlocks;
327 : OGRFeature *FindBlock( const char * );
328 : };
329 :
330 : /************************************************************************/
331 : /* OGRDXFWriterDS */
332 : /************************************************************************/
333 :
334 : class OGRDXFWriterDS : public OGRDataSource
335 : {
336 : friend class OGRDXFWriterLayer;
337 :
338 : int nNextFID;
339 :
340 : CPLString osName;
341 : OGRDXFWriterLayer *poLayer;
342 : OGRDXFBlocksWriterLayer *poBlocksLayer;
343 : VSILFILE *fp;
344 : CPLString osTrailerFile;
345 :
346 : CPLString osTempFilename;
347 : VSILFILE *fpTemp;
348 :
349 : CPLString osHeaderFile;
350 : OGRDXFDataSource oHeaderDS;
351 : char **papszLayersToCreate;
352 :
353 : vsi_l_offset nHANDSEEDOffset;
354 :
355 : std::vector<int> anDefaultLayerCode;
356 : std::vector<CPLString> aosDefaultLayerText;
357 :
358 : std::set<CPLString> aosUsedEntities;
359 : void ScanForEntities( const char *pszFilename,
360 : const char *pszTarget );
361 :
362 : int WriteNewLineTypeRecords( VSILFILE *fp );
363 : int WriteNewBlockRecords( VSILFILE * );
364 : int WriteNewBlockDefinitions( VSILFILE * );
365 : int WriteNewLayerDefinitions( VSILFILE * );
366 : int TransferUpdateHeader( VSILFILE * );
367 : int TransferUpdateTrailer( VSILFILE * );
368 : int FixupHANDSEED( VSILFILE * );
369 :
370 : OGREnvelope oGlobalEnvelope;
371 :
372 : public:
373 : OGRDXFWriterDS();
374 : ~OGRDXFWriterDS();
375 :
376 : int Open( const char * pszFilename,
377 : char **papszOptions );
378 :
379 7 : const char *GetName() { return osName; }
380 :
381 : int GetLayerCount();
382 : OGRLayer *GetLayer( int );
383 :
384 : int TestCapability( const char * );
385 :
386 : OGRLayer *CreateLayer( const char *pszName,
387 : OGRSpatialReference *poSpatialRef = NULL,
388 : OGRwkbGeometryType eGType = wkbUnknown,
389 : char ** papszOptions = NULL );
390 :
391 : int CheckEntityID( const char *pszEntityID );
392 : long WriteEntityID( VSILFILE * fp,
393 : long nPreferredFID = OGRNullFID );
394 :
395 : void UpdateExtent( OGREnvelope* psEnvelope );
396 : };
397 :
398 : /************************************************************************/
399 : /* OGRDXFDriver */
400 : /************************************************************************/
401 :
402 : class OGRDXFDriver : public OGRSFDriver
403 226 : {
404 : public:
405 : ~OGRDXFDriver();
406 :
407 : const char *GetName();
408 : OGRDataSource *Open( const char *, int );
409 : int TestCapability( const char * );
410 :
411 : OGRDataSource *CreateDataSource( const char *pszName,
412 : char ** = NULL );
413 : };
414 :
415 :
416 : #endif /* ndef _OGR_DXF_H_INCLUDED */
|