Is LAYISO available via the API?

Is LAYISO available via the API?

jdowthwaite
Enthusiast Enthusiast
1,242 Views
8 Replies
Message 1 of 9

Is LAYISO available via the API?

jdowthwaite
Enthusiast
Enthusiast

Is it possible to enable LAYISO with fading enabled via LISP or .Net?  I know there's a lot of routines out there to mimic the existing ALYISO command to remove the fade option but I require it for the application I'm writing.

 

What I'm looking for is a way to call LAYISO with a predetermined list of layers so the user doesn't have to select anything.

 

Currently using AutoCAD 2013.

0 Likes
Accepted solutions (1)
1,243 Views
8 Replies
Replies (8)
Message 2 of 9

Jeff_M
Consultant
Consultant
Accepted solution

Sitting here watching football I thought about what I would do for this. The following is what I came up with.

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;

namespace Misc_Commands.LayerTools
{
    public class NewLayIso
    {
        [CommandMethod("LayIsoThruCode")]
        public void layisothrucode()
        {
            List<string> layers = new List<string> { "C-ANNO", "C-ROAD" };
            IsolateLayers(layers);
        }

        [CommandMethod ("LayUnIsoThruCode")]
        public void unisothrucode()
        {
            UnisolateLayers();
        }

        private Dictionary<ObjectId, LayInfo> layerColl = null;

        /// <summary>
        /// Isolates a list of layer names
        /// </summary>
        /// <param name="layerstoisolate">Must be passed as a list of layer names using all UPPERCASE letters</param>
        public void IsolateLayers(List<string> layerstoisolate)
        {
            if (layerColl != null)
            {
                throw new SystemException("Custom LayIso already in use!");
            }
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            layerColl = new Dictionary<ObjectId, LayInfo>();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                foreach (ObjectId id in (LayerTable)db.LayerTableId.GetObject(OpenMode.ForRead))
                {
                    LayerTableRecord layer = (LayerTableRecord)id.GetObject(OpenMode.ForWrite);
                    LayInfo layinfo = new LayInfo();
                    layinfo.Frozen = layer.IsFrozen;
                    layinfo.Off = layer.IsOff;
                    layinfo.Locked = layer.IsLocked;
                    layerColl.Add(id, layinfo);
                    if (layerstoisolate.Contains(layer.Name.ToUpper()))
                    {
                        layer.IsFrozen = false;
                        layer.IsLocked = false;
                        layer.IsOff = false;
                    }
                    else
                    {
                        layer.IsLocked = true;
                    }
                }
                
                tr.Commit();
                doc.Editor.Regen();
            }
        }

        public void UnisolateLayers()
        {
            if (layerColl == null)
                return;
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                foreach (KeyValuePair<ObjectId, LayInfo> pair in layerColl)
                {
                    LayerTableRecord layer = (LayerTableRecord)pair.Key.GetObject(OpenMode.ForWrite);
                    try
                    {
                        layer.IsFrozen = pair.Value.Frozen;
                    }
                    catch { }
                    layer.IsLocked = pair.Value.Locked;
                    layer.IsOff = pair.Value.Off;
                }
                tr.Commit();
                doc.Editor.Regen();
            }
            layerColl = null;
        }
    }

    class LayInfo
    {        
        public bool Frozen;
        public bool Off;
        public bool Locked;
    }
}
Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 3 of 9

jdowthwaite
Enthusiast
Enthusiast

Thanks for the detailed reply Jeff but I've already written a version very close to that.  Unless I'm missing something, this doesn't show the isolated layers faded like LAYISO does.

0 Likes
Message 4 of 9

Balaji_Ram
Alumni
Alumni

Hi James,

 

I think, Jeff's code should already do that.

When a layer is locked, the entities in it get faded. So, entities in layers other than the one that is isolated are all faded.

 

If that's not the behavior that you wanted, can you elaborate on it ?

 

Thanks

 

Balaji



Balaji
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 5 of 9

jdowthwaite
Enthusiast
Enthusiast

Balaji, Jeff,

 

You are correct, the layers do fade (exactly the way I need them to) but they are still selectable.  After I fade the layers, I need to create a boundary region and the locked layers are still being identified.

0 Likes
Message 6 of 9

Balaji_Ram
Alumni
Alumni

Hi James,

 

But, that is the same behavior with AutoCAD's LAYISO command too.

 

It prevents any changes to the entities in those locked layers but are still selectable.

 

If you do not want them to be selectable, please try this 

 

[CommandMethod("LayIsoThruCode")]
public void layisothrucode()
{
    List<string> layers = new List<string> { "LAYER1" };
    IsolateLayers(layers);

    Document doc = Application.DocumentManager.MdiActiveDocument;
    doc.Editor.SelectionAdded += Editor_SelectionAdded;
}

void Editor_SelectionAdded(object sender, SelectionAddedEventArgs e)
{
    ObjectId[] addedIds = e.AddedObjects.GetObjectIds();
    Document doc = Application.DocumentManager.MdiActiveDocument;
    using (Transaction tr = doc.Database.TransactionManager.StartOpenCloseTransaction())
    {
        for (int i = 0; i < addedIds.Length; i++)
        {
            ObjectId oid = addedIds[i];
            Entity ent = tr.GetObject(oid, OpenMode.ForRead, false) as Entity;
            if (! ent.Layer.ToUpper().Equals("LAYER1"))
            {
                e.Remove(i);
            }
        }
        tr.Commit();
    }
}

[CommandMethod("LayUnIsoThruCode")]
public void unisothrucode()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    doc.Editor.SelectionAdded -= Editor_SelectionAdded;

    UnisolateLayers();
}

 

This removes the entity object id from the selected list based on whether its layer was isolated or not.

 

Regards,

Balaji

 



Balaji
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 7 of 9

jdowthwaite
Enthusiast
Enthusiast
I guess I should have worded my original post better (and minus the spelling errors). What I really wanted was Jeff's answer combined with a flag on the Boundary command to ignore locked layers. Hind sight is 20/20.

I'll mark Jeff's answer as a solution since he did answer my question.
0 Likes
Message 8 of 9

jules_g_pare
Community Visitor
Community Visitor

FYI locked layer will not fade correctly based on LAYLOCKFADECTL when locked w/ api, even when running a regen. Workaround from this post here is just to set the IsOff property when changing IsLocked property. 

 

layer.IsOff = layer.IsOff;

 

https://www.keanw.com/2016/01/locking-and-unlocking-autocad-layers-visibly-using-net.html

0 Likes
Message 9 of 9

BlackBox_
Advisor
Advisor

@jules_g_pare wrote:

FYI locked layer will not fade correctly based on LAYLOCKFADECTL when locked w/ api, even when running a regen. Workaround from this post here is just to set the IsOff property when changing IsLocked property. 

 

layer.IsOff = layer.IsOff;

 

https://www.keanw.com/2016/01/locking-and-unlocking-autocad-layers-visibly-using-net.html


What happens when you store LAYLOCKFADECTL current value, set it to zero, perform the layer lock operation(s), then restore LAYLOCKFADECTL (without any regen)?


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

0 Likes