| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Project: DWG Translator |
| 4 |
* Purpose: Implements BlockMap reading and management portion of |
| 5 |
* OGRDWGDataSource class |
| 6 |
* Author: Frank Warmerdam, [email protected] |
| 7 |
* |
| 8 |
****************************************************************************** |
| 9 |
* Copyright (c) 2009, Frank Warmerdam <[email protected]> |
| 10 |
* |
| 11 |
* SPDX-License-Identifier: MIT |
| 12 |
****************************************************************************/ |
| 13 |
|
| 14 |
#include "ogr_dwg.h" |
| 15 |
#include "cpl_conv.h" |
| 16 |
#include "cpl_string.h" |
| 17 |
#include "cpl_csv.h" |
| 18 |
|
| 19 |
/************************************************************************/ |
| 20 |
/* ReadBlockSection() */ |
| 21 |
/************************************************************************/ |
| 22 |
|
| 23 |
void OGRDWGDataSource::ReadBlocksSection() |
| 24 |
|
| 25 |
{ |
| 26 |
OGRDWGLayer *poReaderLayer = |
| 27 |
cpl::down_cast<OGRDWGLayer *>(GetLayerByName("Entities")); |
| 28 |
int bMergeBlockGeometries = |
| 29 |
CPLTestBool(CPLGetConfigOption("DWG_MERGE_BLOCK_GEOMETRIES", "TRUE")); |
| 30 |
|
| 31 |
/* -------------------------------------------------------------------- */ |
| 32 |
/* Loop over all the block tables, skipping *Model_Space which */ |
| 33 |
/* we assume is primary entities. */ |
| 34 |
/* -------------------------------------------------------------------- */ |
| 35 |
OdDbBlockTableRecordPtr poModelSpace, poBlock; |
| 36 |
OdDbBlockTablePtr pTable = GetDB()->getBlockTableId().safeOpenObject(); |
| 37 |
OdDbSymbolTableIteratorPtr pBlkIter = pTable->newIterator(); |
| 38 |
|
| 39 |
for (pBlkIter->start(); !pBlkIter->done(); pBlkIter->step()) |
| 40 |
{ |
| 41 |
poBlock = pBlkIter->getRecordId().safeOpenObject(); |
| 42 |
CPLString osBlockName = (const char *)poBlock->getName(); |
| 43 |
|
| 44 |
if (EQUAL(osBlockName, "*Model_Space")) |
| 45 |
{ |
| 46 |
poModelSpace = poBlock; |
| 47 |
continue; |
| 48 |
} |
| 49 |
|
| 50 |
poReaderLayer->SetBlockTable(poBlock); |
| 51 |
|
| 52 |
// Now we will process entities till we run out. |
| 53 |
// We aggregate the geometries of the features into a multi-geometry, |
| 54 |
// but throw away other stuff attached to the features. |
| 55 |
|
| 56 |
OGRFeature *poFeature = nullptr; |
| 57 |
OGRGeometryCollection *poColl = new OGRGeometryCollection(); |
| 58 |
std::vector<OGRFeature *> apoFeatures; |
| 59 |
|
| 60 |
while ((poFeature = poReaderLayer->GetNextUnfilteredFeature()) != |
| 61 |
nullptr) |
| 62 |
{ |
| 63 |
if ((poFeature->GetStyleString() != nullptr && |
| 64 |
strstr(poFeature->GetStyleString(), "LABEL") != nullptr) || |
| 65 |
!bMergeBlockGeometries) |
| 66 |
{ |
| 67 |
apoFeatures.push_back(poFeature); |
| 68 |
} |
| 69 |
else |
| 70 |
{ |
| 71 |
poColl->addGeometryDirectly(poFeature->StealGeometry()); |
| 72 |
delete poFeature; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if (poColl->getNumGeometries() == 0) |
| 77 |
delete poColl; |
| 78 |
else |
| 79 |
oBlockMap[osBlockName].poGeometry = SimplifyBlockGeometry(poColl); |
| 80 |
|
| 81 |
if (!apoFeatures.empty()) |
| 82 |
oBlockMap[osBlockName].apoFeatures = apoFeatures; |
| 83 |
} |
| 84 |
|
| 85 |
CPLDebug("DWG", "Read %d blocks with meaningful geometry.", |
| 86 |
(int)oBlockMap.size()); |
| 87 |
|
| 88 |
poReaderLayer->SetBlockTable(poModelSpace); |
| 89 |
} |
| 90 |
|
| 91 |
/************************************************************************/ |
| 92 |
/* SimplifyBlockGeometry() */ |
| 93 |
/************************************************************************/ |
| 94 |
|
| 95 |
OGRGeometry * |
| 96 |
OGRDWGDataSource::SimplifyBlockGeometry(OGRGeometryCollection *poCollection) |
| 97 |
|
| 98 |
{ |
| 99 |
/* -------------------------------------------------------------------- */ |
| 100 |
/* If there is only one geometry in the collection, just return */ |
| 101 |
/* it. */ |
| 102 |
/* -------------------------------------------------------------------- */ |
| 103 |
if (poCollection->getNumGeometries() == 1) |
| 104 |
{ |
| 105 |
OGRGeometry *poReturn = poCollection->getGeometryRef(0); |
| 106 |
poCollection->removeGeometry(0, FALSE); |
| 107 |
delete poCollection; |
| 108 |
return poReturn; |
| 109 |
} |
| 110 |
|
| 111 |
/* -------------------------------------------------------------------- */ |
| 112 |
/* Eventually we likely ought to have logic to convert to */ |
| 113 |
/* polygon, multipolygon, multilinestring or multipoint but */ |
| 114 |
/* I'll put that off till it would be meaningful. */ |
| 115 |
/* -------------------------------------------------------------------- */ |
| 116 |
|
| 117 |
return poCollection; |
| 118 |
} |
| 119 |
|
| 120 |
/************************************************************************/ |
| 121 |
/* LookupBlock() */ |
| 122 |
/* */ |
| 123 |
/* Find the geometry collection corresponding to a name if it */ |
| 124 |
/* exists. Note that the returned geometry pointer is to a */ |
| 125 |
/* geometry that continues to be owned by the datasource. It */ |
| 126 |
/* should be cloned for use. */ |
| 127 |
/************************************************************************/ |
| 128 |
|
| 129 |
DWGBlockDefinition *OGRDWGDataSource::LookupBlock(const char *pszName) |
| 130 |
|
| 131 |
{ |
| 132 |
const CPLString osName(pszName); |
| 133 |
|
| 134 |
if (oBlockMap.count(osName) == 0) |
| 135 |
return nullptr; |
| 136 |
else |
| 137 |
return &(oBlockMap[osName]); |
| 138 |
} |
| 139 |
|
| 140 |
/************************************************************************/ |
| 141 |
/* ~DWGBlockDefinition() */ |
| 142 |
/* */ |
| 143 |
/* Safe cleanup of a block definition. */ |
| 144 |
/************************************************************************/ |
| 145 |
|
| 146 |
DWGBlockDefinition::~DWGBlockDefinition() |
| 147 |
|
| 148 |
{ |
| 149 |
delete poGeometry; |
| 150 |
|
| 151 |
while (!apoFeatures.empty()) |
| 152 |
{ |
| 153 |
delete apoFeatures.back(); |
| 154 |
apoFeatures.pop_back(); |
| 155 |
} |
| 156 |
} |