TheSwamp

Code Red => .NET => Topic started by: cadpro on September 15, 2011, 06:12:43 AM

Title: Block Handle
Post by: cadpro on September 15, 2011, 06:12:43 AM
Hi,

Is is possible to get the block handle from an attribute reference? I get the attribute reference from a previously stored attribute collection. I did try attref.BlockId.Handle.Value.ToString(), but it gives a different value, not the block's id.

Thanks
Title: Re: Block Handle
Post by: cadpro on September 21, 2011, 06:48:00 PM
Any hints please???

Thanks
Title: Re: Block Handle
Post by: Jeff H on September 21, 2011, 07:09:42 PM
attref.BlockId.Handle will give you the Handle for the Space it is inserted in.
So Model Space's handle if inserted in  Model Space.
 
If you are wanting to get the BlockTableRecord(Block or Block Definition) Handle for the BlockReference that contains the AttributeReference in it's AttributeCollection Property then
 
You should be able to get the BlockReference from the AttributeReference.OwnerId property then
use the BlockReference.BlockTableRecord to get the Block and get its Handle.
 
 
Title: Re: Block Handle
Post by: Jeff H on September 21, 2011, 07:31:25 PM
Vb and C# code
 
Code: [Select]
        [CommandMethod("AttRefBlockName")]
         public void AttRefBlockName()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
 
            PromptNestedEntityOptions pno = new PromptNestedEntityOptions("\nSelect a Attribute reference:");
            PromptNestedEntityResult pner = ed.GetNestedEntity(pno);
 
            if (pner.ObjectId.ObjectClass != RXClass.GetClass(typeof(AttributeReference)))
            {
                ed.WriteMessage("\nNot a Attribute Reference");
            }
 
            using (Transaction trx = db.TransactionManager.StartTransaction())
            {
 
                AttributeReference attRef = (AttributeReference)trx.GetObject(pner.ObjectId, OpenMode.ForRead);
                BlockReference bref = (BlockReference)trx.GetObject(attRef.OwnerId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)trx.GetObject(bref.BlockTableRecord, OpenMode.ForRead);
 
                ed.WriteMessage("\nBlock Name: {0}     Handle: {1}", btr.Name, btr.Handle.ToString());
 
                trx.Commit();

            }
       
        }

Code: [Select]
        <CommandMethod("AttRefBlockNameVb")> _
        Public Sub AttRefBlockNameVb()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = doc.Database
 
            Dim pno As New PromptNestedEntityOptions(vbLf & "Select a Attribute reference:")
            Dim pner As PromptNestedEntityResult = ed.GetNestedEntity(pno)
 
            If pner.ObjectId.ObjectClass <> RXClass.GetClass(GetType(AttributeReference)) Then
                ed.WriteMessage(vbLf & "Not a Attribute Reference")
            End If
 
            Using trx As Transaction = db.TransactionManager.StartTransaction()
                Dim attRef As AttributeReference = trx.GetObject(pner.ObjectId, OpenMode.ForRead)
                Dim bref As BlockReference = trx.GetObject(attRef.OwnerId, OpenMode.ForRead)
                Dim btr As BlockTableRecord = trx.GetObject(bref.BlockTableRecord, OpenMode.ForRead)
 
                ed.WriteMessage("{0}Block Name: {1}     Handle: {2}", Environment.NewLine, btr.Name, btr.Handle.ToString())
 
                trx.Commit()

            End Using
        End Sub