Пример #1
0
        public static RectangleF ConvertRect_mm(string x1, string y1, string x2, string y2, string rot)
        {
            PointF      p1       = StrToPoint_mm(x1, y1);
            PointF      p2       = StrToPoint_mm(x2, y2);
            ExtRotation rotation = ExtRotation.Parse(rot);

            PointF mid = new PointF((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2);

            p1 = p1.RotateAt(mid, rotation.Rotation);
            p2 = p2.RotateAt(mid, rotation.Rotation);

            p1 = p1.FlipX();
            p2 = p2.FlipX();

            RectangleF rect = new RectangleF(
                Math.Min(p1.X, p2.X), Math.Min(p1.Y, p2.Y),
                Math.Abs(p2.X - p1.X), Math.Abs(p2.Y - p1.Y));

            return(rect);
        }
Пример #2
0
        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);
        }