We need to iterate through the viewports of the current database from time to time. Here is some succinct and good code in C# to do so.
[CommandMethod("ViewportIterator")]
public static void ViewportIterator_Method()
{
Database database = HostApplicationServices.WorkingDatabase;
using (Transaction transaction = database.TransactionManager.StartTransaction())
{
SymbolTable symTable = (SymbolTable)transaction.GetObject(database.ViewportTableId, OpenMode.ForRead);
foreach (ObjectId id in symTable)
{
ViewportTableRecord symbol = (ViewportTableRecord)transaction.GetObject(id, OpenMode.ForRead);
//TODO: Access to the symbol
MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\nName: {0}", symbol.Name));
}
transaction.Commit();
}
}
If the command is run, something like the following may be printed out to the command line window.
Command: ViewportIterator
Name: *Active
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides various wizards, coders, and widgets to help program AutoCAD addins in C#, VB.NET or even C++/CLI.
Hi Spider,
What is the difference between the ViewportTableRecord class and the Viewport class? In other words, if I want to iterate through the viewports, shouldn't I be iterating through Viewports rather than ViewportTableRecords?
chrs, Ben
Posted by: BKSpurgeon | 08/29/2019 at 04:24 AM