Hi Norman,
Sorry for the delayed reaction. You might try the next sample, it can define a custom format when special conditions are met
//Tests the following scenario:
// theLayout is configured: Plot To PDF, sized custom media 7A4 (== w=7X210, h=297)
// when changing to another device, the media 7A4 must be added to that device(pc3), as the "previous media size"
// it appeares to work only when the new device is a PC3 device AND
// it already contains a media sized larger than the "previous media size", like 2500x841
// We can make a PC3 file for a printer, change \\printserver\printername into printername.pc3,
// however you cannot attach a PMP file to the PC3 file.
// The PMP file contains the non standard media size (among other settings) and thus this will work only for
// already defined PC3 devices with PMP files and one custom media size larger than the required media size.
[CommandMethod("Plt")]
public void PlotWithPlotStyle()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
//string psetupPathname = @"C:\Apldata\AutoCAD\AcadConfig_INFRA\Menu\psetup-PDF.dwg";
//string plotStyleName = "4Z-PDF";
string printerName = "TDS600_Beta.pc3";
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
db.TileMode = false;
ed.SwitchToPaperSpace();
LayoutManager layMgr = LayoutManager.Current;
Layout theLayout = (Layout)layMgr.GetLayoutId(layMgr.CurrentLayout).GetObject(OpenMode.ForWrite);
PlotSettingsValidator psVal = Autodesk.AutoCAD.DatabaseServices.PlotSettingsValidator.Current;
PlotSettings plotSet = new PlotSettings(theLayout.ModelType);
plotSet.CopyFrom(theLayout);
//psVal.SetPlotConfigurationName(plotSet, printerName, null);
PlotConfigManager.SetCurrentConfig(printerName);
PlotConfigManager.RefreshList(RefreshCode.All);
PlotConfig tds = PlotConfigManager.CurrentConfig;
string plotFile = null;
if (tds.PlotToFileCapability == PlotToFileCapability.PlotToFileAllowed)
{
tds.IsPlotToFile = true;
plotFile = Path.Combine(Path.GetDirectoryName(Application.DocumentManager.MdiActiveDocument.Database.Filename), Path.GetFileNameWithoutExtension(Application.DocumentManager.MdiActiveDocument.Database.Filename));
plotFile = plotFile + tds.DefaultFileExtension;
if (File.Exists(plotFile))
File.Delete(plotFile);
}
//the next lines will fail because the required PMP file will get referenced by the PC3
//string pc3Dir = @"C:\Documents and Settings\avb\Application Data\Autodesk\AutoCAD 2012 - English\R18.2\enu\Plotters";
//printerName = printerName + ".pc3";
//tds.SaveToPC3(Path.Combine(pc3Dir, printerName ));
//PlotConfigManager.SetCurrentConfig(printerName);
//PlotConfigManager.RefreshList(RefreshCode.All);
//tds = PlotConfigManager.CurrentConfig;
//psVal.RefreshLists(plotSet);
//try
//{
// psVal.SetClosestMediaName(plotSet, plotSet.PlotPaperSize[0], plotSet.PlotPaperSize[1], PlotPaperUnit.Millimeters, true);
//}
//catch ( Autodesk.AutoCAD.Runtime.Exception exx )
//{
// ed.WriteMessage("\n" + exx.ToString());
//}
psVal.SetPlotPaperUnits(plotSet, PlotPaperUnit.Millimeters);
//theLayout.CopyFrom(plotSet);
ed.Regen();
PlotInfo plotInfo = new PlotInfo();
plotInfo.Layout = theLayout.ObjectId;
plotInfo.OverrideSettings = plotSet;
plotInfo.DeviceOverride = tds;
PlotInfoValidator validator = new PlotInfoValidator();
//int itIs = validator.IsCustomPossible(plotInfo);
validator.MediaMatchingPolicy = Autodesk.AutoCAD.PlottingServices.MatchingPolicy.MatchEnabledCustom;
int itIs = validator.IsCustomPossible(plotInfo);
validator.Validate(plotInfo);
//for now see the results
if (plotInfo.IsValidated && itIs == 0)
{
PlotSettings newNamedPlotStyle = new PlotSettings(theLayout.ModelType);
newNamedPlotStyle.CopyFrom(plotInfo.ValidatedSettings);
theLayout.CopyFrom(newNamedPlotStyle);
newNamedPlotStyle.PlotSettingsName = "7A4-TDS600";
psVal.RefreshLists(newNamedPlotStyle);
newNamedPlotStyle.AddToPlotSettingsDictionary(db);
tr.AddNewlyCreatedDBObject(newNamedPlotStyle, true);
psVal.RefreshLists(newNamedPlotStyle);
ed.Regen();
tr.Commit();
return;
}
PlotEngine plotEngine = PlotFactory.CreatePublishEngine();
//None (initial) -> plot -> document -> page -> graphics
try
{
plotEngine.BeginPlot(null, null);
if (tds.IsPlotToFile)
plotEngine.BeginDocument(plotInfo, Application.DocumentManager.MdiActiveDocument.Database.Filename, null, 1, true, plotFile);
else
plotEngine.BeginDocument(plotInfo, Application.DocumentManager.MdiActiveDocument.Database.Filename, null, 1, false, null);
PlotPageInfo pageInfo = new PlotPageInfo();
ed.WriteMessage("\nPlotting {0} Entities, {1} ", pageInfo.EntityCount, pageInfo.RasterCount);
plotEngine.BeginPage(pageInfo, plotInfo, true, null);
plotEngine.BeginGenerateGraphics(null);
plotEngine.EndGenerateGraphics(null);
plotEngine.EndPage(null);
plotEngine.EndDocument(null);
plotEngine.EndPlot(null);
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.Message);
}
plotEngine.Destroy();
tr.Commit();
}
}