Author Topic: Get block name by subentity  (Read 933 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Get block name by subentity
« on: June 16, 2012, 12:16:41 PM »
Code: [Select]
        [CommandMethod("bnm")]
        // expired by Barbara Hun:
        // http://adndevblog.typepad.com/autocad/2012/05/get-block-that-entities-are-included-in.html#tpe-action-posted-6a0167607c2431970b0167674896a4970b
        public static void getBlockNameFromItsSubentity()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            Database db = doc.Database;

            Matrix3d ucs = ed.CurrentUserCoordinateSystem;

            PromptNestedEntityOptions pno = new PromptNestedEntityOptions("\nSelect an subentity inside an INSERT: ");

            PromptNestedEntityResult nres = ed.GetNestedEntity(pno);

            if (nres.Status != PromptStatus.OK)
            {
                ed.WriteMessage("\nEntsel failed");
                return;
            }

            try
            {
                Transaction tr = db.TransactionManager.StartTransaction();

                using (tr)
                {
                    Point3d pickPt = nres.PickedPoint.TransformBy(ucs);

                    ed.WriteMessage("\nPicked point is {0}", pickPt);

                    ObjectId selId = nres.ObjectId;

                    List<ObjectId> objIds = new List<ObjectId>(nres.GetContainers());

                    // Reverse the "containers" list

                    // Now append the selected entity

                    objIds.Add(selId);

                    objIds.Reverse();

                    // Retrieve the sub-entity path for this entity

                    SubentityId subEnt = new SubentityId(SubentityType.Null, 0);

                    FullSubentityPath path = new FullSubentityPath(objIds.ToArray(), subEnt);

                    // Open the outermost container, relying on the open

                    // transaction...

                    Entity subent = tr.GetObject(objIds[0], OpenMode.ForRead, false) as Entity;

                    // Output the class name of the sub entity

                    ed.WriteMessage("\nThe sub entity is of type {0}", subent.GetType().Name);

                    // Get the object id of the owner block

                    ObjectId eid = subent.OwnerId;

                    DBObject bowner = tr.GetObject(eid, OpenMode.ForRead, false) as DBObject;

                    // Output the class name of the owner block

                    ed.WriteMessage("\nThe owner is of type {0}",

                    bowner.GetType().Name);

                    string blkname = string.Empty;

                    // Output the information of the block definition

                    BlockTableRecord btrec = bowner as BlockTableRecord;

                    if (btrec != null)
                    {

                        blkname = btrec.Name;


                        ed.WriteMessage("\nBlock Record name is {0}", blkname);

                    }
                    BlockReference bref = bowner as BlockReference;

                    if (bref != null)
                    {
                        bowner = tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead, false) as BlockTableRecord;

                        if (bref.IsDynamicBlock)
                        {
                            btrec = tr.GetObject(bref.DynamicBlockTableRecord, OpenMode.ForRead, false) as BlockTableRecord;

                            blkname = btrec.Name;
                        }
                        else
                        {
                            blkname = bref.Name;
                        }

                        ed.WriteMessage("\nBlock Reference name is {0}", blkname);

                    }
                    tr.Commit();
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                ed.WriteMessage("\n" + ex.Message + "\n" + ex.StackTrace);
            }
            finally
            {

            }
        }