VB   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何用.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.@H_347_33@mdiActiveDocument

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

Dim layout As AcadLayout = ThisDrawing.ActiveLayout

Dim @H_347_33@mediaName As String = layout.CanonicalMediaName

If @H_347_33@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: " + @H_347_33@mediaName))

End If

Try

Dim oplot As AcadPlotConfiguration = ThisDrawing.PlotConfigurations.Add("PDF",layout.@H_347_33@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 @H_347_33@mediaNames As ArrayList = New ArrayList(CType(oMediaNames,String()))

For Each sName As String In @H_347_33@mediaNames

If sName.Contains(@H_347_33@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.@H_347_33@mdiActiveDocument;

AcadDocument ThisDrawing = activeDoc.AcadDocument as AcadDocument;

AcadLayout layout = ThisDrawing.ActiveLayout;

String @H_347_33@mediaName = layout.CanonicalMediaName;

if (@H_347_33@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: " + @H_347_33@mediaName);

}

try

AcadPlotConfiguration oplot = ThisDrawing.PlotConfigurations.Add("PDF",layout.@H_347_33@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 @H_347_33@mediaNames = new ArrayList((String[])oMediaNames);

foreach (String sName in @H_347_33@mediaNames)

if (sName.Contains(@H_347_33@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的打印定制程序可以是这样写的。但是实际需求是各种各样的,要具体分析情况并使用不同的方法。

大佬总结

以上是大佬教程为你收集整理的如何用.NET(CSharp & VB.NET)将DWG文件打印为PDF全部内容,希望文章能够帮你解决如何用.NET(CSharp & VB.NET)将DWG文件打印为PDF所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。