-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathopennurbs_extensions.cpp
5927 lines (5110 loc) · 177 KB
/
opennurbs_extensions.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"
#include "opennurbs_internal_defines.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
const ONX_ErrorCounter ONX_ErrorCounter::operator+ (
const ONX_ErrorCounter& rhs
)
{
ONX_ErrorCounter sum;
sum.m_failure_count = m_failure_count + rhs.m_failure_count;
sum.m_error_count = m_error_count + rhs.m_error_count;
sum.m_warning_count = m_warning_count + rhs.m_warning_count;
sum.m_state_bit_field = m_state_bit_field | rhs.m_state_bit_field;
sum.m_opennurbs_library_error_count
= (m_opennurbs_library_error_count < rhs.m_opennurbs_library_error_count)
? rhs.m_opennurbs_library_error_count
: m_opennurbs_library_error_count;
sum.m_opennurbs_library_warning_count
= (m_opennurbs_library_warning_count < rhs.m_opennurbs_library_warning_count)
? rhs.m_opennurbs_library_warning_count
: m_opennurbs_library_warning_count;
return sum;
}
const ONX_ErrorCounter ONX_ErrorCounter::operator+= (const ONX_ErrorCounter& rhs)
{
m_failure_count += rhs.m_failure_count;
m_error_count += rhs.m_error_count;
m_warning_count += rhs.m_warning_count;
m_state_bit_field |= rhs.m_state_bit_field;
if (m_opennurbs_library_error_count < rhs.m_opennurbs_library_error_count)
m_opennurbs_library_error_count = rhs.m_opennurbs_library_error_count;
if (m_opennurbs_library_warning_count < rhs.m_opennurbs_library_warning_count)
m_opennurbs_library_warning_count = rhs.m_opennurbs_library_warning_count;
return *this;
}
unsigned int ONX_ErrorCounter::FailureCount() const
{
return this->m_failure_count;
}
unsigned int ONX_ErrorCounter::ErrorCount() const
{
return this->m_error_count;
}
unsigned int ONX_ErrorCounter::WarningCount() const
{
return this->m_warning_count;
}
unsigned int ONX_ErrorCounter::TotalCount() const
{
return FailureCount() + ErrorCount() + WarningCount();
}
unsigned int ONX_ErrorCounter::IncrementFailureCount()
{
return ++m_failure_count; // <- Good location for a debugger breakpoint.
}
unsigned int ONX_ErrorCounter::IncrementErrorCount()
{
return ++m_error_count; // <- Good location for a debugger breakpoint.
}
unsigned int ONX_ErrorCounter::IncrementWarningCount()
{
return ++m_warning_count; // <- Good location for a debugger breakpoint.
}
void ONX_ErrorCounter::ClearLibraryErrors()
{
m_opennurbs_library_error_count = ON_GetErrorCount();
m_state_bit_field |= 1;
}
unsigned int ONX_ErrorCounter::AddLibraryErrors()
{
const bool bActive = (0 != (1 & m_state_bit_field));
const unsigned int count0 = m_opennurbs_library_error_count;
ClearLibraryErrors();
const unsigned int count
= bActive
? (m_opennurbs_library_error_count - count0)
: 0U;
if (bActive && count > 0)
m_error_count += count; // <- Good location for a debugger breakpoint.
return count;
}
void ONX_ErrorCounter::ClearLibraryWarnings()
{
m_opennurbs_library_warning_count = ON_GetWarningCount();
m_state_bit_field |= 2;
}
unsigned int ONX_ErrorCounter::AddLibraryWarnings()
{
const bool bActive = (0 != (2 & m_state_bit_field));
const unsigned int count0 = m_opennurbs_library_warning_count;
ClearLibraryWarnings();
const unsigned int count
= bActive
? (m_opennurbs_library_warning_count - count0)
: 0U;
if (bActive && count>0)
m_warning_count += count; // <- Good location for a debugger breakpoint.
return count;
}
void ONX_ErrorCounter::ClearLibraryErrorsAndWarnings()
{
ClearLibraryErrors();
ClearLibraryWarnings();
}
unsigned int ONX_ErrorCounter::AddLibraryErrorsAndWarnings()
{
return AddLibraryErrors() + AddLibraryWarnings();
}
void ONX_ErrorCounter::Dump(ON_TextLog& text_log) const
{
text_log.Print(
"%u failures, %u errors, %u warnings",
m_failure_count,
m_error_count,
m_warning_count
);
}
#if defined(OPENNURBS_EXPORTS)
////////////////////////////////////////////////////////////////////////////////
//
// When openNURBS is used as a Microsoft Windows DLL, it is possible
// for new/delete to allocate memory in one executable and delete
// it in another. Because Microsoft Windows has incompatible memory
// managers in its plethora of C libraries and the choice of which
// C library actually gets used depends on the code generation
// options you choose, we get lots of support questions asking
// about hard to trace crashes.
//
// If you are using openNURBS as a Windows DLL, you are sure you know
// what you are doing, and you promise never to ask for support, then
// feel free to delete these overrides.
//
//
#if defined(ON_COMPILER_MSC)
#pragma message( " --- OpenNURBS overriding ONX_Model new and delete" )
#endif
// ONX_Model_UserData new/delete
void* ONX_Model_UserData::operator new(size_t sz)
{
// ONX_Model_UserData new
return onmalloc(sz);
}
void ONX_Model_UserData::operator delete(void* p)
{
// ONX_Model_UserData delete
onfree(p);
}
void* ONX_Model_UserData::operator new[] (size_t sz)
{
// ONX_Model_UserData array new
return onmalloc(sz);
}
void ONX_Model_UserData::operator delete[] (void* p)
{
// ONX_Model_UserData array delete
onfree(p);
}
void* ONX_Model_UserData::operator new(size_t, void* p)
{
// ONX_Model_UserData placement new
return p;
}
void ONX_Model_UserData::operator delete(void*, void*)
{
// ONX_Model_UserData placement delete
return;
}
// ONX_Model new/delete
void* ONX_Model::operator new(size_t sz)
{
// ONX_Model new
return onmalloc(sz);
}
void ONX_Model::operator delete(void* p)
{
// ONX_Model delete
onfree(p);
}
void* ONX_Model::operator new[] (size_t sz)
{
// ONX_Model array new
return onmalloc(sz);
}
void ONX_Model::operator delete[] (void* p)
{
// ONX_Model array delete
onfree(p);
}
void* ONX_Model::operator new(size_t, void* p)
{
// ONX_Model placement new
return p;
}
void ONX_Model::operator delete(void*, void*)
{
// ONX_Model placement delete
return;
}
#endif
//
//
////////////////////////////////////////////////////////////////////////////////
class ONX_ModelComponentReferenceLink
{
public:
ONX_ModelComponentReferenceLink() = default;
~ONX_ModelComponentReferenceLink() = default;
ONX_ModelComponentReferenceLink(const ONX_ModelComponentReferenceLink&) = default;
ONX_ModelComponentReferenceLink& operator=(const ONX_ModelComponentReferenceLink&) = default;
public:
// m_mcr in this class is the "first" std::shared_ptr<ON_ModelComponent> that manages
// the referenced ON_ModelComponent and is what insures ON_ModelComponent alive inside
// and ONX_Model for the duration of the model's lifetime.
//
// m_sn = ON_ModelComponent.RuntimeSerialNumber() so sn lookup can be safely used to find runtime pointer.
// When m_mcr.ModelComponent() is not nullptr, then m_sn = m_mcr.ModelComponent()->RuntimeSerialNumber().
ON_ModelComponentReference m_mcr;
ON__UINT64 m_sn = 0;
ONX_ModelComponentReferenceLink* m_next = nullptr;
ONX_ModelComponentReferenceLink* m_prev = nullptr;
};
enum class RenderContentKinds { Material, Environment, Texture };
ON_3dmObjectAttributes* GetComponentAttributes(const ON_ModelComponent& component)
{
// To have attributes, the component must be an ON_ModelGeometryComponent.
const auto* mgc = ON_ModelGeometryComponent::Cast(&component);
if (nullptr == mgc)
return nullptr; // Wrong type of component.
return mgc->ExclusiveAttributes();
}
class ONX_ModelPrivate final
{
public:
ONX_ModelPrivate(ONX_Model& m);
~ONX_ModelPrivate();
using EmbeddedFileMap = std::unordered_map<std::wstring, std::wstring>;
bool GetRDKDocumentXML(ON_wString& xml, bool embedded_files, int archive_3dm_version) const;
ONX_Model_UserData* GetRDKDocumentUserData(int archive_3dm_version) const;
void PopulateDefaultRDKDocumentXML(ON_XMLRootNode& root) const;
bool PopulateRDKComponents(int archive_3dm_version);
bool UpdateRDKUserData(int archive_3dm_version);
bool CreateRenderContentFromXML(class ON_XMLNode& model_node, RenderContentKinds kind);
bool CreateXMLFromRenderContent(ON_XMLNode& model_node, RenderContentKinds kind) const;
bool SetRDKDocumentInformation(const wchar_t* xml, ONX_Model_UserData& docud, int archive_3dm_version) const;
ON_XMLNode* GetRenderContentSectionNode(ON_XMLNode& model_node, RenderContentKinds kind) const;
ON_XMLNode* GetPostEffectSectionNode(ON_XMLNode& model_node, ON_PostEffect::Types type) const;
static void RemoveAllEmbeddedFiles(ONX_Model& model);
static bool GetEntireRDKDocument(const ONX_Model_UserData& docud, ON_wString& xml, ONX_Model* model);
public:
ONX_Model& m_model;
ON__UINT64 m_model_content_version_number = 0;
ON_ClassArray<ONX_Model::ONX_ModelComponentList> m_mcr_lists;
};
ON_InternalXMLImpl::~ON_InternalXMLImpl()
{
if (nullptr != _local_node)
{
delete _local_node;
_local_node = nullptr;
}
}
ON_XMLNode& ON_InternalXMLImpl::Node(void) const
{
// If the model node pointer is set, return that. This is a pointer to a node owned by the ON_3dmRenderSettings
// which contains the entire RDK document XML. This is used by objects (Ground Plane, etc.) that are owned by the
// ON_3dmRenderSettings. In the case of Ground Plane etc, it's a pointer into the ON_3dmRenderSettings XML.
// In the case of decals, it's a pointer into the decal collection's XML.
if (nullptr != _model_node)
return *_model_node;
// Since the model node is not set, we need a local node to hold the XML. If one has not been created yet,
// create it. The local node is owned by this object. This case occurs for free-floating copies of model
// objects and also for free-floating copies of decals and mesh modifiers. This node only contains the XML
// data that's relevant to the object it's for, not the entire XML, but the object's node is still a child
// node under the same node hierarchy as if it were the entire XML.
if (nullptr == _local_node)
_local_node = new ON_XMLNode(NameOfRootNode());
return *_local_node;
}
void ON_InternalXMLImpl::SetModelNode(ON_XMLNode& node)
{
ON_ASSERT(_model_node == nullptr);
if (nullptr != _local_node)
{
delete _local_node;
_local_node = nullptr;
}
_model_node = &node;
}
ON_wString ON_InternalXMLImpl::NameOfRootNode(void) const
{
return ON_XMLRootNode().TagName();
}
ON_XMLVariant ON_InternalXMLImpl::GetParameter(const wchar_t* path_to_node, const wchar_t* param_name, const ON_XMLVariant& def) const
{
return InternalGetParameter(path_to_node, param_name, L"", def);
}
ON_XMLVariant ON_InternalXMLImpl::GetParameter_NoType(const wchar_t* path_to_node, const wchar_t* param_name, const wchar_t* default_type, const ON_XMLVariant& def) const
{
return InternalGetParameter(path_to_node, param_name, default_type, def);
}
ON_XMLVariant ON_InternalXMLImpl::InternalGetParameter(const wchar_t* path_to_node, const wchar_t* param_name, const wchar_t* default_type, const ON_XMLVariant& def) const
{
std::lock_guard<std::recursive_mutex> lg(_mutex);
const ON_XMLNode* node_read = Node().GetNodeAtPath(path_to_node);
if (nullptr == node_read)
return def;
ON_XMLVariant value;
ON_XMLParameters p(*node_read);
p.SetDefaultReadType(default_type);
if (!p.GetParam(param_name, value))
return def;
return value;
}
bool ON_InternalXMLImpl::SetParameter(const wchar_t* path_to_node, const wchar_t* param_name, const ON_XMLVariant& value)
{
return InternalSetParameter(path_to_node, param_name, true, value);
}
bool ON_InternalXMLImpl::SetParameter_NoType(const wchar_t* path_to_node, const wchar_t* param_name, const ON_XMLVariant& value)
{
return InternalSetParameter(path_to_node, param_name, false, value);
}
bool ON_InternalXMLImpl::InternalSetParameter(const wchar_t* path_to_node, const wchar_t* param_name, bool write_type, const ON_XMLVariant& value)
{
std::lock_guard<std::recursive_mutex> lg(_mutex);
bool success = false;
ON_XMLNode* node_write = Node().CreateNodeAtPath(path_to_node);
if (nullptr != node_write)
{
ON_XMLParameters p(*node_write);
p.SetWriteTypeProperty(write_type);
if (nullptr != p.SetParam(param_name, value))
success = true;
}
return success;
}
bool ON_InternalXMLImpl::RemoveParameter(const wchar_t* path_to_node, const wchar_t* param_name)
{
std::lock_guard<std::recursive_mutex> lg(_mutex);
bool success = false;
ON_XMLNode* node = Node().GetNodeAtPath(path_to_node);
if (nullptr != node)
{
ON_XMLNode* child = node->GetNamedChild(param_name);
success = node->RemoveChild(child);
}
return success;
}
// ONX_Model
ONX_Model::ONX_Model()
{
m_private = new ONX_ModelPrivate(*this);
// Tell the sun in our render settings to use the earth anchor point in our settings.
m_settings.m_RenderSettings.Sun().UseEarthAnchorPoint(m_settings.m_earth_anchor_point);
}
ONX_Model::~ONX_Model()
{
Reset();
delete m_private;
m_private = nullptr;
}
void ONX_Model::Reset()
{
m_3dm_file_version = 0;
m_3dm_opennurbs_version = 0;
m_sStartSectionComments = ON_String::EmptyString;
m_properties = ON_3dmProperties::Empty;
m_settings = ON_3dmSettings::Default;
for (unsigned int i = 0; i < m_userdata_table.UnsignedCount(); i++)
{
delete m_userdata_table[i];
}
m_userdata_table.Destroy();
for (int i = 0; i < m_private->m_mcr_lists.Count(); i++)
{
ONX_ModelComponentList& list = m_private->m_mcr_lists[i];
ONX_ModelComponentReferenceLink* mcr_link = list.m_first_mcr_link;
while (nullptr != mcr_link)
{
mcr_link->m_mcr = ON_ModelComponentReference::Empty;
mcr_link = mcr_link->m_next;
}
list.m_first_mcr_link = nullptr;
list.m_last_mcr_link = nullptr;
}
m_mcr_sn_map.EmptyList();
m_mcr_link_fsp.ReturnAll();
m_default_line_pattern = ON_ModelComponentReference::CreateConstantSystemComponentReference(ON_Linetype::Continuous);
m_default_layer = ON_ModelComponentReference::CreateConstantSystemComponentReference(ON_Layer::Default);
m_default_text_style = ON_ModelComponentReference::CreateConstantSystemComponentReference(ON_TextStyle::Default);
m_default_dimension_style = ON_ModelComponentReference::CreateConstantSystemComponentReference(ON_DimStyle::Default);
m_manifest.Reset();
m_original_to_manifest_map = ON_ManifestMap::Empty;
m_manifest_to_original_map = ON_ManifestMap::Empty;
m_model_geometry_bbox = ON_BoundingBox::UnsetBoundingBox;
m_render_light_bbox = ON_BoundingBox::UnsetBoundingBox;
if (nullptr != m_model_user_string_list)
{
delete m_model_user_string_list;
m_model_user_string_list = nullptr;
}
}
void ONX_Model::Internal_ComponentTypeBoundingBox(
const ON_ModelComponent::Type component_type,
ON_BoundingBox& bbox
) const
{
if (false == bbox.IsValid())
{
ON_BoundingBox local_bbox;
for (
const ONX_ModelComponentReferenceLink* link = Internal_ComponentListConst(component_type).m_first_mcr_link;
nullptr != link;
link = link->m_next
)
{
const ON_ModelComponent* model_component = link->m_mcr.ModelComponent();
if (nullptr == model_component)
continue;
if (component_type != model_component->ComponentType())
continue;
const ON_ModelGeometryComponent* model_geometry = ON_ModelGeometryComponent::Cast(model_component);
if (nullptr == model_geometry)
continue;
const ON_3dmObjectAttributes* attributes = model_geometry->Attributes(nullptr);
if (nullptr != attributes && attributes->IsInstanceDefinitionObject())
continue;
const ON_Geometry* geometry = model_geometry->Geometry(nullptr);
if (nullptr == geometry)
continue;
local_bbox.Union(geometry->BoundingBox());
}
bbox = local_bbox;
}
return;
}
ON_BoundingBox ONX_Model::ModelGeometryBoundingBox() const
{
Internal_ComponentTypeBoundingBox(ON_ModelComponent::Type::ModelGeometry,m_model_geometry_bbox);
return m_model_geometry_bbox;
}
ON_BoundingBox ONX_Model::RenderLightBoundingBox() const
{
Internal_ComponentTypeBoundingBox(ON_ModelComponent::Type::RenderLight,m_render_light_bbox);
return m_render_light_bbox;
}
const ON_ComponentManifest& ONX_Model::Manifest() const
{
return m_manifest;
}
const ON_ManifestMap& ONX_Model::OriginalToModelMap() const
{
return m_original_to_manifest_map;
}
const ON_ManifestMap& ONX_Model::ModelToOriginalMap() const
{
return m_manifest_to_original_map;
}
bool ONX_Model::ValdateComponentIdAndName(
ON_ModelComponent::Type component_type,
const ON_UUID& candidate_id,
const ON_UUID& component_parent_id,
const wchar_t* candidate_name,
bool bResolveIdConflict,
bool bResolveNameConflict,
ON_UUID& model_id,
ON_wString& model_name
) const
{
for (;;)
{
if (false == ON_ModelComponent::ComponentTypeIsValid(component_type))
{
ON_ERROR("Invalid component_type parameter.");
break;
}
const bool bIndexRequired = ON_ModelComponent::IndexRequired(component_type);
const unsigned int count = m_manifest.ComponentIndexLimit(component_type);
if (bIndexRequired && count >= 0x7FFFFFFFU)
{
ON_ERROR("Unable to create model component index.");
break;
}
const bool bIdAvailable = m_manifest.IdIsAvailable(candidate_id);
const bool bCreateId = ON_UuidIsNil(candidate_id) || (false == bIdAvailable && bResolveIdConflict);
if (false == bIdAvailable && false == bCreateId)
{
break;
}
ON_wString name(candidate_name);
name.TrimLeftAndRight();
const bool bUniqueNameReqired = ON_ModelComponent::UniqueNameRequired(component_type);
if ( bUniqueNameReqired )
{
const ON_UUID name_parent_id
= ON_ModelComponent::UniqueNameIncludesParent(component_type)
? component_parent_id
: ON_nil_uuid;
ON_NameHash name_hash = ON_NameHash::Create(name_parent_id, name);
if (name_hash.IsInvalidNameHash())
{
if (false == bResolveNameConflict)
{
ON_ERROR("Invalid candidate_name parameter.");
break;
}
name = ON_wString::EmptyString;
name_hash = ON_NameHash::Create(name_parent_id, name);
}
const bool bNameIsValid = name.IsNotEmpty() && m_manifest.NameIsAvailable(component_type, name_hash);
if (false == bNameIsValid )
{
// we need to create a unique and non-empty name
if (false == bResolveNameConflict)
{
// not allowed to modify name
break;
}
name = m_manifest.UnusedName(component_type, component_parent_id, nullptr, name, nullptr, ON_UNSET_UINT_INDEX, nullptr);
if (name.IsEmpty())
{
ON_ERROR("Unable to create component name.");
break;
}
}
}
model_id = bCreateId ? ON_CreateId() : candidate_id;
model_name = name;
return true;
}
model_id = ON_nil_uuid;
model_name = ON_wString::EmptyString;
return false;
}
unsigned int ONX_Model::ComponentIndexLimit(
ON_ModelComponent::Type component_type
) const
{
return m_manifest.ComponentIndexLimit(component_type);
}
unsigned int ONX_Model::ActiveAndDeletedComponentCount(
ON_ModelComponent::Type component_type
) const
{
return m_manifest.ActiveAndDeletedComponentCount(component_type);
}
unsigned int ONX_Model::ActiveComponentCount(
ON_ModelComponent::Type component_type
) const
{
return m_manifest.ActiveComponentCount(component_type);
}
unsigned int ONX_Model::DeletedComponentCount(
ON_ModelComponent::Type component_type
) const
{
return m_manifest.DeletedComponentCount(component_type);
}
ON_ModelComponentReference ONX_Model::ComponentFromIndex(
ON_ModelComponent::Type component_type,
int component_model_index
) const
{
if (component_model_index >= 0)
{
return ComponentFromUnsignedIndex(component_type,(unsigned int)component_model_index);
}
return ON_ModelComponentReference::Empty;
}
ON_ModelComponentReference ONX_Model::ComponentFromUnsignedIndex(
ON_ModelComponent::Type component_type,
unsigned int component_model_index
) const
{
ON__UINT64 sn = m_manifest.ItemFromIndex(component_type,component_model_index).ComponentRuntimeSerialNumber();
ONX_ModelComponentReferenceLink* link = Internal_ModelComponentLinkFromSerialNumber(sn);
return (nullptr != link)
? link->m_mcr
: ON_ModelComponentReference::Empty;
}
ON_ModelComponentReference ONX_Model::ComponentFromId(
ON_ModelComponent::Type component_type,
ON_UUID component_model_id
) const
{
ON__UINT64 sn = m_manifest.ItemFromId(component_type, component_model_id).ComponentRuntimeSerialNumber();
ONX_ModelComponentReferenceLink* link = Internal_ModelComponentLinkFromSerialNumber(sn);
return (nullptr != link)
? link->m_mcr
: ON_ModelComponentReference::Empty;
}
const ON_ModelComponentReference& ONX_Model::ComponentFromRuntimeSerialNumber(
ON__UINT64 runtime_serial_number
) const
{
ONX_ModelComponentReferenceLink* link = Internal_ModelComponentLinkFromSerialNumber(runtime_serial_number);
return (nullptr != link)
? link->m_mcr
: ON_ModelComponentReference::Empty;
}
ON_ModelComponentReference ONX_Model::ModelGeometryFromId(
ON_UUID model_object_id
) const
{
return ComponentFromId(ON_ModelComponent::Type::ModelGeometry, model_object_id);
}
const ON_ModelGeometryComponent& ONX_Model::ModelGeometryComponentFromId(
ON_UUID model_object_id
) const
{
const ON_ModelGeometryComponent* p = ON_ModelGeometryComponent::Cast(ModelGeometryFromId(model_object_id).ModelComponent());
return (nullptr != p) ? *p : ON_ModelGeometryComponent::Unset;
}
ON_ModelComponentReference ONX_Model::ComponentFromName(
ON_ModelComponent::Type component_type,
ON_UUID component_parent_id,
const wchar_t* component_model_name
) const
{
const ON_UUID name_parent_id
= ON_ModelComponent::UniqueNameIncludesParent(component_type)
? component_parent_id
: ON_nil_uuid;
const bool bIgnoreCase = ON_ModelComponent::UniqueNameIgnoresCase(component_type);
const ON_NameHash component_model_name_hash = ON_NameHash::Create(name_parent_id,component_model_name,bIgnoreCase);
return ComponentFromNameHash(component_type,component_model_name_hash);
}
ON_ModelComponentReference ONX_Model::ComponentFromNameHash(
ON_ModelComponent::Type component_type,
const ON_NameHash& component_model_name_hash
) const
{
ON__UINT64 sn = m_manifest.ItemFromNameHash(component_type, component_model_name_hash).ComponentRuntimeSerialNumber();
ONX_ModelComponentReferenceLink* link = Internal_ModelComponentLinkFromSerialNumber(sn);
return (nullptr != link)
? link->m_mcr
: ON_ModelComponentReference::Empty;
}
ON_ModelComponentReference ONX_Model::ImageFromIndex(
int image_model_index
) const
{
ON_ModelComponentReference cr = ComponentFromIndex(ON_ModelComponent::Type::Image, image_model_index);
return cr;
}
ON_ModelComponentReference ONX_Model::ImageFromId(
ON_UUID image_id
) const
{
ON_ModelComponentReference cr = ComponentFromId(ON_ModelComponent::Type::Image, image_id);
return cr;
}
ON_ModelComponentReference ONX_Model::ImageFromFileFullPath(
const wchar_t* image_file_full_path_name
) const
{
ON_FileReference file_reference;
file_reference.SetFullPath(image_file_full_path_name,false);
return ImageFromFileReference(file_reference);
}
ON_ModelComponentReference ONX_Model::ImageFromFileContent(
const ON_ContentHash& image_file_content_hash
) const
{
ON_FileReference file_reference;
file_reference.SetContentHash(image_file_content_hash);
return ImageFromFileReference(file_reference);
}
ON_ModelComponentReference ONX_Model::ImageFromFileReference(
const ON_FileReference& file_reference
) const
{
const ON_wString full_path_name(file_reference.FullPath());
const bool bCheckFullPath = full_path_name.IsNotEmpty();
const ON_ContentHash file_content_hash = file_reference.ContentHash();
const bool bCheckContentHash = file_content_hash.IsSet();
if (false == bCheckFullPath && false == bCheckContentHash)
return ON_ModelComponentReference::Empty;
ONX_ModelComponentIterator it(*this,ON_ModelComponent::Type::Image);
for ( ON_ModelComponentReference cr = it.FirstComponentReference(); false == cr.IsEmpty(); cr = it.NextComponentReference())
{
const ON_Bitmap* image = ON_Bitmap::Cast(cr.ModelComponent());
if (nullptr == image)
continue;
const ON_FileReference& image_file_reference = image->FileReference();
if (bCheckFullPath)
{
if (false == full_path_name.EqualPath(image_file_reference.FullPath()))
continue;
}
if (bCheckContentHash)
{
if (0 != ON_ContentHash::CompareContent(file_content_hash, image_file_reference.ContentHash()))
continue;
}
return cr;
}
return ON_ModelComponentReference::Empty;
}
ON_ModelComponentReference ONX_Model::LinePatternFromIndex(
int layer_model_index
) const
{
ON_ModelComponentReference cr = ComponentFromIndex(ON_ModelComponent::Type::LinePattern,layer_model_index);
return cr.IsEmpty() ? m_default_layer : cr;
}
ON_ModelComponentReference ONX_Model::LinePatternFromId(
ON_UUID layer_model_id
) const
{
ON_ModelComponentReference cr = ComponentFromId(ON_ModelComponent::Type::LinePattern,layer_model_id);
return cr.IsEmpty() ? m_default_layer : cr;
}
ON_ModelComponentReference ONX_Model::LinePatternFromName(
const wchar_t* line_pattern_name
) const
{
ON_ModelComponentReference cr = ComponentFromName(ON_ModelComponent::Type::LinePattern,ON_nil_uuid,line_pattern_name);
return cr.IsEmpty() ? m_default_layer : cr;
}
ON_ModelComponentReference ONX_Model::LinePatternFromNameHash(
ON_NameHash line_pattern_model_name_hash
) const
{
ON_ModelComponentReference cr = ComponentFromNameHash(ON_ModelComponent::Type::LinePattern,line_pattern_model_name_hash);
return cr.IsEmpty() ? m_default_layer : cr;
}
ON_ModelComponentReference ONX_Model::LinePatternFromAttributes(
const ON_3dmObjectAttributes& attributes
) const
{
int line_pattern_index = ON_Linetype::Continuous.Index();
switch ( attributes.LinetypeSource() )
{
case ON::linetype_from_layer:
if (attributes.m_layer_index >= 0)
{
const ON_Layer* layer = ON_Layer::Cast(LayerFromIndex(attributes.m_layer_index).ModelComponent());
if ( nullptr != layer )
line_pattern_index = layer->LinetypeIndex();
}
break;
case ON::linetype_from_object:
line_pattern_index = attributes.m_linetype_index;
break;
case ON::linetype_from_parent:
line_pattern_index = attributes.m_linetype_index;
// TODO: if object is an instance definition, get linetype
// from instance references.
break;
}
return LinePatternFromIndex(line_pattern_index);
}
ON_ModelComponentReference ONX_Model::LinePatternFromLayerIndex(
int layer_index
) const
{
ON_ModelComponentReference layer_component = LayerFromIndex(layer_index);
const ON_Layer* layer = ON_Layer::Cast(layer_component.ModelComponent());
if (nullptr == layer)
layer = &ON_Layer::Default;
return LinePatternFromIndex(layer->m_linetype_index);
}
ON_ModelComponentReference ONX_Model::LayerFromIndex(
int layer_model_index
) const
{
ON_ModelComponentReference cr = ComponentFromIndex(ON_ModelComponent::Type::Layer,layer_model_index);
return cr.IsEmpty() ? m_default_layer : cr;
}
ON_ModelComponentReference ONX_Model::LayerFromId(
ON_UUID layer_model_id
) const
{
ON_ModelComponentReference cr = ComponentFromId(ON_ModelComponent::Type::Layer,layer_model_id);
return cr.IsEmpty() ? m_default_layer : cr;
}
ON_ModelComponentReference ONX_Model::LayerFromName(
ON_UUID layer_parent_id,
const wchar_t* layer_model_name
) const
{
ON_ModelComponentReference cr = ComponentFromName(ON_ModelComponent::Type::Layer,layer_parent_id,layer_model_name);
return cr.IsEmpty() ? m_default_layer : cr;
}
ON_ModelComponentReference ONX_Model::LayerFromNameHash(
const ON_NameHash& layer_model_name_hash
) const
{
ON_ModelComponentReference cr = ComponentFromNameHash(ON_ModelComponent::Type::Layer,layer_model_name_hash);
return cr.IsEmpty() ? m_default_layer : cr;
}
ON_ModelComponentReference ONX_Model::LayerFromAttributes(
const ON_3dmObjectAttributes& attributes
) const
{
return LayerFromIndex(attributes.m_layer_index);
}
ON_ModelComponentReference ONX_Model::DimensionStyleFromIndex(
int dimension_style_model_index
) const
{
ON_ModelComponentReference cr = ComponentFromIndex(ON_ModelComponent::Type::DimStyle,dimension_style_model_index);
return cr.IsEmpty() ? DefaultDimensionStyle() : cr;
}
ON_ModelComponentReference ONX_Model::DimensionStyleFromId(
ON_UUID dimension_style_model_id
) const
{
ON_ModelComponentReference cr = ComponentFromId(ON_ModelComponent::Type::DimStyle,dimension_style_model_id);
return cr.IsEmpty() ? DefaultDimensionStyle() : cr;
}
ON_ModelComponentReference ONX_Model::DimensionStyleFromName(
const wchar_t* dimension_style_model_name
) const
{
ON_ModelComponentReference cr = ComponentFromName(ON_ModelComponent::Type::DimStyle,ON_nil_uuid,dimension_style_model_name);
return cr.IsEmpty() ? DefaultDimensionStyle() : cr;
}
ON_ModelComponentReference ONX_Model::DimensionStyleFromNameHash(
ON_NameHash dimension_style_model_name_hash
) const
{
ON_ModelComponentReference cr = ComponentFromNameHash(ON_ModelComponent::Type::DimStyle,dimension_style_model_name_hash);
return cr.IsEmpty() ? DefaultDimensionStyle() : cr;
}
ON_UUID ONX_Model::CurrentDimensionStyleId() const
{
const ON_UUID id = m_settings.CurrentDimensionStyleId();
if (ON_nil_uuid == id)
return id;
if (id == ON_DimStyle::SystemDimstyleFromId(id).Id())