A quick one to end the week, as I really need to start packing for AU. :-)
Thanks to Augusto Gonçalves, from DevTech Americas, for pointing out this DevNote on the ADN site in a recent email to an ADN member.
The below code shows the steps to set the current visual style to “realistic” in AutoCAD. As with many AutoCAD features, you can also set the current visual style by sending commands to the command-line, but then why do something in 3 lines of code when you can do it in 40? ;-) Seriously, there are some advantages to this approach – such as not polluting the command history, for instance – but that’s really a decision for individual developers to make.
Here’s the C# code defining the SVS command:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
namespace VisualStyles
{
public class Commands
{
[CommandMethod("SVS")]
static public void SetRealisticVisualStyle()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
Transaction tr =
db.TransactionManager.StartTransaction();
using (tr)
{
ViewportTable vt =
(ViewportTable)tr.GetObject(
db.ViewportTableId, OpenMode.ForRead
);
ViewportTableRecord vtr =
(ViewportTableRecord)tr.GetObject(
vt["*Active"], OpenMode.ForWrite
);
DBDictionary dict =
(DBDictionary)tr.GetObject(
db.VisualStyleDictionaryId, OpenMode.ForRead
);
vtr.VisualStyleId = dict.GetAt("Realistic");
tr.Commit();
}
ed.UpdateTiledViewportsFromDatabase();
}
}
}
Here’s a simple indication of the SVS command working. The Visual Styles ribbon panel before…
… and after execution of the SVS command:
That’s it, for now: I’ll hopefully post a few updates next week from AU 2011.