Debian Codegraph @

ogr/ogrsf_frmts/dxf/ogrdxf_dimension.cpp

raw symbols SCIP
1 /******************************************************************************
2 *
3 * Project: DXF Translator
4 * Purpose: Implements translation support for DIMENSION elements as a part
5 * of the OGRDXFLayer class.
6 * Author: Frank Warmerdam, [email protected]
7 *
8 ******************************************************************************
9 * Copyright (c) 2009, Frank Warmerdam <[email protected]>
10 * Copyright (c) 2010, Even Rouault <even dot rouault at spatialys.com>
11 * Copyright (c) 2017, Alan Thomas <[email protected]>
12 *
13 * SPDX-License-Identifier: MIT
14 ****************************************************************************/
15
16 #include "ogr_dxf.h"cxx . . $ `<file>/ogr/ogrsf_frmts/dxf/ogr_dxf.h`/
17 #include "cpl_conv.h"cxx . . $ `<file>/port/cpl_conv.h`/
18
19 #include <stdexcept>
20
21 /************************************************************************/
22 /* PointDist() */
23 /************************************************************************/
24
25 #ifndef PointDist_defined
26 #define PointDist_definedcxx . . $ `ogr/ogrsf_frmts/dxf/ogrdxf_dimension.cpp:26:9`!
27
28 inline static double PointDistcxx . . $ PointDist(6ba6c9f826937c6e).(double x1, double y1, double x2, double y2)
29 {
30 return sqrtcxx . . $ sqrt(fee4f1d22bc8e617).((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
31 }
32 #endif
33
34 /************************************************************************/
35 /* TranslateDIMENSION() */
36 /************************************************************************/
37
38 OGRDXFFeaturecxx . . $ OGRDXFFeature# *OGRDXFLayercxx . . $ OGRDXFLayer#::TranslateDIMENSIONcxx . . $ OGRDXFLayer#TranslateDIMENSION(e6624aae671b4354).()
39
40 {
41 char szLineBuf[257];
42 int nCode = 0;
43 // int nDimType = 0;
44 OGRDXFFeaturecxx . . $ OGRDXFFeature# *poFeature = new OGRDXFFeaturecxx . . $ OGRDXFFeature#OGRDXFFeature(dc2636911e7adfe).(poFeatureDefncxx . . $ OGRDXFLayer#poFeatureDefn.);
45 double dfArrowX1 = 0.0;
46 double dfArrowY1 = 0.0;
47 // double dfArrowZ1 = 0.0;
48 double dfTargetX1 = 0.0;
49 double dfTargetY1 = 0.0;
50 // double dfTargetZ1 = 0.0;
51 double dfTargetX2 = 0.0;
52 double dfTargetY2 = 0.0;
53 // double dfTargetZ2 = 0.0;
54 double dfTextX = 0.0;
55 double dfTextY = 0.0;
56 // double dfTextZ = 0.0;
57
58 bool bReadyForDimstyleOverride = false;
59
60 bool bHaveBlock = false;
61 CPLString osBlockName;
62 CPLString osText;
63
64 stdcxx . . $ std/::mapcxx . . $ std/map#<CPLString, CPLString> oDimStylePropertiescxx . . $ std/map#map(49f6e7a06ebc5aa8).;
65 poDScxx . . $ OGRDXFLayer#poDS.->PopulateDefaultDimStylePropertiescxx . . $ OGRDXFDataSource#PopulateDefaultDimStyleProperties(8627e5f9e25fbdfb).(oDimStyleProperties);
66
67 while ((nCode = poDScxx . . $ OGRDXFLayer#poDS.->ReadValuecxx . . $ OGRDXFDataSource#ReadValue(6abd07ddb1a912f0).(szLineBuf, sizeof(szLineBuf))) > 0)
68 {
69 switch (nCode)
70 {
71 case 2:
72 bHaveBlock = true;
73 osBlockName = szLineBuf;
74 break;
75
76 case 3:
77 // 3 is the dimension style name. We don't need to store it,
78 // let's just fetch the dimension style properties
79 poDScxx . . $ OGRDXFLayer#poDS.->LookupDimStylecxx . . $ OGRDXFDataSource#LookupDimStyle(757a4c7d0decb8ae).(szLineBuf, oDimStyleProperties);
80 break;
81
82 case 10:
83 dfArrowX1 = CPLAtof(szLineBuf);
84 break;
85
86 case 20:
87 dfArrowY1 = CPLAtof(szLineBuf);
88 break;
89
90 case 30:
91 /* dfArrowZ1 = CPLAtof(szLineBuf); */
92 break;
93
94 case 11:
95 dfTextX = CPLAtof(szLineBuf);
96 break;
97
98 case 21:
99 dfTextY = CPLAtof(szLineBuf);
100 break;
101
102 case 31:
103 /* dfTextZ = CPLAtof(szLineBuf); */
104 break;
105
106 case 13:
107 dfTargetX2 = CPLAtof(szLineBuf);
108 break;
109
110 case 23:
111 dfTargetY2 = CPLAtof(szLineBuf);
112 break;
113
114 case 33:
115 /* dfTargetZ2 = CPLAtof(szLineBuf); */
116 break;
117
118 case 14:
119 dfTargetX1 = CPLAtof(szLineBuf);
120 break;
121
122 case 24:
123 dfTargetY1 = CPLAtof(szLineBuf);
124 break;
125
126 case 34:
127 /* dfTargetZ1 = CPLAtof(szLineBuf); */
128 break;
129
130 case 70:
131 /* nDimType = atoi(szLineBuf); */
132 break;
133
134 case 1:
135 osText = szLineBuf;
136 break;
137
138 case 1001:
139 bReadyForDimstyleOverride = EQUALcxx . . $ `port/cpl_port.h:532:9`!(szLineBuf, "ACAD");
140 break;
141
142 case 1070:
143 if (bReadyForDimstyleOverride)
144 {
145 // Store DIMSTYLE override values in the dimension
146 // style property map. The nInnerCode values match the
147 // group codes used in the DIMSTYLE table.
148 const int nInnerCode = atoicxx . . $ atoi(f5ecf3191ed0cd93).(szLineBuf);
149 const char *pszProperty =
150 ACGetDimStylePropertyNamecxx . . $ ACGetDimStylePropertyName(f7ae7e56280f18ae).(nInnerCode);
151 if (pszProperty)
152 {
153 nCode = poDScxx . . $ OGRDXFLayer#poDS.->ReadValuecxx . . $ OGRDXFDataSource#ReadValue(6abd07ddb1a912f0).(szLineBuf, sizeof(szLineBuf));
154 if (nCode == 1005 || nCode == 1040 || nCode == 1070)
155 oDimStyleProperties[cxx . . $ std/map#`operator[]`(da459db78e6e18a3).pszProperty] = szLineBuf;
156 }
157 }
158 break;
159
160 default:
161 TranslateGenericPropertycxx . . $ OGRDXFLayer#TranslateGenericProperty(66d983117de70e31).(poFeature, nCode, szLineBuf);
162 break;
163 }
164 }
165 if (nCode < 0)
166 {
167 DXF_LAYER_READER_ERRORcxx . . $ `ogr/ogrsf_frmts/dxf/ogr_dxf.h:586:9`!();
168 delete poFeature;
169 return nullptr;
170 }
171 if (nCode == 0)
172 poDScxx . . $ OGRDXFLayer#poDS.->UnreadValuecxx . . $ OGRDXFDataSource#UnreadValue(49f6e7a06ebc5aa8).();
173
174 // If osBlockName (group code 2) refers to a valid block, we can just insert
175 // that block - that should give us the correctly exploded geometry of this
176 // dimension. If this value is missing, or doesn't refer to a valid block,
177 // we will need to use our own logic to generate the dimension lines.
178 if (bHaveBlock && osBlockName.length() > 0)
179 {
180 // Always inline the block, because this is an anonymous block that the
181 // user likely doesn't know or care about
182 try
183 {
184 OGRDXFFeaturecxx . . $ OGRDXFFeature# *poBlockFeature = InsertBlockInlinecxx . . $ OGRDXFLayer#InsertBlockInline(10516ba2e234833c).(
185 CPLGetErrorCounter(), osBlockName, OGRDXFInsertTransformercxx . . $ OGRDXFInsertTransformer#OGRDXFInsertTransformer(ced63f7c635d850d).(),
186 poFeature, apoPendingFeaturescxx . . $ OGRDXFLayer#apoPendingFeatures., true, false);
187
188 return poBlockFeature; // may be NULL but that is OK
189 }
190 catch (const stdcxx . . $ std/::invalid_argument &)
191 {
192 }
193 }
194
195 // Unpack the dimension style
196 const double dfScale = CPLAtof(oDimStyleProperties[cxx . . $ std/map#`operator[]`(da459db78e6e18a3)."DIMSCALE"]);
197 const double dfArrowheadSize = CPLAtof(oDimStyleProperties[cxx . . $ std/map#`operator[]`(da459db78e6e18a3)."DIMASZ"]);
198 const double dfExtLineExtendLength = CPLAtof(oDimStyleProperties[cxx . . $ std/map#`operator[]`(da459db78e6e18a3)."DIMEXE"]);
199 const double dfExtLineOffset = CPLAtof(oDimStyleProperties[cxx . . $ std/map#`operator[]`(da459db78e6e18a3)."DIMEXO"]);
200 const bool bWantExtLine1 = atoicxx . . $ atoi(f5ecf3191ed0cd93).(oDimStyleProperties[cxx . . $ std/map#`operator[]`(da459db78e6e18a3)."DIMSE1"]) == 0;
201 const bool bWantExtLine2 = atoicxx . . $ atoi(f5ecf3191ed0cd93).(oDimStyleProperties[cxx . . $ std/map#`operator[]`(da459db78e6e18a3)."DIMSE2"]) == 0;
202 const double dfTextHeight = CPLAtof(oDimStyleProperties[cxx . . $ std/map#`operator[]`(da459db78e6e18a3)."DIMTXT"]);
203 const int nUnitsPrecision = atoicxx . . $ atoi(f5ecf3191ed0cd93).(oDimStyleProperties[cxx . . $ std/map#`operator[]`(da459db78e6e18a3)."DIMDEC"]);
204 const bool bTextSupposedlyCentered =
205 atoicxx . . $ atoi(f5ecf3191ed0cd93).(oDimStyleProperties[cxx . . $ std/map#`operator[]`(da459db78e6e18a3)."DIMTAD"]) == 0;
206 CPLString osTextColor = oDimStyleProperties[cxx . . $ std/map#`operator[]`(da459db78e6e18a3)."DIMCLRT"];
207
208 /*************************************************************************
209
210 DIMENSION geometry layout
211
212 (11,21)(text center point)
213 | DimText |
214 (10,20) X<--------------------------------->X (Arrow2 - computed)
215 (Arrow1)| |
216 | |
217 | X (13,23) (Target2)
218 |
219 X (14,24) (Target1)
220
221 Given:
222 Locations Arrow1, Target1, and Target2 we need to compute Arrow2.
223
224 Steps:
225 1) Compute direction vector from Target1 to Arrow1 (Vec1).
226 2) Compute direction vector for arrow as perpendicular to Vec1 (call Vec2).
227 3) Compute Arrow2 location as intersection between line defined by
228 Vec2 and Arrow1 and line defined by Target2 and direction Vec1 (call
229 Arrow2)
230
231 Then we can draw lines for the various components.
232
233 Note that Vec1 and Vec2 may be horizontal, vertical or on an angle but
234 the approach is as above in all these cases.
235
236 *************************************************************************/
237
238 /* -------------------------------------------------------------------- */
239 /* Step 1, compute direction vector between Target1 and Arrow1. */
240 /* -------------------------------------------------------------------- */
241 double dfVec1X = dfArrowX1 - dfTargetX1;
242 double dfVec1Y = dfArrowY1 - dfTargetY1;
243
244 // make Vec1 a unit vector
245 double dfVec1Length = PointDistcxx . . $ PointDist(6ba6c9f826937c6e).(0, 0, dfVec1X, dfVec1Y);
246 if (dfVec1Length > 0.0)
247 {
248 dfVec1X /= dfVec1Length;
249 dfVec1Y /= dfVec1Length;
250 }
251
252 /* -------------------------------------------------------------------- */
253 /* Step 2, compute the direction vector from Arrow1 to Arrow2 */
254 /* as a perpendicular to Vec1. */
255 /* -------------------------------------------------------------------- */
256 double dfVec2X = dfVec1Y;
257 double dfVec2Y = -dfVec1X;
258
259 /* -------------------------------------------------------------------- */
260 /* Step 3, compute intersection of line from target2 along */
261 /* direction vector 1, with the line through Arrow1 and */
262 /* direction vector 2. */
263 /* -------------------------------------------------------------------- */
264 double dfArrowX2 = 0.0;
265 double dfArrowY2 = 0.0;
266
267 // special case if vec1 is zero, which means the arrow and target
268 // points coincide.
269 if (dfVec1X == 0.0 && dfVec1Y == 0.0)
270 {
271 dfArrowX2 = dfTargetX2;
272 dfArrowY2 = dfTargetY2;
273 }
274
275 // special case if vec1 is vertical.
276 else if (dfVec1X == 0.0)
277 {
278 dfArrowX2 = dfTargetX2;
279 dfArrowY2 = dfArrowY1;
280 }
281
282 // special case if vec1 is horizontal.
283 else if (dfVec1Y == 0.0)
284 {
285 dfArrowX2 = dfArrowX1;
286 dfArrowY2 = dfTargetY2;
287 }
288
289 else // General case for diagonal vectors.
290 {
291 // first convert vec1 + target2 into y = mx + b format: call this L1
292
293 const double dfL1M = dfVec1Y / dfVec1X;
294 const double dfL1B = dfTargetY2 - dfL1M * dfTargetX2;
295
296 // convert vec2 + Arrow1 into y = mx + b format, call this L2
297
298 const double dfL2M = dfVec2Y / dfVec2X;
299 const double dfL2B = dfArrowY1 - dfL2M * dfArrowX1;
300
301 // Compute intersection x = (b2-b1) / (m1-m2)
302
303 dfArrowX2 = (dfL2B - dfL1B) / (dfL1M - dfL2M);
304 dfArrowY2 = dfL2M * dfArrowX2 + dfL2B;
305 }
306
307 /* -------------------------------------------------------------------- */
308 /* Create geometries for the different components of the */
309 /* dimension object. */
310 /* -------------------------------------------------------------------- */
311 OGRMultiLineStringcxx . . $ OGRMultiLineString# *poMLS = new OGRMultiLineStringcxx . . $ OGRMultiLineString#OGRMultiLineString(49f6e7a06ebc5aa8).();
312 OGRLineStringcxx . . $ OGRLineString# oLinecxx . . $ OGRLineString#OGRLineString(49f6e7a06ebc5aa8).;
313
314 // Main arrow line between Arrow1 and Arrow2.
315 oLine.setPointcxx . . $ OGRSimpleCurve#setPoint(9f72e67440d8b329).(0, dfArrowX1, dfArrowY1);
316 oLine.setPointcxx . . $ OGRSimpleCurve#setPoint(9f72e67440d8b329).(1, dfArrowX2, dfArrowY2);
317 poMLS->addGeometrycxx . . $ OGRGeometryCollection#addGeometry(fed901b2566802ff).(&oLine);
318
319 // Insert default arrowheads.
320 InsertArrowheadcxx . . $ OGRDXFLayer#InsertArrowhead(e25ab9807fa4c0a1).(poFeature, "", &oLine, dfArrowheadSize * dfScale);
321 InsertArrowheadcxx . . $ OGRDXFLayer#InsertArrowhead(e25ab9807fa4c0a1).(poFeature, "", &oLine, dfArrowheadSize * dfScale, true);
322
323 // Dimension line from Target1 to Arrow1 with a small extension.
324 oLine.setPointcxx . . $ OGRSimpleCurve#setPoint(9f72e67440d8b329).(0, dfTargetX1 + dfVec1X * dfExtLineOffset,
325 dfTargetY1 + dfVec1Y * dfExtLineOffset);
326 oLine.setPointcxx . . $ OGRSimpleCurve#setPoint(9f72e67440d8b329).(1, dfArrowX1 + dfVec1X * dfExtLineExtendLength,
327 dfArrowY1 + dfVec1Y * dfExtLineExtendLength);
328 if (bWantExtLine1 && oLine.get_Lengthcxx . . $ OGRSimpleCurve#get_Length(21b6c25e127938e2).() > 0.0)
329 {
330 poMLS->addGeometrycxx . . $ OGRGeometryCollection#addGeometry(fed901b2566802ff).(&oLine);
331 }
332
333 // Dimension line from Target2 to Arrow2 with a small extension.
334 oLine.setPointcxx . . $ OGRSimpleCurve#setPoint(9f72e67440d8b329).(0, dfTargetX2 + dfVec1X * dfExtLineOffset,
335 dfTargetY2 + dfVec1Y * dfExtLineOffset);
336 oLine.setPointcxx . . $ OGRSimpleCurve#setPoint(9f72e67440d8b329).(1, dfArrowX2 + dfVec1X * dfExtLineExtendLength,
337 dfArrowY2 + dfVec1Y * dfExtLineExtendLength);
338 if (bWantExtLine2 && oLine.get_Lengthcxx . . $ OGRSimpleCurve#get_Length(21b6c25e127938e2).() > 0.0)
339 {
340 poMLS->addGeometrycxx . . $ OGRGeometryCollection#addGeometry(fed901b2566802ff).(&oLine);
341 }
342
343 poFeature->SetGeometryDirectlycxx . . $ OGRFeature#SetGeometryDirectly(cec3429dca426eb8).(poMLS);
344
345 PrepareLineStylecxx . . $ OGRDXFLayer#PrepareLineStyle(7c5a60073ff99cd2).(poFeature);
346
347 /* -------------------------------------------------------------------- */
348 /* Prepare a new feature to serve as the dimension text label */
349 /* feature. We will push it onto the layer as a pending */
350 /* feature for the next feature read. */
351 /* */
352 /* The DXF format supports a myriad of options for dimension */
353 /* text placement, some of which involve the drawing of */
354 /* additional lines and the like. For now we ignore most of */
355 /* those properties and place the text alongside the dimension */
356 /* line. */
357 /* -------------------------------------------------------------------- */
358
359 // a single space suppresses labeling.
360 if (osText == " ")
361 return poFeature;
362
363 OGRDXFFeaturecxx . . $ OGRDXFFeature# *poLabelFeature = poFeature->CloneDXFFeaturecxx . . $ OGRDXFFeature#CloneDXFFeature(e6624aae671b4354).();
364
365 poLabelFeature->SetGeometryDirectlycxx . . $ OGRFeature#SetGeometryDirectly(cec3429dca426eb8).(new OGRPointcxx . . $ OGRPoint#OGRPoint(bc12c9ec15586e6a).(dfTextX, dfTextY));
366
367 if (osText.empty())
368 osText = "<>";
369
370 // Do we need to compute the dimension value?
371 size_t nDimensionPos = osText.find("<>");
372 if (nDimensionPos == stdcxx . . $ std/::string::npos)
373 {
374 poLabelFeature->SetFieldcxx . . $ OGRFeature#SetField(2fa786934fe80e24).("Text", TextUnescapecxx . . $ OGRDXFLayer#TextUnescape(b291ddbdb582e104).(osText, true));
375 }
376 else
377 {
378 // Replace the first occurrence of <> with the dimension
379 CPLString osDimensionText;
380 FormatDimensioncxx . . $ OGRDXFLayer#FormatDimension(19162bf32f9dfd01).(osDimensionText,
381 PointDistcxx . . $ PointDist(6ba6c9f826937c6e).(dfArrowX1, dfArrowY1, dfArrowX2, dfArrowY2),
382 nUnitsPrecision);
383 osText.replace(nDimensionPos, 2, osDimensionText);
384 poLabelFeature->SetFieldcxx . . $ OGRFeature#SetField(2fa786934fe80e24).("Text", TextUnescapecxx . . $ OGRDXFLayer#TextUnescape(b291ddbdb582e104).(osText, true));
385 }
386
387 CPLString osStyle;
388 char szBuffer[64];
389
390 osStyle.Printf("LABEL(f:\"Arial\",t:\"%s\"",
391 TextUnescapecxx . . $ OGRDXFLayer#TextUnescape(b291ddbdb582e104).(osText.c_str(), true).c_str());
392
393 // If the text is supposed to be centered on the line, we align
394 // it above the line. Drawing it properly would require us to
395 // work out the width of the text, which seems like too much
396 // effort for what is just a fallback renderer.
397 if (bTextSupposedlyCentered)
398 osStyle += ",p:11";
399 else
400 osStyle += ",p:5";
401
402 // Compute the text angle. Use atan to avoid upside-down text
403 const double dfTextAngle =
404 (dfArrowX1 == dfArrowX2)
405 ? -90.0
406 : atancxx . . $ atan(fee4f1d22bc8e617).((dfArrowY1 - dfArrowY2) / (dfArrowX1 - dfArrowX2)) * 180.0 /
407 M_PIcxx . . $ `<external>/11263917310783833665/math.h:1289:10`!;
408
409 if (dfTextAngle != 0.0)
410 {
411 CPLsnprintf(szBuffer, sizeof(szBuffer), "%.3g", dfTextAngle);
412 osStyle += CPLString().Printf(",a:%s", szBuffer);
413 }
414
415 if (dfTextHeight != 0.0)
416 {
417 CPLsnprintf(szBuffer, sizeof(szBuffer), "%.3g", dfTextHeight * dfScale);
418 osStyle += CPLString().Printf(",s:%sg", szBuffer);
419 }
420
421 poLabelFeature->oStylePropertiescxx . . $ OGRDXFFeature#oStyleProperties.[cxx . . $ std/map#`operator[]`(da459db78e6e18a3)."Color"] = stdcxx . . $ std/::move(osTextColor);
422 osStyle += ",c:";
423 osStyle += poLabelFeature->GetColorcxx . . $ OGRDXFFeature#GetColor(6c955dda065cd6e3).(poDScxx . . $ OGRDXFLayer#poDS., poFeature);
424
425 osStyle += ")";
426
427 poLabelFeature->SetStyleStringcxx . . $ OGRFeature#SetStyleString(85c52e162fed56f9).(osStyle);
428
429 apoPendingFeaturescxx . . $ OGRDXFLayer#apoPendingFeatures..pushcxx . . $ OGRDXFFeatureQueue#push(409eaf123d04801f).(poLabelFeature);
430
431 return poFeature;
432 }
433
434 /************************************************************************/
435 /* FormatDimension() */
436 /* */
437 /* Format a dimension number according to the current files */
438 /* formatting conventions. */
439 /************************************************************************/
440
441 void OGRDXFLayercxx . . $ OGRDXFLayer#::FormatDimensioncxx . . $ OGRDXFLayer#FormatDimension(19162bf32f9dfd01).(CPLString &osText, const double dfValue,
442 int nPrecision)
443
444 {
445 if (nPrecision < 0)
446 nPrecision = 0;
447 else if (nPrecision > 20)
448 nPrecision = 20;
449
450 // We could do a significantly more precise formatting if we want
451 // to spend the effort. See QCAD's rs_dimlinear.cpp and related files
452 // for example.
453
454 char szFormat[32];
455 snprintf(szFormat, sizeof(szFormat), "%%.%df", nPrecision);
456
457 char szBuffer[64];
458 CPLsnprintf(szBuffer, sizeof(szBuffer), szFormat, dfValue);
459
460 osText = szBuffer;
461 }