Debian Codegraph @

ogr/ogrsf_frmts/dwg/ogrdgnv8layer.cpp

raw
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 "ogr_featurestyle.h"
16 #include "ogr_api.h"
17
18 #include <math.h>
19 #include <algorithm>
20
21 /* -------------------------------------------------------------------- */
22 /* Line Styles */
23 /* -------------------------------------------------------------------- */
24 #define DGNS_SOLID 0
25 #define DGNS_DOTTED 1
26 #define DGNS_MEDIUM_DASH 2
27 #define DGNS_LONG_DASH 3
28 #define DGNS_DOT_DASH 4
29 #define DGNS_SHORT_DASH 5
30 #define DGNS_DASH_DOUBLE_DOT 6
31 #define DGNS_LONG_DASH_SHORT_DASH 7
32
33 constexpr double DEG_TO_RAD = M_PI / 180.0;
34 constexpr double RAD_TO_DEG = 180.0 / M_PI;
35 constexpr double CONTIGUITY_TOLERANCE = 1e10; // Arbitrary high value
36
37 static CPLString ToUTF8(const OdString &str)
38 {
39 return OGRDGNV8DataSource::ToUTF8(str);
40 }
41
42 /************************************************************************/
43 /* EscapeDoubleQuote() */
44 /************************************************************************/
45
46 static CPLString EscapeDoubleQuote(const char *pszStr)
47 {
48 if (strchr(pszStr, '"') != nullptr)
49 {
50 CPLString osEscaped;
51
52 for (size_t iC = 0; pszStr[iC] != '\0'; iC++)
53 {
54 if (pszStr[iC] == '"')
55 osEscaped += "\\\"";
56 else
57 osEscaped += pszStr[iC];
58 }
59 return osEscaped;
60 }
61 else
62 {
63 return pszStr;
64 }
65 }
66
67 /************************************************************************/
68 /* OGRDGNV8Layer() */
69 /************************************************************************/
70
71 OGRDGNV8Layer::OGRDGNV8Layer(OGRDGNV8DataSource *poDS, OdDgModelPtr pModel)
72 : m_poDS(poDS), m_poFeatureDefn(nullptr), m_pModel(pModel),
73 m_pIterator(static_cast<OdDgElementIterator *>(nullptr)),
74 m_nIdxInPendingFeatures(0)
75 {
76 const char *pszName;
77 if (pModel->getName().isEmpty())
78 pszName = CPLSPrintf("Model #%d", pModel->getEntryId());
79 else
80 pszName = CPLSPrintf("%s", ToUTF8(pModel->getName()).c_str());
81 CPLDebug("DGNV8", "%s is %dd", pszName, pModel->getModelIs3dFlag() ? 3 : 2);
82
83 /* -------------------------------------------------------------------- */
84 /* Create the feature definition. */
85 /* -------------------------------------------------------------------- */
86 m_poFeatureDefn = new OGRFeatureDefn(pszName);
87 SetDescription(m_poFeatureDefn->GetName());
88 m_poFeatureDefn->Reference();
89
90 OGRFieldDefn oField("", OFTInteger);
91
92 /* -------------------------------------------------------------------- */
93 /* Element type */
94 /* -------------------------------------------------------------------- */
95 oField.SetName("Type");
96 oField.SetType(OFTInteger);
97 oField.SetWidth(2);
98 oField.SetPrecision(0);
99 m_poFeatureDefn->AddFieldDefn(&oField);
100
101 /* -------------------------------------------------------------------- */
102 /* Level number. */
103 /* -------------------------------------------------------------------- */
104 oField.SetName("Level");
105 oField.SetType(OFTInteger);
106 oField.SetWidth(2);
107 oField.SetPrecision(0);
108 m_poFeatureDefn->AddFieldDefn(&oField);
109
110 /* -------------------------------------------------------------------- */
111 /* graphic group */
112 /* -------------------------------------------------------------------- */
113 oField.SetName("GraphicGroup");
114 oField.SetType(OFTInteger);
115 oField.SetWidth(4);
116 oField.SetPrecision(0);
117 m_poFeatureDefn->AddFieldDefn(&oField);
118
119 /* -------------------------------------------------------------------- */
120 /* ColorIndex */
121 /* -------------------------------------------------------------------- */
122 oField.SetName("ColorIndex");
123 oField.SetType(OFTInteger);
124 oField.SetWidth(3);
125 oField.SetPrecision(0);
126 m_poFeatureDefn->AddFieldDefn(&oField);
127
128 /* -------------------------------------------------------------------- */
129 /* Weight */
130 /* -------------------------------------------------------------------- */
131 oField.SetName("Weight");
132 oField.SetType(OFTInteger);
133 oField.SetWidth(2);
134 oField.SetPrecision(0);
135 m_poFeatureDefn->AddFieldDefn(&oField);
136
137 /* -------------------------------------------------------------------- */
138 /* Style */
139 /* -------------------------------------------------------------------- */
140 oField.SetName("Style");
141 oField.SetType(OFTInteger);
142 oField.SetWidth(1);
143 oField.SetPrecision(0);
144 m_poFeatureDefn->AddFieldDefn(&oField);
145
146 /* -------------------------------------------------------------------- */
147 /* Text */
148 /* -------------------------------------------------------------------- */
149 oField.SetName("Text");
150 oField.SetType(OFTString);
151 oField.SetWidth(0);
152 oField.SetPrecision(0);
153 m_poFeatureDefn->AddFieldDefn(&oField);
154
155 /* -------------------------------------------------------------------- */
156 /* ULink */
157 /* -------------------------------------------------------------------- */
158 oField.SetName("ULink");
159 oField.SetType(OFTString);
160 oField.SetSubType(OFSTJSON);
161 oField.SetWidth(0);
162 oField.SetPrecision(0);
163 m_poFeatureDefn->AddFieldDefn(&oField);
164
165 OGRDGNV8Layer::ResetReading();
166 }
167
168 /************************************************************************/
169 /* ~OGRDGNV8Layer() */
170 /************************************************************************/
171
172 OGRDGNV8Layer::~OGRDGNV8Layer()
173
174 {
175 CleanPendingFeatures();
176 m_poFeatureDefn->Release();
177 }
178
179 /************************************************************************/
180 /* CleanPendingFeatures() */
181 /************************************************************************/
182
183 void OGRDGNV8Layer::CleanPendingFeatures()
184
185 {
186 for (size_t i = m_nIdxInPendingFeatures; i < m_aoPendingFeatures.size();
187 i++)
188 {
189 delete m_aoPendingFeatures[i].first;
190 }
191 m_aoPendingFeatures.clear();
192 m_nIdxInPendingFeatures = 0;
193 }
194
195 /************************************************************************/
196 /* ResetReading() */
197 /************************************************************************/
198
199 void OGRDGNV8Layer::ResetReading()
200
201 {
202 if (m_pModel.get())
203 {
204 m_pIterator = m_pModel->createGraphicsElementsIterator();
205 }
206 CleanPendingFeatures();
207 }
208
209 /************************************************************************/
210 /* CollectSubElements() */
211 /************************************************************************/
212
213 std::vector<tPairFeatureHoleFlag>
214 OGRDGNV8Layer::CollectSubElements(OdDgElementIteratorPtr iterator, int level)
215 {
216 std::vector<tPairFeatureHoleFlag> oVector;
217 #ifdef DEBUG_VERBOSE
218 CPLString osIndent;
219 for (int i = 0; i < level; i++)
220 osIndent += " ";
221 int counter = 0;
222 #endif
223 for (; !iterator->done(); iterator->step())
224 {
225 #ifdef DEBUG_VERBOSE
226 fprintf(stdout, "%sSub element %d:\n", osIndent.c_str(), counter);
227 counter++;
228 #endif
229 OdRxObjectPtr object = iterator->item().openObject(OdDg::kForRead);
230 OdDgGraphicsElementPtr element = OdDgGraphicsElement::cast(object);
231 if (element.isNull())
232 continue;
233
234 std::vector<tPairFeatureHoleFlag> oSubVector =
235 ProcessElement(element, level + 1);
236 oVector.insert(oVector.end(), oSubVector.begin(), oSubVector.end());
237 }
238 return oVector;
239 }
240
241 /************************************************************************/
242 /* GetAnchorPosition() */
243 /************************************************************************/
244
245 static int GetAnchorPosition(OdDg::TextJustification value)
246 {
247 switch (value)
248 {
249 case OdDg::kLeftTop:
250 return 7;
251 case OdDg::kLeftCenter:
252 return 4;
253 case OdDg::kLeftBottom:
254 return 10;
255 case OdDg::kLeftMarginTop:
256 return 7;
257 case OdDg::kLeftMarginCenter:
258 return 4;
259 case OdDg::kLeftMarginBottom:
260 return 10;
261 case OdDg::kCenterTop:
262 return 8;
263 case OdDg::kCenterCenter:
264 return 5;
265 case OdDg::kCenterBottom:
266 return 11;
267 case OdDg::kRightMarginTop:
268 return 9;
269 case OdDg::kRightMarginCenter:
270 return 6;
271 case OdDg::kRightMarginBottom:
272 return 12;
273 case OdDg::kRightTop:
274 return 9;
275 case OdDg::kRightCenter:
276 return 6;
277 case OdDg::kRightBottom:
278 return 12;
279 case OdDg::kLeftDescender:
280 return 1;
281 case OdDg::kCenterDescender:
282 return 2;
283 case OdDg::kRightDescender:
284 return 3;
285 default:
286 return 0;
287 }
288 }
289
290 /************************************************************************/
291 /* GetAnchorPosition() */
292 /************************************************************************/
293
294 static OdDg::TextJustification GetAnchorPositionFromOGR(int value)
295 {
296 switch (value)
297 {
298 case 1:
299 return OdDg::kLeftDescender;
300 case 2:
301 return OdDg::kCenterDescender;
302 case 3:
303 return OdDg::kRightDescender;
304 case 4:
305 return OdDg::kLeftCenter;
306 case 5:
307 return OdDg::kCenterCenter;
308 case 6:
309 return OdDg::kRightCenter;
310 case 7:
311 return OdDg::kLeftTop;
312 case 8:
313 return OdDg::kCenterTop;
314 case 9:
315 return OdDg::kRightTop;
316 case 10:
317 return OdDg::kLeftBottom;
318 case 11:
319 return OdDg::kCenterBottom;
320 case 12:
321 return OdDg::kRightBottom;
322 default:
323 return OdDg::kLeftTop;
324 }
325 }
326
327 /************************************************************************/
328 /* AlmostEqual() */
329 /************************************************************************/
330
331 static bool AlmostEqual(double dfA, double dfB)
332 {
333 if (fabs(dfB) > 1e-7)
334 return fabs((dfA - dfB) / dfB) < 1e-6;
335 else
336 return fabs(dfA) <= 1e-7;
337 }
338
339 /************************************************************************/
340 /* ProcessTextTraits */
341 /************************************************************************/
342
343 template <class TextPtr> struct ProcessTextTraits
344 {
345 };
346
347 template <> struct ProcessTextTraits<OdDgText2dPtr>
348 {
349 static double getRotation(OdDgText2dPtr text)
350 {
351 return text->getRotation();
352 }
353
354 static void setGeom(OGRFeature *poFeature, OdDgText2dPtr text)
355 {
356 OdGePoint2d point = text->getOrigin();
357 poFeature->SetGeometryDirectly(new OGRPoint(point.x, point.y));
358 }
359 };
360
361 template <> struct ProcessTextTraits<OdDgText3dPtr>
362 {
363 static double getRotation(OdDgText3dPtr /*text*/)
364 {
365 return 0.0;
366 }
367
368 static void setGeom(OGRFeature *poFeature, OdDgText3dPtr text)
369 {
370 OdGePoint3d point = text->getOrigin();
371 poFeature->SetGeometryDirectly(new OGRPoint(point.x, point.y, point.z));
372 }
373 };
374
375 /************************************************************************/
376 /* ProcessText() */
377 /************************************************************************/
378
379 template <class TextPtr>
380 static void ProcessText(OGRFeature *poFeature, const CPLString &osColor,
381 TextPtr text)
382 {
383 const OdString oText = text->getText();
384 poFeature->SetField("Text", ToUTF8(oText).c_str());
385
386 CPLString osStyle;
387 osStyle.Printf("LABEL(t:\"%s\"", EscapeDoubleQuote(ToUTF8(oText)).c_str());
388 osStyle += osColor;
389 osStyle += CPLSPrintf(",s:%fg", text->getHeightMultiplier());
390
391 // Gets Font name
392 OdDgFontTablePtr pFontTable = text->database()->getFontTable();
393 OdDgFontTableRecordPtr pFont = pFontTable->getFont(text->getFontEntryId());
394 if (!pFont.isNull())
395 {
396 osStyle += CPLSPrintf(
397 ",f:\"%s\"", EscapeDoubleQuote(ToUTF8(pFont->getName())).c_str());
398 }
399 else
400 {
401 osStyle += CPLSPrintf(",f:MstnFont%u", text->getFontEntryId());
402 }
403
404 const int nAnchor = GetAnchorPosition(text->getJustification());
405 if (nAnchor > 0)
406 osStyle += CPLSPrintf(",p:%d", nAnchor);
407
408 // Add the angle, if not horizontal
409 const double dfRotation = ProcessTextTraits<TextPtr>::getRotation(text);
410 if (dfRotation != 0.0)
411 osStyle += CPLSPrintf(
412 ",a:%d", static_cast<int>(floor(dfRotation * RAD_TO_DEG + 0.5)));
413
414 osStyle += ")";
415 poFeature->SetStyleString(osStyle);
416 ProcessTextTraits<TextPtr>::setGeom(poFeature, text);
417 }
418
419 /************************************************************************/
420 /* ConsiderBrush() */
421 /************************************************************************/
422
423 static CPLString ConsiderBrush(OdDgGraphicsElementPtr element,
424 const CPLString &osStyle)
425 {
426 CPLString osNewStyle(osStyle);
427 OdRxObjectPtrArray linkages;
428 element->getLinkages(OdDgAttributeLinkage::kFillStyle, linkages);
429 if (linkages.length() >= 1)
430 {
431 OdDgFillColorLinkagePtr fillColor =
432 OdDgFillColorLinkage::cast(linkages[0]);
433 if (!fillColor.isNull())
434 {
435 const OdUInt32 uFillColorIdx = fillColor->getColorIndex();
436 if (OdDgColorTable::isCorrectIndex(element->database(),
437 uFillColorIdx))
438 {
439 ODCOLORREF color = OdDgColorTable::lookupRGB(
440 element->database(), uFillColorIdx);
441 const char *pszBrush = CPLSPrintf(
442 "BRUSH(fc:#%02x%02x%02x,id:\"ogr-brush-0\")",
443 ODGETRED(color), ODGETGREEN(color), ODGETBLUE(color));
444 const OdUInt32 uColorIndex = element->getColorIndex();
445 if (uFillColorIdx != uColorIndex)
446 {
447 osNewStyle = CPLString(pszBrush) + ";" + osNewStyle;
448 }
449 else
450 {
451 osNewStyle = pszBrush;
452 }
453 }
454 }
455 }
456 return osNewStyle;
457 }
458
459 /************************************************************************/
460 /* ProcessCurveTraits */
461 /************************************************************************/
462
463 template <class CurveElementPtr> struct ProcessCurveTraits
464 {
465 };
466
467 template <> struct ProcessCurveTraits<OdDgCurveElement2dPtr>
468 {
469 typedef OdDgCurve2d CurveType;
470 typedef OdDgArc2d ArcType;
471 typedef OdDgBSplineCurve2d BSplineType;
472 typedef OdDgEllipse2d EllipseType;
473 typedef OdGePoint2d PointType;
474
475 static void setPoint(OGRSimpleCurve *poSC, int i, OdGePoint2d point)
476 {
477 poSC->setPoint(i, point.x, point.y);
478 }
479 };
480
481 template <> struct ProcessCurveTraits<OdDgCurveElement3dPtr>
482 {
483 typedef OdDgCurve3d CurveType;
484 typedef OdDgArc3d ArcType;
485 typedef OdDgBSplineCurve3d BSplineType;
486 typedef OdDgEllipse3d EllipseType;
487 typedef OdGePoint3d PointType;
488
489 static void setPoint(OGRSimpleCurve *poSC, int i, OdGePoint3d point)
490 {
491 poSC->setPoint(i, point.x, point.y, point.z);
492 }
493 };
494
495 /************************************************************************/
496 /* ProcessCurve() */
497 /************************************************************************/
498
499 template <class CurveElementPtr>
500 static void ProcessCurve(OGRFeature *poFeature, const CPLString &osPen,
501 CurveElementPtr curveElement)
502 {
503 typedef typename ProcessCurveTraits<CurveElementPtr>::CurveType CurveType;
504 typedef typename ProcessCurveTraits<CurveElementPtr>::ArcType ArcType;
505 typedef
506 typename ProcessCurveTraits<CurveElementPtr>::BSplineType BSplineType;
507 typedef
508 typename ProcessCurveTraits<CurveElementPtr>::EllipseType EllipseType;
509 typedef typename ProcessCurveTraits<CurveElementPtr>::PointType PointType;
510
511 OdSmartPtr<CurveType> curve = CurveType::cast(curveElement);
512 OdSmartPtr<ArcType> arc = ArcType::cast(curveElement);
513 OdSmartPtr<BSplineType> bspline = BSplineType::cast(curveElement);
514 OdSmartPtr<EllipseType> ellipse = EllipseType::cast(curveElement);
515
516 bool bIsCircular = false;
517 if (!ellipse.isNull())
518 {
519 bIsCircular =
520 AlmostEqual(ellipse->getPrimaryAxis(), ellipse->getSecondaryAxis());
521 }
522 else if (!arc.isNull())
523 {
524 bIsCircular =
525 AlmostEqual(arc->getPrimaryAxis(), arc->getSecondaryAxis());
526 }
527
528 double dfStartParam, dfEndParam;
529 OdResult eRes = curveElement->getStartParam(dfStartParam);
530 CPL_IGNORE_RET_VAL(eRes);
531 CPLAssert(eRes == eOk);
532 eRes = curveElement->getEndParam(dfEndParam);
533 CPL_IGNORE_RET_VAL(eRes);
534 CPLAssert(eRes == eOk);
535
536 CPLString osStyle(osPen);
537 bool bIsFilled = false;
538 if (!ellipse.isNull())
539 {
540 osStyle = ConsiderBrush(curveElement, osPen);
541 bIsFilled = osStyle.find("BRUSH") == 0;
542 }
543
544 OGRSimpleCurve *poSC;
545 int nPoints;
546 if (!bspline.isNull())
547 {
548 // 10 is somewhat arbitrary
549 nPoints = 10 * bspline->numControlPoints();
550 poSC = new OGRLineString();
551 }
552 else if (!curve.isNull())
553 {
554 // 5 is what is used in DGN driver
555 nPoints = 5 * curve->getVerticesCount();
556 poSC = new OGRLineString();
557 }
558 else if (bIsCircular)
559 {
560 poSC = new OGRCircularString();
561 if (!ellipse.isNull())
562 {
563 nPoints = 5;
564 }
565 else
566 {
567 nPoints = 3;
568 }
569 }
570 else
571 {
572 if (bIsFilled)
573 poSC = new OGRLinearRing();
574 else
575 poSC = new OGRLineString();
576 const double dfArcStepSize =
577 OGRGeometryFactory::GetDefaultArcStepSize();
578 if (!ellipse.isNull())
579 {
580 nPoints = std::max(2, static_cast<int>(360 / dfArcStepSize));
581 }
582 else
583 {
584 nPoints = std::max(2, static_cast<int>(arc->getSweepAngle() *
585 RAD_TO_DEG / dfArcStepSize));
586 }
587 }
588
589 poSC->setNumPoints(nPoints);
590 for (int i = 0; i < nPoints; i++)
591 {
592 PointType point;
593 double dfParam =
594 dfStartParam + i * (dfEndParam - dfStartParam) / (nPoints - 1);
595 eRes = curveElement->getPointAtParam(dfParam, point);
596 CPL_IGNORE_RET_VAL(eRes);
597 CPLAssert(eRes == eOk);
598 ProcessCurveTraits<CurveElementPtr>::setPoint(poSC, i, point);
599 }
600
601 if (bIsFilled)
602 {
603 if (bIsCircular)
604 {
605 OGRCurvePolygon *poCP = new OGRCurvePolygon();
606 poCP->addRingDirectly(poSC);
607 poFeature->SetGeometryDirectly(poCP);
608 }
609 else
610 {
611 OGRPolygon *poPoly = new OGRPolygon();
612 poPoly->addRingDirectly(poSC);
613 poFeature->SetGeometryDirectly(poPoly);
614 }
615 }
616 else
617 {
618 poFeature->SetGeometryDirectly(poSC);
619 }
620 poFeature->SetStyleString(osStyle);
621 }
622
623 /************************************************************************/
624 /* IsContiguous() */
625 /************************************************************************/
626
627 static bool
628 IsContiguous(const std::vector<tPairFeatureHoleFlag> &oVectorSubElts,
629 bool &bHasCurves, bool &bIsClosed)
630 {
631 bHasCurves = false;
632 bIsClosed = false;
633 OGRPoint oFirstPoint, oLastPoint;
634 bool bLastPointValid = false;
635 bool bIsContiguous = true;
636 for (size_t i = 0; i < oVectorSubElts.size(); i++)
637 {
638 OGRGeometry *poGeom = oVectorSubElts[i].first->GetGeometryRef();
639 if (poGeom != nullptr)
640 {
641 OGRwkbGeometryType eType = wkbFlatten(poGeom->getGeometryType());
642 if (eType == wkbCircularString)
643 {
644 bHasCurves = true;
645 }
646 if (bIsContiguous &&
647 (eType == wkbCircularString || eType == wkbLineString))
648 {
649 OGRCurve *poCurve = poGeom->toCurve();
650 if (poCurve->getNumPoints() >= 2)
651 {
652 OGRPoint oStartPoint;
653 poCurve->StartPoint(&oStartPoint);
654
655 if (bLastPointValid)
656 {
657 if (!AlmostEqual(oStartPoint.getX(),
658 oLastPoint.getX()) ||
659 !AlmostEqual(oStartPoint.getY(),
660 oLastPoint.getY()) ||
661 !AlmostEqual(oStartPoint.getZ(), oLastPoint.getZ()))
662 {
663 bIsContiguous = false;
664 break;
665 }
666 }
667 else
668 {
669 oFirstPoint = oStartPoint;
670 }
671 bLastPointValid = true;
672 poCurve->EndPoint(&oLastPoint);
673 }
674 else
675 {
676 bIsContiguous = false;
677 break;
678 }
679 }
680 else
681 {
682 bIsContiguous = false;
683 break;
684 }
685 }
686 }
687 if (bIsContiguous)
688 {
689 bIsClosed = bLastPointValid &&
690 AlmostEqual(oFirstPoint.getX(), oLastPoint.getX()) &&
691 AlmostEqual(oFirstPoint.getY(), oLastPoint.getY()) &&
692 AlmostEqual(oFirstPoint.getZ(), oLastPoint.getZ());
693 }
694 return bIsContiguous;
695 }
696
697 /************************************************************************/
698 /* ProcessElement() */
699 /************************************************************************/
700
701 std::vector<tPairFeatureHoleFlag>
702 OGRDGNV8Layer::ProcessElement(OdDgGraphicsElementPtr element, int level)
703 {
704 std::vector<tPairFeatureHoleFlag> oVector;
705 #ifdef DEBUG_VERBOSE
706 CPLString osIndent;
707 for (int i = 0; i < level; i++)
708 osIndent += " ";
709 #endif
710
711 bool bHoleFlag = false;
712 OGRFeature *poFeature = new OGRFeature(m_poFeatureDefn);
713
714 OdRxClass *poClass = element->isA();
715 const OdString osName = poClass->name();
716 const char *pszEntityClassName = static_cast<const char *>(osName);
717 poFeature->SetFID(static_cast<GIntBig>(
718 static_cast<OdUInt64>(element->elementId().getHandle())));
719 #ifdef DEBUG_VERBOSE
720 fprintf(stdout, "%s%s\n", osIndent.c_str(), pszEntityClassName);
721 fprintf(
722 stdout, "%sID = %s\n", osIndent.c_str(),
723 static_cast<const char *>(element->elementId().getHandle().ascii()));
724 fprintf(stdout, "%sType = %d\n", osIndent.c_str(),
725 element->getElementType());
726 #endif
727
728 poFeature->SetField("Type", element->getElementType());
729 const int nLevel = static_cast<int>(element->getLevelEntryId());
730 poFeature->SetField("Level", nLevel);
731 poFeature->SetField("GraphicGroup",
732 static_cast<int>(element->getGraphicsGroupEntryId()));
733 const OdUInt32 uColorIndex = element->getColorIndex();
734 CPLString osColor;
735 if (uColorIndex != OdDg::kColorByLevel && uColorIndex != OdDg::kColorByCell)
736 {
737 poFeature->SetField("ColorIndex", static_cast<int>(uColorIndex));
738
739 const ODCOLORREF color = element->getColor();
740 osColor = CPLSPrintf(",c:#%02x%02x%02x", ODGETRED(color),
741 ODGETGREEN(color), ODGETBLUE(color));
742 }
743 const OdInt32 nLineStyle = element->getLineStyleEntryId();
744 if (nLineStyle != OdDg::kLineStyleByLevel &&
745 nLineStyle != OdDg::kLineStyleByCell)
746 {
747 poFeature->SetField("Style", nLineStyle);
748 }
749
750 const OdUInt32 uLineWeight = element->getLineWeight();
751 int nLineWeight = 0;
752 if (uLineWeight != OdDg::kLineWeightByLevel &&
753 uLineWeight != OdDg::kLineWeightByCell)
754 {
755 nLineWeight = static_cast<int>(uLineWeight);
756 poFeature->SetField("Weight", nLineWeight);
757 }
758
759 CPLJSONObject uLinkData;
760 CPLJSONArray previousValues;
761
762 OdRxObjectPtrArray linkages;
763 element->getLinkages(linkages);
764 if (linkages.size() > 0)
765 {
766 for (unsigned i = 0; i < linkages.size(); ++i)
767 {
768 OdDgAttributeLinkagePtr pLinkage = linkages[i];
769 OdUInt16 primaryId = pLinkage->getPrimaryId();
770 CPLString primaryIdStr = CPLSPrintf("%d", primaryId);
771
772 OdBinaryData pabyData;
773 pLinkage->getData(pabyData);
774
775 previousValues = uLinkData.GetArray(primaryIdStr.c_str());
776 if (!previousValues.IsValid())
777 {
778 uLinkData.Add(primaryIdStr.c_str(), CPLJSONArray());
779 previousValues = uLinkData.GetArray(primaryIdStr.c_str());
780 }
781 CPLJSONObject theNewObject = CPLJSONObject();
782 GByte *pData = pabyData.asArrayPtr();
783 int nSize = pabyData.size();
784 theNewObject.Add("size", nSize);
785 previousValues.Add(theNewObject);
786 switch (primaryId)
787 {
788 case OdDgAttributeLinkage::kFRAMME: // DB Linkage - FRAMME tag
789 // data signature
790 case OdDgAttributeLinkage::kBSI: // DB Linkage - secondary id
791 // link (BSI radix 50)
792 case OdDgAttributeLinkage::kXBASE: // DB Linkage - XBase
793 // (DBase)
794 case OdDgAttributeLinkage::kINFORMIX: // DB Linkage - Informix
795 case OdDgAttributeLinkage::kINGRES: // DB Linkage - INGRES
796 case OdDgAttributeLinkage::kSYBASE: // DB Linkage - Sybase
797 case OdDgAttributeLinkage::kODBC: // DB Linkage - ODBC
798 case OdDgAttributeLinkage::kOLEDB: // DB Linkage - OLEDB
799 case OdDgAttributeLinkage::kORACLE: // DB Linkage - Oracle
800 case OdDgAttributeLinkage::kRIS: // DB Linkage - RIS
801 {
802 OdDgDBLinkagePtr dbLinkage = OdDgDBLinkage::cast(pLinkage);
803 if (!dbLinkage.isNull())
804 {
805 std::string namedType;
806
807 switch (dbLinkage->getDBType())
808 {
809 case OdDgDBLinkage::kBSI:
810 namedType.assign("BSI");
811 break;
812 case OdDgDBLinkage::kFRAMME:
813 namedType.assign("FRAMME");
814 break;
815 case OdDgDBLinkage::kInformix:
816 namedType.assign("Informix");
817 break;
818 case OdDgDBLinkage::kIngres:
819 namedType.assign("Ingres");
820 break;
821 case OdDgDBLinkage::kODBC:
822 namedType.assign("ODBC");
823 break;
824 case OdDgDBLinkage::kOLEDB:
825 namedType.assign("OLE DB");
826 break;
827 case OdDgDBLinkage::kOracle:
828 namedType.assign("Oracle");
829 break;
830 case OdDgDBLinkage::kRIS:
831 namedType.assign("RIS");
832 break;
833 case OdDgDBLinkage::kSybase:
834 namedType.assign("Sybase");
835 break;
836 case OdDgDBLinkage::kXbase:
837 namedType.assign("xBase");
838 break;
839 default:
840 namedType.assign("Unknown");
841 break;
842 }
843
844 theNewObject.Add("tableId",
845 int(dbLinkage->getTableEntityId()));
846 theNewObject.Add("MSLink", int(dbLinkage->getMSLink()));
847 theNewObject.Add("type", namedType);
848 }
849 }
850 break;
851 case 0x1995: // 0x1995 (6549) IPCC/Portugal
852 {
853 theNewObject.Add("domain", CPLSPrintf("0x%02x", pData[5]));
854 theNewObject.Add("subdomain",
855 CPLSPrintf("0x%02x", pData[4]));
856 theNewObject.Add("family", CPLSPrintf("0x%02x", pData[7]));
857 theNewObject.Add("object", CPLSPrintf("0x%02x", pData[6]));
858 theNewObject.Add("key",
859 CPLSPrintf("%02x%02x%02x%02x", pData[5],
860 pData[4], pData[7], pData[6]));
861 theNewObject.Add("type", "IPCC/Portugal");
862 }
863 break;
864 case OdDgAttributeLinkage::kString: // 0x56d2 (22226):
865 {
866 OdDgStringLinkagePtr pStrLinkage =
867 OdDgStringLinkage::cast(pLinkage);
868 if (!pStrLinkage.isNull())
869 {
870 theNewObject.Add(
871 "string", ToUTF8(pStrLinkage->getString()).c_str());
872 theNewObject.Add("type", "string");
873 }
874 }
875 break;
876 default:
877 {
878 CPLJSONArray rawWords;
879 for (int k = 0; k < nSize - 1; k += 2)
880 {
881 rawWords.Add(
882 CPLSPrintf("0x%02x%02x", pData[k + 1], pData[k]));
883 }
884 theNewObject.Add("raw", rawWords);
885 theNewObject.Add("type", "unknown");
886 }
887 break;
888 }
889 }
890
891 poFeature->SetField("ULink", uLinkData.ToString().c_str());
892 }
893
894 /* -------------------------------------------------------------------- */
895 /* Generate corresponding PEN style. */
896 /* -------------------------------------------------------------------- */
897 CPLString osPen;
898
899 if (nLineStyle == DGNS_SOLID)
900 osPen = "PEN(id:\"ogr-pen-0\"";
901 else if (nLineStyle == DGNS_DOTTED)
902 osPen = "PEN(id:\"ogr-pen-5\"";
903 else if (nLineStyle == DGNS_MEDIUM_DASH)
904 osPen = "PEN(id:\"ogr-pen-2\"";
905 else if (nLineStyle == DGNS_LONG_DASH)
906 osPen = "PEN(id:\"ogr-pen-4\"";
907 else if (nLineStyle == DGNS_DOT_DASH)
908 osPen = "PEN(id:\"ogr-pen-6\"";
909 else if (nLineStyle == DGNS_SHORT_DASH)
910 osPen = "PEN(id:\"ogr-pen-3\"";
911 else if (nLineStyle == DGNS_DASH_DOUBLE_DOT)
912 osPen = "PEN(id:\"ogr-pen-7\"";
913 else if (nLineStyle == DGNS_LONG_DASH_SHORT_DASH)
914 osPen = "PEN(p:\"10px 5px 4px 5px\"";
915 else
916 osPen = "PEN(id:\"ogr-pen-0\"";
917
918 osPen += osColor;
919
920 if (nLineWeight > 1)
921 osPen += CPLSPrintf(",w:%dpx", nLineWeight);
922
923 osPen += ")";
924
925 if (EQUAL(pszEntityClassName, "OdDgCellHeader2d") ||
926 EQUAL(pszEntityClassName, "OdDgCellHeader3d"))
927 {
928 bool bDestroyFeature = true;
929 OdDgElementIteratorPtr iterator;
930 if (EQUAL(pszEntityClassName, "OdDgCellHeader2d"))
931 {
932 OdDgCellHeader2dPtr elementCell = OdDgCellHeader2d::cast(element);
933 CPLAssert(!elementCell.isNull());
934 iterator = elementCell->createIterator();
935 }
936 else
937 {
938 OdDgCellHeader3dPtr elementCell = OdDgCellHeader3d::cast(element);
939 CPLAssert(!elementCell.isNull());
940 iterator = elementCell->createIterator();
941 }
942 if (!iterator.isNull())
943 {
944 oVector = CollectSubElements(iterator, level + 1);
945 int nCountMain = 0;
946 bool bHasHole = false;
947 bool bHasCurve = false;
948 OGRGeometry *poExterior = nullptr;
949 for (size_t i = 0; i < oVector.size(); i++)
950 {
951 OGRFeature *poFeat = oVector[i].first;
952 CPLAssert(poFeat);
953 OGRGeometry *poGeom = poFeat->GetGeometryRef();
954 if (poGeom == nullptr)
955 {
956 nCountMain = 0;
957 break;
958 }
959 const OGRwkbGeometryType eType =
960 wkbFlatten(poGeom->getGeometryType());
961 if ((eType == wkbPolygon || eType == wkbCurvePolygon) &&
962 poGeom->toCurvePolygon()->getNumInteriorRings() == 0)
963 {
964 if (eType == wkbCurvePolygon)
965 bHasCurve = true;
966 if (oVector[i].second)
967 bHasHole = true;
968 else
969 {
970 poExterior = poGeom;
971 nCountMain++;
972 }
973 }
974 else
975 {
976 nCountMain = 0;
977 break;
978 }
979 }
980 if (nCountMain == 1 && bHasHole)
981 {
982 bDestroyFeature = false;
983 OGRCurvePolygon *poCP;
984 if (bHasCurve)
985 poCP = new OGRCurvePolygon();
986 else
987 poCP = new OGRPolygon();
988 poCP->addRing(
989 poExterior->toCurvePolygon()->getExteriorRingCurve());
990 for (size_t i = 0; i < oVector.size(); i++)
991 {
992 OGRFeature *poFeat = oVector[i].first;
993 OGRGeometry *poGeom = poFeat->GetGeometryRef();
994 if (poGeom != poExterior)
995 {
996 poCP->addRing(
997 poGeom->toCurvePolygon()->getExteriorRingCurve());
998 }
999 delete poFeat;
1000 }
1001 oVector.clear();
1002 poFeature->SetGeometryDirectly(poCP);
1003 poFeature->SetStyleString(ConsiderBrush(element, osPen));
1004 }
1005 }
1006 if (bDestroyFeature)
1007 {
1008 delete poFeature;
1009 poFeature = nullptr;
1010 }
1011 }
1012 else if (EQUAL(pszEntityClassName, "OdDgText2d"))
1013 {
1014 OdDgText2dPtr text = OdDgText2d::cast(element);
1015 CPLAssert(!text.isNull());
1016 ProcessText(poFeature, osColor, text);
1017 }
1018 else if (EQUAL(pszEntityClassName, "OdDgText3d"))
1019 {
1020 OdDgText3dPtr text = OdDgText3d::cast(element);
1021 CPLAssert(!text.isNull());
1022 ProcessText(poFeature, osColor, text);
1023 }
1024 else if (EQUAL(pszEntityClassName, "OdDgTextNode2d") ||
1025 EQUAL(pszEntityClassName, "OdDgTextNode3d"))
1026 {
1027 OdDgElementIteratorPtr iterator;
1028 if (EQUAL(pszEntityClassName, "OdDgTextNode2d"))
1029 {
1030 OdDgTextNode2dPtr textNode = OdDgTextNode2d::cast(element);
1031 CPLAssert(!textNode.isNull());
1032 iterator = textNode->createIterator();
1033 }
1034 else
1035 {
1036 OdDgTextNode3dPtr textNode = OdDgTextNode3d::cast(element);
1037 CPLAssert(!textNode.isNull());
1038 iterator = textNode->createIterator();
1039 }
1040 if (!iterator.isNull())
1041 {
1042 oVector = CollectSubElements(iterator, level + 1);
1043 }
1044 delete poFeature;
1045 poFeature = nullptr;
1046 }
1047 else if (EQUAL(pszEntityClassName, "OdDgLine2d"))
1048 {
1049 OdDgLine2dPtr line = OdDgLine2d::cast(element);
1050 CPLAssert(!line.isNull());
1051 OdGePoint2d pointStart = line->getStartPoint();
1052 OdGePoint2d pointEnd = line->getEndPoint();
1053 if (pointStart == pointEnd)
1054 {
1055 poFeature->SetGeometryDirectly(
1056 new OGRPoint(pointStart.x, pointStart.y));
1057 }
1058 else
1059 {
1060 OGRLineString *poLS = new OGRLineString();
1061 poLS->setNumPoints(2);
1062 poLS->setPoint(0, pointStart.x, pointStart.y);
1063 poLS->setPoint(1, pointEnd.x, pointEnd.y);
1064 poFeature->SetGeometryDirectly(poLS);
1065 poFeature->SetStyleString(osPen);
1066 }
1067 }
1068 else if (EQUAL(pszEntityClassName, "OdDgLine3d"))
1069 {
1070 OdDgLine3dPtr line = OdDgLine3d::cast(element);
1071 CPLAssert(!line.isNull());
1072 OdGePoint3d pointStart = line->getStartPoint();
1073 OdGePoint3d pointEnd = line->getEndPoint();
1074 if (pointStart == pointEnd)
1075 {
1076 poFeature->SetGeometryDirectly(
1077 new OGRPoint(pointStart.x, pointStart.y, pointStart.z));
1078 }
1079 else
1080 {
1081 OGRLineString *poLS = new OGRLineString();
1082 poLS->setNumPoints(2);
1083 poLS->setPoint(0, pointStart.x, pointStart.y, pointStart.z);
1084 poLS->setPoint(1, pointEnd.x, pointEnd.y, pointEnd.z);
1085 poFeature->SetGeometryDirectly(poLS);
1086 poFeature->SetStyleString(osPen);
1087 }
1088 }
1089 else if (EQUAL(pszEntityClassName, "OdDgLineString2d"))
1090 {
1091 OdDgLineString2dPtr line = OdDgLineString2d::cast(element);
1092 CPLAssert(!line.isNull());
1093 const int nPoints = line->getVerticesCount();
1094
1095 OGRLineString *poLS = new OGRLineString();
1096 poLS->setNumPoints(nPoints);
1097 for (int i = 0; i < nPoints; i++)
1098 {
1099 OdGePoint2d point = line->getVertexAt(i);
1100 poLS->setPoint(i, point.x, point.y);
1101 }
1102 poFeature->SetGeometryDirectly(poLS);
1103 poFeature->SetStyleString(osPen);
1104 }
1105 else if (EQUAL(pszEntityClassName, "OdDgLineString3d"))
1106 {
1107 OdDgLineString3dPtr line = OdDgLineString3d::cast(element);
1108 CPLAssert(!line.isNull());
1109 const int nPoints = line->getVerticesCount();
1110
1111 OGRLineString *poLS = new OGRLineString();
1112 poLS->setNumPoints(nPoints);
1113 for (int i = 0; i < nPoints; i++)
1114 {
1115 OdGePoint3d point = line->getVertexAt(i);
1116 poLS->setPoint(i, point.x, point.y, point.z);
1117 }
1118 poFeature->SetGeometryDirectly(poLS);
1119 poFeature->SetStyleString(osPen);
1120 }
1121 else if (EQUAL(pszEntityClassName, "OdDgPointString2d"))
1122 {
1123 OdDgPointString2dPtr string = OdDgPointString2d::cast(element);
1124 CPLAssert(!string.isNull());
1125 const int nPoints = string->getVerticesCount();
1126
1127 // Not sure this is the right way to model this
1128 // We lose the rotation per vertex.
1129 OGRMultiPoint *poMP = new OGRMultiPoint();
1130 for (int i = 0; i < nPoints; i++)
1131 {
1132 OdGePoint2d point = string->getVertexAt(i);
1133 poMP->addGeometryDirectly(new OGRPoint(point.x, point.y));
1134 }
1135 poFeature->SetGeometryDirectly(poMP);
1136 }
1137 else if (EQUAL(pszEntityClassName, "OdDgPointString3d"))
1138 {
1139 OdDgPointString3dPtr string = OdDgPointString3d::cast(element);
1140 CPLAssert(!string.isNull());
1141 const int nPoints = string->getVerticesCount();
1142
1143 // Not sure this is the right way to model this
1144 // We lose the rotation per vertex.
1145 OGRMultiPoint *poMP = new OGRMultiPoint();
1146 for (int i = 0; i < nPoints; i++)
1147 {
1148 OdGePoint3d point = string->getVertexAt(i);
1149 poMP->addGeometryDirectly(new OGRPoint(point.x, point.y, point.z));
1150 }
1151 poFeature->SetGeometryDirectly(poMP);
1152 }
1153 else if (EQUAL(pszEntityClassName, "OdDgMultiline"))
1154 {
1155 // This is a poor approximation since a multiline is a central line
1156 // with parallel lines.
1157 OdDgMultilinePtr line = OdDgMultiline::cast(element);
1158 CPLAssert(!line.isNull());
1159 const int nPoints = static_cast<int>(line->getPointsCount());
1160
1161 OGRLineString *poLS = new OGRLineString();
1162 poLS->setNumPoints(nPoints);
1163 for (int i = 0; i < nPoints; i++)
1164 {
1165 OdDgMultilinePoint point;
1166 OdGePoint3d point3d;
1167 line->getPoint(i, point);
1168 point.getPoint(point3d);
1169 poLS->setPoint(i, point3d.x, point3d.y, point3d.z);
1170 }
1171 poFeature->SetGeometryDirectly(poLS);
1172 poFeature->SetStyleString(osPen);
1173 }
1174 else if (EQUAL(pszEntityClassName, "OdDgArc2d") ||
1175 EQUAL(pszEntityClassName, "OdDgCurve2d") ||
1176 EQUAL(pszEntityClassName, "OdDgBSplineCurve2d") ||
1177 EQUAL(pszEntityClassName, "OdDgEllipse2d"))
1178 {
1179 OdDgCurveElement2dPtr curveElement = OdDgCurveElement2d::cast(element);
1180 CPLAssert(!curveElement.isNull());
1181
1182 ProcessCurve(poFeature, osPen, curveElement);
1183 }
1184 else if (EQUAL(pszEntityClassName, "OdDgArc3d") ||
1185 EQUAL(pszEntityClassName, "OdDgCurve3d") ||
1186 EQUAL(pszEntityClassName, "OdDgBSplineCurve3d") ||
1187 EQUAL(pszEntityClassName, "OdDgEllipse3d"))
1188 {
1189 OdDgCurveElement3dPtr curveElement = OdDgCurveElement3d::cast(element);
1190 CPLAssert(!curveElement.isNull());
1191
1192 ProcessCurve(poFeature, osPen, curveElement);
1193 }
1194 else if (EQUAL(pszEntityClassName, "OdDgShape2d"))
1195 {
1196 OdDgShape2dPtr shape = OdDgShape2d::cast(element);
1197 CPLAssert(!shape.isNull());
1198 bHoleFlag = shape->getHoleFlag();
1199 const int nPoints = shape->getVerticesCount();
1200
1201 OGRLinearRing *poLS = new OGRLinearRing();
1202 poLS->setNumPoints(nPoints);
1203 for (int i = 0; i < nPoints; i++)
1204 {
1205 OdGePoint2d point = shape->getVertexAt(i);
1206 poLS->setPoint(i, point.x, point.y);
1207 }
1208 OGRPolygon *poPoly = new OGRPolygon();
1209 poPoly->addRingDirectly(poLS);
1210 poFeature->SetGeometryDirectly(poPoly);
1211 poFeature->SetStyleString(ConsiderBrush(element, osPen));
1212 }
1213 else if (EQUAL(pszEntityClassName, "OdDgShape3d"))
1214 {
1215 OdDgShape3dPtr shape = OdDgShape3d::cast(element);
1216 CPLAssert(!shape.isNull());
1217 bHoleFlag = shape->getHoleFlag();
1218 const int nPoints = shape->getVerticesCount();
1219
1220 OGRLinearRing *poLS = new OGRLinearRing();
1221 poLS->setNumPoints(nPoints);
1222 for (int i = 0; i < nPoints; i++)
1223 {
1224 OdGePoint3d point = shape->getVertexAt(i);
1225 poLS->setPoint(i, point.x, point.y, point.z);
1226 }
1227 OGRPolygon *poPoly = new OGRPolygon();
1228 poPoly->addRingDirectly(poLS);
1229 poFeature->SetGeometryDirectly(poPoly);
1230 poFeature->SetStyleString(ConsiderBrush(element, osPen));
1231 }
1232 else if (EQUAL(pszEntityClassName, "OdDgComplexString"))
1233 {
1234 OdDgComplexStringPtr complex = OdDgComplexString::cast(element);
1235 CPLAssert(!complex.isNull());
1236
1237 OdDgElementIteratorPtr iterator = complex->createIterator();
1238 if (!iterator.isNull())
1239 {
1240 std::vector<tPairFeatureHoleFlag> oVectorSubElts =
1241 CollectSubElements(iterator, level + 1);
1242
1243 // First pass to determine if we have non-linear pieces.
1244 bool bHasCurves = false;
1245 bool bIsClosed = false;
1246 bool bIsContiguous =
1247 IsContiguous(oVectorSubElts, bHasCurves, bIsClosed);
1248
1249 if (bIsContiguous && bHasCurves)
1250 {
1251 OGRCompoundCurve *poCC = new OGRCompoundCurve();
1252
1253 // Second pass to aggregate the geometries.
1254 for (size_t i = 0; i < oVectorSubElts.size(); i++)
1255 {
1256 OGRGeometry *poGeom =
1257 oVectorSubElts[i].first->GetGeometryRef();
1258 if (poGeom != nullptr)
1259 {
1260 OGRwkbGeometryType eType =
1261 wkbFlatten(poGeom->getGeometryType());
1262 if (eType == wkbCircularString ||
1263 eType == wkbLineString)
1264 {
1265 poCC->addCurve(poGeom->toCurve(),
1266 CONTIGUITY_TOLERANCE);
1267 }
1268 }
1269 delete oVectorSubElts[i].first;
1270 }
1271
1272 poFeature->SetGeometryDirectly(poCC);
1273 }
1274 else
1275 {
1276 OGRMultiCurve *poMC;
1277 if (bHasCurves)
1278 poMC = new OGRMultiCurve();
1279 else
1280 poMC = new OGRMultiLineString();
1281
1282 // Second pass to aggregate the geometries.
1283 for (size_t i = 0; i < oVectorSubElts.size(); i++)
1284 {
1285 OGRGeometry *poGeom =
1286 oVectorSubElts[i].first->GetGeometryRef();
1287 if (poGeom != nullptr)
1288 {
1289 OGRwkbGeometryType eType =
1290 wkbFlatten(poGeom->getGeometryType());
1291 if (eType == wkbCircularString ||
1292 eType == wkbLineString)
1293 {
1294 poMC->addGeometry(poGeom);
1295 }
1296 }
1297 delete oVectorSubElts[i].first;
1298 }
1299
1300 poFeature->SetGeometryDirectly(poMC);
1301 }
1302 poFeature->SetStyleString(osPen);
1303 }
1304 }
1305 else if (EQUAL(pszEntityClassName, "OdDgComplexShape"))
1306 {
1307 OdDgComplexCurvePtr complex = OdDgComplexCurve::cast(element);
1308 CPLAssert(!complex.isNull());
1309
1310 OdDgComplexShapePtr complexShape = OdDgComplexShape::cast(element);
1311 CPLAssert(!complexShape.isNull());
1312 bHoleFlag = complexShape->getHoleFlag();
1313
1314 OdDgElementIteratorPtr iterator = complex->createIterator();
1315 if (!iterator.isNull())
1316 {
1317 std::vector<tPairFeatureHoleFlag> oVectorSubElts =
1318 CollectSubElements(iterator, level + 1);
1319
1320 // First pass to determine if we have non-linear pieces.
1321 bool bHasCurves = false;
1322 bool bIsClosed = false;
1323 bool bIsContiguous =
1324 IsContiguous(oVectorSubElts, bHasCurves, bIsClosed);
1325
1326 if (bIsContiguous && bIsClosed)
1327 {
1328 OGRCurvePolygon *poCP;
1329 OGRCompoundCurve *poCC = nullptr;
1330 OGRLinearRing *poLR = nullptr;
1331
1332 if (bHasCurves)
1333 {
1334 poCP = new OGRCurvePolygon();
1335 poCC = new OGRCompoundCurve();
1336 }
1337 else
1338 {
1339 poCP = new OGRPolygon();
1340 poLR = new OGRLinearRing();
1341 }
1342
1343 // Second pass to aggregate the geometries.
1344 for (size_t i = 0; i < oVectorSubElts.size(); i++)
1345 {
1346 OGRGeometry *poGeom =
1347 oVectorSubElts[i].first->GetGeometryRef();
1348 if (poGeom != nullptr)
1349 {
1350 OGRwkbGeometryType eType =
1351 wkbFlatten(poGeom->getGeometryType());
1352 if (poCC != nullptr)
1353 {
1354 poCC->addCurve(poGeom->toCurve(),
1355 CONTIGUITY_TOLERANCE);
1356 }
1357 else if (eType == wkbLineString)
1358 {
1359 poLR->addSubLineString(
1360 poGeom->toLineString(),
1361 poLR->getNumPoints() == 0 ? 0 : 1);
1362 }
1363 }
1364 delete oVectorSubElts[i].first;
1365 }
1366
1367 poCP->addRingDirectly((bHasCurves) ? poCC->toCurve()
1368 : poLR->toCurve());
1369
1370 poFeature->SetGeometryDirectly(poCP);
1371 }
1372 else
1373 {
1374 OGRGeometryCollection oGC;
1375 for (size_t i = 0; i < oVectorSubElts.size(); i++)
1376 {
1377 OGRGeometry *poGeom =
1378 oVectorSubElts[i].first->StealGeometry();
1379 if (poGeom != nullptr)
1380 {
1381 OGRwkbGeometryType eType =
1382 wkbFlatten(poGeom->getGeometryType());
1383 if (eType == wkbCircularString)
1384 {
1385 oGC.addGeometryDirectly(
1386 OGRGeometryFactory::forceToLineString(poGeom));
1387 }
1388 else if (eType == wkbLineString)
1389 {
1390 oGC.addGeometryDirectly(poGeom);
1391 }
1392 else
1393 {
1394 delete poGeom;
1395 }
1396 }
1397 delete oVectorSubElts[i].first;
1398 }
1399
1400 // Try to assemble into polygon geometry.
1401 OGRGeometry *poGeom =
1402 OGRGeometry::FromHandle(OGRBuildPolygonFromEdges(
1403 OGRGeometry::ToHandle(&oGC), TRUE, TRUE,
1404 CONTIGUITY_TOLERANCE, nullptr));
1405 poGeom->setCoordinateDimension(oGC.getCoordinateDimension());
1406 poFeature->SetGeometryDirectly(poGeom);
1407 }
1408 poFeature->SetStyleString(ConsiderBrush(element, osPen));
1409 }
1410 }
1411 else if (EQUAL(pszEntityClassName, "OdDgSharedCellReference"))
1412 {
1413 OdDgSharedCellReferencePtr ref = OdDgSharedCellReference::cast(element);
1414 CPLAssert(!ref.isNull());
1415 OdGePoint3d point = ref->getOrigin();
1416 poFeature->SetField("Text", ToUTF8(ref->getDefinitionName()).c_str());
1417 poFeature->SetGeometryDirectly(new OGRPoint(point.x, point.y, point.z));
1418 }
1419 else
1420 {
1421 if (m_aoSetIgnoredFeatureClasses.find(pszEntityClassName) ==
1422 m_aoSetIgnoredFeatureClasses.end())
1423 {
1424 m_aoSetIgnoredFeatureClasses.insert(pszEntityClassName);
1425 CPLDebug("DGNV8",
1426 "Unhandled class %s for, at least, "
1427 "feature " CPL_FRMT_GIB,
1428 pszEntityClassName, poFeature->GetFID());
1429 }
1430 }
1431
1432 if (poFeature != nullptr)
1433 oVector.push_back(tPairFeatureHoleFlag(poFeature, bHoleFlag));
1434
1435 return oVector;
1436 }
1437
1438 /************************************************************************/
1439 /* GetNextUnfilteredFeature() */
1440 /************************************************************************/
1441
1442 OGRFeature *OGRDGNV8Layer::GetNextUnfilteredFeature()
1443
1444 {
1445 while (true)
1446 {
1447 if (m_nIdxInPendingFeatures < m_aoPendingFeatures.size())
1448 {
1449 OGRFeature *poFeature =
1450 m_aoPendingFeatures[m_nIdxInPendingFeatures].first;
1451 m_aoPendingFeatures[m_nIdxInPendingFeatures].first = nullptr;
1452 m_nIdxInPendingFeatures++;
1453 return poFeature;
1454 }
1455
1456 if (m_pIterator.isNull())
1457 return nullptr;
1458
1459 while (true)
1460 {
1461 if (m_pIterator->done())
1462 return nullptr;
1463 OdRxObjectPtr object = m_pIterator->item().openObject();
1464 m_pIterator->step();
1465 OdDgGraphicsElementPtr element = OdDgGraphicsElement::cast(object);
1466 if (element.isNull())
1467 continue;
1468
1469 m_aoPendingFeatures = ProcessElement(element);
1470 m_nIdxInPendingFeatures = 0;
1471
1472 break;
1473 }
1474 }
1475 }
1476
1477 /************************************************************************/
1478 /* GetNextFeature() */
1479 /************************************************************************/
1480
1481 OGRFeature *OGRDGNV8Layer::GetNextFeature()
1482
1483 {
1484 while (true)
1485 {
1486 OGRFeature *poFeature = GetNextUnfilteredFeature();
1487 if (poFeature == nullptr)
1488 break;
1489 if (poFeature->GetGeometryRef() == nullptr)
1490 {
1491 delete poFeature;
1492 continue;
1493 }
1494
1495 if ((m_poAttrQuery == nullptr || m_poAttrQuery->Evaluate(poFeature)) &&
1496 FilterGeometry(poFeature->GetGeometryRef()))
1497 return poFeature;
1498
1499 delete poFeature;
1500 }
1501 return nullptr;
1502 }
1503
1504 /************************************************************************/
1505 /* GetFeatureInternal() */
1506 /************************************************************************/
1507
1508 OdDgGraphicsElementPtr
1509 OGRDGNV8Layer::GetFeatureInternal(GIntBig nFID, OdDg::OpenMode openMode)
1510 {
1511 if (nFID < 0)
1512 return OdDgGraphicsElementPtr();
1513 const OdDbHandle handle(static_cast<OdUInt64>(nFID));
1514 const OdDgElementId id = m_pModel->database()->getElementId(handle);
1515 OdRxObjectPtr object = id.openObject(openMode);
1516 OdDgGraphicsElementPtr element = OdDgGraphicsElement::cast(object);
1517 if (element.isNull() || element->ownerId() != m_pModel->elementId())
1518 return OdDgGraphicsElementPtr();
1519 return element;
1520 }
1521
1522 /************************************************************************/
1523 /* GetFeature() */
1524 /************************************************************************/
1525
1526 OGRFeature *OGRDGNV8Layer::GetFeature(GIntBig nFID)
1527 {
1528 OdDgGraphicsElementPtr element = GetFeatureInternal(nFID, OdDg::kForRead);
1529 if (element.isNull())
1530 return nullptr;
1531 std::vector<tPairFeatureHoleFlag> oVector = ProcessElement(element);
1532 // Only return a feature if and only if we have a single element
1533 if (oVector.empty())
1534 return nullptr;
1535 if (oVector.size() > 1)
1536 {
1537 for (size_t i = 0; i < oVector.size(); i++)
1538 delete oVector[i].first;
1539 return nullptr;
1540 }
1541 return oVector[0].first;
1542 }
1543
1544 /************************************************************************/
1545 /* DeleteFeature() */
1546 /************************************************************************/
1547
1548 OGRErr OGRDGNV8Layer::DeleteFeature(GIntBig nFID)
1549 {
1550 if (!m_poDS->GetUpdate())
1551 {
1552 CPLError(CE_Failure, CPLE_AppDefined,
1553 "Attempt to delete feature on read-only DGN file.");
1554 return OGRERR_FAILURE;
1555 }
1556
1557 OdDgGraphicsElementPtr element = GetFeatureInternal(nFID, OdDg::kForWrite);
1558 if (element.isNull())
1559 return OGRERR_FAILURE;
1560 try
1561 {
1562 element->erase(true);
1563 }
1564 catch (const OdError &e)
1565 {
1566 CPLError(CE_Failure, CPLE_AppDefined, "Teigha DGN error occurred: %s",
1567 ToUTF8(e.description()).c_str());
1568 return OGRERR_FAILURE;
1569 }
1570 catch (const std::exception &exc)
1571 {
1572 CPLError(CE_Failure, CPLE_AppDefined, "std::exception occurred: %s",
1573 exc.what());
1574 return OGRERR_FAILURE;
1575 }
1576 catch (...)
1577 {
1578 CPLError(CE_Failure, CPLE_AppDefined, "Unknown exception occurred");
1579 return OGRERR_FAILURE;
1580 }
1581 m_poDS->SetModified();
1582 return OGRERR_NONE;
1583 }
1584
1585 /************************************************************************/
1586 /* IGetExtent() */
1587 /************************************************************************/
1588
1589 OGRErr OGRDGNV8Layer::IGetExtent(int iGeomField, OGREnvelope *psExtent,
1590 bool bForce)
1591 {
1592 OdDgModel::StorageUnitDescription description;
1593 m_pModel->getStorageUnit(description);
1594 OdDgElementIteratorPtr iterator =
1595 m_pModel->createGraphicsElementsIterator();
1596 bool bValid = false;
1597 while (true)
1598 {
1599 if (iterator.isNull() || iterator->done())
1600 break;
1601 OdRxObjectPtr object = iterator->item().openObject();
1602 iterator->step();
1603 OdDgGraphicsElementPtr element = OdDgGraphicsElement::cast(object);
1604 if (element.isNull())
1605 continue;
1606 OdDgGraphicsElementPEPtr pElementPE =
1607 OdDgGraphicsElementPEPtr(OdRxObjectPtr(element));
1608 if (pElementPE.isNull())
1609 continue;
1610 OdGeExtents3d savedExtent;
1611 if (pElementPE->getRange(element, savedExtent) == eOk)
1612 {
1613 OdGePoint3d min = savedExtent.minPoint();
1614 OdGePoint3d max = savedExtent.maxPoint();
1615 if (!bValid)
1616 {
1617 psExtent->MinX = min.x / description.m_uorPerStorageUnit;
1618 psExtent->MinY = min.y / description.m_uorPerStorageUnit;
1619 psExtent->MaxX = max.x / description.m_uorPerStorageUnit;
1620 psExtent->MaxY = max.y / description.m_uorPerStorageUnit;
1621 bValid = true;
1622 }
1623 else
1624 {
1625 psExtent->MinX = std::min(
1626 psExtent->MinX, min.x / description.m_uorPerStorageUnit);
1627 psExtent->MinY = std::min(
1628 psExtent->MinY, min.y / description.m_uorPerStorageUnit);
1629 psExtent->MaxX = std::max(
1630 psExtent->MaxX, max.x / description.m_uorPerStorageUnit);
1631 psExtent->MaxY = std::max(
1632 psExtent->MaxY, max.y / description.m_uorPerStorageUnit);
1633 }
1634 }
1635 }
1636 if (bValid)
1637 return OGRERR_NONE;
1638 return OGRLayer::IGetExtent(iGeomField, psExtent, bForce);
1639 }
1640
1641 /************************************************************************/
1642 /* TestCapability() */
1643 /************************************************************************/
1644
1645 int OGRDGNV8Layer::TestCapability(const char *pszCap) const
1646
1647 {
1648 if (EQUAL(pszCap, OLCRandomRead))
1649 return TRUE;
1650 else if (EQUAL(pszCap, OLCStringsAsUTF8))
1651 return TRUE;
1652 else if (EQUAL(pszCap, OLCSequentialWrite) ||
1653 EQUAL(pszCap, OLCDeleteFeature))
1654 return m_poDS->GetUpdate();
1655 else if (EQUAL(pszCap, OLCCurveGeometries) || EQUAL(pszCap, OLCZGeometries))
1656 return TRUE;
1657
1658 return FALSE;
1659 }
1660
1661 /************************************************************************/
1662 /* ICreateFeature() */
1663 /* */
1664 /* Create a new feature and write to file. */
1665 /************************************************************************/
1666
1667 OGRErr OGRDGNV8Layer::ICreateFeature(OGRFeature *poFeature)
1668
1669 {
1670 if (!m_poDS->GetUpdate())
1671 {
1672 CPLError(CE_Failure, CPLE_AppDefined,
1673 "Attempt to create feature on read-only DGN file.");
1674 return OGRERR_FAILURE;
1675 }
1676
1677 if (poFeature->GetGeometryRef() == nullptr)
1678 {
1679 CPLError(CE_Failure, CPLE_AppDefined,
1680 "Features with empty, geometry collection geometries not "
1681 "supported in DGN format.");
1682 return OGRERR_FAILURE;
1683 }
1684
1685 try
1686 {
1687 OdDgGraphicsElementPtr element =
1688 CreateGraphicsElement(poFeature, poFeature->GetGeometryRef());
1689 if (element.isNull())
1690 return OGRERR_FAILURE;
1691 m_pModel->addElement(element);
1692 poFeature->SetFID(static_cast<GIntBig>(
1693 static_cast<OdUInt64>(element->elementId().getHandle())));
1694 }
1695 catch (const OdError &e)
1696 {
1697 CPLError(CE_Failure, CPLE_AppDefined, "Teigha DGN error occurred: %s",
1698 ToUTF8(e.description()).c_str());
1699 return OGRERR_FAILURE;
1700 }
1701 catch (const std::exception &exc)
1702 {
1703 CPLError(CE_Failure, CPLE_AppDefined, "std::exception occurred: %s",
1704 exc.what());
1705 return OGRERR_FAILURE;
1706 }
1707 catch (...)
1708 {
1709 CPLError(CE_Failure, CPLE_AppDefined, "Unknown exception occurred");
1710 return OGRERR_FAILURE;
1711 }
1712
1713 m_poDS->SetModified();
1714 return OGRERR_NONE;
1715 }
1716
1717 /************************************************************************/
1718 /* GetTool() */
1719 /************************************************************************/
1720
1721 static OGRStyleTool *GetTool(OGRFeature *poFeature, OGRSTClassId eClassId)
1722 {
1723
1724 OGRStyleMgr oMgr;
1725 oMgr.InitFromFeature(poFeature);
1726 for (int i = 0; i < oMgr.GetPartCount(); i++)
1727 {
1728 OGRStyleTool *poTool = oMgr.GetPart(i);
1729 if (poTool == nullptr || poTool->GetType() != eClassId)
1730 {
1731 delete poTool;
1732 }
1733 else
1734 {
1735 return poTool;
1736 }
1737 }
1738 return nullptr;
1739 }
1740
1741 /************************************************************************/
1742 /* TranslateLabel() */
1743 /************************************************************************/
1744
1745 OdDgGraphicsElementPtr OGRDGNV8Layer::TranslateLabel(OGRFeature *poFeature,
1746 OGRPoint *poPoint)
1747
1748 {
1749 const char *pszText = poFeature->GetFieldAsString("Text");
1750
1751 OGRStyleLabel *poLabel =
1752 static_cast<OGRStyleLabel *>(GetTool(poFeature, OGRSTCLabel));
1753
1754 OdDgText2dPtr text = OdDgText2d::createObject();
1755 OdGePoint2d point;
1756 point.x = poPoint->getX();
1757 point.y = poPoint->getY();
1758 text->setOrigin(point);
1759
1760 double dfHeightMultiplier = 1.0;
1761 if (poLabel != nullptr)
1762 {
1763 GBool bDefault;
1764
1765 if (poLabel->TextString(bDefault) != nullptr && !bDefault)
1766 pszText = poLabel->TextString(bDefault);
1767
1768 const double dfRotation = poLabel->Angle(bDefault);
1769 text->setRotation(dfRotation * DEG_TO_RAD);
1770
1771 poLabel->SetUnit(OGRSTUMM);
1772 double dfVal = poLabel->Size(bDefault);
1773 if (!bDefault)
1774 dfHeightMultiplier = dfVal / 1000.0;
1775
1776 /* get font id */
1777 const char *pszFontName = poLabel->FontName(bDefault);
1778 if (!bDefault && pszFontName != nullptr)
1779 {
1780 OdDgFontTablePtr pFontTable =
1781 m_pModel->database()->getFontTable(OdDg::kForRead);
1782 OdDgElementId idFont =
1783 pFontTable->getAt(OGRDGNV8DataSource::FromUTF8(pszFontName));
1784 if (!idFont.isNull())
1785 {
1786 OdDgFontTableRecordPtr pFont =
1787 idFont.openObject(OdDg::kForRead);
1788 OdUInt32 uFontEntryId = pFont->getNumber();
1789 text->setFontEntryId(uFontEntryId);
1790 }
1791 }
1792
1793 int nAnchor = poLabel->Anchor(bDefault);
1794 if (!bDefault)
1795 text->setJustification(GetAnchorPositionFromOGR(nAnchor));
1796 }
1797
1798 text->setHeightMultiplier(dfHeightMultiplier);
1799 text->setLengthMultiplier(text->getHeightMultiplier()); // FIXME ??
1800 text->setText(OGRDGNV8DataSource::FromUTF8(pszText));
1801
1802 if (poLabel)
1803 delete poLabel;
1804 return text;
1805 }
1806
1807 /************************************************************************/
1808 /* GetColorFromString() */
1809 /************************************************************************/
1810
1811 int OGRDGNV8Layer::GetColorFromString(const char *pszColor)
1812 {
1813 unsigned int nRed = 0;
1814 unsigned int nGreen = 0;
1815 unsigned int nBlue = 0;
1816 const int nCount = sscanf(pszColor, "#%2x%2x%2x", &nRed, &nGreen, &nBlue);
1817 if (nCount == 3)
1818 {
1819 OdUInt32 nIdx = OdDgColorTable::getColorIndexByRGB(
1820 m_poDS->GetDb(), ODRGB(nRed, nGreen, nBlue));
1821 return static_cast<int>(nIdx);
1822 }
1823 else
1824 {
1825 return -1;
1826 }
1827 }
1828
1829 /************************************************************************/
1830 /* AttachFillLinkage() */
1831 /************************************************************************/
1832
1833 void OGRDGNV8Layer::AttachFillLinkage(OGRFeature *poFeature,
1834 OdDgGraphicsElementPtr element)
1835 {
1836 const char *pszStyle = poFeature->GetStyleString();
1837 if (pszStyle != nullptr && strstr(pszStyle, "BRUSH") != nullptr)
1838 {
1839 OGRStyleBrush *poBrush =
1840 static_cast<OGRStyleBrush *>(GetTool(poFeature, OGRSTCBrush));
1841 if (poBrush != nullptr)
1842 {
1843 GBool bDefault;
1844 const char *pszColor = poBrush->ForeColor(bDefault);
1845 if (pszColor && !bDefault)
1846 {
1847 const int nIdx = GetColorFromString(pszColor);
1848 if (nIdx >= 0)
1849 {
1850 OdDgFillColorLinkagePtr fillColor =
1851 OdDgFillColorLinkage::createObject();
1852 fillColor->setColorIndex(nIdx);
1853 element->addLinkage(fillColor->getPrimaryId(),
1854 fillColor.get());
1855 }
1856 }
1857
1858 delete poBrush;
1859 }
1860 }
1861 }
1862
1863 /************************************************************************/
1864 /* AttachCommonAttributes() */
1865 /************************************************************************/
1866
1867 void OGRDGNV8Layer::AttachCommonAttributes(OGRFeature *poFeature,
1868 OdDgGraphicsElementPtr element)
1869 {
1870 const int nLevel = poFeature->GetFieldAsInteger("Level");
1871 const int nGraphicGroup = poFeature->GetFieldAsInteger("GraphicGroup");
1872 const int nWeight = poFeature->GetFieldAsInteger("Weight");
1873 const int nStyle = poFeature->GetFieldAsInteger("Style");
1874
1875 element->setLevelEntryId(nLevel);
1876 element->setGraphicsGroupEntryId(nGraphicGroup);
1877
1878 const int nColorIndexField = poFeature->GetFieldIndex("ColorIndex");
1879 if (poFeature->IsFieldSetAndNotNull(nColorIndexField))
1880 {
1881 const int nColor = poFeature->GetFieldAsInteger(nColorIndexField);
1882 element->setColorIndex(nColor);
1883 }
1884 else
1885 {
1886 const char *pszStyle = poFeature->GetStyleString();
1887 if (pszStyle != nullptr && strstr(pszStyle, "PEN") != nullptr)
1888 {
1889 OGRStylePen *poPen =
1890 static_cast<OGRStylePen *>(GetTool(poFeature, OGRSTCPen));
1891 if (poPen != nullptr)
1892 {
1893 GBool bDefault;
1894 const char *pszColor = poPen->Color(bDefault);
1895 if (pszColor && !bDefault)
1896 {
1897 const int nIdx = GetColorFromString(pszColor);
1898 if (nIdx >= 0)
1899 {
1900 element->setColorIndex(nIdx);
1901 }
1902 }
1903 delete poPen;
1904 }
1905 }
1906 else if (pszStyle != nullptr && strstr(pszStyle, "LABEL") != nullptr)
1907 {
1908 OGRStyleLabel *poLabel =
1909 static_cast<OGRStyleLabel *>(GetTool(poFeature, OGRSTCLabel));
1910 if (poLabel != nullptr)
1911 {
1912 GBool bDefault;
1913 const char *pszColor = poLabel->ForeColor(bDefault);
1914 if (pszColor && !bDefault)
1915 {
1916 const int nIdx = GetColorFromString(pszColor);
1917 if (nIdx >= 0)
1918 {
1919 element->setColorIndex(nIdx);
1920 }
1921 }
1922
1923 delete poLabel;
1924 }
1925 }
1926 }
1927
1928 element->setLineStyleEntryId(nStyle);
1929 element->setLineWeight(nWeight);
1930 }
1931
1932 /************************************************************************/
1933 /* AddToComplexCurve() */
1934 /************************************************************************/
1935
1936 void OGRDGNV8Layer::AddToComplexCurve(OGRFeature *poFeature,
1937 OGRCircularString *poCS,
1938 OdDgComplexCurvePtr complexCurve)
1939 {
1940 for (int i = 0; i + 2 < poCS->getNumPoints(); i += 2)
1941 {
1942 double R, cx, cy;
1943 double alpha0, alpha1, alpha2;
1944 if (OGRGeometryFactory::GetCurveParameters(
1945 poCS->getX(i), poCS->getY(i), poCS->getX(i + 1),
1946 poCS->getY(i + 1), poCS->getX(i + 2), poCS->getY(i + 2), R, cx,
1947 cy, alpha0, alpha1, alpha2))
1948 {
1949 OdDgArc2dPtr arc = OdDgArc2d::createObject();
1950 arc->setPrimaryAxis(R);
1951 arc->setSecondaryAxis(R);
1952 OdGePoint2d point;
1953 point.x = cx;
1954 point.y = cy;
1955 arc->setOrigin(point);
1956 arc->setStartAngle(alpha0); // already in radians
1957 arc->setSweepAngle(alpha2 - alpha0);
1958 AttachCommonAttributes(poFeature, arc);
1959 complexCurve->add(arc);
1960 }
1961 }
1962 }
1963
1964 /************************************************************************/
1965 /* AddToComplexCurve() */
1966 /************************************************************************/
1967
1968 void OGRDGNV8Layer::AddToComplexCurve(OGRFeature *poFeature,
1969 OGRCompoundCurve *poCC,
1970 OdDgComplexCurvePtr complexCurve)
1971 {
1972 for (int iCurve = 0; iCurve < poCC->getNumCurves(); ++iCurve)
1973 {
1974 OGRCurve *poCurve = poCC->getCurve(iCurve);
1975 OGRwkbGeometryType eType = wkbFlatten(poCurve->getGeometryType());
1976 if (eType == wkbLineString || OGR_GT_HasZ(eType))
1977 {
1978 complexCurve->add(CreateGraphicsElement(poFeature, poCurve));
1979 }
1980 else if (eType == wkbCircularString)
1981 {
1982 OGRCircularString *poCS = poCurve->toCircularString();
1983 AddToComplexCurve(poFeature, poCS, complexCurve);
1984 }
1985 else
1986 {
1987 CPLAssert(false);
1988 }
1989 }
1990 }
1991
1992 /************************************************************************/
1993 /* CreateShapeFromLS() */
1994 /************************************************************************/
1995
1996 static OdDgGraphicsElementPtr CreateShapeFromLS(OGRLineString *poLS,
1997 bool bHbit = false)
1998 {
1999 if (OGR_GT_HasZ(poLS->getGeometryType()))
2000 {
2001 OdDgShape3dPtr shape = OdDgShape3d::createObject();
2002 for (int i = 0; i < poLS->getNumPoints(); i++)
2003 {
2004 OGRPoint ogrPoint;
2005 OdGePoint3d point;
2006 poLS->getPoint(i, &ogrPoint);
2007 point.x = ogrPoint.getX();
2008 point.y = ogrPoint.getY();
2009 point.z = ogrPoint.getZ();
2010 shape->addVertex(point);
2011 }
2012 shape->setHbitFlag(bHbit);
2013 return shape;
2014 }
2015 else
2016 {
2017 OdDgShape2dPtr shape = OdDgShape2d::createObject();
2018 for (int i = 0; i < poLS->getNumPoints(); i++)
2019 {
2020 OGRPoint ogrPoint;
2021 OdGePoint2d point;
2022 poLS->getPoint(i, &ogrPoint);
2023 point.x = ogrPoint.getX();
2024 point.y = ogrPoint.getY();
2025 shape->addVertex(point);
2026 }
2027 shape->setHbitFlag(bHbit);
2028 return shape;
2029 }
2030 }
2031
2032 /************************************************************************/
2033 /* CreateShape() */
2034 /************************************************************************/
2035
2036 OdDgGraphicsElementPtr OGRDGNV8Layer::CreateShape(OGRFeature *poFeature,
2037 OGRCurve *poCurve,
2038 bool bIsHole)
2039 {
2040 OdDgGraphicsElementPtr element;
2041 OGRwkbGeometryType eType = wkbFlatten(poCurve->getGeometryType());
2042 if (eType == wkbLineString)
2043 {
2044 OGRLineString *poLS = poCurve->toLineString();
2045 element = CreateShapeFromLS(poLS, bIsHole);
2046 }
2047 else if (eType == wkbCircularString)
2048 {
2049 OdDgComplexShapePtr complexShape = OdDgComplexShape::createObject();
2050 complexShape->setHbitFlag(bIsHole);
2051 OGRCircularString *poCS = poCurve->toCircularString();
2052 AddToComplexCurve(poFeature, poCS, complexShape);
2053 element = complexShape;
2054 }
2055 else if (eType == wkbCompoundCurve)
2056 {
2057 OdDgComplexShapePtr complexShape = OdDgComplexShape::createObject();
2058 complexShape->setHbitFlag(bIsHole);
2059 OGRCompoundCurve *poCC = poCurve->toCompoundCurve();
2060 AddToComplexCurve(poFeature, poCC, complexShape);
2061 element = complexShape;
2062 }
2063
2064 if (!bIsHole)
2065 AttachFillLinkage(poFeature, element);
2066
2067 return element;
2068 }
2069
2070 /************************************************************************/
2071 /* IsFullCircle() */
2072 /************************************************************************/
2073
2074 static bool IsFullCircle(OGRCircularString *poCS, double &cx, double &cy,
2075 double &R)
2076 {
2077 if (poCS->getNumPoints() == 3 && poCS->get_IsClosed())
2078 {
2079 const double x0 = poCS->getX(0);
2080 const double y0 = poCS->getY(0);
2081 const double x1 = poCS->getX(1);
2082 const double y1 = poCS->getY(1);
2083 cx = (x0 + x1) / 2;
2084 cy = (y0 + y1) / 2;
2085 R = sqrt((x1 - cx) * (x1 - cx) + (y1 - cy) * (y1 - cy));
2086 return true;
2087 }
2088 // Full circle defined by 2 arcs?
2089 else if (poCS->getNumPoints() == 5 && poCS->get_IsClosed())
2090 {
2091 double R_1 = 0.0;
2092 double cx_1 = 0.0;
2093 double cy_1 = 0.0;
2094 double alpha0_1 = 0.0;
2095 double alpha1_1 = 0.0;
2096 double alpha2_1 = 0.0;
2097 double R_2 = 0.0;
2098 double cx_2 = 0.0;
2099 double cy_2 = 0.0;
2100 double alpha0_2 = 0.0;
2101 double alpha1_2 = 0.0;
2102 double alpha2_2 = 0.0;
2103 if (OGRGeometryFactory::GetCurveParameters(
2104 poCS->getX(0), poCS->getY(0), poCS->getX(1), poCS->getY(1),
2105 poCS->getX(2), poCS->getY(2), R_1, cx_1, cy_1, alpha0_1,
2106 alpha1_1, alpha2_1) &&
2107 OGRGeometryFactory::GetCurveParameters(
2108 poCS->getX(2), poCS->getY(2), poCS->getX(3), poCS->getY(3),
2109 poCS->getX(4), poCS->getY(4), R_2, cx_2, cy_2, alpha0_2,
2110 alpha1_2, alpha2_2) &&
2111 AlmostEqual(R_1, R_2) && AlmostEqual(cx_1, cx_2) &&
2112 AlmostEqual(cy_1, cy_2) &&
2113 (alpha2_1 - alpha0_1) * (alpha2_2 - alpha0_2) > 0)
2114 {
2115 cx = cx_1;
2116 cy = cy_1;
2117 R = R_1;
2118 return true;
2119 }
2120 }
2121 return false;
2122 }
2123
2124 /************************************************************************/
2125 /* CreateGraphicsElement() */
2126 /* */
2127 /* Create an element or element group from a given geometry and */
2128 /* the given feature. This method recurses to handle */
2129 /* collections as essentially independent features. */
2130 /************************************************************************/
2131
2132 OdDgGraphicsElementPtr
2133 OGRDGNV8Layer::CreateGraphicsElement(OGRFeature *poFeature, OGRGeometry *poGeom)
2134
2135 {
2136 const OGRwkbGeometryType eType = poGeom->getGeometryType();
2137 const OGRwkbGeometryType eFType = wkbFlatten(eType);
2138
2139 const int nType = poFeature->GetFieldAsInteger("Type");
2140
2141 OdDgGraphicsElementPtr element;
2142
2143 if (eFType == wkbPoint)
2144 {
2145 OGRPoint *poPoint = poGeom->toPoint();
2146 const char *pszText = poFeature->GetFieldAsString("Text");
2147 const char *pszStyle = poFeature->GetStyleString();
2148
2149 if ((pszText == nullptr || pszText[0] == 0) &&
2150 (pszStyle == nullptr || strstr(pszStyle, "LABEL") == nullptr))
2151 {
2152 if (OGR_GT_HasZ(eType))
2153 {
2154 OdDgLine3dPtr line = OdDgLine3d::createObject();
2155 element = line;
2156 OdGePoint3d point;
2157 point.x = poPoint->getX();
2158 point.y = poPoint->getY();
2159 point.z = poPoint->getZ();
2160 line->setStartPoint(point);
2161 line->setEndPoint(point);
2162 }
2163 else
2164 {
2165 OdDgLine2dPtr line = OdDgLine2d::createObject();
2166 element = line;
2167 OdGePoint2d point;
2168 point.x = poPoint->getX();
2169 point.y = poPoint->getY();
2170 line->setStartPoint(point);
2171 line->setEndPoint(point);
2172 }
2173 }
2174 else
2175 {
2176 element = TranslateLabel(poFeature, poPoint);
2177 }
2178 }
2179 else if (eFType == wkbLineString)
2180 {
2181 OGRLineString *poLS = poGeom->toLineString();
2182 if (poLS->getNumPoints() == 2 &&
2183 (nType == 0 || nType == OdDgElement::kTypeLine))
2184 {
2185 if (OGR_GT_HasZ(eType))
2186 {
2187 OdDgLine3dPtr line = OdDgLine3d::createObject();
2188 element = line;
2189 OGRPoint ogrPoint;
2190 OdGePoint3d point;
2191 poLS->getPoint(0, &ogrPoint);
2192 point.x = ogrPoint.getX();
2193 point.y = ogrPoint.getY();
2194 point.z = ogrPoint.getZ();
2195 line->setStartPoint(point);
2196 poLS->getPoint(1, &ogrPoint);
2197 point.x = ogrPoint.getX();
2198 point.y = ogrPoint.getY();
2199 point.z = ogrPoint.getZ();
2200 line->setEndPoint(point);
2201 }
2202 else
2203 {
2204 OdDgLine2dPtr line = OdDgLine2d::createObject();
2205 element = line;
2206 OGRPoint ogrPoint;
2207 OdGePoint2d point;
2208 poLS->getPoint(0, &ogrPoint);
2209 point.x = ogrPoint.getX();
2210 point.y = ogrPoint.getY();
2211 line->setStartPoint(point);
2212 poLS->getPoint(1, &ogrPoint);
2213 point.x = ogrPoint.getX();
2214 point.y = ogrPoint.getY();
2215 line->setEndPoint(point);
2216 }
2217 }
2218 else
2219 {
2220 if (OGR_GT_HasZ(eType))
2221 {
2222 OdDgLineString3dPtr line = OdDgLineString3d::createObject();
2223 element = line;
2224 for (int i = 0; i < poLS->getNumPoints(); i++)
2225 {
2226 OGRPoint ogrPoint;
2227 OdGePoint3d point;
2228 poLS->getPoint(i, &ogrPoint);
2229 point.x = ogrPoint.getX();
2230 point.y = ogrPoint.getY();
2231 point.z = ogrPoint.getZ();
2232 line->addVertex(point);
2233 }
2234 }
2235 else
2236 {
2237 OdDgLineString2dPtr line = OdDgLineString2d::createObject();
2238 element = line;
2239 for (int i = 0; i < poLS->getNumPoints(); i++)
2240 {
2241 OGRPoint ogrPoint;
2242 OdGePoint2d point;
2243 poLS->getPoint(i, &ogrPoint);
2244 point.x = ogrPoint.getX();
2245 point.y = ogrPoint.getY();
2246 line->addVertex(point);
2247 }
2248 }
2249 }
2250 }
2251 else if (eFType == wkbCircularString)
2252 {
2253 OGRCircularString *poCS = poGeom->toCircularString();
2254 double R, cx, cy;
2255 if (IsFullCircle(poCS, cx, cy, R) && !OGR_GT_HasZ(eType))
2256 {
2257 OdDgEllipse2dPtr ellipse = OdDgEllipse2d::createObject();
2258 element = ellipse;
2259 ellipse->setPrimaryAxis(R);
2260 ellipse->setSecondaryAxis(R);
2261 OdGePoint2d point;
2262 point.x = cx;
2263 point.y = cy;
2264 ellipse->setOrigin(point);
2265 }
2266 else if (poCS->getNumPoints() == 3 && !OGR_GT_HasZ(eType))
2267 {
2268 double alpha0, alpha1, alpha2;
2269 if (OGRGeometryFactory::GetCurveParameters(
2270 poCS->getX(0), poCS->getY(0), poCS->getX(1), poCS->getY(1),
2271 poCS->getX(2), poCS->getY(2), R, cx, cy, alpha0, alpha1,
2272 alpha2))
2273 {
2274 OdDgArc2dPtr arc = OdDgArc2d::createObject();
2275 element = arc;
2276 arc->setPrimaryAxis(R);
2277 arc->setSecondaryAxis(R);
2278 OdGePoint2d point;
2279 point.x = cx;
2280 point.y = cy;
2281 arc->setOrigin(point);
2282 arc->setStartAngle(alpha0); // already in radians
2283 arc->setSweepAngle(alpha2 - alpha0);
2284 }
2285 }
2286 else if (!OGR_GT_HasZ(eType))
2287 {
2288 OdDgComplexCurvePtr complexCurve =
2289 OdDgComplexString::createObject();
2290 element = complexCurve;
2291 AddToComplexCurve(poFeature, poCS, complexCurve);
2292 }
2293
2294 if (element.isNull())
2295 {
2296 OGRGeometry *poLS =
2297 OGRGeometryFactory::forceToLineString(poGeom->clone());
2298 element = CreateGraphicsElement(poFeature, poLS);
2299 delete poLS;
2300 return element;
2301 }
2302 }
2303 else if (eFType == wkbCompoundCurve)
2304 {
2305 OGRCompoundCurve *poCC = poGeom->toCompoundCurve();
2306 OdDgComplexCurvePtr complexCurve = OdDgComplexString::createObject();
2307 element = complexCurve;
2308 AddToComplexCurve(poFeature, poCC, complexCurve);
2309 }
2310 else if (eFType == wkbCurvePolygon || eFType == wkbPolygon)
2311 {
2312 OGRCurvePolygon *poPoly = poGeom->toCurvePolygon();
2313 if (poPoly->getNumInteriorRings() == 0 &&
2314 poPoly->getExteriorRingCurve() == nullptr)
2315 {
2316 if (OGR_GT_HasZ(eType))
2317 {
2318 element = OdDgShape3d::createObject();
2319 }
2320 else
2321 {
2322 element = OdDgShape2d::createObject();
2323 }
2324 }
2325 else if (poPoly->getNumInteriorRings() == 0)
2326 {
2327 element = CreateShape(poFeature, poPoly->getExteriorRingCurve());
2328 }
2329 else
2330 {
2331 if (OGR_GT_HasZ(eType))
2332 {
2333 OdDgCellHeader3dPtr pCell = OdDgCellHeader3d::createObject();
2334 element = pCell;
2335 for (int iRing = -1; iRing < poPoly->getNumInteriorRings();
2336 iRing++)
2337 {
2338 OGRCurve *poCurve =
2339 (iRing < 0) ? poPoly->getExteriorRingCurve()
2340 : poPoly->getInteriorRingCurve(iRing);
2341
2342 OdDgGraphicsElementPtr shape =
2343 CreateShape(poFeature, poCurve, iRing >= 0);
2344 AttachCommonAttributes(poFeature, shape);
2345 pCell->add(shape);
2346 }
2347 }
2348 else
2349 {
2350 OdDgCellHeader2dPtr pCell = OdDgCellHeader2d::createObject();
2351 element = pCell;
2352 for (int iRing = -1; iRing < poPoly->getNumInteriorRings();
2353 iRing++)
2354 {
2355 OGRCurve *poCurve =
2356 (iRing < 0) ? poPoly->getExteriorRingCurve()
2357 : poPoly->getInteriorRingCurve(iRing);
2358
2359 OdDgGraphicsElementPtr shape =
2360 CreateShape(poFeature, poCurve, iRing >= 0);
2361 AttachCommonAttributes(poFeature, shape);
2362 pCell->add(shape);
2363 }
2364 }
2365 AttachFillLinkage(poFeature, element);
2366 }
2367 }
2368 else if (OGR_GT_IsSubClassOf(eFType, wkbGeometryCollection))
2369 {
2370 OGRGeometryCollection *poGC = poGeom->toGeometryCollection();
2371 OdDgCellHeader2dPtr pCell = OdDgCellHeader2d::createObject();
2372 element = pCell;
2373 if (!pCell.isNull())
2374 {
2375 for (auto &&poMember : poGC)
2376 {
2377 pCell->add(CreateGraphicsElement(poFeature, poMember));
2378 }
2379 }
2380 }
2381 else
2382 {
2383 CPLError(CE_Failure, CPLE_AppDefined,
2384 "Unsupported geometry type (%s) for DGN.",
2385 OGRGeometryTypeToName(eType));
2386 }
2387
2388 if (!element.isNull())
2389 AttachCommonAttributes(poFeature, element);
2390
2391 return element;
2392 }
2393
2394 /************************************************************************/
2395 /* GetDataset() */
2396 /************************************************************************/
2397
2398 GDALDataset *OGRDGNV8Layer::GetDataset()
2399 {
2400 return m_poDS;
2401 }