forked from mcneel/opennurbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopennurbs_instance.cpp
2953 lines (2574 loc) · 84.5 KB
/
opennurbs_instance.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
ON_OBJECT_IMPLEMENT( ON_InstanceDefinition, ON_ModelComponent, "26F8BFF6-2618-417f-A158-153D64A94989" );
class /*NEVER EXPORT THIS CLASS DEFINITION*/ ON_ReferencedComponentSettingsImpl
{
public:
ON_ReferencedComponentSettingsImpl() = default;
~ON_ReferencedComponentSettingsImpl()
{
InternalDestroyHelper();
}
ON_ReferencedComponentSettingsImpl(const ON_ReferencedComponentSettingsImpl& src)
{
InternalCopyHelper(src);
}
ON_ReferencedComponentSettingsImpl& operator=(const ON_ReferencedComponentSettingsImpl& src)
{
if (&src != this)
{
InternalDestroyHelper();
InternalCopyHelper(src);
}
return *this;
}
bool ReadImpl(
ON_BinaryArchive& binary_archive
);
bool WriteImpl(
ON_BinaryArchive& binary_archive
) const;
bool IsNotEmptyImpl() const
{
return HasLayerInformationImpl();
}
bool HasLayerInformationImpl() const
{
return (HasLayerTableInformationImpl() || HasParentLayerInformationImpl());
}
bool HasLayerTableInformationImpl() const
{
return (m_layer_referenced_file_copy.Count() > 0);
}
bool HasParentLayerInformationImpl() const
{
return (m_bHasReferenceLayerTableParentLayer || nullptr != m_layer_table_parent_layer);
}
/*
See ON_ReferencedComponentSettings::AfterReferenceLayerTableRead comment.
*/
void AfterReferenceLayerTableReadImpl(
const class ON_ComponentManifest& source_archive_manifest,
const class ON_ComponentManifest& model_manifest,
const class ON_ManifestMap& archive_to_model_map,
ON_Layer* linked_definition_parent_layer,
unsigned int layer_count,
ON_Layer** layers
);
/*
See ON_ReferencedComponentSettings::AfterLayerTableAddedToModel comment.
*/
void AfterLayerTableAddedToModelImpl(
const class ON_ComponentManifest& source_archive_manifest,
const class ON_ComponentManifest& model_manifest,
const class ON_ManifestMap& archive_to_model_map
);
/*
See ON_ReferencedComponentSettings::BeforeLinkedDefinitionWrite comment.
*/
void BeforeLinkedDefinitionWriteImpl(
const class ON_ComponentManifest& model_manifest,
const class ON_ComponentManifest& destination_archive_manifest,
const class ON_ManifestMap& model_to_archive_map,
const ON_Layer* linked_definition_parent_layer,
void* context,
const ON_Layer*(*ModelLayerFromIdFunc)(void* context, const ON_UUID&)
);
private:
/*
Parameters:
previous_referenced_file_layer - [in]
A copy of the layer the last time the reference file was read
Differences between reference_file_layer and referenced_file_layer_copy
indicate that setting changed in the reference file since the last time
the model refreshed the linked instance definition or worksession reference.
reference_file_layer - [in]
Layer read from the referenced file
previous_model_layer - [in]
A copy of the corresponding model layer the last time the model (not the referenced file) was saved.
Differences between model_layer_copy and referenced_file_layer_copy
indicate that setting was modified in the model the last time the
model refreshed or saved the linked instance definition or worksession
model_layer - [out]
Layer used in the model.
To update a layer "in-place", pass the same layer as reference_file_layer and model_layer
Remarks:
NOTE WELL:
The "model" contains the linked instance definition or worksession.
The reference file is not what is read to create the model.
The reference file is read to import the contents of the linked instance definition.
*/
static bool Internal_UpdateLayer(
const ON_Layer& previous_referenced_file_layer,
const ON_Layer& reference_file_layer,
const ON_Layer& previous_model_layer,
ON_Layer& model_layer
);
/*
Parameters:
layer - [in/out]
input layer = values read from referenced file
output layer = values to use in model
(name, index and id are not changed)
*/
bool Internal_UpdateLayer(
ON_Layer& layer
) const;
/*
Returns:
bool setting state to use in current model
*/
static bool Internal_UpdateBool(
bool bPreviousReferenceFileState,
bool bCurrentReferenceFileState,
bool bPreviousModelState
);
/*
Returns:
color to use in current model
*/
static ON_Color Internal_UpdateColor(
ON_Color previous_reference_file_color,
ON_Color current_reference_file_color,
ON_Color previous_model_color
);
/*
Returns:
double value to use in current model
*/
static double Internal_UpdateDouble(
double previous_reference_file_value,
double current_reference_file_value,
double previous_model_value
);
// These m_layer_referenced_file_copy[] and m_layer_model_copy[]
// are parallel arrays when read and written. Shortly after
// reading/writing, m_layer_model_copy[] is destroyed because
// it has no use except during reading/writing.
// m_referenced_file_copy_layers[] contains copies of the last
// layer values read from the referenced file when a linked instance
// definition is inserted into the model.
private:
ON_SimpleArray<ON_Layer*> m_layer_referenced_file_copy;
// m_model_settings_layers[] contains copies of the most
// recent settings used in the model.
//
// The m_model_settings_layers[] identification values
// (name, index, and id) are meaningless when read from the file.
// They are runtime values that often change each time a linked
// instance definition is loaded.
//
// When some other attribute, (visibility, color, plot weight, ...)
// is different between m_ref_file_copy_layers[] and m_model_settings_layers[],
// it indicates the model setting has been changed from the file setting.
private:
ON_SimpleArray<ON_Layer*> m_layer_model_copy;
// Settings for the layer that is the
// parent of the layers in the linked
// file's layer table. This layer is
// not in the linked file and is not
// saved in the layer table of active model containing
// the idef. If null, it is created.
private:
bool m_bHasReferenceLayerTableParentLayer = false;
ON_Layer* m_layer_table_parent_layer = nullptr;
private:
// When a component (layer, material, ...) from a linked file is inserted in the
// active model and a component id collision occurs, the active model id of the
// component has to be changed. This list keeps track of the changes so we can
// determine which runtime component corresponds to a component in the linked file.
// The first id in the pair is component id in the linked reference file.
// The second id in the pair is the component id in the runtime model.
//
// This information is created in AfterLayerTableAddedToModelImpl()
// and used in BeforeLinkedDefinitionWriteImpl().
// It has no other use and is not saved in files because the
// 2nd id is a runtime value that can change every time a file is read.
ON_UuidPairList m_runtime_layer_id_map;
private:
static void InternalDestroyLayerArray(
ON_SimpleArray<ON_Layer*>& a
)
{
int count = a.Count();
for (int i = 0; i < count; i++)
{
ON_Layer* layer = a[i];
if (nullptr == layer)
continue;
a[i] = nullptr;
delete layer;
}
a.SetCount(0);
a.Destroy();
}
private:
void InternalCopyHelper(const ON_ReferencedComponentSettingsImpl& src)
{
int count = src.m_layer_referenced_file_copy.Count();
if (count != src.m_layer_model_copy.Count())
count = 0;
m_layer_referenced_file_copy.Reserve(count);
m_layer_model_copy.Reserve(count);
for ( int i = 0; i < count; i++ )
{
const ON_Layer* src_ref_layer = src.m_layer_referenced_file_copy[i];
if (nullptr == src_ref_layer)
continue;
const ON_Layer* src_model_layer = src.m_layer_model_copy[i];
if (nullptr == src_model_layer)
continue;
m_layer_referenced_file_copy.Append(new ON_Layer(*src_ref_layer));
m_layer_model_copy.Append(new ON_Layer(*src_model_layer));
}
m_bHasReferenceLayerTableParentLayer = src.m_bHasReferenceLayerTableParentLayer;
if ( nullptr != src.m_layer_table_parent_layer )
{
m_layer_table_parent_layer = new ON_Layer( *src.m_layer_table_parent_layer );
m_bHasReferenceLayerTableParentLayer = true;
}
m_runtime_layer_id_map = src.m_runtime_layer_id_map;
m_runtime_layer_id_map.ImproveSearchSpeed();
}
private:
void InternalDestroyListsHelper()
{
ON_ReferencedComponentSettingsImpl::InternalDestroyLayerArray(m_layer_referenced_file_copy);
ON_ReferencedComponentSettingsImpl::InternalDestroyLayerArray(m_layer_model_copy);
m_runtime_layer_id_map.Empty();
}
private:
void InternalDestroyHelper()
{
m_bHasReferenceLayerTableParentLayer = false;
if ( 0 != m_layer_table_parent_layer )
{
delete m_layer_table_parent_layer;
m_layer_table_parent_layer = nullptr;
}
InternalDestroyListsHelper();
}
};
ON_ReferencedComponentSettings::~ON_ReferencedComponentSettings()
{
if (nullptr != m_impl)
delete m_impl;
}
ON_ReferencedComponentSettings::ON_ReferencedComponentSettings(const ON_ReferencedComponentSettings& src)
{
if (nullptr != src.m_impl)
m_impl = new ON_ReferencedComponentSettingsImpl(*src.m_impl);
}
ON_ReferencedComponentSettings& ON_ReferencedComponentSettings::operator=(const ON_ReferencedComponentSettings& src)
{
if (m_impl != src.m_impl)
{
if (nullptr != m_impl)
delete m_impl;
if (nullptr != src.m_impl)
m_impl = new ON_ReferencedComponentSettingsImpl(*src.m_impl);
}
return *this;
}
bool ON_ReferencedComponentSettings::Read(
ON_BinaryArchive& archive
)
{
if (nullptr != m_impl)
{
delete m_impl;
m_impl = nullptr;
}
int major_version = 0;
int minor_version = 0;
if (!archive.BeginRead3dmChunk(TCODE_ANONYMOUS_CHUNK, &major_version, &minor_version))
return false;
bool bSuppressPartiallyReadChunkWarning = false;
bool rc = false;
for (;;)
{
if (1 != major_version)
break;
bool bHaveImpl = false;
if (!archive.ReadBool(&bHaveImpl))
break;
if (bHaveImpl)
{
ON_ReferencedComponentSettingsImpl* impl = new ON_ReferencedComponentSettingsImpl();
if (!impl->ReadImpl(archive))
{
delete impl;
break;
}
m_impl = impl;
}
// end of 1.0 chunk contents
// increment this when Write's minor_version increases
// Settint bSuppressPartiallyReadChunkWarning suppresses warnings when old code reads new files.
const int max_supported_minor_version = 0;
if (minor_version > max_supported_minor_version)
bSuppressPartiallyReadChunkWarning = true;
rc = true;
break;
}
if (!archive.EndRead3dmChunk(bSuppressPartiallyReadChunkWarning))
rc = false;
return rc;
}
bool ON_ReferencedComponentSettings::Write(
ON_BinaryArchive& archive
) const
{
int major_version = 1;
int minor_version = 0;
if (!archive.BeginWrite3dmChunk(TCODE_ANONYMOUS_CHUNK, major_version, minor_version))
return false;
bool rc = false;
for (;;)
{
const bool bHaveImpl
= archive.Archive3dmVersion() >= 60
&& nullptr != m_impl
&& m_impl->IsNotEmptyImpl();
if (!archive.WriteBool(bHaveImpl))
break;
if (bHaveImpl && !m_impl->WriteImpl(archive))
break;
rc = true;
break;
}
if (!archive.EndWrite3dmChunk())
rc = false;
return rc;
}
bool ON_ReferencedComponentSettings::IsEmpty() const
{
return (false == IsNotEmpty());
}
bool ON_ReferencedComponentSettings::IsNotEmpty() const
{
return (nullptr != m_impl && m_impl->IsNotEmptyImpl());
}
bool ON_ReferencedComponentSettings::HasLayerInformation() const
{
return (nullptr != m_impl && m_impl->HasLayerInformationImpl());
}
bool ON_ReferencedComponentSettings::HasLayerTableInformation() const
{
return (nullptr != m_impl && m_impl->HasLayerTableInformationImpl());
}
bool ON_ReferencedComponentSettings::HasParentLayerInformation() const
{
return (nullptr != m_impl && m_impl->HasParentLayerInformationImpl());
}
class ON_ReferencedComponentSettingsImpl* ON_ReferencedComponentSettings::Impl(
bool bCreateIfNull
)
{
if (nullptr == m_impl && bCreateIfNull)
m_impl = new ON_ReferencedComponentSettingsImpl();
return m_impl;
}
void ON_ReferencedComponentSettings::AfterReferenceLayerTableRead(
const class ON_ComponentManifest& source_archive_manifest,
const class ON_ComponentManifest& model_manifest,
const class ON_ManifestMap& archive_to_model_map,
ON_Layer* linked_definition_parent_layer,
unsigned int layer_count,
ON_Layer** layers
)
{
if (layer_count <= 0 || nullptr == layers)
return;
Impl(true)->AfterReferenceLayerTableReadImpl(source_archive_manifest,model_manifest,archive_to_model_map,linked_definition_parent_layer,layer_count, layers);
}
void ON_ReferencedComponentSettings::AfterLayerTableAddedToModel(
const class ON_ComponentManifest& source_archive_manifest,
const class ON_ComponentManifest& model_manifest,
const class ON_ManifestMap& archive_to_model_map
)
{
if (m_impl)
m_impl->AfterLayerTableAddedToModelImpl(source_archive_manifest, model_manifest, archive_to_model_map);
}
void ON_ReferencedComponentSettings::BeforeLinkedDefinitionWrite(
const class ON_ComponentManifest& model_manifest,
const class ON_ComponentManifest& destination_archive_manifest,
const class ON_ManifestMap& model_to_archive_map,
const ON_Layer* linked_definition_parent_layer,
void* context,
const ON_Layer*(*ModelLayerFromIdFunc)(void* context, const ON_UUID&)
)
{
if (m_impl)
m_impl->BeforeLinkedDefinitionWriteImpl(model_manifest,destination_archive_manifest,model_to_archive_map,linked_definition_parent_layer,context,ModelLayerFromIdFunc);
}
void ON_ReferencedComponentSettingsImpl::AfterReferenceLayerTableReadImpl(
const class ON_ComponentManifest& source_archive_manifest,
const class ON_ComponentManifest& model_manifest,
const class ON_ManifestMap& archive_to_model_map,
ON_Layer* linked_definition_parent_layer,
unsigned int layer_count,
ON_Layer** layers
)
{
ON_SimpleArray<ON_Layer*> updated_reference_copy(layer_count);
if (m_layer_referenced_file_copy.Count() != m_layer_model_copy.Count())
InternalDestroyListsHelper();
//const int old_count = m_layer_referenced_file_copy.Count();
for (unsigned int i = 0; i < layer_count; i++)
{
ON_Layer* layer = layers[i];
if (nullptr == layer)
continue;
updated_reference_copy.Append(new ON_Layer(*layer));
Internal_UpdateLayer(*layer);
}
InternalDestroyListsHelper();
m_layer_referenced_file_copy = updated_reference_copy;
updated_reference_copy.Destroy();
if (nullptr != linked_definition_parent_layer)
{
if (nullptr != m_layer_table_parent_layer)
{
ON_ReferencedComponentSettingsImpl::Internal_UpdateLayer(*m_layer_table_parent_layer, *m_layer_table_parent_layer, *m_layer_table_parent_layer, *linked_definition_parent_layer);
}
}
}
void ON_ReferencedComponentSettingsImpl::AfterLayerTableAddedToModelImpl(
const class ON_ComponentManifest& source_archive_manifest,
const class ON_ComponentManifest& model_manifest,
const class ON_ManifestMap& archive_to_model_map
)
{
InternalDestroyLayerArray(m_layer_model_copy);
m_runtime_layer_id_map.Empty();
int count = 0;
for (int i = 0; i < m_layer_referenced_file_copy.Count(); i++)
{
ON_Layer* layer = m_layer_referenced_file_copy[i];
if (nullptr == layer)
continue;
m_layer_referenced_file_copy[i] = nullptr;
ON_ManifestMapItem map_item = archive_to_model_map.MapItemFromSourceId(layer->Id());
if (ON_ModelComponent::Type::Layer != map_item.ComponentType() || ON_nil_uuid == map_item.DestinationId())
{
delete layer;
continue;
}
if (false == m_runtime_layer_id_map.AddPair(map_item.SourceId(),map_item.DestinationId()))
{
delete layer;
continue;
}
// If this layer came from reading a non-.3dm file,
// it might have per viewport settings that apply to
// the viewports in the reference file.
// These viewports are not in the current model and
// any reference file per viewport settings should be deleted.
layer->DeletePerViewportSettings(ON_nil_uuid);
m_layer_referenced_file_copy[count++] = layer;
}
m_layer_referenced_file_copy.SetCount(count);
m_runtime_layer_id_map.ImproveSearchSpeed();
}
void ON_ReferencedComponentSettingsImpl::BeforeLinkedDefinitionWriteImpl(
const class ON_ComponentManifest& model_manifest,
const class ON_ComponentManifest& destination_archive_manifest,
const class ON_ManifestMap& model_to_archive_map,
const ON_Layer* linked_definition_parent_layer,
void* context,
const ON_Layer*(*ModelLayerFromIdFunc)(void* context, const ON_UUID&)
)
{
InternalDestroyLayerArray(m_layer_model_copy);
const int count0 = m_layer_referenced_file_copy.Count();
m_layer_model_copy.Reserve(count0);
int count1 = 0;
for (int i = 0; i < count0; i++)
{
ON_Layer* layer_referenced_file_copy = m_layer_referenced_file_copy[i];
if (nullptr == layer_referenced_file_copy)
continue;
m_layer_referenced_file_copy[i] = nullptr;
ON_UUID model_layer_id = ON_nil_uuid;
if (!m_runtime_layer_id_map.FindId1(layer_referenced_file_copy->Id(), &model_layer_id))
{
delete layer_referenced_file_copy;
continue;
}
if (ON_nil_uuid == model_layer_id)
{
delete layer_referenced_file_copy;
continue;
}
const ON_Layer* model_layer = ModelLayerFromIdFunc(context, model_layer_id);
if (nullptr == model_layer)
{
delete layer_referenced_file_copy;
continue;
}
if (model_layer_id != model_layer->Id())
{
delete layer_referenced_file_copy;
continue;
}
// We found the corresponding runtime model layer
m_layer_referenced_file_copy[count1++] = layer_referenced_file_copy;
ON_Layer* ref_model_layer_copy = new ON_Layer(*model_layer);
// The runtime index and id commonly change every time the file is read,
// so saving this variable runtime identification information
// leads to confusion.
ref_model_layer_copy->ClearName();
ref_model_layer_copy->ClearIndex();
ref_model_layer_copy->ClearId();
ref_model_layer_copy->ClearModelSerialNumber();
m_layer_model_copy.Append(ref_model_layer_copy);
}
if (
count1 > 0
&& count1 <= count0
&& count1 == m_layer_referenced_file_copy.Count()
&& count1 == m_layer_model_copy.Count()
)
{
if (count1 != count0)
{
m_layer_referenced_file_copy.SetCount(count1);
m_runtime_layer_id_map.Empty();
for (int i = 0; i < count1; i++)
{
m_runtime_layer_id_map.AddPair(
m_layer_referenced_file_copy[i]->Id(),
m_layer_model_copy[i]->Id()
);
}
m_runtime_layer_id_map.ImproveSearchSpeed();
}
}
else
{
InternalDestroyHelper();
}
}
bool ON_ReferencedComponentSettingsImpl::Internal_UpdateBool(
bool bPreviousReferenceFileState,
bool bCurrentReferenceFileState,
bool bPreviousModelState
)
{
return
(bPreviousReferenceFileState == bCurrentReferenceFileState)
? bPreviousModelState
: bCurrentReferenceFileState;
}
ON_Color ON_ReferencedComponentSettingsImpl::Internal_UpdateColor(
ON_Color previous_reference_file_color,
ON_Color current_reference_file_color,
ON_Color previous_model_color
)
{
return
(previous_reference_file_color == current_reference_file_color)
? previous_model_color
: current_reference_file_color;
}
double ON_ReferencedComponentSettingsImpl::Internal_UpdateDouble(
double previous_reference_file_value,
double current_reference_file_value,
double previous_model_value
)
{
return
(previous_reference_file_value == current_reference_file_value)
? previous_model_value
: current_reference_file_value;
}
bool ON_ReferencedComponentSettingsImpl::Internal_UpdateLayer(
const ON_Layer& previous_referenced_file_layer,
const ON_Layer& reference_file_layer,
const ON_Layer& previous_model_layer,
ON_Layer& model_layer
)
{
// NOTES:
//
// 1)
// It is critical that model_layer identification information (name, index, id)
// do not change.
//
// 2)
// model_layer is generally the same layer as reference_file_layer and may be
// same layer as referenced_file_layer_copy or model_layer_copy,
// so it must be initialized AFTER the final settings are determined.
//
// Any per view settings on previous_referenced_file_layer or
// reference_file_layer should have been removed.
// In any case they have no meaning in the current context
// since the views from the reference_file are not imported.
//
// There may be per view settings on previous_model_layer or model_layer
// and these need to be handled appropriately.
// See RH-37183 for more details and an example.
//
// Layer visibility
const bool bIsVisible = ON_ReferencedComponentSettingsImpl::Internal_UpdateBool(
previous_referenced_file_layer.IsVisible(),
reference_file_layer.IsVisible(),
previous_model_layer.IsVisible()
);
model_layer.SetVisible(bIsVisible);
// Layer locked
const bool bIsLocked = ON_ReferencedComponentSettingsImpl::Internal_UpdateBool(
previous_referenced_file_layer.IsLocked(),
reference_file_layer.IsLocked(),
previous_model_layer.IsLocked()
);
model_layer.SetLocked(bIsLocked);
// Layer color
const ON_Color color = ON_ReferencedComponentSettingsImpl::Internal_UpdateColor(
previous_referenced_file_layer.Color(),
reference_file_layer.Color(),
previous_model_layer.Color()
);
model_layer.SetColor(color);
// Layer plot color
const ON_Color plot_color = ON_ReferencedComponentSettingsImpl::Internal_UpdateColor(
previous_referenced_file_layer.PlotColor(),
reference_file_layer.PlotColor(),
previous_model_layer.PlotColor()
);
model_layer.SetPlotColor(plot_color);
// Layer plot weight
double plot_weight = ON_ReferencedComponentSettingsImpl::Internal_UpdateDouble(
previous_referenced_file_layer.PlotWeight(),
reference_file_layer.PlotWeight(),
previous_model_layer.PlotWeight()
);
model_layer.SetPlotWeight(plot_weight);
// Add more settings here without breaking anything
// Dale Lear August 2017 - RH-39457
// Saved PerViewport settings need to be applied to model_layer
//
// Per view settings from the reference file have no meaning because the
// views they apply to are in the reference file and those views are
// not merge into the active model.
// The per view settings are simply copied from previous_model_layer to model_layer
// because the previous_model_layer is the only place to persistently
// store per viewport settings in the current' model's archive.
model_layer.DeletePerViewportSettings(ON_nil_uuid);
model_layer.CopyPerViewportSettings(
previous_model_layer,
ON_nil_uuid,
ON_Layer::PER_VIEWPORT_SETTINGS::per_viewport_all_settings
);
return true;
}
bool ON_ReferencedComponentSettingsImpl::Internal_UpdateLayer(
ON_Layer& layer
) const
{
for (;;)
{
// input layer has values read from reference file,
// so layer->Id() = reference file layer id
const ON_UUID reference_file_layer_id = layer.Id();
if (ON_nil_uuid == reference_file_layer_id)
break;
const int count = m_layer_referenced_file_copy.Count();
if (count <= 0)
break;
if (count != m_layer_model_copy.Count())
break;
for (int i = 0; i < count; i++)
{
if (nullptr == m_layer_referenced_file_copy[i])
continue;
if (reference_file_layer_id != m_layer_referenced_file_copy[i]->Id())
continue;
if (nullptr == m_layer_model_copy[i])
continue;
return ON_ReferencedComponentSettingsImpl::Internal_UpdateLayer(
*m_layer_referenced_file_copy[i],
layer,
*m_layer_model_copy[i],
layer
);
}
break;
}
return false;
}
ON_InstanceDefinition::ON_InstanceDefinition() ON_NOEXCEPT
: ON_ModelComponent(ON_ModelComponent::Type::InstanceDefinition)
{}
ON_InstanceDefinition::~ON_InstanceDefinition()
{
Internal_Destroy();
}
ON_InstanceDefinition::ON_InstanceDefinition(const ON_InstanceDefinition& src)
: ON_ModelComponent(ON_ModelComponent::Type::InstanceDefinition,src)
{
Internal_Copy(src);
}
ON_InstanceDefinition& ON_InstanceDefinition::operator=(const ON_InstanceDefinition& src)
{
if ( this != &src )
{
Internal_Destroy();
ON_ModelComponent::operator=(src);
Internal_Copy(src);
}
return *this;
}
void ON_InstanceDefinition::Internal_Destroy()
{
if (nullptr != m_linked_idef_component_settings)
{
delete m_linked_idef_component_settings;
m_linked_idef_component_settings = nullptr;
}
}
void ON_InstanceDefinition::Internal_Copy(const ON_InstanceDefinition& src)
{
m_description = src.m_description;
m_url = src.m_url;
m_url_tag = src.m_url_tag;
m_bbox = src.m_bbox;
m_us = src.m_us;
m_idef_update_type = src.m_idef_update_type;
m_bSkipNestedLinkedDefinitions = src.m_bSkipNestedLinkedDefinitions;
m_linked_file_reference = src.m_linked_file_reference;
m_linked_file_V5_checksum = src.m_linked_file_V5_checksum;
m_linked_component_appearance = src.m_linked_component_appearance;
if (nullptr != src.m_linked_idef_component_settings)
m_linked_idef_component_settings = new ON_ReferencedComponentSettings(*src.m_linked_idef_component_settings);
//This omission was causing part of https://mcneel.myjetbrains.com/youtrack/issue/RH-47128
m_object_uuid = src.m_object_uuid;
}
const ON_InstanceDefinition* ON_InstanceDefinition::FromModelComponentRef(
const class ON_ModelComponentReference& model_component_reference,
const ON_InstanceDefinition* none_return_value
)
{
const ON_InstanceDefinition* p = ON_InstanceDefinition::Cast(model_component_reference.ModelComponent());
return (nullptr != p) ? p : none_return_value;
}
void ON_InstanceDefinition::Dump( ON_TextLog& text_log ) const
{
text_log.Print("Instance Definition\n");
text_log.PushIndent();
ON_ModelComponent::Dump(text_log);
text_log.Print("Type: ");
switch( InstanceDefinitionType() )
{
case ON_InstanceDefinition::IDEF_UPDATE_TYPE::Unset:
text_log.Print("Unset");
break;
case ON_InstanceDefinition::IDEF_UPDATE_TYPE::Static:
text_log.Print("Static");
break;
case ON_InstanceDefinition::IDEF_UPDATE_TYPE::LinkedAndEmbedded:
text_log.Print("LinkedAndEmbedded");
break;
case ON_InstanceDefinition::IDEF_UPDATE_TYPE::Linked:
switch (LinkedComponentAppearance())
{
case ON_InstanceDefinition::eLinkedComponentAppearance::Active:
text_log.Print("Linked - active layer style");
break;
case ON_InstanceDefinition::eLinkedComponentAppearance::Reference:
text_log.Print("Linked - reference layer style");
break;
default:
text_log.Print("Linked");
break;
}
break;
default:
text_log.Print("not valid");
break;
}
text_log.Print("\n");
const wchar_t* wsDescription = static_cast< const wchar_t* >(m_description);
if ( 0 != wsDescription && 0 != wsDescription[0])
text_log.Print("Description: \"%ls\"\n",wsDescription);
const wchar_t* wsURL = static_cast< const wchar_t* >(m_url);
if ( 0 != wsURL && 0 != wsURL[0])
text_log.Print("URL: \"%ls\"\n",wsURL);
const wchar_t* wsTag = static_cast< const wchar_t* >(m_url_tag);
if ( 0 != wsTag && 0 != wsTag[0])
text_log.Print("URL tag: \"%ls\"\n",wsTag);
m_us.Dump(text_log);
if (m_linked_file_reference.IsSet())
{
text_log.Print("Linked definition file path: ");
m_linked_file_reference.Dump(text_log);
}
const int id_count = m_object_uuid.Count();
text_log.Print("Contains: %d objects\n",id_count);
if ( id_count > 0 )
{
text_log.PushIndent();
text_log.Print(m_object_uuid[0]);
text_log.Print("\n");
if ( id_count > 4 )
{
text_log.Print("...\n");
}
else
{
for ( int i = 1; i < id_count; i++ )
{
text_log.Print(m_object_uuid[i]);
text_log.Print("\n");
}
}
text_log.PopIndent();
}
m_bbox.Dump(text_log);
text_log.PopIndent();
}
bool ON_InstanceDefinition::IsValid( ON_TextLog* text_log ) const
{
if (false == ON_ModelComponent::IsValid(text_log))
return false;
if ( IdIsNil() )
{
if (text_log)
{
text_log->Print("ON_InstanceDefinition has nil uuid.\n");
}
return false;
}
if ( !m_bbox.IsValid() )
{
if (text_log)
{
text_log->Print("ON_InstanceDefinition has invalid bounding box.\n");
}
return false;
}
switch( InstanceDefinitionType() )
{
case ON_InstanceDefinition::IDEF_UPDATE_TYPE::Static:
// no source archive information should be present
if ( m_linked_file_reference.IsSet() )
{
if (text_log)
{
text_log->Print("ON_InstanceDefinition is static but m_linked_file_path is not empty.\n");
}
return false;
}
if ( ON_InstanceDefinition::eLinkedComponentAppearance::Unset != LinkedComponentAppearance() )
{
if (text_log)
{
text_log->Print("ON_InstanceDefinition type is Static but LinkedComponentAppearance() is not ON_InstanceDefinition::IDEF_UPDATE_TYPE::Unset.\n");