-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathopennurbs_hatch.cpp
1475 lines (1269 loc) · 33.5 KB
/
opennurbs_hatch.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"
//March 23, 2008 - LW
//Adding ON_HatchExtra class to support movable base point for hatches
//This should be combined with the ON_Hatch class next time that is possible
// Don't put this extension class in a header file or export it.
class ON_HatchExtra : public ON_UserData
{
ON_OBJECT_DECLARE(ON_HatchExtra);
public:
static ON_HatchExtra* HatchExtension(ON_Hatch* pHatch, bool bCreate);
static const ON_HatchExtra* HatchExtension(const ON_Hatch* pHatch, bool bCreate);
ON_HatchExtra();
~ON_HatchExtra();
void SetDefaults();
// override virtual ON_Object::Dump function
void Dump( ON_TextLog& text_log ) const;
// override virtual ON_Object::SizeOf function
unsigned int SizeOf() const;
// override virtual ON_Object::Write function
ON_BOOL32 Write(ON_BinaryArchive& binary_archive) const;
// override virtual ON_Object::Read function
ON_BOOL32 Read(ON_BinaryArchive& binary_archive);
// override virtual ON_UserData::GetDescription function
ON_BOOL32 GetDescription( ON_wString& description );
// override virtual ON_UserData::Archive function
ON_BOOL32 Archive() const;
// Get and set a 2d point in the hatch's ECS coordinates
void SetBasePoint(ON_2dPoint& basepoint);
ON_2dPoint BasePoint() const;
ON_UUID m_parent_hatch; // Hatch this extends or ON_nil_uuid
ON_2dPoint m_basepoint; // Base point in hatch's ECS
};
ON_OBJECT_IMPLEMENT(ON_HatchExtra,ON_UserData,"3FF7007C-3D04-463f-84E3-132ACEB91062");
ON_HatchExtra* ON_HatchExtra::HatchExtension(ON_Hatch* pHatch, bool bCreate)
{
ON_HatchExtra* pExtra = 0;
if(pHatch)
{
pExtra = ON_HatchExtra::Cast(pHatch->GetUserData(ON_HatchExtra::m_ON_HatchExtra_class_id.Uuid()));
if(pExtra == 0 && bCreate)
{
pExtra = new ON_HatchExtra;
if(pExtra)
{
if(!pHatch->AttachUserData(pExtra))
{
delete pExtra;
pExtra = 0;
}
}
}
}
return pExtra;
}
const ON_HatchExtra* ON_HatchExtra::HatchExtension(const ON_Hatch* pHatch, bool bCreate)
{
return HatchExtension((ON_Hatch*)pHatch, bCreate);
}
ON_HatchExtra::ON_HatchExtra()
{
m_userdata_uuid = ON_HatchExtra::m_ON_HatchExtra_class_id.Uuid();
m_application_uuid = ON_opennurbs5_id; // opennurbs.dll reads/writes this userdata
// The id must be the version 5 id because
// V6 SaveAs V5 needs to work, but SaveAs
// V4 should not write this userdata.
m_userdata_copycount = 1;
SetDefaults();
}
ON_HatchExtra::~ON_HatchExtra()
{
}
void ON_HatchExtra::SetDefaults()
{
m_parent_hatch = ON_nil_uuid;
m_basepoint.Set(0.0,0.0);
}
void ON_HatchExtra::Dump(ON_TextLog& text_log) const
{
}
unsigned int ON_HatchExtra::SizeOf() const
{
unsigned int sz = ON_UserData::SizeOf();
sz += sizeof(*this)-sizeof(ON_UserData);
return sz;
}
ON_BOOL32 ON_HatchExtra::Write(ON_BinaryArchive& archive) const
{
bool rc = archive.BeginWrite3dmChunk(TCODE_ANONYMOUS_CHUNK,1,0);
if(rc) rc = archive.WriteUuid( m_parent_hatch);
if(rc) rc = archive.WritePoint(m_basepoint);
if(!archive.EndWrite3dmChunk())
rc = false;
return rc;
}
ON_BOOL32 ON_HatchExtra::Read(ON_BinaryArchive& archive)
{
int major_version = 0;
int minor_version = 0;
bool rc = archive.BeginRead3dmChunk(TCODE_ANONYMOUS_CHUNK,&major_version,&minor_version);
if(major_version != 1)
rc = false;
m_basepoint.Set(0.0,0.0);
if(rc) rc = archive.ReadUuid(m_parent_hatch);
if(rc) rc = archive.ReadPoint(m_basepoint);
if(!archive.EndRead3dmChunk())
rc = false;
return rc;
}
ON_BOOL32 ON_HatchExtra::GetDescription( ON_wString& description)
{
description.Format( "Userdata extension of ON_Hatch (contains basepoint)");
return true;
}
ON_BOOL32 ON_HatchExtra::Archive() const
{
return true;
}
void ON_HatchExtra::SetBasePoint(ON_2dPoint& point)
{
if(point.IsValid())
m_basepoint = point;
}
ON_2dPoint ON_HatchExtra::BasePoint() const
{
return m_basepoint;
}
/////////////////////////////////////////////////////////////////
// class ON_HatchLine
/////////////////////////////////////////////////////////////////
ON_HatchLine::ON_HatchLine()
: m_angle( 0.0), m_base( 0.0,0.0), m_offset( 0.0, 1.0)
{
}
ON_HatchLine::ON_HatchLine(double angle,
const ON_2dPoint& base,
const ON_2dVector& offset,
const ON_SimpleArray<double> dashes)
: m_angle( angle), m_base( base), m_offset( offset), m_dashes( dashes)
{
}
bool ON_HatchLine::operator==(const ON_HatchLine& src) const
{
return( m_angle == src.m_angle &&
m_base == src.m_base &&
m_offset == src.m_offset &&
m_dashes == src.m_dashes);
}
bool ON_HatchLine::operator!=(const ON_HatchLine& src) const
{
return !operator==( src);
}
ON_BOOL32 ON_HatchLine::IsValid( ON_TextLog* text_log) const
{
bool rc = m_angle >= 0.0;
if( !rc)
{
if( text_log)
text_log->Print( "Angle ( %lf) must be >= 0.0\n", m_angle);
return false;
}
rc = m_angle < ON_PI * 2.0;
if( !rc)
{
if( text_log)
text_log->Print( "Angle ( %lf) must be < 2*Pi.\n", m_angle);
return false;
}
rc = m_base != ON_2dPoint( ON_UNSET_VALUE, ON_UNSET_VALUE);
if( !rc)
{
if( text_log)
text_log->Print( "Base is not a valid point.\n");
return false;
}
rc = m_offset.x != ON_UNSET_VALUE;
if( !rc)
{
if( text_log)
text_log->Print( "Offset is not a valid vector.\n");
return false;
}
rc = m_offset.y > ON_SQRT_EPSILON;
if( !rc)
{
if( text_log)
text_log->Print( "Offset.y ( %lf) must be > 0.0", m_offset.y);
return false;
}
return true;
}
void ON_HatchLine::Dump( ON_TextLog& dump) const
{
dump.Print( "ON_HatchLine: angle = %lf radians ( %lf degrees) ",
Angle(), ON_RADIANS_TO_DEGREES * Angle());
dump.Print( " base = ");
dump.Print( m_base);
dump.Print( " offset = ");
dump.Print( m_offset);
int count = m_dashes.Count();
dump.Print( "\nDash count = %d: ", count);
for( int i = 0; i < count; i++)
{
dump.Print( "%lf", Dash( i));
if( i < count-1)
dump.Print( ", ");
}
dump.Print( "\n");
}
ON_BOOL32 ON_HatchLine::Write( ON_BinaryArchive& ar) const
{
ON_BOOL32 rc = ar.Write3dmChunkVersion(1,1);
if (rc) rc = ar.WriteDouble( m_angle);
if (rc) rc = ar.WritePoint( m_base);
if (rc) rc = ar.WriteVector( m_offset);
if (rc) rc = ar.WriteArray( m_dashes);
return rc;
}
ON_BOOL32 ON_HatchLine::Read( ON_BinaryArchive& ar)
{
m_angle = 0.0;
m_base.Set( 0.0, 0.0);
m_offset.Set( 0.0, 1.0);
m_dashes.Empty();
int major_version = 0;
int minor_version = 0;
ON_BOOL32 rc = ar.Read3dmChunkVersion( &major_version, &minor_version);
if ( major_version == 1 )
{
if ( rc) rc = ar.ReadDouble( &m_angle);
if ( rc) rc = ar.ReadPoint( m_base);
if ( rc) rc = ar.ReadVector( m_offset);
if ( rc) rc = ar.ReadArray( m_dashes);
}
return rc;
}
// ON_HatchLine Interface
double ON_HatchLine::Angle() const
{
return m_angle;
}
void ON_HatchLine::SetAngle( double angle)
{
m_angle = angle;
double twopi = ON_PI * 2.0;
// clamp between [0 2pi)
while( m_angle < 0.0)
m_angle += twopi;
while( m_angle > twopi)
m_angle -= twopi;
}
ON_2dPoint ON_HatchLine::Base() const
{
return m_base;
}
void ON_HatchLine::SetBase( const ON_2dPoint& base)
{
m_base = base;
}
ON_2dVector ON_HatchLine::Offset() const
{
return m_offset;
}
void ON_HatchLine::SetOffset( const ON_2dVector& offset)
{
m_offset = offset;
}
int ON_HatchLine::DashCount() const
{
return m_dashes.Count();
}
double ON_HatchLine::Dash( int index) const
{
if( index >= 0 && index < m_dashes.Count())
return m_dashes[index];
return 0.0;
}
void ON_HatchLine::AppendDash( double dash)
{
// if( fabs( dash) > ON_SQRT_EPSILON)
m_dashes.Append( dash);
}
void ON_HatchLine::SetPattern( const ON_SimpleArray<double>& dashes)
{
m_dashes = dashes;
}
void ON_HatchLine::GetLineData( double& angle,
ON_2dPoint& base,
ON_2dVector& offset,
ON_SimpleArray<double>& dashes) const
{
angle = m_angle;
base = m_base;
offset = m_offset; dashes = m_dashes;
}
double ON_HatchLine::GetPatternLength() const
{
int i;
double length = 0.0;
for( i = 0; i < m_dashes.Count(); i++)
length += fabs( m_dashes[i]);
return length;
}
// class ON_HatchPattern
/////////////////////////////////////////////////////////////////
ON_OBJECT_IMPLEMENT( ON_HatchPattern, ON_Object, "064E7C91-35F6-4734-A446-79FF7CD659E1" );
ON_HatchPattern::ON_HatchPattern()
: m_hatchpattern_index(-1)
, m_hatchpattern_id(ON_nil_uuid)
, m_type(ON_HatchPattern::ftSolid)
{
}
ON_HatchPattern::~ON_HatchPattern()
{
}
ON_BOOL32 ON_HatchPattern::IsValid( ON_TextLog* text_log) const
{
eFillType type = FillType();
ON_BOOL32 rc = true;
if( type != ftSolid && type != ftLines && type != ftGradient)
{
if( text_log)
text_log->Print( "Type field not set correctly.\n");
rc = false;
}
if( type == ftLines)
{
int count = m_lines.Count();
if( count < 1)
{
if( text_log)
text_log->Print( "Line type patetern with no lines.\n");
return false;
}
for( int i = 0; i < count; i++)
{
if( !m_lines[i].IsValid())
{
if( text_log)
text_log->Print( "Line[%d] is not valid.\n", i);
return false;
}
}
return true;
}
return rc;
}
void ON_HatchPattern::Dump( ON_TextLog& dump) const
{
dump.Print( "Hatch pattern - ");
switch( m_type)
{
case ftSolid:
dump.Print( "fill type: Solid");
break;
case ftLines:
dump.Print( "fill type: Lines");
break;
case ftGradient:
dump.Print( "fill type: Gradient");
break;
case ftLast:
// no action, but this keeps gcc happy
break;
}
dump.Print( "\n");
const wchar_t* wsHatchPatternName = m_hatchpattern_name;
if ( 0 == wsHatchPatternName )
wsHatchPatternName = L"";
dump.Print( "Name: %ls\n", wsHatchPatternName);
const wchar_t* wsDescription = m_description;
if ( 0 == wsDescription )
wsDescription = L"";
dump.Print( "Description: %ls\n", wsDescription);
if( m_type == ftLines)
{
int count = m_lines.Count();
dump.Print( "Line count = %d\n", count);
for( int i = 0; i < count; i++)
{
m_lines[i].Dump( dump);
}
dump.Print( "\n");
}
}
ON_BOOL32 ON_HatchPattern::Write( ON_BinaryArchive& ar) const
{
ON_BOOL32 rc = ar.Write3dmChunkVersion(1,2);
if (rc) rc = ar.WriteInt( m_hatchpattern_index);
if (rc) rc = ar.WriteInt( m_type);
if (rc) rc = ar.WriteString( m_hatchpattern_name);
if (rc) rc = ar.WriteString( m_description);
if( rc)
{
if( m_type == ftLines)
{
int i, count = m_lines.Count();
if ( count < 0 )
count = 0;
rc = ar.WriteInt( count );
for( i = 0; i < count && rc; i++)
rc = m_lines[i].Write( ar);
}
}
// version 1.2 field
if (rc) rc = ar.WriteUuid(m_hatchpattern_id);
return rc;
}
ON_BOOL32 ON_HatchPattern::Read( ON_BinaryArchive& ar)
{
m_hatchpattern_index = -1;
memset(&m_hatchpattern_id,0,sizeof(m_hatchpattern_id));
m_type = ftSolid;
m_hatchpattern_name.Empty();
m_description.Empty();
m_lines.Empty();
int i;
int major_version = 0;
int minor_version = 0;
ON_BOOL32 rc = ar.Read3dmChunkVersion( &major_version, &minor_version);
if ( major_version == 1 )
{
if( rc) rc = ar.ReadInt( &m_hatchpattern_index);
i = 0;
if( rc) rc = ar.ReadInt( &i);
if( rc)
{
switch( i)
{
case 0: m_type = ftSolid; break;
case 1: m_type = ftLines; break;
case 2: m_type = ftGradient; break;
default: rc = false; break;
}
}
if( rc) rc = ar.ReadString( m_hatchpattern_name);
if( rc) rc = ar.ReadString( m_description);
if( rc)
{
if( m_type == ftLines)
{
m_lines.Empty();
int count = 0;
rc = ar.ReadInt( &count);
if( rc && count > 0 )
{
m_lines.SetCapacity( count);
int i;
for( i = 0; rc && i < count; i++)
{
ON_HatchLine& line = m_lines.AppendNew();
rc = line.Read( ar);
}
}
}
}
if ( minor_version >= 2 )
{
rc = ar.ReadUuid(m_hatchpattern_id);
}
}
return rc;
}
ON_HatchPattern::eFillType ON_HatchPattern::FillType() const
{
if( m_type >= ftSolid && m_type < ftLast)
return m_type;
return ftLast;
}
void ON_HatchPattern::SetFillType( eFillType type)
{
m_type = type;
}
void ON_HatchPattern::SetName( const wchar_t* pName)
{
m_hatchpattern_name = pName;
m_hatchpattern_name.TrimLeftAndRight();
}
void ON_HatchPattern::SetName( const char* pName)
{
m_hatchpattern_name = pName;
m_hatchpattern_name.TrimLeftAndRight();
}
void ON_HatchPattern::GetName( ON_wString& string) const
{
string = m_hatchpattern_name;
}
const wchar_t* ON_HatchPattern::Name() const
{
return m_hatchpattern_name;
}
void ON_HatchPattern::SetDescription( const wchar_t* pDescription)
{
m_description = pDescription;
}
void ON_HatchPattern::SetDescription( const char* pDescription)
{
m_description = pDescription;
}
void ON_HatchPattern::GetDescription( ON_wString& string) const
{
string = m_description;
}
const wchar_t* ON_HatchPattern::Description() const
{
return m_description;
}
void ON_HatchPattern::SetIndex( int i)
{
m_hatchpattern_index = i;
}
int ON_HatchPattern::Index() const
{
return m_hatchpattern_index;
}
// Line HatchPattern functions
int ON_HatchPattern::HatchLineCount() const
{
return m_lines.Count();
}
int ON_HatchPattern::AddHatchLine( const ON_HatchLine& line)
{
m_lines.Append( line);
return m_lines.Count()-1;
}
const ON_HatchLine* ON_HatchPattern::HatchLine( int index) const
{
if( index >= 0 && index < m_lines.Count())
return &m_lines[index];
else
return NULL;
}
bool ON_HatchPattern::RemoveHatchLine( int index)
{
if( index >= 0 && index < m_lines.Count())
{
m_lines.Remove( index);
return true;
}
return false;
}
void ON_HatchPattern::RemoveAllHatchLines()
{
m_lines.Empty();
}
int ON_HatchPattern::SetHatchLines( const ON_ClassArray<ON_HatchLine> lines)
{
m_lines = lines;
return m_lines.Count();
}
// class ON_HatchLoop
/////////////////////////////////////////////////////////////////
#if defined(ON_DLL_EXPORTS)
// When the Microsoft CRT(s) is/are used, this is the best
// way to prevent crashes that happen when a hatch loop is
// allocated with new in one DLL and deallocated with
// delete in another DLL.
void* ON_HatchLoop::operator new(size_t sz)
{
// ON_HatchLoop new
return onmalloc(sz);
}
void ON_HatchLoop::operator delete(void* p)
{
// ON_HatchLoop delete
onfree(p);
}
void* ON_HatchLoop::operator new[] (size_t sz)
{
// ON_HatchLoop array new
return onmalloc(sz);
}
void ON_HatchLoop::operator delete[] (void* p)
{
// ON_HatchLoop array delete
onfree(p);
}
void* ON_HatchLoop::operator new(size_t, void* p)
{
// ON_HatchLoop placement new
return p;
}
void ON_HatchLoop::operator delete(void*, void*)
{
// ON_HatchLoop placement delete
return;
}
#endif
ON_HatchLoop::ON_HatchLoop()
: m_type( ON_HatchLoop::ltOuter), m_p2dCurve( NULL)
{
}
ON_HatchLoop::ON_HatchLoop( ON_Curve* pCurve2d, eLoopType type)
: m_type( type), m_p2dCurve( pCurve2d)
{
}
ON_HatchLoop::ON_HatchLoop( const ON_HatchLoop& src)
: m_type( src.m_type), m_p2dCurve( NULL)
{
if( src.m_p2dCurve)
m_p2dCurve = src.m_p2dCurve->DuplicateCurve();
}
ON_HatchLoop::~ON_HatchLoop()
{
delete m_p2dCurve;
}
ON_HatchLoop& ON_HatchLoop::operator=( const ON_HatchLoop& src)
{
if( this != &src)
{
if( m_p2dCurve)
delete m_p2dCurve;
m_p2dCurve = src.m_p2dCurve->DuplicateCurve();
m_type = src.m_type;
}
return *this;
}
ON_BOOL32 ON_HatchLoop::IsValid( ON_TextLog* text_log) const
{
ON_BOOL32 rc = m_p2dCurve != NULL;
if( !rc)
{
if( text_log)
text_log->Print( "2d loop curve is NULL\n");
}
if( rc)
{
rc = m_p2dCurve->IsValid( text_log);
if( !rc)
{
if( text_log)
text_log->Print( "Loop curve is not valid\n");
}
}
if( rc)
{
ON_BoundingBox box;
m_p2dCurve->GetBoundingBox( box);
rc = ( box.Max().z == box.Min().z && box.Max().z == 0.0);
if( !rc)
{
if( text_log)
text_log->Print( "2d loop curve has non-zero z coordinates\n");
}
}
if( rc && m_type != ltOuter && m_type != ltInner)
{
if( text_log)
text_log->Print( "Loop type is invalid.\n");
rc = false;
}
return rc;
}
void ON_HatchLoop::Dump( ON_TextLog& dump) const
{
if( m_type == ltOuter)
dump.Print( "Outer hatch loop\n");
if( m_type == ltInner)
dump.Print( "Inner hatch loop\n");
if ( 0 == m_p2dCurve )
{
dump.Print( "2d curve: null pointer\n");
}
else
{
dump.Print( "2d curve:\n");
m_p2dCurve->Dump(dump);
}
}
ON_BOOL32 ON_HatchLoop::Write( ON_BinaryArchive& ar) const
{
ON_BOOL32 rc = ar.Write3dmChunkVersion(1,1);
if( rc) rc = ar.WriteInt( m_type);
if( rc) rc = ar.WriteObject( m_p2dCurve);
return rc;
}
ON_BOOL32 ON_HatchLoop::Read( ON_BinaryArchive& ar)
{
m_type = ltOuter;
delete m_p2dCurve;
m_p2dCurve = NULL;
int major_version = 0;
int minor_version = 0;
ON_BOOL32 rc = ar.Read3dmChunkVersion( &major_version, &minor_version);
if ( major_version == 1 )
{
int type = 0;
if( rc) rc = ar.ReadInt( &type);
if( rc)
{
switch( type)
{
case ltOuter: m_type = ltOuter; break;
case ltInner: m_type = ltInner; break;
default: rc = false; break;
}
}
if( rc)
{
ON_Object* pObj = NULL;
rc = ar.ReadObject( &pObj);
if( pObj)
{
m_p2dCurve = ON_Curve::Cast( pObj);
if( !m_p2dCurve) // read something, but it wasn't right
{
rc = false;
delete pObj;
}
}
}
}
return rc;
}
const ON_Curve* ON_HatchLoop::Curve() const
{
return m_p2dCurve;
}
bool ON_HatchLoop::SetCurve( const ON_Curve& curve)
{
ON_Curve* pC = curve.DuplicateCurve();
if( pC)
{
if(pC->Dimension() == 3 && !pC->ChangeDimension(2))
return false;
if( m_p2dCurve)
delete m_p2dCurve;
m_p2dCurve = pC;
}
return true;
}
ON_HatchLoop::eLoopType ON_HatchLoop::Type() const
{
return m_type;
}
void ON_HatchLoop::SetType( eLoopType type)
{
m_type = type;
}
// class ON_Hatch
/////////////////////////////////////////////////////////////////
ON_OBJECT_IMPLEMENT( ON_Hatch, ON_Geometry, "0559733B-5332-49d1-A936-0532AC76ADE5");
ON_Hatch::ON_Hatch()
: m_pattern_scale( 1.0),
m_pattern_rotation( 0.0),
m_pattern_index( -1)
{
}
ON_Hatch::ON_Hatch( const ON_Hatch& src)
: ON_Geometry(src),
m_plane( src.m_plane),
m_pattern_scale( src.m_pattern_scale),
m_pattern_rotation( src.m_pattern_rotation),
m_pattern_index( src.m_pattern_index)
{
m_loops.Reserve( src.m_loops.Count());
for( int i = 0; i < src.m_loops.Count(); i++)
{
ON_HatchLoop* pL = new ON_HatchLoop( *src.m_loops[i]);
m_loops.Append( pL);
}
}
ON_Hatch& ON_Hatch::operator=( const ON_Hatch& src)
{
if( this != &src)
{
// Nov 3 2004 Dale Lear:
// Delete existing loops so we don't leak the memory;
int i;
for ( i = 0; i < m_loops.Count(); i++ )
{
ON_HatchLoop* pL = m_loops[i];
if ( pL )
{
m_loops[i] = 0;
delete pL;
}
}
m_loops.SetCount(0);
ON_Geometry::operator =(src);
m_plane = src.m_plane;
m_pattern_index = src.m_pattern_index;
m_pattern_scale = src.m_pattern_scale;
m_pattern_rotation = src.m_pattern_rotation;
m_loops.Reserve( src.m_loops.Count());
for( i = 0; i < src.m_loops.Count(); i++)
{
ON_HatchLoop* pL = new ON_HatchLoop( *src.m_loops[i]);
m_loops.Append( pL);
}
}
return *this;
}
ON_Hatch::~ON_Hatch()
{
int i;
for ( i = 0; i < m_loops.Count(); i++ )
{
ON_HatchLoop* pL = m_loops[i];
if ( pL )
{
m_loops[i] = 0;
delete pL;
}
}
}
ON_Hatch* ON_Hatch::DuplicateHatch() const
{
return Duplicate();
}
ON_BOOL32 ON_Hatch::IsValid( ON_TextLog* text_log) const
{
ON_BOOL32 rc = m_plane.IsValid();
if( !rc)
{
if( text_log)
text_log->Print( "Plane is not valid\n");
return false;
}
// 18 June 2012 - Lowell - Added loop self-intersection and
// intersecting other loops tests
int count = m_loops.Count();
for(int i = 0; i < count; i++)
{
if(m_loops[i] == 0)
{
if( text_log)
text_log->Print( "Loop[%d] is NULL\n", i);
return false;
}
if(rc)
rc = m_loops[i]->IsValid( text_log);
if( !rc)
{
if( text_log)
text_log->Print( "Loop[%d] is not valid\n", i);
return false;
}
}
return true;
}
void ON_Hatch::Dump( ON_TextLog& dump) const
{
dump.Print("Hatch: Pattern index: %d\n", PatternIndex());