Find Entities Using LayerTableRecord References

Find Entities Using LayerTableRecord References

js75CAD
Enthusiast Enthusiast
456 Views
3 Replies
Message 1 of 4

Find Entities Using LayerTableRecord References

js75CAD
Enthusiast
Enthusiast

Hi All,

 

I can't seem to find anyone who has ever done this and it is making me question if what I am doing is right.

 

At this stage I can get the layer table record that I want, but I cannot seem to find all of the ObjectIDs that are referenced by this record. Does anyone know if/how this can be done?

 

Dim ltr As LayerTableRecord = GetLayerRecord("Chk")

 

So I know that I have one polyline in model space and I have one nested circle in a block. 100% guaranteed.

 

I have tried the selectAll function but the selection constantly has a 5001 error which is caused by removing the polyline in mode space because the nested entity is the one I want ultimately.

 

Dim tvs As TypedValue() = New TypedValue(0) {New TypedValue(CInt(DxfCode.LayerName), "chk")}
            Dim sf As SelectionFilter = New SelectionFilter(tvs)
            Dim psr As PromptSelectionResult = ed.SelectAll(sf)

 

So this is why I am trying it as I have mentioned. I have used the MGSnoopDB and found what i want there, but does necessarily mean it can be done in vb? Screenshot attached...

 

All I want is a list of the base entities (ie Circle, Polyline etc) that are on the layer identified. Can this be done and how?

 

Now this works, but only because these are in model space and not nested, so any help in making me understand how to do this would be a fantastic help!

Dim btr As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)
            Dim b As IEnumerable(Of ObjectId) = btr.Cast(Of ObjectId)()

            ' Grab full list of mains! 
            Dim mainscons As List(Of ObjectId) = (From id In b
                                                  Select id Where (CType(tr.GetObject(id, OpenMode.ForRead), Entity)).Layer.StartsWith("chklayer")).ToList
0 Likes
Accepted solutions (1)
457 Views
3 Replies
Replies (3)
Message 2 of 4

_gile
Consultant
Consultant
Accepted solution

Hi,

You should be able to take inspiration from the ReferencedBy and ReferenceFiler classes used in Inspector (as I recall, MgdDbg also uses a class derived from DwgFiler).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 4

_gile
Consultant
Consultant

If you only need the ObjectIds of the entites on some layer, you can iterate through all BlockTableRecords in the database (except xrefs).

        public static ObjectIdCollection GetReferencedByEntities(Database db, string layerName)
        {
            var ids = new ObjectIdCollection();
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                foreach (ObjectId btrId in blockTable)
                {
                    var btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
                    if (!(btr.IsDependent || btr.IsFromExternalReference || btr.IsFromOverlayReference))
                    {
                        foreach (ObjectId entId in btr)
                        {
                            var entity = (Entity)tr.GetObject(entId, OpenMode.ForRead);
                            if (entity.Layer == layerName)
                            {
                                ids.Add(entId);
                            }
                        }
                    }
                }
                tr.Commit();
            }
            return ids;
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 4

js75CAD
Enthusiast
Enthusiast

Thanks and always appreciate your help _gile!

 

As you have confirmed there is a third party class involved in gaining the information I need.

 

Unfortunately, one part of my search does also include xreference entities, so I had been hoping to kill two birds with one stone. Sadly not to be.

 

Not disheartened though, I appreciate you offering your code and I will see if I can adapt it to do what I ultimately think should be a layertablerecord staple item...

 

Thanks mate!

0 Likes