├── .gitignore
├── Documentation
└── the_gerber_file_format_specification.pdf
├── LICENSE
├── README.md
└── Source
├── GerberWriter.CodewheelGenerator
├── GerberWriter.CodewheelGenerator.csproj
└── Program.cs
├── GerberWriter.sln
└── GerberWriter
├── Apertures
├── CircleAperture.cs
└── IAperture.cs
├── Constants
├── Interpolation.cs
├── Operation.cs
├── QuadrantMode.cs
└── Region.cs
├── Entities
├── Arc.cs
├── IEntity.cs
├── Line.cs
├── Point.cs
├── Polyfill.cs
└── Polyline.cs
├── Generators
└── ArcFill.cs
├── GerberWriter.cs
└── GerberWriter.csproj
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.suo
8 | *.user
9 | *.userosscache
10 | *.sln.docstates
11 |
12 | # User-specific files (MonoDevelop/Xamarin Studio)
13 | *.userprefs
14 |
15 | # Build results
16 | [Dd]ebug/
17 | [Dd]ebugPublic/
18 | [Rr]elease/
19 | [Rr]eleases/
20 | x64/
21 | x86/
22 | bld/
23 | [Bb]in/
24 | [Oo]bj/
25 | [Ll]og/
26 |
27 | # Visual Studio 2015/2017 cache/options directory
28 | .vs/
29 | # Uncomment if you have tasks that create the project's static files in wwwroot
30 | #wwwroot/
31 |
32 | # Visual Studio 2017 auto generated files
33 | Generated\ Files/
34 |
35 | # MSTest test Results
36 | [Tt]est[Rr]esult*/
37 | [Bb]uild[Ll]og.*
38 |
39 | # NUNIT
40 | *.VisualState.xml
41 | TestResult.xml
42 |
43 | # Build Results of an ATL Project
44 | [Dd]ebugPS/
45 | [Rr]eleasePS/
46 | dlldata.c
47 |
48 | # Benchmark Results
49 | BenchmarkDotNet.Artifacts/
50 |
51 | # .NET Core
52 | project.lock.json
53 | project.fragment.lock.json
54 | artifacts/
55 | **/Properties/launchSettings.json
56 |
57 | # StyleCop
58 | StyleCopReport.xml
59 |
60 | # Files built by Visual Studio
61 | *_i.c
62 | *_p.c
63 | *_i.h
64 | *.ilk
65 | *.meta
66 | *.obj
67 | *.iobj
68 | *.pch
69 | *.pdb
70 | *.ipdb
71 | *.pgc
72 | *.pgd
73 | *.rsp
74 | *.sbr
75 | *.tlb
76 | *.tli
77 | *.tlh
78 | *.tmp
79 | *.tmp_proj
80 | *.log
81 | *.vspscc
82 | *.vssscc
83 | .builds
84 | *.pidb
85 | *.svclog
86 | *.scc
87 |
88 | # Chutzpah Test files
89 | _Chutzpah*
90 |
91 | # Visual C++ cache files
92 | ipch/
93 | *.aps
94 | *.ncb
95 | *.opendb
96 | *.opensdf
97 | *.sdf
98 | *.cachefile
99 | *.VC.db
100 | *.VC.VC.opendb
101 |
102 | # Visual Studio profiler
103 | *.psess
104 | *.vsp
105 | *.vspx
106 | *.sap
107 |
108 | # Visual Studio Trace Files
109 | *.e2e
110 |
111 | # TFS 2012 Local Workspace
112 | $tf/
113 |
114 | # Guidance Automation Toolkit
115 | *.gpState
116 |
117 | # ReSharper is a .NET coding add-in
118 | _ReSharper*/
119 | *.[Rr]e[Ss]harper
120 | *.DotSettings.user
121 |
122 | # JustCode is a .NET coding add-in
123 | .JustCode
124 |
125 | # TeamCity is a build add-in
126 | _TeamCity*
127 |
128 | # DotCover is a Code Coverage Tool
129 | *.dotCover
130 |
131 | # AxoCover is a Code Coverage Tool
132 | .axoCover/*
133 | !.axoCover/settings.json
134 |
135 | # Visual Studio code coverage results
136 | *.coverage
137 | *.coveragexml
138 |
139 | # NCrunch
140 | _NCrunch_*
141 | .*crunch*.local.xml
142 | nCrunchTemp_*
143 |
144 | # MightyMoose
145 | *.mm.*
146 | AutoTest.Net/
147 |
148 | # Web workbench (sass)
149 | .sass-cache/
150 |
151 | # Installshield output folder
152 | [Ee]xpress/
153 |
154 | # DocProject is a documentation generator add-in
155 | DocProject/buildhelp/
156 | DocProject/Help/*.HxT
157 | DocProject/Help/*.HxC
158 | DocProject/Help/*.hhc
159 | DocProject/Help/*.hhk
160 | DocProject/Help/*.hhp
161 | DocProject/Help/Html2
162 | DocProject/Help/html
163 |
164 | # Click-Once directory
165 | publish/
166 |
167 | # Publish Web Output
168 | *.[Pp]ublish.xml
169 | *.azurePubxml
170 | # Note: Comment the next line if you want to checkin your web deploy settings,
171 | # but database connection strings (with potential passwords) will be unencrypted
172 | *.pubxml
173 | *.publishproj
174 |
175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
176 | # checkin your Azure Web App publish settings, but sensitive information contained
177 | # in these scripts will be unencrypted
178 | PublishScripts/
179 |
180 | # NuGet Packages
181 | *.nupkg
182 | # The packages folder can be ignored because of Package Restore
183 | **/[Pp]ackages/*
184 | # except build/, which is used as an MSBuild target.
185 | !**/[Pp]ackages/build/
186 | # Uncomment if necessary however generally it will be regenerated when needed
187 | #!**/[Pp]ackages/repositories.config
188 | # NuGet v3's project.json files produces more ignorable files
189 | *.nuget.props
190 | *.nuget.targets
191 |
192 | # Microsoft Azure Build Output
193 | csx/
194 | *.build.csdef
195 |
196 | # Microsoft Azure Emulator
197 | ecf/
198 | rcf/
199 |
200 | # Windows Store app package directories and files
201 | AppPackages/
202 | BundleArtifacts/
203 | Package.StoreAssociation.xml
204 | _pkginfo.txt
205 | *.appx
206 |
207 | # Visual Studio cache files
208 | # files ending in .cache can be ignored
209 | *.[Cc]ache
210 | # but keep track of directories ending in .cache
211 | !*.[Cc]ache/
212 |
213 | # Others
214 | ClientBin/
215 | ~$*
216 | *~
217 | *.dbmdl
218 | *.dbproj.schemaview
219 | *.jfm
220 | *.pfx
221 | *.publishsettings
222 | orleans.codegen.cs
223 |
224 | # Including strong name files can present a security risk
225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
226 | #*.snk
227 |
228 | # Since there are multiple workflows, uncomment next line to ignore bower_components
229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
230 | #bower_components/
231 |
232 | # RIA/Silverlight projects
233 | Generated_Code/
234 |
235 | # Backup & report files from converting an old project file
236 | # to a newer Visual Studio version. Backup files are not needed,
237 | # because we have git ;-)
238 | _UpgradeReport_Files/
239 | Backup*/
240 | UpgradeLog*.XML
241 | UpgradeLog*.htm
242 | ServiceFabricBackup/
243 | *.rptproj.bak
244 |
245 | # SQL Server files
246 | *.mdf
247 | *.ldf
248 | *.ndf
249 |
250 | # Business Intelligence projects
251 | *.rdl.data
252 | *.bim.layout
253 | *.bim_*.settings
254 | *.rptproj.rsuser
255 |
256 | # Microsoft Fakes
257 | FakesAssemblies/
258 |
259 | # GhostDoc plugin setting file
260 | *.GhostDoc.xml
261 |
262 | # Node.js Tools for Visual Studio
263 | .ntvs_analysis.dat
264 | node_modules/
265 |
266 | # Visual Studio 6 build log
267 | *.plg
268 |
269 | # Visual Studio 6 workspace options file
270 | *.opt
271 |
272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
273 | *.vbw
274 |
275 | # Visual Studio LightSwitch build output
276 | **/*.HTMLClient/GeneratedArtifacts
277 | **/*.DesktopClient/GeneratedArtifacts
278 | **/*.DesktopClient/ModelManifest.xml
279 | **/*.Server/GeneratedArtifacts
280 | **/*.Server/ModelManifest.xml
281 | _Pvt_Extensions
282 |
283 | # Paket dependency manager
284 | .paket/paket.exe
285 | paket-files/
286 |
287 | # FAKE - F# Make
288 | .fake/
289 |
290 | # JetBrains Rider
291 | .idea/
292 | *.sln.iml
293 |
294 | # CodeRush
295 | .cr/
296 |
297 | # Python Tools for Visual Studio (PTVS)
298 | __pycache__/
299 | *.pyc
300 |
301 | # Cake - Uncomment if you are using it
302 | # tools/**
303 | # !tools/packages.config
304 |
305 | # Tabs Studio
306 | *.tss
307 |
308 | # Telerik's JustMock configuration file
309 | *.jmconfig
310 |
311 | # BizTalk build output
312 | *.btp.cs
313 | *.btm.cs
314 | *.odx.cs
315 | *.xsd.cs
316 |
317 | # OpenCover UI analysis results
318 | OpenCover/
319 |
320 | # Azure Stream Analytics local run output
321 | ASALocalRun/
322 |
323 | # MSBuild Binary and Structured Log
324 | *.binlog
325 |
326 | # NVidia Nsight GPU debugger configuration file
327 | *.nvuser
328 |
329 | # MFractors (Xamarin productivity tool) working folder
330 | .mfractor/
331 |
--------------------------------------------------------------------------------
/Documentation/the_gerber_file_format_specification.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/macaba/GerberWriter/7af81694a7e6df2fc3dea3a7f47e6a0e10cfbe4a/Documentation/the_gerber_file_format_specification.pdf
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 macaba
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # GerberWriter
2 |
3 | A cross-platform library written in C#, targeting .NET Core, for writing RS-274X ("Gerber") files.
4 |
5 | ## GerberWriter.CodewheelGenerator
6 |
7 | A cross-platform console app for generating reflective encoder codewheels.
8 |
--------------------------------------------------------------------------------
/Source/GerberWriter.CodewheelGenerator/GerberWriter.CodewheelGenerator.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.2
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/Source/GerberWriter.CodewheelGenerator/Program.cs:
--------------------------------------------------------------------------------
1 | using GerberWriter.Entities;
2 | using GerberWriter.Generators;
3 | using System;
4 | using System.IO;
5 |
6 | namespace GerberWriter.CodeWheelGenerator
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | GerberWriter writer = new GerberWriter();
13 |
14 | CircleAperture aperture = new CircleAperture()
15 | {
16 | Index = 10,
17 | Diameter = 0.01
18 | };
19 |
20 | Point centre = new Point() { X = -20, Y = -20 };
21 |
22 | for (int i = 0; i < 1800; i++)
23 | {
24 | ArcFill arcFill = new ArcFill(aperture, centre, 0.1, i * 0.2, 97.5, 100);
25 | writer.AddEntity(arcFill);
26 | }
27 |
28 | var validationErrors = writer.Validate();
29 | if (!string.IsNullOrWhiteSpace(validationErrors))
30 | {
31 | Console.WriteLine("Validation errors:");
32 | Console.Write(validationErrors);
33 | }
34 |
35 | File.WriteAllText("Codewheel.ger", writer.Generate());
36 |
37 | Console.WriteLine("Complete.");
38 | Console.ReadKey();
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Source/GerberWriter.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.28307.168
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GerberWriter", "GerberWriter\GerberWriter.csproj", "{6ED32C50-44D1-482F-AA55-45ADA544BB0E}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GerberWriter.CodewheelGenerator", "GerberWriter.CodewheelGenerator\GerberWriter.CodewheelGenerator.csproj", "{BC75B015-B63A-4E6D-8E23-BBBC40C7C02D}"
9 | EndProject
10 | Global
11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 | Debug|Any CPU = Debug|Any CPU
13 | Release|Any CPU = Release|Any CPU
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {6ED32C50-44D1-482F-AA55-45ADA544BB0E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17 | {6ED32C50-44D1-482F-AA55-45ADA544BB0E}.Debug|Any CPU.Build.0 = Debug|Any CPU
18 | {6ED32C50-44D1-482F-AA55-45ADA544BB0E}.Release|Any CPU.ActiveCfg = Release|Any CPU
19 | {6ED32C50-44D1-482F-AA55-45ADA544BB0E}.Release|Any CPU.Build.0 = Release|Any CPU
20 | {BC75B015-B63A-4E6D-8E23-BBBC40C7C02D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {BC75B015-B63A-4E6D-8E23-BBBC40C7C02D}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {BC75B015-B63A-4E6D-8E23-BBBC40C7C02D}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {BC75B015-B63A-4E6D-8E23-BBBC40C7C02D}.Release|Any CPU.Build.0 = Release|Any CPU
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {FDACB6AC-19AD-4251-8089-F2C53B00027A}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/Source/GerberWriter/Apertures/CircleAperture.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace GerberWriter.Entities
6 | {
7 | public class CircleAperture : IAperture
8 | {
9 | public double Diameter { get; set; }
10 | public int Index { get; set; }
11 | private bool addedToFile = false;
12 |
13 | public string Validate()
14 | {
15 | //To do - check that a valid aperture exists
16 | string error = "";
17 | return error;
18 | }
19 |
20 | public string Generate()
21 | {
22 | var aperture = "";
23 | if (!addedToFile)
24 | {
25 | addedToFile = true;
26 | aperture += string.Format("%ADD{0}C,{1}*%\n", Index, Diameter);
27 | }
28 | aperture += string.Format("D{0}*\n", Index);
29 | return aperture;
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Source/GerberWriter/Apertures/IAperture.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace GerberWriter.Entities
6 | {
7 | public interface IAperture
8 | {
9 | string Validate();
10 | string Generate();
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/Source/GerberWriter/Constants/Interpolation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace GerberWriter.Entities
6 | {
7 | public static class Interpolation
8 | {
9 | public const string Linear = "G01*\n";
10 | public const string CircularClockwise = "G02*\n";
11 | public const string CircularCounterClockwise = "G03*\n";
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/Source/GerberWriter/Constants/Operation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace GerberWriter.Entities
6 | {
7 | public static class Operation
8 | {
9 | public const string Draw = "D01";
10 | public const string Move = "D02";
11 | public const string OperationFlash = "D03";
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/Source/GerberWriter/Constants/QuadrantMode.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace GerberWriter.Entities
6 | {
7 | public static class QuadrantMode
8 | {
9 | public const string Single = "G74*\n";
10 | public const string Multi = "G75*\n";
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/Source/GerberWriter/Constants/Region.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace GerberWriter.Entities
6 | {
7 | public static class Region
8 | {
9 | public const string Enable = "G36*\n";
10 | public const string Disable = "G37*\n";
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/Source/GerberWriter/Entities/Arc.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace GerberWriter.Entities
6 | {
7 | public class Arc : IEntity
8 | {
9 | public IAperture Aperture { get; set; }
10 | public Point Start { get; set; }
11 | public Point End { get; set; }
12 | public Point Centre { get; set; }
13 |
14 | public static Arc Calculate(IAperture aperture, Point centre, double radius, double arcHeadingDeg, double arcWidthDeg)
15 | {
16 | double arcHeadingRadians = arcHeadingDeg * (Math.PI / 180);
17 | double arcWidthRadians = arcWidthDeg * (Math.PI / 180);
18 |
19 | double startAngle = arcHeadingRadians + (arcWidthRadians / 2);
20 | double endAngle = arcHeadingRadians - (arcWidthRadians / 2);
21 |
22 | return new Arc()
23 | {
24 | Aperture = aperture,
25 | Start = new Point()
26 | {
27 | X = centre.X + (radius * Math.Cos(startAngle)),
28 | Y = centre.Y + (radius * Math.Sin(startAngle))
29 | },
30 | End = new Point()
31 | {
32 | X = centre.X + (radius * Math.Cos(endAngle)),
33 | Y = centre.Y + (radius * Math.Sin(endAngle))
34 | },
35 | Centre = centre
36 | };
37 | }
38 |
39 | public static string Draw(Point start, Point end, Point centre)
40 | {
41 | //return string.Format("X{0:####00000}Y{1:####00000}I{2:####00000}J{3:####00000}{4}*\n", end.X * 100000, end.Y * 100000, Math.Abs(centre.X - start.X) * -100000, Math.Abs(centre.Y - start.Y) * -100000, Operation.Draw);
42 | //return string.Format("X{0:####00000}Y{1:####00000}I{2:####00000}J{3:####00000}{4}*\n", end.X * 100000, end.Y * 100000, -start.X * 100000, -start.Y * 100000, Operation.Draw);
43 | return string.Format("X{0:####00000}Y{1:####00000}I{2:####00000}J{3:####00000}{4}*\n", end.X * 100000, end.Y * 100000, centre.X - start.X * 100000, centre.Y - start.Y * 100000, Operation.Draw);
44 | }
45 |
46 | public string Validate()
47 | {
48 | return "";
49 | }
50 |
51 | public string Generate()
52 | {
53 | string gerber = "";
54 | gerber += Aperture.Generate();
55 | gerber += QuadrantMode.Multi;
56 | gerber += Interpolation.CircularClockwise;
57 | gerber += Start.Generate(Operation.Move);
58 | gerber += Draw(Start, End, Centre);
59 | return gerber;
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/Source/GerberWriter/Entities/IEntity.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace GerberWriter
6 | {
7 | public interface IEntity
8 | {
9 | string Validate();
10 | string Generate();
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/Source/GerberWriter/Entities/Line.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace GerberWriter.Entities
6 | {
7 | public class Line : IEntity
8 | {
9 | public IAperture Aperture { get; set; }
10 | public Point Start { get; set; }
11 | public Point End { get; set; }
12 |
13 | public string Validate()
14 | {
15 | string error = "";
16 | if (Start.X == End.X && Start.Y == End.Y)
17 | {
18 | error += "Line has zero length.\n";
19 | }
20 | return error;
21 | }
22 |
23 | public string Generate()
24 | {
25 | string gerber = "";
26 | gerber += Aperture.Generate();
27 | gerber += Interpolation.Linear;
28 | gerber += Start.Generate(Operation.Move);
29 | gerber += End.Generate(Operation.Draw);
30 | return gerber;
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/Source/GerberWriter/Entities/Point.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace GerberWriter.Entities
6 | {
7 | public class Point
8 | {
9 | public double X { get; set; }
10 | public double Y { get; set; }
11 |
12 | public string Generate(string gerberOperation)
13 | {
14 | return string.Format("X{0:####00000}Y{1:####00000}{2}*\n", X * 100000, Y * 100000, gerberOperation);
15 | }
16 | }
17 | }
--------------------------------------------------------------------------------
/Source/GerberWriter/Entities/Polyfill.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace GerberWriter.Entities
6 | {
7 | public class Polyfill : IEntity
8 | {
9 | public IAperture Aperture { get; set; }
10 | public List Points { get; set; }
11 |
12 | public Polyfill()
13 | {
14 | Points = new List();
15 | }
16 |
17 | public void AddPoint(double x, double y)
18 | {
19 | Points.Add(new Point() { X = x, Y = y });
20 | }
21 |
22 | public string Validate()
23 | {
24 | //To do - check the contour is closed, and that a valid aperture exists
25 | string error = "";
26 | return error;
27 | }
28 |
29 | public string Generate()
30 | {
31 | string gerber = "";
32 | if (Points.Count > 1)
33 | {
34 | gerber += Aperture.Generate();
35 | gerber += Interpolation.Linear;
36 | gerber += Region.Enable;
37 | gerber += Points[0].Generate(Operation.Move);
38 | for (int i = 1; i < Points.Count; i++)
39 | {
40 | gerber += Points[i].Generate(Operation.Draw);
41 | }
42 | gerber += Region.Disable;
43 | }
44 | return gerber;
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/Source/GerberWriter/Entities/Polyline.cs:
--------------------------------------------------------------------------------
1 | using GerberWriter.Entities;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Text;
5 |
6 | namespace GerberWriter.Entities
7 | {
8 | public class Polyline : IEntity
9 | {
10 | public IAperture Aperture { get; set; }
11 | public List Points { get; set; }
12 |
13 | public Polyline()
14 | {
15 | Points = new List();
16 | }
17 |
18 | public void AddPoint(double x, double y)
19 | {
20 | Points.Add(new Point() { X = x, Y = y });
21 | }
22 |
23 | public string Validate()
24 | {
25 | //To do - check that a valid aperture exists
26 | string error = "";
27 | return error;
28 | }
29 |
30 | public string Generate()
31 | {
32 | string gerber = "";
33 | if (Points.Count > 1)
34 | {
35 | gerber += Aperture.Generate();
36 | gerber += Interpolation.Linear;
37 | gerber += Points[0].Generate(Operation.Move);
38 | for (int i = 1; i < Points.Count; i++)
39 | {
40 | gerber += Points[i].Generate(Operation.Draw);
41 | }
42 | }
43 | return gerber;
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/Source/GerberWriter/Generators/ArcFill.cs:
--------------------------------------------------------------------------------
1 | using GerberWriter.Entities;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Text;
5 |
6 | namespace GerberWriter.Generators
7 | {
8 | public class ArcFill : IEntity
9 | {
10 | public IAperture Aperture { get; private set; }
11 | public Arc InsideArc { get; private set; }
12 | public Arc OutsideArc { get; private set; }
13 |
14 | public ArcFill(IAperture aperture, Point centre, double arcWidthDegrees, double arcHeadingDegrees, double radialInsideDistance, double radialOutsideDistance)
15 | {
16 | Aperture = aperture;
17 | InsideArc = Arc.Calculate(aperture, centre, radialInsideDistance, arcHeadingDegrees, arcWidthDegrees);
18 | OutsideArc = Arc.Calculate(aperture, centre, radialOutsideDistance, arcHeadingDegrees, arcWidthDegrees);
19 | }
20 |
21 | public string Validate() { return ""; }
22 |
23 | public string Generate()
24 | {
25 | string gerber = "";
26 | gerber += Aperture.Generate();
27 | gerber += Region.Enable;
28 | gerber += QuadrantMode.Multi;
29 | gerber += OutsideArc.Start.Generate(Operation.Move);
30 |
31 | gerber += Interpolation.CircularClockwise;
32 | gerber += Arc.Draw(OutsideArc.Start, OutsideArc.End, OutsideArc.Centre);
33 |
34 | gerber += Interpolation.Linear;
35 | gerber += InsideArc.End.Generate(Operation.Draw);
36 |
37 | gerber += Interpolation.CircularCounterClockwise;
38 | gerber += Arc.Draw(InsideArc.End, InsideArc.Start, InsideArc.Centre);
39 |
40 | gerber += Interpolation.Linear;
41 | gerber += OutsideArc.Start.Generate(Operation.Draw);
42 |
43 | gerber += Region.Disable;
44 | return gerber;
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/Source/GerberWriter/GerberWriter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace GerberWriter
6 | {
7 | public class GerberWriter
8 | {
9 | List entities = new List();
10 |
11 | public void AddEntity(IEntity entity)
12 | {
13 | entities.Add(entity);
14 | }
15 |
16 | public string Validate()
17 | {
18 | string errors = "";
19 | foreach (var entity in entities)
20 | {
21 | errors += entity.Validate();
22 | }
23 | return errors;
24 | }
25 |
26 | public string Generate()
27 | {
28 | string file = "";
29 |
30 | file += "G04 Generated by GerberWriter *\n";
31 | file += "%FSLAX45Y45*%\n";
32 | file += "%MOMM*%\n";
33 | file += "%LPD*%\n";
34 |
35 | foreach(var entity in entities)
36 | {
37 | file += entity.Generate();
38 | }
39 |
40 | file += "M02*";
41 | return file;
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/Source/GerberWriter/GerberWriter.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Library
5 | netcoreapp2.2
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------