Setting a Layout Viewport Style

Setting a Layout Viewport Style

odoshi
Collaborator Collaborator
1,184 Views
4 Replies
Message 1 of 5

Setting a Layout Viewport Style

odoshi
Collaborator
Collaborator

Hi,

 

Is it possible to set a viewport's visual style without opening the drawing in the editor? I would like to set a visual style on all viewports for a batch of drawings.

 

As a followup, could the viewport then be part of a publish event and have the new visual style hold without opening and regen'ing the drawing?

 

Thanks,

 

Mike Caruso
Autodesk Certified Instructor 2014
AutoCAD/Civil 3D Autodesk Certified Professional 2014, 2015, 2018
www.whitemountaincad.com
0 Likes
1,185 Views
4 Replies
Replies (4)
Message 2 of 5

odoshi
Collaborator
Collaborator

Here's a snippet of the code. It gives an internal error. Maybe I am not setting the viewport current?

 

Thanks,

 

Mike Caruso
Autodesk Certified Instructor 2014
AutoCAD/Civil 3D Autodesk Certified Professional 2014, 2015, 2018
www.whitemountaincad.com
0 Likes
Message 3 of 5

odoshi
Collaborator
Collaborator

Using SaveAs() allows the code to run fine. But the VisualStyleID is not set. So I guess I am just wondering if it is possible at all?

 

Thanks,

Mike Caruso
Autodesk Certified Instructor 2014
AutoCAD/Civil 3D Autodesk Certified Professional 2014, 2015, 2018
www.whitemountaincad.com
0 Likes
Message 4 of 5

Anonymous
Not applicable

It can be set, here is a snippet where I am setting a viewports VisualStyleId to the matching one for 2dWireFrame:

 

using (var dl = ACApp.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
    var db = ACApp.Application.DocumentManager.MdiActiveDocument.Database;
    using (var destTrans = db.TransactionManager.StartTransaction())
    {
        // now we set the titleblock attributes on the new layout to our given values
        var layoutID = ACDb.LayoutManager.Current.GetLayoutId(layoutName);
        if (null == layoutID || layoutID.Equals(ACDb.ObjectId.Null)) return false;
        var targetLayout = destTrans.GetObject(ACDb.LayoutManager.Current.GetLayoutId(layoutName), ACDb.OpenMode.ForRead) as ACDb.Layout;
        if (targetLayout != null)
        {
            var btr = destTrans.GetObject(targetLayout.BlockTableRecordId, ACDb.OpenMode.ForRead) as ACDb.BlockTableRecord;
            if (btr != null)
            {
                foreach (var id in btr)
                {
                    var vport = id.GetObject(ACDb.OpenMode.ForRead);
                    if (vport is ACDb.Viewport)
                    {
                        var vsdic = destTrans.GetObject(db.VisualStyleDictionaryId, ACDb.OpenMode.ForRead) as ACDb.DBDictionary;
                        if (null == vsdic) continue;
                        ACDb.ObjectId vs = (ACDb.ObjectId)vsdic["2dWireFrame"];
                        vport.UpgradeOpen();
                        ((ACDb.Viewport)vport).VisualStyleId = vs;
                        vport.DowngradeOpen();
                        continue;
                    }

...snipped the rest...

 

0 Likes
Message 5 of 5

jeff
Collaborator
Collaborator

This works for me but will not update unless the layouts hve been activated once.

It seems like i remeber a method that would do that but i can not find it.

 

        [CommandMethod("ChangeVisualStyles")]

        static public void ChangeVisualStyles()
        {
            using (Database db = new Database(false, true))
            {
                   db.ReadDwgFile(@"C:\Test\VisualStyleChange.dwg", FileOpenMode.OpenForReadAndWriteNoShare, true, null);

                    using (Transaction trx = db.TransactionManager.StartTransaction())
                    {
                        DBDictionary visualStlyeDic = (DBDictionary)trx.GetObject(db.VisualStyleDictionaryId, OpenMode.ForRead);
                        ObjectIdCollection viewportIds = db.GetViewports(false);

                        ObjectId visualStyleId = visualStlyeDic.GetAt("Realistic");

                        foreach (ObjectId vpId in viewportIds)
                        {
                            Viewport vp = (Viewport)trx.GetObject(vpId, OpenMode.ForWrite);
                            vp.VisualStyleId = visualStyleId;
                        }
                        trx.Commit();
                    }                    
                    db.SaveAs(db.Filename, true, DwgVersion.Current, db.SecurityParameters);                
            }


        }

 

You can also find your answers @ TheSwamp
0 Likes