public void CreateLayout(Entity rounds, Template tmpl)
{
if (rounds != null)
{
try
{
using (DocumentLock lockdoc = acadApp.DocumentManager.MdiActiveDocument.LockDocument())
{
using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
{
Layout layout = new Layout();
LayoutManager lm = LayoutManager.Current;
PlotSettings ps = new PlotSettings(false);
string layoutName = "LIRPOCAD_" + (lm.LayoutCount + 1);
try
{
layout = (Layout)tr.GetObject(lm.CreateLayout(layoutName), OpenMode.ForWrite);
using (PlotSettings acPlSet = new PlotSettings(layout.ModelType))
{
acPlSet.CopyFrom(layout);
// Update the PlotConfigurationName property of the PlotSettings object
PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;
acPlSetVdr.SetPlotConfigurationName(acPlSet, tmpl.plotter.plotterName, tmpl.canonic.mediaName);
if (rounds.Bounds.Value.MaxPoint.X - rounds.Bounds.Value.MinPoint.X < rounds.Bounds.Value.MaxPoint.Y - rounds.Bounds.Value.MinPoint.Y) {
acPlSetVdr.SetPlotRotation( acPlSet, PlotRotation.Degrees090 );
} else {
acPlSetVdr.SetPlotRotation( acPlSet, PlotRotation.Degrees000 );
}
// acPlSetVdr.SetPlotRotation( acPlSet, PlotRotation.Degrees000 );
// Zoom to show the whole paper
acPlSetVdr.SetZoomToPaperOnUpdate(acPlSet, true);
// Update the layout
layout.UpgradeOpen();
layout.CopyFrom(acPlSet);
}
lm.CurrentLayout = layout.LayoutName;
CreateViewport(layout, rounds, tr, tmpl);
tr.Commit();
this.layout.Add(layout);
}
catch (System.Exception ex)
{
throw new System.Exception(String.Format("Cannot create layout {0}\n{1}", layoutName, ex.Message));
}
}
}
} catch ( Autodesk.AutoCAD.Runtime.Exception e )
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage(e.Message);
}
}
}
static void CreateViewport(Layout layout, Entity borders, Transaction tr, Template tmpl)
{
int vpCount = layout.GetViewports().Count;
if (vpCount == 0)
{
throw new System.Exception(String.Format("Layout {0} не инициализирован", layout.LayoutName));
}
Viewport vp;
if (vpCount == 1)
{
BlockTableRecord lbtr = (BlockTableRecord)tr.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite);
vp = new Viewport();
vp.SetDatabaseDefaults();
lbtr.AppendEntity(vp);
tr.AddNewlyCreatedDBObject(vp, true);
vp.On = true;
}
else
{
ObjectId vpId = layout.GetViewports()[vpCount - 1];
if (vpId.IsNull)
throw new System.Exception("Не удалось получить вьюпорт!");
vp = (Viewport)tr.GetObject(vpId, OpenMode.ForWrite);
if (vp == null)
throw new System.Exception("Не удалось получить вьюпорт!");
}
double h = borders.Bounds.Value.MaxPoint.Y - borders.Bounds.Value.MinPoint.Y;
double w = borders.Bounds.Value.MaxPoint.X - borders.Bounds.Value.MinPoint.X;
double ScaleH = Convert.ToInt32(layout.CanonicalMediaName.Split('(')[1].Split('_')[0].Split('.')[0]);
double ScaleW = Convert.ToInt32(layout.CanonicalMediaName.Split('(')[1].Split('_')[2].Split('.')[0]);
// Высоту и ширину вьюпорта выставляем в размер выделенной области
if ( h > w )
{
vp.Height = h * (ScaleH / h) - 2;
vp.Width = w * (ScaleW / w) - 2;
} else
{
vp.Height = w * (ScaleW / w) - 2;
vp.Width = h * (ScaleH / h) - 2;
}
// h * (297 / h)-2
vp.CenterPoint = new Point3d((vp.Width / 2),
vp.Height / 2,
0);
vp.ViewTarget = new Point3d(borders.Bounds.Value.MinPoint.X, borders.Bounds.Value.MinPoint.Y, 0);
vp.ViewHeight = h;
vp.ViewCenter = new Point2d(w / 2, h / 2);
/* vp.Locked = LayoutsFromModel.Configuration.AppConfig.Instance.LockViewPorts; */
}