Here is a ObjectARX and .Net sample code to create and access the layer filters. A simple trick to know the layer filter expression that will suit your needs, is to create a layer filter in AutoCAD and use this code to find the filter expression for it. This can then be used in your code to create a new layer filter.
Using ObjectARX :
Acad::ErrorStatus es = Acad::eOk;
AcDbDatabase *pDb =
acdbHostApplicationServices()->workingDatabase();
AcLyLayerFilterManager *pLayerFilterManager =
aclyGetLayerFilterManager(pDb);
AcLyLayerFilter *pRoot = NULL;
AcLyLayerFilter *pCurrent = NULL;
es = pLayerFilterManager->getFilters(pRoot, pCurrent);
if(es != Acad::eOk)
return;
if(pRoot != NULL)
{
AcArray<AcLyLayerFilter *> filters;
filters = pRoot->getNestedFilters();
// Lets find out about the root filter
acutPrintf
(
ACRX_T("\n(Root) Name : %s Expr : %s"),
pRoot->name(),
pRoot->filterExpression()
);
// Lets find out about all the filters
for(int i=0; i < filters.length();i++)
{
acutPrintf
(
ACRX_T("\nName : %s Expr : %s"),
filters.at(i)->name(),
filters.at(i)->filterExpression()
);
}
// Lets find out about the current filter
if(pCurrent != NULL)
{
acutPrintf
(
ACRX_T("\n(Current) Name : %s Expr : %s"),
pCurrent->name(),
pCurrent->filterExpression()
);
}
// Create and add a new layer filter
AcLyLayerFilter *pMyLyFilter = new AcLyLayerFilter();
pMyLyFilter->setName(ACRX_T("MyLyFilter"));
pMyLyFilter->setFilterExpression(ACRX_T("NAME == \"*Test*\""));
es = pRoot->addNested(pMyLyFilter);
es = pLayerFilterManager->setFilters(pRoot, pMyLyFilter);
delete pRoot;
}
Using AutoCAD .Net API :
using Autodesk.AutoCAD.LayerManager;
[CommandMethod("CreateLayerFilter")]
public void CreateLayerFilter()
{
Document doc
= Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
LayerFilterTree filterTree = db.LayerFilters;
LayerFilterCollection filters
= filterTree.Root.NestedFilters;
// Lets find out about the root filter
ed.WriteMessage
(
String.Format("\n(Root) Name : {0} Expr : {1}",
db.LayerFilters.Root.Name,
db.LayerFilters.Root.FilterExpression)
);
// Lets find out about all the filters
foreach(LayerFilter f in filters)
{
ed.WriteMessage
(
String.Format("\nName : {0} Expr : {1}",
f.Name,
f.FilterExpression)
);
}
// Lets find out about the current filter
if(db.LayerFilters.Current != null)
{
ed.WriteMessage
(
String.Format("\n(Current) Name : {0} Expr : {1}",
db.LayerFilters.Current.Name,
db.LayerFilters.Current.FilterExpression)
);
}
// Create and add a new layer filter
LayerFilter layerFilter = new LayerFilter();
layerFilter.Name = "MyLyFilter";
layerFilter.FilterExpression = "NAME == \"*Test*\"";
filters.Add(layerFilter);
// Set the changed layer filters tree to the database
db.LayerFilters = filterTree;
}