public bool ConvertBoardFile(string SourceFilename, string OutputFolder, string ProjectName) { bool result = false; int net_index = 1; DesignRules designRules = new DesignRules(); PartMap = new RenameMap(); k.LayerDescriptor layer; Trace(string.Format("Reading board file {0}", SourceFilename)); board = EagleFile.LoadFromXmlFile(SourceFilename); // if (board != null) { libraryConverter = new LibraryConverter(Parent); ConvertComponentLibraries(OutputFolder, false); k.Pcb.kicad_pcb k_pcb = new k.Pcb.kicad_pcb(); k_pcb.Modules = new List <k.ModuleDef.Module>(); k_pcb.Drawings = new List <k.Pcb.graphic_base>(); // paper and size: get the page size PageStr = "A4"; PageSize = new SizeF(297, 210); foreach (Element element in board.Drawing.Board.Elements.Element) { // if (element.Library == "frames") { //todo: //ConvertFrame(element.Package); break; } } k_pcb.Page = PageStr; // offset from bottom left DrawingOffset = new PointF(2 * Common.inch_to_mm, 2 * Common.inch_to_mm); k_pcb.Setup.grid_origin = StrToPoint_Board("0", "0"); //testFont(k_pcb); // ** debug // get list of part names foreach (Element element in board.Drawing.Board.Elements.Element) { PartMap.Add(element.Name); } PartMap.Annotate(); // layers? #region ==== designrules ==== foreach (Param param in board.Drawing.Board.Designrules.Param) { designRules.Add(param.Name, param.Value); } #endregion #region ==== Plain - text ==== foreach (Text text in board.Drawing.Board.Plain.Text) { bool mirror; int angle = Common.xGetAngleFlip(text.Rot, out mirror); layer = ConvertLayer(text.Layer); if (layer != null) { k.Pcb.gr_text k_text = new k.Pcb.gr_text( text.mText, StrToPoint_Board(text.X, text.Y), layer.Name, new SizeF(Common.StrToVal_mm(text.Size), Common.StrToVal_mm(text.Size)), Common.GetTextThickness_mm(text), angle ); k_text.effects.horiz_align = k.TextJustify.left; SizeF textSize = strokeFont.GetTextSize(text.mText, k_text.effects); PointF offset = new PointF(textSize.Width / 2, textSize.Height / 2); // TODO: spin switch ((int)ExtRotation.Parse(text.Rot).Rotation) { case 0: if (mirror) { k_text.Position.At.Y -= offset.Y; } else { k_text.Position.At.Y -= offset.Y; } break; case 90: if (mirror) { k_text.Position.At.X += offset.Y; k_text.Position.At.Y -= textSize.Width; } else { k_text.Position.At.X -= offset.Y; } break; case 180: if (mirror) { k_text.Position.At.Y += offset.Y; } else { k_text.Position.At.Y += textSize.Height; } break; case 270: if (mirror) { k_text.Position.At.X -= offset.Y; k_text.Position.At.Y += textSize.Width; } else { k_text.Position.At.X += offset.Y; } break; } k_pcb.Drawings.Add(k_text); } } #endregion #region ==== Plain - lines ==== foreach (Wire wire in board.Drawing.Board.Plain.Wire) { layer = ConvertLayer(wire.Layer); if (layer != null) { float width = Common.StrToVal_mm(wire.Width); //todo: arcs k.Pcb.gr_line k_line = new k.Pcb.gr_line( StrToPoint_Board(wire.X1, wire.Y1), StrToPoint_Board(wire.X2, wire.Y2), layer.Name, width ); k_pcb.Drawings.Add(k_line); } } #endregion #region ==== Plain - rectangle ==== // convert to unconnected zones foreach (EagleImport.Rectangle rect in board.Drawing.Board.Plain.Rectangle) { layer = ConvertLayer(rect.Layer); if (layer != null) { PointF p1 = StrToPoint_Board(rect.X1, rect.Y1); PointF p2 = StrToPoint_Board(rect.X2, rect.Y2); k.Pcb.Zone zone = new k.Pcb.Zone(); zone.layer = layer.Name; zone.net = 0; zone.net_name = ""; zone.hatch_pitch = 0.508f; zone.connect_pads_clearance = 0.508f; zone.min_thickness = 0.001f; zone.is_filled = false; zone.fill_arc_segments = 16; zone.fill_thermal_gap = 0.508f; zone.fill_thermal_bridge_width = 0.508f; zone.polygon.Add(new PointF(p1.X, p1.Y)); zone.polygon.Add(new PointF(p2.X, p1.Y)); zone.polygon.Add(new PointF(p2.X, p2.Y)); zone.polygon.Add(new PointF(p1.X, p2.Y)); k_pcb.Zones.Add(zone); // todo : not needed? //k.Pcb.gr_line k_line; //k_line = new k.Pcb.gr_line(new PointF(p1.X, p1.Y), new PointF(p2.X, p1.Y), layer.Name, width); //k_pcb.Drawings.Add(k_line); //k_line = new k.Pcb.gr_line(new PointF(p2.X, p1.Y), new PointF(p2.X, p2.Y), layer.Name, width); //k_pcb.Drawings.Add(k_line); //k_line = new k.Pcb.gr_line(new PointF(p1.X, p2.Y), new PointF(p2.X, p2.Y), layer.Name, width); //k_pcb.Drawings.Add(k_line); //k_line = new k.Pcb.gr_line(new PointF(p1.X, p1.Y), new PointF(p1.X, p2.Y), layer.Name, width); //k_pcb.Drawings.Add(k_line); } } #endregion #region ==== Plain - Hole ==== foreach (Hole hole in board.Drawing.Board.Plain.Hole) { PointF p1 = StrToPoint_Board(hole.X, hole.Y); float drill = Common.StrToVal_mm(hole.Drill); k_pcb.AddModule(NonplatedHole(drill, drill), p1); } #endregion #region ==== plain.dimension ==== foreach (Dimension dim in board.Drawing.Board.Plain.Dimension) { layer = ConvertLayer(dim.Layer); if (layer != null) { PointF p1 = StrToPoint_Board(dim.X1, dim.Y1); PointF p2 = StrToPoint_Board(dim.X2, dim.Y2); PointF p3 = StrToPoint_Board(dim.X3, dim.Y3); float line_width = 0.15f; // default width? float text_size = Common.StrToVal_mm(dim.TextSize); float text_width = Common.GetTextThickness_mm(dim.TextSize, dim.TextRatio); if (!string.IsNullOrEmpty(dim.Width)) { line_width = Common.StrToVal_mm(dim.Width); } switch (dim.Dtype) { case DimensionType.parallel: case DimensionType.radius: case DimensionType.diameter: k.Pcb.Dimension k_dim = new k.Pcb.Dimension(layer.Name, line_width, p1, p2, text_size, text_width, dim.Unit == GridUnit.mm, int.Parse(dim.Precision), dim.Visible == Bool.yes); k_pcb.Dimensions.Add(k_dim); break; //todo : others? } } } #endregion #region ==== plain.polygon ==== foreach (EagleImport.Polygon poly in board.Drawing.Board.Plain.Polygon) { //todo // if layer is tRestrict or bRestrict, create a keepout zone if ((poly.Layer == "41") || (poly.Layer == "42")) { k.Pcb.Zone zone = new k.Pcb.Zone(); if (poly.Layer == "41") { zone.layer = k.LayerList.StandardLayers.GetLayerName(k.Layer.nFront_Cu); } else if (poly.Layer == "42") { zone.layer = k.LayerList.StandardLayers.GetLayerName(k.Layer.nBack_Cu); } zone.net = 0; zone.net_name = ""; zone.hatch_pitch = 0.508f; zone.connect_pads_clearance = 0; zone.min_thickness = 10.0f; zone.is_filled = false; zone.fill_arc_segments = 16; zone.connect_pads_mode = k.Pcb.ZonePadConnection.yes; //solid zone.fill_thermal_gap = 0.508f; zone.fill_thermal_bridge_width = 0.508f; zone.is_keepout = true; zone.outline_style = k.Pcb.ZoneOutlineStyle.none; zone.keepout_allow_copper_pour = Kicad_utils.Allowed.not_allowed; zone.priority = 7; foreach (Vertex v in poly.Vertex) { zone.polygon.Add(StrToPoint_Board(v.X, v.Y)); } k_pcb.Zones.Add(zone); } } #endregion #region ==== Signals ==== // get net list foreach (Signal signal in board.Drawing.Board.Signals.Signal) { //todo: ? k_pcb.Nets.Add(new k.Pcb.Net(net_index, signal.Name)); net_index++; } List <PinConnection> Contacts = new List <PinConnection>(); foreach (Signal signal in board.Drawing.Board.Signals.Signal) { //todo: ? k.Pcb.Net k_net = k_pcb.Nets.Find(x => x.Name == signal.Name); foreach (Wire wire in signal.Wire) { layer = ConvertLayer(wire.Layer); if (layer != null) { // todo: segment must be on a copper layer? // ignore unrouted if (wire.Layer != "19") { float width = Common.StrToVal_mm(wire.Width); //todo: arcs? k.Pcb.PcbSegment seg = new Kicad_utils.Pcb.PcbSegment(); seg.layer = layer.Name; seg.net = k_net.Number; seg.start = StrToPoint_Board(wire.X1, wire.Y1); seg.end = StrToPoint_Board(wire.X2, wire.Y2); seg.width = width; k_pcb.Segments.Add(seg); Contacts.Add(new PinConnection(signal.Name, seg.start, layer.Name, null, null)); Contacts.Add(new PinConnection(signal.Name, seg.end, layer.Name, null, null)); } } } // contactref foreach (Contactref con_ref in signal.Contactref) { Contacts.Add(new PinConnection(signal.Name, PointF.Empty, null, con_ref.Element, con_ref.Pad)); } //<via x="6.6675" y="49.2125" extent="1-16" drill="0.3" shape="octagon"/> foreach (Via via in signal.Via) { float drill = Common.StrToVal_mm(via.Drill); PointF pos = StrToPoint_Board(via.X, via.Y); float size = Common.StrToVal_mm(via.Diameter); if (size == 0) { size = designRules.CalcViaSize(drill); } k.Pcb.Via k_via = new k.Pcb.Via(pos, size, drill, k.LayerList.StandardLayers.GetLayerName(k.Layer.nFront_Cu), k.LayerList.StandardLayers.GetLayerName(k.Layer.nBack_Cu), k_net.Number); PinConnection p_conn = Contacts.Find(x => x.position.X == k_via.at.X && x.position.Y == k_via.at.Y); if (via.Extent == "1-16") { k_via.topmost_layer = k.LayerList.StandardLayers.GetLayerName(k.Layer.nFront_Cu); k_via.backmost_layer = k.LayerList.StandardLayers.GetLayerName(k.Layer.nBack_Cu); } else { Trace(string.Format("error : blind/buried via ? {0},{1} {2} {3}", via.X, via.Y, signal.Name, via.Extent)); } if (p_conn == null) { Trace(string.Format("note : loose via converted to pad at {0},{1} net={2}", via.X, via.Y, signal.Name)); k.ModuleDef.Module k_pad = ViaPad(size, drill, k_net); k_pcb.AddModule(k_pad, pos); } else { k_pcb.Vias.Add(k_via); } } foreach (EagleImport.Polygon poly in signal.Polygon) { //<polygon width="0.2032" layer="1" spacing="0.254" isolate="0.254" rank="2"> //defaults are 6 mil float width = 0.1524f; float isolate = 0.1524f; float spacing = 0.1524f; int rank = int.Parse(poly.Rank); layer = ConvertLayer(poly.Layer); if (layer != null) { //todo: clearances etc should come from DesignRules? if (!string.IsNullOrEmpty(poly.Width)) { width = Common.StrToVal_mm(poly.Width); } if (!string.IsNullOrEmpty(poly.Isolate)) { isolate = Common.StrToVal_mm(poly.Isolate); } if (!string.IsNullOrEmpty(poly.Spacing)) { spacing = Common.StrToVal_mm(poly.Spacing); } if (k.Layer.IsCopperLayer(layer.Number) || (poly.Layer == "41") || (poly.Layer == "42")) { k.Pcb.Zone zone = new k.Pcb.Zone(); if (poly.Layer == "41") { zone.layer = k.LayerList.StandardLayers.GetLayerName(k.Layer.nFront_Cu); } else if (poly.Layer == "42") { zone.layer = k.LayerList.StandardLayers.GetLayerName(k.Layer.nBack_Cu); } else { zone.layer = layer.Name; } zone.net = k_net.Number; zone.net_name = k_net.Name; zone.outline_style = k.Pcb.ZoneOutlineStyle.edge; zone.hatch_pitch = 0.508f; zone.connect_pads_clearance = 0.2032f; zone.min_thickness = width; // ?? zone.fill_arc_segments = 32; zone.fill_thermal_gap = 0.2032f; zone.fill_thermal_bridge_width = 0.2032f; zone.is_filled = false; foreach (Vertex v in poly.Vertex) { zone.polygon.Add(StrToPoint_Board(v.X, v.Y)); } if ((poly.Pour == PolygonPour.cutout) || !k.Layer.IsCopperLayer(layer.Number)) { zone.is_keepout = true; zone.outline_style = k.Pcb.ZoneOutlineStyle.none; zone.keepout_allow_copper_pour = Kicad_utils.Allowed.not_allowed; } if (!string.IsNullOrEmpty(poly.Isolate)) { zone.connect_pads_clearance = isolate; } if (poly.Thermals == Bool.yes) { zone.connect_pads_mode = k.Pcb.ZonePadConnection.thermal_relief; zone.fill_thermal_gap = width + 0.001f; // ** zone.fill_thermal_bridge_width = width + 0.001f; // ** } else { zone.connect_pads_mode = k.Pcb.ZonePadConnection.yes; } // priority on KiCad is opposite to rank zone.priority = 6 - rank; k_pcb.Zones.Add(zone); } } } // } #endregion #region ==== Elements ==== foreach (Element element in board.Drawing.Board.Elements.Element) { //todo: k.ModuleDef.Module k_mod; // find package library : package string footprint_sid = element.Library + ":" + libraryConverter.FootprintNameMap.GetNewName(element.Package); k.ModuleDef.Module k_template = libraryConverter.AllFootprints.Find(x => x.Name == footprint_sid); if (k_template == null) { Trace(string.Format("error: {0} not found", footprint_sid)); } else { k_mod = k_template.Clone(true); k_mod.Name = footprint_sid; k_mod.Reference.Value = PartMap.GetNewName(element.Name); k_mod.At = StrToPoint_Board(element.X, element.Y); if (k_mod.Value != null) { k_mod.Value.Value = element.Value; } k_mod.layer = k.LayerList.StandardLayers.GetLayerName(k.Layer.nFront_Cu); // Set position, orientation ExtRotation elementRot = ExtRotation.Parse(element.Rot); int element_angle = (int)elementRot.Rotation; // get attributes for text foreach (EagleImport.Attribute attrib in element.Attribute) { ExtRotation attrRot = ExtRotation.Parse(attrib.Rot); bool attr_mirror = attrRot.Mirror; int attr_angle = (int)attrRot.Rotation; layer = ConvertLayer(attrib.Layer); if (layer != null) { //k.Symbol.SymbolField sym_field = null; k.ModuleDef.fp_text field = null; switch (attrib.Name) { case "NAME": //sym_field = k_symbol.fReference; field = k_mod.Reference; break; case "VALUE": //sym_field = k_symbol.fValue; field = k_mod.Value; break; // Part? // voltage, current } if (field != null) { field.effects.font.Size = new SizeF(Common.StrToVal_mm(attrib.Size), Common.StrToVal_mm(attrib.Size)); field.layer = layer.Name; field.layer = k.Layer.MakeLayerName(k_mod.layer, field.layer); //field.effects.horiz_align = k.TextJustify.left; //field.effects.vert_align = k.VerticalAlign.bottom; SetPcbTextAttributes(field, StrToPoint_Board(element.X, element.Y), elementRot, StrToPoint_Board(attrib.X, attrib.Y), attrRot); // AdjustPos(field); //debug if (pcb_debug) { PointF ptext = new PointF(field.position.At.X, field.position.At.Y); SizeF textSize = strokeFont.GetTextSize(field.Value, field.effects); if (elementRot.Mirror) { // get bottom right ptext.X += textSize.Width / 2; ptext.Y += textSize.Height / 2; ptext = ptext.Rotate(-elementRot.Rotation - 180); ptext.Y = -ptext.Y; } else { // get bottom left ptext.X -= textSize.Width / 2; ptext.Y += textSize.Height / 2; ptext = ptext.Rotate(-elementRot.Rotation); } ptext = k_mod.position.At.Add(ptext); //!DrawRect(k_pcb, ptext, textSize, -(elementRot.Rotation + field.position.Rotation)); // PointF p1 = new PointF(field.position.At.X, field.position.At.Y); k.Pcb.gr_line k_line; float ds = 1.27f; if (elementRot.Mirror) { p1 = p1.Rotate(-elementRot.Rotation - 180); p1.Y = -p1.Y; } else { p1 = p1.Rotate(-elementRot.Rotation); } //p1 = p1.Rotate(field.position.Rotation); //p1 = p1.Rotate(k_mod.position.Rotation); k_line = new k.Pcb.gr_line( new PointF(k_mod.position.At.X + p1.X - ds, k_mod.position.At.Y + p1.Y), new PointF(k_mod.position.At.X + p1.X + ds, k_mod.position.At.Y + p1.Y), "Dwgs.User", 0.01f); k_pcb.Drawings.Add(k_line); k_line = new k.Pcb.gr_line( new PointF(k_mod.position.At.X + p1.X, k_mod.position.At.Y + p1.Y - ds), new PointF(k_mod.position.At.X + p1.X, k_mod.position.At.Y + p1.Y + ds), "Dwgs.User", 0.01f); k_pcb.Drawings.Add(k_line); } } } } // Note: the Eagle "mirror" attribute reverses side and flips about Y | axis, but // Kicad "flip" reverses side and flips about X -- axis. // therefore Eagle mirror is equivalent to Kicad flip + rotate(180) if (elementRot.Mirror) { k_mod.RotateBy(MathUtil.NormalizeAngle(-(element_angle + 180))); k_mod.FlipX(k_mod.position.At); } else //if (element_angle != 0) { k_mod.RotateBy(element_angle); } // fix up pads foreach (k.ModuleDef.pad pad in k_mod.Pads) { string new_name = PartMap.GetNewName(element.Name); if (pad.type != k.ModuleDef.pad.nonplated_hole) { PinConnection contact = Contacts.Find(x => x.PartName == element.Name && x.PinName == pad.number); if (contact == null) { // may actually be a non-connect // Trace(string.Format("warning: contact {0} {1} not found", element.Name, pad.number)); } else { pad.net = k_pcb.Nets.Find(x => x.Name == contact.NetLabel); } } } // k_pcb.Modules.Add(k_mod); } } #endregion // transfer some design rules k_pcb.Setup.trace_min = designRules.GetValueFloat("msWidth"); k_pcb.Setup.via_min_size = designRules.GetValueFloat("msWidth"); k_pcb.Setup.via_min_drill = designRules.GetValueFloat("msDrill"); k_pcb.Setup.uvia_min_size = designRules.GetValueFloat("msMicroVia"); k_pcb.Setup.uvia_min_drill = designRules.GetValueFloat("msMicroVia"); // not right, but need layer thickness to calculate correctly // allow uvia // allow blind/buried via // grid // text and drawings // pad // pad mask clearance //default netclass k_pcb.NetClasses[0].clearance = designRules.GetValueFloat("mdPadVia"); k_pcb.NetClasses[0].trace_width = designRules.GetValueFloat("msWidth"); k_pcb.NetClasses[0].via_dia = designRules.GetValueFloat("msWidth"); k_pcb.NetClasses[0].via_drill = designRules.GetValueFloat("msDrill"); k_pcb.NetClasses[0].uvia_dia = designRules.GetValueFloat("msMicroVia"); k_pcb.NetClasses[0].uvia_drill = designRules.GetValueFloat("msMicroVia"); // not right // write the KiCad file string filename = Path.Combine(OutputFolder, ProjectName + ".kicad_pcb"); Trace(string.Format("Writing board {0}", filename)); k_pcb.SaveToFile(filename); result = true; } else { result = false; Trace(string.Format("error opening {0}", SourceFilename)); } return(result); }
public bool ConvertLibrary(string LibName, Library lib, List <Layer> layers, string OutputFolder, bool WriteLibFile) { string lib_filename; k.LayerDescriptor layer; Trace("Processing Library: " + LibName); Layers = layers; // Packages k.ModuleDef.LibModule k_footprint_lib = new Kicad_utils.ModuleDef.LibModule(); k_footprint_lib.Name = LibName; foreach (Package package in lib.Packages.Package) { k.ModuleDef.Module k_module = new Kicad_utils.ModuleDef.Module(); k_module.Name = Common.CleanFootprintName(package.Name); FootprintNameMap.Add(package.Name, k_module.Name); if (package.Name != k_module.Name) { Trace(String.Format("note: {0} is renamed to {1}", package.Name, k_module.Name)); } if (package.Description != null) { k_module.description = Common.CleanTags(package.Description.Text); } k_module.position = new k.Position(0, 0, 0); k_module.layer = "F.Cu"; // todo: back ??? foreach (Wire wire in package.Wire) { layer = ConvertLayer(wire.Layer, package.Name); if (layer != null) { // float curve = (float)StringUtils.StringToDouble(wire.Curve); if (curve == 0) { k.ModuleDef.fp_line k_line = new Kicad_utils.ModuleDef.fp_line( Common.StrToPointFlip_mm(wire.X1, wire.Y1), Common.StrToPointFlip_mm(wire.X2, wire.Y2), layer.Name, Common.StrToVal_mm(wire.Width)); k_module.Borders.Add(k_line); } else { PointF start = Common.StrToPointFlip_mm(wire.X1, wire.Y1); PointF end = Common.StrToPointFlip_mm(wire.X2, wire.Y2); float arc_start, arc_end, radius; PointF center = Common.kicad_arc_center(start, end, curve, out radius, out arc_start, out arc_end); k.ModuleDef.fp_arc k_arc = new k.ModuleDef.fp_arc( center, start, -curve, layer.Name, Common.StrToVal_mm(wire.Width)); k_module.Borders.Add(k_arc); } } } foreach (Smd smd in package.Smd) { float roundness = Common.StrToVal(smd.Roundness, 0); string shape = "rect"; if (roundness == 100) { shape = "oval"; } k.ModuleDef.pad k_pad = new k.ModuleDef.pad(smd.Name, "smd", shape, Common.StrToPointFlip_mm(smd.X, smd.Y), Common.StrToSize_mm(smd.Dx, smd.Dy), 0); if (Common.GetAngle(smd.Rot) % 180 == 90) { k_pad.size = Common.StrToSize_mm(smd.Dy, smd.Dx); } if (smd.Stop == Bool.no) { k_pad._layers.RemoveLayer("F.Mask"); } if (smd.Cream == Bool.no) { k_pad._layers.RemoveLayer("F.Paste"); } k_module.Pads.Add(k_pad); } foreach (Pad pad in package.Pad) { float pad_size = Common.StrToVal_mm(pad.Diameter); if (pad_size == 0) { pad_size = designRules.CalcPadSize(Common.StrToVal_mm(pad.Drill)); } k.ModuleDef.pad k_pad = new k.ModuleDef.pad(pad.Name, "thru_hole", "circle", Common.StrToPointFlip_mm(pad.X, pad.Y), new SizeF(pad_size, pad_size), Common.StrToVal_mm(pad.Drill)); if (pad.Stop == Bool.no) { k_pad._layers.RemoveLayer("F.Mask"); k_pad._layers.RemoveLayer("B.Mask"); } if (pad.Thermals == Bool.no) { k_pad.thermal_gap = 0; } if (pad.Shape == PadShape.@long) { k_pad.shape = "oval"; if (Common.GetAngle(pad.Rot) % 180 == 0) { k_pad.size = new SizeF(pad_size * 2, pad_size); } else { k_pad.size = new SizeF(pad_size, pad_size * 2); } } k_module.Pads.Add(k_pad); } foreach (Text text in package.Text) { PointF pos = Common.StrToPointFlip_mm(text.X, text.Y); layer = ConvertLayer(text.Layer, package.Name); if (layer != null) { k.ModuleDef.fp_text k_text = new k.ModuleDef.fp_text("ref", text.mText, pos, layer.Name, new SizeF(Common.StrToVal_mm(text.Size), Common.StrToVal_mm(text.Size)), Common.GetTextThickness_mm(text), true); // TODO: adjust position for center, center alignment ExtRotation rot = ExtRotation.Parse(text.Rot); SizeF textSize = strokeFont.GetTextSize(text.mText, k_text.effects); k_text.position.At = Common.GetTextPos(pos, textSize, rot, text.Align, Align.bottom_left); k_text.position.Rotation = rot.Rotation; k_text.effects.vert_align = k.VerticalAlign.bottom; k_text.effects.horiz_align = k.TextJustify.left; if (rot.Mirror) { k_text.effects.mirror = true; } if (text.mText.StartsWith(">")) { string t = text.mText.ToUpperInvariant(); if (t.Contains("NAME") || t.Contains("PART")) { k_text.Type = "reference"; k_module.Reference = k_text; } else if (t.Contains("VALUE")) { k_text.Type = "value"; k_module.Value = k_text; } // user field ? } else { k_text.Type = "user"; k_module.UserText.Add(k_text); } } } foreach (EagleImport.Rectangle rect in package.Rectangle) { layer = ConvertLayer(rect.Layer, package.Name); if (layer != null) { RectangleF r = Common.ConvertRect_mm(rect.X1, rect.Y1, rect.X2, rect.Y2, rect.Rot); List <PointF> poly = Common.RectToPoly(r); k.ModuleDef.fp_polygon k_poly = new Kicad_utils.ModuleDef.fp_polygon( poly, layer.Name, 0 // width? ); k_module.Borders.Add(k_poly); } } foreach (Circle circle in package.Circle) { layer = ConvertLayer(circle.Layer, package.Name); if (layer != null) { float width = 0; if (!string.IsNullOrEmpty(circle.Width)) { width = Common.StrToVal_mm(circle.Width); } // if width == 0 convert to poly if (width == 0) { PointF center = Common.StrToPointFlip_mm(circle.X, circle.Y); float radius = Common.StrToVal_mm(circle.Radius); List <PointF> pts = new List <PointF>(); int n_segments = 360 / 15; int j = 0; float step = 15; while (j < n_segments) { float angle = MathUtil.DegToRad(j * step); PointF p = new PointF((float)(center.X + Math.Cos(angle) * radius), (float)(center.Y + Math.Sin(angle) * radius)); pts.Add(p); j++; } k.ModuleDef.fp_polygon k_poly = new Kicad_utils.ModuleDef.fp_polygon(pts, layer.Name, width); k_module.Borders.Add(k_poly); } else { k.ModuleDef.fp_circle k_circle = new Kicad_utils.ModuleDef.fp_circle( Common.StrToPointFlip_mm(circle.X, circle.Y), Common.StrToVal_mm(circle.Radius), layer.Name, Common.StrToVal_mm(circle.Width) ); k_module.Borders.Add(k_circle); } } } foreach (Hole hole in package.Hole) { k.ModuleDef.pad k_hole = new Kicad_utils.ModuleDef.pad("", "np_thru_hole", "circle", Common.StrToPointFlip_mm(hole.X, hole.Y), new SizeF(Common.StrToVal_mm(hole.Drill), Common.StrToVal_mm(hole.Drill)), Common.StrToVal_mm(hole.Drill) ); k_module.Pads.Add(k_hole); } foreach (EagleImport.Polygon poly in package.Polygon) { layer = ConvertLayer(poly.Layer, package.Name); if (layer != null) { float width = 0; if (!string.IsNullOrEmpty(poly.Width)) { width = Common.StrToVal_mm(poly.Width); } int index = 0; if (poly.Vertex.Count > 0) { List <PointF> pts = new List <PointF>(); PointF p1 = Common.StrToPoint_mm(poly.Vertex[index].X, poly.Vertex[index].Y); float curve1 = (float)StringUtils.StringToDouble(poly.Vertex[index].Curve); index++; pts.Add(p1.FlipX()); while (index <= poly.Vertex.Count) { PointF p2 = Common.StrToPoint_mm(poly.Vertex[index % poly.Vertex.Count].X, poly.Vertex[index % poly.Vertex.Count].Y); if (curve1 == 0) { if (index < poly.Vertex.Count) { pts.Add(p2.FlipX()); } } else { float arc_start, arc_end, radius; PointF center = Common.kicad_arc_center2(p1, p2, curve1, out radius, out arc_start, out arc_end); if (arc_end < arc_start) { arc_end += 360; } int n_segments = (int)((Math.Abs(arc_end - arc_start) + 7.5f) / 15f); int j = 1; float step = (arc_end - arc_start) / n_segments; while (j <= n_segments) { float angle = MathUtil.DegToRad(arc_start + j * step); PointF p = new PointF((float)(center.X + Math.Cos(angle) * radius), (float)(center.Y + Math.Sin(angle) * radius)); pts.Add(p.FlipX()); j++; } } p1 = p2; if (index < poly.Vertex.Count) { curve1 = (float)StringUtils.StringToDouble(poly.Vertex[index].Curve); } index++; } k.ModuleDef.fp_polygon k_poly = new Kicad_utils.ModuleDef.fp_polygon(pts, layer.Name, width); k_module.Borders.Add(k_poly); } } } // k_footprint_lib.Modules.Add(k_module); // k.ModuleDef.Module k_generic = k_module.Clone(); k_generic.Name = LibName + ":" + k_generic.Name; AllFootprints.Add(k_generic); } if (WriteLibFile & (k_footprint_lib.Modules.Count > 0)) { lib_filename = Path.Combine(OutputFolder); k_footprint_lib.WriteLibrary(lib_filename); //! footprintTable.Entries.Add(new Kicad_utils.Project.LibEntry(LibName, "KiCad", @"$(KIPRJMOD)\\" + k_footprint_lib.Name + ".pretty", "", "")); } if (lib.Devicesets != null) { // Symbols k.Symbol.LibSymbolLegacy kicad_lib = new k.Symbol.LibSymbolLegacy(); kicad_lib.Name = LibName; kicad_lib.Symbols = new List <k.Symbol.Symbol>(); foreach (Deviceset devset in lib.Devicesets.Deviceset) { string prefix; if (string.IsNullOrEmpty(devset.Prefix)) { prefix = "U"; } else { prefix = devset.Prefix; } Trace(string.Format("debug: {0}", devset.Name)); k.Symbol.Symbol k_sym = new k.Symbol.Symbol(devset.Name, true, prefix, 20, true, true, 1, false, false); if (devset.Description != null) { k_sym.Description = Common.CleanTags(devset.Description.Text); } // prefix placeholder for reference = >NAME or >PART if multi-part? // symbol name is placeholder for value = >VALUE k_sym.fReference = new k.Symbol.SymbolField(prefix, new PointF(-50, 0), 50, true, "H", "L", "B", false, false); k_sym.fValue = new k.Symbol.SymbolField(k_sym.Name, new PointF(50, 0), 50, true, "H", "L", "B", false, false); k_sym.Drawings = new List <k.Symbol.sym_drawing_base>(); k_sym.UserFields = new List <k.Symbol.SymbolField>(); // GetSymbol(lib, devset, k_sym); AllSymbols.Add(k_sym); // if ((devset.Devices.Device.Count == 1) && (devset.Devices.Device[0].Package == null)) { // symbol only Trace(string.Format("debug: symbol only {0}", devset.Name)); kicad_lib.Symbols.Add(k_sym); } else { foreach (Device device in devset.Devices.Device) { // foreach technology string name; if (device.Name == "") { name = devset.Name; } else { name = devset.Name + device.Name; } k.Symbol.Symbol k_sym_device = k_sym.Clone(); k_sym_device.Name = name; k_sym_device.fValue.Text.Value = name; // place below value PointF pos; if (k_sym_device.fValue.Text.Pos.Rotation == 0) { pos = new PointF(k_sym_device.fValue.Text.Pos.At.X, k_sym_device.fValue.Text.Pos.At.Y - 100); } else { pos = new PointF(k_sym_device.fValue.Text.Pos.At.X + 100, k_sym_device.fValue.Text.Pos.At.Y); } k_sym_device.fPcbFootprint = new k.Symbol.SymbolField(kicad_lib.Name + ":" + device.Package, pos, 50, true, k_sym_device.fValue.Text.Pos.Rotation == 0 ? "H" : "V", "L", "B", false, false); Trace(string.Format("debug: device {0} {1}", name, k_sym_device.fPcbFootprint.Text.Value)); // pin mapping if (device.Connects != null) { foreach (Connect connect in device.Connects.Connect) { int unit; if (k_sym_device.NumUnits == 1) { unit = 0; } else { unit = 1; foreach (Gate gate in devset.Gates.Gate) { if (gate.Name == connect.Gate) { break; } else { unit++; } } } k.Symbol.sym_pin k_pin = k_sym_device.FindPin(unit, Common.ConvertName(connect.Pin)); if (k_pin == null) { Trace(string.Format("error: pin not found {0} {1}", k_sym_device.Name, connect.Pin)); } else { string[] pads; pads = connect.Pad.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); int index = 0; foreach (string s in pads) { // check length if (s.Length > 4) { Trace(string.Format("error: pad name too long {0} {1}", k_sym_device.Name, connect.Pad)); } if (index == 0) { k_pin.PinNumber = s; } else { k.Symbol.sym_pin k_dup_pin = k.Symbol.sym_pin.Clone(k_pin); k_dup_pin.Visible = false; k_dup_pin.PinNumber = s; k_sym_device.Drawings.Add(k_dup_pin); } index++; } } } } // k_sym_device bool first = true; foreach (Technology tech in device.Technologies.Technology) { if (tech.Name == "") { if (device.Name == "") { name = devset.Name.Replace("*", ""); } else { name = devset.Name.Replace("*", "") + device.Name; } k_sym_device.Name = name; k_sym_device.fValue.Text.Value = name; kicad_lib.Symbols.Add(k_sym_device); AllDevices.Add(new Device(name, device.Package)); } else { if (first) { if (device.Name == "") { name = devset.Name.Replace("*", tech.Name); } else { name = devset.Name.Replace("*", tech.Name) + device.Name; } k_sym_device.Name = name; k_sym_device.fValue.Text.Value = name; kicad_lib.Symbols.Add(k_sym_device); AllDevices.Add(new Device(name, device.Package)); } else { // create alias k_sym_device.Alias.Add(devset.Name.Replace("*", tech.Name) + device.Name); // ? } } first = false; } } } } LibNames.Add(kicad_lib.Name); // if (WriteLibFile) { lib_filename = Path.Combine(OutputFolder, kicad_lib.Name + ".lib"); kicad_lib.WriteToFile(lib_filename); } } return(true); }
public bool ConvertSchematic(string SourceFilename, string DestFolder, string ProjectName, bool ExtractLibraries) { bool result = false; PartMap = new RenameMap(); designRules = new DesignRules(); AllLabels = new List <PinConnection>(); AllComponents = new List <ComponentBase>(); // footprintTable = new k.Project.FootprintTable(); // Trace(string.Format("Reading schematic file {0}", SourceFilename)); schematic = EagleSchematic.LoadFromXmlFile(SourceFilename); OutputFolder = DestFolder; if (schematic != null) { DrawingOffset = new PointF(10.16f, 12.7f); libraryConverter = new LibraryConverter(Parent); // if (Extract... ? ConvertComponentLibraries(ExtractLibraries); Parent.SetLibNames(libraryConverter.LibNames); // foreach (Part part in schematic.Drawing.Schematic.Parts.Part) { k.Symbol.Symbol k_symbol = FindSymbol(part.Deviceset); if ((k_symbol != null) && !k_symbol.PowerSymbol) { PartMap.Add(part.Name); } } PartMap.Annotate(); // k.Schema.SchematicLegacy k_schematic = new SchematicLegacy(); if (schematic.Drawing.Schematic.Sheets.Sheet.Count == 1) { // single sheet is also top level sheet ConvertSheet(k_schematic, 0, ProjectName, true, 1, 1); } else { // create top level CreateMainSheet(k_schematic, schematic.Drawing.Schematic.Sheets.Sheet.Count + 1, ProjectName); for (int sheet_number = 0; sheet_number < schematic.Drawing.Schematic.Sheets.Sheet.Count; sheet_number++) { ConvertSheet(k_schematic, sheet_number, "sheet" + (sheet_number + 1).ToString(), false, sheet_number + 2, schematic.Drawing.Schematic.Sheets.Sheet.Count + 1); } } // Global schematic fixups List <string> labels = new List <string>(); foreach (PinConnection conn in AllLabels) { if (labels.IndexOf(conn.Label.Value) == -1) { labels.Add(conn.Label.Value); } } foreach (string name in labels) { List <PinConnection> instances = AllLabels.FindAll(x => x.Label.Value == name); bool is_global = false; foreach (PinConnection item in instances) { if (item.Sheet != instances[0].Sheet) { is_global = true; break; } } if (is_global) { Trace(String.Format("note: converting {0} to global label", name)); foreach (PinConnection item in instances) { item.Label.Type = "GLabel"; item.Label.Shape = "3State"; item.Label.TextSize = (int)(item.Label.TextSize * 0.75f); if ((item.Label.Orientation % 2) == 0) { item.Label.Orientation = 2 - item.Label.Orientation; } } } } // string filename = Path.Combine(OutputFolder, ProjectName + ".sch"); Trace(string.Format("Writing schematic {0}", filename)); k_schematic.SaveToFile(filename); result = true; } else { result = false; Trace(string.Format("error opening {0}", SourceFilename)); } // return(result); }