“Fork

关于AutoCAD二次开发导出图纸为PDF格式

作为一个刚刚接触AutoCAD二次开发的小白来说,一切都是新鲜的,好像发现新大陆,打开了新的学习大门,越来越感兴趣,同时也深知学无止境,所以努力学习、努力成长。

本次主要写的是关于AutoCAD二次开发的基础文档,目的有两个:1、从基础的学习开始,把学到的或者看到的好东西分享一下。2、记录一下AutoCAD二次开发的过程。也许以后的某一天,回头一看,能够形成一个简单的并且实际应用的体系,初衷如此。

首先引入引用和参考:

 1 using Autodesk.AutoCAD.Runtime;
 2 using Autodesk.AutoCAD.ApplicationServices;
 3 using Autodesk.AutoCAD.DatabaseServices;
 4 using Autodesk.AutoCAD.Colors;
 5 using Autodesk.AutoCAD.Ribbon;
 6 using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
 7 using Autodesk.Windows;
 8 using System;
 9 using System.Collections;
10 using System.Collections.Specialized;
11 using Autodesk.AutoCAD.EditorInput;
12 using Autodesk.AutoCAD.Interop;
13 using Autodesk.AutoCAD.Interop.Common;
14 using Microsoft.VisualBasic;
15 

 然后编写命令代码:

 1 18 [CommandMethod("dwg2PDF",CommandFlags.Session)]
 2 19 public static void dwg2PDF()
 3 20 {
 4 21 
 5 22 Document activeDoc = Application.DocumentManager.MdiActiveDocument;
 6 23 AcadDocument ThisDrawing = (AcadDocument) activeDoc.GetAcadDocument();
 7 24 
 8 25 AcadLayout layout = ThisDrawing.ActiveLayout;
 9 26 
10 27 String MediaName = layout.CanonicalMediaName;
11 28 if (MediaName.Equals(""))
12 29 {
13 30 activeDoc.Editor.WriteMessage("There is no media set for the active layout.");
14 31 return;
15 32 }
16 33 else
17 34 {
18 35 activeDoc.Editor.WriteMessage("The media for the active layout is: " + MediaName);
19 36 }
20 37 
21 38 //转换的主要内容
22 39 
23 40 try
24 41 {
25 42 AcadPlotConfiguration oplot = ThisDrawing.PlotConfigurations.Add("PDF", layout.ModelType);
26 43 oplot.PaperUnits = AcPlotPaperUnits.acMillimeters;
27 44 oplot.StyleSheet = "monochrome.ctb";
28 45 oplot.PlotWithPlotStyles = true;
29 46 oplot.ConfigName = "DWG To PDF.pc3";
30 47 oplot.UseStandardScale = true;
31 48 oplot.StandardScale = AcPlotScale.acScaleToFit;
32 49 oplot.PlotType = AcPlotType.acExtents;
33 50 oplot.CenterPlot = true;
34 51 
35 52 Object oMediaNames = layout.GetCanonicalMediaNames();
36 53 
37 54 ArrayList mediaNames = new ArrayList((string[])oMediaNames);
38 55 
39 56 foreach (String sName in mediaNames)
40 57 {
41 58 if (sName.Contains(MediaName))
42 59 {
43 60 oplot.CanonicalMediaName = sName;
44 61 layout.CopyFrom(oplot);
45 62 layout.PlotRotation = AcPlotRotation.ac0degrees;
46 63 layout.RefreshPlotDeviceInfo();
47 64 
48 65 ThisDrawing.SetVariable("BACKGROUNDPLOT", 0);
49 66 ThisDrawing.Plot.QuietErrorMode = true;
50 67 
51 68 string pdfname = Interaction.InputBox("请输入保存文件名称!", "提示");
52 69 
53 70 ThisDrawing.Plot.PlotToFile("C:\\Users\\Administrator\\Desktop\\" + pdfname+ ".pdf", "DWG To PDF.pc3");
54 71 oplot.Delete();
55 72 oplot = null;
56 73 
57 74 
58 75 //保存并且关闭当前文档
59 76 object obj = Application.GetSystemVariable("DBMOD");
60 77 Document acDoc = Application.DocumentManager.MdiActiveDocument;
61 78 // Check the value of DBMOD, if 0 then the drawing has no unsaved changes
62 79 if (System.Convert.ToInt16(obj) != 0)
63 80 {
64 81 acDoc.Database.SaveAs(acDoc.Name, true, DwgVersion.Current,
65 82 acDoc.Database.SecurityParameters);
66 83 acDoc.CloseAndSave(acDoc.Name);
67 84 }
68 85 return;
69 86 } 
70 87 }
71 88 }
72 89 catch (System.Exception)
73 90 {
74 91 activeDoc .Editor.WriteMessage("\n程序出现硬性错误,请关闭后重新打开!");
75 92 } 
76 93 }

 

作者:AndyTGYan

签名:不忘初心,奋力前行,总有成功时。

声明:转载请标明出处,非常感谢。

posted @ 2019-11-01 10:11  颜廷国  阅读(1832)  评论(0编辑  收藏  举报