易捷 > PDF教程 > PDF开发/编程技术 >

轻松利用CSharp将DWG转换成PDF,打印成PDF格式

分享到:
发布时间:2013-05-22 18:00   浏览次数:

因为有人问到,所以写了个例子。具体的要求是从.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; AutoCAD/ObjectDBX Common 18.0 Type Library.看图片更直观:

 

代码中要导入的命名空间:

using System;

using System.Collections;

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;

C#实现代码:

// 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);

}

//Object mediaNames = layout.GetCanonicalMediaNames();

ArrayList mediaNames = new ArrayList((string[])layout.GetCanonicalMediaNames());

foreach (String sName in mediaNames)

{

//activeDoc.Editor.WriteMessage(sName);

if (sName.Contains(MediaName))

{

AcadPlotConfiguration oplot = ThisDrawing.PlotConfigurations.Add("PDF",false);

oplot.PaperUnits=AcPlotPaperUnits.acMillimeters;

oplot.CanonicalMediaName = sName;

oplot.StyleSheet = "monochrome.ctb";

oplot.PlotWithPlotStyles = true;

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

oplot.UseStandardScale = true;

ThisDrawing.SetVariable("BACKGROUNDPLOT", 0);

oplot.RefreshPlotDeviceInfo();

ThisDrawing.Plot.QuietErrorMode = true;

ThisDrawing.Plot.PlotToFile("c:\\temp\\d1.pdf",oplot.ConfigName);

oplot.Delete();

oplot=null;

return;

}

}

}

输出结果:

 

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

下面是我最初用VBA写的代码。也粘贴在下面,也许有对VB或者VB.net感兴趣的人可以借鉴。

<code_begin>

Sub setCanonicalMediaName()

' This example finds the name of the media for the active layout

Dim Layout As AcadLayout

Set Layout = ThisDrawing.ModelSpace.Layout

Dim MediaName As String

MediaName = ThisDrawing.ModelSpace.Layout.CanonicalMediaName

If MediaName = "" Then

Debug.Print "There is no media set for the active layout."

Else

Debug.Print "The media for the active layout is: " & MediaName

End If

Dim mediaNames As Variant

mediaNames = Layout.GetCanonicalMediaNames()

Dim x As Integer

For x = LBound(mediaNames) To UBound(mediaNames)

Debug.Print mediaNames(x)

If InStr(1, mediaNames(x), MediaName, vbTextCompare) <> 0 Then

Dim oplot As AcadPlotConfiguration

Set oplot = ThisDrawing.PlotConfigurations.Add("PDF")

With oplot

.PaperUnits = acMillimeters

.CanonicalMediaName = mediaNames(x)

.StyleSheet = "monochrome.ctb"

.PlotWithPlotStyles = True

.ConfigName = "DWG To PDF.pc3"

.UseStandardScale = True

End With

ThisDrawing.SetVariable "BACKGROUNDPLOT", 0

oplot.RefreshPlotDeviceInfo

ThisDrawing.Plot.QuietErrorMode = True

ThisDrawing.Plot.PlotToFile "c:\temp\d1.pdf", oplot.ConfigName

oplot.Delete

Set oplot = Nothing

Exit For

End If

Next

End Sub

<code_end>

 

相关文章推荐

易捷服务大全

在线PDF转换工具

Word

在线Word转PDF

http://wordtopdf.yjpdf.com
 
Excel

在线Excel转PDF

http://exceltopdf.yjpdf.com
 
PPT

在线PPT转PDF

http://ppttopdf.yjpdf.com

本类最新文章

本类本月热门

大家在关注…