Kean Walmsley


  • About the Author
    Kean on Google+

April 2014

Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      







« Coming soon: Kinect for your PC | Main | Adding speech recognition to AutoCAD via Kinect (continued) »

November 25, 2011

Setting the current visual style in AutoCAD using .NET

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…

Default visual style setting

… and after execution of the SVS command:

The updated visual style setting

That’s it, for now: I’ll hopefully post a few updates next week from AU 2011.

blog comments powered by Disqus

10 Random Posts