Detecting anonymous blocks (.NET C#)

I know how to detect if a given object is a polyline or a block reference. But how do I detect if an entity is an anonymous block?
// Examine the object
var ent = acTransNewDb.GetObject(id, _AcDb.OpenMode.ForRead) as _AcDb.Entity;

// Is it a polyline or block?
if (ent is _AcDb.Polyline || ent is _AcDb.BlockReference)
{
}

Comments

  • avc_programming
    edited March 1
    You can get the BTR of a block and check its IsAnonymous property.
    In addition, all anonymous blocks have a name starting with an asterisk.
  • Thank you. I asked AI (CoPilot) how it would do it and I believe it used the principles you stated:
    public bool IsAnonymousBlock(ObjectId objectId)
    {
        using (Transaction transaction = objectId.Database.TransactionManager.StartTransaction())
        {
            BlockReference blockReference = transaction.GetObject(objectId, OpenMode.ForRead) as BlockReference;
            if (blockReference != null)
            {
                // Check the block name.
                string blockName = blockReference.Name;
                if (blockName.StartsWith("*U"))
                    return true;
    
                // Check the block type flag.
                BlockTableRecord blockDef = transaction.GetObject(blockReference.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
                if (blockDef != null && blockDef.IsAnonymous)
                    return true;
            }
        }
        return false;
    }
    
  • > if (blockName.StartsWith("*U"))

    that is wrong/incomplete :-)
    the only indicator of an anonymous block is the asteriks "*", not the sequence "*U" ...
    because
    a) also "*u" (lowercase) is valid, as block names are in generally case-insensitive
    and
    b) any "*" sequence like "*D" (seen with dimensions) or "*x" also indocates an anonymous block ...

    So far for the "intelligence" - if the so-called AI "learns" from erroneous samples, and effectively has no clue what text pieces are clobbered together ... then you get such improper results;
    in the end, such "CoPilot" mangles valid + invalid code, from whatever & whereever "CoPilot" finds on the web ...
    no trace of intelligence, just plain statistics including valid + invalid samples, and (naturally !) not being able to distinguish between ...

    In the end, it is *KNOWLEDGE* what matters, not blindly collecting any rubbish :-)
    hope this helps a bit ...
  • Chuckie
    edited March 2
    @TorstenMoses I agree 100% and thanks for the clarification about anonymous blocks.

    But, it brings me on to a related question about these blocks. My client sent me a drawing with a parametric array in it. If I list it in Bricscad it is an anonymous block. If I use the EXPLODE command in Bricscad it ends up as a series of LINE entities.

    Yet, using .NET C# to explode the same entity with:

    ent.Explode(objExplodedObjs);

    (I have extra code to delete the old etc and just showing this line for brevity). Why is it that in the resulting file I do not have a series of LINE entities but another series of anonymous block entities? I can explode them in Bricscad and they turn to LINES again.

    Why is it that BricsCAD exploded the parametric array directly to LINE entities whereas usigng .NET exploded it into a bunch of anonymous blocks?
  • A parametric array is a block consisting of block referenceses. You need to use the Explode command twice to get the lines.