Пример #1
0
        /// <summary>
        /// Create stackList controls and fills them with the reels, one layer only
        /// </summary>
        /// <param name="reelsToFill">Reels to fill in the stackControls</param>
        private void FillStackListers(List <Reel> reelsToFill)
        {
            if (reelsToFill.Count == 0)
            {
                return;
            }
            GenerateNewPhase();
            int reelIndex = 0;

            //foreach (Reel goodReel in reelsToFill)
            while (reelIndex < reelsToFill.Count)
            {
                Reel             goodReel             = reelsToFill[reelIndex];
                List <StackList> matchingStackListers = stacklisters.FindAll(stacklister => ((stacklister.Layer == ReelLayer.Both) || (stacklister.Layer == goodReel.ReelLayer)));
                if (matchingStackListers.Count == 0)
                {
                    matchingStackListers.Add(GenerateNewPhase());
                }
                int  index  = 0;
                bool succes = false;
                do
                {
                    succes = matchingStackListers[index].AddReel(goodReel);
                    index++; //go to next (in case it failed)
                    if ((index == matchingStackListers.Count) && (!succes))
                    {
                        StackList newStackControl = GenerateNewPhase();
                        if (!newStackControl.AddReel(goodReel))
                        {
                            //this reel can't be added
                            //newStackControl.updateAllListsEvent -= updateAllListsEvent;
                            //stacklisters.Remove(newStackControl);
                            //newStackControl.Dispose();
                            reelIndex--; //go to previous : otherwise a reel could be skipped
                            reelsToFill.Remove(goodReel);
                        }
                        succes = true;
                    }
                } while (!succes);
                reelIndex++;
            }
        }
        /// <summary>
        /// Load a pick and place project from the project.path
        /// </summary>
        /// <param name="project">Project to load data to</param>
        /// <exception cref="PickAndPlace.FileOperationsException">Thrown when data is missing or when a line could not be decoded</exception>
        public static void LoadProject(PnpProject project)
        {
            List <PnpComponent> includedComponents = new List <PnpComponent>();
            List <PnpComponent> excludedComponents = new List <PnpComponent>();

            //project.stackListers = new List<StackList>();
            using (StreamReader reader = new StreamReader(project.Path))
            {
                while (!reader.EndOfStream)
                {
                    string curLine = reader.ReadLine();
                    //# is a line with comment
                    if (curLine.StartsWith("#") || String.IsNullOrWhiteSpace(curLine))
                    {
                        continue;
                    }
                    string[] splitedLine = curLine.Split(new char[] { ',', '=' }, StringSplitOptions.None);
                    switch (splitedLine[0])
                    {
                    case "ProjectName":
                        project.ProjectName = splitedLine[1];
                        break;

                    case "ProjectFolder":
                        project.ProjectFolder = splitedLine[1];
                        break;

                    case "MachineType":
                        project.Machine = (IMachine)Assembly.GetExecutingAssembly().CreateInstance(splitedLine[1]);
                        project.Machine.DefaultSpeed = Convert.ToInt32(splitedLine[3]);
                        for (int i = 5; i < splitedLine.Length; i++)
                        {
                            Nozzle nozzle = PNPconverterTools.StringToNozzle(splitedLine[i]);
                            project.Machine.SetNozzle(i - 5, nozzle);
                        }
                        break;

                    case "originOffsetX":
                        project.HorizontalOriginOffset = float.Parse(splitedLine[1]);
                        project.VerticalOriginOffset   = float.Parse(splitedLine[3]);
                        break;

                    case "boardsX":
                        project.BoardsX = Convert.ToInt32(splitedLine[1]);
                        project.BoardsY = Convert.ToInt32(splitedLine[3]);
                        break;

                    case "distanceX":
                        project.DistanceX = float.Parse(splitedLine[1]);
                        project.DistanceY = float.Parse(splitedLine[3]);
                        break;

                    case "boardDimX":
                        project.BoardWidth  = float.Parse(splitedLine[1]);
                        project.BoardLength = float.Parse(splitedLine[3]);
                        break;

                    case "Designator":
                        //load new component
                        string designator = splitedLine[1];
                        string manufacturerPartNumber_ = splitedLine[3];
                        float  x        = float.Parse(splitedLine[5]);
                        float  y        = float.Parse(splitedLine[7]);
                        int    rotation = Convert.ToInt32(splitedLine[9]);
                        Layer  layer_;
                        if (splitedLine[11] == Layer.Bottom.ToString())
                        {
                            layer_ = Layer.Bottom;
                        }
                        else
                        {
                            layer_ = Layer.Top;
                        }
                        Location     location_ = new Location(x, y, rotation, layer_);
                        string       comment   = splitedLine[13];
                        bool         included  = Convert.ToBoolean(splitedLine[15]);
                        PnpComponent comp      = new PnpComponent(designator, location_, comment, manufacturerPartNumber_);
                        if (included)
                        {
                            includedComponents.Add(comp);
                        }
                        else
                        {
                            excludedComponents.Add(comp);
                        }
                        break;

                    case "Phase":
                        //last part of the file
                        string   remainingLines        = reader.ReadToEnd();
                        string[] remainingLinesSplited = remainingLines.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
                        //1) make reels from the components:

                        project.IncludedReels = ComponentsToReels(includedComponents, project.Machine.DefaultSpeed);
                        project.ExcludedReels = ComponentsToReels(excludedComponents, project.Machine.DefaultSpeed);

                        List <Reel> completeReelList = new List <Reel>(project.IncludedReels);
                        completeReelList.AddRange(project.ExcludedReels);
                        //the completeReelList contains all reels, however these reels where not splited
                        List <Reel> topReels, bottomReels;
                        Reel.SplitReelList(completeReelList, out topReels, out bottomReels);
                        completeReelList.Clear();
                        completeReelList.AddRange(topReels);
                        completeReelList.AddRange(bottomReels);
                        //topReels and bottomReels contain all reels, also the excluded reels
                        //they will be reused later

                        //2) read each line
                        StackList newStackList = new StackList(project.Machine, Convert.ToInt32(splitedLine[1]));
                        newStackList.Name = "stackList";
                        project.StackListers.Add(newStackList);
                        int index = -1;

                        for (int i = 0; i < remainingLinesSplited.Length; i++)
                        {
                            splitedLine = remainingLinesSplited[i].Split(new char[] { '=', ',' });
                            if (splitedLine[0] != "Phase")
                            {
                                index++;     //Stacks are saved in order (starting at reel 0 -> reel n)
                                string designator_ = splitedLine[1];
                                if (String.IsNullOrWhiteSpace(designator_))
                                {
                                    continue;                                             //empty stack
                                }
                                bool locked = Convert.ToBoolean(splitedLine[5]);
                                //find the reel in the completeReelList
                                Reel matchingReel = completeReelList.Find(reel => reel.GetDesignators().Contains(designator_));
                                matchingReel.Speed = Convert.ToInt32(splitedLine[3]);
                                newStackList.AddReel(matchingReel, locked, index);
                                project.ReelsInStackList.Add(matchingReel);
                            }
                            else
                            {
                                //make a new phase
                                //Phase=X (X=phase number)
                                newStackList      = new StackList(project.Machine, Convert.ToInt32(splitedLine[1]));
                                newStackList.Name = "stackList";
                                project.StackListers.Add(newStackList);
                                index = -1;
                            }
                        }
                        //Reusing topReels and bottomReels
                        Reel.SplitReelList(project.ReelsInStackList, out topReels, out bottomReels);
                        project.TopReels    = topReels;
                        project.BottomReels = bottomReels;

                        break;

                    default:
                        throw new FileOperationsException(String.Format("Unable to read file: {0}{2}Problem with reading folowing line:{2}{1}", project.Path, curLine, Environment.NewLine));
                    }
                }
            }
            if (project.StackListers.Count == 0)
            {
                //no phases saved
                project.IncludedReels = ComponentsToReels(includedComponents, project.Machine.DefaultSpeed);
                project.ExcludedReels = ComponentsToReels(excludedComponents, project.Machine.DefaultSpeed);
            }
            if ((project.HorizontalOriginOffset == -1f) || (project.VerticalOriginOffset == -1f) ||
                (project.BoardsX == -1) || (project.BoardsY == -1) ||
                (project.DistanceX == -1f) || (project.DistanceY == -1f) ||
                (project.BoardWidth == -1f) || (project.BoardLength == -1f) ||
                (project.Machine == null))
            {
                throw new FileOperationsException("Not all data was found in the file");
            }
        }