-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDlgLibraryManager.cpp
1006 lines (957 loc) · 34.3 KB
/
DlgLibraryManager.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
// DlgLibraryManager.cpp : implementation file
//
#include "stdafx.h"
#include <math.h>
#include "FreePcb.h"
#include "DlgLibraryManager.h"
#include "cpdflib.h"
#include "DlgLog.h"
#include "PathDialog.h"
// CDlgLibraryManager dialog
IMPLEMENT_DYNAMIC(CDlgLibraryManager, CDialog)
CDlgLibraryManager::CDlgLibraryManager(CWnd* pParent /*=NULL*/)
: CDialog(CDlgLibraryManager::IDD, pParent)
{
}
CDlgLibraryManager::~CDlgLibraryManager()
{
}
void CDlgLibraryManager::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT_MGR_FOLDER, m_edit_footlib);
DDX_Control(pDX, IDC_COMBO_MGR_FILE, m_combo_libfile);
DDX_Control(pDX, IDC_COMBO_PAGE_SIZE, m_combo_page_size);
DDX_Control(pDX, IDC_COMBO_UNITS, m_combo_units);
if( !pDX->m_bSaveAndValidate )
{
// incoming
CString folder_path = *m_foldermap->GetLastFolder();
m_footlib = m_foldermap->GetFolder( &folder_path, m_dlg_log );
m_edit_footlib.SetWindowText( *m_footlib->GetFullPath() );
for( int i=0; i<m_footlib->GetNumLibs(); i++ )
m_combo_libfile.InsertString( i, *m_footlib->GetLibraryFullPath(i) );
m_combo_libfile.InsertString( m_footlib->GetNumLibs(), "*** all library files ***" );
m_combo_libfile.SetCurSel( 0 );
m_combo_page_size.InsertString( PG_LETTER, "letter (8.5 x 11.0 in)" );
m_combo_page_size.InsertString( PG_A4, "A4 (210 x 297 mm)" );
m_combo_page_size.SetCurSel( m_page_sel );
m_combo_units.InsertString( U_NATIVE, "native for footprint" );
m_combo_units.InsertString( U_MM, "mm" );
m_combo_units.InsertString( U_MIL, "mils" );
m_combo_units.InsertString( U_MM_MIL, "mm and mils" );
m_combo_units.SetCurSel( m_units_sel );
}
}
BEGIN_MESSAGE_MAP(CDlgLibraryManager, CDialog)
ON_BN_CLICKED(IDC_BUTTON_MAKE_PDF, OnBnClickedButtonMakePdf)
ON_BN_CLICKED(IDC_BUTTON_MGR_BROWSE, OnBnClickedButtonMgrBrowse)
END_MESSAGE_MAP()
// CDlgLibraryManager message handlers
void CDlgLibraryManager::OnBnClickedButtonMakePdf()
{
#if 0
//** temporarily disabled
AfxMessageBox( "Sorry, this function is currently disabled" );
return;
#endif
// set page size
double PageWidth;
double PageHeight;
m_page_sel = m_combo_page_size.GetCurSel();
switch( m_page_sel )
{
case PG_LETTER: PageWidth = 8.5; PageHeight = 11.5; break; // letter size
case PG_A4: PageWidth = 210.0/25.4; PageHeight = 297.0/25.4; break; // A4
default: ASSERT(0); break; // illegal
}
// set format for dimensional units
int format;
m_units_sel = m_combo_units.GetCurSel();
switch( m_units_sel )
{
case U_NATIVE: format = NATIVE; break; // use native units for each footprint
case U_MM: format = MM; break; // use mm
case U_MIL: format = MIL; break; // use mils
case U_MM_MIL: format = MM_MIL; break; // use both mm and mils
default: ASSERT(0); break; // illegal
}
// magic constant for converting ellipses to Bezier curves
const double MagicBezier = 0.2761423749154;
// formatting constants
const double LeftMargin = 0.75; // left margin width
const double RightMargin = 0.75; // right margin width
const double TopMargin = 0.75; // top margin height
const double BottomMargin = 0.75; // bottom margin height
const double TitleHeightPts = 12.0; // size of title font
const double TitleAboveLine = 0.1; // spacing of title above header line
const double PageTitleLineY = PageHeight - TopMargin - TitleHeightPts/72.0 - TitleAboveLine; // height of header line
const double NameHeightPts = 13.0; // size of footprint name font
const double NameLeadingPts = 30.0; // line spacing of footprint name font
const double TextHeightPts = 10.0; // size of font for general text
const double TextLeadingPts = 14.0; // line spacing of general text font
const double TableTextHeightPts = 10.0; // size of font for table text
const double TableTextLeadingPts = 14.0; // line spacing of table text
const double TableTextOffsetY = 0.05; // spacing of table text above bottom cell border
const double TableTypeX = LeftMargin + 0.7; // position of border between "PIN NUM" and "TYPE"
const double TableShapeX = TableTypeX + 0.7;// position of border between "TYPE and "SHAPE"
const double TableSizeX = TableShapeX + 0.7;// position of border between "SHAPE and "SIZE"
const double TableHoleX = TableSizeX + 1.4; // position of border between "SIZE and "HOLE"
const double TableEndX = TableHoleX + 0.7; // position of right border of table
const double FootprintSpaceY = 0.2; // spacing between scale text and footprint drawing
int ilib = m_combo_libfile.GetCurSel();
int first_ilib = ilib;
int last_ilib = ilib;
// pop up log dialog
m_dlg_log->ShowWindow( SW_SHOW );
m_dlg_log->UpdateWindow();
m_dlg_log->BringWindowToTop();
m_dlg_log->Clear();
m_dlg_log->UpdateWindow();
if( ilib == m_footlib->GetNumLibs() )
{
// do all libraries
first_ilib = 0;
last_ilib = ilib - 1;
}
CPDFdoc *pdf;
float y;
for( ilib=first_ilib; ilib<=last_ilib; ilib++ )
{
// make PDF file
CString title_str = *m_footlib->GetLibraryFullPath( ilib );
title_str = title_str.Left( title_str.GetLength() - 4 );
title_str = title_str + ".pdf";
// post message to log, if it exists
if( m_dlg_log )
{
CString log_message;
log_message.Format( "Creating file: \"%s\"\r\n", title_str );
m_dlg_log->AddLine( log_message );
}
/* == Initialization == */
pdf = cpdf_open(0, NULL);
cpdf_init(pdf);
int pagenum = 1;
int n_footprints = 6;
BOOL new_page = TRUE;
y = PageTitleLineY;
// init first page
if( m_page_sel == PG_LETTER )
cpdf_pageInit(pdf, pagenum, PORTRAIT, LETTER, LETTER); /* page orientation */
else if( m_page_sel == PG_A4 )
cpdf_pageInit(pdf, pagenum, PORTRAIT, A4, A4); /* page orientation */
BOOL new_page_init = TRUE;
// now loop through all headings
for( int i=0; i<m_footlib->GetNumFootprints(ilib) ; i++ )
{
// get next footprint
CShape foot;
int err = foot.MakeFromFile( NULL, *m_footlib->GetFootprintName( ilib, i ),
*m_footlib->GetLibraryFullPath(ilib), m_footlib->GetFootprintOffset( ilib, i ) );
if( err )
ASSERT(0);
// make array of padstack info
int num_padinfo = 0;
CArray<int> start_num;
CArray<int> num_same;
CArray<int> pad_shape;
CArray<int> pad_w;
CArray<int> pad_l;
CArray<int> pad_hole;
for( int ip=0; ip<foot.m_padstack.GetSize(); ip++ )
{
int this_pad_shape = foot.m_padstack[ip].top.shape;
int this_pad_w = foot.m_padstack[ip].top.size_h;
int this_pad_l = 0;
if( this_pad_shape == PAD_RECT || this_pad_shape == PAD_RRECT || this_pad_shape == PAD_OVAL )
this_pad_l = foot.m_padstack[ip].top.size_l + foot.m_padstack[ip].top.size_r;
int this_pad_hole = foot.m_padstack[ip].hole_size;
// now see if we need a new line in the pad table
BOOL new_pad_line = FALSE;
if( num_padinfo == 0 )
{
new_pad_line = TRUE;
}
else
{
if( this_pad_shape != pad_shape[num_padinfo-1]
|| this_pad_w != pad_w[num_padinfo-1]
|| this_pad_l != pad_l[num_padinfo-1]
|| this_pad_hole != pad_hole[num_padinfo-1] )
new_pad_line = TRUE;
}
if( new_pad_line && num_padinfo )
{
// finish last info line
int the_num_same = ip - start_num[num_padinfo-1] + 1;
num_same.Add( the_num_same );
}
if( new_pad_line )
{
// start new info line
start_num.Add( ip+1 );
pad_shape.Add( this_pad_shape );
pad_w.Add( this_pad_w );
pad_l.Add( this_pad_l );
pad_hole.Add( this_pad_hole );
num_padinfo = pad_shape.GetSize();
}
if( ip == (foot.m_padstack.GetSize()-1) )
{
// last pin, finish last info line
int the_num_same = ip - start_num[num_padinfo-1] + 2;
num_same.Add( the_num_same );
}
}
// now remove any lines for non-existent pads (i.e. pad shape = PAD_NONE and hole_size = 0)
for( int i=num_same.GetSize()-1; i>=0; i-- )
{
if( pad_shape[i] == PAD_NONE && pad_hole[i] == 0 )
{
start_num.RemoveAt(i);
num_same.RemoveAt(i);
pad_shape.RemoveAt(i);
pad_w.RemoveAt(i);
pad_l.RemoveAt(i);
pad_hole.RemoveAt(i);
}
}
num_padinfo = pad_shape.GetSize();
// now split author, source and description strings into 2 lines if needed
CString author_1;
CString author_2;
CString source_1;
CString source_2;
CString desc_1;
CString desc_2;
cpdf_setFont(pdf, "Times-Italic", "MacRomanEncoding", TextHeightPts);
double start_x_rel = cpdf_stringWidth( pdf, (const unsigned char*)(LPCSTR)"Author: " )/72.0;
double max_len = TableEndX - LeftMargin - start_x_rel;
cpdf_setFont(pdf, "Times-Roman", "MacRomanEncoding", TextHeightPts);
double str_len = cpdf_stringWidth( pdf, (const unsigned char*)(LPCSTR)foot.m_author )/72.0;
if( str_len > max_len )
{
// text string too long, need to break it up at a space
int isp = 0;
int last_isp;
while( max_len > cpdf_stringWidth( pdf, (const unsigned char*)(LPCSTR)foot.m_author.Left(isp) )/72.0
&& isp != -1 )
{
last_isp = isp+1;
isp = foot.m_author.Find( " ", last_isp );
}
author_1 = foot.m_author.Left( last_isp-1 );
author_2 = foot.m_author.Right( foot.m_author.GetLength() - last_isp );
}
else
{
author_1 = foot.m_author;
author_2 = "";
}
cpdf_setFont(pdf, "Times-Italic", "MacRomanEncoding", TextHeightPts);
start_x_rel = cpdf_stringWidth( pdf, (const unsigned char*)(LPCSTR)"Source: " )/72.0;
max_len = TableEndX - LeftMargin - start_x_rel;
cpdf_setFont(pdf, "Times-Roman", "MacRomanEncoding", TextHeightPts);
str_len = cpdf_stringWidth( pdf, (const unsigned char*)(LPCSTR)foot.m_source )/72.0;
if( str_len > max_len )
{
// text string too long, need to break it up at a space
int isp = 0;
int last_isp;
while( max_len > cpdf_stringWidth( pdf, (const unsigned char*)(LPCSTR)foot.m_source.Left(isp) )/72.0
&& isp != -1 )
{
last_isp = isp+1;
isp = foot.m_source.Find( " ", last_isp );
}
source_1 = foot.m_source.Left( last_isp-1 );
source_2 = foot.m_source.Right( foot.m_source.GetLength() - last_isp );
}
else
{
source_1 = foot.m_source;
source_2 = "";
}
cpdf_setFont(pdf, "Times-Italic", "MacRomanEncoding", TextHeightPts);
start_x_rel = cpdf_stringWidth( pdf, (const unsigned char*)(LPCSTR)"Description: " )/72.0;
max_len = TableEndX - LeftMargin - start_x_rel;
cpdf_setFont(pdf, "Times-Roman", "MacRomanEncoding", TextHeightPts);
str_len = cpdf_stringWidth( pdf, (const unsigned char*)(LPCSTR)foot.m_desc )/72.0;
if( str_len > max_len )
{
// text string too long, need to break it up at a space
int isp = 0;
int last_isp;
while( max_len > cpdf_stringWidth( pdf, (const unsigned char*)(LPCSTR)foot.m_desc.Left(isp) )/72.0
&& isp != -1 )
{
last_isp = isp+1;
isp = foot.m_desc.Find( " ", last_isp );
}
desc_1 = foot.m_desc.Left( last_isp-1 );
desc_2 = foot.m_desc.Right( foot.m_desc.GetLength() - last_isp );
}
else
{
desc_1 = foot.m_desc;
desc_2 = "";
}
// get height rquired for footprint info and see if we need a new page
int npadlines = num_padinfo;
float foot_height = NameLeadingPts/72.0 + (2+npadlines)*TableTextLeadingPts/72.0
+ 2*TextLeadingPts/72.0;
if( author_1 != "" )
foot_height += TextLeadingPts/72.0;
if( author_2 != "" )
foot_height += TextLeadingPts/72.0;
if( source_1 != "" )
foot_height += TextLeadingPts/72.0;
if( source_2 != "" )
foot_height += TextLeadingPts/72.0;
if( desc_1 != "" )
foot_height += TextLeadingPts/72.0;
if( desc_2 != "" )
foot_height += TextLeadingPts/72.0;
if( (y - foot_height) < BottomMargin )
{
// new page
pagenum++;
new_page = TRUE;
new_page_init = FALSE;
}
if( new_page )
{
// new page
if( !new_page_init )
{
if( m_page_sel == PG_LETTER )
cpdf_pageInit(pdf, pagenum, PORTRAIT, LETTER, LETTER);
else if( m_page_sel == PG_A4 )
cpdf_pageInit(pdf, pagenum, PORTRAIT, A4, A4);
new_page_init = TRUE;
}
CString pagenum_str;
pagenum_str.Format( "page %d", pagenum );
// draw header
cpdf_setlinecap( pdf, 1 ); // round end-caps
cpdf_setrgbcolorStroke(pdf, 0.0, 0.0, 0.0); /* black as stroke color */
cpdf_setgrayFill(pdf, 0.0); /* Black */
cpdf_moveto( pdf, LeftMargin, PageTitleLineY );
cpdf_lineto( pdf, PageWidth-RightMargin, PageTitleLineY );
cpdf_stroke( pdf );
cpdf_beginText(pdf, 0);
cpdf_setFont(pdf, "Times-Italic", "MacRomanEncoding", TitleHeightPts);
cpdf_text(pdf, LeftMargin, PageTitleLineY + TitleAboveLine, 0.0, title_str);
cpdf_textAligned( pdf, PageWidth-RightMargin, PageTitleLineY + TitleAboveLine,
0.0, TEXTPOS_LR, pagenum_str );
cpdf_endText(pdf);
y = PageTitleLineY;
new_page = FALSE;
}
// get bounds for drawing footprint later
float top_y = y - NameLeadingPts/72.0 - FootprintSpaceY;
float right_x = PageWidth - RightMargin;
// draw footprint info
// decide on units to use (if format == MM_MIL, use both mm and mil)
int units;
units = foot.m_units; // native units for footprint
if( units == NM )
units = MM;
if( format == MM )
units = MM; // use mm
else if( format == MIL )
units = MIL; // use mil
// draw footprint name
cpdf_beginText(pdf, 0);
cpdf_setTextPosition( pdf, LeftMargin, y );
cpdf_setTextLeading( pdf, NameLeadingPts );
cpdf_setFont(pdf, "Times-Bold", "MacRomanEncoding", NameHeightPts);
cpdf_textCRLFshow(pdf, foot.m_name );
y -= NameLeadingPts/72.0;
cpdf_setTextLeading( pdf, TextLeadingPts );
if( author_1 != "" )
{
cpdf_setFont(pdf, "Times-Italic", "MacRomanEncoding", TextHeightPts);
cpdf_textCRLFshow(pdf, "Author: " );
cpdf_setFont(pdf, "Times-Roman", "MacRomanEncoding", TextHeightPts);
cpdf_textShow(pdf, author_1 );
y -= TextLeadingPts/72.0;
if( author_2 != "" )
{
cpdf_textCRLFshow(pdf, author_2 );
y -= TextLeadingPts/72.0;
}
}
if( source_1 != "" )
{
cpdf_setFont(pdf, "Times-Italic", "MacRomanEncoding", TextHeightPts);
cpdf_textCRLFshow(pdf, "Source: " );
cpdf_setFont(pdf, "Times-Roman", "MacRomanEncoding", TextHeightPts);
cpdf_textShow(pdf, source_1 );
y -= TextLeadingPts/72.0;
if( source_2 != "" )
{
cpdf_textCRLFshow(pdf, source_2 );
y -= TextLeadingPts/72.0;
}
}
if( desc_1 != "" )
{
cpdf_setFont(pdf, "Times-Italic", "MacRomanEncoding", TextHeightPts);
cpdf_textCRLFshow(pdf, "Description: " );
cpdf_setFont(pdf, "Times-Roman", "MacRomanEncoding", TextHeightPts);
cpdf_textShow(pdf, desc_1 );
y -= TextLeadingPts/72.0;
if( desc_2 != "" )
{
cpdf_textCRLFshow(pdf, desc_2 );
y -= TextLeadingPts/72.0;
}
}
cpdf_setFont(pdf, "Times-Italic", "MacRomanEncoding", TextHeightPts);
if( format == MM_MIL )
cpdf_textCRLFshow(pdf, "Size ( mm[mil] ): " );
else
cpdf_textCRLFshow(pdf, "Size: " );
y -= TextLeadingPts/72.0;
cpdf_setFont(pdf, "Times-Roman", "MacRomanEncoding", TextHeightPts);
CString x_str;
CString y_str;
CRect br = foot.GetCornerBounds();
if( format == MM_MIL )
{
::MakeCStringFromDimension( &x_str, br.right-br.left, MM, FALSE, FALSE, FALSE, 2 );
::MakeCStringFromDimension( &y_str, br.top-br.bottom, MM, FALSE, FALSE, FALSE, 2 );
cpdf_textShow( pdf, x_str + " x " + y_str + " [" );
::MakeCStringFromDimension( &x_str, br.right-br.left, MIL, FALSE, FALSE, FALSE, 0 );
::MakeCStringFromDimension( &y_str, br.top-br.bottom, MIL, FALSE, FALSE, FALSE, 0 );
cpdf_textShow( pdf, x_str + " x " + y_str + "]" );
}
else
{
if( units == MM )
{
::MakeCStringFromDimension( &x_str, br.right-br.left, units, FALSE, FALSE, FALSE, 2 );
::MakeCStringFromDimension( &y_str, br.top-br.bottom, units, TRUE, TRUE, TRUE, 2 );
}
else
{
::MakeCStringFromDimension( &x_str, br.right-br.left, units, FALSE, FALSE, FALSE, 0 );
::MakeCStringFromDimension( &y_str, br.top-br.bottom, units, TRUE, TRUE, TRUE, 0 );
}
cpdf_textShow( pdf, x_str + " x " + y_str );
}
if( foot.GetNumPins() == 2 && foot.m_padstack[0].hole_size != 0 )
{
CString pin_spacing_str;
double pin_spacing;
double dx = foot.m_padstack[0].x_rel - foot.m_padstack[1].x_rel;
double dy = foot.m_padstack[0].y_rel - foot.m_padstack[1].y_rel;
if( dx == 0.0 )
pin_spacing = fabs(dy);
else if( dy == 0.0 )
pin_spacing = fabs(dx);
else
pin_spacing = sqrt(dx*dx + dy*dy);
cpdf_setFont(pdf, "Times-Italic", "MacRomanEncoding", TextHeightPts);
cpdf_textShow( pdf, " Pin spacing: " );
cpdf_setFont(pdf, "Times-Roman", "MacRomanEncoding", TextHeightPts);
if( format == MM_MIL )
{
CString mil_str;
::MakeCStringFromDimension( &pin_spacing_str, pin_spacing, MM, FALSE, FALSE, FALSE, 2 );
::MakeCStringFromDimension( &mil_str, pin_spacing, MIL, FALSE, FALSE, FALSE, 2 );
pin_spacing_str += " [" + mil_str + "]";
}
else
{
::MakeCStringFromDimension( &pin_spacing_str, pin_spacing, units,
TRUE, TRUE, TRUE, 2 );
}
cpdf_textShow( pdf, pin_spacing_str );
}
cpdf_textCRLF(pdf);
y -= TextLeadingPts/72.0;
cpdf_endText(pdf);
// draw pad table
float y_line1 = y;
cpdf_setlinewidth( pdf, 1 );
cpdf_setlinecap( pdf, 1 ); // round end-caps
cpdf_setrgbcolorStroke(pdf, 0.0, 0.0, 0.0); /* black as stroke color */
cpdf_moveto( pdf, LeftMargin, y );
cpdf_lineto( pdf, TableEndX, y );
cpdf_stroke( pdf );
y -= TableTextLeadingPts/72.0;
float y_line2 = y;
cpdf_beginText( pdf, 0 );
cpdf_textAligned( pdf, (LeftMargin+TableTypeX)/2, y_line2, 0, TEXTPOS_MM, "PIN(s)" );
cpdf_textAligned( pdf, (TableTypeX+TableEndX)/2, y_line2+TableTextOffsetY, 0, TEXTPOS_LM, "PAD" );
cpdf_endText( pdf );
cpdf_moveto( pdf, TableTypeX, y );
cpdf_lineto( pdf, TableEndX, y );
cpdf_stroke( pdf );
y -= TableTextLeadingPts/72.0;
float y_line3 = y;
cpdf_beginText( pdf, 0 );
cpdf_textAligned( pdf, (TableTypeX+TableShapeX)/2, y_line3+TableTextOffsetY, 0, TEXTPOS_LM, "TYPE" );
cpdf_textAligned( pdf, (TableShapeX+TableSizeX)/2, y_line3+TableTextOffsetY, 0, TEXTPOS_LM, "SHAPE" );
cpdf_textAligned( pdf, (TableSizeX+TableHoleX)/2, y_line3+TableTextOffsetY, 0, TEXTPOS_LM, "SIZE" );
cpdf_textAligned( pdf, (TableHoleX+TableEndX)/2, y_line3+TableTextOffsetY, 0, TEXTPOS_LM, "HOLE" );
cpdf_endText( pdf );
cpdf_moveto( pdf, LeftMargin, y );
cpdf_lineto( pdf, TableEndX, y );
cpdf_stroke( pdf );
for( int i=0; i<npadlines; i++ )
{
CString pin_str;
if( num_same[i] > 1 )
pin_str.Format( "%d-%d", start_num[i], start_num[i]+num_same[i]-1 );
else
pin_str.Format( "%d", start_num[i] );
CString type_str;
if( pad_hole[i] == 0 )
type_str = "SMT";
else
type_str = "TH";
CString shape_str;
if( pad_shape[i] == PAD_NONE )
shape_str = "None";
else if( pad_shape[i] == PAD_ROUND )
shape_str = "Round";
else if( pad_shape[i] == PAD_SQUARE )
shape_str = "Square";
else if( pad_shape[i] == PAD_RECT )
shape_str = "Rect";
else if( pad_shape[i] == PAD_RRECT )
shape_str = "RndRect";
else if( pad_shape[i] == PAD_OVAL )
shape_str = "Oval";
else if( pad_shape[i] == PAD_OCTAGON )
shape_str = "Octagon";
else
ASSERT(0);
CString size_str;
if( pad_shape[i] == PAD_NONE )
size_str = "---";
else if( pad_shape[i] == PAD_RECT || pad_shape[i] == PAD_RRECT || pad_shape[i] == PAD_OVAL )
{
if( format == MM_MIL )
{
CString dim_str;
MakeCStringFromDimension( &size_str, pad_l[i], MM, FALSE, FALSE, FALSE, 2 );
MakeCStringFromDimension( &dim_str, pad_w[i], MM, FALSE, FALSE, FALSE, 2 );
size_str += " x " + dim_str + " [";
MakeCStringFromDimension( &dim_str, pad_l[i], MIL, FALSE, FALSE, FALSE, 1 );
size_str += dim_str;
MakeCStringFromDimension( &dim_str, pad_w[i], MIL, FALSE, FALSE, FALSE, 1 );
size_str += " x " + dim_str + "]";
}
else
{
CString wid_str;
MakeCStringFromDimension( &size_str, pad_l[i], units, FALSE, FALSE, FALSE, 2 );
MakeCStringFromDimension( &wid_str, pad_w[i], units, TRUE, TRUE, TRUE, 2 );
size_str = size_str + " x " + wid_str;
}
}
else
{
if( format == MM_MIL )
{
CString mil_str;
MakeCStringFromDimension( &size_str, pad_w[i], MM, FALSE, FALSE, FALSE, 2 );
MakeCStringFromDimension( &mil_str, pad_w[i], MIL, FALSE, FALSE, FALSE, 1 );
size_str += " [" + mil_str + "]";
}
else
{
MakeCStringFromDimension( &size_str, pad_w[i], units, TRUE, TRUE, TRUE, 2 );
}
}
size_str.MakeLower();
CString hole_str;
if( pad_hole[i] )
{
if( format == MM_MIL )
{
CString mil_str;
MakeCStringFromDimension( &hole_str, pad_hole[i], MM, FALSE, FALSE, FALSE, 2 );
MakeCStringFromDimension( &mil_str, pad_hole[i], MIL, FALSE, FALSE, FALSE, 1 );
hole_str += " [" + mil_str + "]";
}
else
{
MakeCStringFromDimension( &hole_str, pad_hole[i], units, TRUE, TRUE, TRUE, 2 );
}
}
else
hole_str = "---";
y -= TableTextLeadingPts/72.0;
cpdf_moveto( pdf, LeftMargin, y );
cpdf_lineto( pdf, TableEndX, y );
cpdf_stroke( pdf );
cpdf_beginText( pdf, 0 );
cpdf_textAligned( pdf, (LeftMargin+TableTypeX)/2, y+TableTextOffsetY, 0, TEXTPOS_LM, pin_str );
cpdf_textAligned( pdf, (TableTypeX+TableShapeX)/2, y+TableTextOffsetY, 0, TEXTPOS_LM, type_str );
cpdf_textAligned( pdf, (TableShapeX+TableSizeX)/2, y+TableTextOffsetY, 0, TEXTPOS_LM, shape_str );
cpdf_textAligned( pdf, (TableSizeX+TableHoleX)/2, y+TableTextOffsetY, 0, TEXTPOS_LM, size_str );
cpdf_textAligned( pdf, (TableHoleX+TableEndX)/2, y+TableTextOffsetY, 0, TEXTPOS_LM, hole_str );
cpdf_endText( pdf );
}
float y_bottom = y;
cpdf_moveto( pdf, LeftMargin, y );
cpdf_lineto( pdf, TableEndX, y );
cpdf_stroke( pdf );
cpdf_moveto( pdf, LeftMargin, y_line1 );
cpdf_lineto( pdf, LeftMargin, y_bottom );
cpdf_stroke( pdf );
cpdf_moveto( pdf, TableTypeX, y_line1 );
cpdf_lineto( pdf, TableTypeX, y_bottom );
cpdf_stroke( pdf );
cpdf_moveto( pdf, TableShapeX, y_line2 );
cpdf_lineto( pdf, TableShapeX, y_bottom );
cpdf_stroke( pdf );
cpdf_moveto( pdf, TableSizeX, y_line2 );
cpdf_lineto( pdf, TableSizeX, y_bottom );
cpdf_stroke( pdf );
cpdf_moveto( pdf, TableHoleX, y_line2 );
cpdf_lineto( pdf, TableHoleX, y_bottom );
cpdf_stroke( pdf );
cpdf_moveto( pdf, TableEndX, y_line1 );
cpdf_lineto( pdf, TableEndX, y_bottom );
cpdf_stroke( pdf );
y = y_bottom;
// get rest of footprint space boundaries
float bottom_y = y_bottom;
float left_x = TableEndX + 0.5;
// get bounding rectangle and convert to inches
const float NM_PER_INCH = 25400000.0;
CRect foot_r = foot.GetBounds();
float foot_left = foot_r.left/NM_PER_INCH;
float foot_right = foot_r.right/NM_PER_INCH;
float foot_top = foot_r.top/NM_PER_INCH;
float foot_bottom = foot_r.bottom/NM_PER_INCH;
// get scale factor
float x_scale = (right_x - left_x)/(foot_right - foot_left);
float y_scale = (top_y - bottom_y )/(foot_top - foot_bottom);
float scale;
if( x_scale > 2.0 && y_scale > 2.0 )
scale = 2.0;
else if( x_scale > 1.0 && y_scale > 1.0 )
scale = 1.0;
else if( x_scale > 0.5 && y_scale > 0.5 )
scale = 0.5;
else if( x_scale > 0.25 && y_scale > 0.25 )
scale = 0.25;
else if( x_scale > 0.125 && y_scale > 0.125 )
scale = 0.125;
else if( x_scale > 0.0625 && y_scale > 0.0625 )
scale = 0.0625;
else if( x_scale > 0.03125 && y_scale > 0.03125 )
scale = 0.03125;
else if( x_scale > 0.015625 && y_scale > 0.015625 )
scale = 0.015625;
else
ASSERT(0);
// draw scale
float middle_x = (left_x + right_x)/2.0;
float middle_y = (bottom_y + top_y)/2.0;
cpdf_beginText( pdf, 0 );
if( scale == 8.0 )
cpdf_textAligned( pdf, middle_x, top_y + FootprintSpaceY, 0.0, TEXTPOS_LM, "Scale 8:1" );
if( scale == 4.0 )
cpdf_textAligned( pdf, middle_x, top_y + FootprintSpaceY, 0.0, TEXTPOS_LM, "Scale 4:1" );
if( scale == 2.0 )
cpdf_textAligned( pdf, middle_x, top_y + FootprintSpaceY, 0.0, TEXTPOS_LM, "Scale 2:1" );
else if( scale == 1.0 )
cpdf_textAligned( pdf, middle_x, top_y + FootprintSpaceY, 0.0, TEXTPOS_LM, "Scale 1:1" );
else if( scale == 0.5 )
cpdf_textAligned( pdf, middle_x, top_y + FootprintSpaceY, 0.0, TEXTPOS_LM, "Scale 1:2" );
else if( scale == 0.25 )
cpdf_textAligned( pdf, middle_x, top_y + FootprintSpaceY, 0.0, TEXTPOS_LM, "Scale 1:4" );
else if( scale == 0.125 )
cpdf_textAligned( pdf, middle_x, top_y + FootprintSpaceY, 0.0, TEXTPOS_LM, "Scale 1:8" );
else if( scale == 0.0625 )
cpdf_textAligned( pdf, middle_x, top_y + FootprintSpaceY, 0.0, TEXTPOS_LM, "Scale 1:16" );
else if( scale == 0.03125 )
cpdf_textAligned( pdf, middle_x, top_y + FootprintSpaceY, 0.0, TEXTPOS_LM, "Scale 1:32" );
else if( scale == 0.015625 )
cpdf_textAligned( pdf, middle_x, top_y + FootprintSpaceY, 0.0, TEXTPOS_LM, "Scale 1:64" );
cpdf_endText( pdf );
// set origin for footprint
float shift_x = ((right_x - left_x) - (foot_right - foot_left)*scale)/2.0;
float org_x = right_x - foot_right*scale - shift_x;
float org_y = top_y - foot_top*scale;
// draw outline
for(int ip=0; ip<foot.m_outline_poly.GetSize(); ip++ )
{
float last_x, last_y;
CPolyLine * poly = &foot.m_outline_poly[ip];
int nsides = poly->GetNumCorners() - 1;
if( poly->GetClosed() )
nsides = poly->GetNumCorners();
cpdf_newpath( pdf );
last_x = org_x + poly->GetX(0)*scale/NM_PER_INCH;
last_y = org_y + poly->GetY(0)*scale/NM_PER_INCH;
cpdf_moveto( pdf, last_x, last_y );
for( int is=0; is<nsides; is++ )
{
float x;
float y;
if( is == (nsides-1 ) && poly->GetClosed() )
{
// close poly
x = org_x + poly->GetX(0)*scale/NM_PER_INCH;
y = org_y + poly->GetY(0)*scale/NM_PER_INCH;
}
else
{
// normal side
x = org_x + poly->GetX(is+1)*scale/NM_PER_INCH;
y = org_y + poly->GetY(is+1)*scale/NM_PER_INCH;
}
if( poly->GetSideStyle(is) == CPolyLine::STRAIGHT )
{
// straight line segment
cpdf_lineto( pdf, x, y );
}
else if( poly->GetSideStyle(is) == CPolyLine::ARC_CW )
{
// ellipse quadrant, clockwise, start vertical
double x1 = last_x;
double y1 = last_y + (y-last_y)*2.0*MagicBezier;
double x2 = x - (x-last_x)*2.0*MagicBezier;
double y2 = y;
if( (x>last_x && y<last_y) || (x<last_x && y>last_y) )
{
// ellipse quadrant, clockwise, start horizontal
x1 = last_x + (x-last_x)*2.0*MagicBezier;
y1 = last_y;
x2 = x;
y2 = y - (y-last_y)*2.0*MagicBezier;
}
cpdf_curveto( pdf, x1, y1, x2, y2, x, y );
}
else if( poly->GetSideStyle(is) == CPolyLine::ARC_CCW )
{
// ellipse quadrant, clockwise, start vertical
double x1 = last_x;
double y1 = last_y + (y-last_y)*2.0*MagicBezier;
double x2 = x - (x-last_x)*2.0*MagicBezier;
double y2 = y;
if( (x>last_x && y>last_y) || (x<last_x && y<last_y) )
{
// ellipse quadrant, clockwise, start horizontal
x1 = last_x + (x-last_x)*2.0*MagicBezier;
y1 = last_y;
x2 = x;
y2 = y - (y-last_y)*2.0*MagicBezier;
}
cpdf_curveto( pdf, x1, y1, x2, y2, x, y );
}
last_x = x;
last_y = y;
}
cpdf_closepath( pdf );
cpdf_setlinewidth( pdf, 72.0*scale*poly->GetW()/NM_PER_INCH );
cpdf_stroke( pdf );
}
// draw text
for(int it=0; it<foot.m_tl->text_ptr.GetSize(); it++ )
{
CText * t = foot.m_tl->text_ptr[it];
SMFontUtil * smfontutil = ((CFreePcbApp*)AfxGetApp())->m_Doc->m_smfontutil;
t->Draw( NULL, smfontutil );
for( int is=0; is<t->m_stroke.GetSize(); is++ )
{
double xi = org_x + t->m_stroke[is].xi*scale/NM_PER_INCH;
double xf = org_x + t->m_stroke[is].xf*scale/NM_PER_INCH;
double yi = org_y + t->m_stroke[is].yi*scale/NM_PER_INCH;
double yf = org_y + t->m_stroke[is].yf*scale/NM_PER_INCH;
cpdf_moveto( pdf, xi, yi );
cpdf_lineto( pdf, xf, yf );
cpdf_setlinewidth( pdf, 72.0*scale*t->m_stroke_width/NM_PER_INCH );
cpdf_stroke( pdf );
}
t->Undraw();
}
// draw pads
cpdf_setlinecap( pdf, 1 ); // round end-caps
cpdf_setlinewidth( pdf, 0.3 ); // 5 mil
for( int ip=0; ip<foot.GetNumPins(); ip++ )
{
padstack * ps = &foot.m_padstack[ip];
if( ps->hole_size )
cpdf_setrgbcolor( pdf, 0.0, 0.0, 1.0 );
else
cpdf_setrgbcolor( pdf, 0.0, 1.0, 0.0 );
float x = org_x + ps->x_rel*scale/NM_PER_INCH;
float y = org_y + ps->y_rel*scale/NM_PER_INCH;
float w = ps->top.size_h*scale/NM_PER_INCH;
float l = ps->top.size_l*2*scale/NM_PER_INCH;
float r = ps->top.radius*2*scale/NM_PER_INCH;
if( ps->top.shape == PAD_OVAL )
r = min(l,w)/2.0;
float hole = ps->hole_size*scale/NM_PER_INCH;
if( ps->top.shape == PAD_NONE )
{
cpdf_circle( pdf, x, y, hole/2 );
cpdf_stroke( pdf );
}
else if( ps->top.shape == PAD_ROUND )
{
cpdf_newpath( pdf );
cpdf_circle( pdf, x, y, w/2 );
if( ps->hole_size )
cpdf_arc( pdf, x, y, hole/2, 360.0, 0.0, 1 );
cpdf_closepath( pdf );
cpdf_fill( pdf );
}
else if( ps->top.shape == PAD_OCTAGON )
{
const double pi = 3.14159265359;
cpdf_newpath( pdf );
cpdf_moveto( pdf, x+w/2, y );
double angle = pi/8.0;
for( int i=0; i<8; i++ )
{
if( i==0 )
cpdf_moveto( pdf, x+w*cos(angle)/2.0, y+w*sin(angle)/2.0 );
else
cpdf_lineto( pdf, x+w*cos(angle)/2.0, y+w*sin(angle)/2.0 );
angle += pi/4.0;
}
if( ps->hole_size )
cpdf_arc( pdf, x, y, hole/2, 360.0, 0.0, 1 );
cpdf_closepath( pdf );
cpdf_fill( pdf );
}
else if( ps->top.shape == PAD_SQUARE )
{
cpdf_newpath( pdf );
cpdf_rect( pdf, x-w/2, y-w/2, w, w );
if( ps->hole_size )
cpdf_arc( pdf, x, y, hole/2, 360.0, 0.0, 1 );
cpdf_closepath( pdf );
cpdf_fill( pdf );
}
else if( ps->top.shape == PAD_RECT)
{
cpdf_newpath( pdf );
if( ps->angle == 0 || ps->angle == 180 )
cpdf_rect( pdf, x-l/2, y-w/2, l, w );
else
cpdf_rect( pdf, x-w/2, y-l/2, w, l );
if( ps->hole_size )
cpdf_arc( pdf, x, y, hole/2, 360.0, 0.0, 1 );
cpdf_closepath( pdf );
cpdf_fill( pdf );
}
else if( ps->top.shape == PAD_OVAL )
{
cpdf_newpath( pdf );
double h = l; // horizontal length of pad
double v = w; // vertical width of pad
if( ps->angle == 90 || ps->angle == 270 )
{
h = w;
v = l;
}
if( h > w )
{
// horizontal, clockwise
cpdf_moveto( pdf, x-h/2.0+r, y+w/2.0 );
cpdf_arc( pdf, x+h/2.0-r, y, r, 270.0, 90.0, 0 );
cpdf_arc( pdf, x+h/2.0-r, y, r, 90.0, 270.0, 0 );
}
else
{
// vertical, clockwise
cpdf_moveto( pdf, x-h/2.0, y-w/2.0+r );
cpdf_arc( pdf, x, y+v/2.0-r, r, 180.0, 0.0, 0 );
cpdf_arc( pdf, x, y-v/2.0+r, r, 360.0, 180.0, 0 );
}
if( ps->hole_size )
cpdf_arc( pdf, x, y, hole/2, 0.0, 360.0, 1 );
cpdf_closepath( pdf );
cpdf_fill( pdf );
}
else if( ps->top.shape == PAD_RRECT )
{
cpdf_newpath( pdf );
double h = l; // horizontal length of pad
double v = w; // vertical width of pad
if( ps->angle == 90 || ps->angle == 270 )
{
h = w;
v = l;
}
// clockwise
cpdf_moveto( pdf, x-h/2.0, y+v/2.0-r );
cpdf_arc( pdf, x-h/2.0+r, y+v/2.0-r, r, 180.0, 90.0, 0 );
cpdf_lineto( pdf, x+h/2.0-r, y+v/2.0 );
cpdf_arc( pdf, x+h/2.0-r, y+v/2.0-r, r, 90.0, 0.0, 0 );
cpdf_lineto( pdf, x+h/2.0, y-v/2.0+r );
cpdf_arc( pdf, x+h/2.0-r, y-v/2.0+r, r, 360.0, 270.0, 0 );
cpdf_lineto( pdf, x-h/2.0+r, y-v/2.0 );
cpdf_arc( pdf, x-h/2.0+r, y-v/2.0+r, r, 270.0, 180.0, 0 );
if( ps->hole_size )
cpdf_arc( pdf, x, y, hole/2, 0.0, 360.0, 1 );
cpdf_closepath( pdf );
cpdf_fill( pdf );
}
else
ASSERT(0);
}
cpdf_setrgbcolor( pdf, 0.0, 0.0, 0.0 );
}
cpdf_finalizeAll(pdf); /* PDF file/memstream is actually written here */
int err = cpdf_savePDFmemoryStreamToFile(pdf, title_str );
if( err == -1 )
{
if( !m_dlg_log )
{
AfxMessageBox( "Error: Unable to write file\nIt may be read-only or open in another application", MB_OK );
}
else
{
CString log_message = "*** Error: unable to write file ***\r\n";
m_dlg_log->AddLine( log_message );
}
}
cpdf_close(pdf); /* shut down */
}
}
void CDlgLibraryManager::Initialize( CFootLibFolderMap * foldermap, CDlgLog * log )
{
// if editor_footlib exists, use it, otherwise use project_footlib
m_foldermap = foldermap;
m_dlg_log = log;
}
void CDlgLibraryManager::OnBnClickedButtonMgrBrowse()
{
CPathDialog dlg( "Open Folder", "Select footprint library folder", *m_footlib->GetFullPath() );
int ret = dlg.DoModal();
if( ret == IDOK )
{
CString path_str = dlg.GetPathName();
m_edit_footlib.SetWindowText( path_str );
m_footlib = m_foldermap->GetFolder( &path_str, m_dlg_log );
if( !m_footlib )
{
ASSERT(0);
}
m_foldermap->SetLastFolder( &path_str );
while( m_combo_libfile.GetCount() != 0 )
{
m_combo_libfile.DeleteString(0);
}
for( int i=0; i<m_footlib->GetNumLibs(); i++ )
{