public void Screen() { using (var cad = new AutoCadConnector()) { try { AcadDocument doc = cad.Application.Documents.Open(FileFullName); Scale = Convert.ToDouble(doc.GetVariable("DIMSCALE")); var points = (double[])doc.Limits; var width = (int)Math.Ceiling((points[2] - points[0]) / Scale); var height = (int)Math.Ceiling((points[3] - points[1]) / Scale); //Console.WriteLine(@"file name:{0},GetPaperSize:{1}*{2}", FileShortName, w,h); doc.Close(false, ""); Angle = width > height ? 0 : 90; Width = width; Height = height; PrintedNum = 0; } catch (Exception ex) { MessageBox.Show(string.Format("调用CAD执行图纸筛选时时发生错误!\n错误描述:{0}", ex.Message), @"批量打印", MessageBoxButtons.OK, MessageBoxIcon.Error); throw; } } }
private void UploadCurCAD(AcadApplication AcadApp, IXMRK pXMRK) { AcadDocument AcadDoc = AcadApp.ActiveDocument; Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption); AcadDoc.Save(); string path = AcadDoc.Path; string DocmantPath = AcadDoc.FullName; //string strPath = HR.Utility.CommonConstString.STR_TempPath + "\\" + pXMRK.ID + ".dwg"; string TempstrPath = HR.Utility.CommonConstString.STR_TempPath + pXMRK.TZBH + ".dwg"; AcadDoc.SaveAs(TempstrPath, AcSaveAsType.ac2000_dwg, null); AcadDoc = AcadApp.ActiveDocument; AcadDoc.Close(); Application.DoEvents(); /* if (System.IO.File.Exists(strPath)) * { * System.IO.File.Delete(strPath); * } * System.IO.File.Copy(TempstrPath, strPath);*/ pXMRK.UploadCAD(TempstrPath); }
private bool OpenDwgAndProcessFile(string Path) { if (App != null) { AcadDocument Doc = this.App.Documents.Open(Path); App.ZoomExtents(); App.ZoomScaled(0.6, AcZoomScaleType.acZoomScaledRelativePSpace); Doc.Save(); Doc.Close(); return(true); } else { MessageBox.Show("请先点击截面上的AutoCAD启动按钮再使用该软件"); return(false); } }
public void CloseDocument() { try { if (!(_doc == null)) { _doc.Close(Type.Missing, Type.Missing); } } catch (Exception ex) { throw ex; } finally { this._connector = null; this._doc = null; } }
//Dwg转Jpg public static string Dwg2JPG(FileInfo file) { AcadApplication app = (AcadApplication) new AcadDocument().Application; AcadDocument doc = null; string destPath = string.Empty; try { doc = app.Documents.Application.ActiveDocument; doc.SetVariable("sdi", 0); doc.SetVariable("Filedia", 0); doc.SetVariable("BACKGROUNDPLOT", 0); doc.ActiveLayout.ConfigName = "PublishToWeb JPG.pc3"; doc.ActiveLayout.UseStandardScale = true; doc.ActiveLayout.StandardScale = AutoCAD.AcPlotScale.acScaleToFit; doc.ActiveLayout.PlotType = AutoCAD.AcPlotType.acExtents; doc.ActiveLayout.CenterPlot = true; doc.ActiveLayout.PlotRotation = AutoCAD.AcPlotRotation.ac0degrees; doc.Plot.QuietErrorMode = true; destPath = Path.Combine(file.Directory.FullName, Path.GetFileNameWithoutExtension(file.Name) + ".jpg"); doc.Plot.PlotToFile(destPath); return(destPath); } catch (Exception e) { throw e; } finally { try { doc.Close(false); //app.Quit(); } catch { } } }
public void Print() { using (var cad = new AutoCadConnector()) { try { AcadDocument doc = cad.Application.Documents.Open(FileFullName); AcadLayout layout = doc.ModelSpace.Layout; #region Autocad.Layout对象的属性及方法,主要用于打印输出 //Application //Block //CanonicalMediaName //CenterPlot //ConfigName //Document //GetCanonicalMediaNames //GetLocaleMediaName //GetPlotDeviceNames //GetPlotStyleTableNames //Handle //HasExtensionDictionary //ModelType //Name //ObjectID //ObjectName //OwnerID //PaperUnits //PlotHidden //PlotOrigin //PlotRotation //PlotType //PlotViewportBorders //PlotViewportsFirst //PlotWithLineweights //PlotWithPlotStyles //RefreshPlotDeviceInfo //ScaleLineweights //ShowPlotStyles //StandardScale //StyleSheet //TabOrder //UseStandardScale //ViewToPlot #endregion //layout.RefreshPlotDeviceInfo(); //ConfigName 指定打印机配置名。用于布局或打印配置的 PC3 文件或打印设备的名称。 layout.ConfigName = PlotConfigName; layout.StyleSheet = StyleSheet; #region /* * //PlotType 指定布局或打印配置的类型。 * //acDisplay 打印当前显示的所有对象。 * //acExtents 打印当前选定空间范围内的所有对象。 * //acLimits 打印当前空间界限内的所有对象。 * //acView 打印在ViewToPlot属性中命名的视口。 * //acWindow 打印在SetWindowToPlot方法中指定的窗口中的所有对象。 * //acLayout 打印指定纸张尺寸边界内的所有对象,它的原点是由布局选项卡中从坐标(0,0)计算得到。该选项在从模型空间打印时不可用。 * * */ #endregion layout.PlotType = AcPlotType.acExtents; //PlotWithPlotStyles:指定对象是按在打印样式文件中分配的配置打印,还是按图形文件中的配置打印。 layout.PlotWithPlotStyles = true; //CanonicalMediaName 按名称指定图纸尺寸.String[字符串]; 可读写,图纸尺寸的名称 layout.CanonicalMediaName = CanonicalMediaName; layout.PaperUnits = AcPlotPaperUnits.acMillimeters; layout.PlotOrigin = new double[] { 0, 0 }; layout.UseStandardScale = true; layout.StandardScale = AcPlotScale.acScaleToFit; if (MapSheet == "A3" || MapSheet == "A4") { layout.PlotRotation = Angle == 0 ? AcPlotRotation.ac90degrees : AcPlotRotation.ac0degrees; } else { layout.PlotRotation = Angle == 90 ? AcPlotRotation.ac90degrees : AcPlotRotation.ac0degrees; } layout.RefreshPlotDeviceInfo(); double w, h; layout.GetPaperSize(out w, out h); Console.WriteLine(@"图幅:{0},{1}*{2}", CanonicalMediaName, w, h); doc.Plot.PlotToDevice(null); doc.Close(false); PrintedNum++; } catch (Exception ex) { MessageBox.Show(string.Format("调用CAD执行批量打印时发生错误!\n错误描述:{0}", ex.Message), @"批量打印", MessageBoxButtons.OK, MessageBoxIcon.Error); throw; } } }
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; } } }
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; } } }
/// <summary> /// 打印线程,可传入参数 /// </summary> /// <param name="obj"></param> private void Thread_print(object obj) { bool islock = false; while (!islock) { try { islock = Monitor.TryEnter(progressNum); if (islock) { progressNum.num++; Monitor.Exit(progressNum); } } catch { } } AcadDocument drawing=null; mulInfo str=(mulInfo)obj; AcadApplication app = NewCAD(); string[] PrintNames_inThread = str.Files; foreach (string fname in PrintNames_inThread) { bool isFinish = false; while (!isFinish) { try { app.Documents.Open(fname,true); isFinish = true; } catch { isFinish = false; } } //防止AutoCAD响应慢,上一步还未响应程序就开始下一步命令,不断try直到成功执行 isFinish = false; while (!isFinish) { try { drawing = app.ActiveDocument; isFinish = true; } catch { isFinish = false; } } isFinish = false; while (!isFinish) { try { drawing.SetVariable("BACKGROUNDPLOT", 0); drawing.ActiveLayout.CenterPlot = true;//居中 drawing.ActiveLayout.ConfigName = str.Selected_Printer; drawing.ActiveLayout.UseStandardScale = true; drawing.ActiveLayout.StandardScale = AcPlotScale.acScaleToFit; drawing.ActiveLayout.PlotType = AcPlotType.acExtents; drawing.ActiveLayout.CanonicalMediaName = str.Selected_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 = str.SaveAddr + "/" + pureName; //drawing.SaveAs("C:/Users/JabinGuo/Desktop/convert", AcSaveAsType.ac2007_dxf); #endregion islock = false; while (!islock) { try { islock = Monitor.TryEnter(drawing); } catch { //此处测试过exception } } bool isfinish = false; while (!isfinish) { try { isfinish = drawing.Plot.PlotToFile(SaveFileName); } catch { } } Monitor.Exit(drawing); drawing.Close(false); } app.Quit(); islock = false; while (!islock) { try { islock = Monitor.TryEnter(progressNum); if (islock) { progressNum.num--; } } catch (Exception e) { MessageBox.Show(e.Message); } }Monitor.Exit(progressNum); return; //button_FindFile.Enabled = true; }