示例#1
0
        private void Thread_print2()
        {
            AcadDocument drawing = null;
            bool islock = false;
            
            DB.BLL.TASK bll_Task = new DB.BLL.TASK();
            while (true)
            {
                if (0 < bll_Task.GetModelList("1=1").Count)
                {
                    if (TaskFlag == 2)
                    {
                        TaskFlag = 0;
                    }
                    DB.Model.TASK model_Task;
                    if (0 < bll_Task.GetModelList("ISPRINTING=0").Count)
                    {
                        try
                        {
                            model_Task = bll_Task.GetModelList("ISPRINTING=0")[0];
                        }
                        catch { continue; }
                        if (0 == model_Task.ORDERED)
                        {
                            model_Task.ORDERED = 2;
                            bll_Task.Update(model_Task);
                        }
                        else if (2 == model_Task.ORDERED)
                        {
                            model_Task.ISPRINTING = 2;
                            model_Task.PRINTING_TIME = System.DateTime.Now;
                            bll_Task.Update(model_Task);
                            string fname = model_Task.FILE_FULLNAME;
                            bool isFinish = false;
                            while (!isFinish)
                            {
                                try
                                {
                                    app2.Documents.Open(fname, true);
                                    isFinish = true;
                                }
                                catch
                                {
                                    isFinish = false;
                                }
                            }
                            //防止CAD反应慢,上一步任务未完成就开始了下一步命令
                            
                            isFinish = false;
                            while (!isFinish)
                            {
                                try
                                {
                                    drawing = app2.ActiveDocument; isFinish = true;
                                }
                                catch
                                {
                                    isFinish = false;
                                }
                            }
                            isFinish = false;
                            while (!isFinish)
                            {
                                try
                                {
                                    drawing.SetVariable("BACKGROUNDPLOT", 0);
                                    drawing.ActiveLayout.CenterPlot = true;//居中
                                    drawing.ActiveLayout.ConfigName = model_Task.PRINTER;
                                    drawing.ActiveLayout.PlotRotation = AcPlotRotation.ac0degrees;
                                    drawing.ActiveLayout.UseStandardScale = true;
                                    drawing.ActiveLayout.StandardScale = AcPlotScale.acScaleToFit;
                                    drawing.ActiveLayout.PlotType = AcPlotType.acExtents;
                                    drawing.ActiveLayout.CanonicalMediaName = model_Task.PAPER;
                                    isFinish = true;
                                }
                                catch
                                {
                                    isFinish = false;
                                }
                            }
                            #region 截取文件名
                            string[] dwgFullName = fname.Split('\\');
                            string[] dwgName = dwgFullName[dwgFullName.Length - 1].Split('.');
                            string pureName = null;
                            for (int i = 0; i < dwgName.Length - 2; i++)
                            {
                                pureName += dwgName[i];
                                pureName += ".";
                            }

                            pureName += dwgName[dwgName.Length - 2];
                            string SaveFileName = model_Task.SAVE_PATH + "/" + pureName;
                            #endregion
                            islock = false;
                            while (!islock)
                            {
                                try
                                {
                                    islock = Monitor.TryEnter(drawing);
                                }
                                catch
                                {
                                    
                                }
                            }
                            bool isfinish = false;
                            while (!isfinish)
                            {
                                try
                                {
                                    isfinish = drawing.Plot.PlotToFile(SaveFileName);
                                }
                                catch { }
                            }
                            Monitor.Exit(drawing);
                            drawing.Close(false);
                            bll_Task.Delete(model_Task.TASK_GUID);
                        }
                    }
                    if (0 < bll_Task.GetModelList("ISPRINTING!=0").Count)
                    {
                        try
                        {
                            model_Task = bll_Task.GetModelList("ISPRINTING!=0")[0];
                        }
                        catch { continue; }
                        if (2 < System.DateTime.Now.Subtract((DateTime)model_Task.PRINTING_TIME).Minutes)
                        {
                            switch (model_Task.ISPRINTING)
                            {
                                case 1:
                                    try
                                    {
                                        app1.Quit();
                                        
                                    }
                                    catch /*(Exception e) */{ /*MessageBox.Show(e.ToString());*/ }
                                    Thread.Sleep(2000);
                                    app1 = NewCAD();
                                    break;
                                case 2:
                                    try
                                    {
                                        app2.Quit();
                                    }
                                    catch/* (Exception e) */{/* MessageBox.Show(e.ToString()); */}
                                    Thread.Sleep(2000);
                                    app2 = NewCAD();
                                    break;
                                default:
                                    MessageBox.Show("找不到打印{0}的进程", model_Task.FILE_FULLNAME);
                                    break;
                            }

                            model_Task.ORDERED = 0;
                            model_Task.ISPRINTING = 0;
                            model_Task.PRINTING_TIME = System.DateTime.Now;
                            bll_Task.Update(model_Task);
                        }
                    }
                }
                else
                {
                    
                    continue;
                }
            }
        }
示例#2
0
        private void Thread_print1()
        {
            AcadDocument drawing = null;

            

            bool islock = false;
            //mulInfo TaskInfo;
            DB.BLL.TASK bll_Task = new DB.BLL.TASK();
            while (true)
            {
                if (0 < bll_Task.GetModelList("1=1").Count)//判断任务列表是否为空
                {
                    if (TaskFlag == 2)
                    {
                        TaskFlag = 0;
                    }
                    DB.Model.TASK model_Task;
                    if (0 < bll_Task.GetModelList("ISPRINTING=0").Count )//判断是否有未进行的打印任务
                    {
                        try
                        {
                            model_Task = bll_Task.GetModelList("ISPRINTING=0")[0];
                        }
                        catch { continue; }
                        if (0 == model_Task.ORDERED)//如果此任务没有被预约,先预约此任务,此次循环并不开始打印,这是为了防止多个cad进程同时打印一个任务
                        {
                            model_Task.ORDERED = 1;
                            bll_Task.Update(model_Task);
                        }
                        else if (1 == model_Task.ORDERED)//如果此任务已经被预约,且预约者是进程1,那么开始打印任务
                        {
                            model_Task.ISPRINTING = 1;
                            model_Task.PRINTING_TIME = System.DateTime.Now;
                            bll_Task.Update(model_Task);
                            string fname = model_Task.FILE_FULLNAME;
                            bool isFinish = false;
                            while (!isFinish)
                            {
                                try
                                {
                                    app1.Documents.Open(fname, true);
                                    
                                    isFinish = true;
                                }
                                catch
                                {
                                    isFinish = false;
                                }
                            }
                            //防止CAD反应慢,上一步任务未完成就开始了下一步命令
                            //List<DB.Model.TASK> model_Task = bll_Task.GetModelList("1=1");

                            isFinish = false;
                            while (!isFinish)
                            {
                                try
                                {
                                    drawing = app1.ActiveDocument; isFinish = true;
                                }
                                catch
                                {
                                    isFinish = false;
                                }
                            }
                            isFinish = false;
                            while (!isFinish)
                            {
                                try
                                {
                                    drawing.SetVariable("BACKGROUNDPLOT", 0);
                                    drawing.ActiveLayout.CenterPlot = true;//居中
                                    drawing.ActiveLayout.ConfigName = model_Task.PRINTER;
                                    drawing.ActiveLayout.PlotRotation = AcPlotRotation.ac0degrees;
                                    drawing.ActiveLayout.UseStandardScale = true;
                                    drawing.ActiveLayout.StandardScale = AcPlotScale.acScaleToFit;
                                    drawing.ActiveLayout.PlotType = AcPlotType.acExtents;
                                    drawing.ActiveLayout.CanonicalMediaName = model_Task.PAPER;
                                    
                                    isFinish = true;
                                }
                                catch
                                {
                                    isFinish = false;
                                }
                            }
                            #region 截取文件名
                            string[] dwgFullName = fname.Split('\\');
                            string[] dwgName = dwgFullName[dwgFullName.Length - 1].Split('.');
                            string pureName = null;
                            for (int i = 0; i < dwgName.Length - 2; i++)
                            {
                                pureName += dwgName[i];
                                pureName += ".";
                            }

                            pureName += dwgName[dwgName.Length - 2];
                            string SaveFileName = model_Task.SAVE_PATH + "/" + pureName;
                            //drawing.SaveAs("C:/Users/JabinGuo/Desktop/convert", AcSaveAsType.ac2007_dxf);
                            #endregion
                            islock = false;
                            while (!islock)
                            {
                                try
                                {
                                    islock = Monitor.TryEnter(drawing);
                                }
                                catch
                                {
                                   
                                }
                            }
                            bool isfinish = false;
                            while (!isfinish)
                            {
                                try
                                {
                                    isfinish = drawing.Plot.PlotToFile(SaveFileName);
                                }
                                catch { }
                            }
                            Monitor.Exit(drawing);
                            drawing.Close(false);
                            bll_Task.Delete(model_Task.TASK_GUID);
                        }
                    } 
                    if (0 < bll_Task.GetModelList("ISPRINTING!=0").Count)//如果长时间没有完成打印任务,说明cad卡死,关闭此进程并重新打开
                    {
                        try
                        {
                            model_Task = bll_Task.GetModelList("ISPRINTING!=0")[0];
                        }
                        catch { continue; }
                        if (2 < System.DateTime.Now.Subtract((DateTime)model_Task.PRINTING_TIME).Minutes)
                        {
                            switch (model_Task.ISPRINTING)
                            {
                                case 1:
                                    try
                                    {
                                        app1.Quit();
                                        
                                    }catch/*(Exception e) */{ /*MessageBox.Show(e.ToString());*/ }
                                    Thread.Sleep(2000);
                                    app1 = NewCAD();
                                    break;
                                case 2:
                                    try
                                    {
                                        //app2.ActiveDocument.Close(false);
                                        app2.Quit();
                                    }catch/*(Exception e)*/ { /*MessageBox.Show(e.ToString());*/ }
                                    Thread.Sleep(2000);
                                    app2 = NewCAD();
                                    break;
                                default:
                                    MessageBox.Show("找不到打印{0}的进程", model_Task.FILE_FULLNAME);
                                    break;
                            }
                            model_Task.ORDERED = 0;
                            model_Task.ISPRINTING = 0;
                            model_Task.PRINTING_TIME = System.DateTime.Now;
                            bll_Task.Update(model_Task);
                        }
                    }                   
                }
                else
                {
                    if (0 == TaskFlag)
                    {
                        MessageBox.Show("打印完成");
                        TaskFlag = 1;
                    }
                    if (1 == TaskFlag)
                    {
                        TaskFlag = 2;
                    }
                    continue;
                }
            }
        }