Summary
I'm trying to write a C# ObjectARX script to create a viewport that fits the extents of the layout using the -VPORTS Fit
command, but for some reason the new viewport shows in AutoCAD but is never saved to the output drawing.
Some things I've tried
- Using
acDoc.SendStringToExecute()
instead ofacDoc.Editor.Command()
- Using
-VPORTS
instead of._VIEWPORTS
- Running the commands before commiting the transaction
- Caused AutoCAD to freeze and crash. I think I have to commit the new layout before I can make the viewport
Code Snippet
[CommandMethod("CreateViewport")]
public static void CreateViewport()
{
// Get the current document and database, and start a transaction
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
try
{
Entity acObj = null;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Omitted code for opening block record ...
// Switch to the previous Paper space layout
Application.SetSystemVariable("TILEMODE", 0);
acDoc.Editor.SwitchToPaperSpace();
// Omitted code for creating a polyline in model space to focus on (acObj) ...
// Omitted code for creating a new layout ...
// Save the new objects to the database
acTrans.Commit();
}
// This is the part that shows in AutoCAD but doesn't save
acDoc.Editor.Command("._VIEWPORTS", "Fit");
acDoc.Editor.Command("._MSPACE");
acDoc.Editor.Command("._ZOOM", "_O", SelectionSet.FromObjectIds(new ObjectId[] { acObj.ObjectId }), "");
acDoc.Editor.Command("._PSPACE");
acDoc.Editor.Command("._ZOOM", "E");
acCurDb.SaveAs("OutputDrawing.dwg", DwgVersion.AC1027);
}
catch (Exception ex)
{
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
}
}