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