Debian Codegraph @

ogr/ogrsf_frmts/dwg/ogrdwg_dimension.cpp

raw
1 /******************************************************************************
2 *
3 * Project: DWG Translator
4 * Purpose: Implements translation support for DIMENSION elements as a 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
17 /************************************************************************/
18 /* TranslateDIMENSION() */
19 /************************************************************************/
20
21 OGRFeature *OGRDWGLayer::TranslateDIMENSION(OdDbEntityPtr poEntity)
22
23 {
24 OdDbDimensionPtr poDim = OdDbDimension::cast(poEntity);
25 OGRFeature *poFeature = new OGRFeature(poFeatureDefn);
26
27 double dfHeight = CPLAtof(poDS->GetVariable("$DIMTXT", "2.5"));
28 OdGePoint3d oTextPos, oTarget1, oTarget2, oArrow1;
29 CPLString osText;
30
31 TranslateGenericProperties(poFeature, poEntity);
32
33 /* -------------------------------------------------------------------- */
34 /* Generic Dimension stuff. */
35 /* -------------------------------------------------------------------- */
36 osText = (const char *)poDim->dimensionText();
37
38 oTextPos = poDim->textPosition();
39
40 /* -------------------------------------------------------------------- */
41 /* Specific based on the subtype. */
42 /* -------------------------------------------------------------------- */
43 OdRxClass *poClass = poEntity->isA();
44 const OdString osName = poClass->name();
45 const char *pszEntityClassName = (const char *)osName;
46
47 if (EQUAL(pszEntityClassName, "AcDbRotatedDimension"))
48 {
49 OdDbRotatedDimensionPtr poRDim = OdDbDimension::cast(poEntity);
50
51 oTarget2 = poRDim->xLine1Point();
52 oTarget1 = poRDim->xLine2Point();
53 oArrow1 = poRDim->dimLinePoint();
54 }
55
56 else if (EQUAL(pszEntityClassName, "AcDbAlignedDimension"))
57 {
58 OdDbAlignedDimensionPtr poADim = OdDbDimension::cast(poEntity);
59
60 oTarget2 = poADim->xLine1Point();
61 oTarget1 = poADim->xLine2Point();
62 oArrow1 = poADim->dimLinePoint();
63 }
64
65 /*************************************************************************
66
67 DIMENSION geometry layout
68
69 (11,21)(text center point)
70 | DimText |
71 (10,20) X<--------------------------------->X (Arrow2 - computed)
72 (Arrow1)| |
73 | |
74 | X (13,23) (Target2)
75 |
76 X (14,24) (Target1)
77
78 Given:
79 Locations Arrow1, Target1, and Target2 we need to compute Arrow2.
80
81 Steps:
82 1) Compute direction vector from Target1 to Arrow1 (Vec1).
83 2) Compute direction vector for arrow as perpendicular to Vec1 (call Vec2).
84 3) Compute Arrow2 location as intersection between line defined by
85 Vec2 and Arrow1 and line defined by Target2 and direction Vec1 (call
86 Arrow2)
87
88 Then we can draw lines for the various components.
89
90 Note that Vec1 and Vec2 may be horizontal, vertical or on an angle but
91 the approach is as above in all these cases.
92
93 *************************************************************************/
94
95 /* -------------------------------------------------------------------- */
96 /* Step 1, compute direction vector between Target1 and Arrow1. */
97 /* -------------------------------------------------------------------- */
98 double dfVec1X, dfVec1Y;
99
100 dfVec1X = (oArrow1.x - oTarget1.x);
101 dfVec1Y = (oArrow1.y - oTarget1.y);
102
103 /* -------------------------------------------------------------------- */
104 /* Step 2, compute the direction vector from Arrow1 to Arrow2 */
105 /* as a perpendicular to Vec1. */
106 /* -------------------------------------------------------------------- */
107 double dfVec2X, dfVec2Y;
108
109 dfVec2X = dfVec1Y;
110 dfVec2Y = -dfVec1X;
111
112 /* -------------------------------------------------------------------- */
113 /* Step 3, compute intersection of line from target2 along */
114 /* direction vector 1, with the line through Arrow1 and */
115 /* direction vector 2. */
116 /* -------------------------------------------------------------------- */
117 double dfL1M, dfL1B, dfL2M, dfL2B;
118 double dfArrowX2, dfArrowY2;
119
120 // special case if vec1 is vertical.
121 if (dfVec1X == 0.0)
122 {
123 dfArrowX2 = oTarget2.x;
124 dfArrowY2 = oArrow1.y;
125 }
126
127 // special case if vec2 is horizontal.
128 else if (dfVec1Y == 0.0)
129 {
130 dfArrowX2 = oArrow1.x;
131 dfArrowY2 = oTarget2.y;
132 }
133
134 else // General case for diagonal vectors.
135 {
136 // first convert vec1 + target2 into y = mx + b format: call this L1
137
138 dfL1M = dfVec1Y / dfVec1X;
139 dfL1B = oTarget2.y - dfL1M * oTarget2.x;
140
141 // convert vec2 + Arrow1 into y = mx + b format, call this L2
142
143 dfL2M = dfVec2Y / dfVec2X;
144 dfL2B = oArrow1.y - dfL2M * oArrow1.x;
145
146 // Compute intersection x = (b2-b1) / (m1-m2)
147
148 dfArrowX2 = (dfL2B - dfL1B) / (dfL1M - dfL2M);
149 dfArrowY2 = dfL2M * dfArrowX2 + dfL2B;
150 }
151
152 /* -------------------------------------------------------------------- */
153 /* Compute the text angle. */
154 /* -------------------------------------------------------------------- */
155 double dfAngle = atan2(dfVec2Y, dfVec2X) * 180.0 / M_PI;
156
157 /* -------------------------------------------------------------------- */
158 /* Rescale the direction vectors so we can use them in */
159 /* constructing arrowheads. We want them to be about 3% of the */
160 /* length of line on which the arrows will be drawn. */
161 /* -------------------------------------------------------------------- */
162 #define VECTOR_LEN(x, y) sqrt((x) * (x) + (y) * (y))
163 #define POINT_DIST(x1, y1, x2, y2) VECTOR_LEN((x2 - x1), (y2 - y1))
164
165 double dfBaselineLength =
166 POINT_DIST(oArrow1.x, oArrow1.y, dfArrowX2, dfArrowY2);
167 double dfTargetLength = dfBaselineLength * 0.03;
168 double dfScaleFactor;
169
170 // recompute vector 2 to ensure the direction is regular
171 dfVec2X = (dfArrowX2 - oArrow1.x);
172 dfVec2Y = (dfArrowY2 - oArrow1.y);
173
174 // vector 1
175 dfScaleFactor = dfTargetLength / VECTOR_LEN(dfVec1X, dfVec1Y);
176 dfVec1X *= dfScaleFactor;
177 dfVec1Y *= dfScaleFactor;
178
179 // vector 2
180 dfScaleFactor = dfTargetLength / VECTOR_LEN(dfVec2X, dfVec2Y);
181 dfVec2X *= dfScaleFactor;
182 dfVec2Y *= dfScaleFactor;
183
184 /* -------------------------------------------------------------------- */
185 /* Create geometries for the different components of the */
186 /* dimension object. */
187 /* -------------------------------------------------------------------- */
188 OGRMultiLineString *poMLS = new OGRMultiLineString();
189 OGRLineString oLine;
190
191 // main arrow line between Arrow1 and Arrow2
192 oLine.setPoint(0, oArrow1.x, oArrow1.y);
193 oLine.setPoint(1, dfArrowX2, dfArrowY2);
194 poMLS->addGeometry(&oLine);
195
196 // dimension line from Target1 to Arrow1 with a small extension.
197 oLine.setPoint(0, oTarget1.x, oTarget1.y);
198 oLine.setPoint(1, oArrow1.x + dfVec1X, oArrow1.y + dfVec1Y);
199 poMLS->addGeometry(&oLine);
200
201 // dimension line from Target2 to Arrow2 with a small extension.
202 oLine.setPoint(0, oTarget2.x, oTarget2.y);
203 oLine.setPoint(1, dfArrowX2 + dfVec1X, dfArrowY2 + dfVec1Y);
204 poMLS->addGeometry(&oLine);
205
206 // add arrow1 arrow head.
207
208 oLine.setPoint(0, oArrow1.x, oArrow1.y);
209 oLine.setPoint(1, oArrow1.x + dfVec2X * 3 + dfVec1X,
210 oArrow1.y + dfVec2Y * 3 + dfVec1Y);
211 poMLS->addGeometry(&oLine);
212
213 oLine.setPoint(0, oArrow1.x, oArrow1.y);
214 oLine.setPoint(1, oArrow1.x + dfVec2X * 3 - dfVec1X,
215 oArrow1.y + dfVec2Y * 3 - dfVec1Y);
216 poMLS->addGeometry(&oLine);
217
218 // add arrow2 arrow head.
219
220 oLine.setPoint(0, dfArrowX2, dfArrowY2);
221 oLine.setPoint(1, dfArrowX2 - dfVec2X * 3 + dfVec1X,
222 dfArrowY2 - dfVec2Y * 3 + dfVec1Y);
223 poMLS->addGeometry(&oLine);
224
225 oLine.setPoint(0, dfArrowX2, dfArrowY2);
226 oLine.setPoint(1, dfArrowX2 - dfVec2X * 3 - dfVec1X,
227 dfArrowY2 - dfVec2Y * 3 - dfVec1Y);
228 poMLS->addGeometry(&oLine);
229
230 poFeature->SetGeometryDirectly(poMLS);
231
232 PrepareLineStyle(poFeature);
233
234 /* -------------------------------------------------------------------- */
235 /* Is the layer disabled/hidden/frozen/off? */
236 /* -------------------------------------------------------------------- */
237 CPLString osLayer = poFeature->GetFieldAsString("Layer");
238
239 int bHidden = EQUAL(poDS->LookupLayerProperty(osLayer, "Hidden"), "1");
240
241 /* -------------------------------------------------------------------- */
242 /* Work out the color for this feature. */
243 /* -------------------------------------------------------------------- */
244 int nColor = 256;
245
246 if (oStyleProperties.count("Color") > 0)
247 nColor = atoi(oStyleProperties["Color"]);
248
249 // Use layer color?
250 if (nColor < 1 || nColor > 255)
251 {
252 const char *pszValue = poDS->LookupLayerProperty(osLayer, "Color");
253 if (pszValue != nullptr)
254 nColor = atoi(pszValue);
255 }
256
257 if (nColor < 1 || nColor > 255)
258 nColor = 8;
259
260 /* -------------------------------------------------------------------- */
261 /* Prepare a new feature to serve as the dimension text label */
262 /* feature. We will push it onto the layer as a pending */
263 /* feature for the next feature read. */
264 /* -------------------------------------------------------------------- */
265
266 // a single space suppresses labeling.
267 if (osText == " ")
268 return poFeature;
269
270 OGRFeature *poLabelFeature = poFeature->Clone();
271
272 poLabelFeature->SetGeometryDirectly(new OGRPoint(oTextPos.x, oTextPos.y));
273
274 // Do we need to compute the dimension value?
275 if (osText.empty())
276 {
277 FormatDimension(osText,
278 POINT_DIST(oArrow1.x, oArrow1.y, dfArrowX2, dfArrowY2));
279 }
280
281 CPLString osStyle;
282 char szBuffer[64];
283 char *pszComma = nullptr;
284
285 osStyle.Printf("LABEL(f:\"Arial\",t:\"%s\",p:5", osText.c_str());
286
287 if (dfAngle != 0.0)
288 {
289 CPLsnprintf(szBuffer, sizeof(szBuffer), "%.3g", dfAngle);
290 pszComma = strchr(szBuffer, ',');
291 if (pszComma)
292 *pszComma = '.';
293 osStyle += CPLString().Printf(",a:%s", szBuffer);
294 }
295
296 if (dfHeight != 0.0)
297 {
298 CPLsnprintf(szBuffer, sizeof(szBuffer), "%.3g", dfHeight);
299 pszComma = strchr(szBuffer, ',');
300 if (pszComma)
301 *pszComma = '.';
302 osStyle += CPLString().Printf(",s:%sg", szBuffer);
303 }
304
305 const unsigned char *pabyDWGColors = ACGetColorTable();
306
307 snprintf(szBuffer, sizeof(szBuffer), ",c:#%02x%02x%02x",
308 pabyDWGColors[nColor * 3 + 0], pabyDWGColors[nColor * 3 + 1],
309 pabyDWGColors[nColor * 3 + 2]);
310 osStyle += szBuffer;
311
312 if (bHidden)
313 osStyle += "00";
314
315 osStyle += ")";
316
317 poLabelFeature->SetStyleString(osStyle);
318
319 apoPendingFeatures.push(poLabelFeature);
320
321 return poFeature;
322 }
323
324 /************************************************************************/
325 /* FormatDimension() */
326 /* */
327 /* Format a dimension number according to the current files */
328 /* formatting conventions. */
329 /************************************************************************/
330
331 void OGRDWGLayer::FormatDimension(CPLString &osText, double dfValue)
332
333 {
334 int nPrecision = atoi(poDS->GetVariable("$LUPREC", "4"));
335 char szFormat[32];
336 char szBuffer[64];
337
338 // we could do a significantly more precise formatting if we want
339 // to spend the effort. See QCAD's rs_dimlinear.cpp and related files
340 // for example.
341
342 snprintf(szFormat, sizeof(szFormat), "%%.%df", nPrecision);
343 CPLsnprintf(szBuffer, sizeof(szBuffer), szFormat, dfValue);
344 char *pszComma = strchr(szBuffer, ',');
345 if (pszComma)
346 *pszComma = '.';
347 osText = szBuffer;
348 }