.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Layer State Save & Set Error

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
Dale.Bartlett
584 Views, 3 Replies

Layer State Save & Set Error

All,
I have an application which saves a Layer State ("XXX_SaveState") and re-sets it after processing. The problem is that on some occasions, layers are turned off when resetting. You will see from my comment below:
RestoreLayerState(pstrLayerStateName, ObjectId.Null, 1, ...
 // changed from 0 to 1
 // think this is problem, xref layers think they are new
 // so are restored off
This is why I implemented the Layer State as previously Xref layer settings were not obeying VISRETAIN after processing. Now, except for some isolated Layers turning off, it is ok for Xrefs. I wonder if my set/saveLayerStateAllProps() has something incorrect? Has anyone used Layer States before/after differently? Thanks for reading, Dale

<code>
using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    // save Layer State
                    string LayerStateName = "XXX_SaveState";
                    LayerStateTools.saveLayerStateAllProps(db, tr, LayerStateName);

                    // myProcess - so secret even NSA doesn't have a copy
                    myProcess(db, tr, true, true, true);

                    // restore Layer State
                    LayerStateTools.setLayerStateAllProps(db, tr, LayerStateName);

                    tr.Commit();
               }

// restore layer state - overloaded
        public static void setLayerStateAllProps(string pstrLayerStateName)
        {
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            LayerStateManager acLyrStMan = db.LayerStateManager;

            try
            {
                if (acLyrStMan.HasLayerState(pstrLayerStateName) != false)
                {
                    acLyrStMan.RestoreLayerState(pstrLayerStateName,
                    ObjectId.Null,
                    1,
                        // changed from 0 to 1
                        // think this is problem, xref layers think they are new
                        // so are restored off
                    LayerStateMasks.Color |
                    LayerStateMasks.CurrentViewport |
                    LayerStateMasks.Frozen |
                    LayerStateMasks.LineType |
                    LayerStateMasks.LineWeight |
                    LayerStateMasks.Locked |
                    LayerStateMasks.NewViewport |
                    LayerStateMasks.On |
                    LayerStateMasks.Plot |
                    LayerStateMasks.PlotStyle);
                }
                else
                {
                    ed.WriteMessage(pstrLayerStateName + " Layer State does not exist");
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // restore layer state - overloaded
        public static void setLayerStateAllProps(Database pdb, Transaction ptr, string pstrLayerStateName)
        {
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

            LayerStateManager acLyrStMan = pdb.LayerStateManager;

            try
            {
                if (acLyrStMan.HasLayerState(pstrLayerStateName) != false)
                {
                    acLyrStMan.RestoreLayerState(pstrLayerStateName,
                    ObjectId.Null,
                    1,
                        // changed from 0 to 1
                        // think this is problem, xref layers think they are new
                        // so are restored off
                    LayerStateMasks.Color |
                    LayerStateMasks.CurrentViewport |
                    LayerStateMasks.Frozen |
                    LayerStateMasks.LineType |
                    LayerStateMasks.LineWeight |
                    LayerStateMasks.Locked |
                    LayerStateMasks.NewViewport |
                    LayerStateMasks.On |
                    LayerStateMasks.Plot |
                    LayerStateMasks.PlotStyle);
                }
                else
                {
                    ed.WriteMessage(pstrLayerStateName + " Layer State does not exist");
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
<code>




______________
Yes, I'm Satoshi.
3 REPLIES 3
Message 2 of 4

Self correction, this is the overloaded version used:

<code>
// save layer state  - overloaded
        public static void saveLayerStateAllProps(Database pdb, Transaction ptr, string pstrLayerStateName)
        {
            LayerStateManager acLyrStMan = pdb.LayerStateManager;

            try
            {
                if (acLyrStMan.HasLayerState(pstrLayerStateName) == false)
                {
                    acLyrStMan.SaveLayerState(pstrLayerStateName,
                        LayerStateMasks.Color |
                        LayerStateMasks.CurrentViewport |
                        LayerStateMasks.Frozen |
                        LayerStateMasks.LineType |
                        LayerStateMasks.LineWeight |
                        LayerStateMasks.Locked |
                        LayerStateMasks.NewViewport |
                        LayerStateMasks.On |
                        LayerStateMasks.Plot |
                        LayerStateMasks.PlotStyle
                        , ObjectId.Null);
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
<code>




______________
Yes, I'm Satoshi.
Message 3 of 4
Balaji_Ram
in reply to: Dale.Bartlett

Hi,

 

Sorry for the delay.

 

I do not see anything obviously incorrect in the way the layerstates are being saved and restored.

 

The restore flags that you have set as "1" in your code could be reason why the layers were frozen.

I have attached a screenshot from the documentation on setting the restore flag to 1.

 

But I would expect the behavior to remain consistent in turning off some of the layers.

 

Can please provide a non-confidential drawing and sample project for me to reproduce the problem ?

 

Regards,

Balaji

 

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

Message 4 of 4
Dale.Bartlett
in reply to: Balaji_Ram

I found that if the Layer State existed when create/set the new one, the results were unrealiable. Typically some layers would be turned off. I now check if it exists, if so delete and create new, and then also delete (cleanup) on completion. All testing shows that this has resolved the problem. Dale




______________
Yes, I'm Satoshi.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost