1 : /******************************************************************************
2 : * $Id: ogrdxf_blockmap.cpp 22011 2011-03-22 20:13:38Z warmerdam $
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 22011 2011-03-22 20:13:38Z warmerdam $");
37 :
38 : /************************************************************************/
39 : /* ReadBlockSection() */
40 : /************************************************************************/
41 :
42 13 : void OGRDXFDataSource::ReadBlocksSection()
43 :
44 : {
45 : char szLineBuf[257];
46 : int nCode;
47 13 : OGRDXFLayer *poReaderLayer = (OGRDXFLayer *) GetLayerByName( "Entities" );
48 : int bMergeBlockGeometries = CSLTestBoolean(
49 13 : CPLGetConfigOption( "DXF_MERGE_BLOCK_GEOMETRIES", "TRUE" ) );
50 :
51 13 : iEntitiesSectionOffset = oReader.iSrcBufferFileOffset + oReader.iSrcBufferOffset;
52 :
53 23 : while( (nCode = ReadValue( szLineBuf, sizeof(szLineBuf) )) > -1
54 : && !EQUAL(szLineBuf,"ENDSEC") )
55 : {
56 : // We are only interested in extracting blocks.
57 299 : if( nCode != 0 || !EQUAL(szLineBuf,"BLOCK") )
58 251 : continue;
59 :
60 : // Process contents of BLOCK definition till we find the
61 : // first entity.
62 48 : CPLString osBlockName;
63 :
64 48 : while( (nCode = ReadValue( szLineBuf,sizeof(szLineBuf) )) > 0 )
65 : {
66 577 : if( nCode == 2 )
67 48 : osBlockName = szLineBuf;
68 :
69 : // anything else we want?
70 : }
71 :
72 48 : if( EQUAL(szLineBuf,"ENDBLK") )
73 38 : continue;
74 :
75 10 : UnreadValue();
76 :
77 : // Now we will process entities till we run out at the ENDBLK code.
78 : // we aggregate the geometries of the features into a multi-geometry,
79 : // but throw away other stuff attached to the features.
80 :
81 : OGRFeature *poFeature;
82 10 : OGRGeometryCollection *poColl = new OGRGeometryCollection();
83 10 : std::vector<OGRFeature*> apoFeatures;
84 :
85 69 : while( (poFeature = poReaderLayer->GetNextUnfilteredFeature()) != NULL )
86 : {
87 95 : if( (poFeature->GetStyleString() != NULL
88 46 : && strstr(poFeature->GetStyleString(),"LABEL") != NULL)
89 : || !bMergeBlockGeometries )
90 : {
91 4 : apoFeatures.push_back( poFeature );
92 : }
93 : else
94 : {
95 45 : poColl->addGeometryDirectly( poFeature->StealGeometry() );
96 45 : delete poFeature;
97 : }
98 : }
99 :
100 10 : if( poColl->getNumGeometries() == 0 )
101 0 : delete poColl;
102 : else
103 10 : oBlockMap[osBlockName].poGeometry = SimplifyBlockGeometry(poColl);
104 :
105 10 : if( apoFeatures.size() > 0 )
106 2 : oBlockMap[osBlockName].apoFeatures = apoFeatures;
107 : }
108 :
109 : CPLDebug( "DXF", "Read %d blocks with meaningful geometry.",
110 13 : (int) oBlockMap.size() );
111 13 : }
112 :
113 : /************************************************************************/
114 : /* SimplifyBlockGeometry() */
115 : /************************************************************************/
116 :
117 10 : OGRGeometry *OGRDXFDataSource::SimplifyBlockGeometry(
118 : OGRGeometryCollection *poCollection )
119 :
120 : {
121 : /* -------------------------------------------------------------------- */
122 : /* If there is only one geometry in the collection, just return */
123 : /* it. */
124 : /* -------------------------------------------------------------------- */
125 10 : if( poCollection->getNumGeometries() == 1 )
126 : {
127 0 : OGRGeometry *poReturn = poCollection->getGeometryRef(0);
128 0 : poCollection->removeGeometry(0,FALSE);
129 0 : delete poCollection;
130 0 : return poReturn;
131 : }
132 :
133 : /* -------------------------------------------------------------------- */
134 : /* Eventually we likely ought to have logic to convert to */
135 : /* polygon, multipolygon, multilinestring or multipoint but */
136 : /* I'll put that off till it would be meaningful. */
137 : /* -------------------------------------------------------------------- */
138 :
139 10 : return poCollection;
140 : }
141 :
142 : /************************************************************************/
143 : /* LookupBlock() */
144 : /* */
145 : /* Find the geometry collection corresponding to a name if it */
146 : /* exists. Note that the returned geometry pointer is to a */
147 : /* geometry that continues to be owned by the datasource. It */
148 : /* should be cloned for use. */
149 : /************************************************************************/
150 :
151 13 : DXFBlockDefinition *OGRDXFDataSource::LookupBlock( const char *pszName )
152 :
153 : {
154 13 : CPLString osName = pszName;
155 :
156 13 : if( oBlockMap.count( osName ) == 0 )
157 5 : return NULL;
158 : else
159 8 : return &(oBlockMap[osName]);
160 : }
161 :
162 : /************************************************************************/
163 : /* ~DXFBlockDefinition() */
164 : /* */
165 : /* Safe cleanup of a block definition. */
166 : /************************************************************************/
167 :
168 30 : DXFBlockDefinition::~DXFBlockDefinition()
169 :
170 : {
171 30 : delete poGeometry;
172 :
173 64 : while( !apoFeatures.empty() )
174 : {
175 4 : delete apoFeatures.back();
176 4 : apoFeatures.pop_back();
177 : }
178 30 : }
|