| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Project: DWG Translator |
| 4 |
* Purpose: Implements translation support for HATCH elements as part |
| 5 |
* of the OGRDWGLayer class. |
| 6 |
* Author: Frank Warmerdam, [email protected] |
| 7 |
* |
| 8 |
****************************************************************************** |
| 9 |
* Copyright (c) 2011, Frank Warmerdam <[email protected]> |
| 10 |
* |
| 11 |
* SPDX-License-Identifier: MIT |
| 12 |
****************************************************************************/ |
| 13 |
|
| 14 |
#include "ogr_dwg.h" |
| 15 |
#include "cpl_conv.h" |
| 16 |
#include "ogr_api.h" |
| 17 |
|
| 18 |
#include "ogrdxf_polyline_smooth.h" |
| 19 |
|
| 20 |
static OGRErr DWGCollectBoundaryLoop(OdDbHatchPtr poHatch, int iLoop, |
| 21 |
OGRGeometryCollection *poGC); |
| 22 |
|
| 23 |
/************************************************************************/ |
| 24 |
/* TranslateHATCH() */ |
| 25 |
/* */ |
| 26 |
/* We mostly just try to convert hatch objects as polygons or */ |
| 27 |
/* multipolygons representing the hatched area. It is hard to */ |
| 28 |
/* preserve the actual details of the hatching. */ |
| 29 |
/************************************************************************/ |
| 30 |
|
| 31 |
OGRFeature *OGRDWGLayer::TranslateHATCH(OdDbEntityPtr poEntity) |
| 32 |
|
| 33 |
{ |
| 34 |
OGRFeature *poFeature = new OGRFeature(poFeatureDefn); |
| 35 |
OdDbHatchPtr poHatch = OdDbHatch::cast(poEntity); |
| 36 |
OGRGeometryCollection oGC; |
| 37 |
|
| 38 |
TranslateGenericProperties(poFeature, poEntity); |
| 39 |
|
| 40 |
poFeature->SetField("Text", (const char *)poHatch->patternName()); |
| 41 |
|
| 42 |
/* -------------------------------------------------------------------- */ |
| 43 |
/* Collect the loops. */ |
| 44 |
/* -------------------------------------------------------------------- */ |
| 45 |
for (int i = 0; i < poHatch->numLoops(); i++) |
| 46 |
{ |
| 47 |
DWGCollectBoundaryLoop(poHatch, i, &oGC); |
| 48 |
} |
| 49 |
|
| 50 |
/* -------------------------------------------------------------------- */ |
| 51 |
/* Try to turn the set of lines into something useful. */ |
| 52 |
/* -------------------------------------------------------------------- */ |
| 53 |
OGRErr eErr; |
| 54 |
|
| 55 |
OGRGeometryH hFinalGeom = OGRBuildPolygonFromEdges((OGRGeometryH)&oGC, TRUE, |
| 56 |
TRUE, 0.0000001, &eErr); |
| 57 |
|
| 58 |
poFeature->SetGeometryDirectly((OGRGeometry *)hFinalGeom); |
| 59 |
|
| 60 |
/* -------------------------------------------------------------------- */ |
| 61 |
/* Work out the color for this feature. For now we just assume */ |
| 62 |
/* solid fill. We cannot trivially translate the various sorts */ |
| 63 |
/* of hatching. */ |
| 64 |
/* -------------------------------------------------------------------- */ |
| 65 |
CPLString osLayer = poFeature->GetFieldAsString("Layer"); |
| 66 |
|
| 67 |
int nColor = 256; |
| 68 |
|
| 69 |
if (oStyleProperties.count("Color") > 0) |
| 70 |
nColor = atoi(oStyleProperties["Color"]); |
| 71 |
|
| 72 |
// Use layer color? |
| 73 |
if (nColor < 1 || nColor > 255) |
| 74 |
{ |
| 75 |
const char *pszValue = poDS->LookupLayerProperty(osLayer, "Color"); |
| 76 |
if (pszValue != nullptr) |
| 77 |
nColor = atoi(pszValue); |
| 78 |
} |
| 79 |
|
| 80 |
/* -------------------------------------------------------------------- */ |
| 81 |
/* Setup the style string. */ |
| 82 |
/* -------------------------------------------------------------------- */ |
| 83 |
if (nColor >= 1 && nColor <= 255) |
| 84 |
{ |
| 85 |
CPLString osStyle; |
| 86 |
const unsigned char *pabyDWGColors = ACGetColorTable(); |
| 87 |
|
| 88 |
osStyle.Printf("BRUSH(fc:#%02x%02x%02x)", pabyDWGColors[nColor * 3 + 0], |
| 89 |
pabyDWGColors[nColor * 3 + 1], |
| 90 |
pabyDWGColors[nColor * 3 + 2]); |
| 91 |
|
| 92 |
poFeature->SetStyleString(osStyle); |
| 93 |
} |
| 94 |
|
| 95 |
return poFeature; |
| 96 |
} |
| 97 |
|
| 98 |
/************************************************************************/ |
| 99 |
/* CollectBoundaryLoop() */ |
| 100 |
/************************************************************************/ |
| 101 |
|
| 102 |
static OGRErr DWGCollectBoundaryLoop(OdDbHatchPtr poHatch, int iLoop, |
| 103 |
OGRGeometryCollection *poGC) |
| 104 |
|
| 105 |
{ |
| 106 |
int i; |
| 107 |
|
| 108 |
/* -------------------------------------------------------------------- */ |
| 109 |
/* Handle simple polyline loops. */ |
| 110 |
/* -------------------------------------------------------------------- */ |
| 111 |
if (poHatch->loopTypeAt(iLoop) & OdDbHatch::kPolyline) |
| 112 |
{ |
| 113 |
DXFSmoothPolyline oSmoothPolyline; |
| 114 |
OdGePoint2dArray vertices; |
| 115 |
OdGeDoubleArray bulges; |
| 116 |
|
| 117 |
poHatch->getLoopAt(iLoop, vertices, bulges); |
| 118 |
|
| 119 |
for (i = 0; i < (int)vertices.size(); i++) |
| 120 |
{ |
| 121 |
if (i >= (int)bulges.size()) |
| 122 |
oSmoothPolyline.AddPoint(vertices[i].x, vertices[i].y, 0.0, |
| 123 |
0.0); |
| 124 |
else |
| 125 |
oSmoothPolyline.AddPoint(vertices[i].x, vertices[i].y, 0.0, |
| 126 |
bulges[i]); |
| 127 |
} |
| 128 |
|
| 129 |
oSmoothPolyline.Close(); |
| 130 |
|
| 131 |
OGRLineString *poLS = oSmoothPolyline.Tessellate(false)->toLineString(); |
| 132 |
poGC->addGeometryDirectly(poLS); |
| 133 |
|
| 134 |
return OGRERR_NONE; |
| 135 |
} |
| 136 |
|
| 137 |
/* -------------------------------------------------------------------- */ |
| 138 |
/* Handle an edges array. */ |
| 139 |
/* -------------------------------------------------------------------- */ |
| 140 |
EdgeArray oEdges; |
| 141 |
poHatch->getLoopAt(iLoop, oEdges); |
| 142 |
|
| 143 |
for (OdGeCurve2d *poEdge : oEdges) |
| 144 |
{ |
| 145 |
if (poEdge->type() == OdGe::kLineSeg2d) |
| 146 |
{ |
| 147 |
OGRLineString *poLS = new OGRLineString(); |
| 148 |
OdGePoint2d oStart = poEdge->evalPoint(0.0); |
| 149 |
OdGePoint2d oEnd = poEdge->evalPoint(1.0); |
| 150 |
|
| 151 |
poLS->addPoint(oStart.x, oStart.y); |
| 152 |
poLS->addPoint(oEnd.x, oEnd.y); |
| 153 |
poGC->addGeometryDirectly(poLS); |
| 154 |
} |
| 155 |
else if (poEdge->type() == OdGe::kCircArc2d) |
| 156 |
{ |
| 157 |
OdGeCircArc2d *poCircArc = static_cast<OdGeCircArc2d *>(poEdge); |
| 158 |
OdGePoint2d oCenter = poCircArc->center(); |
| 159 |
double dfStartAngle = poCircArc->startAng() * 180 / M_PI; |
| 160 |
double dfEndAngle = poCircArc->endAng() * 180 / M_PI; |
| 161 |
|
| 162 |
if (!poCircArc->isClockWise()) |
| 163 |
{ |
| 164 |
dfStartAngle *= -1; |
| 165 |
dfEndAngle *= -1; |
| 166 |
} |
| 167 |
else if (dfStartAngle > dfEndAngle) |
| 168 |
{ |
| 169 |
dfEndAngle += 360.0; |
| 170 |
} |
| 171 |
|
| 172 |
OGRLineString *poLS = |
| 173 |
OGRGeometryFactory::approximateArcAngles( |
| 174 |
oCenter.x, oCenter.y, 0.0, poCircArc->radius(), |
| 175 |
poCircArc->radius(), 0.0, dfStartAngle, dfEndAngle, 0.0) |
| 176 |
->toLineString(); |
| 177 |
|
| 178 |
poGC->addGeometryDirectly(poLS); |
| 179 |
} |
| 180 |
else if (poEdge->type() == OdGe::kEllipArc2d) |
| 181 |
{ |
| 182 |
OdGeEllipArc2d *poArc = static_cast<OdGeEllipArc2d *>(poEdge); |
| 183 |
OdGePoint2d oCenter = poArc->center(); |
| 184 |
double dfRatio = poArc->minorRadius() / poArc->majorRadius(); |
| 185 |
OdGeVector2d oMajorAxis = poArc->majorAxis(); |
| 186 |
double dfRotation; |
| 187 |
double dfStartAng, dfEndAng; |
| 188 |
|
| 189 |
dfRotation = -1 * atan2(oMajorAxis.y, oMajorAxis.x) * 180 / M_PI; |
| 190 |
|
| 191 |
dfStartAng = poArc->startAng() * 180 / M_PI; |
| 192 |
dfEndAng = poArc->endAng() * 180 / M_PI; |
| 193 |
|
| 194 |
if (!poArc->isClockWise()) |
| 195 |
{ |
| 196 |
dfStartAng *= -1; |
| 197 |
dfEndAng *= -1; |
| 198 |
} |
| 199 |
else if (dfStartAng > dfEndAng) |
| 200 |
{ |
| 201 |
dfEndAng += 360.0; |
| 202 |
} |
| 203 |
|
| 204 |
OGRLineString *poLS = |
| 205 |
OGRGeometryFactory::approximateArcAngles( |
| 206 |
oCenter.x, oCenter.y, 0.0, poArc->majorRadius(), |
| 207 |
poArc->minorRadius(), dfRotation, |
| 208 |
OGRDWGLayer::AngleCorrect(dfStartAng, dfRatio), |
| 209 |
OGRDWGLayer::AngleCorrect(dfEndAng, dfRatio), 0.0) |
| 210 |
->toLineString(); |
| 211 |
poGC->addGeometryDirectly(poLS); |
| 212 |
} |
| 213 |
else |
| 214 |
CPLDebug("DWG", "Unsupported edge type (%d) in hatch loop.", |
| 215 |
(int)poEdge->type()); |
| 216 |
|
| 217 |
// case OdGe::kNurbCurve2d : dumpNurbCurveEdge(indent + 1, pEdge); |
| 218 |
} |
| 219 |
|
| 220 |
return OGRERR_NONE; |
| 221 |
} |