Using Autodesk.AutoCAD.ApplicationServices;
Using Autodesk.AutoCAD.DatabaseServices;
Using Autodesk.AutoCAD.EditorInput;
Using Autodesk.AutoCAD.Runtime;
Using Autodesk.AutoCAD.LayerManager;
Namespace Layerfilters
{
public class Commands
{
[Commandmethod ("Llfs")]
static public void Listlayerfilters ()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Database db = Doc. Database;
Editor ed = doc. Editor;
//List the nested layer filters
layerfiltercollection LFC =
Db. LayerFilters.Root.NestedFilters;
For (int i = 0; i < LFC. Count; ++i)
{
layerfilter lf = lfc[i];
Ed. Writemessage (
"\n{0}-{1} (Can{2} be deleted)",
i + 1,
Lf. Name,
(LF.) AllowDelete? " ": "not")
);
}
}
[Commandmethod ("CLFS")]
static public void Createlayerfilters ()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Database db = Doc. Database;
Editor ed = doc. Editor;
Try
{
//Get The existing layer filters
//(we'll add to them and set them back)
Layerfiltertree lft =
Db. Layerfilters;
layerfiltercollection LFC =
LfT. Root.nestedfilters;
//Create three new layer filters
layerfilter lf1 = new layerfilter ();
Lf1. Name = "Unlocked Layers";
Lf1. FilterExpression = "locked==\" false\ "";
layerfilter lf2 = new layerfilter ();
Lf2. Name = "White Layers";
Lf2. FilterExpression = "color==\" 7\ "";
layerfilter lf3 = new layerfilter ();
Lf3. Name = "Visible Layers";
Lf3. FilterExpression =
" off==\" false\ "and frozen==\" false\ "";
//ADD them to the collection
Lfc. ADD (LF1);
Lfc. ADD (LF2);
Lfc. ADD (LF3);
//Set them back on the Database
Db. Layerfilters = LFT;
//List The layer filters, to see the new ones
Listlayerfilters ();
}
catch (Exception ex)
{
Ed. Writemessage (
"\nexception: {0}",
Ex. Message
);
}
}
[Commandmethod ("DLF")]
static public void Deletelayerfilter ()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Database db = Doc. Database;
Editor ed = doc. Editor;
Listlayerfilters ();
Try
{
//Get The existing layer filters
//(we'll add to them and set them back)
Layerfiltertree lft =
Db. Layerfilters;
layerfiltercollection LFC =
LfT. Root.nestedfilters;
//Prompt for the index of the filter to delete
Promptintegeroptions pio =
new promptintegeroptions (
"\n\nenter index of filter to delete"
);
Pio. Lowerlimit = 1;
Pio. Upperlimit = lfc. Count;
Promptintegerresult PIR =
Ed. Getinteger (PIO);
//Get the selected filter
layerfilter lf = Lfc[pir. VALUE-1];
//If It ' s possible to delete it
if (!LF. AllowDelete)
{
Ed. Writemessage (
"\nlayer filter cannot be deleted."
);
}
Else
{
Lfc. Remove (LF);
Db. Layerfilters = LFT;
Listlayerfilters ();
}
}
catch (Exception ex)
{
Ed. Writemessage (
"\nexception: {0}",
Ex. Message
);
}
}
}
}