Changing Visual Style

By Balaji Ramamoorthy

Here is a code snippet to change the visual style using the AutoCAD .Net API :

[CommandMethod("ChangeVisualStyle")]
public static void changeVisualStyleMethod()
{
    Document activeDoc =
        Application.DocumentManager.MdiActiveDocument;
    Database db = activeDoc.Database;
    Editor ed = activeDoc.Editor;
    String visualStyleName = "Realistic";
    try
    {
        using(Transaction tr =
           db.TransactionManager.StartTransaction())
        {
            ViewportTable vt = tr.GetObject(
              db.ViewportTableId,
              OpenMode.ForRead)as ViewportTable;
            ViewportTableRecord vtr =
              tr.GetObject(vt["*Active"],
              OpenMode.ForWrite)as ViewportTableRecord;
            DBDictionary dict = tr.GetObject(
                        db.VisualStyleDictionaryId,
                        OpenMode.ForRead) as DBDictionary;
 
            vtr.VisualStyleId = dict.GetAt(visualStyleName);
            tr.Commit();
        }
        ed.UpdateTiledViewportsFromDatabase();
    }
    catch (System.Exception ex)
    {
        ed.WriteMessage(ex.Message);
    }
}

 

Here is the ObjectARX equivalent of the code with minimal error checking

static void AdsProjectChangeVS(void)
{
    Acad::ErrorStatus es = Acad::eOk;
    AcDbDatabase* pDb 
            = acdbHostApplicationServices()->workingDatabase();
 
    AcDbViewportTable* pVportTable = NULL;
    es = pDb->getViewportTable(pVportTable, AcDb::kForRead);
 
    AcDbViewportTableRecord *pVportTableRec = NULL;
    es = pVportTable->getAt(
                                ACRX_T("*Active"), 
                                pVportTableRec, 
                                AcDb::kForWrite
                            ); 
 
    AcDbDictionaryPointer pNOD (
                                    pDb->visualStyleDictionaryId(), 
                                    AcDb::kForRead
                                ) ;
    AcDbObjectId vsId = AcDbObjectId::kNull;
    pNOD->getAt(ACRX_T("Realistic"), vsId);
    pVportTableRec->setVisualStyle(vsId);
    pVportTableRec->close ();
    pVportTable->close();
 
    es = acedVportTableRecords2Vports();
}

Comments

One response to “Changing Visual Style”

  1. hi,
    the ObjectARX example only work on model space, how about the layout?

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading