• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    Autodesk ObjectARX

    Reply
    Active Contributor
    Posts: 41
    Registered: ‎03-20-2002
    Accepted Solution

    Iterating attributes of a block reference

    216 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;
    	pPLBlkRef->setBlockTableRecord(pPL->objectId());
    	pPL->close(); pPL = NULL;
    	pPLBlkRef->setPosition(isp);
    	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->setTextString(value);
    			pAtt->close(); pAtt = NULL;
    			pAtt = NULL;
    			continue;
    		}
    	}//for
    	delete pPLIter;
    	pPLBlkRef->close(); pPLBlkRef = NULL;
    	return es;
    }

     

     

    Please use plain text.
    Mentor
    Posts: 239
    Registered: ‎08-06-2002

    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
    ManuSoft
    Please use plain text.
    Active Contributor
    Posts: 41
    Registered: ‎03-20-2002

    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.

    Ralph

    Please use plain text.
    Mentor
    Posts: 239
    Registered: ‎08-06-2002

    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
    ManuSoft
    Please use plain text.