Issue in filter selection

Issue in filter selection

mehdi-guida
Advocate Advocate
588 Views
3 Replies
Message 1 of 4

Issue in filter selection

mehdi-guida
Advocate
Advocate

Hi

I have below codes. But it has error(image attached).

If i omit the line 'filterlist[1] = new TypedValue(67, s);' it works.

What is the issue?

Thank you

 

 Editor editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
LayoutManager layoutMgr = LayoutManager.Current;
string s = layoutMgr.CurrentLayout;
TypedValue[] filterlist = new TypedValue[2];
filterlist[0] = new TypedValue(2, "note");
filterlist[1] = new TypedValue(67, s);
Autodesk.AutoCAD.EditorInput.SelectionFilter filter = new Autodesk.AutoCAD.EditorInput.SelectionFilter(filterlist);
PromptSelectionResult Promptentity = editor.SelectAll(filter);
SelectionSet sval = Promptentity.Value; 

0 Likes
589 Views
3 Replies
Replies (3)
Message 2 of 4

_gile
Consultant
Consultant

Hi,

 

Try this way:

var filter = new SelectionFilter(new[]
{
    new TypedValue(2, "note"),
    new TypedValue(410, LayoutManager.Current.CurrentLayout)
});


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 4

mehdi-guida
Advocate
Advocate

Thank you

But it works only for simple blocks.

if the blocks are dynamic , it can't select them.

0 Likes
Message 4 of 4

_gile
Consultant
Consultant

You cannot directly filter dynamic blocs by name wit a selection filter. You have to check the Name of the DynamicBlockTableRecord of each block reference.

Assuming you want to get all blocks by name in the current layout BlockTableRecord, the simplest (and faster) way is to iterate through all entities in the current space.

 

Here's an example:

using (var tr = db.TransactionManager.StartTransaction())
{
    var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
    foreach (ObjectId id in curSpace)
    {
        if (id.ObjectClass.DxfName == "INSERT")
        {
            var br = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
            var btr = (BlockTableRecord)tr.GetObject(br.DynamicBlockTableRecord, OpenMode.ForRead);
            if (btr.Name == "note")
            {
                // do your stuff with the block reference
            }
        }
    }
    tr.Commit();
}

 

Another example with a separate method using Linq

static IEnumerable<ObjectId> GetAllCurrentSpaceBlocksByName(string blockName)
{
    var db = HostApplicationServices.WorkingDatabase;
    using (var tr = db.TransactionManager.StartOpenCloseTransaction())
    {
        var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
        return curSpace
            .Cast<ObjectId>()
            .Where(id => id.ObjectClass.DxfName == "INSERT")
            .Select(id => (BlockReference)tr.GetObject(id, OpenMode.ForRead))
            .Where(br => ((BlockTableRecord)tr.GetObject(br.DynamicBlockTableRecord, OpenMode.ForRead)).Name == blockName)
            .Select(br => br.ObjectId);
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes