forked from mcneel/opennurbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opennurbs_annotationbase.cpp
3507 lines (3014 loc) · 109 KB
/
opennurbs_annotationbase.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
/* $NoKeywords: $ */
/*
//
// Copyright (c) 1993-2012 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_textiterator.h"
ON_VIRTUAL_OBJECT_IMPLEMENT(ON_Annotation, ON_Geometry, "B5802E0C-5B16-43C9-BD43-A3E0AC18203B");
ON::AnnotationType ON::AnnotationTypeFromUnsigned(
unsigned int annotation_type_as_unsigned
)
{
switch (annotation_type_as_unsigned)
{
ON_ENUM_FROM_UNSIGNED_CASE(ON::AnnotationType::Unset);
ON_ENUM_FROM_UNSIGNED_CASE(ON::AnnotationType::Aligned);
ON_ENUM_FROM_UNSIGNED_CASE(ON::AnnotationType::Angular);
ON_ENUM_FROM_UNSIGNED_CASE(ON::AnnotationType::Diameter);
ON_ENUM_FROM_UNSIGNED_CASE(ON::AnnotationType::Radius);
ON_ENUM_FROM_UNSIGNED_CASE(ON::AnnotationType::Rotated);
ON_ENUM_FROM_UNSIGNED_CASE(ON::AnnotationType::Ordinate);
ON_ENUM_FROM_UNSIGNED_CASE(ON::AnnotationType::ArcLen);
ON_ENUM_FROM_UNSIGNED_CASE(ON::AnnotationType::CenterMark);
ON_ENUM_FROM_UNSIGNED_CASE(ON::AnnotationType::Text);
ON_ENUM_FROM_UNSIGNED_CASE(ON::AnnotationType::Leader);
ON_ENUM_FROM_UNSIGNED_CASE(ON::AnnotationType::Angular3pt);
}
ON_ERROR("Invalid annotation_type_as_unsigned parameter");
return ON::AnnotationType::Unset;
}
ON_Annotation::ON_Annotation(ON::AnnotationType annotation_type)
: ON_Geometry()
, m_annotation_type(annotation_type)
{}
ON::AnnotationType ON_Annotation::Type() const
{
return m_annotation_type;
}
void ON_Annotation::Internal_CopyFrom(const ON_Annotation& src)
{
m_annotation_type = src.m_annotation_type;
m_dimstyle_id = src.m_dimstyle_id;
m_plane = src.m_plane;
m_horizontal_direction = src.m_horizontal_direction;
m_allow_text_scaling = src.m_allow_text_scaling;
if (nullptr != src.m_text)
m_text = new ON_TextContent(*src.m_text);
if (nullptr != src.m_override_dimstyle)
{
m_override_dimstyle = new ON_DimStyle(*src.m_override_dimstyle);
}
}
void ON_Annotation::Internal_Destroy()
{
ClearText();
Internal_DeleteOverrideDimstyle();
}
ON_Annotation::ON_Annotation(const ON_Annotation& src)
: ON_Geometry(src)
{
Internal_CopyFrom(src);
}
ON_Annotation::~ON_Annotation()
{
Internal_Destroy();
}
ON_Annotation& ON_Annotation::operator=(const ON_Annotation& src)
{
if (&src != this)
{
Internal_Destroy();
Internal_CopyFrom(src);
}
return *this;
}
ON::object_type ON_Annotation::ObjectType() const
{
return ON::annotation_object;
}
bool ON_BinaryArchive::Internal_Write3dmDimStyleOverrides(
const ON_Annotation& annotation,
const ON_DimStyle* dim_style_overrides
)
{
// Chunk containing dimstyle overrides
const int content_version = 1;
if (false == BeginWrite3dmAnonymousChunk(content_version))
return false;
bool rc = false;
for (;;)
{
const ON_UUID annotation_dimstyle_id = annotation.DimensionStyleId();
const bool bHasOverrideDimStyle
= nullptr != dim_style_overrides
&& ON_nil_uuid != annotation_dimstyle_id
&& annotation_dimstyle_id == dim_style_overrides->ParentId()
&& annotation.IsOverrideDimStyleCandidate(dim_style_overrides,true)
&& (ON_ModelComponent::Type::DimStyle == Manifest().ItemFromId(annotation_dimstyle_id).ComponentType()
|| ON_DimStyle::SystemDimstyleFromId(annotation_dimstyle_id).Id() == annotation_dimstyle_id
)
;
if (!WriteBool(bHasOverrideDimStyle))
break;
if (bHasOverrideDimStyle)
{
// use archive.WriteObject() so user data will persist.
if (!WriteObject(dim_style_overrides))
break;
}
rc = true;
break;
}
if (!EndWrite3dmChunk())
rc = false;
return rc;
}
static bool Internal_UpdateOverrideCandidateParentId(
const ON_BinaryArchive& archive,
ON_DimStyle* override_candidate
)
{
for (;;)
{
if (
false == archive.CheckForRemappedIds()
&& 0 == archive.ReferenceModelSerialNumber()
&& 0 == archive.InstanceDefinitionModelSerialNumber()
)
{
return false; // common situation - no change reqired
}
if (nullptr == override_candidate)
break;
const ON_UUID archive_parent_id = override_candidate->ParentId();
if (ON_nil_uuid == archive_parent_id)
break;
// We are reading a worksession reference model or reference style linked instance defintion.
// The ids in the reference file may have had a collision with ids in the active model
// and been changed.
const ON_ManifestMapItem parent_id_map_item = archive.ManifestMap().MapItemFromSourceId(archive_parent_id);
if (ON_ModelComponent::Type::DimStyle != parent_id_map_item.ComponentType())
break;
if (false == parent_id_map_item.SourceIsSet())
break;
if (false == parent_id_map_item.DestinationIsSet())
break;
if (parent_id_map_item.SourceId() != archive_parent_id)
break;
const ON_UUID model_parent_id = parent_id_map_item.DestinationId();
if (model_parent_id == ON_nil_uuid)
break;
if (model_parent_id == archive_parent_id)
return false; // common situation - no change reqired
// We are reading a worksession reference model or reference style linked instance defintion.
// The ids in the reference file may have had a collision with ids in the active model
// and been changed.
override_candidate->SetParentId(model_parent_id);
return true; // changed parent id
}
return false; // unexpected but, no changes
}
bool ON_BinaryArchive::Internal_Read3dmDimStyleOverrides(
class ON_Annotation& annotation,
bool bFromDimStyleTable
)
{
if (bFromDimStyleTable)
{
// V5 or old V6 file where override styles were kept in the dim style table
for (;;)
{
if (false == m_bLegacyOverrideDimStylesInArchive)
break;
const ON_UUID dim_style_id = annotation.DimensionStyleId();
if (ON_nil_uuid == dim_style_id)
break;
const unsigned count = this->m_archive_dim_style_table.UnsignedCount();
if (count <= 0)
break;
for (unsigned int i = 0; i < count; i++)
{
const ON_DimStyle* dim_style = m_archive_dim_style_table[i];
if (nullptr == dim_style)
continue;
if (dim_style_id != dim_style->Id())
continue;
const ON_UUID parent_id = dim_style->ParentId();
if (ON_nil_uuid == parent_id)
continue;
if (dim_style_id == parent_id)
continue;
if (ON_ModelComponent::Type::DimStyle != Manifest().ItemFromId(parent_id).ComponentType())
continue;
const ON_DimStyle* parent_ds = nullptr;
for (unsigned int j = 0; j < count; j++)
{
const ON_DimStyle* archive_ds = m_archive_dim_style_table[j];
if (nullptr == archive_ds)
continue;
if (parent_id != archive_ds->Id())
continue;
parent_ds = archive_ds;
break;
}
if (nullptr == parent_ds)
break;
if (parent_ds->ParentIdIsNotNil())
break;
annotation.SetDimensionStyleId(parent_id);
ON_DimStyle* override_candidate = new ON_DimStyle(*dim_style);
override_candidate->ClearId();
override_candidate->ClearIndex();
override_candidate->ClearName();
Internal_UpdateOverrideCandidateParentId(*this, override_candidate);
annotation.SetOverrideDimensionStyle(override_candidate);
if (nullptr != override_candidate)
{
// If this error occurs frequently, then we will comment out the call to ON_ERROR.
ON_ERROR("Legacy override dimstyle information discarded.");
delete override_candidate;
break;
}
// update override_candidate settings.
annotation.DimensionStyle(*parent_ds);
break;
}
break;
}
return true;
}
// Read information written by ON_BinaryArchive::Internal_Write3dmDimStyleOverrides()
int content_version = 0;
if (false == BeginRead3dmAnonymousChunk(&content_version))
return false;
bool rc = false;
for (;;)
{
bool bHasOverrideDimStyle = false;
if (!ReadBool(&bHasOverrideDimStyle))
break;
if (bHasOverrideDimStyle)
{
// use archive.ReadObject() so user data will persist.
ON_Object* ptr = nullptr;
if (!ReadObject(&ptr))
break;
for (;;)
{
if (ON_nil_uuid == annotation.DimensionStyleId())
break;
ON_DimStyle* override_candidate = ON_DimStyle::Cast(ptr);
if (nullptr == override_candidate)
break;
Internal_UpdateOverrideCandidateParentId(*this, override_candidate);
if (false == annotation.IsOverrideDimStyleCandidate(override_candidate,true))
break;
annotation.SetOverrideDimensionStyle(override_candidate);
ptr = override_candidate; // if annotation is managing override_candidate, then it is nullptr here.
break;
}
if (nullptr != ptr)
delete ptr;
}
rc = true;
break;
}
if (!EndRead3dmChunk())
rc = false;
return rc;
}
bool ON_Annotation::Internal_WriteAnnotation(
ON_BinaryArchive& archive
) const
{
// content_version = 2 - added override dimstyle to ON_Annotation RH-37176
// content_version = 3 - added m_horizontal_direction
// content_version = 4 - added m_allow_text_scaling - Lowell
const int content_version = 4;
if (false == archive.BeginWrite3dmAnonymousChunk(content_version))
return false;
bool rc = false;
for (;;)
{
const ON_TextContent* text_to_write = (nullptr != m_text) ? m_text : &ON_TextContent::Empty;
if (false == text_to_write->Write(archive))
break;
if (!archive.WriteUuid(m_dimstyle_id))
break;
if (!archive.WritePlane(m_plane))
break;
// Added for content_version 1
// Required to write/write linear dimensions and radial dimensions
// because they have multiple possible values of m_annotation_type.
unsigned int u = static_cast<unsigned char>(m_annotation_type);
if (!archive.WriteInt(u))
break;
// Dale Lear 2016 Dec 12 https://mcneel.myjetbrains.com/youtrack/issue/RH-37176
// content_version incremented to 2.
if (!archive.Internal_Write3dmDimStyleOverrides(*this, m_override_dimstyle))
break;
// content_version = 3 ( 13 July, 2017 )
if (!archive.WriteVector(m_horizontal_direction))
break;
// content_version = 4 ( 17 May, 2018 - Lowell)
if (!archive.WriteBool(m_allow_text_scaling))
break;
rc = true;
break;
}
if (!archive.EndWrite3dmChunk())
rc = false;
return rc;
}
bool ON_Annotation::Internal_ReadAnnotation(
ON_BinaryArchive& archive
)
{
Internal_Destroy();
m_dimstyle_id = ON_nil_uuid;
m_plane = ON_Plane::World_xy;
// do not change the value of m_annotation_type
int content_version = -1;
if (false == archive.BeginRead3dmAnonymousChunk(&content_version))
return false;
bool rc = false;
for (;;)
{
if (content_version < 0)
break;
ON_TextContent* text = new ON_TextContent;
if (nullptr == text)
break;
if (false == text->Read(archive))
{
delete text;
break;
}
m_text = text;
if (!archive.Read3dmReferencedComponentId(ON_ModelComponent::Type::DimStyle, &m_dimstyle_id))
break;
if (!archive.ReadPlane(m_plane))
break;
if (content_version <= 0)
{
rc = true;
break;
}
unsigned int u = 0;
if (!archive.ReadInt(&u))
break;
const ON::AnnotationType annotation_type = ON::AnnotationTypeFromUnsigned(u);
if (annotation_type != m_annotation_type)
{
const ON::AnnotationType annotation_type_pairs[3][2]
{
// ON_DimLinear linear dimensions can have type = Aligned or Rotated
{ON::AnnotationType::Aligned,ON::AnnotationType::Rotated},
// ON_DimRadial radial dimensions can have type = Diameter or Radius
{ ON::AnnotationType::Diameter,ON::AnnotationType::Radius },
// ON_DimAngular radial dimensions can have type = Angular or Angular3pt
{ ON::AnnotationType::Angular,ON::AnnotationType::Angular3pt },
};
for (int pair_dex = 0; pair_dex < 3 && annotation_type != m_annotation_type; pair_dex++)
{
for (int k = 0; k < 2; k++)
{
if (
annotation_type_pairs[pair_dex][k] == annotation_type
&& annotation_type_pairs[pair_dex][1-k] == m_annotation_type
)
{
m_annotation_type = annotation_type;
break;
}
}
}
if (annotation_type != m_annotation_type)
{
ON_ERROR("Invalid annotation type.");
}
}
// Dale Lear 2016 Dec 12 https://mcneel.myjetbrains.com/youtrack/issue/RH-37176
// content_version incremented to 2.
// optional override dimension style move from dimstyle table to annotaton object
const bool bFromDimStyleTable = content_version <= 1;
if (!archive.Internal_Read3dmDimStyleOverrides(*this, bFromDimStyleTable))
break;
if (content_version <= 2)
{
rc = true;
break;
}
// content_version = 3 ( 13 July, 2017 )
if (!archive.ReadVector(m_horizontal_direction))
break;
if (content_version <= 3)
{
rc = true;
break;
}
// content_version = 4 ( 17 May, 2018 - Lowell)
if (!archive.ReadBool(&m_allow_text_scaling))
break;
rc = true;
break;
}
if (!archive.EndRead3dmChunk())
rc = false;
return rc;
}
bool ON_Annotation::IsValid(ON_TextLog* text_log) const
{
return (
m_text->IsValid()
&& m_plane.IsValid()
);
}
bool ON_Annotation::GetTextXform(
const ON_Xform* model_xform,
const ON_Viewport* vp,
const ON_DimStyle* dimstyle,
double dimscale,
ON_Xform& text_xform_out
) const
{
const ON_Text* pText = ON_Text::Cast(this);
if (nullptr != pText)
return pText->GetTextXform(model_xform, vp, dimstyle, dimscale, text_xform_out);
const ON_Leader* pLeader = ON_Leader::Cast(this);
if (nullptr != pLeader)
return pLeader->GetTextXform(model_xform, vp, dimstyle, dimscale, text_xform_out);
const ON_DimLinear* pDimLinear = ON_DimLinear::Cast(this);
if (nullptr != pDimLinear)
return pDimLinear->GetTextXform(model_xform, vp, dimstyle, dimscale, text_xform_out);
const ON_DimAngular* pDimAngular = ON_DimAngular::Cast(this);
if (nullptr != pDimAngular)
return pDimAngular->GetTextXform(model_xform, vp, dimstyle, dimscale, text_xform_out);
const ON_DimRadial* pDimRadial = ON_DimRadial::Cast(this);
if (nullptr != pDimRadial)
return pDimRadial->GetTextXform(model_xform, vp, dimstyle, dimscale, text_xform_out);
const ON_DimOrdinate* pDimOrdinate = ON_DimOrdinate::Cast(this);
if (nullptr != pDimOrdinate)
return pDimOrdinate->GetTextXform(model_xform, vp, dimstyle, dimscale, text_xform_out);
const ON_Centermark* pCentermark = ON_Centermark::Cast(this);
if (nullptr != pCentermark)
return pCentermark->GetTextXform(vp, dimstyle, dimscale, text_xform_out);
ON_ERROR("Annotation type not handled");
return false;
}
void ON_Annotation::SetPlane(const ON_Plane& plane)
{
m_plane = plane;
}
const ON_Plane& ON_Annotation::Plane() const
{
return m_plane;
}
void ON_Annotation::SetHorizontalDirection(ON_2dVector horiz)
{
if (horiz.Unitize())
m_horizontal_direction = horiz;
}
const ON_2dVector ON_Annotation::HorizontalDirection() const
{
return m_horizontal_direction;
}
ON_3dVector ON_Annotation::GetDefaultHorizontal(const ON_Plane& plane)
{
double dx = plane.zaxis * ON_3dVector::XAxis;
double dy = plane.zaxis * ON_3dVector::YAxis;
double dz = plane.zaxis * ON_3dVector::ZAxis;
ON_3dVector h3d = ON_3dVector::XAxis;
if (fabs(dz) > fabs(dx) && fabs(dz) > fabs(dy))
h3d = dz > 0.0 ? ON_3dVector::XAxis : -ON_3dVector::XAxis;
else if (fabs(dx) > fabs(dy) && fabs(dx) > fabs(dz))
h3d = dx > 0.0 ? ON_3dVector::YAxis : -ON_3dVector::YAxis;
else if (fabs(dy) > fabs(dx) && fabs(dy) > fabs(dz))
h3d = dy > 0.0 ? ON_3dVector::XAxis : -ON_3dVector::XAxis;
ON_2dVector h2d;
ON_3dPoint p = plane.origin + h3d;
if (plane.ClosestPointTo(p, &h2d.x, &h2d.y) && h2d.Unitize())
{
p = plane.PointAt(h2d.x, h2d.y);
h3d = p - plane.origin;
if (h3d.Unitize())
return h3d;
}
return ON_3dVector::XAxis;
}
void ON_Annotation::CalcTextFlip(
const ON_3dVector& text_xdir, const ON_3dVector& text_ydir, const ON_3dVector& text_zdir,
const ON_3dVector& view_xdir, const ON_3dVector& view_ydir, const ON_3dVector& view_zdir,
const ON_Xform* model_xform,
const double fliptol,
bool& flip_x,
bool& flip_y)
{
double XoX = text_xdir * view_xdir;
double XoY = text_xdir * view_ydir;
double YoX = text_ydir * view_xdir;
double YoY = text_ydir * view_ydir;
bool from_the_back = (view_zdir * text_zdir < 0.0);
if (nullptr != model_xform && model_xform->Determinant() < 0.0)
from_the_back = !from_the_back;
double upsign = 1.0;
// This part shifts text to the correct side of the dimension line
if (fabs(XoX) > fabs(XoY)) // more horizontal
{
if (YoY > 0.0)
upsign = 1.0;
else
upsign = -1.0;
}
else // more vertical
{
if (from_the_back)
{
if (YoX < 0.0)
{
if (XoX < fliptol)
upsign = 1.0;
else
upsign = -1.0;
}
else
{
if (XoX > -fliptol)
upsign = -1.0;
else
upsign = 1.0;
}
}
else
{
if (YoX > 0.0)
{
if (XoX > fliptol)
upsign = 1.0;
else
upsign = -1.0;
}
else
{
if (XoX < -fliptol)
upsign = -1.0;
else
upsign = 1.0;
}
}
}
flip_x = false;
flip_y = false;
if (from_the_back)
upsign = -upsign;
flip_x = upsign < 0.0;
if (from_the_back)
flip_y = !flip_x;
else
flip_y = flip_x;
}
const ON_wString ON_Annotation::PlainText() const
{
if (nullptr == m_text)
return ON_wString::EmptyString;
const ON_TextRunArray* runs = m_text->TextRuns(true);
if (nullptr != runs && 0 == runs->Count())
BoundingBox(); // Side effect of building text runs
return m_text->PlainText();
}
const ON_wString ON_Annotation::PlainTextWithFields() const
{
if (nullptr == m_text)
return ON_wString::EmptyString;
const ON_TextRunArray* runs = m_text->TextRuns(true);
if (nullptr != runs && 0 == runs->Count())
BoundingBox();
return m_text->PlainTextWithFields();
}
const ON_wString ON_Annotation::PlainTextWithFields(ON_SimpleArray<ON_3dex>* runmap) const
{
if (nullptr == m_text)
return ON_wString::EmptyString;
const ON_TextRunArray* runs = m_text->TextRuns(true);
if (nullptr != runs && 0 == runs->Count())
BoundingBox();
return m_text->PlainTextWithFields(runmap);
}
const ON_wString ON_Annotation::RichText() const
{
return (nullptr == m_text)
? ON_wString::EmptyString
: m_text->RichText();
}
ON_UUID ON_Annotation::DimensionStyleId() const
{
return m_dimstyle_id;
}
void ON_Annotation::SetDimensionStyleId(ON_UUID dimstyle_id)
{
const bool bKeepOverrides = false;
SetDimensionStyleIdForExperts(dimstyle_id, bKeepOverrides);
}
void ON_Annotation::SetDimensionStyleId(
const class ON_DimStyle& dim_style
)
{
const ON_UUID dim_style_id
= dim_style.ParentIdIsNil()
? dim_style.Id()
: dim_style.ParentId();
ON_DimStyle* override_dim_style
= (ON_nil_uuid != dim_style_id
&& dim_style_id != dim_style.Id()
&& ON_Annotation::Internal_IsOverrideDimStyleCandidate(&dim_style, dim_style_id, true, false)
)
? new ON_DimStyle(dim_style)
: nullptr;
// If dim_style parameter was the override on this class,
// the next line will delete it.
SetDimensionStyleId(dim_style_id);
SetOverrideDimensionStyle(override_dim_style);
}
void ON_Annotation::SetDimensionStyleIdForExperts(
ON_UUID dimstyle_id,
bool bKeepOverrides
)
{
if (bKeepOverrides)
{
if (ON_nil_uuid == dimstyle_id || nullptr == m_override_dimstyle)
bKeepOverrides = false;
else if (IsOverrideDimStyleCandidate(m_override_dimstyle,true))
bKeepOverrides = m_override_dimstyle->SetParentId(dimstyle_id);
}
if (false == bKeepOverrides)
{
// This resets all overrides
Internal_DeleteOverrideDimstyle();
}
m_dimstyle_id = dimstyle_id;
}
const ON_DimStyle& ON_Annotation::DimensionStyle(const ON_DimStyle& parent_dimstyle) const
{
return DimensionStyle(parent_dimstyle, false);
}
const ON_DimStyle& ON_Annotation::DimensionStyle(
const ON_DimStyle& parent_dimstyle,
bool bForceOverrideUpdate
) const
{
if (nullptr == m_override_dimstyle )
return parent_dimstyle;
if (false == m_override_dimstyle->HasOverrides())
{
Internal_DeleteOverrideDimstyle();
return parent_dimstyle;
}
if (
bForceOverrideUpdate
|| m_override_dimstyle->ParentId() != parent_dimstyle.Id()
|| parent_dimstyle.ContentVersionNumber() != m_parent_dimstyle_content_version_number
|| m_override_dimstyle->ParentContentHash() != parent_dimstyle.ContentHash()
)
{
// update m_override_dimstyle if parent content has changed.
m_override_dimstyle->OverrideFields(*m_override_dimstyle, parent_dimstyle);
if (false == m_override_dimstyle->HasOverrides())
{
// Updating overrides unsets all overrides that match parent values.
// When this happens, the overrides are deleted here.
Internal_DeleteOverrideDimstyle();
return parent_dimstyle;
}
m_parent_dimstyle_content_version_number = parent_dimstyle.ContentVersionNumber();
}
return *m_override_dimstyle;
}
bool ON_Annotation::IsOverrideStylePointer(
const ON_DimStyle* ptr
) const
{
return (nullptr != ptr && ptr == m_override_dimstyle);
}
bool ON_Annotation::AllowTextScaling() const
{
// These functions are being added to continue the V5 behavior of
// per-object text scaling. There is no user interface
// in V6 or V7 that shows this setting or that allows a user
// to change this setting.
// AllowTextScaling() = false means the effective dimstyle value
// of DimScale() (model space scale factor) is ignored (treated as if it were 1).
return m_allow_text_scaling;
}
void ON_Annotation::SetAllowTextScaling(bool scale)
{
// These functions are being added to continue the V5 behavior of
// per-object text scaling. There is no user interface
// in V6 or V7 that shows this setting or that allows a user
// to change this setting.
// AllowTextScaling() = false means the effective dimstyle value
// of DimScale() (model space scale factor) is ignored (treated as if it were 1).
if (scale != m_allow_text_scaling)
{
m_allow_text_scaling = scale ? true : false;
ClearBoundingBox();
}
}
bool ON_Annotation::IsOverrideDimStyleCandidate(
const ON_DimStyle* override_style_candidate,
bool bRequireSetOverrides
) const
{
const bool bIssueErrorsAndWarnings = false;
return Internal_IsOverrideDimStyleCandidate(override_style_candidate, m_dimstyle_id, bRequireSetOverrides, bIssueErrorsAndWarnings);
}
bool ON_DimStyle::IsOverrideDimStyleCandidate(
ON_UUID parent_id,
bool bRequireSetOverrides,
ON_wString* error_description
) const
{
if (bRequireSetOverrides)
{
if (false == this->HasOverrides())
{
if (error_description)
{
*error_description = "this->HasOverrides() is false.";
}
return false;
}
}
if (this->IsDeleted())
{
if (error_description)
{
*error_description = "override style parameter is marked as deleted.";
}
return false;
}
const ON_UUID candidate_parent_id = this->ParentId();
if (ON_nil_uuid == candidate_parent_id)
{
if (this->ParentIdIsLocked())
{
if (error_description)
{
*error_description = "override style parent id is nil and locked.";
}
return false;
}
}
else if (candidate_parent_id != parent_id)
{
if (error_description)
{
*error_description = "override style parent id is incorrectly set.";
}
return false;
}
if ( this->IdIsNotNil() )
{
if (error_description)
{
*error_description = "override style id is not nil.";
}
return false;
}
if ( this->NameIsNotEmpty() )
{
if (error_description)
{
*error_description = "override style name is not empty.";
}
return false;
}
if ( ON_ModelComponent::Unset.Index() != this->Index() )
{
if (error_description)
{
*error_description = "override style index is not unset index.";
}
return false;
}
return true;
}
bool ON_Annotation::Internal_IsOverrideDimStyleCandidate(
const ON_DimStyle* override_style_candidate,
ON_UUID parent_id,
bool bRequireSetOverrides,
bool bIssueErrorsAndWarnings
)
{
if (nullptr == override_style_candidate)
{
if (bIssueErrorsAndWarnings)
{
ON_ERROR("override_style_candidate is nullptr.");
}
return false;
}
ON_wString error_description;
bool rc = override_style_candidate->IsOverrideDimStyleCandidate(
parent_id,
bRequireSetOverrides,
(bIssueErrorsAndWarnings ? &error_description : nullptr)
);
if (false == rc && bIssueErrorsAndWarnings)
{
error_description.TrimLeftAndRight();
ON_String utf8_error_description = error_description;
utf8_error_description.TrimLeftAndRight();
if (utf8_error_description.IsEmpty())
{
ON_ERROR("override_style_candidate is not a valid override candidate.");
}
else
{
ON_ERROR(utf8_error_description);
}
}
return true;
}
void ON_Annotation::ClearOverrideDimensionStyle()
{
Internal_DeleteOverrideDimstyle();
}
/*
Description:
If this->IsOverrideDimStyleCandidate(override_style_candidate,bRequireSetOverrides)
is true, then a managed copy of override_style_candidate is set as an override.
Returns:
True if an override is set.
*/
bool ON_Annotation::SetOverrideDimensionStyle(
const ON_DimStyle* override_style_candidate,
bool bRequireSetOverrides
)
{
bool rc;
if (nullptr == override_style_candidate)
{
ClearOverrideDimensionStyle();
rc = true;
}
else if ( IsOverrideDimStyleCandidate(override_style_candidate, bRequireSetOverrides) )