public HPoint Add(HPoint p) { return(new HPoint() { X = X + p.X, Y = Y + p.Y }); }
protected override void MoveTo(HPoint p) { if (isPenDown) { if (Numbering && HPoint.LengthAbs(p, lastText)>20) { var tp = ToPoint(p); g.DrawString(counter + "", SystemFonts.IconTitleFont, Brushes.Black, tp.X, tp.Y); counter++; lastText = p; } if (current == p) { g.DrawLine(currentGPen, ToPoint(p), ToPoint(p.Add(new HPoint(1, 1)))); } else { g.DrawLine(currentGPen, ToPoint(current), ToPoint(p)); } } else { if (DebugPenUp) { g.DrawLine(debugPen, ToPoint(current), ToPoint(p)); } } base.MoveTo(p); }
private bool readPoint(out HPoint pt) { pt = new HPoint(); int i; if (!readNumber(out i)) { return(false); } pt.X = i; if (!readWhitespaces()) { return(false); } if (peek() != ',') { return(false); } read(); if (!readNumber(out i)) { return(false); } pt.Y = i; return(true); }
public HPoint Sub(HPoint p) { return(new HPoint() { X = X - p.X, Y = Y - p.Y }); }
public static double Length(HPoint p1, HPoint p2) { var l2 = (p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y); if (l2 < 0) return -Math.Sqrt(-l2); else return Math.Sqrt(l2); }
protected override void MoveTo(HPoint pt) { if (isPenDown) { result.Add(new PenDown() { Points = { pt } }); } else { result.Add(new PenUp() { Points = { pt } }); } base.MoveTo(pt); }
public static double Length(HPoint p1, HPoint p2) { var l2 = (p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y); if (l2 < 0) { return(-Math.Sqrt(-l2)); } else { return(Math.Sqrt(l2)); } }
protected double GetPenUpLength(List<Line> lines) { double length = 0; var pos = new HPoint(); foreach (var item in lines) { if (item.P1 != pos) { length += HPoint.LengthAbs(pos, item.P1); } pos = item.P2; } return length; }
protected virtual void MoveTo(HPoint pt) { if (isPenDown) { PenDownLength += HPoint.LengthAbs(current, pt); Min.X = Math.Min(pt.X, Min.X); Min.Y = Math.Min(pt.Y, Min.Y); Max.X = Math.Max(pt.X, Max.X); Max.Y = Math.Max(pt.Y, Max.Y); } else { PenUpLength += HPoint.LengthAbs(current, pt); } current = pt; }
protected override void VisitPenAbsolute(PenAbsolute item) { if (isPenDown) { foreach (var p in item.Points) { segments.Add(new Line(current, p, currentAttribs)); current = p; } } else { if (item.Points.Count > 0) { current = item.Points.Last(); } } }
internal List<Hpgl.Language.HpglItem> Translate(List<GerberItem> gerberData) { if (PenWidthCompensation > 0) fillStep = (int)(PenWidthCompensation * 1000 * 0.8 * 2); var result = new List<Hpgl.Language.HpglItem>(); foreach (var g in gerberData) { if (g is SetUnitsCommand) { Units = ((SetUnitsCommand)g).Units; } if (g is SetUnitsTypeCommand) { var cmd = (SetUnitsTypeCommand)g; if (cmd.UnitsType != UnitsType.Absolute) { throw new NotImplementedException("Units " + cmd.UnitsType + " are not implemented"); } } if (g is ToolPrepareCommand) { var cmd = (ToolPrepareCommand)g; _currentTool = cmd.Param; _currentAperture = gerberData.OfType<ApertureDefinition>().FirstOrDefault(a => a.Name == cmd.Param); if (_currentAperture == null) { Console.WriteLine("Error: tool prepare - tool not found " + cmd.Param); } } if (g is SetExposureCommand) { var cmd = (SetExposureCommand)g; _exposure = cmd.On; } if (g is XYCommand) { var cmd = (XYCommand)g; var pos = Transform(cmd.X, cmd.Y); if (cmd.Param == "D01") { var ca = _currentAperture as CircularApertureDefinition; if (ca != null) { //result.Add(new SelectPen() { Pen = (int)ca.R * 100 }); var w = 1000 * 25.4 * ca.R / 2; //var size = Transform(w, w); var points = new List<HPoint>(); points.Add(pos); var offset = fillStep; while (offset < w) { points.AddRange(GetOutlinedRect(pos, _lastPosition, offset)); offset += fillStep; } if (points.Count > 1) { points.Add(pos); } result.Add(new PenDown() { Points = points }); } else { result.Add(new PenDown() { Points = { pos } }); } } if (cmd.Param == "D02") { result.Add(new PenUp() { Points = { pos } }); } if (cmd.Param == "D03") { if (_currentAperture == null) { throw new ApplicationException("No selected aperture"); } //result.Add(new PenUp(){Points = {Transform(cmd.X, cmd.Y)}}); //result.Add(new Label() { Text = _currentAperture.Name }); if (_currentAperture is CircularApertureDefinition) { FilledCircle(result, cmd.X, cmd.Y, ((CircularApertureDefinition)_currentAperture).R); } else if (_currentAperture is RectangleApertureDefinition) { var r = (RectangleApertureDefinition)_currentAperture; FilledRectangle(result, cmd.X, cmd.Y, r.X, r.Y); } else { throw new InvalidOperationException("Unknown type of aperture " + _currentAperture); } } _lastPosition = pos; } } return result; }
private HPoint[] GetOutlinedRect(HPoint p1, HPoint p2, int outline) { /* p1 * +--+--+ * \ \ \ * \ \ \ * +--+--+ * p2 p2+len * */ if (p1 == p2) { return new[] { p1, p1, p1, p1 }; } var pt1 = new Point2D(p1.X, p1.Y); var pt2 = new Point2D(p2.X, p2.Y); var vector = Vector2D.FromPoints(pt2, pt1); var points = new[]{ new Point2D(-outline,0), new Point2D(0,outline), new Point2D(vector.Length,outline), new Point2D(vector.Length+outline,0), new Point2D(vector.Length,-outline), new Point2D(0,-outline), new Point2D(-outline,0), }; var rotated = points.Select(pt => pt1.Add(pt.Rotate(vector.Angle))).Select(pt => new HPoint((int)pt.X, (int)pt.Y)).ToArray(); return rotated; }
private void FillRect(List<HpglItem> result, HPoint pt, int w, int h) { do { DrawRectangle(result, pt, w, h); if (w > h) { var ratio = 1.0 * w / h; h = h - fillStep; w = w - (int)(fillStep / ratio); } else { var ratio = 1.0 * w / h; w = w - fillStep; h = h - (int)(fillStep / ratio); } } while (w > 0 && h > 0); }
private bool readPoint(out HPoint pt) { pt = new HPoint(); int i; if (!readNumber(out i)) return false; pt.X = i; if (!readWhitespaces()) return false; if (peek() != ',') return false; read(); if (!readNumber(out i)) return false; pt.Y = i; return true; }
public Line(HPoint p1, HPoint p2, Attributes a) { P1 = p1; P2 = p2; Attribs = new Attributes(a); }
protected override void VisitPenUp(PenUp item) { isPenDown = false; if (item.Points.Count > 0) current = item.Points.Last(); }
private static void DrawRectangle(List<HpglItem> result, HPoint pt, int w, int h) { var p1 = pt.Add(new HPoint() { X = w, Y = h }); var p2 = pt.Add(new HPoint() { X = -w, Y = h }); var p3 = pt.Add(new HPoint() { X = -w, Y = -h }); var p4 = pt.Add(new HPoint() { X = w, Y = -h }); result.Add(new PenUp() { Points = { p1 } }); result.Add(new PenDown() { Points = { p2, p3, p4, p1 } }); result.Add(new PenUp() { }); }
private static void DrawCircle(List<HpglItem> result, HPoint pt, int w) { var q = (int)(w / 1.44); var p1 = pt.Add(new HPoint() { X = w, Y = w }); var p2 = pt.Add(new HPoint() { X = -w, Y = w }); var p3 = pt.Add(new HPoint() { X = -w, Y = -w }); var p4 = pt.Add(new HPoint() { X = w, Y = -w }); /* .4. * 5 3 * . . * 6 2 * . . * 7 1 * .0. */ result.Add(new PenUp() { Points = { pt.Add(0, w) } }); result.Add(new PenDown() { Points = { //pt.Add(0,w), pt.Add(q,q), pt.Add(w,0), pt.Add(q,-q), pt.Add(0,-w), pt.Add(-q,-q), pt.Add(-w,0), pt.Add(-q,q), pt.Add(0,w), } }); result.Add(new PenUp() { }); }
public HPoint Add(HPoint p) { return new HPoint() { X = X + p.X, Y = Y + p.Y }; }
public static List<HpglItem> DrawString(string s, int scale = 6) { var result = new List<HpglItem>(); EnsureCharMap(); bool penUp = true; result.Add(new PenUp()); foreach (var ch in s) { var current = new Point(0, scale); //jeden bod nad carou if (ch < ' ' || ch > '\x7f') { continue; } if (ch != ' ') { var charInstructions = charMap[ch]; for (int i = 0; i < charInstructions.Count; i++) { var instr = charInstructions[i]; if ((instr & 0x40) > 0) { if (penUp) { result.Add(new PenDown()); } penUp = false; } else { if (!penUp) { result.Add(new PenUp()); } penUp = true; } var np = new Point(instr & 0x7, (instr & 0x38) >> 3); //absolutni pozice od 0,0 np = new Point(np.X * scale, np.Y * scale); result.Add(new PenRelative() { Points = { new HPoint(np.X - current.X, np.Y - current.Y) } }); current = np; } } //move penUp = true; var p = new HPoint(6 * scale - current.X, scale - current.Y); result.Add(new PenUp()); result.Add(new PenRelative() { Points = { p } }); } return result; }
public HPoint Mul(HPoint p) { return new HPoint() { X = (int)(values[0, 0] * p.X + values[1, 0] * p.Y + values[2, 0] * 1), Y = (int)(values[0, 1] * p.X + values[1, 1] * p.Y + values[2, 1] * 1) }; }
private HPoint TransformPoint(HPoint p) { var r = transformMatrix.Mul(p); return r; }
private HPoint TransformPointRelative(HPoint p) { var t = new HMatrix(transformMatrix); t[2, 0] = 0; t[2, 1] = 0; return t.Mul(p); }
protected override void VisitPenRelative(PenRelative item) { if (isPenDown) { foreach (var rp in item.Points) { var p = current.Add(rp); segments.Add(new Line(current, p, currentAttribs)); current = p; } } else { if (item.Points.Count > 0) { current = current.Add(item.Points.Last()); } } }
private List<HpglItem> SegmentsToHpgl() { var result = new List<HpglItem>(); result.Add(new Initialization()); var last = new HPoint(); foreach (var line in segments) { if (last != line.P1 || result.Count == 0) { result.Add(new PenUp() { Points = { line.P1 } }); result.Add(new PenDown() { Points = { line.P2 } }); } else { var lastDown = (PenDown)result.Last(); lastDown.Points.Add(line.P2); } last = line.P2; } result.Add(new PenUp()); return result; }
public static double LengthAbs(HPoint p1, HPoint p2) { return Math.Abs(Length(p1, p2)); }
public bool Equals(HPoint p) { return X == p.X && Y == p.Y; }
private void FilledRectangle(List<HpglItem> result, double x, double y, double width, double height) { //mils = 1/1000 inch var w = 1000 * 25.4 * width / 2; var h = 1000 * 25.4 * height / 2; var pt = Transform(x, y); var size = new HPoint((int)w, (int)h); // Transform(w, h); size = CompensatePen(size); FillRect(result, pt, size.X, size.Y); }
protected override void VisitPenDown(PenDown item) { foreach (var p in item.Points) { if (isPenDown && p.Equals(current)) { Console.WriteLine("Optimized dup"); continue; } isPenDown = true; segments.Add(new Line(current, p, currentAttribs)); current = p; } }
private bool IsNear(HPoint a, HPoint b, double maxDestination) { if (maxDestination < 1) { return a.X == b.X && a.Y == b.Y; } else { //optimalizace //if (Math.Abs(a.X - b.X) + Math.Abs(a.Y - b.Y) > maxDestination) // return false; return HPoint.LengthAbs(a, b) < maxDestination; } }
private HPoint CompensatePen(HPoint size) { if (PenWidthCompensation > 0) { size = size.Sub(new HPoint((int)(1000 * PenWidthCompensation), (int)(1000 * PenWidthCompensation))); } return size; }
public HPoint Sub(HPoint p) { return new HPoint() { X = X - p.X, Y = Y - p.Y }; }
public static double LengthAbs(HPoint p1, HPoint p2) { return(Math.Abs(Length(p1, p2))); }
private int FillCircle(List<HpglItem> result, HPoint pt, int radius) { do { DrawCircle(result, pt, radius); radius = radius - fillStep; } while (radius > 0); return radius; }
public bool Equals(HPoint p) { return(X == p.X && Y == p.Y); }
private void FilledCircle(List<HpglItem> result, double x, double y, double r) { //mils = 1/1000 inch var w = 1000 * 25.4 * r / 2; var pt = Transform(x, y); var size = new HPoint((int)w, (int)w); // Transform(w, w); size = CompensatePen(size); w = FillCircle(result, pt, size.X); }