1 : /******************************************************************************
2 : * $Id: ogrdxfwriterlayer.cpp 20670 2010-09-22 00:21:17Z warmerdam $
3 : *
4 : * Project: DXF Translator
5 : * Purpose: Implements OGRDXFBlocksWriterLayer used for capturing block
6 : * definitions for writing to a DXF file.
7 : * Author: Frank Warmerdam, warmerdam@pobox.com
8 : *
9 : ******************************************************************************
10 : * Copyright (c) 2010, 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 "ogr_featurestyle.h"
35 :
36 : CPL_CVSID("$Id: ogrdxfwriterlayer.cpp 20670 2010-09-22 00:21:17Z warmerdam $");
37 :
38 : /************************************************************************/
39 : /* OGRDXFBlocksWriterLayer() */
40 : /************************************************************************/
41 :
42 2 : OGRDXFBlocksWriterLayer::OGRDXFBlocksWriterLayer( OGRDXFWriterDS *poDS )
43 :
44 : {
45 : (void) poDS;
46 :
47 2 : poFeatureDefn = new OGRFeatureDefn( "blocks" );
48 2 : poFeatureDefn->Reference();
49 :
50 2 : OGRFieldDefn oLayerField( "Layer", OFTString );
51 2 : poFeatureDefn->AddFieldDefn( &oLayerField );
52 :
53 2 : OGRFieldDefn oClassField( "SubClasses", OFTString );
54 2 : poFeatureDefn->AddFieldDefn( &oClassField );
55 :
56 2 : OGRFieldDefn oExtendedField( "ExtendedEntity", OFTString );
57 2 : poFeatureDefn->AddFieldDefn( &oExtendedField );
58 :
59 2 : OGRFieldDefn oLinetypeField( "Linetype", OFTString );
60 2 : poFeatureDefn->AddFieldDefn( &oLinetypeField );
61 :
62 2 : OGRFieldDefn oEntityHandleField( "EntityHandle", OFTString );
63 2 : poFeatureDefn->AddFieldDefn( &oEntityHandleField );
64 :
65 2 : OGRFieldDefn oTextField( "Text", OFTString );
66 2 : poFeatureDefn->AddFieldDefn( &oTextField );
67 :
68 2 : OGRFieldDefn oBlockField( "BlockName", OFTString );
69 2 : poFeatureDefn->AddFieldDefn( &oBlockField );
70 2 : }
71 :
72 : /************************************************************************/
73 : /* ~OGRDXFBlocksWriterLayer() */
74 : /************************************************************************/
75 :
76 2 : OGRDXFBlocksWriterLayer::~OGRDXFBlocksWriterLayer()
77 :
78 : {
79 4 : for( size_t i=0; i < apoBlocks.size(); i++ )
80 2 : delete apoBlocks[i];
81 :
82 2 : if( poFeatureDefn )
83 2 : poFeatureDefn->Release();
84 2 : }
85 :
86 : /************************************************************************/
87 : /* TestCapability() */
88 : /************************************************************************/
89 :
90 0 : int OGRDXFBlocksWriterLayer::TestCapability( const char * pszCap )
91 :
92 : {
93 0 : if( EQUAL(pszCap,OLCSequentialWrite) )
94 0 : return TRUE;
95 : else
96 0 : return FALSE;
97 : }
98 :
99 : /************************************************************************/
100 : /* CreateField() */
101 : /* */
102 : /* This is really a dummy as our fields are precreated. */
103 : /************************************************************************/
104 :
105 0 : OGRErr OGRDXFBlocksWriterLayer::CreateField( OGRFieldDefn *poField,
106 : int bApproxOK )
107 :
108 : {
109 0 : if( poFeatureDefn->GetFieldIndex(poField->GetNameRef()) >= 0
110 : && bApproxOK )
111 0 : return OGRERR_NONE;
112 :
113 : CPLError( CE_Failure, CPLE_AppDefined,
114 : "DXF layer does not support arbitrary field creation, field '%s' not created.",
115 0 : poField->GetNameRef() );
116 :
117 0 : return OGRERR_FAILURE;
118 : }
119 :
120 : /************************************************************************/
121 : /* CreateFeature() */
122 : /* */
123 : /* We just stash a copy of the features for later writing to */
124 : /* the blocks section of the header. */
125 : /************************************************************************/
126 :
127 2 : OGRErr OGRDXFBlocksWriterLayer::CreateFeature( OGRFeature *poFeature )
128 :
129 : {
130 2 : apoBlocks.push_back( poFeature->Clone() );
131 :
132 2 : return OGRERR_NONE;
133 : }
134 :
135 : /************************************************************************/
136 : /* FindBlock() */
137 : /************************************************************************/
138 :
139 6 : OGRFeature *OGRDXFBlocksWriterLayer::FindBlock( const char *pszBlockName )
140 :
141 : {
142 8 : for( size_t i=0; i < apoBlocks.size(); i++ )
143 : {
144 6 : const char *pszThisName = apoBlocks[i]->GetFieldAsString("BlockName");
145 :
146 6 : if( pszThisName != NULL && strcmp(pszBlockName,pszThisName) == 0 )
147 4 : return apoBlocks[i];
148 : }
149 :
150 2 : return NULL;
151 : }
|