/// <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");
            }
        }
Пример #2
0
        /// <summary>
        /// Checks all reels and fill the stacks automatic with the good reels
        /// </summary>
        /// <param name="showWarnings">True if warnings are shown, else false</param>
        private void AssignReels(bool showWarnings)
        {
            //1) Filter the reels
            List <Reel> acceptedReels = new List <Reel>();

            acceptedReels = reelsToPlace.FindAll(curReel => pnpMachine.ReelCanBePlaced(curReel));
            if (showWarnings)
            {
                //Show warnings to the user
                if (acceptedReels.Count == 0)
                {
                    MessageBox.Show("No reels accepted", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                int numberRejected = reelsToPlace.Count - acceptedReels.Count;
                //Not all components are accepted
                if ((numberRejected != 0) &&
                    (MessageBox.Show(numberRejected.ToString() + " rejected" + Environment.NewLine + "Do you want to continue?",
                                     "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No))
                {
                    return;
                }
            }
            //2) clear the old tabPages
            ClearPhases();

            //3) split the list: top and bottom
            List <Reel> topReels, bottomReels;

            Reel.SplitReelList(acceptedReels, out topReels, out bottomReels);
            FillStackListers(topReels);
            FillStackListers(bottomReels);


            int index = 0;

            while (index < stacklisters.Count)
            {
                if (stacklisters[index].Layer == ReelLayer.Top)
                {
                    stacklisters[index].SetTotalList(topReels);
                }
                else if (stacklisters[index].Layer == ReelLayer.Bottom)
                {
                    stacklisters[index].SetTotalList(bottomReels);
                }
                else if (!stacklisters[index].ContainsReels)
                {
                    //Stacklayer == both -> empty stacklist
                    //if, for some reasen (reasen = bug), the layer is both and it contains reels, it should not be removed
                    StackList removedStackList = stacklisters[index]; //need pointer to control for dispose
                    removedStackList.updateAllListsEvent -= UpdateAllListsEvent;
                    stacklisters.RemoveAt(index);
                    removedStackList.Dispose();
                    continue;
                }
                GenerateTabPage(stacklisters[index]);
                index++; //index > 0
            }
            if (index != 0)
            {
                this.pnlExportButtons.Enabled = true;
            }
        }