1 : /******************************************************************************
2 : * $Id: ogrdxf_blockmap.cpp 23668 2011-12-30 21:44:47Z rouault $
3 : *
4 : * Project: DXF Translator
5 : * Purpose: Implements BlockMap reading and management portion of
6 : * OGRDXFDataSource class
7 : * Author: Frank Warmerdam, warmerdam@pobox.com
8 : *
9 : ******************************************************************************
10 : * Copyright (c) 2009, Frank Warmerdam <warmerdam@pobox.com>
11 : *
12 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : ****************************************************************************/
30 :
31 : #include "ogr_dxf.h"
32 : #include "cpl_conv.h"
33 : #include "cpl_string.h"
34 : #include "cpl_csv.h"
35 :
36 : CPL_CVSID("$Id: ogrdxf_blockmap.cpp 23668 2011-12-30 21:44:47Z rouault $");
37 :
38 : /************************************************************************/
39 : /* ReadBlockSection() */
40 : /************************************************************************/
41 :
42 26 : void OGRDXFDataSource::ReadBlocksSection()
43 :
44 : {
45 : char szLineBuf[257];
46 : int nCode;
47 26 : OGRDXFLayer *poReaderLayer = (OGRDXFLayer *) GetLayerByName( "Entities" );
48 : int bMergeBlockGeometries = CSLTestBoolean(
49 26 : CPLGetConfigOption( "DXF_MERGE_BLOCK_GEOMETRIES", "TRUE" ) );
50 :
51 26 : iEntitiesSectionOffset = oReader.iSrcBufferFileOffset + oReader.iSrcBufferOffset;
52 :
53 46 : while( (nCode = ReadValue( szLineBuf, sizeof(szLineBuf) )) > -1
54 : && !EQUAL(szLineBuf,"ENDSEC") )
55 : {
56 : // We are only interested in extracting blocks.
57 598 : if( nCode != 0 || !EQUAL(szLineBuf,"BLOCK") )
58 502 : continue;
59 :
60 : // Process contents of BLOCK definition till we find the
61 : // first entity.
62 96 : CPLString osBlockName;
63 :
64 96 : while( (nCode = ReadValue( szLineBuf,sizeof(szLineBuf) )) > 0 )
65 : {
66 1154 : if( nCode == 2 )
67 96 : osBlockName = szLineBuf;
68 :
69 : // anything else we want?
70 : }
71 :
72 96 : if( EQUAL(szLineBuf,"ENDBLK") )
73 76 : continue;
74 :
75 20 : if (nCode >= 0)
76 20 : UnreadValue();
77 :
78 : // Now we will process entities till we run out at the ENDBLK code.
79 : // we aggregate the geometries of the features into a multi-geometry,
80 : // but throw away other stuff attached to the features.
81 :
82 : OGRFeature *poFeature;
83 20 : OGRGeometryCollection *poColl = new OGRGeometryCollection();
84 20 : std::vector<OGRFeature*> apoFeatures;
85 :
86 138 : while( (poFeature = poReaderLayer->GetNextUnfilteredFeature()) != NULL )
87 : {
88 190 : if( (poFeature->GetStyleString() != NULL
89 92 : && strstr(poFeature->GetStyleString(),"LABEL") != NULL)
90 : || !bMergeBlockGeometries )
91 : {
92 8 : apoFeatures.push_back( poFeature );
93 : }
94 : else
95 : {
96 90 : poColl->addGeometryDirectly( poFeature->StealGeometry() );
97 90 : delete poFeature;
98 : }
99 : }
100 :
101 20 : if( poColl->getNumGeometries() == 0 )
102 0 : delete poColl;
103 : else
104 20 : oBlockMap[osBlockName].poGeometry = SimplifyBlockGeometry(poColl);
105 :
106 20 : if( apoFeatures.size() > 0 )
107 4 : oBlockMap[osBlockName].apoFeatures = apoFeatures;
108 : }
109 :
110 : CPLDebug( "DXF", "Read %d blocks with meaningful geometry.",
111 26 : (int) oBlockMap.size() );
112 26 : }
113 :
114 : /************************************************************************/
115 : /* SimplifyBlockGeometry() */
116 : /************************************************************************/
117 :
118 20 : OGRGeometry *OGRDXFDataSource::SimplifyBlockGeometry(
119 : OGRGeometryCollection *poCollection )
120 :
121 : {
122 : /* -------------------------------------------------------------------- */
123 : /* If there is only one geometry in the collection, just return */
124 : /* it. */
125 : /* -------------------------------------------------------------------- */
126 20 : if( poCollection->getNumGeometries() == 1 )
127 : {
128 0 : OGRGeometry *poReturn = poCollection->getGeometryRef(0);
129 0 : poCollection->removeGeometry(0,FALSE);
130 0 : delete poCollection;
131 0 : return poReturn;
132 : }
133 :
134 : /* -------------------------------------------------------------------- */
135 : /* Eventually we likely ought to have logic to convert to */
136 : /* polygon, multipolygon, multilinestring or multipoint but */
137 : /* I'll put that off till it would be meaningful. */
138 : /* -------------------------------------------------------------------- */
139 :
140 20 : return poCollection;
141 : }
142 :
143 : /************************************************************************/
144 : /* LookupBlock() */
145 : /* */
146 : /* Find the geometry collection corresponding to a name if it */
147 : /* exists. Note that the returned geometry pointer is to a */
148 : /* geometry that continues to be owned by the datasource. It */
149 : /* should be cloned for use. */
150 : /************************************************************************/
151 :
152 26 : DXFBlockDefinition *OGRDXFDataSource::LookupBlock( const char *pszName )
153 :
154 : {
155 26 : CPLString osName = pszName;
156 :
157 26 : if( oBlockMap.count( osName ) == 0 )
158 10 : return NULL;
159 : else
160 16 : return &(oBlockMap[osName]);
161 : }
162 :
163 : /************************************************************************/
164 : /* ~DXFBlockDefinition() */
165 : /* */
166 : /* Safe cleanup of a block definition. */
167 : /************************************************************************/
168 :
169 60 : DXFBlockDefinition::~DXFBlockDefinition()
170 :
171 : {
172 60 : delete poGeometry;
173 :
174 128 : while( !apoFeatures.empty() )
175 : {
176 8 : delete apoFeatures.back();
177 8 : apoFeatures.pop_back();
178 : }
179 60 : }
|