Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: DXF Translator 4 : * Purpose: Implements OGRDXFBlocksWriterLayer used for capturing block 5 : * definitions for writing to a DXF file. 6 : * Author: Frank Warmerdam, warmerdam@pobox.com 7 : * 8 : ****************************************************************************** 9 : * Copyright (c) 2010, Frank Warmerdam <warmerdam@pobox.com> 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 : #include "ogr_dxf.h" 31 : #include "cpl_conv.h" 32 : #include "cpl_string.h" 33 : #include "ogr_featurestyle.h" 34 : 35 : /************************************************************************/ 36 : /* OGRDXFBlocksWriterLayer() */ 37 : /************************************************************************/ 38 : 39 2 : OGRDXFBlocksWriterLayer::OGRDXFBlocksWriterLayer(OGRDXFWriterDS * /* poDS */) 40 2 : : poFeatureDefn(new OGRFeatureDefn("blocks")) 41 : { 42 2 : poFeatureDefn->Reference(); 43 : 44 2 : OGRDXFDataSource::AddStandardFields(poFeatureDefn, ODFM_IncludeBlockFields); 45 2 : } 46 : 47 : /************************************************************************/ 48 : /* ~OGRDXFBlocksWriterLayer() */ 49 : /************************************************************************/ 50 : 51 4 : OGRDXFBlocksWriterLayer::~OGRDXFBlocksWriterLayer() 52 : 53 : { 54 6 : for (size_t i = 0; i < apoBlocks.size(); i++) 55 4 : delete apoBlocks[i]; 56 : 57 2 : if (poFeatureDefn) 58 2 : poFeatureDefn->Release(); 59 4 : } 60 : 61 : /************************************************************************/ 62 : /* TestCapability() */ 63 : /************************************************************************/ 64 : 65 4 : int OGRDXFBlocksWriterLayer::TestCapability(const char *pszCap) 66 : 67 : { 68 4 : return EQUAL(pszCap, OLCSequentialWrite); 69 : } 70 : 71 : /************************************************************************/ 72 : /* CreateField() */ 73 : /* */ 74 : /* This is really a dummy as our fields are precreated. */ 75 : /************************************************************************/ 76 : 77 0 : OGRErr OGRDXFBlocksWriterLayer::CreateField(const OGRFieldDefn *poField, 78 : int bApproxOK) 79 : 80 : { 81 0 : if (poFeatureDefn->GetFieldIndex(poField->GetNameRef()) >= 0 && bApproxOK) 82 0 : return OGRERR_NONE; 83 : 84 0 : CPLError(CE_Failure, CPLE_AppDefined, 85 : "DXF layer does not support arbitrary field creation, field '%s' " 86 : "not created.", 87 : poField->GetNameRef()); 88 : 89 0 : return OGRERR_FAILURE; 90 : } 91 : 92 : /************************************************************************/ 93 : /* ICreateFeature() */ 94 : /* */ 95 : /* We just stash a copy of the features for later writing to */ 96 : /* the blocks section of the header. */ 97 : /************************************************************************/ 98 : 99 4 : OGRErr OGRDXFBlocksWriterLayer::ICreateFeature(OGRFeature *poFeature) 100 : 101 : { 102 4 : apoBlocks.push_back(poFeature->Clone()); 103 : 104 4 : return OGRERR_NONE; 105 : } 106 : 107 : /************************************************************************/ 108 : /* FindBlock() */ 109 : /************************************************************************/ 110 : 111 7 : OGRFeature *OGRDXFBlocksWriterLayer::FindBlock(const char *pszBlockName) 112 : 113 : { 114 16 : for (size_t i = 0; i < apoBlocks.size(); i++) 115 : { 116 14 : const char *pszThisName = apoBlocks[i]->GetFieldAsString("Block"); 117 : 118 14 : if (pszThisName != nullptr && strcmp(pszBlockName, pszThisName) == 0) 119 5 : return apoBlocks[i]; 120 : } 121 : 122 2 : return nullptr; 123 : }