Message 1 of 1
Loading and plotting a drawing via .NET
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Similar to the example shown here (https://www.keanw.com/2007/09/driving-a-basic.html), I put together a plugin to load a drawing and then plot it. The line weights, however, don't match my current plot style consistently. If I adjust the code to plot the active document it plots just fine but when I revised the code to load a drawing then plot it is when I noticed it plotting incorrectly. The odd thing is that the test drawing that I was plotting seemed to plot fine with a certain layer in place. If I delete the layer (even though nothing is on that layer) then the error occurs. Below is the code, any suggestions as to a fix would be appreciated.
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.PlottingServices;
namespace SimplePlot
{
public class PlottingCommands
{
[CommandMethod("simplot")]
public static void SimplePlot()
{
Document doc_org = Application.DocumentManager.MdiActiveDocument;
Editor ed_org = doc_org.Editor;
Database db_org = doc_org.Database;
string FileName2 = "\\\\projects\\temp\\test2.dwg";
using (Database db = new Database(false, true))
{
db.ReadDwgFile(FileName2, System.IO.FileShare.Read, false, null);
db.CloseInput(true);
db.UpdateExt(true);
HostApplicationServices.WorkingDatabase = db;
var plSettings = new DBDictionary();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
Layout lo = (Layout)tr.GetObject(btr.LayoutId, OpenMode.ForRead);
PlotInfo pi = new PlotInfo();
pi.Layout = btr.LayoutId;
PlotSettings ps = new PlotSettings(lo.ModelType);
ps.CopyFrom(lo);
PlotSettingsValidator psv = PlotSettingsValidator.Current;
psv.SetPlotType(ps, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
psv.SetUseStandardScale(ps, true);
psv.SetStdScaleType(ps, StdScaleType.ScaleToFit);
psv.SetPlotCentered(ps, true);
psv.SetPlotRotation(ps, PlotRotation.Degrees000);
psv.SetPlotConfigurationName(ps, "PostScript Level 2.pc3", null);
psv.RefreshLists(ps);
//Get the Canonical Media Name
string canonicalMediaName = LocalToCanonicalMediaName("EPS(3.5x3.5)", ps, psv);
//set the canonical media name
psv.SetCanonicalMediaName(ps, canonicalMediaName);
psv.SetCurrentStyleSheet(ps, "MYPRINTER.ctb");
pi.OverrideSettings = ps;
PlotInfoValidator piv = new PlotInfoValidator();
piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
piv.Validate(pi);
if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
{
PlotEngine pe = PlotFactory.CreatePublishEngine();
using (pe)
{
PlotProgressDialog ppd = new PlotProgressDialog(false, 1, true);
using (ppd)
{
ppd.set_PlotMsgString(PlotMessageIndex.DialogTitle, "Custom Plot Progress");
ppd.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "Cancel Job");
ppd.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "Cancel Sheet");
ppd.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Sheet Set Progress");
ppd.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Progress");
ppd.LowerPlotProgressRange = 0;
ppd.UpperPlotProgressRange = 100;
ppd.PlotProgressPos = 0;
ppd.OnBeginPlot();
ppd.IsVisible = true;
pe.BeginPlot(ppd, null);
string PlotPath = "\\\\projects\\temp\\test.eps";
// We'll be plotting a single document
pe.BeginDocument(
pi,
//docnew.Name,
FileName2,
null,
1,
true, // Let's plot to file
PlotPath
);
// Which contains a single sheet
ppd.OnBeginSheet();
ppd.LowerSheetProgressRange = 0;
ppd.UpperSheetProgressRange = 100;
ppd.SheetProgressPos = 0;
PlotPageInfo ppi = new PlotPageInfo();
pe.BeginPage(ppi, pi, true, null);
pe.BeginGenerateGraphics(null);
pe.EndGenerateGraphics(null);
// Finish the sheet
pe.EndPage(null);
ppd.SheetProgressPos = 100;
ppd.OnEndSheet();
// Finish the document
pe.EndDocument(null);
// And finish the plot
ppd.PlotProgressPos = 100;
ppd.OnEndPlot();
pe.EndPlot(null);
}
}
}
else
{
ed_org.WriteMessage(
"\nAnother plot is in progress."
);
}
}
HostApplicationServices.WorkingDatabase = db_org;
}
}
/*
* Get the Canonical Media Name
*/
private static string LocalToCanonicalMediaName(string localMediaName, PlotSettings acPlSet, PlotSettingsValidator acPlSetVdr)
{
int cnt = 0;
foreach (string CanonicalMediaName in acPlSetVdr.GetCanonicalMediaNameList(acPlSet))
{
string localName = acPlSetVdr.GetLocaleMediaName(acPlSet, cnt);
if (string.Equals(localMediaName, localName))
{
return CanonicalMediaName;
}
cnt++;
}
return null;
}
}
}