コード例 #1
0
ファイル: CadItem.cs プロジェクト: HoangHaUI/Stancil
        public CadItem Copy(string ID)
        {
            CadItem cadItem = new CadItem();

            cadItem.CadFileID = ID;
            cadItem.Name      = this.Name;
            cadItem.Angle     = this.Angle;
            cadItem.Center    = new PointF(this.Center.X, this.Center.Y);
            cadItem.Code      = Code;
            cadItem.PadsIndex = new List <int>();
            return(cadItem);
        }
コード例 #2
0
        }                                          //
        public static CadFile GetNewCadFile(string ID, string Path, double DPI, int GerberWidth = 0, int GerberHeight = 0)
        {
            FileInfo fi  = new FileInfo(Path);
            CadFile  cad = new CadFile();

            cad.ModelID   = ID;
            cad.CadFileID = Utils.GetNewID();
            cad.Color     = Color.FromArgb(0, 255, 0);
            cad.FileName  = fi.Name;
            cad.FilePath  = fi.FullName;
            cad.Visible   = true;
            cad.Angle     = 0;
            cad.CadItems  = new List <CadItem>();
            string[] content = File.ReadAllLines(fi.FullName);
            double   _XMin   = 999999;
            double   _YMin   = 999999;
            double   _XMax   = -999999;
            double   _YMax   = -999999;

            foreach (string item in content)
            {
                string        line   = item.Replace('\t', ' ');
                string[]      arrall = line.Split(' ');
                List <string> arr    = new List <string>();
                foreach (var str in arrall)
                {
                    if (str != "")
                    {
                        arr.Add(str);
                    }
                }
                CadItem cadItem = new CadItem();
                try
                {
                    cadItem.CadFileID = cad.CadFileID;
                    cadItem.Name      = arr[0];
                    cadItem.Center    = new PointF((float)(-Convert.ToDouble(arr[1]) * DPI / 25.4), (float)(Convert.ToDouble(arr[2]) * DPI / 25.4));
                    cadItem.Angle     = Convert.ToDouble(arr[3]);
                    cadItem.Code      = arr[4];
                    cadItem.PadsIndex = new List <int>();
                    cad.CadItems.Add(cadItem);
                    if (cadItem.Center.X < _XMin)
                    {
                        _XMin = cadItem.Center.X;
                    }
                    if (cadItem.Center.Y < _YMin)
                    {
                        _YMin = cadItem.Center.Y;
                    }
                    if (cadItem.Center.X > _XMax)
                    {
                        _XMax = cadItem.Center.X;
                    }
                    if (cadItem.Center.Y > _YMax)
                    {
                        _YMax = cadItem.Center.Y;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(item);
                    mLog.Error(ex.Message);
                    return(null);
                }
            }
            // add undefine caditem
            CadItem cadItemUndefine = new CadItem();

            cadItemUndefine.CadFileID = cad.CadFileID;
            cadItemUndefine.Name      = "UNDEFINE";
            cadItemUndefine.Center    = new PointF(-9999, -9999);
            cadItemUndefine.Angle     = 0;
            cadItemUndefine.Code      = "UNDEFINE";
            cadItemUndefine.PadsIndex = new List <int>();
            cad.CadItems.Add(cadItemUndefine);
            // calculate rotate point
            for (int i = 0; i < cad.CadItems.Count; i++)
            {
                PointF ct = cad.CadItems[i].Center;
                cad.CadItems[i].Center = new PointF((float)(ct.X - _XMin), ct.Y);
            }
            _YMax -= _XMin;
            _XMax -= _XMin;
            _YMin -= _XMin;
            _XMin  = 0;
            cad.CenterRotation = new Point((int)(_XMax - _XMin) / 2, (int)(_YMax - _YMin) / 2);
            cad.X           = GerberWidth / 2 - cad.CenterRotation.X;
            cad.Y           = GerberHeight / 2 - cad.CenterRotation.Y;
            cad.CadFileData = content;
            return(cad);
        }
コード例 #3
0
        public void AutoLinkPad(CadFile Cad, int Mode, int Width = 800, int Height = 800)
        {
            List <Tuple <Point, int> > padsList = new List <Tuple <Point, int> >();

            for (int i = 0; i < this.Gerber.PadItems.Count; i++)
            {
                padsList.Add(new Tuple <Point, int>(this.Gerber.PadItems[i].Center, i));
            }
            Point cadCenterRotate = Cad.CenterRotation;
            List <Tuple <CadItem, int> > cadItemNotLink = new List <Tuple <CadItem, int> >();
            int    cadX     = Cad.X;
            int    cadY     = Cad.Y;
            double cadAngle = Cad.Angle;

            // filter Resistor and Capacitor component
            for (int t = 0; t < Cad.CadItems.Count; t++)
            {
                var    item             = Cad.CadItems[t];
                string name             = Convert.ToString(item.Name[0]);
                string nextName         = Convert.ToString(item.Name[1]);
                Point  cadCenter        = Point.Round(item.Center);
                Point  cadCenterRotated = CadItem.GetCenterRotated(cadCenter, cadCenterRotate, cadX, cadY, cadAngle);
                var    sorted           = padsList.OrderBy(i => ImageProcessingUtils.DistanceTwoPoint(i.Item1, cadCenterRotated));
                if ((name.ToUpper() == "R" || name.ToUpper() == "C") && "0123456789".Contains(nextName))
                {
                    item.PadsIndex.Add(sorted.ElementAt(0).Item2);
                    item.PadsIndex.Add(sorted.ElementAt(1).Item2);
                    this.Gerber.PadItems[sorted.ElementAt(0).Item2].CadFileID    = Cad.CadFileID;
                    this.Gerber.PadItems[sorted.ElementAt(0).Item2].CadItemIndex = t;
                    this.Gerber.PadItems[sorted.ElementAt(1).Item2].CadFileID    = Cad.CadFileID;
                    this.Gerber.PadItems[sorted.ElementAt(1).Item2].CadItemIndex = t;
                }
                else
                {
                    if (name.ToUpper() != "S")
                    {
                        cadItemNotLink.Add(new Tuple <CadItem, int>(item, t));
                    }
                }
            }
            // reset pad list
            padsList.Clear();
            for (int i = 0; i < this.Gerber.PadItems.Count; i++)
            {
                if (string.IsNullOrEmpty(this.Gerber.PadItems[i].CadFileID))
                {
                    padsList.Add(new Tuple <Point, int>(this.Gerber.PadItems[i].Center, i));
                }
            }
            if (Mode < 1)
            {
                return;
            }
            // filter only has two pad
            foreach (var itemtl in cadItemNotLink)
            {
                var    item                   = itemtl.Item1;
                string name                   = Convert.ToString(item.Name[0]);
                string nextName               = Convert.ToString(item.Name[1]);
                Point  cadCenter              = Point.Round(item.Center);
                Point  cadCenterRotated       = CadItem.GetCenterRotated(cadCenter, cadCenterRotate, cadX, cadY, cadAngle);
                var    sorted                 = padsList.OrderBy(i => ImageProcessingUtils.DistanceTwoPoint(i.Item1, cadCenterRotated));
                Tuple <Point, int>[] arSorted = sorted.ToArray();
                List <int>           idGot    = new List <int>();
                int    limit                  = arSorted.Length > 10 ? 10 : arSorted.Length;
                double crDist                 = -1;
                for (int i = 0; i < limit - 1; i++)
                {
                    if (idGot.Contains(arSorted[i].Item2))
                    {
                        continue;
                    }
                    if (i == 1 && idGot.Count == 0)
                    {
                        break;
                    }
                    Point  p1 = arSorted[i].Item1;
                    double d1 = ImageProcessingUtils.DistanceTwoPoint(p1, cadCenterRotated);
                    if (idGot.Count > 0 && 2 * crDist < d1)
                    {
                        break;
                    }
                    if (d1 > this.DPI / 2)
                    {
                        break;
                    }
                    for (int j = i + 1; j < limit; j++)
                    {
                        Point  p2            = arSorted[j].Item1;
                        double d2            = ImageProcessingUtils.DistanceTwoPoint(p2, cadCenterRotated);
                        double deviationDist = Math.Max(d1, d2) > 0.05 * this.DPI ? 0.1 * Math.Min(d1, d2) : 0.03 * this.DPI;
                        if (Math.Abs(d1 - d2) > deviationDist || d2 > this.DPI / 2)
                        {
                            break;
                        }
                        Point ctP12 = new Point((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2);
                        if (Math.Abs(ctP12.X - cadCenterRotated.X) < 0.01 * this.DPI || Math.Abs(ctP12.Y - cadCenterRotated.Y) < 0.01 * this.DPI)
                        {
                            if (crDist != -1 && (Math.Abs(crDist - d1) > 0.5 * crDist || Math.Abs(crDist - d2) > 0.5 * crDist))
                            {
                                break;
                            }
                            crDist = (d1 + d2) / 2;
                            crDist = Math.Min(d1, d2);
                            idGot.Add(arSorted[i].Item2);
                            idGot.Add(arSorted[j].Item2);
                        }
                    }
                }
                if (idGot.Count == 2)
                {
                    for (int i = 0; i < idGot.Count; i++)
                    {
                        this.Gerber.PadItems[idGot[i]].CadFileID    = Cad.CadFileID;
                        this.Gerber.PadItems[idGot[i]].CadItemIndex = itemtl.Item2;
                        item.PadsIndex.Add(idGot[i]);
                    }
                }
            }
            // reset pads and cad list
            padsList.Clear();
            cadItemNotLink.Clear();
            if (Mode < 2)
            {
                return;
            }
            //get all pad
            for (int i = 0; i < this.Gerber.PadItems.Count; i++)
            {
                padsList.Add(new Tuple <Point, int>(this.Gerber.PadItems[i].Center, i));
            }
            for (int i = 0; i < Cad.CadItems.Count; i++)
            {
                if (Cad.CadItems[i].PadsIndex.Count == 0 && Cad.CadItems[i].Name[0].ToString().ToUpper() != "S")
                {
                    cadItemNotLink.Add(new Tuple <CadItem, int>(Cad.CadItems[i], i));
                }
            }
            foreach (var itemtl in cadItemNotLink)
            {
                var    item             = itemtl.Item1;
                string name             = Convert.ToString(item.Name[0]);
                string nextName         = Convert.ToString(item.Name[1]);
                Point  cadCenter        = Point.Round(item.Center);
                Point  cadCenterRotated = CadItem.GetCenterRotated(cadCenter, cadCenterRotate, cadX, cadY, cadAngle);
                double angle            = Math.Abs(item.Angle % 90);
                angle = angle > 45 ? 90 - angle : angle;
                var sorted = padsList.OrderBy(i => ImageProcessingUtils.DistanceTwoPoint(i.Item1, cadCenterRotated) * Math.Cos(angle * Math.PI / 180.0));

                Tuple <Point, int>[] arSorted = sorted.ToArray();
                int        limit = arSorted.Length > 1000 ? 1000 : arSorted.Length;
                List <int> idGot = new List <int>();
                //double deviationDist = Math.Max(d1, d2) > 0.05 * this.DPI ? 0.1 * Math.Min(d1, d2) : 0.03 * this.DPI;
                for (int i = 0; i < limit - 1; i++)
                {
                    if (!string.IsNullOrEmpty(this.Gerber.PadItems[arSorted[i].Item2].CadFileID))
                    {
                        break;
                    }
                    double d1 = ImageProcessingUtils.DistanceTwoPoint(arSorted[i].Item1, cadCenterRotated);
                    if (d1 > this.DPI)
                    {
                        break;
                    }
                    idGot.Add(arSorted[i].Item2);
                }
                for (int i = 0; i < idGot.Count; i++)
                {
                    this.Gerber.PadItems[idGot[i]].CadFileID    = Cad.CadFileID;
                    this.Gerber.PadItems[idGot[i]].CadItemIndex = itemtl.Item2;
                    item.PadsIndex.Add(idGot[i]);
                }
            }
        }