-
Notifications
You must be signed in to change notification settings - Fork 1
/
PolyLine.cpp
2089 lines (1946 loc) · 48.2 KB
/
PolyLine.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
// PolyLine.cpp ... implementation of CPolyLine class
#include "stdafx.h"
#include "math.h"
#include "PolyLine.h"
#include "FreePcb.h"
#include "utility.h"
#include "layers.h"
#include "php_polygon.h"
#include "php_polygon_vertex.h"
#define pi 3.14159265359
#define DENOM 25400 // to use mils for php clipping
//#define DENOM 1 // to use nm for php clipping
// constructor:
// dl is a pointer to CDisplayList for drawing graphic elements
// if dl = NULL, doesn't draw anything but can still hold data
//
CPolyLine::CPolyLine( CDisplayList * dl )
{
m_dlist = dl;
m_dl_job = NULL;
m_ncorners = 0;
m_ptr = 0;
m_hatch = 0;
m_sel_box = 0;
m_gpc_poly = new gpc_polygon;
m_gpc_poly->num_contours = 0;
m_php_poly = new polygon;
}
// destructor, removes display elements
//
CPolyLine::~CPolyLine()
{
Undraw();
FreeGpcPoly();
delete m_gpc_poly;
delete m_php_poly;
// if( m_closed )
// DeleteObject( m_hrgn );
}
// Use the General Polygon Clipping Library to clip contours
// If this results in new polygons, return them as CArray p
// If bRetainArcs == TRUE, try to retain arcs in polys
// Returns number of external contours, or -1 if error
//
int CPolyLine::NormalizeWithGpc( CArray<CPolyLine*> * pa, BOOL bRetainArcs )
{
CArray<CArc> arc_array;
if( bRetainArcs )
MakeGpcPoly( -1, &arc_array );
else
MakeGpcPoly( -1, NULL );
Undraw();
// now, recreate poly
// first, find outside contours and create new CPolyLines if necessary
int n_ext_cont = 0;
for( int ic=0; ic<m_gpc_poly->num_contours; ic++ )
{
if( !(m_gpc_poly->hole)[ic] )
{
if( n_ext_cont == 0 )
{
// first external contour, replace this poly
corner.RemoveAll();
side_style.RemoveAll();
m_ncorners = 0;
for( int i=0; i<m_gpc_poly->contour[ic].num_vertices; i++ )
{
int x = ((m_gpc_poly->contour)[ic].vertex)[i].x;
int y = ((m_gpc_poly->contour)[ic].vertex)[i].y;
if( i==0 )
Start( m_layer, m_w, m_sel_box, x, y, m_hatch, &m_id, m_ptr );
else
AppendCorner( x, y, STRAIGHT, FALSE );
}
Close();
n_ext_cont++;
}
else if( pa )
{
// next external contour, create new poly
CPolyLine * poly = new CPolyLine;
pa->SetSize(n_ext_cont); // put in array
(*pa)[n_ext_cont-1] = poly;
for( int i=0; i<m_gpc_poly->contour[ic].num_vertices; i++ )
{
int x = ((m_gpc_poly->contour)[ic].vertex)[i].x;
int y = ((m_gpc_poly->contour)[ic].vertex)[i].y;
if( i==0 )
poly->Start( m_layer, m_w, m_sel_box, x, y, m_hatch, &m_id, m_ptr );
else
poly->AppendCorner( x, y, STRAIGHT, FALSE );
}
poly->Close( STRAIGHT, FALSE );
n_ext_cont++;
}
}
}
// now add cutouts to the CPolyLine(s)
for( int ic=0; ic<m_gpc_poly->num_contours; ic++ )
{
if( (m_gpc_poly->hole)[ic] )
{
CPolyLine * ext_poly = NULL;
if( n_ext_cont == 1 )
{
ext_poly = this;
}
else
{
// find the polygon that contains this hole
for( int i=0; i<m_gpc_poly->contour[ic].num_vertices; i++ )
{
int x = ((m_gpc_poly->contour)[ic].vertex)[i].x;
int y = ((m_gpc_poly->contour)[ic].vertex)[i].y;
if( TestPointInside( x, y ) )
ext_poly = this;
else
{
for( int ext_ic=0; ext_ic<n_ext_cont-1; ext_ic++ )
{
if( (*pa)[ext_ic]->TestPointInside( x, y ) )
{
ext_poly = (*pa)[ext_ic];
break;
}
}
}
if( ext_poly )
break;
}
}
if( !ext_poly )
ASSERT(0);
for( int i=0; i<m_gpc_poly->contour[ic].num_vertices; i++ )
{
int x = ((m_gpc_poly->contour)[ic].vertex)[i].x;
int y = ((m_gpc_poly->contour)[ic].vertex)[i].y;
ext_poly->AppendCorner( x, y, STRAIGHT, FALSE );
}
ext_poly->Close( STRAIGHT, FALSE );
}
}
if( bRetainArcs )
RestoreArcs( &arc_array, pa );
FreeGpcPoly();
return n_ext_cont;
}
// make a php_polygon from first contour
int CPolyLine::MakePhpPoly()
{
FreePhpPoly();
polygon test_poly;
int nv = GetContourEnd(0);
for( int iv=0; iv<=nv; iv++ )
{
int x = GetX(iv)/DENOM;
int y = GetY(iv)/DENOM;
m_php_poly->addv( x, y );
}
return 0;
}
void CPolyLine::FreePhpPoly()
{
// delete all vertices
while( m_php_poly->m_cnt > 1 )
{
vertex * fv = m_php_poly->getFirst();
m_php_poly->del( fv->m_nextV );
}
delete m_php_poly->m_first;
m_php_poly->m_first = NULL;
m_php_poly->m_cnt = 0;
}
// Use the php clipping lib to clip this poly against poly
//
void CPolyLine::ClipPhpPolygon( int php_op, CPolyLine * poly )
{
Undraw();
poly->MakePhpPoly();
MakePhpPoly();
polygon * p = m_php_poly->boolean( poly->m_php_poly, php_op );
poly->FreePhpPoly();
FreePhpPoly();
if( p )
{
// now screw with the PolyLine
corner.RemoveAll();
side_style.RemoveAll();
do
{
vertex * v = p->getFirst();
Start( m_layer, m_w, m_sel_box, v->X()*DENOM, v->Y()*DENOM, m_hatch, &m_id, m_ptr );
do
{
vertex * n = v->Next();
AppendCorner( v->X()*DENOM, v->Y()*DENOM );
v = n;
}
while( v->id() != p->getFirst()->id() );
Close();
// p = p->NextPoly();
delete p;
p = NULL;
}
while( p );
}
Draw();
}
// make a gpc_polygon for a closed polyline contour
// approximates arcs with multiple straight-line segments
// if icontour = -1, make polygon with all contours,
// combining intersecting contours if possible
// returns data on arcs in arc_array
//
int CPolyLine::MakeGpcPoly( int icontour, CArray<CArc> * arc_array )
{
if( m_gpc_poly->num_contours )
FreeGpcPoly();
if( !GetClosed() && (icontour == (GetNumContours()-1) || icontour == -1))
return 1; // error
// initialize m_gpc_poly
m_gpc_poly->num_contours = 0;
m_gpc_poly->hole = NULL;
m_gpc_poly->contour = NULL;
int n_arcs = 0;
int first_contour = icontour;
int last_contour = icontour;
if( icontour == -1 )
{
first_contour = 0;
last_contour = GetNumContours() - 1;
}
if( arc_array )
arc_array->SetSize(0);
int iarc = 0;
for( int icont=first_contour; icont<=last_contour; icont++ )
{
// make gpc_polygon for this contour
gpc_polygon * gpc = new gpc_polygon;
gpc->num_contours = 0;
gpc->hole = NULL;
gpc->contour = NULL;
// first, calculate number of vertices in contour
int n_vertices = 0;
int ic_st = GetContourStart(icont);
int ic_end = GetContourEnd(icont);
for( int ic=ic_st; ic<=ic_end; ic++ )
{
int style = side_style[ic];
int x1 = corner[ic].x;
int y1 = corner[ic].y;
int x2, y2;
if( ic < ic_end )
{
x2 = corner[ic+1].x;
y2 = corner[ic+1].y;
}
else
{
x2 = corner[ic_st].x;
y2 = corner[ic_st].y;
}
if( style == STRAIGHT )
n_vertices++;
else
{
// style is ARC_CW or ARC_CCW
int n; // number of steps for arcs
n = (abs(x2-x1)+abs(y2-y1))/(CArc::MAX_STEP);
n = max( n, CArc::MIN_STEPS ); // or at most 5 degrees of arc
n_vertices += n;
n_arcs++;
}
}
// now create gcp_vertex_list for this contour
gpc_vertex_list * g_v_list = new gpc_vertex_list;
g_v_list->vertex = (gpc_vertex*)calloc( sizeof(gpc_vertex), n_vertices );
g_v_list->num_vertices = n_vertices;
int ivtx = 0;
for( int ic=ic_st; ic<=ic_end; ic++ )
{
int style = side_style[ic];
int x1 = corner[ic].x;
int y1 = corner[ic].y;
int x2, y2;
if( ic < ic_end )
{
x2 = corner[ic+1].x;
y2 = corner[ic+1].y;
}
else
{
x2 = corner[ic_st].x;
y2 = corner[ic_st].y;
}
if( style == STRAIGHT )
{
g_v_list->vertex[ivtx].x = x1;
g_v_list->vertex[ivtx].y = y1;
ivtx++;
}
else
{
// style is arc_cw or arc_ccw
int n; // number of steps for arcs
n = (abs(x2-x1)+abs(y2-y1))/(CArc::MAX_STEP);
n = max( n, CArc::MIN_STEPS ); // or at most 5 degrees of arc
double xo, yo, theta1, theta2, a, b;
a = fabs( (double)(x1 - x2) );
b = fabs( (double)(y1 - y2) );
if( style == CPolyLine::ARC_CW )
{
// clockwise arc (ie.quadrant of ellipse)
int i=0, j=0;
if( x2 > x1 && y2 > y1 )
{
// first quadrant, draw second quadrant of ellipse
xo = x2;
yo = y1;
theta1 = pi;
theta2 = pi/2.0;
}
else if( x2 < x1 && y2 > y1 )
{
// second quadrant, draw third quadrant of ellipse
xo = x1;
yo = y2;
theta1 = 3.0*pi/2.0;
theta2 = pi;
}
else if( x2 < x1 && y2 < y1 )
{
// third quadrant, draw fourth quadrant of ellipse
xo = x2;
yo = y1;
theta1 = 2.0*pi;
theta2 = 3.0*pi/2.0;
}
else
{
xo = x1; // fourth quadrant, draw first quadrant of ellipse
yo = y2;
theta1 = pi/2.0;
theta2 = 0.0;
}
}
else
{
// counter-clockwise arc
int i=0, j=0;
if( x2 > x1 && y2 > y1 )
{
xo = x1; // first quadrant, draw fourth quadrant of ellipse
yo = y2;
theta1 = 3.0*pi/2.0;
theta2 = 2.0*pi;
}
else if( x2 < x1 && y2 > y1 )
{
xo = x2; // second quadrant
yo = y1;
theta1 = 0.0;
theta2 = pi/2.0;
}
else if( x2 < x1 && y2 < y1 )
{
xo = x1; // third quadrant
yo = y2;
theta1 = pi/2.0;
theta2 = pi;
}
else
{
xo = x2; // fourth quadrant
yo = y1;
theta1 = pi;
theta2 = 3.0*pi/2.0;
}
}
// now write steps for arc
if( arc_array )
{
arc_array->SetSize(iarc+1);
(*arc_array)[iarc].style = style;
(*arc_array)[iarc].n_steps = n;
(*arc_array)[iarc].xi = x1;
(*arc_array)[iarc].yi = y1;
(*arc_array)[iarc].xf = x2;
(*arc_array)[iarc].yf = y2;
iarc++;
}
for( int is=0; is<n; is++ )
{
double theta = theta1 + ((theta2-theta1)*(double)is)/n;
double x = xo + a*cos(theta);
double y = yo + b*sin(theta);
if( is == 0 )
{
x = x1;
y = y1;
}
g_v_list->vertex[ivtx].x = x;
g_v_list->vertex[ivtx].y = y;
ivtx++;
}
}
}
if( n_vertices != ivtx )
ASSERT(0);
// add vertex_list to gpc
gpc_add_contour( gpc, g_v_list, 0 );
// now clip m_gpc_poly with gpc, put new poly into result
gpc_polygon * result = new gpc_polygon;
if( icontour == -1 && icont != 0 )
gpc_polygon_clip( GPC_DIFF, m_gpc_poly, gpc, result ); // hole
else
gpc_polygon_clip( GPC_UNION, m_gpc_poly, gpc, result ); // outside
// now copy result to m_gpc_poly
gpc_free_polygon( m_gpc_poly );
delete m_gpc_poly;
m_gpc_poly = result;
gpc_free_polygon( gpc );
delete gpc;
free( g_v_list->vertex );
free( g_v_list );
}
return 0;
}
int CPolyLine::FreeGpcPoly()
{
if( m_gpc_poly->num_contours )
{
delete m_gpc_poly->contour->vertex;
delete m_gpc_poly->contour;
delete m_gpc_poly->hole;
}
m_gpc_poly->num_contours = 0;
return 0;
}
// Restore arcs to a polygon where they were replaced with steps
// If pa != NULL, also use polygons in pa array
//
int CPolyLine::RestoreArcs( CArray<CArc> * arc_array, CArray<CPolyLine*> * pa )
{
// get poly info
int n_polys = 1;
if( pa )
n_polys += pa->GetSize();
CPolyLine * poly;
// undraw polys and clear utility flag for all corners
for( int ip=0; ip<n_polys; ip++ )
{
if( ip == 0 )
poly = this;
else
poly = (*pa)[ip-1];
poly->Undraw();
for( int ic=0; ic<poly->GetNumCorners(); ic++ )
poly->SetUtility( ic, 0 ); // clear utility flag
}
// find arcs and replace them
BOOL bFound;
int arc_start;
int arc_end;
for( int iarc=0; iarc<arc_array->GetSize(); iarc++ )
{
int arc_xi = (*arc_array)[iarc].xi;
int arc_yi = (*arc_array)[iarc].yi;
int arc_xf = (*arc_array)[iarc].xf;
int arc_yf = (*arc_array)[iarc].yf;
int n_steps = (*arc_array)[iarc].n_steps;
int style = (*arc_array)[iarc].style;
bFound = FALSE;
// loop through polys
for( int ip=0; ip<n_polys; ip++ )
{
if( ip == 0 )
poly = this;
else
poly = (*pa)[ip-1];
for( int icont=0; icont<poly->GetNumContours(); icont++ )
{
int ic_start = poly->GetContourStart(icont);
int ic_end = poly->GetContourEnd(icont);
if( (ic_end-ic_start) > n_steps )
{
for( int ic=ic_start; ic<=ic_end; ic++ )
{
int ic_next = ic+1;
if( ic_next > ic_end )
ic_next = ic_start;
int xi = poly->GetX(ic);
int yi = poly->GetY(ic);
if( xi == arc_xi && yi == arc_yi )
{
// test for forward arc
int ic2 = ic + n_steps;
if( ic2 > ic_end )
ic2 = ic2 - ic_end + ic_start - 1;
int xf = poly->GetX(ic2);
int yf = poly->GetY(ic2);
if( xf == arc_xf && yf == arc_yf )
{
// arc from ic to ic2
bFound = TRUE;
arc_start = ic;
arc_end = ic2;
}
else
{
// try reverse arc
ic2 = ic - n_steps;
if( ic2 < ic_start )
ic2 = ic2 - ic_start + ic_end + 1;
xf = poly->GetX(ic2);
yf = poly->GetY(ic2);
if( xf == arc_xf && yf == arc_yf )
{
// arc from ic2 to ic
bFound = TRUE;
arc_start = ic2;
arc_end = ic;
style = 3 - style;
}
}
if( bFound )
{
poly->side_style[arc_start] = style;
// mark corners for deletion from arc_start+1 to arc_end-1
for( int i=arc_start+1; i!=arc_end; )
{
if( i > ic_end )
i = ic_start;
poly->SetUtility( i, 1 );
if( i == ic_end )
i = ic_start;
else
i++;
}
break;
}
}
if( bFound )
break;
}
}
if( bFound )
break;
}
}
if( bFound )
(*arc_array)[iarc].bFound = TRUE;
}
// now delete all marked corners
for( int ip=0; ip<n_polys; ip++ )
{
if( ip == 0 )
poly = this;
else
poly = (*pa)[ip-1];
for( int ic=poly->GetNumCorners()-1; ic>=0; ic-- )
{
if( poly->GetUtility(ic) )
poly->DeleteCorner( ic, FALSE );
}
}
return 0;
}
// initialize new polyline
// set layer, width, selection box size, starting point, id and pointer
//
// if sel_box = 0, don't create selection elements at all
//
// if polyline is board outline, enter with:
// id.type = ID_BOARD
// id.st = ID_BOARD_OUTLINE
// id.i = 0
// ptr = NULL
//
// if polyline is copper area, enter with:
// id.type = ID_NET;
// id.st = ID_AREA
// id.i = index to area
// ptr = pointer to net
//
void CPolyLine::Start( int layer, int w, int sel_box, int x, int y, int hatch, id * id, void * ptr )
{
m_layer = layer;
m_w = w;
m_sel_box = sel_box;
if( id )
m_id = *id;
else
m_id.Clear();
m_ptr = ptr;
m_ncorners = 1;
m_hatch = hatch;
corner.SetSize( DEF_SIZE );
side_style.SetSize( DEF_SIZE );
corner[0].x = x;
corner[0].y = y;
corner[0].end_contour = FALSE;
Draw();
}
// add a corner to unclosed polyline
//
void CPolyLine::AppendCorner( int x, int y, int style, BOOL bDraw )
{
Undraw();
// increase size of arrays
if( corner.GetSize() < m_ncorners+1 )
{
corner.SetSize( m_ncorners+DEF_ADD );
side_style.SetSize( m_ncorners+DEF_ADD );
}
// add entries for new corner and side
corner[m_ncorners].x = x;
corner[m_ncorners].y = y;
corner[m_ncorners].end_contour = FALSE;
if( !corner[m_ncorners-1].end_contour )
side_style[m_ncorners-1] = style;
int dl_type;
if( style == CPolyLine::STRAIGHT )
dl_type = DL_LINE;
else if( style == CPolyLine::ARC_CW )
dl_type = DL_ARC_CW;
else if( style == CPolyLine::ARC_CCW )
dl_type = DL_ARC_CCW;
else
ASSERT(0);
m_ncorners++;
if( bDraw )
Draw();
}
// close last polyline contour
//
void CPolyLine::Close( int style, BOOL bDraw )
{
if( GetClosed() )
ASSERT(0);
Undraw();
side_style[m_ncorners-1] = style;
corner[m_ncorners-1].end_contour = TRUE;
corner.SetSize( m_ncorners );
side_style.SetSize( m_ncorners );
if( bDraw )
Draw();
}
// move corner of polyline
//
void CPolyLine::MoveCorner( int ic, int x, int y )
{
Undraw();
corner[ic].x = x;
corner[ic].y = y;
Draw();
}
// delete corner and adjust arrays
//
void CPolyLine::DeleteCorner( int ic, BOOL bDraw )
{
Undraw();
int icont = GetContour( ic );
int istart = GetContourStart( icont );
int iend = GetContourEnd( icont );
BOOL bClosed = icont < GetNumContours()-1 || GetClosed();
if( !bClosed )
{
// open contour, must be last contour
corner.RemoveAt( ic );
if( ic != istart )
side_style.RemoveAt( ic-1 );
m_ncorners--;
}
else
{
// closed contour
corner.RemoveAt( ic );
side_style.RemoveAt( ic );
if( ic == iend )
corner[ic-1].end_contour = TRUE;
m_ncorners--;
}
if( bClosed && GetContourSize(icont) < 3 )
{
// delete the entire contour
RemoveContour( icont );
}
if( bDraw )
Draw();
}
void CPolyLine::RemoveContour( int icont )
{
Undraw();
int istart = GetContourStart( icont );
int iend = GetContourEnd( icont );
if( icont == 0 && GetNumContours() == 1 )
{
// remove the only contour
ASSERT(0);
}
else if( icont == GetNumContours()-1 )
{
// remove last contour
corner.SetSize( GetContourStart(icont) );
side_style.SetSize( GetContourStart(icont) );
m_ncorners -= iend - istart + 1;
}
else
{
// remove closed contour
for( int ic=iend; ic>=istart; ic-- )
{
corner.RemoveAt( ic );
side_style.RemoveAt( ic );
m_ncorners--;
}
}
Draw();
}
// insert a new corner between two existing corners
//
void CPolyLine::InsertCorner( int ic, int x, int y )
{
Undraw();
corner.InsertAt( ic, CPolyPt(x,y) );
side_style.InsertAt( ic, STRAIGHT );
m_ncorners++;
if( ic )
{
if( corner[ic-1].end_contour )
{
// Extend to the new corner
corner[ic-1].end_contour = FALSE;
corner[ic].end_contour = TRUE;
}
}
Draw();
}
// undraw polyline by removing all graphic elements from display list
//
void CPolyLine::Undraw()
{
if( m_dlist && bDrawn )
{
// remove display elements, if present
int i;
for( i=0; i<dl_side .GetSize(); i++ ) dl_side[i]->Remove();
for( i=0; i<dl_side_sel .GetSize(); i++ ) dl_side_sel[i]->Remove();
for( i=0; i<dl_corner_sel.GetSize(); i++ ) dl_corner_sel[i]->Remove();
for( i=0; i<dl_hatch .GetSize(); i++ ) dl_hatch[i]->Remove();
// remove pointers
dl_side.RemoveAll();
dl_side_sel.RemoveAll();
dl_corner_sel.RemoveAll();
dl_hatch.RemoveAll();
delete m_dl_job;
m_dl_job = NULL;
m_nhatch = 0;
}
bDrawn = FALSE;
}
// draw polyline by adding all graphics to display list
// if side style is ARC_CW or ARC_CCW but endpoints are not angled,
// convert to STRAIGHT
//
void CPolyLine::Draw( CDisplayList * dl )
{
int i_start_contour = 0;
// first, undraw if necessary
if( bDrawn )
Undraw();
// use new display list if provided
if( dl )
m_dlist = dl;
if( m_dlist )
{
// set up CArrays
dl_side.SetSize( m_ncorners );
if( m_sel_box )
{
dl_side_sel.SetSize( m_ncorners );
dl_corner_sel.SetSize( m_ncorners );
}
else
{
dl_side_sel.RemoveAll();
dl_corner_sel.RemoveAll();
}
CDL_job *pDL_job;
if (m_layer >= LAY_TOP_COPPER)
{
CDL_job_copper_area *pDL_job_ca = new CDL_job_copper_area(m_dlist, this);
m_dl_job = pDL_job = pDL_job_ca;
}
else
{
pDL_job = m_dlist->GetJob_traces(m_layer);
m_dl_job = NULL;
}
// now draw elements
for( int ic=0; ic<m_ncorners; ic++ )
{
m_id.ii = ic;
int xi = corner[ic].x;
int yi = corner[ic].y;
int xf, yf;
if( corner[ic].end_contour == FALSE && ic < m_ncorners-1 )
{
xf = corner[ic+1].x;
yf = corner[ic+1].y;
}
else
{
xf = corner[i_start_contour].x;
yf = corner[i_start_contour].y;
i_start_contour = ic+1;
}
// draw
if( m_sel_box )
{
m_id.sst = ID_SEL_CORNER;
dl_corner_sel[ic] = m_dlist->AddSelector( m_id, m_ptr, m_layer, DL_HOLLOW_RECT,
1, 0, 0, xi-m_sel_box, yi-m_sel_box,
xi+m_sel_box, yi+m_sel_box, 0, 0 );
}
if( ic<(m_ncorners-1) || corner[ic].end_contour )
{
// draw side
if( xi == xf || yi == yf )
{
// if endpoints not angled, make side STRAIGHT
side_style[ic] = STRAIGHT;
}
int g_type = DL_LINE;
if( side_style[ic] == STRAIGHT )
g_type = DL_LINE;
else if( side_style[ic] == ARC_CW )
g_type = DL_ARC_CW;
else if( side_style[ic] == ARC_CCW )
g_type = DL_ARC_CCW;
m_id.sst = ID_SIDE;
int dx = xf - xi;
int dy = yf - yi;
#if 0 // BAF attempt to move borders in by w [
int w = 10 * PCBU_PER_MIL;
if (abs(dx) == 0)
{
// Vertical
if (dy<0) w = -w;
dl_side[ic] = m_dlist->Add( pDL_job, m_id, m_ptr, m_layer, g_type, 1, m_w+5*PCBU_PER_MIL, 0, 0, xi+w, yi, xf+w, yf, 0, 0 );
}
else if (abs(dy) == 0)
{
if (dx<0) w = -w;
dl_side[ic] = m_dlist->Add( pDL_job, m_id, m_ptr, m_layer, g_type, 1, m_w+5*PCBU_PER_MIL, 0, 0, xi, yi-w, xf, yf-w, 0, 0 );
}
else
{
double s, l;
if (abs(dy) > abs(dx))
{
s = double(dx)/dy;
l = double(w) / sqrt(1 + s*s);
if (dy<0) l = -l;
dl_side[ic] = m_dlist->Add( pDL_job, m_id, m_ptr, m_layer, g_type, 1, m_w+5*PCBU_PER_MIL, 0, 0, xi+l, yi-s*l, xf+l, yf-s*l, 0, 0 );
}
else
{
s = double(dy)/dx;
l = double(w) / sqrt(1 + s*s);
if (dx<0) l = -l;
dl_side[ic] = m_dlist->Add( pDL_job, m_id, m_ptr, m_layer, g_type, 1, m_w+5*PCBU_PER_MIL, 0, 0, xi+s*l, yi-l, xf+s*l, yf-l, 0, 0 );
}
}
#endif // ]
dl_side[ic] = m_dlist->Add( pDL_job, m_id, m_ptr, m_layer, g_type, 1, m_w+0*PCBU_PER_MIL, 0, 0, xi, yi, xf, yf, 0, 0 );
if( m_sel_box )
{
m_id.sst = ID_SEL_SIDE;
dl_side_sel[ic] = m_dlist->AddSelector( m_id, m_ptr, m_layer, g_type, 1, m_w, 0, xi, yi, xf, yf, 0, 0 );
}
}
}
if( m_hatch )
Hatch(pDL_job);
m_dlist->Add(pDL_job, m_layer);
}
bDrawn = TRUE;
}
void CPolyLine::SetSideVisible( int is, int visible )
{
if( m_dlist && dl_side.GetSize() > is )
{
m_dlist->Set_visible( dl_side[is], visible );
}
}
// start dragging new corner to be inserted into side, make side and hatching invisible
//
void CPolyLine::StartDraggingToInsertCorner( CDC * pDC, int ic, int x, int y, int crosshair )
{
if( !m_dlist )
ASSERT(0);
int icont = GetContour( ic );
int istart = GetContourStart( icont );
int iend = GetContourEnd( icont );
int post_c;
if( ic == iend )
post_c = istart;
else
post_c = ic + 1;
int xi = corner[ic].x;
int yi = corner[ic].y;
int xf = corner[post_c].x;
int yf = corner[post_c].y;
m_dlist->StartDraggingLineVertex( pDC, x, y, xi, yi, xf, yf,
LAY_SELECTION, LAY_SELECTION, 1, 1, DSS_STRAIGHT, DSS_STRAIGHT,
0, 0, 0, 0, crosshair );
m_dlist->CancelHighLight();
m_dlist->Set_visible( dl_side[ic], 0 );
for( int ih=0; ih<m_nhatch; ih++ )
m_dlist->Set_visible( dl_hatch[ih], 0 );
}
// cancel dragging inserted corner, make side and hatching visible again
//
void CPolyLine::CancelDraggingToInsertCorner( int ic )
{
if( !m_dlist )
ASSERT(0);
int post_c;
if( ic == (m_ncorners-1) )
post_c = 0;
else