Skip to content

Commit

Permalink
Marcelshapes for the artwork
Browse files Browse the repository at this point in the history
  • Loading branch information
StijnKuipers committed Sep 20, 2019
1 parent 58b8de8 commit f6a15ec
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 23 deletions.
11 changes: 10 additions & 1 deletion DirtyPCBs/DirtyPCB_BoardRender/DirtyPCB_BoardRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ enum Arguments
Copper,
SkipFix,
None,
TimeOut
TimeOut,
TraceColor
}

public class BoardRenderSettings
Expand All @@ -34,6 +35,8 @@ public class BoardRenderSettings
public string OutputFolder { get; internal set; }
public int TimeOut = -1;
public bool TimeOutExpired = false;
public Color TraceColor;
public bool HasTraceColor = false;
}
public static BoardRenderSettings TheSettings = new BoardRenderSettings();

Expand All @@ -49,11 +52,14 @@ static void Main(string[] args)
Console.WriteLine("Usage:");
Console.WriteLine("DirtyPCB_BoardRender.exe [--soldermask_color {blue,yellow,green,black,white,red}]");
Console.WriteLine("\t[--silkscreen_color {white, black}]");
Console.WriteLine("\t[--trace_color {blue,yellow,green,black,white,red}]");
Console.WriteLine("\t[--copper_color {silver, gold}]");
Console.WriteLine("\t[--timeout {seconds}]");
Console.WriteLine("\t[--skipeaglefix]");
Console.WriteLine("\tinput_path");
Console.WriteLine("\toutput_directory");
Console.WriteLine("");
Console.WriteLine("\tHTML colors in the form of #rrggbb are also supported");

return;
}
Expand All @@ -66,6 +72,7 @@ static void Main(string[] args)
case Arguments.SilkScreen: TheSettings.SilkScreenColor = GerberLibrary.Gerber.ParseColor(args[i]); NextArg = Arguments.None; break;
case Arguments.Copper: TheSettings.CopperColor = GerberLibrary.Gerber.ParseColor(args[i]); NextArg = Arguments.None; break;
case Arguments.SolderMask: TheSettings.SolderMaskColor = GerberLibrary.Gerber.ParseColor(args[i]); NextArg = Arguments.None; break;
case Arguments.TraceColor: TheSettings.TraceColor = GerberLibrary.Gerber.ParseColor(args[i]); NextArg = Arguments.None; TheSettings.HasTraceColor = true; break;
case Arguments.TimeOut: TheSettings.TimeOut = int.Parse(args[i]); NextArg = Arguments.None; break;

case Arguments.None:
Expand All @@ -74,6 +81,7 @@ static void Main(string[] args)
case "--silkscreen_color": NextArg = Arguments.SilkScreen; break;
case "--copper_color": NextArg = Arguments.Copper; break;
case "--soldermask_color": NextArg = Arguments.SolderMask; break;
case "--trace_color": NextArg = Arguments.TraceColor; break;
case "--timeout": NextArg = Arguments.TimeOut; break;
case "--skipeaglefix": NextArg = Arguments.None; Gerber.SkipEagleDrillFix = true; break;
}
Expand Down Expand Up @@ -130,6 +138,7 @@ private static void RunImageGeneration()
BoardRenderColorSet Colors = new BoardRenderColorSet();
Colors.BoardRenderColor = TheSettings.SolderMaskColor;
Colors.BoardRenderTraceColor = TheSettings.SolderMaskColor;
if (TheSettings.HasTraceColor) Colors.BoardRenderTraceColor = TheSettings.TraceColor;

Colors.BoardRenderSilkColor = TheSettings.SilkScreenColor;
Colors.BoardRenderPadColor = TheSettings.CopperColor;
Expand Down
5 changes: 3 additions & 2 deletions GerberLibrary/Artwork Related/SVGWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void DrawLine(Pen P, PointF p1, PointF p2)

}

public void DrawPolyline(Pen p, List<PointF> TheList, bool closed = false, bool clipagainstboundary = false)
public void DrawPolyline(Pen p, List<PointF> TheList, bool closed = false, bool clipagainstboundary = false, float strokewidth = -1)
{
string commands = "";

Expand All @@ -158,7 +158,8 @@ public void DrawPolyline(Pen p, List<PointF> TheList, bool closed = false, bool
scaletrns[0].Y -= CurrentTransform.Elements[5];


double stroke = Math.Sqrt(scaletrns[0].X * scaletrns[0].X + scaletrns[0].Y* scaletrns[0].Y);
double stroke = strokewidth;
if (stroke<=-1) stroke = Math.Sqrt(scaletrns[0].X * scaletrns[0].X + scaletrns[0].Y* scaletrns[0].Y);
var list = TheList.ToArray();
CurrentTransform.TransformPoints(list);
if (clipagainstboundary)
Expand Down
2 changes: 1 addition & 1 deletion GerberLibrary/GerberLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<UseVSHostingProcess>true</UseVSHostingProcess>
<UseVSHostingProcess>false</UseVSHostingProcess>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down
69 changes: 69 additions & 0 deletions Project_Utilities/TilingLibrary/MarcelShape.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ClipperLib;


namespace ArtWork
{
using Path = List<ClipperLib.IntPoint>;
using Paths = List<List<ClipperLib.IntPoint>>;

public class MarcelShape
{
public List<ClipperLib.IntPoint> Vertices = new List<ClipperLib.IntPoint>();

public void ShrinkFromShape(Path shape, float amt=7*3)
{
Path pp = new Path();
pp.AddRange(shape);
Paths pps = new Paths();
pps.Add(pp);
var Res = Clipper.OffsetPolygons(pps, -amt);

if (Res.Count == 1)
{
Vertices.Clear();
Vertices.AddRange(Res[0]);
}
}

public Paths BuildOutlines(float growamt = 5*3)
{

ClipperLib.Clipper cp = new ClipperLib.Clipper();
Path pp = new Path();
pp.AddRange(Vertices);
Paths pps = new Paths();
pps.Add(pp);
var Res = Clipper.OffsetPolygons(pps, growamt, JoinType.jtRound);


return Res;
}

public Paths BuildHoles(float radius = 3*3.0f/2.0f)
{
Paths pps = new Paths();
foreach(var v in Vertices)
{
Path c = new Path();
for(int i =0;i<20;i++)
{
double P = (i * Math.PI * 2) / 20;
double x = Math.Cos(P) * radius + v.X;
double y = Math.Sin(P) * radius + v.Y;

c.Add(new IntPoint((long)x, (long)y));
}

pps.Add(c);
}

return pps;
}
}
}
3 changes: 2 additions & 1 deletion Project_Utilities/TilingLibrary/TINRS-ArtWork.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<PlatformTarget>AnyCPU</PlatformTarget>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<Prefer32Bit>false</Prefer32Bit>
<UseVSHostingProcess>true</UseVSHostingProcess>
<UseVSHostingProcess>false</UseVSHostingProcess>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down Expand Up @@ -57,6 +57,7 @@
<ItemGroup>
<Compile Include="Announcer.cs" />
<Compile Include="DelaunayBuilder.cs" />
<Compile Include="MarcelShape.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="QuadTree.cs" />
<Compile Include="Tiling.cs" />
Expand Down
138 changes: 128 additions & 10 deletions Project_Utilities/TilingLibrary/TINRSArtWorkRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using GerberLibrary.Core;
using ArtWork;
using GerberLibrary.Core;
using GlmNet;
using System;
using System.Collections.Generic;
using System.Drawing;
Expand All @@ -10,6 +12,9 @@

namespace Artwork
{
using Path = List<ClipperLib.IntPoint>;
using Paths = List<List<ClipperLib.IntPoint>>;

public class SolidQuadTreeItem : QuadTreeItem
{
int _x;
Expand Down Expand Up @@ -345,17 +350,16 @@ public void DrawTiling(Settings S, Bitmap MaskBitmap, Graphics G, Color FGColor,
if (S.Mode == Settings.ArtMode.Tiling)
{
if (Clear) G.Clear(BG);
PointF[] ThePoints = new PointF[3] { new PointF(), new PointF(), new PointF() };
Pen P = new Pen(FG, linewidth);
for (int j = 0; j < SubDivPoly.Count; j++)
{
var a = SubDivPoly[j];
for (int i = 0; i < 3; i++)
PointF[] ThePoints = new PointF[a.Vertices.Count];
for (int i = 0; i < a.Vertices.Count; i++)
{
ThePoints[i].X = (float)a.Vertices[i].x;
ThePoints[i].Y = (float)a.Vertices[i].y;
ThePoints[i] = new PointF((float)a.Vertices[i].x,(float)a.Vertices[i].y);
}
G.DrawPolygon(P, ThePoints);
G.DrawPolygon(P, ThePoints);
}
}
}
Expand Down Expand Up @@ -521,7 +525,7 @@ public int BuildStuff(Bitmap Mask, Settings TheSettings)
}
}
}

Delaunay.Build(ArtTree, TheSettings.DegreesOff);

var Elapsed = DateTime.Now - rR;
Expand Down Expand Up @@ -565,7 +569,7 @@ public int BuildStuff(Bitmap Mask, Settings TheSettings)
foreach (var A in SubDivPoly)
{
var M = A.Mid();
float scaler = 1.0f - ((float)(M.x-offs) / width) * TheSettings.xscalesmallerlevel * 0.01f;
float scaler = 1.0f - ((float)(M.x - offs) / width) * TheSettings.xscalesmallerlevel * 0.01f;
//scaler = Math.Max(0, Math.Min(1.0f, scaler));
A.ScaleDown(TheSettings.scalingMode, scaler);
}
Expand All @@ -591,18 +595,80 @@ public int BuildStuff(Bitmap Mask, Settings TheSettings)
}
foreach (var A in SubDivPoly)
{

if (A.depth - TheSettings.scalesmallerlevel <= 1)
{

}
else
{
A.ScaleDown(TheSettings.scalingMode, (1 + scaler * (1.0f / (A.depth - TheSettings.scalesmallerlevel))));

}
}
}
if (TheSettings.distanceToMaskScale != 0)
{
float scaler = Math.Abs(TheSettings.distanceToMaskScale);
if (TheSettings.distanceToMaskScale > 0)
{
scaler = scaler / 10.0f;
}
else
{
scaler = -scaler / 10.0f;
}


float aThresholdLevel = TheSettings.Threshold * 0.01f;

foreach (var A in SubDivPoly)
{

var m = A.Mid();
float sum = GetPixelSum(m, Mask, TheSettings.distanceToMaskRange, aThresholdLevel, TheSettings.InvertSource);
//if (sum > 1) sum = 1;
A.ScaleDown(TheSettings.scalingMode, (scaler * sum));


}
}


if (TheSettings.MarcelPlating)
{
List<Tiling.Polygon> MarcelShapes = new List<Tiling.Polygon>();
foreach (var A in SubDivPoly)
{


MarcelShape MS = new MarcelShape();
MarcelShape MS2 = new MarcelShape();

foreach (var v in A.Vertices)
{
MS2.Vertices.Add(new ClipperLib.IntPoint((long)((v.x+1000)*1000), (long)((v.y+1000)*1000)));
}

MS.ShrinkFromShape(MS2.Vertices, TheSettings.Gap /2 + TheSettings.Rounding/2 );
Paths Ps = new Paths();

Ps.AddRange(MS.BuildOutlines(TheSettings.Rounding / 2.0f));
if (TheSettings.BallRadius > 0) Ps.AddRange(MS.BuildHoles(TheSettings.BallRadius));

foreach(var p in Ps)
{
Tiling.Polygon Poly = new Tiling.Polygon();
Poly.Vertices.AddRange(from a in p select new vec2((a.X) * 0.001f-1000, (a.Y ) * 0.001f-1000));
MarcelShapes.Add(Poly);
}


}
SubDivPoly.Clear();
SubDivPoly = MarcelShapes;
}

var Elapsed = DateTime.Now - rR;
return (int)Elapsed.TotalMilliseconds;
}
Expand All @@ -611,6 +677,58 @@ public int BuildStuff(Bitmap Mask, Settings TheSettings)
return 0;
}



private float GetPixelSum(vec2 m, Bitmap mask, float distanceToMaskRange, float ThresholdLevel, bool invert)
{
float sum = 0;
if (distanceToMaskRange == 0) distanceToMaskRange = 0.001f;
float wrange = distanceToMaskRange * mask.Width * 0.5f;
float hrange = distanceToMaskRange * mask.Width * 0.5f;
float total = 0;
float[] cp = new float[40];
float[] sp = new float[40];
for (int p = 0; p < 40; p++)

{
double P = (p * Math.PI * 2) / 40.0f;
sp[p] = (float)Math.Sin(P) * wrange;
cp[p] = (float)Math.Cos(P) * wrange ;
}

for (int ring = 1;ring<10;ring++)
{
float RW = ring / 10.0f;
for (int p = 0; p < 40; p++)

{
int x = (int)(cp[p] * RW + m.x);
int y = (int)(sp[p] * RW + m.y);
total++;
if (x >= 0 && x < mask.Width)
{
if (y >= 0 && y < mask.Height)
{
var C = mask.GetPixel(x, y);
bool doit = false;
if (invert)
{
doit = C.GetBrightness() > ThresholdLevel;
}
else
{
doit = C.GetBrightness() < ThresholdLevel;
}
if (doit) sum++;

}
}
}

}

return 1000*sum / total;
}
}

}
Loading

0 comments on commit f6a15ec

Please sign in to comment.