.NET API を使用して印刷を行う方法について紹介します。
ただ、現在いくつかの不具合が確認されているため、注意しなければならない点があります。
- 印刷設定に関しては予めレイアウトに保存する必要があります
- BeginPage メソッドや EndPage メソッドを呼ぶと例外が発生する
- BeginGenerateGraphics メソッドや EndGenerateGraphics メソッドで例外がスローされる
下の例では、上記の問題点をクリアしつつ、現在のレイアウトを PDF ファイルに出力します。
VB.NET
-
Public Sub PlotSample() Dim icDb As Database = Application.DocumentManager.MdiActiveDocument.Database Dim icEd As Editor = Application.DocumentManager.MdiActiveDocument.Editor Dim icOpt As PromptSaveFileOptions = New PromptSaveFileOptions(vbLf + "出力先を指定") icOpt.Filter = "PDF(*.pdf)|*.pdf" icOpt.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) Dim icRes As PromptFileNameResult = icEd.GetFileNameForSave(icOpt) If icRes.Status <> PromptStatus.OK Then Return Using icTrans As Transaction = icDb.TransactionManager.StartTransaction() Dim icLayMgr As LayoutManager = LayoutManager.Current Dim icLayout As Layout = icTrans.GetObject(icLayMgr.GetLayoutId(icLayMgr.CurrentLayout), OpenMode.ForWrite) Dim icPlSetVdr = PlotSettingsValidator.Current icPlSetVdr.SetPlotType(icLayout, GrxCAD.DatabaseServices.PlotType.Extents) icPlSetVdr.SetUseStandardScale(icLayout, True) icPlSetVdr.SetStdScaleType(icLayout, StdScaleType.ScaleToFit) icPlSetVdr.SetPlotCentered(icLayout, True) icPlSetVdr.SetPlotConfigurationName(icLayout, "DWG To PDF.pc3", Nothing) For Each mediaName As String In icPlSetVdr.GetCanonicalMediaNameList(icLayout) Dim localName As String = icPlSetVdr.GetLocaleMediaName(icLayout, mediaName) If localName = "ISO A4 (297.00 x 210.00 MM)" Then icPlSetVdr.SetCanonicalMediaName(icLayout, mediaName) Exit For End If Next Using icPlInfo As PlotInfo = New PlotInfo() icPlInfo.Layout = icLayout.ObjectId icPlInfo.OverrideSettings = icLayout Using icPlInfoVdr As PlotInfoValidator = New PlotInfoValidator() icPlInfoVdr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled icPlInfoVdr.Validate(icPlInfo) Using icPlEng As PlotEngine = PlotFactory.CreatePublishEngine() Using icPlProgDlg = New PlotProgressDialog(False, 1, True) icPlProgDlg.PlotMsgString(PlotMessageIndex.DialogTitle) = "Plot Progress" icPlProgDlg.PlotMsgString(PlotMessageIndex.CancelJobButtonMessage) = "Cancel Job" icPlProgDlg.PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage) = "Cancel Sheet" icPlProgDlg.PlotMsgString(PlotMessageIndex.SheetSetProgressCaption) = "Sheet Set Progress" icPlProgDlg.PlotMsgString(PlotMessageIndex.SheetProgressCaption) = "Sheet Progress" icPlProgDlg.LowerPlotProgressRange = 0 icPlProgDlg.UpperPlotProgressRange = 100 icPlProgDlg.PlotProgressPos = 0 icPlProgDlg.OnBeginPlot() icPlProgDlg.IsVisible = True icPlEng.BeginPlot(icPlProgDlg, Nothing) icPlEng.BeginDocument(icPlInfo, icDb.OriginalFileName, Nothing, 1, True, icRes.StringResult) icPlProgDlg.PlotMsgString(PlotMessageIndex.Status) = "Plotting: " + icDb.OriginalFileName + " - " + icLayout.LayoutName icPlProgDlg.OnBeginSheet() icPlProgDlg.LowerSheetProgressRange = 0 icPlProgDlg.UpperSheetProgressRange = 100 icPlProgDlg.SheetProgressPos = 0 icPlProgDlg.SheetProgressPos = 100 icPlProgDlg.OnEndSheet() icPlEng.EndDocument(Nothing) icPlProgDlg.PlotProgressPos = 100 icPlProgDlg.OnEndPlot() icPlEng.EndPlot(Nothing) End Using End Using End Using End Using icTrans.Commit() End Using End Sub
- C#
-
public void PlotSample() { Database icDb = Application.DocumentManager.MdiActiveDocument.Database; Editor icEd = Application.DocumentManager.MdiActiveDocument.Editor; PromptSaveFileOptions icOpt = new PromptSaveFileOptions("\n出力先を指定"); icOpt.Filter = "PDF(*.pdf)|*.pdf"; icOpt.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); PromptFileNameResult icRes = icEd.GetFileNameForSave(icOpt); if (icRes.Status != PromptStatus.OK) return; using (Transaction icTrans = icDb.TransactionManager.StartTransaction()) { LayoutManager icLayMgr = LayoutManager.Current; Layout icLayout = icTrans.GetObject(icLayMgr.GetLayoutId(icLayMgr.CurrentLayout), OpenMode.ForWrite) as Layout; PlotSettingsValidator icPlSetVdr = PlotSettingsValidator.Current; icPlSetVdr.SetPlotType(icLayout, GrxCAD.DatabaseServices.PlotType.Extents); icPlSetVdr.SetUseStandardScale(icLayout, true); icPlSetVdr.SetStdScaleType(icLayout, StdScaleType.ScaleToFit); icPlSetVdr.SetPlotCentered(icLayout, true); icPlSetVdr.SetPlotConfigurationName(icLayout, "DWG To PDF.pc3", null); foreach (string mediaName in icPlSetVdr.GetCanonicalMediaNameList(icLayout)) { string localName = icPlSetVdr.GetLocaleMediaName(icLayout, mediaName); if (localName == "ISO A4 (297.00 x 210.00 MM)") { icPlSetVdr.SetCanonicalMediaName(icLayout, mediaName); break; } } using (PlotInfo icPlInfo = new PlotInfo()) { icPlInfo.Layout = icLayout.ObjectId; icPlInfo.OverrideSettings = icLayout; using (PlotInfoValidator icPlInfoVdr = new PlotInfoValidator()) { icPlInfoVdr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled; icPlInfoVdr.Validate(icPlInfo); using (PlotEngine icPlEng = PlotFactory.CreatePublishEngine()) { using (PlotProgressDialog icPlProgDlg = new PlotProgressDialog(false, 1, true)) { icPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "Cancel Job"); icPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "Cancel Sheet"); icPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Sheet Set Progress"); icPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Progress"); icPlProgDlg.LowerPlotProgressRange = 0; icPlProgDlg.UpperPlotProgressRange = 100; icPlProgDlg.PlotProgressPos = 0; icPlProgDlg.OnBeginPlot(); icPlProgDlg.IsVisible = true; icPlEng.BeginPlot(icPlProgDlg, null); icPlEng.BeginDocument(icPlInfo, icDb.OriginalFileName, null, 1, true, icRes.StringResult); icPlProgDlg.set_PlotMsgString(PlotMessageIndex.Status, "Plotting: " + icDb.OriginalFileName + " - " + icLayout.LayoutName); icPlProgDlg.OnBeginSheet(); icPlProgDlg.LowerSheetProgressRange = 0; icPlProgDlg.UpperSheetProgressRange = 100; icPlProgDlg.SheetProgressPos = 0; icPlProgDlg.SheetProgressPos = 100; icPlProgDlg.OnEndSheet(); icPlEng.EndDocument(null); icPlProgDlg.PlotProgressPos = 100; icPlProgDlg.OnEndPlot(); icPlEng.EndPlot(null); } } } } icTrans.Commit(); } }