IJCAD2019上で.NETのプリンター出力後にIJCADを終了するとエラーになる。
IJCAD2019上で.NETを使用してプリンター出力しIJCADを終了するとエラーが出力される。
PlotReactorManagerを使用しない場合には問題なく終了する。
プリンター出力をしないでPlotReactorManagerのイベントの登録と解除だけの場合も問題ない。
Visual Studioのデバック上で動作させている場合にはエラーとならない。
エラーが出ないようにする方法はないでしょうか?
namespace PlotTest
{
public partial class Plotting : Form
{
public int PlotReactorErrChk;
public PlotReactorManager plotReactorMng = null;
public string PlotTestFileName = @"C:\temp\test.dwg";// 印刷ファイル
public Plotting()
{
InitializeComponent();
plotReactorMng = new PlotReactorManager();
PlotEventsRegister();// イベントの登録
// プリンターデバイス群をコンボボックスに代入
GcadDocument Gdoc = (GcadDocument)GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.AcadDocument;
var styleNames = Gdoc.ActiveLayout.GetPlotDeviceNames();
foreach( string PlotDevice in styleNames)
{
PlotListBox.Items.Add(PlotDevice);
}
}
private void Plot_Click(object sender, EventArgs e)
{
try
{
using (Document Doc = GrxCAD.ApplicationServices.Application.DocumentManager.Open(PlotTestFileName, false))
{
using (var db = Doc.Database)
{
using (var tr = db.TransactionManager.StartTransaction())
{
LayoutManager acLayoutMgr = LayoutManager.Current;
var plotSettings = db.PlotSettingsDictionaryId.GetObject(OpenMode.ForWrite) as DBDictionary;
DBDictionary lays = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite) as DBDictionary;
Layout layout = null;
foreach (DBDictionaryEntry item in lays)
{
if (item.Key.ToUpper() == "MODEL")
{
ObjectId LayObjId = item.Value;
layout = tr.GetObject(LayObjId, OpenMode.ForWrite) as Layout;
break;
}
}
if (layout == null)
{
MessageBox.Show("印刷するLayoutの取得に失敗!");
Doc.CloseAndDiscard();
return;
}
acLayoutMgr.CurrentLayout = layout.LayoutName;
PlotSettings plSet = GetPlotSettings(db, tr, plotSettings, layout);
var plSetvdr = PlotSettingsValidator.Current;
double DwgSc = db.Dimscale;
if (DwgSc == 0)
{
DwgSc = 1;
}
plSetvdr.RefreshLists(plSet);
plSetvdr.SetPlotConfigurationName(plSet, PlotListBox.SelectedItem.ToString(), "A4");
plSetvdr.SetPlotCentered(plSet, true);
plSetvdr.SetPlotType(plSet, GrxCAD.DatabaseServices.PlotType.Limits);
plSetvdr.SetPlotPaperUnits(plSet, PlotPaperUnit.Millimeters);
plSetvdr.SetCurrentStyleSheet(plSet, "縮小用.ctb");
plSet.PrintLineweights = true;
plSet.PlotPlotStyles = true;
Point2d PaperSz = plSet.PlotPaperSize;
double PaperWidth = PaperSz.X;
double PaperHeight = PaperSz.Y;
if (PaperWidth > PaperHeight)
{
plSetvdr.SetPlotRotation(plSet, PlotRotation.Degrees000);
}
else
{
plSetvdr.SetPlotRotation(plSet, PlotRotation.Degrees270);
}
plSetvdr.SetUseStandardScale(layout, true);
plSetvdr.SetStdScaleType(plSet, StdScaleType.ScaleToFit);
layout.CopyFrom(plSet);
// 印刷
using (PlotInfo icPlInfo = new PlotInfo())
{
icPlInfo.Layout = layout.ObjectId;
icPlInfo.OverrideSettings = layout;
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, db.OriginalFileName, null, 1, false, "");
icPlProgDlg.set_PlotMsgString(PlotMessageIndex.Status, "Plotting: " + db.OriginalFileName + " - " + layout.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);
}
}
}
}
if (PlotReactorErrChk == 2)
{
MessageBox.Show(Doc.Name + "の印刷に成功!");
}
else
{
MessageBox.Show(Doc.Name + "の印刷に失敗!");
}
tr.Commit();
}
}
Doc.CloseAndDiscard();
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
static public PlotSettings GetPlotSettings(Database idb, Transaction itr, DBDictionary iDic, Layout iLay)
{
PlotSettings plSet = default(PlotSettings);
string strName = "PlotTest";
if (iDic.Contains(strName))
{
plSet = iDic.GetAt(strName).GetObject(OpenMode.ForWrite) as PlotSettings;
}
else
{
plSet = new PlotSettings(iLay.ModelType);
plSet.CopyFrom(iLay);
plSet.PlotSettingsName = strName;
plSet.AddToPlotSettingsDictionary(idb);
itr.AddNewlyCreatedDBObject(plSet, true);
}
return plSet;
}
public void PlotEventsRegister()
{
if (plotReactorMng != null)
{
plotReactorMng.BeginPlot += new BeginPlotEventHandler(PlotReactorMng_BeginPlot);
plotReactorMng.EndPlot += new EndPlotEventHandler(PlotReactorMng_EndPlot);
plotReactorMng.BeginDocument += new BeginDocumentEventHandler(PlotReactorMng_BeginDocument);
plotReactorMng.EndDocument += new EndDocumentEventHandler(PlotReactorMng_EndDocument);
plotReactorMng.BeginPage += new BeginPageEventHandler(PlotReactorMng_BeginPage);
plotReactorMng.EndPage += new EndPageEventHandler(PlotReactorMng_EndPage);
plotReactorMng.PlotCancelled += new PlotCancelledEventHandler(PlotReactorMng_PlotCancelled);
plotReactorMng.PageCancelled += new PageCancelledEventHandler(PlotReactorMng_PageCancelled);
}
}
public void PlotEventsUnRegister()
{
if (plotReactorMng != null)
{
plotReactorMng.BeginPlot -= new BeginPlotEventHandler(PlotReactorMng_BeginPlot);
plotReactorMng.EndPlot -= new EndPlotEventHandler(PlotReactorMng_EndPlot);
plotReactorMng.BeginDocument -= new BeginDocumentEventHandler(PlotReactorMng_BeginDocument);
plotReactorMng.EndDocument -= new EndDocumentEventHandler(PlotReactorMng_EndDocument);
plotReactorMng.BeginPage -= new BeginPageEventHandler(PlotReactorMng_BeginPage);
plotReactorMng.EndPage -= new EndPageEventHandler(PlotReactorMng_EndPage);
plotReactorMng.PlotCancelled -= new PlotCancelledEventHandler(PlotReactorMng_PlotCancelled);
plotReactorMng.PageCancelled -= new PageCancelledEventHandler(PlotReactorMng_PageCancelled);
}
}
void PlotReactorMng_BeginPlot(object sender, BeginPlotEventArgs e)
{
}
void PlotReactorMng_EndPlot(object sender, EndPlotEventArgs e)
{
}
void PlotReactorMng_PlotCancelled(object sender, EventArgs e)
{
}
void PlotReactorMng_PageCancelled(object sender, EventArgs e)
{
}
void PlotReactorMng_BeginDocument(object sender, BeginDocumentEventArgs e)
{
PlotReactorErrChk = 0;
}
void PlotReactorMng_BeginPage(object sender, BeginPageEventArgs e)
{
PlotReactorErrChk++;
}
void PlotReactorMng_EndPage(object sender, EndPageEventArgs e)
{
PlotReactorErrChk++;
}
void PlotReactorMng_EndDocument(object sender, EndDocumentEventArgs e)
{
}
private void EndButton_Click(object sender, EventArgs e)
{
PlotEventsUnRegister();// イベントの解除
this.Close();
}
}
}