Exemplo n.º 1
0
 public LuaEditor(string code, ACA.LabelX.Label.LabelSet labelset)
 {
     this.code     = code;
     this.labelset = labelset;
     InitializeComponent();
     init();
 }
        private bool HandlePrintJob(PrintGroupItem it, PrintJobInfo jobinfo, string PrintJobsRootFolder, string PaperDefinitionsRootFolder, string LabelDefinitionsRootFolder, int language)
        {
            bool   ret = true;
            string sXMLFile;

            sXMLFile = jobinfo.FullFilename;

            //Retrieve some information for the selected printjob
            // The requested queueu, the printgroup (should be the current), and the LabelType
            string sPrintQueue;
            string sPrintGroup;
            string sLabelType;

            sPrintGroup = string.Empty;
            sPrintQueue = string.Empty;
            sLabelType  = string.Empty;
            try
            {
                XPathDocument doc;
                doc = new XPathDocument(sXMLFile);

                XPathNavigator nav;
                nav = doc.CreateNavigator();

                XPathNodeIterator theNode;
                theNode = nav.Select("/printjob/destination/printqueue");

                if (theNode != null)
                {
                    theNode.MoveNext();
                    sPrintQueue = theNode.Current.Value;
                }
                theNode = nav.Select("/printjob/destination/printgroup");

                if (theNode != null)
                {
                    theNode.MoveNext();
                    sPrintGroup = theNode.Current.Value;
                }

                theNode = nav.Select("/printjob/destination/labeltype");

                if (theNode != null)
                {
                    theNode.MoveNext();
                    sLabelType = theNode.Current.Value;
                }
            }
            catch (Exception e)
            {
                GlobalDataStore.Logger.Warning(string.Format("The following error occured while handeling file {0}:", jobinfo.FullFilename)
                                               + Environment.NewLine + e.Message);
                return(false);
            }
            //We now have the labeltype requested... we gather information on which paper this can be printed...
            List <string> papertypes;

            papertypes = new List <string>();

            try
            {
                papertypes = GetPaperTypesOfLabelType(sLabelType);
            }
            catch (System.IO.FileNotFoundException)
            {
                //dit zou alleen mogen gebeuren als het labeltype niet bestaat als xmlbestand...
                GlobalDataStore.Logger.Warning(string.Format("Label {0} not defined. Job on hold.", sLabelType));

                if (papertypes.Count > 0)
                {
                    papertypes.Clear();
                }
                return(false);
            }

            try
            {
                if (papertypes.Count == 0)
                {
                    //can not print on no paper...
                    GlobalDataStore.Logger.Warning(string.Format("Label {0} has no defined paper types. Job on hold.", sLabelType));
                    return(false);
                }
                else
                {
                    if (papertypes.Count > 1)
                    {
                        //only one default allowed for now.
                        GlobalDataStore.Logger.Warning(string.Format("Label {0} has more than one default paper type. Job on hold.", sLabelType));
                        return(false);
                    }
                    //check if papertype exists as an xml file
                    string paperFileName = PaperDefinitionsRootFolder + papertypes[0] + ".xml";
                    if (!File.Exists(paperFileName))
                    {
                        GlobalDataStore.Logger.Warning(string.Format("Papertype {0} has no xml file. Job on hold.", papertypes[0]));
                        return(false);
                    }
                }
                PrinterItemLocals pils;
                pils = new PrinterItemLocals();
                foreach (PrinterItem pit in it.GroupPrinters)
                {
                    PrinterItemLocal pil = new PrinterItemLocal();
                    pil.item = pit;
                    pils.Add(pil);
                    //
                    //Gather windowsinformation for each printer in the windows printer queue
                    // We gather the numberofjobs and if the printer is online.
                    //
                }
                PrinterItemLocal bestFit;
                bestFit = null;
                foreach (PrinterItemLocal pil in pils)
                {
                    //we do not print to printers that are physically offline
                    if (!pil.item.Online)
                    {
                        continue;
                    }

                    //we do not print to printers that are marked disabled.
                    if (!pil.item.Enabled)
                    {
                        continue;
                    }

                    if (pil.item.Trays != null)
                    {
                        foreach (PrinterTrayItem tit in pil.item.Trays)
                        {
                            if (tit.CurrentPapertypeName.Equals(papertypes[0], StringComparison.OrdinalIgnoreCase) || pil.item.LongName.Contains("Microsoft XPS Document Writer"))
                            {
                                //papiersoort klopt..
                                if (bestFit == null)
                                {
                                    pil.Tray = tit;
                                    bestFit  = pil;
                                }
                                else
                                {
                                    if (pil.item.QueueLength < bestFit.item.QueueLength)
                                    {
                                        pil.Tray = tit;
                                        bestFit  = pil;
                                    }
                                }
                            }
                        }
                    }
                }
                //We have selected a printer to print to
                if (bestFit != null)
                {
                    ACA.LabelX.PrintJob.PrintJob p = new ACA.LabelX.PrintJob.PrintJob(PaperDefinitionsRootFolder, LabelDefinitionsRootFolder);
                    p.Parse(jobinfo.FullFilename);

                    //
                    if (p.labels.Count == 0)
                    {
                        //Oeps.. a printjob with a default label, but no actual label is detected
                        //Mark this job as handled now, without error
                        ret = MarkPrintJobReady(jobinfo, "No Label in job.");
                        return(ret);
                    }

                    //Check if all pictures are available
                    ACA.LabelX.Label.LabelSet labelset = new ACA.LabelX.Label.LabelSet();
                    labelset.CurrentLabel    = p.labels[0];
                    labelset.DefaultLabel    = p.Defaultlabel;
                    labelset.BaseLabel       = p.LabelDef.DefaultLabel;
                    labelset.StaticVarsLabel = p.StaticVarslabel;

                    if (!p.LabelDef.CheckAllPicturesAvailable(labelset, language, true))
                    {
                        GlobalDataStore.Logger.Warning(string.Format("Missing picture to print. Job on hold.", sLabelType));
                        return(false);
                    }
                    //
                    //check if th language requested is pressent in the printjob
                    //
                    bool bFound = false;
                    if (p.languages != null)
                    {
                        if (p.languages.Count > 0)
                        {
                            foreach (PrintJob.PrintJob.PrintLanguage x in p.languages)
                            {
                                if (x.Id == language)
                                {
                                    bFound = true;
                                }
                            }
                        }
                    }

                    //We have selected a language this job can be printed in...
                    if (bFound)
                    {
                        ACA.LabelX.PrintEngine.PrintEngine pi = new ACA.LabelX.PrintEngine.PrintEngine(Environment.MachineName);
                        pi.DesignMode = false;
                        pi.AddPrintJob(p);
                        GlobalDataStore.Logger.Info("Printing " + p.ID.ToString());
                        pi.Print(p.ID.ToString(), bestFit.item.LongName, bestFit.Tray.TrayName, papertypes[0], 0, uint.MaxValue, language);
                        bestFit.item.QueueLength++;     //There is now one job more in the queue..


                        ret = MarkPrintJobReady(jobinfo, bestFit.item.LongName);
                        //Voeg printernaam en datum toe aan printjob xml file
                        //mve, 2016-03-06

                        /*
                         * lock (GlobalDataStore.LockClass)
                         * {
                         *  string path = jobinfo.FullFilename;
                         *  XmlDocument theDoc = new XmlDocument();
                         *  if (!File.Exists(path))
                         *  {
                         *      return false;
                         *  }
                         *  theDoc.Load(path);
                         *
                         *  XmlNode printedto = theDoc.SelectSingleNode("/printjob/destination/printedto");
                         *  if (printedto == null)
                         *  {
                         *      XmlNode destinationnode = theDoc.SelectSingleNode("/printjob/destination");
                         *      XmlElement NewPrintedTo = theDoc.CreateElement("printedto");
                         *      NewPrintedTo.InnerText = bestFit.item.LongName;
                         *      destinationnode.AppendChild(NewPrintedTo);
                         *  }
                         *  else
                         *  {
                         *      printedto.InnerText = bestFit.item.LongName;
                         *  }
                         *
                         *  XmlNode lastprinted = theDoc.SelectSingleNode("/printjob/destination/lastprinted");
                         *  if (lastprinted == null)
                         *  {
                         *      XmlNode lastprintednode = theDoc.SelectSingleNode("/printjob/destination");
                         *      XmlElement NewLastPrinted = theDoc.CreateElement("lastprinted");
                         *      NewLastPrinted.InnerText = System.DateTime.Now.ToString("s");
                         *      lastprintednode.AppendChild(NewLastPrinted);
                         *  }
                         *  else
                         *  {
                         *      lastprinted.InnerText = System.DateTime.Now.ToString("s");
                         *  }
                         *
                         *  XmlNode autorelease = theDoc.SelectSingleNode("/printjob/autorelease");
                         *  autorelease.InnerText = "false";
                         *
                         *
                         *  int counter = 0;
                         *  bool saveSuccessful = false;
                         *  while ((counter < 5) && (saveSuccessful == false))
                         *  {
                         *      try
                         *      {
                         *          theDoc.Save(path);
                         *          Thread.Sleep(2000);
                         *          saveSuccessful = true;
                         *      }
                         *      catch (Exception ex)
                         *      {
                         *          Thread.Sleep(2000);
                         *          counter += 1;
                         *          GlobalDataStore.Logger.Warning(string.Format("The file {0} could not be saved: {1}", theDoc.Name, ex.Message));
                         *      }
                         *  }
                         *  if (!saveSuccessful)
                         *      ret = false;
                         *
                         * }
                         */
                    }
                    else
                    {
                        GlobalDataStore.Logger.Warning(String.Format("Language of printjob {0} not suitable for this location.", p.ID.ToString()));
                        ret = false;
                    }
                }
                else
                {
                    GlobalDataStore.Logger.Warning(String.Format("No suitable printer found for file {0}.", jobinfo.FullFilename));
                    ret = false;
                }
            }
            catch (Exception egen)
            {
                GlobalDataStore.Logger.Error(string.Format("Unexpected Error in HandlePrintJob: {0}.", egen.Message));
                GlobalDataStore.Logger.Error(string.Format("Extra info: {0}.", egen.StackTrace));
                if (egen.InnerException != null)
                {
                    GlobalDataStore.Logger.Error(string.Format("Extra info: {0}.", egen.InnerException.Message));
                }
                return(false);
            }

            return(ret);
        }
Exemplo n.º 3
0
        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            Length LeftMargin;
            Length TopMargin;

            //The labelnumber of the first label to be printed on this page!
            //This is NOT necessary the index of the labelnumer in the list as
            //each label can have a quantity to be printed of more than one.

            //Euhhh NextPage contains the number of the page to be drawn next.
            //But because we a now drawing it... it's the page to be drawn now.
            //Upon leave we increase this number. We work zero based, to the first page
            //actually has numbe 0
            uint FirstLabel = NextPage * LabelsPerPage;

            //LastLabel = the labelnumber of the max last label on THIS page,
            //(thus not the end of the document)
            uint LastLabel = ((NextPage + 1) * LabelsPerPage) - 1;

            //A limit can be given bij pages (print page 5 to 7) or
            //by reaching the last label.
            //So the number labels max to go is:
            uint LastLabelOfJob;

            if (ToPage == uint.MaxValue)
            {
                LastLabelOfJob = uint.MaxValue;
            }
            else
            {
                LastLabelOfJob = (ToPage + 1) * LabelsPerPage - 1;
            }
            //uint LastLabelOfJob = ((ToPage+1) * LabelsPerPage) - 1;
            uint AllLabelsCount;

            AllLabelsCount = Labels.CountAll - 1; //make this max also zero based

            //If we requested pages 0 to n but we need less than n pages to
            //print all labels, we limit to the last label.
            if (LastLabelOfJob > AllLabelsCount)
            {
                LastLabelOfJob = AllLabelsCount;
            }

            //If we have less labels than would fit the page, will still need to
            //stop at the last label...
            if (LastLabel > AllLabelsCount)
            {
                LastLabel = AllLabelsCount;
            }

            //Adjust for physical margings of the printer in respect to
            //paperdefinitions.
            //test
            //PrinterBounds theBounds;
            //theBounds = new PrinterBounds(ev);
            if (!theBoundsCached)
            {
                theBounds       = new PrinterBounds(ev);
                theBoundsCached = true;
            }
            //*
            LeftMargin = theBounds.HardMarginLeft;
            TopMargin  = theBounds.HardMarginTop;

            PaperDef.PhysicalLeftMargin = LeftMargin;
            PaperDef.PhysicalTopMargin  = TopMargin;

            ev.Graphics.PageUnit = GraphicsUnit.Pixel;
            float dpiX = ev.Graphics.DpiX;
            float dpiY = ev.Graphics.DpiY;

            uint LabelIndex      = 0;
            uint HorizontalIndex = 0;
            uint VerticalIndex   = 0;
            uint QuantityCount   = 0;

            foreach (Label.Label CurrentLabel in Labels)
            {
                if (LabelIndex + CurrentLabel.Quantity < FirstLabel)
                {
                    LabelIndex += CurrentLabel.Quantity;
                    continue;
                }

                // Stop if we don't need any more labels
                if (LabelIndex > LastLabel)
                {
                    break;
                }

                for (QuantityCount = 0; QuantityCount < CurrentLabel.Quantity; QuantityCount++)
                {
                    // skip labels that don't need printing..
                    if (LabelIndex < FirstLabel)
                    {
                        LabelIndex++;
                        continue;
                    }

                    // Stop if we don't need any more labels
                    if (LabelIndex > LastLabel)
                    {
                        break;
                    }

                    Tools.Rectangle          rect     = PaperDef.GetLabelRectangle((int)HorizontalIndex, (int)VerticalIndex);
                    System.Drawing.Rectangle convRect = new System.Drawing.Rectangle((int)rect.Left.InPixels(dpiX), (int)rect.Top.InPixels(dpiY), (int)rect.Size.Width.InPixels(dpiX), (int)rect.Size.Height.InPixels(dpiY));

                    ACA.LabelX.Label.LabelSet labelset = new ACA.LabelX.Label.LabelSet();

                    labelset.CurrentLabel    = CurrentLabel;
                    labelset.DefaultLabel    = DefaultLabel;
                    labelset.BaseLabel       = LabelDef.DefaultLabel;
                    labelset.StaticVarsLabel = StaticVarslabel;


                    executeLua(labelset.CurrentLabel, labelset.DefaultLabel, labelset.BaseLabel, labelset.StaticVarsLabel);

                    CurrentLabel.Draw(ev.Graphics, convRect, LabelDef, labelset, Language, DrawBorders);
                    counter++;
                    if (counter > 200)
                    {
                        GC.Collect();
                        counter = 0;
                    }
                    HorizontalIndex++;

                    if (HorizontalIndex >= PaperDef.labelLayout.HorizontalCount)
                    {
                        HorizontalIndex = 0;
                        VerticalIndex++;
                    }

                    if (VerticalIndex >= PaperDef.labelLayout.VerticalCount)
                    {
                        VerticalIndex = 0;
                    }

                    LabelIndex++;
                }
            }

            if (LabelIndex <= LastLabelOfJob)
            {
                NextPage++;
                ev.HasMorePages = true;
            }
            else
            {
                NextPage        = 0;
                ev.HasMorePages = false;
            }
        }