-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathopennurbs_dimension.cpp
5832 lines (5153 loc) · 155 KB
/
opennurbs_dimension.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 1993-2022 Robert McNeel & Associates. All rights reserved.
// OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
// McNeel & Associates.
//
// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
// MERCHANTABILITY ARE HEREBY DISCLAIMED.
//
// For complete openNURBS copyright information see <http://www.opennurbs.org>.
//
////////////////////////////////////////////////////////////////
#include "opennurbs.h"
#if !defined(ON_COMPILING_OPENNURBS)
// This check is included in all opennurbs source .c and .cpp files to insure
// ON_COMPILING_OPENNURBS is defined when opennurbs source is compiled.
// When opennurbs source is being compiled, ON_COMPILING_OPENNURBS is defined
// and the opennurbs .h files alter what is declared and how it is declared.
#error ON_COMPILING_OPENNURBS must be defined when compiling opennurbs
#endif
#include "opennurbs_internal_defines.h"
ON_VIRTUAL_OBJECT_IMPLEMENT(ON_Dimension, ON_Annotation, "EE6571FE-1596-4D5B-BD6D-7072B0643986");
ON_OBJECT_IMPLEMENT(ON_DimLinear, ON_Dimension, "E550882B-F44D-4154-A1EF-6E50CBBBF543");
ON_OBJECT_IMPLEMENT(ON_DimAngular, ON_Dimension, "D417786B-F6CD-4F12-9E1F-063F414DBEB6");
ON_OBJECT_IMPLEMENT(ON_DimRadial, ON_Dimension, "FC749C2F-4C00-41FD-9840-26D94F047AD3");
ON_OBJECT_IMPLEMENT(ON_DimOrdinate, ON_Dimension, "03124828-4C9B-4D28-9A82-664DDDE7A14F");
ON_OBJECT_IMPLEMENT(ON_Centermark, ON_Dimension, "D46767BA-7E8F-4D9D-9A92-66050219A5B9");
ON_Dimension::ON_Dimension(ON::AnnotationType annotation_type)
: ON_Annotation(annotation_type)
{}
ON_Dimension::~ON_Dimension()
{
Internal_Destroy();
}
ON_Dimension::ON_Dimension( const ON_Dimension& src )
: ON_Annotation(src)
{
Internal_CopyFrom(src);
}
ON_Dimension& ON_Dimension::operator=(
const ON_Dimension& src
)
{
if (this != &src)
{
Internal_Destroy();
ON_Annotation::operator=(src);
Internal_CopyFrom(src);
}
return *this;
}
void ON_Dimension::Internal_Destroy()
{
m_user_text.Destroy();
}
void ON_Dimension::Internal_CopyFrom(const ON_Dimension& src)
{
// m_text_rotation NOT used in 6.0 - m_text_rotation = src.m_text_rotation;
m_use_default_text_point = src.m_use_default_text_point;
m_user_text_point = src.m_user_text_point;
m_user_text = src.m_user_text;
m_distance_scale = src.m_distance_scale;
m_detail_measured = src.m_detail_measured;
m_flip_arrow_1 = src.m_flip_arrow_1;
m_flip_arrow_2 = src.m_flip_arrow_2;
}
bool ON_Dimension::IsValid(ON_TextLog* text_log) const
{
return true;
}
ON_2dPoint ON_Dimension::TextPoint() const
{
if (m_use_default_text_point)
return DefaultTextPoint();
else
return m_user_text_point;
}
void ON_Dimension::Set2dTextPoint(const ON_2dPoint& textpoint)
{
SetUseDefaultTextPoint(false);
m_user_text_point = textpoint;
}
ON_2dPoint ON_Dimension::DefaultTextPoint() const
{
return ON_2dPoint::Origin;
}
bool ON_Dimension::UseDefaultTextPoint() const
{
return m_use_default_text_point;
}
void ON_Dimension::SetUseDefaultTextPoint(bool usedefault)
{
m_use_default_text_point = usedefault;
}
const wchar_t* ON_Dimension::UserText() const
{
return static_cast< const wchar_t* >(m_user_text);
}
void ON_Dimension::SetUserText(const wchar_t* text)
{
if (nullptr == text)
return;
if (m_user_text.CompareOrdinal(text, false))
{
if (0 == *text)
m_user_text = L"<>";
else
m_user_text = text;
m_plain_user_text.Empty();
}
}
const wchar_t* ON_Dimension::PlainUserText() const
{
if (m_plain_user_text.IsEmpty())
{
ON_TextContent tc;
tc.Create(UserText(), Type(), &ON_DimStyle::Default);
m_plain_user_text = tc.PlainText();
}
return static_cast< const wchar_t* >(m_plain_user_text);
}
// Add to natural rotation
double ON_Dimension::TextRotation() const
{
// This V5 function should have been removed from the 6.0 SDK.
// It returned some angle in radians and it doesn't do anything in V6. It was almost always zero.
// Text rotation is handled completely differently in V5 an V6.
return 0.0;
}
void ON_Dimension::SetTextRotation(double ignored_rotation_radians)
{
// This V5 function and m_text_rotation should have been removed from the 6.0 SDK.
// Text rotation is handled completely differently in V5 an V6.
// m_text_rotation = remainder(rotation_radians, (2.0 * ON_PI));
return;
}
bool ON_Dimension::GetTextRect(ON_3dPoint text_rect[4]) const
{
const ON_TextContent* text = Text();
if (nullptr != text)
{
ON_BoundingBox text_box;
if (text->GetTightBoundingBox(text_box))
{
ON_3dPoint text_point( TextPoint());
text_rect[0].Set(text_box.m_min.x, text_box.m_min.y, 0.0);
text_rect[1].Set(text_box.m_max.x, text_box.m_min.y, 0.0);
text_rect[2].Set(text_box.m_max.x, text_box.m_max.y, 0.0);
text_rect[3].Set(text_box.m_min.x, text_box.m_max.y, 0.0);
return true;
}
}
return false;
}
ON_TextContent* ON_Dimension::RebuildDimensionText(
ON::LengthUnitSystem units_in,
const ON_DimStyle* dimstyle,
bool expandanglebrackets
) const
{
if (Type() == ON::AnnotationType::CenterMark)
return nullptr;
ON_wString displaytext;
if (expandanglebrackets)
{
if (!GetDistanceDisplayText(units_in, dimstyle, displaytext))
return nullptr;
}
else
{
displaytext = displaytext + UserText();
if (dimstyle->Prefix().IsNotEmpty() || dimstyle->Suffix().IsNotEmpty())
{
int ci = displaytext.Find(L"<>");
if (ci > -1)
{
ON_wString right;
if (displaytext.Length() > ci + 2)
right = displaytext.Right(displaytext.Length() - ci - 2);
displaytext = displaytext.Left(ci);
displaytext = displaytext + dimstyle->Prefix();
displaytext = displaytext + L"<>";
displaytext = displaytext + dimstyle->Suffix();
displaytext = displaytext + right;
}
}
}
ON_TextContent* newtext = new ON_TextContent;
if (nullptr != newtext)
{
bool wrapped = m_text ? m_text->TextIsWrapped() : false;
double rect_width = m_text ? m_text->FormattingRectangleWidth() : 0.0;
double rotation = m_text ? m_text->TextRotationRadians() : 0.0;
if (newtext->Create(displaytext, Type(), dimstyle,wrapped, rect_width, rotation))
{
#ifdef _DEBUG
newtext->IsValid();
#endif
}
}
return newtext;
}
bool ON_Dimension::UpdateDimensionText(
ON::LengthUnitSystem units_in,
const ON_DimStyle* dimstyle
) const
{
if (Type() == ON::AnnotationType::CenterMark)
return false;
ON_TextContent* newtext = RebuildDimensionText(units_in, dimstyle, true);
if (nullptr != newtext)
{
SetText(newtext);
return true;
}
return false;
}
bool ON_Dimension::GetDistanceDisplayText(
ON::LengthUnitSystem units_in,
const ON_DimStyle* dimstyle,
ON_wString& displaytext) const
{
if (Type() == ON::AnnotationType::CenterMark)
return false;
if (nullptr == dimstyle)
return false;
double measurement = Measurement();
const wchar_t* user_text = UserText();
ON_TextContent::FormatDistanceMeasurement(measurement, units_in, dimstyle, user_text, displaytext);
return true;
}
bool ON_Dimension::ArrowIsFlipped(int i) const
{
if (i == 0)
return m_flip_arrow_1;
else
return m_flip_arrow_2;
}
void ON_Dimension::FlipArrow(int i, bool flip) const
{
if (i == 0)
m_flip_arrow_1 = flip;
else
m_flip_arrow_2 = flip;
}
double ON_Dimension::DistanceScale() const
{
return m_distance_scale;
}
void ON_Dimension::SetDistanceScale(double distance_scale) const
{
m_distance_scale = distance_scale;
}
ON_UUID ON_Dimension::DetailMeasured() const
{
return m_detail_measured;
}
void ON_Dimension::SetDetailMeasured(ON_UUID uuid)
{
m_detail_measured = uuid;
}
ON_Dimension::ForceArrow ON_Dimension::ForceArrowPosition() const
{
ON_ERROR("Use ON_Dimension::ArrowFit(const ON_DimStyle* parent_style)");
return ON_Dimension::ForceArrow::Auto;
}
void ON_Dimension::SetForceArrowPosition(ON_Dimension::ForceArrow force)
{
//
ON_ERROR("Use ON_Dimension::SetArrowFit(const ON_DimStyle* parent_style,ON_DimStyle::arrow_fit arrowfit)");
}
ON_Dimension::ForceText ON_Dimension::ForceTextPosition() const
{
ON_ERROR("Use ON_Dimension::TextFit(const ON_DimStyle* parent_style)");
return ON_Dimension::ForceText::Auto;
}
void ON_Dimension::SetForceTextPosition(ON_Dimension::ForceText force)
{
ON_ERROR("Use ON_Dimension::SetTextFit(const ON_DimStyle* parent_style,ON_DimStyle::text_fit textfit)");
}
//--------------------------------
void ON_Dimension::SetForceDimLine(
const ON_DimStyle* parent_style,
bool force_dimline
)
{
parent_style = &ON_DimStyle::DimStyleOrDefault(parent_style);
bool bCreate = (force_dimline != parent_style->ForceDimLine());
ON_DimStyle* override_style = Internal_GetOverrideStyle(bCreate);
if (nullptr != override_style)
{
override_style->SetForceDimLine(force_dimline);
override_style->SetFieldOverride(ON_DimStyle::field::ForceDimLine, bCreate);
}
}
bool ON_Dimension::ForceDimLine(
const ON_DimStyle* parent_style) const
{
return Internal_StyleForFieldQuery(parent_style, ON_DimStyle::field::ForceDimLine).ForceDimLine();
}
void ON_Dimension::SetTextFit(
const ON_DimStyle* parent_style,
ON_DimStyle::text_fit textfit)
{
parent_style = &ON_DimStyle::DimStyleOrDefault(parent_style);
bool bCreate = (textfit != parent_style->TextFit());
ON_DimStyle* override_style = Internal_GetOverrideStyle(bCreate);
if (nullptr != override_style)
{
override_style->SetTextFit(textfit);
override_style->SetFieldOverride(ON_DimStyle::field::TextFit, bCreate);
}
}
ON_DimStyle::text_fit ON_Dimension::TextFit(
const ON_DimStyle* parent_style) const
{
return Internal_StyleForFieldQuery(parent_style, ON_DimStyle::field::TextFit).TextFit();
}
void ON_Dimension::SetArrowFit(
const ON_DimStyle* parent_style,
ON_DimStyle::arrow_fit arrowfit)
{
parent_style = &ON_DimStyle::DimStyleOrDefault(parent_style);
bool bCreate = (arrowfit != parent_style->ArrowFit());
ON_DimStyle* override_style = Internal_GetOverrideStyle(bCreate);
if (nullptr != override_style)
{
override_style->SetArrowFit(arrowfit);
override_style->SetFieldOverride(ON_DimStyle::field::TextFit, bCreate);
}
}
ON_DimStyle::arrow_fit ON_Dimension::ArrowFit(
const ON_DimStyle* parent_style) const
{
return Internal_StyleForFieldQuery(parent_style, ON_DimStyle::field::ArrowFit).ArrowFit();
}
//----------------------------------------------------------
// Class ON_DimLinear
ON_DimLinear::ON_DimLinear()
: ON_Dimension(ON::AnnotationType::Rotated)
{}
ON_Dimension::ForceArrow ON_Dimension::ForceArrowFromUnsigned(
unsigned int force_arrow_as_unsigned)
{
switch (force_arrow_as_unsigned)
{
ON_ENUM_FROM_UNSIGNED_CASE(ON_Dimension::ForceArrow::Auto);
ON_ENUM_FROM_UNSIGNED_CASE(ON_Dimension::ForceArrow::Inside);
ON_ENUM_FROM_UNSIGNED_CASE(ON_Dimension::ForceArrow::Outside);
}
ON_ERROR("Invalid type_as_unsigned parameter.");
return (ON_Dimension::ForceArrow::Auto);
}
ON_Dimension::ForceText ON_Dimension::ForceTextFromUnsigned(
unsigned int force_text_as_unsigned)
{
switch (force_text_as_unsigned)
{
ON_ENUM_FROM_UNSIGNED_CASE(ON_Dimension::ForceText::Auto);
ON_ENUM_FROM_UNSIGNED_CASE(ON_Dimension::ForceText::Inside);
ON_ENUM_FROM_UNSIGNED_CASE(ON_Dimension::ForceText::Right);
ON_ENUM_FROM_UNSIGNED_CASE(ON_Dimension::ForceText::Left);
ON_ENUM_FROM_UNSIGNED_CASE(ON_Dimension::ForceText::HintRight);
ON_ENUM_FROM_UNSIGNED_CASE(ON_Dimension::ForceText::HintLeft);
}
ON_ERROR("Invalid type_as_unsigned parameter.");
return (ON_Dimension::ForceText::Auto);
}
bool ON_Dimension::Internal_WriteDimension(
ON_BinaryArchive& archive
) const
{
// content_version = 1 added m_force_textpos
const int content_version = 1;
if (false == archive.BeginWrite3dmAnonymousChunk(content_version))
return false;
bool rc = false;
for (;;)
{
if (!ON_Annotation::Internal_WriteAnnotation(archive))
break;
const ON_DimStyle& ds = archive.ArchiveCurrentDimStyle();
if (!archive.WriteString(m_user_text))
break;
if (!archive.WriteDouble(0.0)) // OBSOLETE m_text_rotation
break;
if (!archive.WriteBool(m_use_default_text_point))
break;
if (!archive.WritePoint(m_user_text_point))
break;
if (!archive.WriteBool(m_flip_arrow_1))
break;
if (!archive.WriteBool(m_flip_arrow_2))
break;
const unsigned int legacy_arrow_fit = static_cast<unsigned int>(ArrowFit(&ds));
if (!archive.WriteInt(legacy_arrow_fit))
break;
if (!archive.WriteUuid(m_detail_measured))
break;
if (!archive.WriteDouble(m_distance_scale))
break;
// content_version 1
const unsigned int legacy_text_fit = static_cast<unsigned int>(TextFit(&ds));
if (!archive.WriteInt(legacy_text_fit))
break;
rc = true;
break;
}
if (!archive.EndWrite3dmChunk())
rc = false;
return rc;
}
bool ON_Dimension::Internal_ReadDimension(
ON_BinaryArchive& archive
)
{
// This is a helper function called by liner and angular annotation classes.
// "this" has already been set to default values before this function is called.
int content_version = -1;
if (false == archive.BeginRead3dmAnonymousChunk(&content_version))
return false;
unsigned int legacy_arrow_fit = 0;
unsigned int legacy_text_fit = 0;
bool rc = false;
for (;;)
{
if (content_version < 0)
break;
if (!ON_Annotation::Internal_ReadAnnotation(archive))
break;
if (!archive.ReadString(m_user_text))
break;
double obsolete_text_rotation = 0.0;
if (!archive.ReadDouble(&obsolete_text_rotation))
break;
if (!archive.ReadBool(&m_use_default_text_point))
break;
if (!archive.ReadPoint(m_user_text_point))
break;
if (!archive.ReadBool(&m_flip_arrow_1))
break;
if (!archive.ReadBool(&m_flip_arrow_2))
break;
if (!archive.ReadInt(&legacy_arrow_fit))
break;
if (!archive.ReadUuid(m_detail_measured))
break;
if (!archive.ReadDouble(&m_distance_scale))
break;
// 24-Sep-2021 Dale Fugier, https://mcneel.myjetbrains.com/youtrack/issue/RH-65605
//if (ON_nil_uuid == m_detail_measured)
// m_distance_scale = 1.0;
if (content_version <= 0)
{
rc = true;
break;
}
// content_version 1
if (!archive.ReadInt(&legacy_text_fit))
break;
rc = true;
break;
}
if (!archive.EndRead3dmChunk())
rc = false;
const unsigned int version_v7_may_8_2019 = ON_VersionNumberConstruct(7, 0, 2019, 5, 8, 0);
if (rc && archive.ArchiveOpenNURBSVersion() < version_v7_may_8_2019 )
{
// may 2019 - "arrow fit" and "text fit" moved from member settings on ON_Dimension
// to settings on ON_DimStyle.
// The file being read is older than the change.
const ON_DimStyle::arrow_fit new_arrow_fit = ON_DimStyle::ArrowFitFromUnsigned((unsigned int)legacy_arrow_fit);
const ON_DimStyle::text_fit new_text_fit = ON_DimStyle::TextFitFromUnsigned((unsigned int)legacy_text_fit);
const ON_DimStyle& ds = archive.ArchiveCurrentDimStyle();
bool bSetArrowFit = (new_arrow_fit != ArrowFit(&ds));
if (bSetArrowFit)
SetArrowFit(&ds, new_arrow_fit);
bool bSetTextFit = (new_text_fit != TextFit(&ds));
if (bSetTextFit)
SetTextFit(&ds, new_text_fit);
}
return rc;
}
bool ON_DimLinear::Write(
ON_BinaryArchive& archive
) const
{
const int content_version = 0;
if (false == archive.BeginWrite3dmAnonymousChunk(content_version))
return false;
bool rc = false;
for (;;)
{
if (!ON_Dimension::Internal_WriteDimension(archive))
break;
if (!archive.WritePoint(m_def_pt_2))
break;
if (!archive.WritePoint(m_dimline_pt))
break;
rc = true;
break;
}
if (!archive.EndWrite3dmChunk())
rc = false;
return rc;
}
bool ON_DimLinear::Read(
ON_BinaryArchive& archive
)
{
*this = ON_DimLinear::Empty;
int content_version = -1;
if (false == archive.BeginRead3dmAnonymousChunk(&content_version))
return false;
bool rc = false;
for (;;)
{
if (content_version < 0)
break;
if (!ON_Dimension::Internal_ReadDimension(archive))
break;
if (!archive.ReadPoint(m_def_pt_2))
break;
if (!archive.ReadPoint(m_dimline_pt))
break;
rc = true;
break;
}
if (!archive.EndRead3dmChunk())
rc = false;
return rc;
}
bool ON_DimLinear::Transform(const ON_Xform& xform)
{
bool rc = xform.IsIdentity();
if (!rc)
{
rc = true;
bool scaling = false;
ON_3dVector v = m_plane.xaxis;
v.Transform(xform);
if (fabs(1.0 - v.Length()) > ON_SQRT_EPSILON)
scaling = true;
else
{
v = m_plane.yaxis;
v.Transform(xform);
if (fabs(1.0 - v.Length()) > ON_SQRT_EPSILON)
scaling = true;
else
{
v = m_plane.zaxis;
v.Transform(xform);
if (fabs(1.0 - v.Length()) > ON_SQRT_EPSILON)
scaling = true;
}
}
if (rc)
{
if (scaling)
{
ON_3dPoint defpt2_0(ON_3dPoint::UnsetPoint);
ON_3dPoint dimlinept_0(ON_3dPoint::UnsetPoint);
ON_3dPoint textpt_0(ON_3dPoint::UnsetPoint);
if (Get3dPoints(nullptr, &defpt2_0, nullptr, nullptr, &dimlinept_0, &textpt_0))
{
ON_2dPoint defpt2(ON_2dPoint::NanPoint), dimlinept(ON_2dPoint::NanPoint), textpt(ON_2dPoint::NanPoint);
rc = m_plane.Transform(xform);
defpt2_0.Transform(xform);
dimlinept_0.Transform(xform);
if (!UseDefaultTextPoint())
textpt_0.Transform(xform);
if (rc && !m_plane.ClosestPointTo(defpt2_0, &defpt2.x, &defpt2.y))
rc = false;
else if (rc && !m_plane.ClosestPointTo(dimlinept_0, &dimlinept.x, &dimlinept.y))
rc = false;
else if (rc && !UseDefaultTextPoint() && !m_plane.ClosestPointTo(textpt_0, &textpt.x, &textpt.y))
rc = false;
if (rc)
{
Set2dDefPoint2(defpt2);
Set2dDimlinePoint(dimlinept);
if (!UseDefaultTextPoint())
Set2dTextPoint(textpt);
}
}
}
else
rc = m_plane.Transform(xform);
}
if (rc)
ON_Geometry::Transform(xform);
}
return rc;
}
bool ON_DimLinear::GetTextXform(
const ON_Viewport* vp,
const ON_DimStyle* dimstyle,
double dimscale,
ON_Xform& text_xform_out
) const
{
return GetTextXform(nullptr, vp, dimstyle, dimscale, text_xform_out);
}
bool ON_DimLinear::GetTextXform(
const ON_Xform* model_xform,
const ON_Viewport* vp,
const ON_DimStyle* dimstyle,
double dimscale,
ON_Xform& text_xform_out
) const
{
ON_3dVector view_x = nullptr == vp ? ON_3dVector::XAxis : vp->CameraX();
ON_3dVector view_y = nullptr == vp ? ON_3dVector::YAxis : vp->CameraY();
ON_3dVector view_z = nullptr == vp ? ON_3dVector::ZAxis : vp->CameraZ();
ON::view_projection projection = vp ? vp->Projection() : ON::view_projection::parallel_view;
bool bDrawForward = dimstyle == nullptr ? false : dimstyle->DrawForward();
return GetTextXform(model_xform, view_x, view_y, view_z, projection, bDrawForward, dimstyle, dimscale, text_xform_out);
}
bool ON_DimLinear::GetTextXform(
const ON_Xform * model_xform,
const ON_3dVector view_x,
const ON_3dVector view_y,
const ON_3dVector view_z,
ON::view_projection projection,
bool bDrawForward,
const ON_DimStyle * dimstyle,
double dimscale,
ON_Xform & text_xform_out
) const
{
bool rc = false;
if (nullptr == dimstyle)
return false;
// This gets the display text that's already on the dimension
const ON_TextContent* text = Text();
if (nullptr == text)
return false;
// See if the text needs remade because of some change in some property that
// would change its appearance
if (DimStyleTextPositionPropertiesHash() != dimstyle->TextPositionPropertiesHash())
{
ON_wString rtfstr = text->RtfText();
ON::AnnotationType annotation_type = this->Type();
bool wrapped = text->TextIsWrapped();
double width = text->FormattingRectangleWidth();
double rot = text->TextRotationRadians();
const_cast<ON_TextContent*>(text)->Create(rtfstr, annotation_type, dimstyle, wrapped, width, rot);
}
double text_width = 0.0;
double text_height = 0.0;
double text_gap = 0.0;
double text_angle = 0.0; // in radians - deviation from horizontal ccw
const ON_DimStyle::TextLocation text_location = dimstyle->DimTextLocation();
const ON::TextOrientation text_orientation = dimstyle->DimTextOrientation();
const ON_DimStyle::ContentAngleStyle text_angle_style = dimstyle->DimTextAngleStyle();
ON_Xform text_to_dimplane(1.0); // Text plane (world xy) to dimension plane rotation
ON_Xform dimplane_to_textpoint(1.0); // Dimension plane to text point translation
ON_Xform text_rotation(1.0); // Text rotation around text plane origin point
// The amount past vertical where text flips to the other orientation
const double fliptol = (projection == ON::view_projection::perspective_view) ? cos(89.0 * ON_DEGREES_TO_RADIANS) : cos(80.001 * ON_DEGREES_TO_RADIANS);
ON_3dPoint text_center = ON_3dPoint::Origin;
// Text starts out approximately centered at origin
ON_3dPoint cp[4];
// 06 Feb 2025 - Jeff: https://mcneel.myjetbrains.com/youtrack/issue/RH-84156
// Don't exit just because the corners don't exist (i.e. no text or zero-length text).
// We still need the transform to get computed.
if (text->Get3dCorners(cp))
{
text_center = (cp[0] + cp[2]) / 2.0;
text_width = (cp[1].x - cp[0].x) * dimscale;
text_height = (cp[3].y - cp[0].y) * dimscale;
text_gap = dimstyle->TextGap();
if (dimstyle->MaskFrameType() != ON_TextMask::MaskFrame::NoFrame)
text_gap += dimstyle->TextMask().MaskBorder(); // RH-71452
text_gap *= dimscale;
}
if (dimstyle->Alternate() && dimstyle->AlternateBelow())
text_height = -2.0 * text_gap;
text_xform_out = ON_Xform::IdentityTransformation;
text_to_dimplane.Rotation(ON_Plane::World_xy, Plane()); // Rotate text from starting text plane to dimension plane
bool draw_forward = dimstyle->DrawForward();
#pragma region ArrowAndTextFitting
// See if arrows and text will all fit inside extension lines
// or what has to be moved outside
bool arrowflipped[2] = { false, false };
ON_DimStyle::arrow_fit arrow_fit = dimstyle->ArrowFit();
if (ON_DimStyle::arrow_fit::ArrowsOutside == arrow_fit)
arrowflipped[0] = arrowflipped[1] = true;
ON_DimStyle::text_fit text_fit = dimstyle->TextFit();
bool text_outside = false;
double dist = Measurement();
// Display scale for detail viewports when page space dimension measures model space
if (ON_nil_uuid != DetailMeasured())
{
double dist_scale = DistanceScale();
if (dist_scale != 1.0 && dist_scale > 0.0)
dist /= dist_scale;
}
// V6_Dimstyle Arrow1 & Arrow2
double asz = dimstyle->ArrowSize() * dimscale;
double total_text_width = (ON_DimStyle::ContentAngleStyle::Horizontal == text_angle_style) ? text_height : text_width;
if (text_fit == ON_DimStyle::text_fit::TextLeft || text_fit == ON_DimStyle::text_fit::TextRight)
{
total_text_width = 0.0;
text_outside = true;
}
//else if (force_text == ON_Dimension::ForceText::Inside)
else if (text_fit == ON_DimStyle::text_fit::TextInside)
{
total_text_width = 0.0;
text_outside = false;
}
else if (0.0 < total_text_width)
total_text_width += text_gap;
static double arrow_width_factor = 1.1;
double total_arrow_width = asz * arrow_width_factor * 2;
if (ON_DimStyle::arrow_fit::ArrowsOutside == arrow_fit)
total_arrow_width = 0.0;
if (total_arrow_width + total_text_width > dist) // arrows + text dont fit
{
// Try to leave text inside and move arrows outside
if (total_text_width > dist) // text doesnt fit
{
// move text outside
text_outside = true;
if (total_arrow_width > dist && ON_DimStyle::arrow_fit::Auto == arrow_fit) // arrows dont fit either
{
arrowflipped[0] = true;
arrowflipped[1] = true;
}
}
//else if (ForceArrow::Auto == force_arrow) // text fits
else if (ON_DimStyle::arrow_fit::Auto == arrow_fit) // text fits
{
// flip arrows
arrowflipped[0] = true;
arrowflipped[1] = true;
}
}
FlipArrow(0, arrowflipped[0]);
FlipArrow(1, arrowflipped[1]);
// This returns the midpoint of the dimension line in 2d coordinates
ON_2dPoint text_pt = TextPoint();
if (text_outside && ON_DimStyle::ContentAngleStyle::Horizontal != text_angle_style && UseDefaultTextPoint())
{
// move textpoint outside right arrow by 1/2 text width + 1-1/2 arrow width
double x = (text_width * 0.5) + (text_gap * 3.0);
if (text_fit == ON_DimStyle::text_fit::TextLeft || text_fit == ON_DimStyle::text_fit::TextHintLeft)
{
if (arrowflipped[0])
x += (asz * arrow_width_factor);
text_pt = ArrowPoint1().x < ArrowPoint2().x ? ArrowPoint1() : ArrowPoint2();
text_pt.x -= x;
}
else // right or auto
{
if (arrowflipped[1])
x += (asz * arrow_width_factor);
text_pt = ArrowPoint1().x < ArrowPoint2().x ? ArrowPoint2() : ArrowPoint1();
text_pt.x += x;
}
}
#pragma endregion ArrowAndTextFitting
ON_3dVector dim_xaxis = Plane().xaxis;
ON_3dVector dim_yaxis = Plane().yaxis;
ON_3dVector dim_zaxis = Plane().zaxis;
if (nullptr != model_xform && !model_xform->IsIdentity())
{
dim_xaxis.Transform(*model_xform);
dim_yaxis.Transform(*model_xform);
dim_zaxis.Transform(*model_xform);
}
ON_3dVector view_xdir = view_x;
ON_3dVector view_ydir = view_y;
ON_3dVector view_zdir = view_z;
// text is in dimension plane, not horizontal to the view
ON_2dVector h_dir = HorizontalDirection();
ON_3dVector text_xdir = dim_xaxis;
ON_3dVector text_ydir = dim_yaxis;
ON_3dVector text_zdir = dim_zaxis;
if (ON::TextOrientation::InPlane == text_orientation)
{
if (ON_DimStyle::ContentAngleStyle::Rotated == text_angle_style)
{
// Rotation angle = 0 means the text is horizontal
text_angle = 0.0; //TextRotation();
}
else if (ON_DimStyle::ContentAngleStyle::Aligned == text_angle_style)
{
text_angle = 0.0;
}
if (ON_DimStyle::ContentAngleStyle::Aligned != text_angle_style)
{
double h_angle = atan2(h_dir.y, h_dir.x);
text_angle += h_angle;
text_xdir.Rotate(h_angle, dim_zaxis);
text_ydir.Rotate(h_angle, dim_zaxis);
}
}
bool flip_x = false;
bool flip_y = false;
CalcTextFlip(
text_xdir, text_ydir, text_zdir,
view_xdir, view_ydir, view_zdir,
model_xform,
fliptol,
flip_x,
flip_y);
if (ON_DimStyle::TextLocation::AboveDimLine == text_location)
{
// Moves the text to AboveLine if that's the alignment mode
double dy = flip_y ? -1.0 : 1.0;
double d = (text_height * 0.5 + text_gap) * dy;
text_pt.y += d;
}
ON_3dPoint text_point_3d = Plane().PointAt(text_pt.x, text_pt.y); // 3d text point
dimplane_to_textpoint = ON_Xform::TranslationTransformation(text_point_3d - Plane().origin); // Move from dimplane origin to text point
text_xform_out = ON_Xform::DiagonalTransformation(dimscale, dimscale, dimscale); // dimscale
if (1.0e-2 < fabs(text_angle)) // There's a rotation angle change of more than 1/100 radian (~1/2 degree)
{
text_rotation.Rotation(text_angle, ON_3dVector::ZAxis, ON_3dPoint::Origin);
if (ON::TextOrientation::InView != text_orientation)
text_xform_out = text_rotation * text_xform_out; // text rotation
}
text_xform_out = text_to_dimplane * text_xform_out; // text plane to dim plane
text_xform_out = dimplane_to_textpoint * text_xform_out; // dimension plane to text point
if (ON::TextOrientation::InView == text_orientation) // Draw dimension horizontal to view
{
if (nullptr != model_xform)
{
ON_Xform xf(*model_xform);
xf.Invert();
view_xdir.Transform(xf);
view_ydir.Transform(xf);
view_zdir.Transform(xf);
}
ON_Xform tp2sxf; // Text point to view plane rotation
tp2sxf.Rotation(text_point_3d, Plane().xaxis, Plane().yaxis, Plane().zaxis, text_point_3d, view_xdir, view_ydir, view_zdir);
text_xform_out = tp2sxf * text_xform_out;
}
else if (draw_forward)
{
ON_Xform mxf; // Mirror xform for backwards text to adjust DrawForward
if (flip_x)
{
mxf.Mirror(text_center, ON_3dVector::XAxis);
text_xform_out = text_xform_out * mxf;
}
if (flip_y)
{
mxf.Mirror(text_center, ON_3dVector::YAxis);
text_xform_out = text_xform_out * mxf;
}
}
return rc;
}
bool ON_DimLinear::GetBBox(double* bmin, double* bmax, bool grow) const
{
const ON_DimStyle* dimstyle = nullptr;
return GetAnnotationBoundingBox(nullptr, dimstyle, 1.0, bmin, bmax, grow?true:false);
}
bool ON_DimLinear::GetAnnotationBoundingBox(
const ON_Viewport* vp,