1 : /******************************************************************************
2 : * $Id: ogr_dxf.h 25811 2013-03-29 22:16:56Z 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 : int nLineNumber;
167 :
168 : int ReadValue( char *pszValueBuffer,
169 : int nValueBufferSize = 81 );
170 : void UnreadValue();
171 : void LoadDiskChunk();
172 : void ResetReadPointer( int iNewOffset );
173 : };
174 :
175 :
176 : /************************************************************************/
177 : /* OGRDXFDataSource */
178 : /************************************************************************/
179 :
180 : class OGRDXFDataSource : public OGRDataSource
181 : {
182 : VSILFILE *fp;
183 :
184 : CPLString osName;
185 : std::vector<OGRLayer*> apoLayers;
186 :
187 : int iEntitiesSectionOffset;
188 :
189 : std::map<CPLString,DXFBlockDefinition> oBlockMap;
190 : std::map<CPLString,CPLString> oHeaderVariables;
191 :
192 : CPLString osEncoding;
193 :
194 : // indexed by layer name, then by property name.
195 : std::map< CPLString, std::map<CPLString,CPLString> >
196 : oLayerTable;
197 :
198 : std::map<CPLString,CPLString> oLineTypeTable;
199 :
200 : int bInlineBlocks;
201 :
202 : OGRDXFReader oReader;
203 :
204 : public:
205 : OGRDXFDataSource();
206 : ~OGRDXFDataSource();
207 :
208 : int Open( const char * pszFilename, int bHeaderOnly=FALSE );
209 :
210 16 : const char *GetName() { return osName; }
211 :
212 58 : int GetLayerCount() { return apoLayers.size(); }
213 : OGRLayer *GetLayer( int );
214 :
215 : int TestCapability( const char * );
216 :
217 : // The following is only used by OGRDXFLayer
218 :
219 30 : int InlineBlocks() { return bInlineBlocks; }
220 : void AddStandardFields( OGRFeatureDefn *poDef );
221 :
222 : // Implemented in ogrdxf_blockmap.cpp
223 : void ReadBlocksSection();
224 : OGRGeometry *SimplifyBlockGeometry( OGRGeometryCollection * );
225 : DXFBlockDefinition *LookupBlock( const char *pszName );
226 5 : std::map<CPLString,DXFBlockDefinition> &GetBlockMap() { return oBlockMap; }
227 :
228 : // Layer and other Table Handling (ogrdatasource.cpp)
229 : void ReadTablesSection();
230 : void ReadLayerDefinition();
231 : void ReadLineTypeDefinition();
232 : const char *LookupLayerProperty( const char *pszLayer,
233 : const char *pszProperty );
234 : const char *LookupLineType( const char *pszName );
235 :
236 : // Header variables.
237 : void ReadHeaderSection();
238 : const char *GetVariable(const char *pszName,
239 : const char *pszDefault=NULL );
240 :
241 351 : const char *GetEncoding() { return osEncoding; }
242 :
243 : // reader related.
244 21424 : int ReadValue( char *pszValueBuffer, int nValueBufferSize = 81 )
245 21424 : { return oReader.ReadValue( pszValueBuffer, nValueBufferSize ); }
246 18 : void RestartEntities()
247 18 : { oReader.ResetReadPointer(iEntitiesSectionOffset); }
248 287 : void UnreadValue()
249 287 : { oReader.UnreadValue(); }
250 7 : void ResetReadPointer( int iNewOffset )
251 7 : { oReader.ResetReadPointer( iNewOffset ); }
252 : };
253 :
254 : /************************************************************************/
255 : /* OGRDXFWriterLayer */
256 : /************************************************************************/
257 :
258 : class OGRDXFWriterDS;
259 :
260 : class OGRDXFWriterLayer : public OGRLayer
261 : {
262 : VSILFILE *fp;
263 : OGRFeatureDefn *poFeatureDefn;
264 :
265 : OGRDXFWriterDS *poDS;
266 :
267 : int WriteValue( int nCode, const char *pszValue );
268 : int WriteValue( int nCode, int nValue );
269 : int WriteValue( int nCode, double dfValue );
270 :
271 : OGRErr WriteCore( OGRFeature* );
272 : OGRErr WritePOINT( OGRFeature* );
273 : OGRErr WriteTEXT( OGRFeature* );
274 : OGRErr WritePOLYLINE( OGRFeature*, OGRGeometry* = NULL );
275 : OGRErr WriteHATCH( OGRFeature*, OGRGeometry* = NULL );
276 : OGRErr WriteINSERT( OGRFeature* );
277 :
278 : static CPLString TextEscape( const char * );
279 : int ColorStringToDXFColor( const char * );
280 : CPLString PrepareLineTypeDefinition( OGRFeature*, OGRStyleTool* );
281 :
282 : std::map<CPLString,CPLString> oNewLineTypes;
283 : int nNextAutoID;
284 : int bWriteHatch;
285 :
286 : public:
287 : OGRDXFWriterLayer( OGRDXFWriterDS *poDS, VSILFILE *fp );
288 : ~OGRDXFWriterLayer();
289 :
290 0 : void ResetReading() {}
291 0 : OGRFeature *GetNextFeature() { return NULL; }
292 :
293 15 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
294 :
295 : int TestCapability( const char * );
296 : OGRErr CreateFeature( OGRFeature *poFeature );
297 : OGRErr CreateField( OGRFieldDefn *poField,
298 : int bApproxOK = TRUE );
299 :
300 : void ResetFP( VSILFILE * );
301 :
302 7 : std::map<CPLString,CPLString>& GetNewLineTypeMap() { return oNewLineTypes;}
303 : };
304 :
305 : /************************************************************************/
306 : /* OGRDXFBlocksWriterLayer */
307 : /************************************************************************/
308 :
309 : class OGRDXFBlocksWriterLayer : public OGRLayer
310 : {
311 : OGRFeatureDefn *poFeatureDefn;
312 :
313 : public:
314 : OGRDXFBlocksWriterLayer( OGRDXFWriterDS *poDS );
315 : ~OGRDXFBlocksWriterLayer();
316 :
317 0 : void ResetReading() {}
318 0 : OGRFeature *GetNextFeature() { return NULL; }
319 :
320 5 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
321 :
322 : int TestCapability( const char * );
323 : OGRErr CreateFeature( OGRFeature *poFeature );
324 : OGRErr CreateField( OGRFieldDefn *poField,
325 : int bApproxOK = TRUE );
326 :
327 : std::vector<OGRFeature*> apoBlocks;
328 : OGRFeature *FindBlock( const char * );
329 : };
330 :
331 : /************************************************************************/
332 : /* OGRDXFWriterDS */
333 : /************************************************************************/
334 :
335 : class OGRDXFWriterDS : public OGRDataSource
336 : {
337 : friend class OGRDXFWriterLayer;
338 :
339 : int nNextFID;
340 :
341 : CPLString osName;
342 : OGRDXFWriterLayer *poLayer;
343 : OGRDXFBlocksWriterLayer *poBlocksLayer;
344 : VSILFILE *fp;
345 : CPLString osTrailerFile;
346 :
347 : CPLString osTempFilename;
348 : VSILFILE *fpTemp;
349 :
350 : CPLString osHeaderFile;
351 : OGRDXFDataSource oHeaderDS;
352 : char **papszLayersToCreate;
353 :
354 : vsi_l_offset nHANDSEEDOffset;
355 :
356 : std::vector<int> anDefaultLayerCode;
357 : std::vector<CPLString> aosDefaultLayerText;
358 :
359 : std::set<CPLString> aosUsedEntities;
360 : void ScanForEntities( const char *pszFilename,
361 : const char *pszTarget );
362 :
363 : int WriteNewLineTypeRecords( VSILFILE *fp );
364 : int WriteNewBlockRecords( VSILFILE * );
365 : int WriteNewBlockDefinitions( VSILFILE * );
366 : int WriteNewLayerDefinitions( VSILFILE * );
367 : int TransferUpdateHeader( VSILFILE * );
368 : int TransferUpdateTrailer( VSILFILE * );
369 : int FixupHANDSEED( VSILFILE * );
370 :
371 : OGREnvelope oGlobalEnvelope;
372 :
373 : public:
374 : OGRDXFWriterDS();
375 : ~OGRDXFWriterDS();
376 :
377 : int Open( const char * pszFilename,
378 : char **papszOptions );
379 :
380 7 : const char *GetName() { return osName; }
381 :
382 : int GetLayerCount();
383 : OGRLayer *GetLayer( int );
384 :
385 : int TestCapability( const char * );
386 :
387 : OGRLayer *CreateLayer( const char *pszName,
388 : OGRSpatialReference *poSpatialRef = NULL,
389 : OGRwkbGeometryType eGType = wkbUnknown,
390 : char ** papszOptions = NULL );
391 :
392 : int CheckEntityID( const char *pszEntityID );
393 : long WriteEntityID( VSILFILE * fp,
394 : long nPreferredFID = OGRNullFID );
395 :
396 : void UpdateExtent( OGREnvelope* psEnvelope );
397 : };
398 :
399 : /************************************************************************/
400 : /* OGRDXFDriver */
401 : /************************************************************************/
402 :
403 : class OGRDXFDriver : public OGRSFDriver
404 244 : {
405 : public:
406 : ~OGRDXFDriver();
407 :
408 : const char *GetName();
409 : OGRDataSource *Open( const char *, int );
410 : int TestCapability( const char * );
411 :
412 : OGRDataSource *CreateDataSource( const char *pszName,
413 : char ** = NULL );
414 : };
415 :
416 :
417 : #endif /* ndef _OGR_DXF_H_INCLUDED */
|