| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Project: OpenGIS Simple Features Reference Implementation |
| 4 |
* Purpose: OGR Driver for DGNv8 |
| 5 |
* Author: Even Rouault <even.rouault at spatialys.com> |
| 6 |
* |
| 7 |
****************************************************************************** |
| 8 |
* Copyright (c) 2017, Even Rouault <even.rouault at spatialys.com> |
| 9 |
* |
| 10 |
* SPDX-License-Identifier: MIT |
| 11 |
****************************************************************************/ |
| 12 |
|
| 13 |
#include "ogr_dgnv8.h" |
| 14 |
#include "cpl_conv.h" |
| 15 |
#include "cpl_string.h" |
| 16 |
|
| 17 |
/************************************************************************/ |
| 18 |
/* OGRDGNV8DataSource() */ |
| 19 |
/************************************************************************/ |
| 20 |
|
| 21 |
OGRDGNV8DataSource::OGRDGNV8DataSource(OGRDGNV8Services *poServices) |
| 22 |
: m_poServices(poServices), m_papoLayers(nullptr), m_nLayers(0), |
| 23 |
m_papszOptions(nullptr), m_poDb(static_cast<const OdRxObject *>(nullptr)), |
| 24 |
m_bUpdate(false), m_bModified(false) |
| 25 |
{ |
| 26 |
} |
| 27 |
|
| 28 |
/************************************************************************/ |
| 29 |
/* ~OGRDGNV8DataSource() */ |
| 30 |
/************************************************************************/ |
| 31 |
|
| 32 |
OGRDGNV8DataSource::~OGRDGNV8DataSource() |
| 33 |
|
| 34 |
{ |
| 35 |
OGRDGNV8DataSource::FlushCache(true); |
| 36 |
|
| 37 |
for (int i = 0; i < m_nLayers; i++) |
| 38 |
delete m_papoLayers[i]; |
| 39 |
|
| 40 |
CPLFree(m_papoLayers); |
| 41 |
CSLDestroy(m_papszOptions); |
| 42 |
} |
| 43 |
|
| 44 |
/************************************************************************/ |
| 45 |
/* FlushCache() */ |
| 46 |
/************************************************************************/ |
| 47 |
|
| 48 |
CPLErr OGRDGNV8DataSource::FlushCache(bool /* bAtClosing */) |
| 49 |
{ |
| 50 |
if (m_poDb.isNull() || !m_bModified) |
| 51 |
return CE_None; |
| 52 |
m_bModified = false; |
| 53 |
|
| 54 |
for (int i = 0; i < m_nLayers; i++) |
| 55 |
{ |
| 56 |
m_papoLayers[i]->m_pModel->fitToView(); |
| 57 |
} |
| 58 |
|
| 59 |
CPLErr eErr = CE_None; |
| 60 |
OdString oFilename(FromUTF8(GetDescription())); |
| 61 |
try |
| 62 |
{ |
| 63 |
m_poDb->writeFile(oFilename); |
| 64 |
} |
| 65 |
catch (const OdError &e) |
| 66 |
{ |
| 67 |
eErr = CE_Failure; |
| 68 |
CPLError(CE_Failure, CPLE_AppDefined, "Teigha DGN error occurred: %s", |
| 69 |
ToUTF8(e.description()).c_str()); |
| 70 |
} |
| 71 |
catch (const std::exception &exc) |
| 72 |
{ |
| 73 |
eErr = CE_Failure; |
| 74 |
CPLError(CE_Failure, CPLE_AppDefined, "std::exception occurred: %s", |
| 75 |
exc.what()); |
| 76 |
} |
| 77 |
catch (...) |
| 78 |
{ |
| 79 |
eErr = CE_Failure; |
| 80 |
CPLError(CE_Failure, CPLE_AppDefined, "Unknown exception occurred"); |
| 81 |
} |
| 82 |
return eErr; |
| 83 |
} |
| 84 |
|
| 85 |
/************************************************************************/ |
| 86 |
/* Open() */ |
| 87 |
/************************************************************************/ |
| 88 |
|
| 89 |
int OGRDGNV8DataSource::Open(const char *pszFilename, bool bUpdate) |
| 90 |
|
| 91 |
{ |
| 92 |
SetDescription(pszFilename); |
| 93 |
|
| 94 |
OdString oFilename(FromUTF8(pszFilename)); |
| 95 |
try |
| 96 |
{ |
| 97 |
m_poDb = m_poServices->readFile(oFilename); |
| 98 |
} |
| 99 |
catch (const OdError &e) |
| 100 |
{ |
| 101 |
CPLError(CE_Failure, CPLE_AppDefined, "Teigha DGN error occurred: %s", |
| 102 |
ToUTF8(e.description()).c_str()); |
| 103 |
return FALSE; |
| 104 |
} |
| 105 |
catch (const std::exception &exc) |
| 106 |
{ |
| 107 |
CPLError(CE_Failure, CPLE_AppDefined, "std::exception occurred: %s", |
| 108 |
exc.what()); |
| 109 |
return FALSE; |
| 110 |
} |
| 111 |
catch (...) |
| 112 |
{ |
| 113 |
CPLError(CE_Failure, CPLE_AppDefined, "Unknown exception occurred"); |
| 114 |
return FALSE; |
| 115 |
} |
| 116 |
|
| 117 |
OdDgModelTablePtr pModelTable = m_poDb->getModelTable(); |
| 118 |
if (pModelTable.isNull()) |
| 119 |
{ |
| 120 |
CPLError(CE_Failure, CPLE_AppDefined, "No model table found"); |
| 121 |
return FALSE; |
| 122 |
} |
| 123 |
|
| 124 |
// Loop over models |
| 125 |
OdDgElementIteratorPtr pIter = pModelTable->createIterator(); |
| 126 |
for (; !pIter.isNull() && !pIter->done(); pIter->step()) |
| 127 |
{ |
| 128 |
OdDgModelPtr pModel = OdDgModel::cast(pIter->item().openObject( |
| 129 |
bUpdate ? OdDg::kForWrite : OdDg::kForRead)); |
| 130 |
if (!pModel.isNull()) |
| 131 |
{ |
| 132 |
OGRDGNV8Layer *poLayer = new OGRDGNV8Layer(this, pModel); |
| 133 |
m_papoLayers = static_cast<OGRDGNV8Layer **>(CPLRealloc( |
| 134 |
m_papoLayers, sizeof(OGRDGNV8Layer *) * (m_nLayers + 1))); |
| 135 |
m_papoLayers[m_nLayers++] = poLayer; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
m_bUpdate = bUpdate; |
| 140 |
|
| 141 |
return m_bUpdate || m_nLayers > 0; |
| 142 |
} |
| 143 |
|
| 144 |
/************************************************************************/ |
| 145 |
/* TestCapability() */ |
| 146 |
/************************************************************************/ |
| 147 |
|
| 148 |
int OGRDGNV8DataSource::TestCapability(const char *pszCap) const |
| 149 |
|
| 150 |
{ |
| 151 |
if (EQUAL(pszCap, ODsCCreateLayer)) |
| 152 |
return m_bUpdate; |
| 153 |
else if (EQUAL(pszCap, ODsCCurveGeometries)) |
| 154 |
return TRUE; |
| 155 |
else if (EQUAL(pszCap, ODsCZGeometries)) |
| 156 |
return TRUE; |
| 157 |
|
| 158 |
return FALSE; |
| 159 |
} |
| 160 |
|
| 161 |
/************************************************************************/ |
| 162 |
/* GetLayer() */ |
| 163 |
/************************************************************************/ |
| 164 |
|
| 165 |
const OGRLayer *OGRDGNV8DataSource::GetLayer(int iLayer) const |
| 166 |
|
| 167 |
{ |
| 168 |
if (iLayer < 0 || iLayer >= m_nLayers) |
| 169 |
return nullptr; |
| 170 |
|
| 171 |
return m_papoLayers[iLayer]; |
| 172 |
} |
| 173 |
|
| 174 |
/************************************************************************/ |
| 175 |
/* EraseSubElements() */ |
| 176 |
/************************************************************************/ |
| 177 |
|
| 178 |
template <class T> static void EraseSubElements(T container) |
| 179 |
{ |
| 180 |
if (!container.isNull()) |
| 181 |
{ |
| 182 |
OdDgElementIteratorPtr pIter = container->createIterator(); |
| 183 |
for (; !pIter.isNull() && !pIter->done(); pIter->step()) |
| 184 |
{ |
| 185 |
OdDgElementPtr pElement = pIter->item().openObject(OdDg::kForWrite); |
| 186 |
if (!pElement.isNull()) |
| 187 |
{ |
| 188 |
pElement->erase(true); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/************************************************************************/ |
| 195 |
/* InitWithSeed() */ |
| 196 |
/************************************************************************/ |
| 197 |
|
| 198 |
void OGRDGNV8DataSource::InitWithSeed() |
| 199 |
{ |
| 200 |
#if 0 |
| 201 |
EraseSubElements(m_poDb->getLevelTable(OdDg::kForWrite)); |
| 202 |
#endif |
| 203 |
|
| 204 |
if (!CPLTestBool(CSLFetchNameValueDef(m_papszOptions, |
| 205 |
"COPY_SEED_FILE_COLOR_TABLE", "NO"))) |
| 206 |
{ |
| 207 |
OdDgColorTablePtr colorTable = m_poDb->getColorTable(OdDg::kForWrite); |
| 208 |
if (!colorTable.isNull()) |
| 209 |
{ |
| 210 |
const ODCOLORREF *defColors = OdDgColorTable::defaultPalette(); |
| 211 |
OdArray<ODCOLORREF> palette; |
| 212 |
palette.insert(palette.begin(), defColors, defColors + 256); |
| 213 |
colorTable->setPalette(palette); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
OdDgModelTablePtr pModelTable = m_poDb->getModelTable(); |
| 218 |
|
| 219 |
if (CPLTestBool(CSLFetchNameValueDef(m_papszOptions, "COPY_SEED_FILE_MODEL", |
| 220 |
"YES"))) |
| 221 |
{ |
| 222 |
if (!pModelTable.isNull()) |
| 223 |
{ |
| 224 |
OdDgElementIteratorPtr pIter = pModelTable->createIterator(); |
| 225 |
for (; !pIter.isNull() && !pIter->done(); pIter->step()) |
| 226 |
{ |
| 227 |
OdDgModelPtr pModel = |
| 228 |
OdDgModel::cast(pIter->item().openObject(OdDg::kForWrite)); |
| 229 |
if (!pModel.isNull()) |
| 230 |
{ |
| 231 |
OdDgElementIteratorPtr pIter2 = |
| 232 |
pModel->createGraphicsElementsIterator(); |
| 233 |
for (; !pIter2.isNull() && !pIter2->done(); pIter2->step()) |
| 234 |
{ |
| 235 |
OdDgElementPtr pElement = |
| 236 |
pIter2->item().openObject(OdDg::kForWrite); |
| 237 |
if (!pElement.isNull()) |
| 238 |
{ |
| 239 |
pElement->erase(true); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
if (!CPLTestBool(CSLFetchNameValueDef( |
| 244 |
m_papszOptions, |
| 245 |
"COPY_SEED_FILE_MODEL_CONTROL_ELEMENTS", "YES"))) |
| 246 |
{ |
| 247 |
pIter2 = pModel->createControlElementsIterator(); |
| 248 |
for (; !pIter2.isNull() && !pIter2->done(); |
| 249 |
pIter2->step()) |
| 250 |
{ |
| 251 |
OdDgElementPtr pElement = |
| 252 |
pIter2->item().openObject(OdDg::kForWrite); |
| 253 |
if (!pElement.isNull()) |
| 254 |
{ |
| 255 |
pElement->erase(true); |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
else |
| 264 |
{ |
| 265 |
// Erase existing models |
| 266 |
EraseSubElements(pModelTable); |
| 267 |
|
| 268 |
// Recreate a new model and bind it as default |
| 269 |
OdDgModelPtr model = OdDgModel::createObject(); |
| 270 |
pModelTable->add(model); |
| 271 |
|
| 272 |
m_poDb->setActiveModelId(model->elementId()); |
| 273 |
m_poDb->setDefaultModelId(model->elementId()); |
| 274 |
|
| 275 |
// Erase existing views |
| 276 |
EraseSubElements(m_poDb->getNamedViewTable()); |
| 277 |
OdDgViewGroupTablePtr pViewGroupTable = m_poDb->getViewGroupTable(); |
| 278 |
EraseSubElements(pViewGroupTable); |
| 279 |
|
| 280 |
// Recreate a new view group and bind it as default |
| 281 |
model->createViewGroup(); |
| 282 |
|
| 283 |
OdDgElementIteratorPtr pIter = pViewGroupTable->createIterator(); |
| 284 |
m_poDb->setActiveViewGroupId(pIter->item()); |
| 285 |
} |
| 286 |
|
| 287 |
#if 0 |
| 288 |
CPLString osTmpFile(CPLString(GetDescription()) + ".tmp"); |
| 289 |
OdString odTmpFile( FromUTF8( osTmpFile ) ); |
| 290 |
m_poDb->writeFile( odTmpFile ); |
| 291 |
m_poDb = m_poServices->readFile( odTmpFile ); |
| 292 |
VSIUnlink(osTmpFile); |
| 293 |
#endif |
| 294 |
} |
| 295 |
|
| 296 |
/************************************************************************/ |
| 297 |
/* FillMD() */ |
| 298 |
/************************************************************************/ |
| 299 |
|
| 300 |
static void FillMD(CPLStringList &osDGNMD, const char *pszKey, OdString str) |
| 301 |
{ |
| 302 |
CPLString osVal(OGRDGNV8DataSource::ToUTF8(str)); |
| 303 |
if (!osVal.empty()) |
| 304 |
osDGNMD.SetNameValue(pszKey, osVal); |
| 305 |
} |
| 306 |
|
| 307 |
/************************************************************************/ |
| 308 |
/* GetMetadataDomainList() */ |
| 309 |
/************************************************************************/ |
| 310 |
|
| 311 |
char **OGRDGNV8DataSource::GetMetadataDomainList() |
| 312 |
{ |
| 313 |
return BuildMetadataDomainList(GDALDataset::GetMetadataDomainList(), TRUE, |
| 314 |
"DGN", nullptr); |
| 315 |
} |
| 316 |
|
| 317 |
/************************************************************************/ |
| 318 |
/* GetMetadata() */ |
| 319 |
/************************************************************************/ |
| 320 |
|
| 321 |
CSLConstList OGRDGNV8DataSource::GetMetadata(const char *pszDomain) |
| 322 |
{ |
| 323 |
if (pszDomain != nullptr && EQUAL(pszDomain, "DGN")) |
| 324 |
{ |
| 325 |
m_osDGNMD.Clear(); |
| 326 |
OdDgSummaryInformationPtr summary = oddgGetSummaryInformation(m_poDb); |
| 327 |
FillMD(m_osDGNMD, "APPLICATION", summary->getApplicationName()); |
| 328 |
FillMD(m_osDGNMD, "TITLE", summary->getTitle()); |
| 329 |
FillMD(m_osDGNMD, "SUBJECT", summary->getSubject()); |
| 330 |
FillMD(m_osDGNMD, "AUTHOR", summary->getAuthor()); |
| 331 |
FillMD(m_osDGNMD, "KEYWORDS", summary->getKeywords()); |
| 332 |
FillMD(m_osDGNMD, "TEMPLATE", summary->getTemplate()); |
| 333 |
FillMD(m_osDGNMD, "COMMENTS", summary->getComments()); |
| 334 |
FillMD(m_osDGNMD, "LAST_SAVED_BY", summary->getLastSavedBy()); |
| 335 |
FillMD(m_osDGNMD, "REVISION_NUMBER", summary->getRevisionNumber()); |
| 336 |
OdDgDocumentSummaryInformationPtr docSummaryInfo = |
| 337 |
oddgGetDocumentSummaryInformation(m_poDb); |
| 338 |
FillMD(m_osDGNMD, "CATEGORY", docSummaryInfo->getCategory()); |
| 339 |
FillMD(m_osDGNMD, "MANAGER", docSummaryInfo->getManager()); |
| 340 |
FillMD(m_osDGNMD, "COMPANY", docSummaryInfo->getCompany()); |
| 341 |
return m_osDGNMD.List(); |
| 342 |
} |
| 343 |
return GDALDataset::GetMetadata(pszDomain); |
| 344 |
} |
| 345 |
|
| 346 |
/************************************************************************/ |
| 347 |
/* GetMetadataItem() */ |
| 348 |
/************************************************************************/ |
| 349 |
|
| 350 |
const char *OGRDGNV8DataSource::GetMetadataItem(const char *pszName, |
| 351 |
const char *pszDomain) |
| 352 |
{ |
| 353 |
return CSLFetchNameValue(GetMetadata(pszDomain), pszName); |
| 354 |
} |
| 355 |
|
| 356 |
/************************************************************************/ |
| 357 |
/* PreCreate() */ |
| 358 |
/* */ |
| 359 |
/* Called by OGRDGNV8DriverCreate() method to setup a stub */ |
| 360 |
/* OGRDataSource object without the associated file created */ |
| 361 |
/* yet. */ |
| 362 |
/************************************************************************/ |
| 363 |
|
| 364 |
bool OGRDGNV8DataSource::PreCreate(const char *pszFilename, |
| 365 |
CSLConstList papszOptionsIn) |
| 366 |
|
| 367 |
{ |
| 368 |
m_bUpdate = true; |
| 369 |
m_bModified = true; |
| 370 |
m_papszOptions = CSLDuplicate(papszOptionsIn); |
| 371 |
SetDescription(pszFilename); |
| 372 |
|
| 373 |
VSILFILE *fp = VSIFOpenL(pszFilename, "wb"); |
| 374 |
if (fp == nullptr) |
| 375 |
{ |
| 376 |
CPLError(CE_Failure, CPLE_FileIO, "Cannot write %s", pszFilename); |
| 377 |
return false; |
| 378 |
} |
| 379 |
VSIFCloseL(fp); |
| 380 |
|
| 381 |
const char *pszSeed = CSLFetchNameValue(m_papszOptions, "SEED"); |
| 382 |
|
| 383 |
try |
| 384 |
{ |
| 385 |
if (pszSeed) |
| 386 |
m_poDb = m_poServices->readFile(FromUTF8(pszSeed)); |
| 387 |
else |
| 388 |
m_poDb = m_poServices->createDatabase(); |
| 389 |
|
| 390 |
if (pszSeed) |
| 391 |
{ |
| 392 |
InitWithSeed(); |
| 393 |
} |
| 394 |
} |
| 395 |
catch (const OdError &e) |
| 396 |
{ |
| 397 |
CPLError(CE_Failure, CPLE_AppDefined, "Teigha DGN error occurred: %s", |
| 398 |
ToUTF8(e.description()).c_str()); |
| 399 |
return false; |
| 400 |
} |
| 401 |
catch (const std::exception &exc) |
| 402 |
{ |
| 403 |
CPLError(CE_Failure, CPLE_AppDefined, "std::exception occurred: %s", |
| 404 |
exc.what()); |
| 405 |
return false; |
| 406 |
} |
| 407 |
catch (...) |
| 408 |
{ |
| 409 |
CPLError(CE_Failure, CPLE_AppDefined, "Unknown exception occurred"); |
| 410 |
return false; |
| 411 |
} |
| 412 |
|
| 413 |
OdDgSummaryInformationPtr summary = oddgGetSummaryInformation(m_poDb); |
| 414 |
CPLString osDefaultAppName("GDAL "); |
| 415 |
osDefaultAppName += GDALVersionInfo("RELEASE_NAME"); |
| 416 |
osDefaultAppName += " with " + ToUTF8(summary->getApplicationName()); |
| 417 |
const char *pszAppName = CSLFetchNameValue(m_papszOptions, "APPLICATION"); |
| 418 |
if (pszSeed == nullptr && pszAppName == nullptr) |
| 419 |
pszAppName = osDefaultAppName; |
| 420 |
if (pszAppName) |
| 421 |
summary->setApplicationName(FromUTF8(pszAppName)); |
| 422 |
|
| 423 |
const char *pszTitle = CSLFetchNameValue(m_papszOptions, "TITLE"); |
| 424 |
if (pszTitle) |
| 425 |
summary->setTitle(FromUTF8(pszTitle)); |
| 426 |
|
| 427 |
const char *pszSubject = CSLFetchNameValue(m_papszOptions, "SUBJECT"); |
| 428 |
if (pszSubject) |
| 429 |
summary->setSubject(FromUTF8(pszSubject)); |
| 430 |
|
| 431 |
const char *pszAuthor = CSLFetchNameValue(m_papszOptions, "AUTHOR"); |
| 432 |
if (pszAuthor) |
| 433 |
summary->setAuthor(FromUTF8(pszAuthor)); |
| 434 |
|
| 435 |
const char *pszKeywords = CSLFetchNameValue(m_papszOptions, "KEYWORDS"); |
| 436 |
if (pszKeywords) |
| 437 |
summary->setKeywords(FromUTF8(pszKeywords)); |
| 438 |
|
| 439 |
const char *pszTemplate = CSLFetchNameValue(m_papszOptions, "TEMPLATE"); |
| 440 |
if (pszTemplate) |
| 441 |
summary->setTemplate(FromUTF8(pszTemplate)); |
| 442 |
|
| 443 |
const char *pszComments = CSLFetchNameValue(m_papszOptions, "COMMENTS"); |
| 444 |
if (pszComments) |
| 445 |
summary->setComments(FromUTF8(pszComments)); |
| 446 |
|
| 447 |
const char *pszLastSavedBy = |
| 448 |
CSLFetchNameValue(m_papszOptions, "LAST_SAVED_BY"); |
| 449 |
if (pszLastSavedBy) |
| 450 |
summary->setLastSavedBy(FromUTF8(pszLastSavedBy)); |
| 451 |
|
| 452 |
const char *pszRevisionNumber = |
| 453 |
CSLFetchNameValue(m_papszOptions, "REVISION_NUMBER"); |
| 454 |
if (pszRevisionNumber) |
| 455 |
summary->setRevisionNumber(FromUTF8(pszRevisionNumber)); |
| 456 |
|
| 457 |
OdDgDocumentSummaryInformationPtr docSummaryInfo = |
| 458 |
oddgGetDocumentSummaryInformation(m_poDb); |
| 459 |
|
| 460 |
const char *pszCategory = CSLFetchNameValue(m_papszOptions, "CATEGORY"); |
| 461 |
if (pszCategory) |
| 462 |
docSummaryInfo->setCategory(FromUTF8(pszCategory)); |
| 463 |
|
| 464 |
const char *pszManager = CSLFetchNameValue(m_papszOptions, "MANAGER"); |
| 465 |
if (pszManager) |
| 466 |
docSummaryInfo->setManager(FromUTF8(pszManager)); |
| 467 |
|
| 468 |
const char *pszCompany = CSLFetchNameValue(m_papszOptions, "COMPANY"); |
| 469 |
if (pszCompany) |
| 470 |
docSummaryInfo->setCompany(FromUTF8(pszCompany)); |
| 471 |
|
| 472 |
return true; |
| 473 |
} |
| 474 |
|
| 475 |
/************************************************************************/ |
| 476 |
/* ToUTF8() */ |
| 477 |
/************************************************************************/ |
| 478 |
|
| 479 |
CPLString OGRDGNV8DataSource::ToUTF8(const OdString &str) |
| 480 |
{ |
| 481 |
CPL_STATIC_ASSERT(sizeof(OdChar) == sizeof(wchar_t)); |
| 482 |
char *pszUTF8 = |
| 483 |
CPLRecodeFromWChar(reinterpret_cast<const wchar_t *>(str.c_str()), |
| 484 |
"WCHAR_T", CPL_ENC_UTF8); |
| 485 |
CPLString osRet(pszUTF8); |
| 486 |
CPLFree(pszUTF8); |
| 487 |
return osRet; |
| 488 |
} |
| 489 |
|
| 490 |
/************************************************************************/ |
| 491 |
/* FromUTF8() */ |
| 492 |
/************************************************************************/ |
| 493 |
|
| 494 |
OdString OGRDGNV8DataSource::FromUTF8(const CPLString &str) |
| 495 |
{ |
| 496 |
CPL_STATIC_ASSERT(sizeof(OdChar) == sizeof(wchar_t)); |
| 497 |
OdChar *pwszWide = reinterpret_cast<OdChar *>( |
| 498 |
CPLRecodeToWChar(str.c_str(), CPL_ENC_UTF8, "WCHAR_T")); |
| 499 |
OdString osRet(pwszWide); |
| 500 |
CPLFree(pwszWide); |
| 501 |
return osRet; |
| 502 |
} |
| 503 |
|
| 504 |
/************************************************************************/ |
| 505 |
/* ICreateLayer() */ |
| 506 |
/************************************************************************/ |
| 507 |
|
| 508 |
OGRLayer * |
| 509 |
OGRDGNV8DataSource::ICreateLayer(const char *pszLayerName, |
| 510 |
const OGRGeomFieldDefn * /*poGeomFieldDefn*/, |
| 511 |
CSLConstList papszOptions) |
| 512 |
|
| 513 |
{ |
| 514 |
if (!m_bUpdate) |
| 515 |
{ |
| 516 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 517 |
"CreateLayer() only supported on update mode."); |
| 518 |
return nullptr; |
| 519 |
} |
| 520 |
|
| 521 |
OdDgModelPtr model; |
| 522 |
try |
| 523 |
{ |
| 524 |
OdDgModelTablePtr pModelTable = m_poDb->getModelTable(OdDg::kForWrite); |
| 525 |
// First try to find a model that matches the layer name (case of a seed |
| 526 |
// file) |
| 527 |
OdDgElementIteratorPtr pIter = pModelTable->createIterator(); |
| 528 |
for (; !pIter->done(); pIter->step()) |
| 529 |
{ |
| 530 |
OdDgModelPtr pModel = |
| 531 |
OdDgModel::cast(pIter->item().openObject(OdDg::kForWrite)); |
| 532 |
if (!pModel.isNull()) |
| 533 |
{ |
| 534 |
if (ToUTF8(pModel->getName()) == CPLString(pszLayerName)) |
| 535 |
{ |
| 536 |
model = pModel; |
| 537 |
break; |
| 538 |
} |
| 539 |
} |
| 540 |
} |
| 541 |
// If we don't find a match, but there's at least one model, pick |
| 542 |
// the default one |
| 543 |
if (model.isNull() && m_nLayers == 0) |
| 544 |
model = m_poDb->getActiveModelId().openObject(OdDg::kForWrite); |
| 545 |
if (model.isNull()) |
| 546 |
{ |
| 547 |
model = OdDgModel::createObject(); |
| 548 |
pModelTable->add(model); |
| 549 |
} |
| 550 |
|
| 551 |
const char *pszDim = CSLFetchNameValue(papszOptions, "DIM"); |
| 552 |
if (pszDim != nullptr) |
| 553 |
{ |
| 554 |
model->setModelIs3dFlag(EQUAL(pszDim, "3")); |
| 555 |
} |
| 556 |
|
| 557 |
model->setWorkingUnit(OdDgModel::kWuMasterUnit); |
| 558 |
|
| 559 |
model->setName(FromUTF8(pszLayerName)); |
| 560 |
|
| 561 |
const char *pszDescription = |
| 562 |
CSLFetchNameValue(papszOptions, "DESCRIPTION"); |
| 563 |
if (pszDescription) |
| 564 |
model->setDescription(FromUTF8(pszDescription)); |
| 565 |
} |
| 566 |
catch (const OdError &e) |
| 567 |
{ |
| 568 |
CPLError(CE_Failure, CPLE_AppDefined, "Teigha DGN error occurred: %s", |
| 569 |
ToUTF8(e.description()).c_str()); |
| 570 |
return nullptr; |
| 571 |
} |
| 572 |
catch (const std::exception &exc) |
| 573 |
{ |
| 574 |
CPLError(CE_Failure, CPLE_AppDefined, "std::exception occurred: %s", |
| 575 |
exc.what()); |
| 576 |
return nullptr; |
| 577 |
} |
| 578 |
catch (...) |
| 579 |
{ |
| 580 |
CPLError(CE_Failure, CPLE_AppDefined, "Unknown exception occurred"); |
| 581 |
return nullptr; |
| 582 |
} |
| 583 |
|
| 584 |
m_bModified = true; |
| 585 |
|
| 586 |
OGRDGNV8Layer *poLayer = new OGRDGNV8Layer(this, model); |
| 587 |
m_papoLayers = static_cast<OGRDGNV8Layer **>( |
| 588 |
CPLRealloc(m_papoLayers, sizeof(OGRDGNV8Layer *) * (m_nLayers + 1))); |
| 589 |
m_papoLayers[m_nLayers++] = poLayer; |
| 590 |
return poLayer; |
| 591 |
} |