.NET

Reply
Mentor
bsee1
Posts: 211
Registered: ‎11-14-2011
Message 1 of 5 (274 Views)
Accepted Solution

Retrieve attributes without user input

274 Views, 4 Replies
09-14-2012 08:37 AM

I develop addins for Inventor primarily, so the AutoCAD api is very new to me.  I'm working in AutoCAD 2010 and visual studio 2012.

 

I've looked at many posts for getting block attributes, but they either seem to assume I already have the Blockreference, or they give a prompt to the user.  I'm hoping to avoid both of those.

 

I know the info I need is inside of a blockTable by the name of "DET".

 

Database acCurDb = openDoc.Database;
using (Transaction acTrans = CurDb.TransactionManager.StartTransaction())
     {
       //How can I access the blockreference in here which corresponds to the blockTable "DET"?
     }

 

Any help would be appreciated.

 

Thanks,

Brandon

*****************************
Win7 x64 - 16gb ram
i7 3610qm
FirePro M4000

Inventor 2013
ETO 6.1
Mentor
khoa.ho
Posts: 161
Registered: ‎09-15-2011
Message 2 of 5 (264 Views)

Re: Retrieve attributes without user input

09-14-2012 09:05 AM in reply to: bsee1

An AutoCAD database has only one BlockTable but it has many BlockTableRecords.

 

A BlockTableRecord can be ModelSpace, PaperSpace, or any BlockDefinition.

 

A BlockReference is an instance of a BlockDefinition, so actually you only get BlockDefinition (not BlockReference) from a BlockTable. We will find all BlockReferences on ModelSpace which is blockTable[BlockTableRecord.ModelSpace].

 

To get a BlockTableRecord from a given name, jus simply call blockTable[name]. Make sure you pass the correct name that exists on the current DWG database.

 

See the following code to get all block table record names:

 

[CommandMethod("GetAllBlockTableRecordNames")]
public static void GetAllBlockTableRecordNames()
{
	var nameList = new List<string>();
	Database db = Application.DocumentManager.MdiActiveDocument.Database;
	using (Transaction trans = db.TransactionManager.StartTransaction())
	{
		var bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
		foreach (ObjectId btrId in bt)
		{
			var btr = (BlockTableRecord)trans.GetObject(btrId, OpenMode.ForRead);
			nameList.Add(btr.Name);
		}
	}
	// Do something with the new created nameList

}

 

-Khoa

 

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 5 (223 Views)

Re: Retrieve attributes without user input

09-14-2012 11:25 PM in reply to: bsee1

If you need to iterate through the attribute definitions within the block table record,

see if that helps:

 

       //  borrowed from Tony TanZillo as posted on:
        //  http://forums.autodesk.com/t5/NET/Stupid-confusion-with-blocks-and-stuff/td-p/2697447
        [CommandMethod("DET")]
        public static void UpdateBlocks()
        {
        Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed= doc.Editor;
            // Do this *before* starting the transaction where
            // the changes will occur:
            doc.TransactionManager.EnableGraphicsFlush(true);
            try
            {
            // Start a transaction:
            using (Transaction tr = doc.TransactionManager.StartTransaction())
            {
                // Open the block table:
                BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
                // check if block exists, if not then exit command
                if (!bt.Has("DET")) return;

                // Get the ObjectId of the block's BlockTableRecord:
                ObjectId btrId = bt["DET"];

                // Open the BlockTableRecord for write:
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId,OpenMode.ForWrite);
                // Iterate through the BlockTableRecord items:
                foreach (ObjectId id in btr)
                {
                    // open each BlocReference for write and call its
                    // RecordGraphicsModified() method.  Note the use of 
                    // the overload of GetObject() that allows objects on 
                    // locked layers to be opened for write:
                    if (id.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(AttributeDefinition))))
                    {
                        AttributeDefinition attDef = (AttributeDefinition)tr.GetObject(id, OpenMode.ForRead, false);
                     
                            ed.WriteMessage("\nAttribute  --->  Tag: {0}  --->  Prompt: {1}  --->  Value: {2}", attDef.Tag, attDef.Prompt, attDef.TextString);
                        
                        // ----------   do changes with attributes here, e.g. ----------   // 
                        // http://adndevblog.typepad.com/autocad/2012/08/change-the-style-of-attributes.html
                    }
                }
                //---------------------------------------------------------------//
                // Get the ObjectIds of all insertions of the block:
                ObjectIdCollection ids = btr.GetBlockReferenceIds(true, true);
                // Iterate through the ids:
                foreach (ObjectId blkId in ids)
                {
                    BlockReference blkref = (BlockReference)tr.GetObject(blkId, OpenMode.ForWrite, false, true);

                    //update BlockReference
                    blkref.BlockTableRecord = btrId;
                    // Indicate that the block reference's graphics have changed:

                    blkref.RecordGraphicsModified(true);
                }
                // Tell the transaction manager to flush all changes
                // to the display:
                //---------------------------------------------------------------//
                doc.TransactionManager.QueueForGraphicsFlush();
                doc.TransactionManager.FlushGraphics();
                doc.Editor.UpdateScreen();
                tr.Commit();
                }
            }
                catch (Autodesk.AutoCAD.Runtime.Exception ex)
                {

                    ed.WriteMessage("\nBlock updating exception!\nError:" + ex.Message + "\nTrace:" + ex.StackTrace);
                }
                finally
                {
                }
            }

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Mentor
bsee1
Posts: 211
Registered: ‎11-14-2011
Message 4 of 5 (185 Views)

Re: Retrieve attributes without user input

09-17-2012 12:47 PM in reply to: Hallex

This worked for me.  The post prior to yours got me to the Attribute Definitions.  Yours was able to get me to the BlockReferences which contained the AttributeReferences. Thank you.

*****************************
Win7 x64 - 16gb ram
i7 3610qm
FirePro M4000

Inventor 2013
ETO 6.1
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 5 of 5 (181 Views)

Re: Retrieve attributes without user input

09-17-2012 12:54 PM in reply to: bsee1

You're welcome

Cheers :smileyhappy:

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community