-
Notifications
You must be signed in to change notification settings - Fork 1
/
Eagle.vb
5275 lines (4624 loc) · 203 KB
/
Eagle.vb
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
Imports System.Xml
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Math
Namespace Eagle
Public Enum GridUnit
GU_mic
GU_mm
GU_mil
GU_inch
End Enum
Public Enum GridStyle
GS_lines
GS_dots
End Enum
Public Enum WireStyle
WS_continuous
WS_longdash
WS_shortdash
WS_dashdot
End Enum
Public Enum WireCap
WC_flat
WC_round
End Enum
Public Enum PadShape
PS_square
PS_round
PS_octagon
PS_long
PS_offset
End Enum
Public Enum ViaShape
VS_square
VS_round
VS_octagon
End Enum
Public Enum TextFont
TF_vector
TF_proportional
TF_fixed
End Enum
Public Enum AttributeDisplay
AD_off
AD_value
AD_name
AD_both
End Enum
Public Enum PolygonPour
PP_solid
PP_hatch
PP_cutout
End Enum
Public Enum PinVisible
PV_off
PV_pad
PV_pin
PV_both
End Enum
Public Enum PinDirection
PD_nc
PD_in
PD_out
PD_io
PD_oc
PD_pwr
PD_pas
PD_hiz
PD_sup
End Enum
Public Enum PinFunction
PF_none
PF_dot
PF_clk
PF_dotclk
End Enum
Public Enum PinLength
PL_point
PL_short
PL_middle
PL_long
End Enum
Public Enum GateAddLevel
GAL_must
GAL_can
GAL_addnext
GAL_request
GAL_always
End Enum
Public Enum ContactRoute
CR_all
CR_any
End Enum
Public Enum DimensionType
DT_parallel
DT_horizontal
DT_vertical
DT_radius
DT_diameter
DT_leader
End Enum
Public Enum Severity
S_info
S_warning
S_error
End Enum
Public Enum Align
A_bottom_left
A_bottom_center
A_bottom_right
A_center_left
A_center
A_center_right
A_top_left
A_top_center
A_top_right
End Enum
Public Enum VerticalText
VT_up
VT_down
End Enum
Public Enum DrawingType
schematic
board
library
End Enum
Public Enum PadTypes
PadTypeThroughHole
PadTypeSMD
End Enum
Public Enum GraphicOwnerType
GO_Package
GO_Symbol
GO_Plain
GO_Library
GO_Sheet
GO_Segment
End Enum
Public Class Rotation
Implements ICloneable
Dim m_Rotation As Single
Dim m_Value As String
Public Sub New()
Rotation = 0
End Sub
Public Sub New(ByVal Value As String)
m_Value = Value
m_Rotation = toSingle(Value.Replace("M", "").Replace("S", "").Replace("R", ""))
End Sub
Public Overrides Function ToString() As String
Return m_Value
End Function
Public Property Rotation() As Single
Get
Return m_Rotation
End Get
Set(ByVal value As Single)
m_Rotation = value
m_Value = "R" & value
End Set
End Property
Public Function Clone() As Object Implements System.ICloneable.Clone
Return Me.MemberwiseClone()
End Function
Public Function RotationRadians() As Double
Return m_Rotation / 180 * PI
End Function
End Class
Public Interface EaglePad
Property Name() As String
ReadOnly Property PadType() As PadTypes
End Interface
Public Interface EagleSaveable
ReadOnly Property NodeName() As String
Sub WriteXml(ByVal Doc As XmlDocument, ByVal Node As XmlNode) 'saves the xml of the object to doc appending to node
End Interface
Public MustInherit Class Graphic
Implements ICloneable
Protected m_Owner As GraphicOwner 'the owner, schematic, package, plain, symbol,...
Protected m_UpdateGraphics As Boolean 'set to true if some of the graphics classes need to be updated before rendering
Protected m_Selected As Boolean
Public Sub New(ByVal Owner As GraphicOwner)
m_Owner = Owner
m_UpdateGraphics = True
End Sub
''' <summary>
''' Gets / Sets the owner of this graphic object, may have to be adjusted after a clone() was called
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Owner() As GraphicOwner
Get
Return m_Owner
End Get
Set(ByVal value As GraphicOwner)
m_Owner = value
End Set
End Property
''' <summary>
''' This sub executes code that generates graphics objects for faster rendering
''' </summary>
''' <remarks></remarks>
Protected Overridable Sub RebuildGraphics()
m_UpdateGraphics = False
End Sub
''' <summary>
''' Renders the Graphic (line, wire, rectangle,...) to the graphics
''' </summary>
''' <param name="Graphics"></param>
''' <remarks></remarks>
Public Overridable Sub Render(ByVal Graphics As Graphics)
If m_UpdateGraphics Then RebuildGraphics()
End Sub
''' <summary>
''' Returns the region that fits all points after rendering to Graphics
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Overridable Function GetRegion() As Region
If m_UpdateGraphics Then RebuildGraphics()
Return Nothing
End Function
''' <summary>
''' Gets / Sets if the graphic must be drawn as selected
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Overridable Property Selected() As Boolean
Get
Return m_Selected
End Get
Set(ByVal value As Boolean)
If value <> m_Selected Then
m_Selected = value
m_UpdateGraphics = True
End If
End Set
End Property
Protected Function GetLayerColor(ByVal Layer As Integer) As System.Drawing.Color
If m_Selected Then
Return m_Owner.Drawing.Layer(Layer).SelectedColor
Else
Return m_Owner.Drawing.Layer(Layer).Color
End If
End Function
''' <summary>
''' Returns a clone of the graphic, having the same owner
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Overridable Function Clone() As Object Implements System.ICloneable.Clone
Dim Cloned As Graphic = DirectCast(Me.MemberwiseClone, Graphic)
Cloned.m_UpdateGraphics = True
Return Cloned
End Function
End Class
''' <summary>
''' This class represents an owner of a graphic element, this can be package, symbol or plain (for schematic)
''' </summary>
''' <remarks></remarks>
Public Class GraphicOwner
Public Package As Package
Public Symbol As Symbol
Public Plain As Plain
Public Sheet As Sheet
Public Library As Library
Public Segment As Segment
Public Owner As Object
Public Drawing As Drawing
Dim m_Type As GraphicOwnerType
'Public Sub New(ByVal Package As Package)
' Me.Package = Package
' Owner = Package
' m_Type = GraphicOwnerType.GO_Package
' Drawing = Package.Drawing
'End Sub
Public Sub New(ByVal SymbolOrPackage As GraphicSymbol)
Owner = SymbolOrPackage
If TypeOf SymbolOrPackage Is Package Then
m_Type = GraphicOwnerType.GO_Package
Me.Package = CType(SymbolOrPackage, Package)
Else
m_Type = GraphicOwnerType.GO_Symbol
Me.Symbol = CType(SymbolOrPackage, Symbol)
End If
Drawing = SymbolOrPackage.Drawing
End Sub
Public Sub New(ByVal Plain As Plain)
Me.Plain = Plain
Owner = Plain
m_Type = GraphicOwnerType.GO_Plain
Drawing = Plain.Sheet.Schematic.Drawing
End Sub
Public Sub New(ByVal Library As Library)
Me.Library = Library
Owner = Library
m_Type = GraphicOwnerType.GO_Library
Drawing = Library.Drawing
End Sub
Public Sub New(ByVal Sheet As Sheet)
Me.Sheet = Sheet
Owner = Sheet
m_Type = GraphicOwnerType.GO_Sheet
Drawing = Sheet.Schematic.Drawing
End Sub
Public Sub New(ByVal Segment As Segment)
Me.Segment = Segment
Owner = Segment
m_Type = GraphicOwnerType.GO_Segment
Drawing = Segment.Sheet.Schematic.Drawing
End Sub
Public Overridable ReadOnly Property Type() As GraphicOwnerType
Get
Return m_Type
End Get
End Property
End Class
Public Class Vertex
Implements EagleSaveable
Implements ICloneable
Dim m_Location As PointF
Dim m_Curve As Single
Public Sub New(ByVal Polygon As Polygon, ByVal Xml As XmlNode)
m_Location = New PointF(toSingle(Xml.Attributes("x").Value), toSingle(Xml.Attributes("y").Value))
If Xml.Attributes("curve") IsNot Nothing Then m_Curve = toSingle(Xml.Attributes("curve").Value)
End Sub
Public ReadOnly Property Location() As PointF
Get
Return m_Location
End Get
End Property
Public ReadOnly Property X() As Single
Get
Return m_Location.X
End Get
End Property
Public ReadOnly Property Y() As Single
Get
Return m_Location.Y
End Get
End Property
Public ReadOnly Property Curve() As Single
Get
Return m_Curve
End Get
End Property
Public Sub WriteXml(ByVal Doc As System.Xml.XmlDocument, ByVal Node As System.Xml.XmlNode) Implements EagleSaveable.WriteXml
With Node.Attributes
.Append(Doc.CreateAttribute("x")).Value = ToEagleSingle(m_Location.X)
.Append(Doc.CreateAttribute("y")).Value = ToEagleSingle(m_Location.Y)
.Append(Doc.CreateAttribute("curve")).Value = ToEagleSingle(m_Curve)
End With
End Sub
Public ReadOnly Property NodeName() As String Implements EagleSaveable.NodeName
Get
Return "vertex"
End Get
End Property
Public Overridable Function Clone() As Object Implements System.ICloneable.Clone
Return Me.MemberwiseClone
End Function
End Class
Public Class Polygon
Inherits Graphic
Implements EagleSaveable
Dim m_Vertices As New List(Of Vertex)
Dim m_Width As Single
Dim m_Layer As Integer
Dim m_Spacing As Single
Dim m_Pour As PolygonPour = PolygonPour.PP_solid
Dim m_Isolate As Single
Dim m_Orphans As Boolean
Dim m_Thermals As Boolean = True
Dim m_Rank As Integer
Dim m_Pen As Pen
Dim m_Region As Region
Dim m_Path As GraphicsPath
Public Sub New(ByVal Owner As GraphicOwner, ByVal Xml As XmlNode)
MyBase.New(Owner)
m_Width = toSingle(Xml.Attributes("width").Value)
m_Layer = Xml.Attributes("layer").Value
If Xml.Attributes("pour") IsNot Nothing Then m_Pour = [Enum].Parse(GetType(PolygonPour), "PP_" & Xml.Attributes("pour").Value)
If Xml.Attributes("spacing") IsNot Nothing Then m_Spacing = toSingle(Xml.Attributes("spacing").Value)
If Xml.Attributes("isolate") IsNot Nothing Then m_Isolate = toSingle(Xml.Attributes("isolate").Value)
If Xml.Attributes("orphans") IsNot Nothing Then m_Orphans = Functions.ParseBool(Xml.Attributes("orphans").Value)
If Xml.Attributes("thermals") IsNot Nothing Then m_Thermals = Functions.ParseBool(Xml.Attributes("thermals").Value)
If Xml.Attributes("rank") IsNot Nothing Then m_Isolate = Xml.Attributes("rank").Value
Dim VertexNodes As XmlNodeList = Xml.SelectNodes("./vertex")
For Each VertexNode As XmlNode In VertexNodes
m_Vertices.Add(New Vertex(Me, VertexNode))
Next
End Sub
Private Sub AddCurve(ByVal Point0 As PointF, ByVal Point1 As PointF, ByVal Curve As Single, ByVal Path As GraphicsPath)
'http://mymathforum.com/algebra/21368-find-equation-circle-given-two-points-arc-angle.html
Dim d As Single = DistanceF(Point0, Point1) 'distance between 2 points
Dim r2 As Single = d ^ 2 / (2 * (1 - Cos(Curve / 180 * PI))) '=r² of circle
Dim r As Single = Sqrt(r2)
Dim MidPoint As New PointF((Point0.X + Point1.X) / 2, (Point0.Y + Point1.Y) / 2) 'midpoint on line from point 0 to point 1
Dim m As Single = (Point0.X - Point1.X) / (Point1.Y - Point0.Y) ' slope of the perpendicular P0-P1
Dim a As Single = Sqrt(r2 - (d / 2) ^ 2) 'pythagoras distance to center
Dim C As PointF 'center of circle
If (r2 - (d / 2) ^ 2) <= 0 Then
a = 0
End If
If (Point1.Y = Point0.Y) Then
m = 0
End If
Dim Vector As New PointF(Point1.X - Point0.X, Point1.Y - Point0.Y) 'this vector gives the direction of the center of the circle depending on the location of point 1 and 2
If Vector.X <> 0 Then Vector.X = Vector.X / Abs(Vector.X)
If Vector.Y <> 0 Then Vector.Y = Vector.Y / Abs(Vector.Y)
Dim tmp As Single = Vector.X
Vector.X = -Vector.Y
Vector.Y = Vector.X
If Abs(Curve) > 180 Then 'for the center coo we can do +/-sqrt(), if the curve is larger than 180 we need * -1
Vector.X = Vector.X * -1
Vector.Y = Vector.Y * -1
End If
C.X = MidPoint.X + a / Sqrt(m ^ 2 + 1) * Curve / Abs(Curve) * Vector.X
C.Y = MidPoint.Y + (m * a) / Sqrt(m ^ 2 + 1) * Curve / Abs(Curve) * Vector.Y
Dim CurveStart As Single = GetAngle(C, r, Point0) 'get the start angle on the circle always begin at point 0
Dim Rect As New RectangleF
Rect.X = C.X - r 'rectangle the circle fits in
Rect.Y = C.Y - r
Rect.Width = r * 2
Rect.Height = r * 2
Path.AddArc(Rect, CurveStart, Curve)
End Sub
Protected Overrides Sub RebuildGraphics()
Dim PVertex As Vertex
MyBase.RebuildGraphics()
m_Path = New GraphicsPath()
m_Pen = New Pen(GetLayerColor(m_Layer), m_Width)
m_Path.StartFigure()
If m_Vertices.Count > 0 Then
PVertex = m_Vertices(0)
For i As Integer = 1 To m_Vertices.Count - 1
If PVertex.Curve = 0 Then 'straight line
m_Path.AddLine(PVertex.Location, m_Vertices(i).Location)
Else 'curved line (=circle segment)
AddCurve(PVertex.Location, m_Vertices(i).Location, PVertex.Curve, m_Path)
End If
PVertex = m_Vertices(i)
Next
If PVertex.Curve = 0 Then 'straight line
m_Path.AddLine(PVertex.Location, m_Vertices(0).Location)
Else 'curved line (=circle segment)
AddCurve(PVertex.Location, m_Vertices(0).Location, PVertex.Curve, m_Path)
End If
End If
m_Path.CloseFigure()
m_Region = New Region(m_Path)
End Sub
Public Overrides Sub Render(ByVal Graphics As System.Drawing.Graphics)
MyBase.Render(Graphics)
Graphics.DrawPath(m_Pen, m_Path)
Select Case (m_Pour)
Case PolygonPour.PP_cutout
Case PolygonPour.PP_hatch
Graphics.FillPath(New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.Cross, m_Pen.Color, System.Drawing.Color.Transparent), m_Path)
'Graphics.FillPolygon(New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.Cross, m_Pen.Color, System.Drawing.Color.Transparent), m_Points)
Case PolygonPour.PP_solid
Graphics.FillPath(New SolidBrush(m_Pen.Color), m_Path)
End Select
End Sub
Public Overrides Function GetRegion() As Region
MyBase.GetRegion()
Return m_Region
End Function
Public Sub WriteXml(ByVal Doc As System.Xml.XmlDocument, ByVal Node As System.Xml.XmlNode) Implements EagleSaveable.WriteXml
With Node.Attributes
.Append(Doc.CreateAttribute("width")).Value = ToEagleSingle(m_Width)
.Append(Doc.CreateAttribute("layer")).Value = m_Layer
.Append(Doc.CreateAttribute("pour")).Value = Mid([Enum].GetName(GetType(PolygonPour), m_Pour), 4)
.Append(Doc.CreateAttribute("spacing")).Value = ToEagleSingle(m_Spacing)
.Append(Doc.CreateAttribute("isolate")).Value = ToEagleSingle(m_Isolate)
.Append(Doc.CreateAttribute("orphans")).Value = ToEagleBool(m_Orphans)
.Append(Doc.CreateAttribute("thermals")).Value = ToEagleBool(m_Thermals)
.Append(Doc.CreateAttribute("rank")).Value = m_Isolate
End With
For Each Vertex As Vertex In m_Vertices
Vertex.WriteXml(Doc, Node.AppendChild(Doc.CreateAttribute("vertex")))
Next
End Sub
Public ReadOnly Property NodeName() As String Implements EagleSaveable.NodeName
Get
Return "polygon"
End Get
End Property
Public Overrides Function Clone() As Object
Dim Cloned As Polygon = DirectCast(MyBase.Clone(), Polygon)
Cloned.m_Vertices = New List(Of Vertex)
For Each Vertex As Vertex In m_Vertices
Cloned.m_Vertices.Add(Vertex.Clone())
Next
Return Cloned
End Function
End Class
Public Class Pad
Inherits Graphic
Implements EaglePad
Implements EagleSaveable
Dim m_Name As String
Dim m_Location As PointF
Dim m_Drill As Single
Dim m_Diameter As Single = 0
Dim m_Shape As PadShape = PadShape.PS_round
Dim m_Rotation As New Rotation
Dim m_Stop As Boolean = True
Dim m_Thermals As Boolean = True
Dim m_First As Boolean = False
Dim m_Rectangle As New RectangleF
Dim m_Hole As New RectangleF
Dim m_Brush As SolidBrush
Dim m_HoleRegion As Region
Dim m_OctagonPoints() As PointF
Dim m_Region As Region
Public Const DefaultPadColorNumber As Integer = 2
Public Sub New(ByVal Owner As GraphicOwner, ByVal Xml As XmlNode)
MyBase.New(Owner)
m_Name = Xml.Attributes("name").Value
m_Location.X = toSingle(Xml.Attributes("x").Value)
m_Location.Y = toSingle(Xml.Attributes("y").Value)
m_Drill = toSingle(Xml.Attributes("drill").Value)
If Xml.Attributes("diameter") IsNot Nothing Then m_Diameter = toSingle(Xml.Attributes("diameter").Value)
If Xml.Attributes("shape") IsNot Nothing Then m_Shape = [Enum].Parse(GetType(PadShape), "PS_" & Xml.Attributes("shape").Value)
If Xml.Attributes("rot") IsNot Nothing Then m_Rotation = New Rotation(Xml.Attributes("rot").Value)
If Xml.Attributes("stop") IsNot Nothing Then m_Stop = Functions.ParseBool(Xml.Attributes("stop").Value)
If Xml.Attributes("thermals") IsNot Nothing Then m_Thermals = Functions.ParseBool(Xml.Attributes("thermals").Value)
If Xml.Attributes("first") IsNot Nothing Then m_First = Functions.ParseBool(Xml.Attributes("first").Value)
End Sub
Protected Overrides Sub RebuildGraphics()
Dim Path As New GraphicsPath()
Dim Transform As New Matrix()
MyBase.RebuildGraphics()
Select Case m_Shape
Case PadShape.PS_octagon
ReDim m_OctagonPoints(0 To 8)
For i As Integer = 0 To 8
m_OctagonPoints(i) = New PointF(m_Diameter / 2 * Math.Sin(Math.PI / 4 * i + Math.PI / 8), m_Diameter / 2 * Math.Cos(Math.PI / 4 * i + Math.PI / 8))
Next
Path.AddPolygon(m_OctagonPoints)
Transform.Rotate(-45)
Case PadShape.PS_offset
m_Rectangle.X = 0
m_Rectangle.Y = -m_Diameter / 2
m_Rectangle.Height = m_Diameter
m_Rectangle.Width = m_Diameter
Path.AddRectangle(m_Rectangle)
Path.AddRectangle(New RectangleF(-m_Diameter / 2, -m_Diameter / 2, m_Diameter, m_Diameter))
Path.AddRectangle(New RectangleF(m_Diameter / 2, -m_Diameter / 2, m_Diameter, m_Diameter))
Case PadShape.PS_round, PadShape.PS_square, PadShape.PS_long
m_Rectangle.X = -m_Diameter / 2
m_Rectangle.Y = -m_Diameter / 2
m_Rectangle.Width = m_Diameter
m_Rectangle.Height = m_Diameter
Path.AddRectangle(m_Rectangle)
If m_Shape = PadShape.PS_long Then
Path.AddRectangle(New RectangleF(-m_Diameter, -m_Diameter / 2, m_Diameter / 2, m_Diameter / 2))
Path.AddRectangle(New RectangleF(0, -m_Diameter / 2, m_Diameter / 2, m_Diameter / 2))
End If
End Select
m_Hole.X = -m_Diameter / 2
m_Hole.Y = -m_Diameter / 2
m_Hole.Width = m_Drill
m_Hole.Height = m_Drill
m_Brush = New SolidBrush(m_Owner.Drawing.Project().ConvertColor(DefaultPadColorNumber))
m_HoleRegion = New Region(m_Hole)
Transform.Rotate(-m_Rotation.Rotation)
Transform.Translate(m_Location.X, m_Location.Y)
Path.Transform(Transform)
m_Region = New Region(Path)
End Sub
Public Overrides Sub Render(ByVal Graphics As System.Drawing.Graphics)
MyBase.Render(Graphics)
Dim Container As GraphicsContainer = Graphics.BeginContainer()
Graphics.TranslateTransform(m_Location.X, m_Location.Y)
Graphics.RotateTransform(-m_Rotation.Rotation)
Select Case m_Shape
Case PadShape.PS_long
Graphics.ExcludeClip(m_HoleRegion)
Graphics.FillRectangle(m_Brush, m_Rectangle)
Graphics.FillPie(m_Brush, -m_Diameter, -m_Diameter / 2, m_Diameter, m_Diameter, 90, 180)
Graphics.FillPie(m_Brush, 0, -m_Diameter / 2, m_Diameter, m_Diameter, -90, 180)
Case PadShape.PS_octagon
Graphics.ExcludeClip(m_HoleRegion)
Graphics.FillPolygon(m_Brush, m_OctagonPoints)
Case PadShape.PS_offset
Graphics.ExcludeClip(m_HoleRegion)
Graphics.FillRectangle(m_Brush, m_Rectangle)
Graphics.FillPie(m_Brush, -m_Diameter / 2, -m_Diameter / 2, m_Diameter, m_Diameter, 90, 180)
Graphics.FillPie(m_Brush, m_Diameter / 2, -m_Diameter / 2, m_Diameter, m_Diameter, -90, 180)
Case PadShape.PS_round
Graphics.ExcludeClip(m_HoleRegion)
Graphics.FillEllipse(m_Brush, m_Rectangle)
Graphics.ResetClip()
Case PadShape.PS_square
Graphics.ExcludeClip(m_HoleRegion)
Graphics.FillRectangle(m_Brush, m_Rectangle)
Graphics.ResetClip()
End Select
Graphics.EndContainer(Container)
End Sub
Public Overrides Function GetRegion() As Region
MyBase.GetRegion()
Return m_Region
End Function
Public Property Name() As String Implements EaglePad.Name
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Public ReadOnly Property PadType() As PadTypes Implements EaglePad.PadType
Get
Return PadTypes.PadTypeThroughHole
End Get
End Property
Public Property Location() As PointF
Get
Return m_Location
End Get
Set(ByVal value As PointF)
m_Location = value
End Set
End Property
Public Property Drill() As Single
Get
Return m_Drill
End Get
Set(ByVal value As Single)
m_Drill = value
m_UpdateGraphics = True 'remember that we need to update the graphics objects before rendering
End Set
End Property
Public Sub WriteXml(ByVal Doc As System.Xml.XmlDocument, ByVal Node As System.Xml.XmlNode) Implements EagleSaveable.WriteXml
With Node.Attributes
.Append(Doc.CreateAttribute("name")).Value = m_Name
.Append(Doc.CreateAttribute("x")).Value = ToEagleSingle(m_Location.X)
.Append(Doc.CreateAttribute("y")).Value = ToEagleSingle(m_Location.Y)
.Append(Doc.CreateAttribute("drill")).Value = ToEagleSingle(m_Drill)
.Append(Doc.CreateAttribute("diameter")).Value = ToEagleSingle(m_Diameter)
.Append(Doc.CreateAttribute("shape")).Value = Mid([Enum].GetName(GetType(PadShape), m_Shape), 4)
.Append(Doc.CreateAttribute("rot")).Value = m_Rotation.ToString()
.Append(Doc.CreateAttribute("stop")).Value = ToEagleBool(m_Stop)
.Append(Doc.CreateAttribute("thermals")).Value = ToEagleBool(m_Thermals)
.Append(Doc.CreateAttribute("first")).Value = ToEagleBool(m_First)
End With
End Sub
Public ReadOnly Property NodeName() As String Implements EagleSaveable.NodeName
Get
Return "pad"
End Get
End Property
Public Overrides Function Clone() As Object
Dim Cloned As Pad = DirectCast(MyBase.Clone(), Pad)
Cloned.m_Rotation = m_Rotation.Clone()
Return Cloned
End Function
End Class
Public Class SMD
Inherits Graphic
Implements EaglePad
Implements EagleSaveable
Dim m_Location As New PointF
Dim m_Name As String
Dim m_Width As Single
Dim m_Height As Single
Dim m_Layer As Integer
Dim m_RoundNess As Integer
Dim m_Rotation As New Rotation
Dim m_Stop As Boolean
Dim m_Thermals As Boolean
Dim m_Cream As Boolean
Dim m_Rect As New RectangleF
Dim m_GraphicsPath As GraphicsPath
Dim m_Brush As SolidBrush
Dim m_Region As Region
Public Sub New(ByVal Owner As GraphicOwner, ByVal Xml As XmlNode)
MyBase.New(Owner)
m_Name = Xml.Attributes("name").Value
m_Location.X = toSingle(Xml.Attributes("x").Value)
m_Location.Y = toSingle(Xml.Attributes("y").Value)
m_Width = toSingle(Xml.Attributes("dx").Value)
m_Height = toSingle(Xml.Attributes("dy").Value)
m_Layer = Xml.Attributes("layer").Value
If Xml.Attributes("roundness") IsNot Nothing Then m_RoundNess = Xml.Attributes("roundness").Value
If Xml.Attributes("rot") IsNot Nothing Then m_Rotation = New Rotation(Xml.Attributes("rot").Value)
If Xml.Attributes("stop") IsNot Nothing Then m_Stop = Functions.ParseBool(Xml.Attributes("stop").Value)
If Xml.Attributes("thermals") IsNot Nothing Then m_Thermals = Functions.ParseBool(Xml.Attributes("thermals").Value)
If Xml.Attributes("cream") IsNot Nothing Then m_Cream = Functions.ParseBool(Xml.Attributes("cream").Value)
End Sub
Protected Overrides Sub RebuildGraphics()
MyBase.RebuildGraphics()
m_Rect.X = -m_Width
m_Rect.Y = -m_Height
m_Rect.Width = m_Width
m_Rect.Height = m_Height
If m_RoundNess > 0 Then
m_GraphicsPath = Functions.RoundedRectangle(m_Rect, m_RoundNess * m_Height)
m_Region = New Region(m_GraphicsPath)
Else
m_Region = New Region(m_Rect)
End If
Dim Mtrx As New Matrix()
Mtrx.Rotate(-m_Rotation.Rotation)
Mtrx.Translate(m_Location.X, m_Location.Y)
m_Region.Transform(Mtrx)
m_Brush = New SolidBrush(GetLayerColor(m_Layer))
End Sub
Public Overrides Sub Render(ByVal Graphics As System.Drawing.Graphics)
MyBase.Render(Graphics)
Dim Container As GraphicsContainer = Graphics.BeginContainer()
Graphics.TranslateTransform(m_Location.X, m_Location.Y)
Graphics.RotateTransform(-m_Rotation.Rotation)
If m_RoundNess > 0 Then
Graphics.FillPath(m_Brush, m_GraphicsPath)
Else
Graphics.FillRectangle(m_Brush, m_Rect)
End If
Graphics.EndContainer(Container)
'return new RectangleF (m_location.X , m_location.Y
End Sub
Public Overrides Function GetRegion() As Region
MyBase.GetRegion()
Return m_Region
End Function
Public Property Name() As String Implements EaglePad.Name
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
m_UpdateGraphics = True
End Set
End Property
Public ReadOnly Property PadType() As PadTypes Implements EaglePad.PadType
Get
Return PadTypes.PadTypeSMD
End Get
End Property
Public Sub WriteXml(ByVal Doc As System.Xml.XmlDocument, ByVal Node As System.Xml.XmlNode) Implements EagleSaveable.WriteXml
With Node.Attributes
.Append(Doc.CreateAttribute("name")).Value = m_Name
.Append(Doc.CreateAttribute("x")).Value = ToEagleSingle(m_Location.X)
.Append(Doc.CreateAttribute("y")).Value = ToEagleSingle(m_Location.Y)
.Append(Doc.CreateAttribute("dx")).Value = ToEagleSingle(m_Width)
.Append(Doc.CreateAttribute("dy")).Value = ToEagleSingle(m_Height)
.Append(Doc.CreateAttribute("layer")).Value = m_Layer
.Append(Doc.CreateAttribute("roundness")).Value = m_RoundNess
.Append(Doc.CreateAttribute("rot")).Value = m_Rotation.ToString()
.Append(Doc.CreateAttribute("stop")).Value = ToEagleBool(m_Stop)
.Append(Doc.CreateAttribute("thermals")).Value = ToEagleBool(m_Thermals)
.Append(Doc.CreateAttribute("cream")).Value = ToEagleBool(m_Cream)
End With
End Sub
Public ReadOnly Property NodeName() As String Implements EagleSaveable.NodeName
Get
Return "smd"
End Get
End Property
Public Overrides Function Clone() As Object
Dim Cloned As SMD = DirectCast(MyBase.Clone(), SMD)
Cloned.m_Rotation = m_Rotation.Clone()
Return Cloned
End Function
End Class
Public Class Wire
Inherits Graphic
Implements EagleSaveable
Dim m_Points(0 To 1) As PointF
Dim m_Width As Double
Dim m_Layer As Integer
Dim m_Extent As String
Dim m_Style As WireStyle = WireStyle.WS_continuous
Dim m_Cap As WireCap = WireCap.WC_round
Dim m_Curve As Single
Dim m_Pen As Pen
Dim m_Rect As New RectangleF
Dim m_CurveStart As Single
Dim m_CurveEnd As Single
Dim m_CurveSweep As Single
Public Sub New(ByVal Owner As GraphicOwner, ByVal Point1 As PointF, ByVal Point2 As PointF, ByVal Width As Single, ByVal Layer As Integer)
MyBase.New(Owner)
m_Points(0) = Point1
m_Points(1) = Point2
m_Width = Width
m_Layer = Layer
End Sub
Public Sub New(ByVal Owner As GraphicOwner, ByVal Xml As XmlNode)
MyBase.New(Owner)
m_Points(0).X = toSingle(Xml.Attributes("x1").Value)
m_Points(0).Y = toSingle(Xml.Attributes("y1").Value)
m_Points(1).X = toSingle(Xml.Attributes("x2").Value)
m_Points(1).Y = toSingle(Xml.Attributes("y2").Value)
m_Width = toSingle(Xml.Attributes("width").Value)
m_Layer = Xml.Attributes("layer").Value
If Xml.Attributes("extent") IsNot Nothing Then m_Extent = Xml.Attributes("extent").Value
If Xml.Attributes("style") IsNot Nothing Then m_Style = [Enum].Parse(GetType(WireStyle), "WS_" & Xml.Attributes("style").Value)
If Xml.Attributes("curve") IsNot Nothing Then m_Curve = toSingle(Xml.Attributes("curve").Value)
If Xml.Attributes("cap") IsNot Nothing Then m_Cap = [Enum].Parse(GetType(WireCap), "WC_" & Xml.Attributes("cap").Value)
End Sub
Protected Overrides Sub RebuildGraphics()
MyBase.RebuildGraphics()
m_Pen = New Pen(GetLayerColor(m_Layer), m_Width)
Select Case m_Style
Case WireStyle.WS_continuous
m_Pen.DashStyle = DashStyle.Solid
Case WireStyle.WS_dashdot
m_Pen.DashStyle = DashStyle.DashDot
Case WireStyle.WS_longdash
m_Pen.DashStyle = DashStyle.Dash
Case WireStyle.WS_shortdash
m_Pen.DashStyle = DashStyle.Dot
End Select
Select Case m_Cap
Case WireCap.WC_flat
m_Pen.StartCap = LineCap.Flat
m_Pen.EndCap = LineCap.Flat
Case WireCap.WC_round
m_Pen.StartCap = LineCap.Round
m_Pen.EndCap = LineCap.Round
End Select
If m_Curve = 0 Then 'normal line, easy
m_Rect.X = Min(m_Points(0).X - m_Width / 2, m_Points(1).X - m_Width / 2)
m_Rect.Y = Min(m_Points(0).Y - m_Width / 2, m_Points(1).Y - m_Width / 2)
m_Rect.Width = Abs(m_Points(0).X - m_Points(1).X) + m_Width * 2
m_Rect.Height = Abs(m_Points(0).Y - m_Points(1).Y) + m_Width * 2
Else
'http://mymathforum.com/algebra/21368-find-equation-circle-given-two-points-arc-angle.html
Dim d As Single = DistanceF(m_Points(0), m_Points(1)) 'distance between 2 points
Dim r2 As Single = d ^ 2 / (2 * (1 - Cos(m_Curve / 180 * PI))) '=r² of circle
Dim r As Single = Sqrt(r2)
Dim MidPoint As New PointF((m_Points(0).X + m_Points(1).X) / 2, (m_Points(0).Y + m_Points(1).Y) / 2) 'midpoint on line from point 0 to point 1
Dim m As Single = (m_Points(0).X - m_Points(1).X) / (m_Points(1).Y - m_Points(0).Y) ' slope of the perpendicular P0-P1
Dim a As Single = Sqrt(r2 - (d / 2) ^ 2) 'pythagoras distance to center
Dim C As PointF 'center of circle
If (r2 - (d / 2) ^ 2) <= 0 Then
a = 0
End If
If (m_Points(1).Y = m_Points(0).Y) Then
m = 0
End If
'If m_Points(1).X > m_Points(0).X Then
' If m_Points(1).Y > m_Points(0).Y Then
' If m_Curve > 0 Then
' C.X = MidPoint.X - a / Sqrt(m ^ 2 + 1) 'ok
' C.Y = MidPoint.Y - (m * a) / Sqrt(m ^ 2 + 1)
' 'C.Y = MidPoint.Y + (m * a) / Sqrt(m ^ 2 + 1)
' m_CurveStart = GetAngle(C, r, m_Points(1))
' m_CurveEnd = GetAngle(C, r, m_Points(0))
' m_CurveSweep = m_CurveStart - m_CurveEnd
' Else
' C.X = MidPoint.X + a / Sqrt(m ^ 2 + 1) 'ok
' C.Y = MidPoint.Y + (m * a) / Sqrt(m ^ 2 + 1) '-?
' m_CurveStart = GetAngle(C, r, m_Points(1))
' m_CurveEnd = GetAngle(C, r, m_Points(0))
' m_CurveSweep = -(m_CurveStart - m_CurveEnd)
' End If
' Else
' If m_Curve > 0 Then
' C.X = MidPoint.X + a / Sqrt(m ^ 2 + 1)
' C.Y = MidPoint.Y + (m * a) / Sqrt(m ^ 2 + 1)
' m_CurveStart = GetAngle(C, r, m_Points(1))
' m_CurveEnd = GetAngle(C, r, m_Points(0))
' m_CurveSweep = m_CurveStart - m_CurveEnd
' Else
' C.X = MidPoint.X - a / Sqrt(m ^ 2 + 1)
' C.Y = MidPoint.Y - (m * a) / Sqrt(m ^ 2 + 1)
' m_CurveStart = GetAngle(C, r, m_Points(1))
' m_CurveEnd = GetAngle(C, r, m_Points(0))