//get print info
//contains the target window area
List<FrameAttributeExtend> entitylist = GetFrameEntity(title);
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
Transaction acTrans = acCurDb.TransactionManager.StartTransaction();
object iSys = AcadApplication.GetSystemVariable("BACKGROUNDPLOT");
try
{
using (acTrans)
{
int flag = 1;
//AcadApplication.SetSystemVariable("BACKGROUNDPLOT", 1);
AcadApplication.SetSystemVariable("BACKGROUNDPLOT", 0);
if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
{
PlotProgressDialog ppd
= new PlotProgressDialog
(false, entitylist
.Count,
true); using (ppd)
{
ppd.OnBeginPlot();
PlotEngine pe = PlotFactory.CreatePublishEngine();
pe.BeginPlot(ppd, null);
ppd.StatusMsgString = "Plotting..."; // add name of document, if you need it
ppd.LowerSheetProgressRange = 0;
ppd.UpperSheetProgressRange = 100;
ppd.SheetProgressPos = 0;
ppd.OnBeginSheet();
foreach (var frame in entitylist)
{
//get cur layoutmanager
LayoutManager acLayoutMgr = LayoutManager.Current;
//get current layout
Layout acLayout = (Layout)acTrans.GetObject(acLayoutMgr.GetLayoutId(acLayoutMgr.CurrentLayout), OpenMode.ForRead);
//Get print layout info
PlotInfo pi
= new PlotInfo
() { Layout
= acLayout
.ObjectId }; PlotSettings ps
= new PlotSettings
(acLayout
.ModelType); ps.CopyFrom(acLayout);
PlotSettingsValidator psv = PlotSettingsValidator.Current;
//set the plot style
System.Collections.Specialized.StringCollection sc = psv.GetPlotStyleSheetList();
psv.SetCurrentStyleSheet(ps, "TestStyle.ctb");
psv.SetPlotType(ps, PlotType.Extents);
psv.SetUseStandardScale(ps, true);
psv.SetStdScaleType(ps, StdScaleType.ScaleToFit);
psv.SetPlotCentered(ps, true);
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
//scale
string entityscale = frame.Scale;
//orientation
string vertial = frame.IsVertial;
//index
string picindex = frame.ImageIndex;
string imagename = "";
if (frame.ImageType == 0)
{
imagename = string.Format("1-{0}-{1}-{2}.pdf", entityscale, vertial, picindex);
totalflatimage = directory + imagename;
}
else
{
imagename = string.Format("2-{0}-{1}-{2}.pdf", entityscale, vertial, picindex);
imagelist.Add(directory + imagename);
}
ps.ShowPlotStyles = true;
//set print extends
Extents3d extents3d = frame.Extent;
double h = double.Parse(entityscale) * 6;
double x = extents3d.MinPoint.X + unit * 2;
double y = extents3d.MaxPoint.Y + h + unit;
if (unit == 1)
{
x = extents3d.MinPoint.X + unit * 1;
y = extents3d.MaxPoint.Y + double.Parse(entityscale) / 250 + unit * 2;
}
Extents2d E2d
= new Extents2d
(x, extents3d
.MinPoint.Y, extents3d
.MaxPoint.X, y
);
psv.SetPlotPaperUnits(ps, PlotPaperUnit.Millimeters);
//psv.SetPlotOrigin(ps, new Point2d(extents3d.MinPoint.X, extents3d.MinPoint.Y));
psv.SetPlotWindowArea(ps, E2d);
//psv.SetPlotOrigin(ps, E2d.MinPoint);
psv.SetPlotType(ps, Autodesk.AutoCAD.DatabaseServices.PlotType.Window);
psv.SetPlotConfigurationName(ps, "DWG To PDF.pc3", "ISO_expand_A4_(210.00_x_297.00_mm)");
psv.SetPlotRotation(ps, PlotRotation.Degrees000);
PlotInfoValidator piv
= new PlotInfoValidator
(); piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
pi.OverrideSettings = ps;
piv.Validate(pi);
ppd.IsVisible = true;
if (flag == 1)
{
pe.BeginDocument(pi, acDoc.Name, null, 1, true, directory + imagename);
}
ppd.LowerSheetProgressRange = 0;
ppd.UpperSheetProgressRange = 100;
ppd.SheetProgressPos = 0;
PlotPageInfo ppi
= new PlotPageInfo
(); pe.BeginPage(ppi, pi, flag == entitylist.Count, null);
pe.BeginGenerateGraphics(null);
ppd.SheetProgressPos = 50;
pe.EndGenerateGraphics(null);
pe.EndPage(null);
ppd.SheetProgressPos = 100;
ppd.OnEndSheet();
ppd.PlotProgressPos = (100 / entitylist.Count) * flag;
flag++;
}
pe.EndDocument(null);
ppd.PlotProgressPos = 100;
ppd.OnEndPlot();
pe.EndPlot(null);
pe.Dispose();
}
}
}
//verify the plot state
while (PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
continue;
MessageBox.Show("The Plot Is Complated!");
}
catch (Exception ex)
{
return null;
}
finally
{
AcadApplication.SetSystemVariable("BACKGROUNDPLOT", iSys);
acTrans.Dispose();
}
}