using System;
using System.Timers;
using System.Diagnostics;
using System.Collections.Generic;
using System.Windows.Forms;
using acApp = Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Publishing;
using Autodesk.AutoCAD.PlottingServices;
namespace Acad_Startup
{
public class startup : IExtensionApplication
{
public void Initialize()
{
PublishEvents m_pe
= new PublishEvents
(); }
public void Terminate()
{
throw new NotImplementedException
(); }
}
public class PublishEvents
{
public PublishEvents()
{
m_bDone = false;
m_plotStatus = true;
m_plotContinue = false;
m_resend = false;
m_plotMan
= new PlotReactorManager
(); Execute();
}
public static Publisher m_pubMan = acApp.Application.Publisher;
public static PlotReactorManager m_plotMan;
private bool m_bDone;
private bool m_plotStatus;
private bool m_plotContinue;
private bool m_resend;
private DsdData m_DsdData = null;
private PlotConfig m_PlotConfig = null;
private static System.Timers.Timer mTimer;
public void Execute()
{
if (m_bDone == false)
{
m_bDone = true;
}
try
{
m_pubMan
.AboutToBeginPublishing += new AboutToBeginPublishingEventHandler
(callback_AboutToBeginPublish
); m_plotMan
.BeginPlot += new BeginPlotEventHandler
(callback_BeginPlot
); m_plotMan
.PlotCancelled += new PlotCancelledEventHandler
(callback_CancelledPlot
); }
catch (System.Exception)
{
throw;
}
}
void callback_CancelledPlot(object sender, EventArgs e)
{
if (m_resend == true)
{
if (m_DsdData != null && m_PlotConfig != null)
{
resendPublish(m_DsdData, m_PlotConfig);
}
else
{
MessageBox.Show("null");
}
}
m_plotContinue = false;
}
void callback_BeginPlot(object sender, BeginPlotEventArgs e)
{
if (m_plotStatus == true) //plot continues
{
return;
}
else if (m_plotStatus == false) //plot is cancelled
{
m_PlotConfig = PlotConfigManager.CurrentConfig;
if (m_resend == true)
{
//cancel publish if Cancel or No is selected from AboutToBeginPublish dialog
PlotProgress m_PlotProgress = e.PlotProgress;
m_PlotProgress.PlotCancelStatus = PlotCancelStatus.CanceledByCancelAllButton;
m_plotStatus = true;
}
}
}
void callback_AboutToBeginPublish(object sender, AboutToBeginPublishingEventArgs e)
{
if (m_plotContinue == false) //Test to see if publish collate needs to be checked. Needed if coming from resend command
{
m_DsdData = e.DsdData.Copy();
string sPubCol = "";
int nPubCol = System.Convert.ToInt32(acApp.Application.GetSystemVariable("PUBLISHCOLLATE"));
if (nPubCol == 1)
{
sPubCol = "This job will be published as an individual set, is this correct?";
}
else if (nPubCol == 0)
{
sPubCol = "This job will be published as seperate sheets, is this correct?";
}
DialogResult frm = MessageBox.Show(sPubCol, "Publish Collate check", MessageBoxButtons.YesNoCancel);
if (frm == DialogResult.Yes)
{
m_plotStatus = true; // set flag to true which will continue plot once print is triggered
return;
}
else if (frm == DialogResult.No)
{
m_plotStatus = false; // set flag to false which will cancel plot once print is triggered
m_resend = true;
if (nPubCol == 1)
{
acApp.Application.SetSystemVariable("PUBLISHCOLLATE", 0);
}
else if (nPubCol == 0)
{
acApp.Application.SetSystemVariable("PUBLISHCOLLATE", 1);
}
}
else if (frm == DialogResult.Cancel)
{
m_plotStatus = false; // set flag to false which will cancel plot once print is triggered
}
}
else
{
MessageBox.Show("made it");
}
}
void resendPublish(DsdData m_DsdData, PlotConfig m_PlotConfig)
{
m_plotContinue = true;
m_resend = false;
if (System.IO.File.Exists("c:\\AutoCAD\\temp.dsd"))
{
System.IO.File.Delete("c:\\AutoCAD\\temp.dsd");
}
m_DsdData.WriteDsd("c:\\AutoCAD\\temp.dsd");
if (System.IO.File.Exists("c:\\AutoCAD\\temp.pc3"))
{
System.IO.File.Delete("c:\\AutoCAD\\temp.pc3");
}
m_PlotConfig.SaveToPC3("c:\\AutoCAD\\temp.pc3");
Print();
}
void Print()
{
mTimer
= new System.Timers.Timer(2000); mTimer.Elapsed += OnTimer;
mTimer.Enabled = true;
}
void OnTimer(object sender, ElapsedEventArgs e)
{
mTimer.Elapsed -= OnTimer;
mTimer.Enabled = false;
resend();
}
void resend()
{
try
{
using (DsdData dsdDataFile
= new DsdData
()) {
dsdDataFile.ReadDsd("c:\\AutoCAD\\temp.dsd");
int nbSheets = dsdDataFile.NoOfCopies;
using (PlotProgressDialog progressDlg
= new PlotProgressDialog
(false, nbSheets,
true)) {
progressDlg.set_PlotMsgString(PlotMessageIndex.DialogTitle, "Plot API Progress");
progressDlg.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "Cancel Job");
progressDlg.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "Cancel Sheet");
progressDlg.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Job Progress");
progressDlg.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Progress");
progressDlg.UpperPlotProgressRange = 100;
progressDlg.LowerPlotProgressRange = 0;
progressDlg.UpperSheetProgressRange = 100;
progressDlg.LowerSheetProgressRange = 0;
progressDlg.IsVisible = true;
Autodesk.AutoCAD.Publishing.Publisher publisher = acApp.Application.Publisher;
Autodesk.AutoCAD.PlottingServices.PlotConfigManager.SetCurrentConfig("c:\\AutoCAD\\temp.pc3");
try
{
//publisher.PublishExecute(dsdDataFile, Autodesk.AutoCAD.PlottingServices.PlotConfigManager.CurrentConfig);
publisher.PublishDsd("c:\\AutoCAD\\temp.dsd", progressDlg);
}
catch (System.Exception e)
{
string excep = "";
excep += e.Source.ToString();
excep += "\n\n";
excep += e.Message.ToString();
MessageBox.Show(excep + "\n" + e.ToString());
}
}
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}