如何用.NET(CSharp & VB.NET)将DWG文件打印为PDF

因为有人问到,所以写了个例子。具体的要求是从.NET(比如C#)里面调用AutoCAD ActiveX API实现后台打印DWG文件为PDF文件,而且要把打印页面的大小设置成和DWG视图的页面的大小一致。当然除了ActiveX API,其它接口,比如ObjectARX和AutoCAD.NET API也支持打印并能实现上述功能的。不过我们今天就限定一下范围,用一用ActiveX API,而且指定产品是AutoCAD 2010吧。

执行步骤:打开一个dwg文件,用netload加载下面代码所在的.dll文件,再输入命令plottest,就得到输出结果(一个.pdf文件)。

要用到的参考:

AcDbMgd.dll;AcMgd.dll;AutoCAD 2010 Type Library;System.Windows.Forms; AutoCAD/objectdbX Common 18.0 Type Library.

VB.NET:

Imports System

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.Interop

Imports Autodesk.AutoCAD.Interop.Common

Imports Autodesk.AutoCAD.applicationservices

Imports Autodesk.AutoCAD.DatabaseServices

Imports Autodesk.AutoCAD.EditorInput

<Autodesk.AutoCAD.Runtime.CommandMethod("Plottest")> _

Public Sub PlottoPDF()

Dim activeDoc As Document = Application.DocumentManager.MdiActiveDocument

Dim ThisDrawing As AcadDocument = CType(activeDoc.AcadDocument,AcadDocument)

Dim layout As AcadLayout = ThisDrawing.ActiveLayout

Dim MediaName As String = layout.CanonicalMediaName

If MediaName.Equals("") Then

activeDoc.Editor.WriteMessage("There is no media set for the active layout.")

Return

Else

activeDoc.Editor.WriteMessage(("The media for the active layout is: " + MediaName))

End If

Try

Dim oplot As AcadplotConfiguration = ThisDrawing.PlotConfigurations.Add("PDF",layout.ModelType)

oplot.PaperUnits = AcPlotPaperUnits.acMillimeters

oplot.StyleSheet = "monochrome.ctb"

oplot.PlotWithPlotStyles = True

oplot.ConfigName = "DWG To PDF.pc3"

oplot.UseStandardScale = True

oplot.StandardScale = AcPlotScale.acScaletoFit

oplot.Plottype = AcPlottype.acExtents

oplot.CenterPlot = True

Dim oMediaNames As Object = layout.GetCanonicalMediaNames

Dim mediaNames As ArrayList = New ArrayList(CType(oMediaNames,String()))

For Each sName As String In mediaNames

If sName.Contains(MediaName) Then

oplot.CanonicalMediaName = sName

layout.copyFrom(oplot)

layout.PlotRotation = AcPlotRotation.ac0degrees

layout.RefreshPlotDeviceInfo()

ThisDrawing.Setvariable("BACKGROUNdplOT",0)

ThisDrawing.Plot.QuietErrorMode = True

ThisDrawing.Plot.PlottoFile("c:/temp/d1.pdf","DWG To PDF.pc3")

oplot.Delete()

oplot = nothing

Next

Catch es As System.Exception

System.Windows.Forms.MessageBox.Show(es.ToString)

End Try

End Sub

C#:

using System;

using System.Collections;

using System.Collections.Specialized;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.applicationservices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Interop;

using Autodesk.AutoCAD.Interop.Common;

// Define Command "plottest"

[CommandMethod("plottest")]

static public void PlottoPDF()

{

Document activeDoc = Application.DocumentManager.MdiActiveDocument;

AcadDocument ThisDrawing = activeDoc.AcadDocument as AcadDocument;

AcadLayout layout = ThisDrawing.ActiveLayout;

String MediaName = layout.CanonicalMediaName;

if (MediaName.Equals(""))

activeDoc.Editor.WriteMessage("There is no media set for the active layout.");

return;

}

else

activeDoc.Editor.WriteMessage("The media for the active layout is: " + MediaName);

}

try

AcadplotConfiguration oplot = ThisDrawing.PlotConfigurations.Add("PDF",layout.ModelType);

oplot.PaperUnits = AcPlotPaperUnits.acMillimeters;

oplot.StyleSheet = "monochrome.ctb";

oplot.PlotWithPlotStyles = true;

oplot.ConfigName = "DWG To PDF.pc3";

oplot.UseStandardScale = true;

oplot.StandardScale = AcPlotScale.acScaletoFit;

oplot.Plottype = AcPlottype.acExtents;

oplot.CenterPlot = true;

Object oMediaNames = layout.GetCanonicalMediaNames();

ArrayList mediaNames = new ArrayList((string[])oMediaNames);

foreach (String sName in mediaNames)

if (sName.Contains(MediaName))

oplot.CanonicalMediaName = sName;

layout.copyFrom(oplot);

layout.PlotRotation = AcPlotRotation.ac0degrees;

layout.RefreshPlotDeviceInfo();

ThisDrawing.Plot.QuietErrorMode = true;

ThisDrawing.Plot.PlottoFile("c://temp//d1.pdf","DWG To PDF.pc3");

oplot.Delete();

oplot=null;

catch (System.Exception es)

System.Windows.Forms.MessageBox.Show(es.ToString());

}

输出结果:

这只是个例子。大家从中可以看到AutoCAD的打印定制程序可以是这样写的。但是实际需求是各种各样的,要具体分析情况并使用不同的方法

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...