forked from zharovdv/InteractiveHTMLBOM4Altium2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteractiveHTMLBOM4Altium2.pas
More file actions
3061 lines (2679 loc) · 104 KB
/
InteractiveHTMLBOM4Altium2.pas
File metadata and controls
3061 lines (2679 loc) · 104 KB
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
const
constKindPcb = 'PCB';
epPredictOutputFileNames = 'epPredictOutputFileNames';
epConfigure = 'epConfigure';
epGenerate = 'epGenerate';
epRunGUI = 'epRunGUI';
SCannotCreateDir = 'Unable to create the folder ''%s''';
var
CurrWorkSpace: IWorkSpace; // An Interface handle to the current workspace
CurrProject: IProject; // An Interface handle to the current Project
ProjectVariant: IProjectVariant; // An Interface handle to the current Variant
FlattenedDoc: IDocument; // An Interface handle to the "flattened" document
CurrComponent: IComponent; // An Interface handle to the current Component
TargetFileName: String;
// System parameter supplied by OutJob : desired output file name, if any
TargetFolder: String;
// System parameter supplied by OutJob : desired output folder
TargetPrefix: String;
// System parameter supplied by OutJob : desired output file name prefix, if any
LayerFilterIndex: Integer;
FormatIndex: Integer;
FieldSeparatorIndex: Integer;
DarkMode: Boolean;
AddNets: Boolean;
AddTracks: Boolean;
Highlighting1Pin: Boolean;
FabLayer: Boolean;
Title: String;
Company: String;
Revision: String;
ValueParameterName: String;
ColumnsParametersNames: TStringList;
GroupParametersNames: TStringList;
Nets: TStringList;
EntryPoint: String;
BaseFullDir: String;
ProjectTitle: String;
ProjectRevision: String;
ProjectCompany: String;
SkippedFootprints: TStringList;
i: Integer;
FootprintIdCount: Integer;
procedure SetupProjectVariant(Dummy: Integer); forward;
function GetBoard(Dummy: Integer): IPCB_Board; forward;
function GenerateNativeConfig(Dummy: Integer): String; forward;
function GenerateAltiumConfig(Dummy: Integer): String; forward;
procedure PopulateChoiceFields(Dummy: Integer); forward;
procedure PopulateStaticFields(Dummy: Integer); forward;
procedure PopulateDynamicFields(Board: IPCB_Board); forward;
procedure InitializeProject(Dummy: Integer); forward;
function GetSelectedFields(Dummy: Integer): TStringList; forward;
function GetSelectedGroupParameters(Dummy: Integer): TStringList; forward;
function GetState_FromParameters(Dummy: Integer): String; forward;
{ ..................................................................................................................... }
{ . Global Variable Mapping . }
{ ..................................................................................................................... }
function GetSeparator(Dummy: Integer): String;
var
Separator: TString;
begin
case FieldSeparatorIndex of
0:
Separator := ',';
1:
Separator := ';';
2:
Separator := ' ';
3:
Separator := #9;
// else
end;
Result := Separator;
end;
{ ..................................................................................................................... }
{ . Path and Filename Handling . }
{ ..................................................................................................................... }
Function GetOutputFileNameWithExtension(Ext: String): String;
Begin
If TargetFolder = '' Then
TargetFolder := CurrProject.DM_GetOutputPath;
If TargetFileName = '' Then
TargetFileName := CurrProject.DM_ProjectFileName;
Result := AddSlash(TargetFolder) + TargetPrefix +
ChangeFileExt(TargetFileName, Ext);
// CurrString := StringReplace( CurrString, '<DATE>', DateStr, MkSet( rfReplaceAll, rfIgnoreCase ) );
end;
function GetWDFileName(FF: String): String;
begin
Result := FF;
if not IsPathRelative(FF) then
Exit;
Result := BaseFullDir + '\' + FF;
end;
// TODO: Crash in Release Manager
procedure MyAbort(Value: string);
begin
// Empty
end;
{ ***************************************************************************
* function FindProjectPcbDocFile()
* Find the PcbDoc file associated with this project.
* Panic if we find any number not equal to 1 (eg 0 or 2).
*
* Returns full path to this project's PcbDoc file in var parm pcbDocPath.
* Returns: 0 on success, 1 if not successful.
*************************************************************************** }
function FindProjectPcbDocFile(Project: IProject;
flagRequirePcbDocFile: Boolean; var pcbDocPath: TDynamicString;): Integer;
var
Document: IDocument;
k: Integer;
numPcbDocs: Integer;
begin
{ For now, assume/hope/pray that we will succeed. }
Result := 0;
{ Flag that we haven't yet found the PcbDoc file. }
pcbDocPath := '';
{ Init number of PcbDoc files found to be 0. }
numPcbDocs := 0;
{ *** Find name of this project's .PcbDoc file. *** }
{ Loop over all logical documents in the project.... }
for k := 0 to Project.DM_LogicalDocumentCount - 1 do
begin
Document := Project.DM_LogicalDocuments(k);
{ See if this document is a PcbDoc file. }
if (Document.DM_DocumentKind = constKindPcb) then
begin
{ Increment number of PcbDoc files that we've found. }
numPcbDocs := numPcbDocs + 1;
{ Record name of this PcbDoc file to return to caller. }
pcbDocPath := Document.DM_FullPath;
// ShowMessage('Found PCB document with full path ' + pcbDocPath);
end;
end; { endfor loop over all logical documents in project }
{ Make sure there is at least one PcbDoc file. }
if (numPcbDocs < 1) then
begin
{ See if the user has requested operations that require a PcbDoc file. }
if (flagRequirePcbDocFile) then
begin
MyAbort('Found ' + IntToStr(numPcbDocs) +
' PcbDoc files in your project. This number should have been exactly 1!');
end
{ Else just issue a warning. }
else
begin
{ Issue warning modal dialog box with specified warning message,
no reply after clicking Ok, and specified reply after clicking Cancel. }
IssueWarningWithOkOrCancel
('Unable to find a PcbDoc file within this project.' + constLineBreak +
'However, since you have not requested operations that require a PcbDoc file, I will proceed to generate other OutJob outputs if you click OK.',
'', 'Aborting script at user request due to missing PcbDoc file ' +
constPcbVersionParm + '.');
end; { endelse }
end; { endif }
{ Make sure there is no more than 1 PcbDoc file. }
if (numPcbDocs > 1) then
begin
MyAbort('Found ' + IntToStr(numPcbDocs) +
' PcbDoc files in your project. This script currently only supports having 1 PcbDoc file per project!');
end;
// ShowMessage('About to leave FindProjectPcbDocFile(), pcbDocPath is ' + pcbDocPath);
end; { end FindProjectPcbDocFile() }
{ ..................................................................................................................... }
{ . Workspace Interaction . }
{ ..................................................................................................................... }
Function GetBoard(Dummy: Integer): IPCB_Board;
Var
Board: IPCB_Board; // document board object
Document: IServerDocument;
pcbDocPath: TString;
flagRequirePcbDocFile: Boolean;
Begin
// Make sure the current Workspace opens or else quit this script
CurrWorkSpace := GetWorkSpace;
If (CurrWorkSpace = Nil) Then
Exit;
// Make sure the currently focussed Project in this Workspace opens or else
// quit this script
CurrProject := CurrWorkSpace.DM_FocusedProject;
If CurrProject = Nil Then
Exit;
flagRequirePcbDocFile := True;
FindProjectPcbDocFile(CurrProject, flagRequirePcbDocFile, pcbDocPath);
// TODO: Close
Document := Client.OpenDocument('pcb', pcbDocPath);
Board := PCBServer.GetPCBBoardByPath(pcbDocPath);
If Not Assigned(Board) Then // check of active document
Begin
ShowMessage('The Current Document is not a PCB Document.');
Exit;
end;
Result := Board;
end;
{ ..................................................................................................................... }
{ . JSON String Conversions . }
{ ..................................................................................................................... }
function JSONFloatToStr(v: Single): String;
begin
Result := StringReplace(FloatToStr(v), ',', '.',
MkSet(rfReplaceAll, rfIgnoreCase));
end;
function JSONBoolToStr(v: Boolean): String;
begin
if v then
Result := 'true'
else
Result := 'false';
end;
function JSONStrToStr(v: String): String;
var
i: Integer;
begin
Result := '"';
for i := 1 to Length(v) do
begin
case v[i] of
'"':
Result := Result + '\"';
'\':
Result := Result + '\\';
#$08:
Result := Result + '\b';
#$09:
Result := Result + '\t';
#$0a:
Result := Result + '\n';
#$0b:
Result := Result + '\v';
#$0c:
Result := Result + '\f';
#$0d:
Result := Result + '\r';
#$00 .. #$07, #$0e .. #$1f, #$7e .. #$7f:
begin
Result := Result + '\u' + IntToHex(Ord(v[i]), 4);
end;
else
Result := Result + '\u' + IntToHex(Ord(v[i]), 4);
end;
end;
Result := Result + '"';
end;
function _String_(v: String): String;
var
i: Integer;
begin
Result := '';
for i := 1 to Length(v) do
begin
case v[i] of
'.', '-', '_':
Result := Result + v[i];
'0' .. '9':
Result := Result + v[i];
'A' .. 'W':
Result := Result + v[i];
else
Result := Result + '';
end;
end;
end;
function GetCompFromComp(pcbc: IPCB_Component): IComponent;
begin
// Make sure the current Workspace opens or else quit this script
CurrWorkSpace := GetWorkSpace;
If (CurrWorkSpace = Nil) Then
Exit;
// Make sure the currently focussed Project in this Workspace opens or else
// quit this script
CurrProject := CurrWorkSpace.DM_FocusedProject;
If CurrProject = Nil Then
Exit;
Result := nil;
end;
function GetFlatDoc(Dummy: Integer): IDocument;
Begin
Result := CurrProject.DM_DocumentFlattened;
If (Result = Nil) Then
Begin
// First try compiling the project
AddStringParameter('Action', 'Compile');
AddStringParameter('ObjectKind', 'Project');
RunProcess('WorkspaceManager:Compile');
// Try Again to open the flattened document
Result := CurrProject.DM_DocumentFlattened;
end;
end;
procedure ListAllFields(Dummy: Integer);
Var
CompIndex: Integer; // An Index for pullin out components
PhysCompCount: Integer; // A count of the number of components in document
PhysComponent: IComponent; // An Interface handle to the current Component
PhysCompIndex: Integer; // An index for pulling out Physical Parts
CurrPart: IPart; // An Interface handle to the current Part of a Component
CurrSortStr: String; // A String to hold the sorting field
CurrPhysDesStr: String; // A String to hold the current Physical Designator
CurrFootprintStr: String; // A String to hold the current Footprint
CurrDescrStr: String; // A String to hold the current Description
CurrLibRefStr: String; // A String to hold the current Library Reference
CurrStuffedStr: String;
// Flag for processing the rest of the designator characters
SortIndex: Integer; // Temporary Index for chars in the Designator
CompCount: Integer; // The Number of Components Flattened Document
CompListIndex: Integer; // An index for strings in CompList
i: Integer;
_n: IParameter;
_nn: string;
_vv: string;
_ss: TStringList;
_pp: TStringList;
ParmIndex: Integer;
ParmCount: Integer; // The Number of Parameters in Component
CurrParm: IParameter; // An interface handle to a Parameter
iii: Integer;
Line: String;
ComponentVariation: IComponentVariation;
Begin
FlattenedDoc := GetFlatDoc(0);
CompCount := FlattenedDoc.DM_ComponentCount;
_pp := TStringList.Create;
_pp.Sorted := True;
_pp.Duplicates := dupIgnore;
For CompIndex := 0 To CompCount - 1 Do
Begin
CurrComponent := FlattenedDoc.DM_Components[CompIndex];
ParmCount := CurrComponent.DM_ParameterCount;
For ParmIndex := 0 To ParmCount - 1 Do
Begin
CurrParm := CurrComponent.DM_Parameters(ParmIndex);
_pp.Add(CurrParm.DM_Name);
end;
end;
_pp.Free;
end;
function GetCompFromCompEx(pcbc: IPCB_Component): IComponent;
Var
CompIndex: Integer; // An Index for pullin out components
PhysCompCount: Integer; // A count of the number of components in document
PhysComponent: IComponent; // An Interface handle to the current Component
PhysCompIndex: Integer; // An index for pulling out Physical Parts
CurrPart: IPart; // An Interface handle to the current Part of a Component
CurrSortStr: String; // A String to hold the sorting field
CurrPhysDesStr: String; // A String to hold the current Physical Designator
CurrFootprintStr: String; // A String to hold the current Footprint
CurrDescrStr: String; // A String to hold the current Description
CurrLibRefStr: String; // A String to hold the current Library Reference
CurrStuffedStr: String;
// Flag for processing the rest of the designator characters
SortIndex: Integer; // Temporary Index for chars in the Designator
CompCount: Integer; // The Number of Components Flattened Document
CompListIndex: Integer; // An index for strings in CompList
i: Integer;
_n: IParameter;
_nn: string;
_vv: string;
_ss: TStringList;
_pp: TStringList;
ParmIndex: Integer;
ParmCount: Integer; // The Number of Parameters in Component
CurrParm: IParameter; // An interface handle to a Parameter
iii: Integer;
Line: String;
ComponentVariation: IComponentVariation;
Begin
FlattenedDoc := GetFlatDoc(0);
CompCount := FlattenedDoc.DM_ComponentCount;
// Component.SourceUniqueId, Component.SourceDesignator
For CompIndex := 0 To CompCount - 1 Do
Begin
CurrComponent := FlattenedDoc.DM_Components[CompIndex];
if CurrComponent.DM_UniqueId = pcbc.SourceUniqueId then
begin
Result := CurrComponent;
Exit;
end;
end;
end;
{ ..................................................................................................................... }
{ . Generation Helper Functions . }
{ ..................................................................................................................... }
{
ComponentIsFittedInCurrentVariant cross-checks the ComponentId and Designator
against the current project variant (an @ in the ComponentId indicates that
the component is part of a variant). Only components that are part of the
current variation (and don't have a DNP directive) are included in the
output.
}
Function ComponentIsFittedInCurrentVariant(ComponentId, Designator: TString;
_ProjectVariant: IProjectVariant): Boolean;
var
// Designator: IPart;
ComponentVariation: IComponentVariation;
begin
// [!!!] UGLY
// Designator := ComponentId.DM_SubParts[0];
if _ProjectVariant = nil then
begin
if pos('@', ComponentId) <> 0 then
begin
// Exclude components that are part of a variant but we're not inside a variant
Result := False;
end
else
begin
// Component is not part of a variant, and we're not inside a variant
Result := True;
end;
end;
if _ProjectVariant <> nil then
begin
ComponentVariation := _ProjectVariant.DM_FindComponentVariationByDesignator
(Designator);
if ComponentVariation <> nil then
begin
if ComponentId <> ComponentVariation.DM_UniqueId + '@' + _ProjectVariant.DM_Description
then
begin
// Exclude component that is part of another variant
Result := False;
end
else
begin
// Include component that is part of the current variant
Result := True;
end;
// [!!!] Never
if ComponentVariation.DM_VariationKind = eVariation_NotFitted then
begin
// In any case, exclude components that are not fitted
Result := False;
end;
end
else
begin
if pos('@', ComponentId) <> 0 then
begin
// Component has a variant-specific ID, but there is no variation defined for it.
// Possibly belongs to a different variant --> exclude it.
Result := False;
end
else
begin
// Component has no variant-specific ID, and no variation is defined --> include it.
Result := True;
end;
end;
end;
end;
function GetComponentParameters(comp: IPCB_Component): TStringList;
var
stateText: string;
str1: string;
paramsComponent: TStringList;
pcb_primitiveparametersIntf: IPCB_PrimitiveParameters;
argIndex: Integer;
parameterByIndex: IPCB_Parameter;
name, Value: String;
begin
stateText := comp.GetState_Name().GetState_Text();
str1 := comp.GetState_SourceCompDesignItemID();
paramsComponent := TStringList.Create;
paramsComponent.Add('[DesignItemID]' + '=' + str1);
paramsComponent.Add('[Footprint]' + '=' + comp.GetState_Pattern());
paramsComponent.Add('[Comment]' + '=' + comp.GetState_Comment()
.GetState_ConvertedString());
paramsComponent.Add('[Description]' + '=' +
comp.GetState_SourceDescription());
begin
pcb_primitiveparametersIntf := comp;
for argIndex := 0 to pcb_primitiveparametersIntf.Count() - 1 do
begin
parameterByIndex := pcb_primitiveparametersIntf.GetParameterByIndex
(argIndex);
name := parameterByIndex.GetName();
Value := parameterByIndex.GetValue();
paramsComponent.Add(name + '=' + Value);
end;
end;
Result := paramsComponent;
end;
{ ..................................................................................................................... }
{ . Geometry Helper Functions . }
{ ..................................................................................................................... }
function NormalizeAngle(Angle: Single): Single;
begin
Result := Angle - 360 * Trunc(Angle / 360);
if Result < 0 then
Result := Result + 360;
end;
{ ..................................................................................................................... }
{ . Generic JSON Generation . }
{ ..................................................................................................................... }
function ParseArcGeneric(Board: IPCB_Board; Prim: TObject): String;
const
Epsilon = 0.001;
var
PnPout: TStringList;
EdgeWidth, EdgeX1, EdgeY1, EdgeX2, EdgeY2, EdgeRadius: String;
EdgeType: String;
begin
PnPout := TStringList.Create;
if (Abs(NormalizeAngle(-Prim.EndAngle) - NormalizeAngle(-Prim.StartAngle)) <
Epsilon) then
begin
EdgeWidth := JSONFloatToStr(CoordToMMs(Prim.LineWidth));
EdgeX1 := JSONFloatToStr(CoordToMMs(Prim.XCenter - Board.XOrigin));
EdgeY1 := JSONFloatToStr(-CoordToMMs(Prim.YCenter - Board.YOrigin));
EdgeRadius := JSONFloatToStr(CoordToMMs(Prim.Radius));
EdgeType := 'circle';
PnPout.Add('{');
PnPout.Add('"type":' + JSONStrToStr(EdgeType) + ',');
PnPout.Add('"start":' + '[' + EdgeX1 + ', ' + EdgeY1 + ']' + ',');
PnPout.Add('"radius":' + EdgeRadius + ',');
// filled
PnPout.Add('"width":' + EdgeWidth);
PnPout.Add('}');
end
else
begin
EdgeWidth := JSONFloatToStr(CoordToMMs(Prim.LineWidth));
EdgeX1 := JSONFloatToStr(CoordToMMs(Prim.XCenter - Board.XOrigin));
EdgeY1 := JSONFloatToStr(-CoordToMMs(Prim.YCenter - Board.YOrigin));
EdgeX2 := JSONFloatToStr(-Prim.EndAngle);
EdgeY2 := JSONFloatToStr(-Prim.StartAngle);
EdgeRadius := JSONFloatToStr(CoordToMMs(Prim.Radius));
EdgeType := 'arc';
PnPout.Add('{');
PnPout.Add('"type":' + JSONStrToStr(EdgeType) + ',');
PnPout.Add('"width":' + EdgeWidth + ',');
PnPout.Add('"start":' + '[' + EdgeX1 + ', ' + EdgeY1 + ']' + ',');
PnPout.Add('"radius":' + EdgeRadius + ',');
PnPout.Add('"startangle":' + EdgeX2 + ',');
PnPout.Add('"endangle":' + EdgeY2);
PnPout.Add('}');
end;
Result := PnPout.Text;
PnPout.Free;
end;
function ParseTrackGeneric(Board: IPCB_Board; Prim: TObject;
NoType: Boolean): String;
var
PnPout: TStringList;
EdgeWidth, EdgeX1, EdgeY1, EdgeX2, EdgeY2, EdgeRadius: String;
EdgeType: String;
Net: String;
begin
PnPout := TStringList.Create;
EdgeWidth := JSONFloatToStr(CoordToMMs(Prim.Width));
EdgeX1 := JSONFloatToStr(CoordToMMs(Prim.X1 - Board.XOrigin));
EdgeY1 := JSONFloatToStr(-CoordToMMs(Prim.Y1 - Board.YOrigin));
EdgeX2 := JSONFloatToStr(CoordToMMs(Prim.X2 - Board.XOrigin));
EdgeY2 := JSONFloatToStr(-CoordToMMs(Prim.Y2 - Board.YOrigin));
Net := 'No Net';
if Prim.Net <> nil then
Net := Prim.Net.name;
Nets.Add(Net);
EdgeType := 'segment';
PnPout.Add('{');
if not NoType then
PnPout.Add('"type":' + JSONStrToStr(EdgeType) + ',');
if NoType then
PnPout.Add('"net":' + JSONStrToStr(Net) + ',');
PnPout.Add('"start":' + '[' + EdgeX1 + ', ' + EdgeY1 + ']' + ',');
PnPout.Add('"end":' + '[' + EdgeX2 + ', ' + EdgeY2 + ']' + ',');
PnPout.Add('"width":' + EdgeWidth);
PnPout.Add('}');
Result := PnPout.Text;
PnPout.Free;
end;
function ParseVIAGeneric(Board: IPCB_Board; Prim: TObject;
NoType: Boolean): String;
var
PnPout: TStringList;
EdgeWidth, EdgeX1, EdgeY1, EdgeX2, EdgeY2, EdgeRadius: String;
EdgeType: String;
PadDrillWidth: String;
Net: String;
begin
PnPout := TStringList.Create;
EdgeWidth := JSONFloatToStr(CoordToMMs(Prim.Size));
EdgeX1 := JSONFloatToStr(CoordToMMs(Prim.x - Board.XOrigin));
EdgeY1 := JSONFloatToStr(-CoordToMMs(Prim.Y - Board.YOrigin));
PadDrillWidth := JSONFloatToStr(CoordToMMs(Prim.HoleSize));
Net := 'No Net';
if Prim.Net <> nil then
Net := Prim.Net.name;
Nets.Add(Net);
EdgeType := 'segment';
PnPout.Add('{');
if not NoType then
PnPout.Add('"type":' + JSONStrToStr(EdgeType) + ',');
if NoType then
PnPout.Add('"net":' + JSONStrToStr(Net) + ',');
PnPout.Add('"start":' + '[' + EdgeX1 + ', ' + EdgeY1 + ']' + ',');
PnPout.Add('"end":' + '[' + EdgeX1 + ', ' + EdgeY1 + ']' + ',');
PnPout.Add('"width":' + EdgeWidth + ',');
PnPout.Add('"drillsize":' + PadDrillWidth);
PnPout.Add('}');
{
// TODO: Layers
EdgeWidth := JSONFloatToStr(CoordToMMs(Prim.Size));
EdgeX1 := JSONFloatToStr(CoordToMMs(Prim.x - Board.XOrigin));
EdgeY1 := JSONFloatToStr(-CoordToMMs(Prim.Y - Board.YOrigin));
PadDrillWidth := JSONFloatToStr(CoordToMMs(Prim.HoleSize));
If (Prim.Layer = eTopOverlay) Then
Layer := 'TopOverlay'
Else If (Prim.Layer = eBottomOverlay) Then
Layer := 'BottomOverlay'
Else If (Prim.Layer = eTopLayer) Then
Layer := 'TopLayer'
Else If (Prim.Layer = eBottomLayer) Then
Layer := 'BottomLayer'
Else If (Prim.Layer = eMultiLayer) Then
Layer := 'MultiLayer';
Net := 'No Net';
if Prim.Net <> nil then
Net := Prim.Net.name;
PnPout.Add('"Layer":' + JSONStrToStr(Layer) + ',');
PnPout.Add('"Type":' + JSONStrToStr(EdgeType) + ',');
PnPout.Add('"Width":' + JSONStrToStr(EdgeWidth) + ',');
PnPout.Add('"X":' + (EdgeX1) + ',');
PnPout.Add('"Y":' + (EdgeY1) + ',');
PnPout.Add('"DrillWidth":' + (PadDrillWidth) + ',');
PnPout.Add('"Net":' + JSONStrToStr(Net));
}
Result := PnPout.Text;
PnPout.Free;
end;
function ListIndexOf(s: TStringList; s2: String): Integer;
var
i: Integer;
begin
Result := -1;
for i := 0 to s.Count - 1 do
if s[i] = s2 then
begin
Result := i;
Exit;
end;
end;
function ParseComponentGeneric(Board: IPCB_Board; Component: TObject;
SelectedFields, SelectedGroupParameters: TStringList; NoBOM: Boolean): String;
var
PnPout: TStringList;
Iterator: IPCB_BoardIterator;
ComponentIterator: IPCB_GroupIterator;
Pad: IPCB_Pad;
x, Y, Rotation, Layer, Net: TString;
Iter, Prim: TObject;
PadsCount: Integer;
X1, Y1, X2, Y2, _W, _H: Single;
Width, Height: String;
Parameters: TStringList;
i, j: Integer;
Key: String;
begin
PnPout := TStringList.Create;
If (Component.Layer = eTopLayer) Then
Layer := 'F'
Else
Layer := 'B';
x := JSONFloatToStr(CoordToMMs(Component.x - Board.XOrigin));
Y := JSONFloatToStr(-CoordToMMs(Component.Y - Board.YOrigin));
Rotation := IntToStr(Component.Rotation);
// TODO: Is it correct? X1,Y1 vs X,Y
X1 := CoordToMMs(Component.BoundingRectangleNoNameCommentForSignals.Left -
Board.XOrigin);
Y1 := CoordToMMs(Component.BoundingRectangleNoNameCommentForSignals.Bottom -
Board.YOrigin);
X2 := CoordToMMs(Component.BoundingRectangleNoNameCommentForSignals.Right -
Board.XOrigin);
Y2 := CoordToMMs(Component.BoundingRectangleNoNameCommentForSignals.Top -
Board.YOrigin);
Width := JSONFloatToStr(X2 - X1);
Height := JSONFloatToStr(Y2 - Y1);
x := StringReplace(FloatToStr(X1), ',', '.',
MkSet(rfReplaceAll, rfIgnoreCase));
Y := StringReplace(FloatToStr(-Y2), ',', '.',
MkSet(rfReplaceAll, rfIgnoreCase));
{
bbox['pos'] :=[x0.round(), -y1.round()]; //
bbox['relpos'] :=[0, 0];
bbox['angle'] :=0;
bbox['size'] :=[(x1 - x0).round(), (y1 - y0).round()];
bbox['center'] :=[(x0 + bbox.size[0] / 2).round(), -(y0 + bbox.size[1] / 2).round()];
}
Parameters := GetComponentParameters(Component);
PnPout.Add('{');
PnPout.Add('"attr":' + JSONStrToStr('') + ',');
PnPout.Add('"footprint":' + JSONStrToStr(Component.Pattern) + ',');
PnPout.Add('"layer":' + JSONStrToStr(Layer) + ',');
PnPout.Add('"ref":' + JSONStrToStr(Component.SourceDesignator) + ',');
PnPout.Add('"val":' + JSONStrToStr(Parameters.Values[ValueParameterName]) + ',');
PnPout.Add('"extra_fields": {');
j := 0;
for i := 0 to SelectedFields.Count - 1 do
begin
Key := SelectedFields[i];
if Key = ValueParameterName then
continue;
if Key = '[Footprint]' then
continue;
if ((NoBOM) And (Key = '[DesignItemID]')) then
begin
Parameters.Values[Key] := 'DNP-' + Parameters.Values[Key];
end;
if (j > 0) then
PnPout.Add(',');
PnPout.Add('"' + Key + '":' + JSONStrToStr(Parameters.Values[Key]));
j := j + 1;
end;
for i := 0 to SelectedGroupParameters.Count - 1 do
begin
Key := SelectedGroupParameters[i];
if Key = ValueParameterName then
continue;
if Key = '[Footprint]' then
continue;
if SelectedFields.IndexOf(Key) = -1 then
continue;
// TODO: Strange hack
if ListIndexOf(SelectedFields, Key) = -1 then
continue;
if (j > 0) then
PnPout.Add(',');
PnPout.Add('"' + Key + '":' + JSONStrToStr(Parameters.Values[Key]));
j := j + 1;
end;
if (j > 0) then
PnPout.Add(',');
PnPout.Add('"NoBOM":' + JSONBoolToStr(NoBOM));
// attr? extra_fields?
// PnPout.Add('"PartNumber":' + JSONStrToStr
// (sl.Values[ValueParameterName]) + ',');
// PnPout.Add('"Value":' + JSONStrToStr(sl.Values[ValueParameterName]) + ',');
PnPout.Add('}');
PnPout.Add('}');
Result := PnPout.Text;
PnPout.Free;
end;
function ParsePadGeneric(Board: IPCB_Board; Prim, Pad: TObject): String;
var
PnPout: TStringList;
// Layer, Net: String;
// X1, Y1, X2, Y2, _W, _H: Single;
// EdgeWidth, EdgeX1, EdgeY1, PadDrillWidth: String;
// EdgeType: String;
PadsCount: Integer;
PadLayer, PadType: String;
PadX, PadY, PadAngle: TString;
X1, Y1, X2, Y2, _W, _H: Single;
Width, Height: String;
PadWidth, PadHeight: String;
PadRadius: String;
PadPin1: Boolean;
PadShape, PadDrillShape: String;
PadDrillWidth, PadDrillHeight: String;
Net: String;
begin
PnPout := TStringList.Create;
PnPout.Add('{');
// TODO: Not sure
PadType := Pad.Layer;
if (Pad.Layer = eTopLayer) then
begin
PadLayer := '["F"]';
PadType := 'smd';
PadWidth := FloatToStr(CoordToMMs(Pad.TopXSize));
PadHeight := FloatToStr(CoordToMMs(Pad.TopYSize));
PadShape := 'circle';
case (Pad.TopShape) of
1:
begin
if PadWidth = PadHeight then
PadShape := 'circle'
else
PadShape := 'oval';
// (res['size'][0] == res['size'][1]) ? 'circle' : 'oval';
end;
2:
PadShape := 'rect';
// 3:PadShape :='chamfrect';
9:
begin
PadShape := 'roundrect';
PadRadius := JSONFloatToStr
(CoordToMMs(Prim.CornerRadius(Prim.Layer)));
end;
// default:
// res['shape'] :='custom';
end;
end
else if (Pad.Layer = eBottomLayer) then
begin
PadLayer := '["B"]';
PadType := 'smd';
PadWidth := FloatToStr(CoordToMMs(Prim.BotXSize));
PadHeight := FloatToStr(CoordToMMs(Prim.BotYSize));
PadShape := 'circle';
case (Pad.BotShape) of
1:
begin
if PadWidth = PadHeight then
PadShape := 'circle'
else
PadShape := 'oval';
// (res['size'][0] == res['size'][1]) ? 'circle' : 'oval';
end;
2:
PadShape := 'rect';
// 3:PadShape :='chamfrect';
9:
begin
PadShape := 'roundrect';
PadRadius := JSONFloatToStr
(CoordToMMs(Prim.CornerRadius(Prim.Layer)));
end;
// default:
// res['shape'] :='custom';
end;
end
else
begin
PadLayer := '["F", "B"]';
PadType := 'th';
PadWidth := FloatToStr(CoordToMMs(Prim.TopXSize));
PadHeight := FloatToStr(CoordToMMs(Prim.TopYSize));
// TODO: Is it norm?
PadShape := 'circle';
case (Pad.TopShape) of
1:
begin
if PadWidth = PadHeight then
PadShape := 'circle'
else
PadShape := 'oval';
// (res['size'][0] == res['size'][1]) ? 'circle' : 'oval';
end;
// 3:PadShape :='chamfrect';
9:
begin
PadShape := 'roundrect';
PadRadius := JSONFloatToStr
(CoordToMMs(Prim.CornerRadius(Prim.Layer)));
end;
// default:
// res['shape'] :='custom';
end;
case (Pad.BotShape) of
1:
begin
if PadWidth = PadHeight then
PadShape := 'circle'
else
PadShape := 'oval';
// (res['size'][0] == res['size'][1]) ? 'circle' : 'oval';
end;
2:
PadShape := 'rect';
// 3:PadShape :='chamfrect';
9:
begin
PadShape := 'roundrect';
PadRadius := JSONFloatToStr
(CoordToMMs(Prim.CornerRadius(Prim.Layer)));
end;
// default:
// res['shape'] :='custom';
end;
case (Pad.HoleType) of
0: // circle
begin
// res["drillsize"] = [CoordToMMs(Prim.HoleSize).round(), CoordToMMs(Prim.HoleSize).round()];
PadDrillShape := 'circle';
PadDrillWidth := JSONFloatToStr(CoordToMMs(Prim.HoleSize));