TheSwamp
Code Red => .NET => Topic started by: vince1327 on November 09, 2011, 01:26:05 PM
-
Hey everyone,
I'm trying to write a plug-in for autocad that simplifies the lives of our designers here at work. I'm new to the Autocad API but have a fair background in C and C++ and have just recently jumped into C#. I've got a windows form that prompts the user to input some settings relating to setting up a new drawing, however where i'm stuck right now is setting the sizes of the layouts. I've designed a command (with some help) that allows the user to specify how many layouts they want created which goes a little like this:
namespace CHPaperSpace
{
public class PaperSpace
{
[Autodesk.AutoCAD.Runtime.CommandMethod("CHPAPERSPACE")]
public void CHPAPERSPACECOMMAND(int x)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
int num = x;
CreateLayouts(num);
}
private void CreateLayouts(int numLayouts)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (DocumentLock acLckDoc = doc.LockDocument())
using (Transaction trx = db.TransactionManager.StartTransaction())// Start Transaction
{
LayoutManager lm = LayoutManager.Current;
for (int i = 3; i <= numLayouts; i++)
{
Layout layout = (Layout)trx.GetObject(lm.CreateLayout("Layout" + i.ToString()), OpenMode.ForWrite);
layout.Initialize();
}
trx.Commit();
ed.Regen();
}
}
}
}
What i'm now trying to do is allow the user to specify the dimensions of the layouts that will be created. One dimension is good for all the layouts, so if they choose to create 7 layouts and choose their dimensions to be 24"x32", then all 7 will take that size. Honestly though, i'm not even sure where to begin. I've checked the developer guide but i'm still stuck. Any advice or assistance would be greatly appreciated.
Many thanks
-
In a normal cad session how would you set this layout size?
-
I believe that you right click on the layout tab and use Page Manager to modify the settings (i.e. dimensions)
-
If you search for "layout" in the Developers Guide you may find something you can use,
http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html?url=WS1a9193826455f5ff2566ffd511ff6f8c7ca-4875.htm,topicNumber=d0e51 (http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html?url=WS1a9193826455f5ff2566ffd511ff6f8c7ca-4875.htm,topicNumber=d0e51)
I found this:
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.PlottingServices;
[CommandMethod("ChangePlotSetting")]
public static void ChangePlotSetting()
{
// Get the current document and database, and start a transaction
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Reference the Layout Manager
LayoutManager acLayoutMgr;
acLayoutMgr = LayoutManager.Current;
// Get the current layout and output its name in the Command Line window
Layout acLayout;
acLayout = acTrans.GetObject(acLayoutMgr.GetLayoutId(acLayoutMgr.CurrentLayout),
OpenMode.ForRead) as Layout;
// Output the name of the current layout and its device
acDoc.Editor.WriteMessage("\nCurrent layout: " +
acLayout.LayoutName);
acDoc.Editor.WriteMessage("\nCurrent device name: " +
acLayout.PlotConfigurationName);
// Get the PlotInfo from the layout
PlotInfo acPlInfo = new PlotInfo();
acPlInfo.Layout = acLayout.ObjectId;
// Get a copy of the PlotSettings from the layout
PlotSettings acPlSet = new PlotSettings(acLayout.ModelType);
acPlSet.CopyFrom(acLayout);
// Update the PlotConfigurationName property of the PlotSettings object
PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;
acPlSetVdr.SetPlotConfigurationName(acPlSet, "DWF6 ePlot.pc3",
"ANSI_A_(8.50_x_11.00_Inches)");
// Update the layout
acLayout.UpgradeOpen();
acLayout.CopyFrom(acPlSet);
// Output the name of the new device assigned to the layout
acDoc.Editor.WriteMessage("\nNew device name: " +
acLayout.PlotConfigurationName);
// Save the new objects to the database
acTrans.Commit();
}
}
-
That's excellent thank, i must have missed that completely in the guide. I'll give it a go, thanks!
-
I've tried this code out with a few modifications but can't seem to change the Paper Size. I'm still new to developing for autocad and this is confusing me to no end. Shouldn't there be a parameter when defining a new layout that will allow me to change the default paper size from ANSI_A_(11.00_x_8.50_Inches) to ANSI_D_(22.00_x_34.00_Inches) for example?
Cheers
-
I think like either in SetPlotConfigurationName like in RMS post
or
PlotSettingsValidator.SetCanonicalMediaName
-
the papersize isn't necessarily consistant from 1 printer to another and can vary with the rotation, eg your papersize when printed upside down could be User1639. Best to set the plotter manually and print those settings
-
Thanks for the input, can anyone help me figure out the actual syntax as to how to set the sizing, i can't seem to find much in the developers guide and experimentation seems to end in an awful lot of fatal errors. Thanks again!
-
Made this one a while ago, see if helps...
// overload no scale factor or set the view - just make the layout
public void LayoutAndViewport ( Database db, string layoutName, out ObjectId rvpid, string deviceName, string mediaName, out ObjectId id )
{
// set default values
rvpid = new ObjectId();
bool flagVp = false; // flag to create a new floating view port
double viewSize = (double)AcadApp.GetSystemVariable("VIEWSIZE");
double height = viewSize;
double width = viewSize;
Point2d loCenter = new Point2d(); // layout center point
Point2d vpLowerCorner = new Point2d();
Point2d vpUpperCorner = new Point2d();
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
LayoutManager lm = LayoutManager.Current;
id = lm.CreateLayout(layoutName);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Layout lo = tr.GetObject(id, OpenMode.ForWrite, false) as Layout;
if (lo != null)
{
lm.CurrentLayout = lo.LayoutName; // make it current!
#region do some plotting settings here for the paper size...
ObjectId loid = lm.GetLayoutId(lo.LayoutName);
PlotInfo pi = new PlotInfo();
pi.Layout = loid;
PlotSettings ps = new PlotSettings(false);
PlotSettingsValidator psv = PlotSettingsValidator.Current;
psv.RefreshLists(ps);
psv.SetPlotConfigurationName(ps, deviceName, mediaName);
psv.SetPlotType(ps, Autodesk.AutoCAD.DatabaseServices.PlotType.Layout);
psv.SetPlotPaperUnits(ps, PlotPaperUnit.Inches);
psv.SetUseStandardScale(ps, true);
psv.SetStdScaleType(ps, StdScaleType.ScaleToFit); // use this as default
pi.OverrideSettings = ps;
PlotInfoValidator piv = new PlotInfoValidator();
piv.Validate(pi);
lo.CopyFrom(ps);
PlotConfig pc = PlotConfigManager.CurrentConfig;
// returns data in millimeters...
MediaBounds mb = pc.GetMediaBounds(mediaName);
Point2d p1 = mb.LowerLeftPrintableArea;
Point2d p3 = mb.UpperRightPrintableArea;
Point2d p2 = new Point2d(p3.X, p1.Y);
Point2d p4 = new Point2d(p1.X, p3.Y);
// convert millimeters to inches
double mm2inch = 25.4;
height = p1.GetDistanceTo(p4) / mm2inch;
width = p1.GetDistanceTo(p2) / mm2inch;
vpLowerCorner = lo.PlotOrigin;
vpUpperCorner = new Point2d(vpLowerCorner.X + width, vpLowerCorner.Y + height);
LineSegment2d seg = new LineSegment2d(vpLowerCorner, vpUpperCorner);
loCenter = seg.MidPoint;
#endregion
if (lo.GetViewports().Count == 1) // Viewport was not created by default
{
// the create by default view ports on new layouts it
// is off we need to mark a flag to generate a new one
// in another transaction - out of this one
flagVp = true;
}
else if (lo.GetViewports().Count == 2) // create Viewports by default it is on
{
// extract the last item from the collection
// of view ports inside of the layout
int i = lo.GetViewports().Count - 1;
ObjectId vpId = lo.GetViewports()[i];
if (!vpId.IsNull)
{
Viewport vp = tr.GetObject(vpId, OpenMode.ForWrite, false) as Viewport;
if (vp != null)
{
vp.Height = height; // change height
vp.Width = width; // change width
vp.CenterPoint = new Point3d(loCenter.X, loCenter.Y, 0.0); // change center
//vp.ColorIndex = 1; // debug
// zoom to the Viewport extents
Zoom(new Point3d(vpLowerCorner.X, vpLowerCorner.Y, 0.0),
new Point3d(vpUpperCorner.X, vpUpperCorner.Y, 0.0), new Point3d(), 1.0);
rvpid = vp.ObjectId; // return the output ObjectId to out...
}
}
}
}
tr.Commit();
} // end of transaction
// we need another transaction to create a new paper space floating Viewport
if (flagVp)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr_ps = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForWrite);
Viewport vp = new Viewport();
vp.Height = height; // set the height
vp.Width = width; // set the width
vp.CenterPoint = new Point3d(loCenter.X, loCenter.Y, 0.0); // set the center
//vp.ColorIndex = 2; // debug
btr_ps.AppendEntity(vp);
tr.AddNewlyCreatedDBObject(vp, true);
vp.On = true; // make it accessible!
// zoom to the Viewport extents
Zoom(new Point3d(vpLowerCorner.X, vpLowerCorner.Y, 0.0),
new Point3d(vpUpperCorner.X, vpUpperCorner.Y, 0.0), new Point3d(), 1.0);
rvpid = vp.ObjectId; // return the ObjectId to the out...
tr.Commit();
} // end of transaction
}
}