How to thaw one layer in one viewport?

How to thaw one layer in one viewport?

sonny3g
Collaborator Collaborator
846 Views
5 Replies
Message 1 of 6

How to thaw one layer in one viewport?

sonny3g
Collaborator
Collaborator

I have been trying to figure out how to make sure a specific layer is thawed in the current viewport.  My program makes sure a layer is thawed when in the model tab.  But, I cannot seem to get it to work when in a viewport.  I did figure out how to test if my layer is frozen in the viewport but cannot seem to get the layer thawed.  

 

The goal is to take a given layer and create a sibling layer if the sibling layer does not exist.  My code is below:

 

 //***************************************************************************************************************************
        /// <summary>
        /// Takes selected block layer and creates required sibling layer if it does
        /// not exist and makes it the current layer.
        /// </summary>
        /// <param name="blkLyrName"></param>
        /// <returns></returns>
        public string MakeSiblingLayer(string blkLyrName)
        {
            // Intialize the sibling layer namve variable.
            string sibLyrName;
            try
            {
                // Creates new layer name string for sibling layer.
                if (blkLyrName.EndsWith("_CONV"))
                {
                    int cnt = blkLyrName.Length - 5;
                    sibLyrName = blkLyrName.Remove(cnt, 5) + "_DIM_EL";
                }
                else
                {
                    sibLyrName = blkLyrName + "_DIM_EL";
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception a6)
            {
                System.Windows.Forms.MessageBox.Show("ERROR:\n" + a6.ToString());
                return null;
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("ERROR:\n PROGRAM WILL EXIT!");
                return null;
            }
            // Checks for the existence of the sibling layer and creates the new sibling layer if it does not exist.
            using (Transaction mklyrTrans = db.TransactionManager.StartTransaction())
            {
                LayerTable sibLyrTable = mklyrTrans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
                LayerTableRecord sibLTRec = mklyrTrans.GetObject(db.Clayer, OpenMode.ForWrite) as LayerTableRecord;
                try
                {
                    // Thaws the existing sibling layer if it is frozen and it already exists.
                    if (sibLyrTable.Has(sibLyrName) == true)
                    {
                        using (Transaction frznTrans = db.TransactionManager.StartTransaction())
                        {
                            try
                            {
                                LayerTableRecord frznRec;
                                frznRec = frznTrans.GetObject(sibLyrTable[sibLyrName], OpenMode.ForRead) as LayerTableRecord;
                                if (db.TileMode)
                                {
                                    if (frznRec.IsFrozen == true)
                                    {
                                        frznRec.UpgradeOpen();
                                        frznRec.IsFrozen = false;
                                        frznTrans.Commit();
                                    }
                                    else
                                    {
                                        frznTrans.Dispose();
                                    }
                                }
                                else
                                {
                                    Viewport vp = (Viewport)ed.ActiveViewportId.GetObject(OpenMode.ForRead);

                                    if (vp.IsLayerFrozenInViewport(frznRec.Id))
                                    {
                                       frznRec.UpgradeOpen();
                                       vp.;
                                    }
                                }
                            }
                            catch (Autodesk.AutoCAD.Runtime.Exception fl)
                            {
                                System.Windows.Forms.MessageBox.Show("FROZEN LAYER ERROR:\n" + fl.ToString());
                                return null;
                            }
                            catch
                            {
                                System.Windows.Forms.MessageBox.Show("ERROR:\n PROGRAM WILL EXIT!");
                                return null;
                            }
                        }
                        db.Clayer = sibLyrTable[sibLyrName];
                    }// end if sibLyrTable exists make it current layer
                     // Creates the sibling layer if it does not exist already.
                    else
                    {
                        sibLTRec = mklyrTrans.GetObject(sibLyrTable[blkLyrName], OpenMode.ForWrite).Clone() as LayerTableRecord;
                        sibLTRec.Name = sibLyrName;
                        sibLTRec.Color = Color.FromColorIndex(ColorMethod.ByAci, 7);
                        sibLyrTable.Add(sibLTRec);
                        mklyrTrans.AddNewlyCreatedDBObject(sibLTRec, true);
                        db.Clayer = sibLyrTable[sibLyrName];
                    }// end clone layer if sibling layer does not exist and make new sibling layer the current layer
                }
                catch (Autodesk.AutoCAD.Runtime.Exception a7)
                {
                    System.Windows.Forms.MessageBox.Show("ERROR:\n" + a7.ToString());
                    return null;
                }
                catch
                {
                    System.Windows.Forms.MessageBox.Show("ERROR:\n PROGRAM WILL EXIT!");
                    return null;
                }
                // Commits the transaction to add the sibling layer.
                mklyrTrans.Commit();
            }// end the mklyrTrans transaction
            // Returns the new sibling layer name to the calling program.
            return sibLyrName;
        }
        //***************************************************************************************************************************
Scott G. Sawdy
scott.sawdy@bluecoyotecad.com
0 Likes
Accepted solutions (1)
847 Views
5 Replies
Replies (5)
Message 2 of 6

_gile
Consultant
Consultant

You should be able to get some inspiration from this snippet (assuming layers is an IEnumerable of string containing the layer names to freeze in the viewport)

 

 

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
// open the viewport for write Viewport vp = (Viewport)tr.GetObject(vpId, OpenMode.ForWrite);

// get the viewport layer LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead); LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(lt[vp.Layer], OpenMode.ForRead);

// unlock the viewport layer if needed bool locked = ltr.IsLocked; if (locked) { ltr.UpgradeOpen(); ltr.IsLocked = false; }

// freeze the layers in the viewport ObjectIdCollection ids = new ObjectIdCollection(); foreach (string layer in layers) { if (lt.Has(layer)) { ids.Add(lt[layer]); } } vp.FreezeLayersInViewport(ids.GetEnumerator());

// reset the lock state of the viewport layer if (locked) ltr.IsLocked = true; tr.Commit(); }

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 6

sonny3g
Collaborator
Collaborator

To clarify, I cannot thaw a specific layer for which I already have the name and ID inside of a viewport unless I create a objectId collection for that viewport and step through the collection to find the enumerator for the layer?

 

Scott G. Sawdy
scott.sawdy@bluecoyotecad.com
0 Likes
Message 4 of 6

_gile
Consultant
Consultant

This is the way the Viewport.FreezeLayersInViewport() method works.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 6

_gile
Consultant
Consultant
Accepted solution

You can build some extension methods to freeze or thaw a single in a viewport by passing a layer name or ObjectId

 

    public static class Extension
    {
        public static void FreezeLayer(this Viewport vp, string layerName)
        {
            var layerTable = (LayerTable)vp.Database.LayerTableId.GetObject(OpenMode.ForRead);
            vp.FreezeLayer(layerTable[layerName]);
        }

        public static void FreezeLayer(this Viewport vp, ObjectId layerId)
        {
            if (!layerId.IsNull)
            {
                var ids = new ObjectIdCollection();
                ids.Add(layerId);
                vp.FreezeLayersInViewport(ids.GetEnumerator());
            }
        }

        public static void ThawLayer(this Viewport vp, string layerName)
        {
            var layerTable = (LayerTable)vp.Database.LayerTableId.GetObject(OpenMode.ForRead);
            vp.ThawLayer(layerTable[layerName]);
        }

        public static void ThawLayer(this Viewport vp, ObjectId layerId)
        {
            if (!layerId.IsNull)
            {
                var ids = new ObjectIdCollection();
                ids.Add(layerId);
                vp.ThawLayersInViewport(ids.GetEnumerator());
            }
        }
    }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 6

sonny3g
Collaborator
Collaborator

Thank you Giles.  That was exactly what I needed. 

 

My list of subjects to study and learn is getting longer the more I try to write code.

 

I am grateful for forums with people who are willing to share their knowledge with the rest of us.

 

Sonny

Scott G. Sawdy
scott.sawdy@bluecoyotecad.com
0 Likes