在三维CAD设计软件中,二维图转PDF一般是一个软件的内置功能,所以这些软件一般都会直接提供一个API,来实现转PDF的功能。如PROE的TOOLKIT库,NX的NXOpen库(注意不是C库,而是C++库。NX的C库中似乎是没有转PDF的功能的)。但是AutoCAD这个大名鼎鼎,领导世界二维图设计标准的软件,却没有内置的一个转PDF功能。
早期版本的AutoCAD本身是完全不支持PDF转换的,那时候若要实现DWG图纸转PDF,要么使用第三方的库OpenDWG(早期名叫DWG DirectX)转PDF,要么就使用Adobe Acrobat打印机将图纸打印成PDF。后来AutoCAD终于内置了……一个将图纸打印成PDF的打印机。OpenDWG库转PDF以及Acrobat打印机打印PDF我都没研究过,我主要用的是AutoCAD自带的DWG TO PDF打印机。
使用DWG TO PDF打印机打印图纸,大致有两种方式:基于AutoCAD COM库的调用(C++中使用ObjectARX来做这个事情我没研究过)和基于AutoCAD .NET库的调用。
COM库:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455///
/// 通过自定义打印参数获取ACAD内部打印机对象参数
///
/// 图纸对象
/// 自定义打印参数
///
内置打印机参数 public
static
AcadPlotConfiguration getPlotConfiguration(AcadDocument document, PlotSetting plotSetting)
{
PageSize pageSize = plotSetting.PageSize;
AcadPlotConfiguration plotConfiguration = document.PlotConfigurations.Add(Guid.NewGuid().ToString(
"N"
), document.ActiveLayout.ModelType);
plotConfiguration.ConfigName = plotSetting.Printer;
//打印机名
plotConfiguration.StyleSheet = plotSetting.StyleSheet;
//样式表名
plotConfiguration.PlotWithLineweights =
true
;
//线宽比例
plotConfiguration.PlotWithPlotStyles =
true
;
//使用样式
plotConfiguration.CanonicalMediaName = pageSize.Name;
//图纸尺寸
double
[] LowerLeft = document.Utility.TranslateCoordinates(
new
double
[] { pageSize.LowerLeftX, pageSize.LowerLeftY,
0
}, AcCoordinateSystem.acWorld, AcCoordinateSystem.acDisplayDCS,
0
, Type.Missing) as
double
[];
double
[] UpperRight = document.Utility.TranslateCoordinates(
new
double
[] { pageSize.UpperRightX, pageSize.UpperRightY,
0
}, AcCoordinateSystem.acWorld, AcCoordinateSystem.acDisplayDCS,
0
, Type.Missing) as
double
[];
plotConfiguration.SetWindowToPlot(
new
double
[] { LowerLeft[
0
], LowerLeft[
1
] },
new
double
[] { UpperRight[
0
], UpperRight[
1
] });
plotConfiguration.PlotType = AcPlotType.acWindow;
//打印模式
plotConfiguration.StandardScale = AcPlotScale.acScaleToFit;
//标准比例,需要UseStandardScale = true;
plotConfiguration.UseStandardScale =
true
;
//configuration.UseStandardScale = false;
//configuration.SetCustomScale(1, 30);
plotConfiguration.CenterPlot =
true
;
//居中打印
plotConfiguration.PaperUnits = AcPlotPaperUnits.acMillimeters;
//单位毫米
plotConfiguration.PlotRotation = (pageSize.Rotation ? AcPlotRotation.ac90degrees : AcPlotRotation.ac0degrees);
//横向纵向
//plotConfiguration.PlotHidden = false; //隐藏图元
return
plotConfiguration;
}
///
/// 打印图纸的模型空间到PDF文件
///
/// 图纸对象
/// PDF打印机设置
public
static
void
goPlotting(AcadDocument document, PlotSetting plotSetting)
{
if
(AcActiveSpace.acPaperSpace == document.ActiveSpace)
{
document.MSpace =
true
;
document.ActiveSpace = AcActiveSpace.acModelSpace;
}
document.Regen(AcRegenType.acAllViewports);
object oIsBackground = document.GetVariable(
"BACKGROUNDPLOT"
);
AcadPlotConfiguration plotConfiguration = getPlotConfiguration(document, plotSetting);
AcadLayout layout = document.ActiveLayout;
layout.CopyFrom(plotConfiguration);
layout.RefreshPlotDeviceInfo();
document.SetVariable(
"BACKGROUNDPLOT"
,
0
);
//前台打印
document.Plot.QuietErrorMode =
true
;
document.Plot.NumberOfCopies =
1
;
document.Plot.PlotToFile(plotSetting.TargetFile, plotSetting.Printer);
plotConfiguration.Delete();
document.SetVariable(
"BACKGROUNDPLOT"
, oIsBackground);
}
.net库:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public
static
void
goPlotting(PlotEngine engine, PlotInfo plotInfo, PlotPageInfo pageInfo, string sName, string sFile)
{
PlotInfoValidator validator =
new
PlotInfoValidator();
validator.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
validator.Validate(plotInfo);
engine.BeginPlot(
null
,
null
);
engine.BeginDocument(plotInfo, sName,
null
,
1
,
true
, sFile);
engine.BeginPage(pageInfo, plotInfo,
true
,
null
);
engine.BeginGenerateGraphics(
null
);
engine.EndGenerateGraphics(
null
);
engine.EndPage(
null
);
engine.EndDocument(
null
);
engine.EndPlot(
null
);
}
|