.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Retrieve attributes without user input

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
bsee1
565 Views, 4 Replies

Retrieve attributes without user input

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
4 REPLIES 4
Message 2 of 5
khoa.ho
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

 

 

Message 3 of 5
Hallex
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
Message 4 of 5
bsee1
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
Message 5 of 5
Hallex
in reply to: bsee1

You're welcome

Cheers Smiley Happy

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost