Active Contributor
Posts: 42
Registered: ‎03-20-2002
Message 1 of 4 (340 Views)
Accepted Solution

Iterating attributes of a block reference

340 Views, 3 Replies
09-17-2012 07:44 AM

Can anyone answer the following?

1. Why will this code not enter the for loop? It inserts the block, but leaves the attributes blank. Stepping through, it never enters the loop.

2. Is it necessary to add the block reference to the drawing database prior to getting the attributes? I did this in the hopes that it would solve the attribute access problem, to no avail. Not sure if there is some refresh method I am missing that will require the block to be in the drawing. Not sure why, but other examples seem to add the entities last, and I have been doing the same so far.

3. If/when I can get it to enter the for loop, is it better to get the attribute using the entity() function of the iterator as commented out, or the objectId() as I have currently?

4. Finally, is this the way to even do this? I intend to access the individual attributes of a newly inserted block and fill each of them out with passed in values.


I've stripped out as much code as I could to try and make it more to the point. I can repost if the problem does not lie in what is posted below.

Thanks for any help,

Ralph Gaston


Acad::ErrorStatus insertPointLoad(	AcGePoint3d isp, int load)
	Acad::ErrorStatus es = Acad::eOk;
	AcDbBlockTable *pBlkTbl = NULL;
	acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlkTbl, AcDb::kForRead);

	AcDbBlockTableRecord *pPL = NULL;
	pBlkTbl->getAt(_T("LOAD"), pPL, AcDb::kForRead);
	pBlkTbl->close(); pBlkTbl = NULL;

	AcDbBlockReference *pPLBlkRef = new AcDbBlockReference;
	pPL->close(); pPL = NULL;
	AcDbObjectId PLBlkRefId = AcDbObjectId::kNull;
	PLBlkRefId = addToModelSpace(pPLBlkRef);

	//fill out the attribute values
	AcDbObjectIterator *pPLIter;
	pPLIter = pPLBlkRef->attributeIterator();
	AcDbAttribute *pAtt = NULL;
	for(pPLIter->start(); !pPLIter->done(); pPLIter->step())
		//pAtt = (AcDbAttribute*)pPLIter->entity();

		AcDbObjectId pAttId = AcDbObjectId::kNull;
		pAttId = pPLIter->objectId();
		acdbOpenObject(pAtt, pAttId, AcDb::kForWrite);

		TCHAR *value = NULL;
		if(!_tcscmp(_T("LIVE"), pAtt->tag()))
			_stprintf(value, _T("%d"), load);
			pAtt->close(); pAtt = NULL;
			pAtt = NULL;
	delete pPLIter;
	pPLBlkRef->close(); pPLBlkRef = NULL;
	return es;



Valued Mentor
Posts: 498
Registered: ‎08-06-2002
Message 2 of 4 (334 Views)

Re: Iterating attributes of a block reference

09-17-2012 09:38 AM in reply to: cadproman

1) You haven't added any attributes, so there are none to iterate.

2) No.

3) You can only use one or the other depending on whether the attributes are database resident.

4) Except for failing to add the attributes, your code should work.

Owen Wengerd
Active Contributor
Posts: 42
Registered: ‎03-20-2002
Message 3 of 4 (296 Views)

Re: Iterating attributes of a block reference

10-04-2012 07:16 AM in reply to: owenwengerd

Thanks Owen, I added the attributes to each insert and it now enters the loop and works fine. I still don't completely understand the solution. Previously I believed that both the geometry AND the attributes were part of the block table record, and I'm not sure why I have to add them to the block reference when it is set to the block table record that I thought already contained the attributes.


Thanks again for your solution.


Valued Mentor
Posts: 498
Registered: ‎08-06-2002
Message 4 of 4 (291 Views)

Re: Iterating attributes of a block reference

10-04-2012 08:52 AM in reply to: cadproman

By design, attributes (except for constant attributes) can have different values for each instance of a block, so they need to belong to the block reference. The block definition can contain AcDbAttributeDefinition objects, which are used like templates by the INSERT command to create the new AcDbAttribute objects.

Owen Wengerd

You are not logged in.

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

Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

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

Ask the Community