| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Project: DWG Translator |
| 4 |
* Purpose: Implements OGRDWGLayer class. |
| 5 |
* Author: Frank Warmerdam, [email protected] |
| 6 |
* |
| 7 |
****************************************************************************** |
| 8 |
* Copyright (c) 2011, Frank Warmerdam <[email protected]> |
| 9 |
* |
| 10 |
* SPDX-License-Identifier: MIT |
| 11 |
****************************************************************************/ |
| 12 |
|
| 13 |
#include "ogr_dwg.h" |
| 14 |
#include "cpl_conv.h" |
| 15 |
|
| 16 |
#include "ogrdxf_polyline_smooth.h" |
| 17 |
|
| 18 |
/************************************************************************/ |
| 19 |
/* OGRDWGLayer() */ |
| 20 |
/************************************************************************/ |
| 21 |
|
| 22 |
OGRDWGLayer::OGRDWGLayer(OGRDWGDataSource *poDSIn) |
| 23 |
|
| 24 |
{ |
| 25 |
this->poDS = poDSIn; |
| 26 |
|
| 27 |
iNextFID = 0; |
| 28 |
|
| 29 |
poFeatureDefn = new OGRFeatureDefn("entities"); |
| 30 |
SetDescription(poFeatureDefn->GetName()); |
| 31 |
poFeatureDefn->Reference(); |
| 32 |
|
| 33 |
poDS->AddStandardFields(poFeatureDefn); |
| 34 |
|
| 35 |
if (!poDS->InlineBlocks()) |
| 36 |
{ |
| 37 |
OGRFieldDefn oScaleField("BlockScale", OFTRealList); |
| 38 |
poFeatureDefn->AddFieldDefn(&oScaleField); |
| 39 |
|
| 40 |
OGRFieldDefn oBlockAngleField("BlockAngle", OFTReal); |
| 41 |
poFeatureDefn->AddFieldDefn(&oBlockAngleField); |
| 42 |
} |
| 43 |
|
| 44 |
if (!poDS->GetAttributes().empty()) |
| 45 |
{ |
| 46 |
std::set<CPLString>::iterator it; |
| 47 |
for (it = poDS->GetAttributes().begin(); |
| 48 |
it != poDS->GetAttributes().end(); ++it) |
| 49 |
{ |
| 50 |
OGRFieldDefn oAttField(*it, OFTString); |
| 51 |
poFeatureDefn->AddFieldDefn(&oAttField); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/* -------------------------------------------------------------------- */ |
| 56 |
/* Find the *Paper_Space block, which seems to contain all the */ |
| 57 |
/* regular entities. */ |
| 58 |
/* -------------------------------------------------------------------- */ |
| 59 |
OdDbBlockTablePtr pTable = |
| 60 |
poDS->GetDB()->getBlockTableId().safeOpenObject(); |
| 61 |
OdDbSymbolTableIteratorPtr pBlkIter = pTable->newIterator(); |
| 62 |
|
| 63 |
for (pBlkIter->start(); !pBlkIter->done(); pBlkIter->step()) |
| 64 |
{ |
| 65 |
m_poBlock = pBlkIter->getRecordId().safeOpenObject(); |
| 66 |
|
| 67 |
if (EQUAL(m_poBlock->getName(), "*Model_Space")) |
| 68 |
break; |
| 69 |
else |
| 70 |
m_poBlock = nullptr; |
| 71 |
} |
| 72 |
|
| 73 |
OGRDWGLayer::ResetReading(); |
| 74 |
} |
| 75 |
|
| 76 |
/************************************************************************/ |
| 77 |
/* ~OGRDWGLayer() */ |
| 78 |
/************************************************************************/ |
| 79 |
|
| 80 |
OGRDWGLayer::~OGRDWGLayer() |
| 81 |
|
| 82 |
{ |
| 83 |
ClearPendingFeatures(); |
| 84 |
if (m_nFeaturesRead > 0 && poFeatureDefn != nullptr) |
| 85 |
{ |
| 86 |
CPLDebug("DWG", "%d features read on layer '%s'.", (int)m_nFeaturesRead, |
| 87 |
poFeatureDefn->GetName()); |
| 88 |
} |
| 89 |
|
| 90 |
if (poFeatureDefn) |
| 91 |
poFeatureDefn->Release(); |
| 92 |
} |
| 93 |
|
| 94 |
/************************************************************************/ |
| 95 |
/* TextUnescape() */ |
| 96 |
/************************************************************************/ |
| 97 |
|
| 98 |
CPLString OGRDWGLayer::TextUnescape(OdString oString, bool bIsMText) |
| 99 |
|
| 100 |
{ |
| 101 |
return ACTextUnescape((const char *)oString, poDS->GetEncoding(), bIsMText); |
| 102 |
} |
| 103 |
|
| 104 |
/************************************************************************/ |
| 105 |
/* SetBlockTable() */ |
| 106 |
/* */ |
| 107 |
/* Set what block table to read features from. This layer */ |
| 108 |
/* object is used to read blocks features as well as generic */ |
| 109 |
/* entities. */ |
| 110 |
/************************************************************************/ |
| 111 |
|
| 112 |
void OGRDWGLayer::SetBlockTable(OdDbBlockTableRecordPtr poNewBlock) |
| 113 |
|
| 114 |
{ |
| 115 |
m_poBlock = poNewBlock; |
| 116 |
|
| 117 |
ResetReading(); |
| 118 |
} |
| 119 |
|
| 120 |
/************************************************************************/ |
| 121 |
/* ClearPendingFeatures() */ |
| 122 |
/************************************************************************/ |
| 123 |
|
| 124 |
void OGRDWGLayer::ClearPendingFeatures() |
| 125 |
|
| 126 |
{ |
| 127 |
while (!apoPendingFeatures.empty()) |
| 128 |
{ |
| 129 |
delete apoPendingFeatures.front(); |
| 130 |
apoPendingFeatures.pop(); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/************************************************************************/ |
| 135 |
/* ResetReading() */ |
| 136 |
/************************************************************************/ |
| 137 |
|
| 138 |
void OGRDWGLayer::ResetReading() |
| 139 |
|
| 140 |
{ |
| 141 |
iNextFID = 0; |
| 142 |
ClearPendingFeatures(); |
| 143 |
|
| 144 |
if (!m_poBlock.isNull()) |
| 145 |
poEntIter = m_poBlock->newIterator(); |
| 146 |
} |
| 147 |
|
| 148 |
/************************************************************************/ |
| 149 |
/* TranslateGenericProperties() */ |
| 150 |
/* */ |
| 151 |
/* Try and convert entity properties handled similarly for most */ |
| 152 |
/* or all entity types */ |
| 153 |
/************************************************************************/ |
| 154 |
|
| 155 |
void OGRDWGLayer::TranslateGenericProperties(OGRFeature *poFeature, |
| 156 |
OdDbEntityPtr poEntity) |
| 157 |
|
| 158 |
{ |
| 159 |
poFeature->SetField("Layer", TextUnescape(poEntity->layer(), false)); |
| 160 |
poFeature->SetField("Linetype", TextUnescape(poEntity->layer(), false)); |
| 161 |
|
| 162 |
CPLString osValue; |
| 163 |
osValue.Printf("%d", (int)poEntity->lineWeight()); |
| 164 |
oStyleProperties["LineWeight"] = osValue; |
| 165 |
|
| 166 |
OdDbHandle oHandle = poEntity->getDbHandle(); |
| 167 |
poFeature->SetField("EntityHandle", (const char *)oHandle.ascii()); |
| 168 |
|
| 169 |
if (poEntity->colorIndex() != 256) |
| 170 |
{ |
| 171 |
osValue.Printf("%d", poEntity->colorIndex()); |
| 172 |
oStyleProperties["Color"] = osValue.c_str(); |
| 173 |
} |
| 174 |
|
| 175 |
/* -------------------------------------------------------------------- */ |
| 176 |
/* Collect the subclasses. */ |
| 177 |
/* -------------------------------------------------------------------- */ |
| 178 |
CPLString osSubClasses; |
| 179 |
OdRxClass *poClass = poEntity->isA(); |
| 180 |
|
| 181 |
while (poClass != nullptr) |
| 182 |
{ |
| 183 |
if (!osSubClasses.empty()) |
| 184 |
osSubClasses = ":" + osSubClasses; |
| 185 |
|
| 186 |
osSubClasses = ((const char *)poClass->name()) + osSubClasses; |
| 187 |
if (EQUAL(poClass->name(), "AcDbEntity")) |
| 188 |
break; |
| 189 |
|
| 190 |
poClass = poClass->myParent(); |
| 191 |
} |
| 192 |
|
| 193 |
poFeature->SetField("SubClasses", osSubClasses.c_str()); |
| 194 |
|
| 195 |
/* -------------------------------------------------------------------- */ |
| 196 |
/* Collect Xdata. */ |
| 197 |
/* -------------------------------------------------------------------- */ |
| 198 |
OdResBufPtr poResBufBase = poEntity->xData(); |
| 199 |
OdResBuf *poResBuf = poResBufBase; |
| 200 |
CPLString osFullXData; |
| 201 |
|
| 202 |
for (; poResBuf != nullptr; poResBuf = poResBuf->next()) |
| 203 |
{ |
| 204 |
CPLString osXDataItem; |
| 205 |
|
| 206 |
switch (OdDxfCode::_getType(poResBuf->restype())) |
| 207 |
{ |
| 208 |
case OdDxfCode::Name: |
| 209 |
case OdDxfCode::String: |
| 210 |
case OdDxfCode::LayerName: |
| 211 |
osXDataItem = (const char *)poResBuf->getString(); |
| 212 |
break; |
| 213 |
|
| 214 |
case OdDxfCode::Bool: |
| 215 |
if (poResBuf->getBool()) |
| 216 |
osXDataItem = "true"; |
| 217 |
else |
| 218 |
osXDataItem = "false"; |
| 219 |
break; |
| 220 |
|
| 221 |
case OdDxfCode::Integer8: |
| 222 |
osXDataItem.Printf("%d", (int)poResBuf->getInt8()); |
| 223 |
break; |
| 224 |
|
| 225 |
case OdDxfCode::Integer16: |
| 226 |
osXDataItem.Printf("%d", (int)poResBuf->getInt16()); |
| 227 |
break; |
| 228 |
|
| 229 |
case OdDxfCode::Integer32: |
| 230 |
osXDataItem.Printf("%d", (int)poResBuf->getInt32()); |
| 231 |
break; |
| 232 |
|
| 233 |
case OdDxfCode::Double: |
| 234 |
case OdDxfCode::Angle: |
| 235 |
osXDataItem.Printf("%g", poResBuf->getDouble()); |
| 236 |
break; |
| 237 |
|
| 238 |
case OdDxfCode::Point: |
| 239 |
{ |
| 240 |
OdGePoint3d oPoint = poResBuf->getPoint3d(); |
| 241 |
osXDataItem.Printf("(%g,%g,%g)", oPoint.x, oPoint.y, oPoint.z); |
| 242 |
} |
| 243 |
break; |
| 244 |
|
| 245 |
case OdDxfCode::BinaryChunk: |
| 246 |
{ |
| 247 |
OdBinaryData oBinData = poResBuf->getBinaryChunk(); |
| 248 |
char *pszAsHex = CPLBinaryToHex(oBinData.size(), |
| 249 |
(GByte *)oBinData.asArrayPtr()); |
| 250 |
osXDataItem = pszAsHex; |
| 251 |
CPLFree(pszAsHex); |
| 252 |
} |
| 253 |
break; |
| 254 |
|
| 255 |
case OdDxfCode::ObjectId: |
| 256 |
case OdDxfCode::SoftPointerId: |
| 257 |
case OdDxfCode::HardPointerId: |
| 258 |
case OdDxfCode::SoftOwnershipId: |
| 259 |
case OdDxfCode::HardOwnershipId: |
| 260 |
case OdDxfCode::Handle: |
| 261 |
osXDataItem = (const char *)poResBuf->getHandle().ascii(); |
| 262 |
break; |
| 263 |
|
| 264 |
default: |
| 265 |
break; |
| 266 |
} |
| 267 |
|
| 268 |
if (!osFullXData.empty()) |
| 269 |
osFullXData += " "; |
| 270 |
osFullXData += (const char *)osXDataItem; |
| 271 |
} |
| 272 |
|
| 273 |
poFeature->SetField("ExtendedEntity", osFullXData); |
| 274 |
|
| 275 |
#ifdef notdef |
| 276 |
// OCS vector. |
| 277 |
case 210: |
| 278 |
oStyleProperties["210_N.dX"] = pszValue; |
| 279 |
break; |
| 280 |
|
| 281 |
case 220: |
| 282 |
oStyleProperties["220_N.dY"] = pszValue; |
| 283 |
break; |
| 284 |
|
| 285 |
case 230: |
| 286 |
oStyleProperties["230_N.dZ"] = pszValue; |
| 287 |
break; |
| 288 |
|
| 289 |
default: |
| 290 |
break; |
| 291 |
} |
| 292 |
#endif |
| 293 |
} |
| 294 |
|
| 295 |
/************************************************************************/ |
| 296 |
/* PrepareLineStyle() */ |
| 297 |
/************************************************************************/ |
| 298 |
|
| 299 |
void OGRDWGLayer::PrepareLineStyle(OGRFeature *poFeature) |
| 300 |
|
| 301 |
{ |
| 302 |
CPLString osLayer = poFeature->GetFieldAsString("Layer"); |
| 303 |
|
| 304 |
/* -------------------------------------------------------------------- */ |
| 305 |
/* Is the layer disabled/hidden/frozen/off? */ |
| 306 |
/* -------------------------------------------------------------------- */ |
| 307 |
int bHidden = EQUAL(poDS->LookupLayerProperty(osLayer, "Hidden"), "1"); |
| 308 |
|
| 309 |
/* -------------------------------------------------------------------- */ |
| 310 |
/* Work out the color for this feature. */ |
| 311 |
/* -------------------------------------------------------------------- */ |
| 312 |
int nColor = 256; |
| 313 |
|
| 314 |
if (oStyleProperties.count("Color") > 0) |
| 315 |
nColor = atoi(oStyleProperties["Color"]); |
| 316 |
|
| 317 |
// Use layer color? |
| 318 |
if (nColor < 1 || nColor > 255) |
| 319 |
{ |
| 320 |
const char *pszValue = poDS->LookupLayerProperty(osLayer, "Color"); |
| 321 |
if (pszValue != nullptr) |
| 322 |
nColor = atoi(pszValue); |
| 323 |
} |
| 324 |
|
| 325 |
if (nColor < 1 || nColor > 255) |
| 326 |
return; |
| 327 |
|
| 328 |
/* -------------------------------------------------------------------- */ |
| 329 |
/* Get line weight if available. */ |
| 330 |
/* -------------------------------------------------------------------- */ |
| 331 |
double dfWeight = 0.0; |
| 332 |
|
| 333 |
if (oStyleProperties.count("LineWeight") > 0) |
| 334 |
{ |
| 335 |
CPLString osWeight = oStyleProperties["LineWeight"]; |
| 336 |
|
| 337 |
if (osWeight == "-1") |
| 338 |
osWeight = poDS->LookupLayerProperty(osLayer, "LineWeight"); |
| 339 |
|
| 340 |
dfWeight = CPLAtof(osWeight) / 100.0; |
| 341 |
} |
| 342 |
|
| 343 |
/* -------------------------------------------------------------------- */ |
| 344 |
/* Do we have a dash/dot line style? */ |
| 345 |
/* -------------------------------------------------------------------- */ |
| 346 |
const char *pszPattern = |
| 347 |
poDS->LookupLineType(poFeature->GetFieldAsString("Linetype")); |
| 348 |
|
| 349 |
/* -------------------------------------------------------------------- */ |
| 350 |
/* Format the style string. */ |
| 351 |
/* -------------------------------------------------------------------- */ |
| 352 |
CPLString osStyle; |
| 353 |
const unsigned char *pabyDWGColors = ACGetColorTable(); |
| 354 |
|
| 355 |
osStyle.Printf("PEN(c:#%02x%02x%02x", pabyDWGColors[nColor * 3 + 0], |
| 356 |
pabyDWGColors[nColor * 3 + 1], |
| 357 |
pabyDWGColors[nColor * 3 + 2]); |
| 358 |
|
| 359 |
if (bHidden) |
| 360 |
osStyle += "00"; |
| 361 |
|
| 362 |
if (dfWeight > 0.0) |
| 363 |
{ |
| 364 |
char szBuffer[64]; |
| 365 |
CPLsnprintf(szBuffer, sizeof(szBuffer), "%.2g", dfWeight); |
| 366 |
char *pszComma = strchr(szBuffer, ','); |
| 367 |
if (pszComma) |
| 368 |
*pszComma = '.'; |
| 369 |
osStyle += CPLString().Printf(",w:%sg", szBuffer); |
| 370 |
} |
| 371 |
|
| 372 |
if (pszPattern) |
| 373 |
{ |
| 374 |
osStyle += ",p:\""; |
| 375 |
osStyle += pszPattern; |
| 376 |
osStyle += "\""; |
| 377 |
} |
| 378 |
|
| 379 |
osStyle += ")"; |
| 380 |
|
| 381 |
poFeature->SetStyleString(osStyle); |
| 382 |
} |
| 383 |
|
| 384 |
/************************************************************************/ |
| 385 |
/* TranslateMTEXT() */ |
| 386 |
/************************************************************************/ |
| 387 |
|
| 388 |
OGRFeature *OGRDWGLayer::TranslateMTEXT(OdDbEntityPtr poEntity) |
| 389 |
|
| 390 |
{ |
| 391 |
OdDbMTextPtr poMTE = OdDbMText::cast(poEntity); |
| 392 |
OGRFeature *poFeature = new OGRFeature(poFeatureDefn); |
| 393 |
|
| 394 |
TranslateGenericProperties(poFeature, poEntity); |
| 395 |
|
| 396 |
/* -------------------------------------------------------------------- */ |
| 397 |
/* Set the location. */ |
| 398 |
/* -------------------------------------------------------------------- */ |
| 399 |
OdGePoint3d oLocation = poMTE->location(); |
| 400 |
|
| 401 |
poFeature->SetGeometryDirectly( |
| 402 |
new OGRPoint(oLocation.x, oLocation.y, oLocation.z)); |
| 403 |
|
| 404 |
/* -------------------------------------------------------------------- */ |
| 405 |
/* Apply text after stripping off any extra terminating newline. */ |
| 406 |
/* -------------------------------------------------------------------- */ |
| 407 |
CPLString osText = TextUnescape(poMTE->contents(), true); |
| 408 |
|
| 409 |
if (!osText.empty() && osText.back() == '\n') |
| 410 |
osText.pop_back(); |
| 411 |
|
| 412 |
poFeature->SetField("Text", osText); |
| 413 |
|
| 414 |
/* -------------------------------------------------------------------- */ |
| 415 |
/* We need to escape double quotes with backslashes before they */ |
| 416 |
/* can be inserted in the style string. */ |
| 417 |
/* -------------------------------------------------------------------- */ |
| 418 |
if (strchr(osText, '"') != nullptr) |
| 419 |
{ |
| 420 |
CPLString osEscaped; |
| 421 |
size_t iC; |
| 422 |
|
| 423 |
for (iC = 0; iC < osText.size(); iC++) |
| 424 |
{ |
| 425 |
if (osText[iC] == '"') |
| 426 |
osEscaped += "\\\""; |
| 427 |
else |
| 428 |
osEscaped += osText[iC]; |
| 429 |
} |
| 430 |
osText = osEscaped; |
| 431 |
} |
| 432 |
|
| 433 |
/* -------------------------------------------------------------------- */ |
| 434 |
/* Work out the color for this feature. */ |
| 435 |
/* -------------------------------------------------------------------- */ |
| 436 |
int nColor = 256; |
| 437 |
|
| 438 |
if (oStyleProperties.count("Color") > 0) |
| 439 |
nColor = atoi(oStyleProperties["Color"]); |
| 440 |
|
| 441 |
// Use layer color? |
| 442 |
if (nColor < 1 || nColor > 255) |
| 443 |
{ |
| 444 |
CPLString osLayer = poFeature->GetFieldAsString("Layer"); |
| 445 |
const char *pszValue = poDS->LookupLayerProperty(osLayer, "Color"); |
| 446 |
if (pszValue != nullptr) |
| 447 |
nColor = atoi(pszValue); |
| 448 |
} |
| 449 |
|
| 450 |
/* -------------------------------------------------------------------- */ |
| 451 |
/* Prepare style string. */ |
| 452 |
/* -------------------------------------------------------------------- */ |
| 453 |
double dfAngle = poMTE->rotation() * 180 / M_PI; |
| 454 |
double dfHeight = poMTE->textHeight(); |
| 455 |
int nAttachmentPoint = (int)poMTE->attachment(); |
| 456 |
|
| 457 |
CPLString osStyle; |
| 458 |
char szBuffer[64]; |
| 459 |
char *pszComma = nullptr; |
| 460 |
|
| 461 |
osStyle.Printf("LABEL(f:\"Arial\",t:\"%s\"", osText.c_str()); |
| 462 |
|
| 463 |
if (dfAngle != 0.0) |
| 464 |
{ |
| 465 |
CPLsnprintf(szBuffer, sizeof(szBuffer), "%.3g", dfAngle); |
| 466 |
pszComma = strchr(szBuffer, ','); |
| 467 |
if (pszComma) |
| 468 |
*pszComma = '.'; |
| 469 |
osStyle += CPLString().Printf(",a:%s", szBuffer); |
| 470 |
} |
| 471 |
|
| 472 |
if (dfHeight != 0.0) |
| 473 |
{ |
| 474 |
CPLsnprintf(szBuffer, sizeof(szBuffer), "%.3g", dfHeight); |
| 475 |
pszComma = strchr(szBuffer, ','); |
| 476 |
if (pszComma) |
| 477 |
*pszComma = '.'; |
| 478 |
osStyle += CPLString().Printf(",s:%sg", szBuffer); |
| 479 |
} |
| 480 |
|
| 481 |
if (nAttachmentPoint >= 0 && nAttachmentPoint <= 9) |
| 482 |
{ |
| 483 |
const static int anAttachmentMap[10] = {-1, 7, 8, 9, 4, 5, 6, 1, 2, 3}; |
| 484 |
|
| 485 |
osStyle += |
| 486 |
CPLString().Printf(",p:%d", anAttachmentMap[nAttachmentPoint]); |
| 487 |
} |
| 488 |
|
| 489 |
if (nColor > 0 && nColor < 256) |
| 490 |
{ |
| 491 |
const unsigned char *pabyDWGColors = ACGetColorTable(); |
| 492 |
osStyle += CPLString().Printf( |
| 493 |
",c:#%02x%02x%02x", pabyDWGColors[nColor * 3 + 0], |
| 494 |
pabyDWGColors[nColor * 3 + 1], pabyDWGColors[nColor * 3 + 2]); |
| 495 |
} |
| 496 |
|
| 497 |
osStyle += ")"; |
| 498 |
|
| 499 |
poFeature->SetStyleString(osStyle); |
| 500 |
|
| 501 |
return poFeature; |
| 502 |
} |
| 503 |
|
| 504 |
/************************************************************************/ |
| 505 |
/* TranslateTEXT() */ |
| 506 |
/************************************************************************/ |
| 507 |
|
| 508 |
OGRFeature *OGRDWGLayer::TranslateTEXT(OdDbEntityPtr poEntity) |
| 509 |
|
| 510 |
{ |
| 511 |
OdDbTextPtr poText = OdDbText::cast(poEntity); |
| 512 |
OGRFeature *poFeature = new OGRFeature(poFeatureDefn); |
| 513 |
|
| 514 |
TranslateGenericProperties(poFeature, poEntity); |
| 515 |
|
| 516 |
/* -------------------------------------------------------------------- */ |
| 517 |
/* Set the location. */ |
| 518 |
/* -------------------------------------------------------------------- */ |
| 519 |
OdGePoint3d oLocation = poText->position(); |
| 520 |
|
| 521 |
poFeature->SetGeometryDirectly( |
| 522 |
new OGRPoint(oLocation.x, oLocation.y, oLocation.z)); |
| 523 |
|
| 524 |
/* -------------------------------------------------------------------- */ |
| 525 |
/* Apply text after stripping off any extra terminating newline. */ |
| 526 |
/* -------------------------------------------------------------------- */ |
| 527 |
CPLString osText = TextUnescape(poText->textString(), false); |
| 528 |
|
| 529 |
if (!osText.empty() && osText.back() == '\n') |
| 530 |
osText.pop_back(); |
| 531 |
|
| 532 |
poFeature->SetField("Text", osText); |
| 533 |
|
| 534 |
/* -------------------------------------------------------------------- */ |
| 535 |
/* We need to escape double quotes with backslashes before they */ |
| 536 |
/* can be inserted in the style string. */ |
| 537 |
/* -------------------------------------------------------------------- */ |
| 538 |
if (strchr(osText, '"') != nullptr) |
| 539 |
{ |
| 540 |
CPLString osEscaped; |
| 541 |
size_t iC; |
| 542 |
|
| 543 |
for (iC = 0; iC < osText.size(); iC++) |
| 544 |
{ |
| 545 |
if (osText[iC] == '"') |
| 546 |
osEscaped += "\\\""; |
| 547 |
else |
| 548 |
osEscaped += osText[iC]; |
| 549 |
} |
| 550 |
osText = osEscaped; |
| 551 |
} |
| 552 |
|
| 553 |
/* -------------------------------------------------------------------- */ |
| 554 |
/* Is the layer disabled/hidden/frozen/off? */ |
| 555 |
/* -------------------------------------------------------------------- */ |
| 556 |
CPLString osLayer = poFeature->GetFieldAsString("Layer"); |
| 557 |
|
| 558 |
int bHidden = EQUAL(poDS->LookupLayerProperty(osLayer, "Hidden"), "1"); |
| 559 |
|
| 560 |
/* -------------------------------------------------------------------- */ |
| 561 |
/* Work out the color for this feature. */ |
| 562 |
/* -------------------------------------------------------------------- */ |
| 563 |
int nColor = 256; |
| 564 |
|
| 565 |
if (oStyleProperties.count("Color") > 0) |
| 566 |
nColor = atoi(oStyleProperties["Color"]); |
| 567 |
|
| 568 |
// Use layer color? |
| 569 |
if (nColor < 1 || nColor > 255) |
| 570 |
{ |
| 571 |
const char *pszValue = poDS->LookupLayerProperty(osLayer, "Color"); |
| 572 |
if (pszValue != nullptr) |
| 573 |
nColor = atoi(pszValue); |
| 574 |
} |
| 575 |
|
| 576 |
if (nColor < 1 || nColor > 255) |
| 577 |
nColor = 8; |
| 578 |
|
| 579 |
/* -------------------------------------------------------------------- */ |
| 580 |
/* Prepare style string. */ |
| 581 |
/* -------------------------------------------------------------------- */ |
| 582 |
double dfAngle = poText->rotation() * 180 / M_PI; |
| 583 |
double dfHeight = poText->height(); |
| 584 |
|
| 585 |
CPLString osStyle; |
| 586 |
char szBuffer[64]; |
| 587 |
char *pszComma = nullptr; |
| 588 |
|
| 589 |
osStyle.Printf("LABEL(f:\"Arial\",t:\"%s\"", osText.c_str()); |
| 590 |
|
| 591 |
if (dfAngle != 0.0) |
| 592 |
{ |
| 593 |
CPLsnprintf(szBuffer, sizeof(szBuffer), "%.3g", dfAngle); |
| 594 |
pszComma = strchr(szBuffer, ','); |
| 595 |
if (pszComma) |
| 596 |
*pszComma = '.'; |
| 597 |
osStyle += CPLString().Printf(",a:%s", szBuffer); |
| 598 |
} |
| 599 |
|
| 600 |
if (dfHeight != 0.0) |
| 601 |
{ |
| 602 |
CPLsnprintf(szBuffer, sizeof(szBuffer), "%.3g", dfHeight); |
| 603 |
pszComma = strchr(szBuffer, ','); |
| 604 |
if (pszComma) |
| 605 |
*pszComma = '.'; |
| 606 |
osStyle += CPLString().Printf(",s:%sg", szBuffer); |
| 607 |
} |
| 608 |
|
| 609 |
const unsigned char *pabyDWGColors = ACGetColorTable(); |
| 610 |
|
| 611 |
snprintf(szBuffer, sizeof(szBuffer), ",c:#%02x%02x%02x", |
| 612 |
pabyDWGColors[nColor * 3 + 0], pabyDWGColors[nColor * 3 + 1], |
| 613 |
pabyDWGColors[nColor * 3 + 2]); |
| 614 |
osStyle += szBuffer; |
| 615 |
|
| 616 |
if (bHidden) |
| 617 |
osStyle += "00"; |
| 618 |
|
| 619 |
osStyle += ")"; |
| 620 |
|
| 621 |
poFeature->SetStyleString(osStyle); |
| 622 |
|
| 623 |
return poFeature; |
| 624 |
} |
| 625 |
|
| 626 |
/************************************************************************/ |
| 627 |
/* TranslatePOINT() */ |
| 628 |
/************************************************************************/ |
| 629 |
|
| 630 |
OGRFeature *OGRDWGLayer::TranslatePOINT(OdDbEntityPtr poEntity) |
| 631 |
|
| 632 |
{ |
| 633 |
OdDbPointPtr poPE = OdDbPoint::cast(poEntity); |
| 634 |
OGRFeature *poFeature = new OGRFeature(poFeatureDefn); |
| 635 |
|
| 636 |
TranslateGenericProperties(poFeature, poEntity); |
| 637 |
|
| 638 |
OdGePoint3d oPoint = poPE->position(); |
| 639 |
|
| 640 |
poFeature->SetGeometryDirectly(new OGRPoint(oPoint.x, oPoint.y, oPoint.z)); |
| 641 |
|
| 642 |
return poFeature; |
| 643 |
} |
| 644 |
|
| 645 |
/************************************************************************/ |
| 646 |
/* TranslateLWPOLYLINE() */ |
| 647 |
/************************************************************************/ |
| 648 |
|
| 649 |
OGRFeature *OGRDWGLayer::TranslateLWPOLYLINE(OdDbEntityPtr poEntity) |
| 650 |
|
| 651 |
{ |
| 652 |
OGRFeature *poFeature = new OGRFeature(poFeatureDefn); |
| 653 |
OdDbPolylinePtr poPL = OdDbPolyline::cast(poEntity); |
| 654 |
|
| 655 |
TranslateGenericProperties(poFeature, poEntity); |
| 656 |
|
| 657 |
/* -------------------------------------------------------------------- */ |
| 658 |
/* Collect polyline details. */ |
| 659 |
/* -------------------------------------------------------------------- */ |
| 660 |
DXFSmoothPolyline oSmoothPolyline; |
| 661 |
|
| 662 |
for (unsigned int i = 0; i < poPL->numVerts(); i++) |
| 663 |
{ |
| 664 |
OdGePoint3d oPoint; |
| 665 |
poPL->getPointAt(i, oPoint); |
| 666 |
|
| 667 |
oSmoothPolyline.AddPoint(oPoint.x, oPoint.y, 0.0, poPL->getBulgeAt(i)); |
| 668 |
} |
| 669 |
|
| 670 |
if (oSmoothPolyline.IsEmpty()) |
| 671 |
{ |
| 672 |
delete poFeature; |
| 673 |
return nullptr; |
| 674 |
} |
| 675 |
|
| 676 |
if (poPL->isClosed()) |
| 677 |
oSmoothPolyline.Close(); |
| 678 |
|
| 679 |
const bool bAsPolygon = poPL->isClosed() && poDS->ClosedLineAsPolygon(); |
| 680 |
|
| 681 |
poFeature->SetGeometryDirectly(oSmoothPolyline.Tessellate(bAsPolygon)); |
| 682 |
|
| 683 |
PrepareLineStyle(poFeature); |
| 684 |
|
| 685 |
return poFeature; |
| 686 |
} |
| 687 |
|
| 688 |
/************************************************************************/ |
| 689 |
/* Translate2DPOLYLINE() */ |
| 690 |
/************************************************************************/ |
| 691 |
|
| 692 |
OGRFeature *OGRDWGLayer::Translate2DPOLYLINE(OdDbEntityPtr poEntity) |
| 693 |
|
| 694 |
{ |
| 695 |
OGRFeature *poFeature = new OGRFeature(poFeatureDefn); |
| 696 |
OdDb2dPolylinePtr poPL = OdDb2dPolyline::cast(poEntity); |
| 697 |
|
| 698 |
TranslateGenericProperties(poFeature, poEntity); |
| 699 |
|
| 700 |
/* -------------------------------------------------------------------- */ |
| 701 |
/* Create a polyline geometry from the vertices. */ |
| 702 |
/* -------------------------------------------------------------------- */ |
| 703 |
OGRLineString *poLS = new OGRLineString(); |
| 704 |
OdDbObjectIteratorPtr poIter = poPL->vertexIterator(); |
| 705 |
|
| 706 |
while (!poIter->done()) |
| 707 |
{ |
| 708 |
OdDb2dVertexPtr poVertex = poIter->entity(); |
| 709 |
OdGePoint3d oPoint = poPL->vertexPosition(*poVertex); |
| 710 |
poLS->addPoint(oPoint.x, oPoint.y, oPoint.z); |
| 711 |
poIter->step(); |
| 712 |
} |
| 713 |
|
| 714 |
if (poPL->isClosed() && !poLS->IsEmpty()) |
| 715 |
{ |
| 716 |
poLS->addPoint(poLS->getX(0), poLS->getY(0), poLS->getZ(0)); |
| 717 |
} |
| 718 |
|
| 719 |
poFeature->SetGeometryDirectly(poLS); |
| 720 |
|
| 721 |
PrepareLineStyle(poFeature); |
| 722 |
|
| 723 |
return poFeature; |
| 724 |
} |
| 725 |
|
| 726 |
/************************************************************************/ |
| 727 |
/* Translate3DPOLYLINE() */ |
| 728 |
/************************************************************************/ |
| 729 |
|
| 730 |
OGRFeature *OGRDWGLayer::Translate3DPOLYLINE(OdDbEntityPtr poEntity) |
| 731 |
|
| 732 |
{ |
| 733 |
OGRFeature *poFeature = new OGRFeature(poFeatureDefn); |
| 734 |
OdDb3dPolylinePtr poPL = OdDb3dPolyline::cast(poEntity); |
| 735 |
|
| 736 |
TranslateGenericProperties(poFeature, poEntity); |
| 737 |
|
| 738 |
/* -------------------------------------------------------------------- */ |
| 739 |
/* Create a polyline geometry from the vertices. */ |
| 740 |
/* -------------------------------------------------------------------- */ |
| 741 |
OGRLineString *poLS = new OGRLineString(); |
| 742 |
OdDbObjectIteratorPtr poIter = poPL->vertexIterator(); |
| 743 |
|
| 744 |
while (!poIter->done()) |
| 745 |
{ |
| 746 |
OdDb3dPolylineVertexPtr poVertex = poIter->entity(); |
| 747 |
OdGePoint3d oPoint = poVertex->position(); |
| 748 |
poLS->addPoint(oPoint.x, oPoint.y, oPoint.z); |
| 749 |
poIter->step(); |
| 750 |
} |
| 751 |
|
| 752 |
if (poPL->isClosed() && !poLS->IsEmpty()) |
| 753 |
{ |
| 754 |
poLS->addPoint(poLS->getX(0), poLS->getY(0), poLS->getZ(0)); |
| 755 |
} |
| 756 |
|
| 757 |
poFeature->SetGeometryDirectly(poLS); |
| 758 |
|
| 759 |
PrepareLineStyle(poFeature); |
| 760 |
|
| 761 |
return poFeature; |
| 762 |
} |
| 763 |
|
| 764 |
/************************************************************************/ |
| 765 |
/* TranslateLINE() */ |
| 766 |
/************************************************************************/ |
| 767 |
|
| 768 |
OGRFeature *OGRDWGLayer::TranslateLINE(OdDbEntityPtr poEntity) |
| 769 |
|
| 770 |
{ |
| 771 |
OGRFeature *poFeature = new OGRFeature(poFeatureDefn); |
| 772 |
OdDbLinePtr poPL = OdDbLine::cast(poEntity); |
| 773 |
|
| 774 |
TranslateGenericProperties(poFeature, poEntity); |
| 775 |
|
| 776 |
/* -------------------------------------------------------------------- */ |
| 777 |
/* Create a polyline geometry from the vertices. */ |
| 778 |
/* -------------------------------------------------------------------- */ |
| 779 |
OGRLineString *poLS = new OGRLineString(); |
| 780 |
OdGePoint3d oPoint; |
| 781 |
|
| 782 |
poPL->getStartPoint(oPoint); |
| 783 |
poLS->addPoint(oPoint.x, oPoint.y, oPoint.z); |
| 784 |
|
| 785 |
poPL->getEndPoint(oPoint); |
| 786 |
poLS->addPoint(oPoint.x, oPoint.y, oPoint.z); |
| 787 |
|
| 788 |
poFeature->SetGeometryDirectly(poLS); |
| 789 |
|
| 790 |
PrepareLineStyle(poFeature); |
| 791 |
|
| 792 |
return poFeature; |
| 793 |
} |
| 794 |
|
| 795 |
/************************************************************************/ |
| 796 |
/* TranslateCIRCLE() */ |
| 797 |
/************************************************************************/ |
| 798 |
|
| 799 |
OGRFeature *OGRDWGLayer::TranslateCIRCLE(OdDbEntityPtr poEntity) |
| 800 |
|
| 801 |
{ |
| 802 |
OGRFeature *poFeature = new OGRFeature(poFeatureDefn); |
| 803 |
OdDbCirclePtr poC = OdDbCircle::cast(poEntity); |
| 804 |
|
| 805 |
TranslateGenericProperties(poFeature, poEntity); |
| 806 |
|
| 807 |
/* -------------------------------------------------------------------- */ |
| 808 |
/* Get geometry information. */ |
| 809 |
/* -------------------------------------------------------------------- */ |
| 810 |
OdGePoint3d oCenter = poC->center(); |
| 811 |
double dfRadius = poC->radius(); |
| 812 |
|
| 813 |
/* -------------------------------------------------------------------- */ |
| 814 |
/* Create geometry */ |
| 815 |
/* -------------------------------------------------------------------- */ |
| 816 |
OGRGeometry *poCircle = OGRGeometryFactory::approximateArcAngles( |
| 817 |
oCenter.x, oCenter.y, oCenter.z, dfRadius, dfRadius, 0.0, 0.0, 360.0, |
| 818 |
0.0); |
| 819 |
|
| 820 |
poFeature->SetGeometryDirectly(poCircle); |
| 821 |
PrepareLineStyle(poFeature); |
| 822 |
|
| 823 |
return poFeature; |
| 824 |
} |
| 825 |
|
| 826 |
/************************************************************************/ |
| 827 |
/* AngleCorrect() */ |
| 828 |
/* */ |
| 829 |
/* Convert from a "true" angle on the ellipse as returned by */ |
| 830 |
/* the DWG API to an angle of rotation on the ellipse as if the */ |
| 831 |
/* ellipse were actually circular. */ |
| 832 |
/************************************************************************/ |
| 833 |
|
| 834 |
double OGRDWGLayer::AngleCorrect(double dfTrueAngle, double dfRatio) |
| 835 |
|
| 836 |
{ |
| 837 |
double dfRotAngle; |
| 838 |
double dfDeltaX, dfDeltaY; |
| 839 |
|
| 840 |
dfTrueAngle *= (M_PI / 180); // convert to radians. |
| 841 |
|
| 842 |
dfDeltaX = cos(dfTrueAngle); |
| 843 |
dfDeltaY = sin(dfTrueAngle); |
| 844 |
|
| 845 |
dfRotAngle = atan2(dfDeltaY, dfDeltaX * dfRatio); |
| 846 |
|
| 847 |
dfRotAngle *= (180 / M_PI); // convert to degrees. |
| 848 |
|
| 849 |
if (dfTrueAngle < 0 && dfRotAngle > 0) |
| 850 |
dfRotAngle -= 360.0; |
| 851 |
|
| 852 |
if (dfTrueAngle > 360 && dfRotAngle < 360) |
| 853 |
dfRotAngle += 360.0; |
| 854 |
|
| 855 |
return dfRotAngle; |
| 856 |
} |
| 857 |
|
| 858 |
/************************************************************************/ |
| 859 |
/* TranslateELLIPSE() */ |
| 860 |
/************************************************************************/ |
| 861 |
|
| 862 |
OGRFeature *OGRDWGLayer::TranslateELLIPSE(OdDbEntityPtr poEntity) |
| 863 |
|
| 864 |
{ |
| 865 |
OdDbEllipsePtr poEE = OdDbEllipse::cast(poEntity); |
| 866 |
OGRFeature *poFeature = new OGRFeature(poFeatureDefn); |
| 867 |
|
| 868 |
TranslateGenericProperties(poFeature, poEntity); |
| 869 |
|
| 870 |
/* -------------------------------------------------------------------- */ |
| 871 |
/* Get some details. */ |
| 872 |
/* -------------------------------------------------------------------- */ |
| 873 |
double dfStartAngle, dfEndAngle, dfRatio; |
| 874 |
OdGePoint3d oCenter; |
| 875 |
OdGeVector3d oMajorAxis, oUnitNormal; |
| 876 |
|
| 877 |
// note we reverse start and end angles to account for ogr orientation. |
| 878 |
poEE->get(oCenter, oUnitNormal, oMajorAxis, dfRatio, dfEndAngle, |
| 879 |
dfStartAngle); |
| 880 |
|
| 881 |
dfStartAngle = -1 * dfStartAngle * 180 / M_PI; |
| 882 |
dfEndAngle = -1 * dfEndAngle * 180 / M_PI; |
| 883 |
|
| 884 |
/* -------------------------------------------------------------------- */ |
| 885 |
/* The DWG SDK expresses the angles as the angle to a real */ |
| 886 |
/* point on the ellipse while DXF and the OGR "arc angles" API */ |
| 887 |
/* work in terms of an angle of rotation on the ellipse as if */ |
| 888 |
/* the ellipse were actually circular. So we need to "correct" */ |
| 889 |
/* for the ratio. */ |
| 890 |
/* -------------------------------------------------------------------- */ |
| 891 |
dfStartAngle = AngleCorrect(dfStartAngle, dfRatio); |
| 892 |
dfEndAngle = AngleCorrect(dfEndAngle, dfRatio); |
| 893 |
|
| 894 |
if (dfStartAngle > dfEndAngle) |
| 895 |
dfEndAngle += 360.0; |
| 896 |
|
| 897 |
/* -------------------------------------------------------------------- */ |
| 898 |
/* Compute primary and secondary axis lengths, and the angle of */ |
| 899 |
/* rotation for the ellipse. */ |
| 900 |
/* -------------------------------------------------------------------- */ |
| 901 |
double dfPrimaryRadius, dfSecondaryRadius; |
| 902 |
double dfRotation; |
| 903 |
|
| 904 |
dfPrimaryRadius = |
| 905 |
sqrt(oMajorAxis.x * oMajorAxis.x + oMajorAxis.y * oMajorAxis.y + |
| 906 |
oMajorAxis.z * oMajorAxis.z); |
| 907 |
|
| 908 |
dfSecondaryRadius = dfRatio * dfPrimaryRadius; |
| 909 |
|
| 910 |
dfRotation = -1 * atan2(oMajorAxis.y, oMajorAxis.x) * 180 / M_PI; |
| 911 |
|
| 912 |
/* -------------------------------------------------------------------- */ |
| 913 |
/* Create geometry */ |
| 914 |
/* -------------------------------------------------------------------- */ |
| 915 |
OGRGeometry *poEllipse = OGRGeometryFactory::approximateArcAngles( |
| 916 |
oCenter.x, oCenter.y, oCenter.z, dfPrimaryRadius, dfSecondaryRadius, |
| 917 |
dfRotation, dfStartAngle, dfEndAngle, 0.0); |
| 918 |
|
| 919 |
poFeature->SetGeometryDirectly(poEllipse); |
| 920 |
|
| 921 |
PrepareLineStyle(poFeature); |
| 922 |
|
| 923 |
return poFeature; |
| 924 |
} |
| 925 |
|
| 926 |
/************************************************************************/ |
| 927 |
/* TranslateARC() */ |
| 928 |
/************************************************************************/ |
| 929 |
|
| 930 |
OGRFeature *OGRDWGLayer::TranslateARC(OdDbEntityPtr poEntity) |
| 931 |
|
| 932 |
{ |
| 933 |
OdDbArcPtr poAE = OdDbArc::cast(poEntity); |
| 934 |
OGRFeature *poFeature = new OGRFeature(poFeatureDefn); |
| 935 |
|
| 936 |
TranslateGenericProperties(poFeature, poEntity); |
| 937 |
|
| 938 |
/* -------------------------------------------------------------------- */ |
| 939 |
/* Collect parameters. */ |
| 940 |
/* -------------------------------------------------------------------- */ |
| 941 |
double dfEndAngle = -1 * poAE->startAngle() * 180 / M_PI; |
| 942 |
double dfStartAngle = -1 * poAE->endAngle() * 180 / M_PI; |
| 943 |
double dfRadius = poAE->radius(); |
| 944 |
OdGePoint3d oCenter = poAE->center(); |
| 945 |
|
| 946 |
/* -------------------------------------------------------------------- */ |
| 947 |
/* Create geometry */ |
| 948 |
/* -------------------------------------------------------------------- */ |
| 949 |
if (dfStartAngle > dfEndAngle) |
| 950 |
dfEndAngle += 360.0; |
| 951 |
|
| 952 |
OGRGeometry *poArc = OGRGeometryFactory::approximateArcAngles( |
| 953 |
oCenter.x, oCenter.y, oCenter.z, dfRadius, dfRadius, 0.0, dfStartAngle, |
| 954 |
dfEndAngle, 0.0); |
| 955 |
|
| 956 |
poFeature->SetGeometryDirectly(poArc); |
| 957 |
|
| 958 |
PrepareLineStyle(poFeature); |
| 959 |
|
| 960 |
return poFeature; |
| 961 |
} |
| 962 |
|
| 963 |
/************************************************************************/ |
| 964 |
/* TranslateSPLINE() */ |
| 965 |
/************************************************************************/ |
| 966 |
|
| 967 |
void rbspline(int npts, int k, int p1, double b[], double h[], double p[]); |
| 968 |
void rbsplinu(int npts, int k, int p1, double b[], double h[], double p[]); |
| 969 |
|
| 970 |
OGRFeature *OGRDWGLayer::TranslateSPLINE(OdDbEntityPtr poEntity) |
| 971 |
|
| 972 |
{ |
| 973 |
OdDbSplinePtr poSpline = OdDbSpline::cast(poEntity); |
| 974 |
OGRFeature *poFeature = new OGRFeature(poFeatureDefn); |
| 975 |
std::vector<double> adfControlPoints; |
| 976 |
int nDegree, i; |
| 977 |
|
| 978 |
TranslateGenericProperties(poFeature, poEntity); |
| 979 |
|
| 980 |
nDegree = poSpline->degree(); |
| 981 |
|
| 982 |
/* -------------------------------------------------------------------- */ |
| 983 |
/* Collect the control points in our vector. */ |
| 984 |
/* -------------------------------------------------------------------- */ |
| 985 |
int nControlPoints = poSpline->numControlPoints(); |
| 986 |
|
| 987 |
adfControlPoints.push_back(0.0); // some sort of control info. |
| 988 |
|
| 989 |
for (i = 0; i < nControlPoints; i++) |
| 990 |
{ |
| 991 |
OdGePoint3d oCP; |
| 992 |
|
| 993 |
poSpline->getControlPointAt(i, oCP); |
| 994 |
|
| 995 |
adfControlPoints.push_back(oCP.x); |
| 996 |
adfControlPoints.push_back(oCP.y); |
| 997 |
adfControlPoints.push_back(0.0); |
| 998 |
} |
| 999 |
|
| 1000 |
/* -------------------------------------------------------------------- */ |
| 1001 |
/* Process values. */ |
| 1002 |
/* -------------------------------------------------------------------- */ |
| 1003 |
if (poSpline->isClosed()) |
| 1004 |
{ |
| 1005 |
for (i = 0; i < nDegree; i++) |
| 1006 |
{ |
| 1007 |
adfControlPoints.push_back(adfControlPoints[i * 3 + 1]); |
| 1008 |
adfControlPoints.push_back(adfControlPoints[i * 3 + 2]); |
| 1009 |
adfControlPoints.push_back(adfControlPoints[i * 3 + 3]); |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
/* -------------------------------------------------------------------- */ |
| 1014 |
/* Interpolate spline */ |
| 1015 |
/* -------------------------------------------------------------------- */ |
| 1016 |
std::vector<double> h, p; |
| 1017 |
|
| 1018 |
h.push_back(1.0); |
| 1019 |
for (i = 0; i < nControlPoints; i++) |
| 1020 |
h.push_back(1.0); |
| 1021 |
|
| 1022 |
// resolution: |
| 1023 |
// int p1 = getGraphicVariableInt("$SPLINESEGS", 8) * npts; |
| 1024 |
int p1 = nControlPoints * 8; |
| 1025 |
|
| 1026 |
p.push_back(0.0); |
| 1027 |
for (i = 0; i < 3 * p1; i++) |
| 1028 |
p.push_back(0.0); |
| 1029 |
|
| 1030 |
if (poSpline->isClosed()) |
| 1031 |
rbsplinu(nControlPoints, nDegree + 1, p1, &(adfControlPoints[0]), |
| 1032 |
&(h[0]), &(p[0])); |
| 1033 |
else |
| 1034 |
rbspline(nControlPoints, nDegree + 1, p1, &(adfControlPoints[0]), |
| 1035 |
&(h[0]), &(p[0])); |
| 1036 |
|
| 1037 |
/* -------------------------------------------------------------------- */ |
| 1038 |
/* Turn into OGR geometry. */ |
| 1039 |
/* -------------------------------------------------------------------- */ |
| 1040 |
OGRLineString *poLS = new OGRLineString(); |
| 1041 |
|
| 1042 |
poLS->setNumPoints(p1); |
| 1043 |
for (i = 0; i < p1; i++) |
| 1044 |
poLS->setPoint(i, p[i * 3 + 1], p[i * 3 + 2]); |
| 1045 |
|
| 1046 |
poFeature->SetGeometryDirectly(poLS); |
| 1047 |
|
| 1048 |
PrepareLineStyle(poFeature); |
| 1049 |
|
| 1050 |
return poFeature; |
| 1051 |
} |
| 1052 |
|
| 1053 |
/************************************************************************/ |
| 1054 |
/* GeometryInsertTransformer */ |
| 1055 |
/************************************************************************/ |
| 1056 |
|
| 1057 |
class GeometryInsertTransformer : public OGRCoordinateTransformation |
| 1058 |
{ |
| 1059 |
public: |
| 1060 |
GeometryInsertTransformer() |
| 1061 |
: dfXOffset(0), dfYOffset(0), dfZOffset(0), dfXScale(1.0), |
| 1062 |
dfYScale(1.0), dfZScale(1.0), dfAngle(0.0) |
| 1063 |
{ |
| 1064 |
} |
| 1065 |
|
| 1066 |
double dfXOffset; |
| 1067 |
double dfYOffset; |
| 1068 |
double dfZOffset; |
| 1069 |
double dfXScale; |
| 1070 |
double dfYScale; |
| 1071 |
double dfZScale; |
| 1072 |
double dfAngle; |
| 1073 |
|
| 1074 |
const OGRSpatialReference *GetSourceCS() const override |
| 1075 |
{ |
| 1076 |
return nullptr; |
| 1077 |
} |
| 1078 |
|
| 1079 |
const OGRSpatialReference *GetTargetCS() const override |
| 1080 |
{ |
| 1081 |
return nullptr; |
| 1082 |
} |
| 1083 |
|
| 1084 |
OGRCoordinateTransformation *Clone() const override |
| 1085 |
{ |
| 1086 |
return new GeometryInsertTransformer(*this); |
| 1087 |
} |
| 1088 |
|
| 1089 |
OGRCoordinateTransformation *GetInverse() const override |
| 1090 |
{ |
| 1091 |
return nullptr; |
| 1092 |
} |
| 1093 |
|
| 1094 |
int Transform(size_t nCount, double *x, double *y, double *z = nullptr, |
| 1095 |
double * /*t*/ = nullptr, int *pabSuccess = nullptr) override |
| 1096 |
{ |
| 1097 |
for (size_t i = 0; i < nCount; i++) |
| 1098 |
{ |
| 1099 |
double dfXNew, dfYNew; |
| 1100 |
|
| 1101 |
x[i] *= dfXScale; |
| 1102 |
y[i] *= dfYScale; |
| 1103 |
if (z) |
| 1104 |
z[i] *= dfZScale; |
| 1105 |
|
| 1106 |
dfXNew = x[i] * cos(dfAngle) - y[i] * sin(dfAngle); |
| 1107 |
dfYNew = x[i] * sin(dfAngle) + y[i] * cos(dfAngle); |
| 1108 |
|
| 1109 |
x[i] = dfXNew; |
| 1110 |
y[i] = dfYNew; |
| 1111 |
|
| 1112 |
x[i] += dfXOffset; |
| 1113 |
y[i] += dfYOffset; |
| 1114 |
if (z) |
| 1115 |
z[i] += dfZOffset; |
| 1116 |
|
| 1117 |
if (pabSuccess) |
| 1118 |
pabSuccess[i] = TRUE; |
| 1119 |
} |
| 1120 |
return TRUE; |
| 1121 |
} |
| 1122 |
}; |
| 1123 |
|
| 1124 |
/************************************************************************/ |
| 1125 |
/* Translate3DFACE() */ |
| 1126 |
/************************************************************************/ |
| 1127 |
|
| 1128 |
OGRFeature *OGRDWGLayer::Translate3DFACE(OdDbEntityPtr poEntity) |
| 1129 |
{ |
| 1130 |
OGRFeature *poFeature = new OGRFeature(poFeatureDefn); |
| 1131 |
OdDbFacePtr pFace = OdDbFace::cast(poEntity); |
| 1132 |
|
| 1133 |
TranslateGenericProperties(poFeature, poEntity); |
| 1134 |
|
| 1135 |
/* -------------------------------------------------------------------- */ |
| 1136 |
/* Create a polygon geometry from the vertices. */ |
| 1137 |
/* -------------------------------------------------------------------- */ |
| 1138 |
OGRPolygon *poPolygon = new OGRPolygon(); |
| 1139 |
|
| 1140 |
OGRLinearRing *poLinearRing = new OGRLinearRing(); |
| 1141 |
|
| 1142 |
OdInt16 index; |
| 1143 |
OdGePoint3d point; |
| 1144 |
|
| 1145 |
for (index = 0; index <= 3; index++) |
| 1146 |
{ |
| 1147 |
pFace->getVertexAt(index, point); |
| 1148 |
poLinearRing->addPoint(point.x, point.y, point.z); |
| 1149 |
} |
| 1150 |
poLinearRing->closeRings(); |
| 1151 |
poPolygon->addRingDirectly(poLinearRing); |
| 1152 |
poFeature->SetGeometryDirectly(poPolygon); |
| 1153 |
PrepareLineStyle(poFeature); |
| 1154 |
return poFeature; |
| 1155 |
} |
| 1156 |
|
| 1157 |
/************************************************************************/ |
| 1158 |
/* TranslateINSERT() */ |
| 1159 |
/************************************************************************/ |
| 1160 |
|
| 1161 |
OGRFeature *OGRDWGLayer::TranslateINSERT(OdDbEntityPtr poEntity) |
| 1162 |
|
| 1163 |
{ |
| 1164 |
OdDbBlockReferencePtr poRef = OdDbBlockReference::cast(poEntity); |
| 1165 |
OGRFeature *poFeature = new OGRFeature(poFeatureDefn); |
| 1166 |
|
| 1167 |
TranslateGenericProperties(poFeature, poEntity); |
| 1168 |
|
| 1169 |
/* -------------------------------------------------------------------- */ |
| 1170 |
/* Collect parameters from the object. */ |
| 1171 |
/* -------------------------------------------------------------------- */ |
| 1172 |
GeometryInsertTransformer oTransformer; |
| 1173 |
CPLString osBlockName; |
| 1174 |
double dfAngle = poRef->rotation() * 180 / M_PI; |
| 1175 |
OdGePoint3d oPosition = poRef->position(); |
| 1176 |
OdGeScale3d oScale = poRef->scaleFactors(); |
| 1177 |
|
| 1178 |
oTransformer.dfXOffset = oPosition.x; |
| 1179 |
oTransformer.dfYOffset = oPosition.y; |
| 1180 |
oTransformer.dfZOffset = oPosition.z; |
| 1181 |
|
| 1182 |
oTransformer.dfXScale = oScale.sx; |
| 1183 |
oTransformer.dfYScale = oScale.sy; |
| 1184 |
oTransformer.dfZScale = oScale.sz; |
| 1185 |
|
| 1186 |
oTransformer.dfAngle = poRef->rotation(); |
| 1187 |
|
| 1188 |
OdDbBlockTableRecordPtr poBlockRec = poRef->blockTableRecord().openObject(); |
| 1189 |
if (poBlockRec.get()) |
| 1190 |
osBlockName = (const char *)poBlockRec->getName(); |
| 1191 |
|
| 1192 |
/* -------------------------------------------------------------------- */ |
| 1193 |
/* In the case where we do not inlined blocks we just capture */ |
| 1194 |
/* info on a point feature. */ |
| 1195 |
/* -------------------------------------------------------------------- */ |
| 1196 |
if (!poDS->InlineBlocks()) |
| 1197 |
{ |
| 1198 |
poFeature->SetGeometryDirectly(new OGRPoint(oTransformer.dfXOffset, |
| 1199 |
oTransformer.dfYOffset, |
| 1200 |
oTransformer.dfZOffset)); |
| 1201 |
|
| 1202 |
poFeature->SetField("BlockName", osBlockName); |
| 1203 |
|
| 1204 |
poFeature->SetField("BlockAngle", dfAngle); |
| 1205 |
poFeature->SetField("BlockScale", 3, &(oTransformer.dfXScale)); |
| 1206 |
|
| 1207 |
OdDbObjectIteratorPtr pEntIter = poRef->attributeIterator(); |
| 1208 |
OdDbAttributePtr openAttr; |
| 1209 |
|
| 1210 |
CPLJSONObject uAttrData = CPLJSONObject(); |
| 1211 |
for (; !pEntIter->done(); pEntIter->step()) |
| 1212 |
{ |
| 1213 |
openAttr = |
| 1214 |
pEntIter->entity()->objectId().safeOpenObject(OdDb::kForRead); |
| 1215 |
|
| 1216 |
CPLString attrText = TextUnescape(openAttr->textString(), false); |
| 1217 |
|
| 1218 |
const char *fieldName = CPLSPrintf("%ls", openAttr->tag().c_str()); |
| 1219 |
if (poDS->AllAttributes() || |
| 1220 |
(!openAttr->isInvisible() && |
| 1221 |
openAttr->visibility() != OdDb::kInvisible)) |
| 1222 |
{ |
| 1223 |
uAttrData.Add(fieldName, attrText); |
| 1224 |
} |
| 1225 |
|
| 1226 |
if (poDS->Attributes() && |
| 1227 |
poFeatureDefn->GetFieldIndex(fieldName) != -1) |
| 1228 |
{ |
| 1229 |
poFeature->SetField(fieldName, attrText); |
| 1230 |
} |
| 1231 |
} |
| 1232 |
|
| 1233 |
poFeature->SetField("BlockAttributes", uAttrData.ToString().c_str()); |
| 1234 |
|
| 1235 |
return poFeature; |
| 1236 |
} |
| 1237 |
|
| 1238 |
/* -------------------------------------------------------------------- */ |
| 1239 |
/* Lookup the block. */ |
| 1240 |
/* -------------------------------------------------------------------- */ |
| 1241 |
DWGBlockDefinition *poBlock = poDS->LookupBlock(osBlockName); |
| 1242 |
|
| 1243 |
if (poBlock == nullptr) |
| 1244 |
{ |
| 1245 |
delete poFeature; |
| 1246 |
return nullptr; |
| 1247 |
} |
| 1248 |
|
| 1249 |
/* -------------------------------------------------------------------- */ |
| 1250 |
/* Transform the geometry. */ |
| 1251 |
/* -------------------------------------------------------------------- */ |
| 1252 |
if (poBlock->poGeometry != nullptr) |
| 1253 |
{ |
| 1254 |
OGRGeometry *poGeometry = poBlock->poGeometry->clone(); |
| 1255 |
|
| 1256 |
poGeometry->transform(&oTransformer); |
| 1257 |
|
| 1258 |
poFeature->SetGeometryDirectly(poGeometry); |
| 1259 |
} |
| 1260 |
|
| 1261 |
/* -------------------------------------------------------------------- */ |
| 1262 |
/* If we have complete features associated with the block, push */ |
| 1263 |
/* them on the pending feature stack copying over key override */ |
| 1264 |
/* information. */ |
| 1265 |
/* */ |
| 1266 |
/* Note that while we transform the geometry of the features we */ |
| 1267 |
/* don't adjust subtle things like text angle. */ |
| 1268 |
/* -------------------------------------------------------------------- */ |
| 1269 |
unsigned int iSubFeat; |
| 1270 |
|
| 1271 |
for (iSubFeat = 0; iSubFeat < poBlock->apoFeatures.size(); iSubFeat++) |
| 1272 |
{ |
| 1273 |
OGRFeature *poSubFeature = poBlock->apoFeatures[iSubFeat]->Clone(); |
| 1274 |
CPLString osCompEntityId; |
| 1275 |
|
| 1276 |
if (poSubFeature->GetGeometryRef() != nullptr) |
| 1277 |
poSubFeature->GetGeometryRef()->transform(&oTransformer); |
| 1278 |
|
| 1279 |
ACAdjustText(dfAngle, oScale.sx, oScale.sy, poSubFeature); |
| 1280 |
|
| 1281 |
#ifdef notdef |
| 1282 |
osCompEntityId = poSubFeature->GetFieldAsString("EntityHandle"); |
| 1283 |
osCompEntityId += ":"; |
| 1284 |
#endif |
| 1285 |
osCompEntityId += poFeature->GetFieldAsString("EntityHandle"); |
| 1286 |
|
| 1287 |
poSubFeature->SetField("EntityHandle", osCompEntityId); |
| 1288 |
|
| 1289 |
apoPendingFeatures.push(poSubFeature); |
| 1290 |
} |
| 1291 |
|
| 1292 |
/* -------------------------------------------------------------------- */ |
| 1293 |
/* If we have attributes, insert them on the stack at this */ |
| 1294 |
/* point too. */ |
| 1295 |
/* -------------------------------------------------------------------- */ |
| 1296 |
OdDbObjectIteratorPtr pIter = poRef->attributeIterator(); |
| 1297 |
for (; !pIter->done(); pIter->step()) |
| 1298 |
{ |
| 1299 |
OdDbAttributePtr pAttr = pIter->entity(); |
| 1300 |
if (!pAttr.isNull()) |
| 1301 |
{ |
| 1302 |
oStyleProperties.clear(); |
| 1303 |
|
| 1304 |
OGRFeature *poAttrFeat = TranslateTEXT(pAttr); |
| 1305 |
const char *fieldName = CPLSPrintf("%ls", pAttr->tag().c_str()); |
| 1306 |
if (poDS->Attributes() && |
| 1307 |
poFeatureDefn->GetFieldIndex(fieldName) != -1) |
| 1308 |
{ |
| 1309 |
CPLString attrText = TextUnescape(pAttr->textString(), false); |
| 1310 |
poFeature->SetField(fieldName, attrText); |
| 1311 |
if (poAttrFeat) |
| 1312 |
poAttrFeat->SetField(fieldName, attrText); |
| 1313 |
} |
| 1314 |
if (poAttrFeat) |
| 1315 |
apoPendingFeatures.push(poAttrFeat); |
| 1316 |
} |
| 1317 |
} |
| 1318 |
|
| 1319 |
/* -------------------------------------------------------------------- */ |
| 1320 |
/* Return the working feature if we had geometry, otherwise */ |
| 1321 |
/* return NULL and let the machinery find the rest of the */ |
| 1322 |
/* features in the pending feature stack. */ |
| 1323 |
/* -------------------------------------------------------------------- */ |
| 1324 |
if (poBlock->poGeometry == nullptr) |
| 1325 |
{ |
| 1326 |
delete poFeature; |
| 1327 |
return nullptr; |
| 1328 |
} |
| 1329 |
else |
| 1330 |
{ |
| 1331 |
return poFeature; |
| 1332 |
} |
| 1333 |
} |
| 1334 |
|
| 1335 |
/************************************************************************/ |
| 1336 |
/* GetNextUnfilteredFeature() */ |
| 1337 |
/************************************************************************/ |
| 1338 |
|
| 1339 |
OGRFeature *OGRDWGLayer::GetNextUnfilteredFeature() |
| 1340 |
|
| 1341 |
{ |
| 1342 |
OGRFeature *poFeature = nullptr; |
| 1343 |
|
| 1344 |
/* -------------------------------------------------------------------- */ |
| 1345 |
/* If we have pending features, return one of them. */ |
| 1346 |
/* -------------------------------------------------------------------- */ |
| 1347 |
if (!apoPendingFeatures.empty()) |
| 1348 |
{ |
| 1349 |
poFeature = apoPendingFeatures.front(); |
| 1350 |
apoPendingFeatures.pop(); |
| 1351 |
|
| 1352 |
poFeature->SetFID(iNextFID++); |
| 1353 |
return poFeature; |
| 1354 |
} |
| 1355 |
|
| 1356 |
/* -------------------------------------------------------------------- */ |
| 1357 |
/* Fetch the next entity. */ |
| 1358 |
/* -------------------------------------------------------------------- */ |
| 1359 |
while (poFeature == nullptr && !poEntIter->done()) |
| 1360 |
{ |
| 1361 |
|
| 1362 |
OdDbObjectId oId = poEntIter->objectId(); |
| 1363 |
OdDbEntityPtr poEntity = OdDbEntity::cast(oId.openObject()); |
| 1364 |
|
| 1365 |
if (poEntity.isNull()) |
| 1366 |
return nullptr; |
| 1367 |
|
| 1368 |
/* -------------------------------------------------------------------- |
| 1369 |
*/ |
| 1370 |
/* What is the class name for this entity? */ |
| 1371 |
/* -------------------------------------------------------------------- |
| 1372 |
*/ |
| 1373 |
OdRxClass *poClass = poEntity->isA(); |
| 1374 |
const OdString osName = poClass->name(); |
| 1375 |
const char *pszEntityClassName = (const char *)osName; |
| 1376 |
|
| 1377 |
/* -------------------------------------------------------------------- |
| 1378 |
*/ |
| 1379 |
/* Handle the entity. */ |
| 1380 |
/* -------------------------------------------------------------------- |
| 1381 |
*/ |
| 1382 |
oStyleProperties.clear(); |
| 1383 |
|
| 1384 |
if (EQUAL(pszEntityClassName, "AcDbPoint")) |
| 1385 |
{ |
| 1386 |
poFeature = TranslatePOINT(poEntity); |
| 1387 |
} |
| 1388 |
else if (EQUAL(pszEntityClassName, "AcDbLine")) |
| 1389 |
{ |
| 1390 |
poFeature = TranslateLINE(poEntity); |
| 1391 |
} |
| 1392 |
else if (EQUAL(pszEntityClassName, "AcDbPolyline")) |
| 1393 |
{ |
| 1394 |
poFeature = TranslateLWPOLYLINE(poEntity); |
| 1395 |
} |
| 1396 |
else if (EQUAL(pszEntityClassName, "AcDb2dPolyline")) |
| 1397 |
{ |
| 1398 |
poFeature = Translate2DPOLYLINE(poEntity); |
| 1399 |
} |
| 1400 |
else if (EQUAL(pszEntityClassName, "AcDb3dPolyline")) |
| 1401 |
{ |
| 1402 |
poFeature = Translate3DPOLYLINE(poEntity); |
| 1403 |
} |
| 1404 |
else if (EQUAL(pszEntityClassName, "AcDbEllipse")) |
| 1405 |
{ |
| 1406 |
poFeature = TranslateELLIPSE(poEntity); |
| 1407 |
} |
| 1408 |
else if (EQUAL(pszEntityClassName, "AcDbArc")) |
| 1409 |
{ |
| 1410 |
poFeature = TranslateARC(poEntity); |
| 1411 |
} |
| 1412 |
else if (EQUAL(pszEntityClassName, "AcDbMText")) |
| 1413 |
{ |
| 1414 |
poFeature = TranslateMTEXT(poEntity); |
| 1415 |
} |
| 1416 |
else if (EQUAL(pszEntityClassName, "AcDbText") || |
| 1417 |
EQUAL(pszEntityClassName, "AcDbAttributeDefinition")) |
| 1418 |
{ |
| 1419 |
poFeature = TranslateTEXT(poEntity); |
| 1420 |
} |
| 1421 |
else if (EQUAL(pszEntityClassName, "AcDbAlignedDimension") || |
| 1422 |
EQUAL(pszEntityClassName, "AcDbRotatedDimension")) |
| 1423 |
{ |
| 1424 |
poFeature = TranslateDIMENSION(poEntity); |
| 1425 |
} |
| 1426 |
else if (EQUAL(pszEntityClassName, "AcDbCircle")) |
| 1427 |
{ |
| 1428 |
poFeature = TranslateCIRCLE(poEntity); |
| 1429 |
} |
| 1430 |
else if (EQUAL(pszEntityClassName, "AcDbSpline")) |
| 1431 |
{ |
| 1432 |
poFeature = TranslateSPLINE(poEntity); |
| 1433 |
} |
| 1434 |
else if (EQUAL(pszEntityClassName, "AcDbHatch")) |
| 1435 |
{ |
| 1436 |
poFeature = TranslateHATCH(poEntity); |
| 1437 |
} |
| 1438 |
else if (EQUAL(pszEntityClassName, "AcDbFace")) |
| 1439 |
{ |
| 1440 |
poFeature = Translate3DFACE(poEntity); |
| 1441 |
} |
| 1442 |
else if (EQUAL(pszEntityClassName, "AcDbBlockReference")) |
| 1443 |
{ |
| 1444 |
poFeature = TranslateINSERT(poEntity); |
| 1445 |
if (poFeature == nullptr && !apoPendingFeatures.empty()) |
| 1446 |
{ |
| 1447 |
poFeature = apoPendingFeatures.front(); |
| 1448 |
apoPendingFeatures.pop(); |
| 1449 |
} |
| 1450 |
} |
| 1451 |
else |
| 1452 |
{ |
| 1453 |
if (oIgnoredEntities.count(pszEntityClassName) == 0) |
| 1454 |
{ |
| 1455 |
oIgnoredEntities.insert(pszEntityClassName); |
| 1456 |
CPLDebug("DWG", "Ignoring one or more of entity '%s'.", |
| 1457 |
pszEntityClassName); |
| 1458 |
} |
| 1459 |
} |
| 1460 |
|
| 1461 |
poEntIter->step(); |
| 1462 |
} |
| 1463 |
|
| 1464 |
/* -------------------------------------------------------------------- */ |
| 1465 |
/* Set FID. */ |
| 1466 |
/* -------------------------------------------------------------------- */ |
| 1467 |
if (poFeature != nullptr) |
| 1468 |
{ |
| 1469 |
poFeature->SetFID(iNextFID++); |
| 1470 |
m_nFeaturesRead++; |
| 1471 |
} |
| 1472 |
|
| 1473 |
return poFeature; |
| 1474 |
} |
| 1475 |
|
| 1476 |
/************************************************************************/ |
| 1477 |
/* GetNextFeature() */ |
| 1478 |
/************************************************************************/ |
| 1479 |
|
| 1480 |
OGRFeature *OGRDWGLayer::GetNextFeature() |
| 1481 |
|
| 1482 |
{ |
| 1483 |
while (true) |
| 1484 |
{ |
| 1485 |
OGRFeature *poFeature = GetNextUnfilteredFeature(); |
| 1486 |
|
| 1487 |
if (poFeature == nullptr) |
| 1488 |
return nullptr; |
| 1489 |
|
| 1490 |
if ((m_poFilterGeom == nullptr || |
| 1491 |
FilterGeometry(poFeature->GetGeometryRef())) && |
| 1492 |
(m_poAttrQuery == nullptr || m_poAttrQuery->Evaluate(poFeature))) |
| 1493 |
{ |
| 1494 |
return poFeature; |
| 1495 |
} |
| 1496 |
|
| 1497 |
delete poFeature; |
| 1498 |
} |
| 1499 |
} |
| 1500 |
|
| 1501 |
/************************************************************************/ |
| 1502 |
/* TestCapability() */ |
| 1503 |
/************************************************************************/ |
| 1504 |
|
| 1505 |
int OGRDWGLayer::TestCapability(const char *pszCap) const |
| 1506 |
|
| 1507 |
{ |
| 1508 |
if (EQUAL(pszCap, OLCStringsAsUTF8)) |
| 1509 |
return TRUE; |
| 1510 |
else if (EQUAL(pszCap, OLCZGeometries)) |
| 1511 |
return TRUE; |
| 1512 |
else |
| 1513 |
return FALSE; |
| 1514 |
} |
| 1515 |
|
| 1516 |
/************************************************************************/ |
| 1517 |
/* GetDataset() */ |
| 1518 |
/************************************************************************/ |
| 1519 |
|
| 1520 |
GDALDataset *OGRDWGLayer::GetDataset() |
| 1521 |
{ |
| 1522 |
return poDS; |
| 1523 |
} |