wadeyu 发表于 2005-8-22 10:09:00

[求助]如何获取用insert插入的属性块的各属性值(块名已知)

如何获取用insert插入的属性块的各属性值(块名已知)

风之助 发表于 2005-8-22 10:47:00

<P>这是一本书中的例子,每一行代码的例子我还不太懂,你自己看看吧.其中包括插入块的代码.</P>
<P>void insrtBlk()<BR>{<BR>&nbsp;char blkName;<BR>&nbsp;AcDbDatabase *pCurDb;<BR>&nbsp;AcDbBlockTable *pBlkTable;<BR>&nbsp;AcDbBlockTableRecord *pBlkTableRecord;<BR>&nbsp;AcDbBlockTableRecord *pBlkDefRecord;<BR>&nbsp;AcDbBlockReference *pInsrtObj;<BR>&nbsp;AcDbEntity *pEnt;<BR>&nbsp;AcDbBlockTableRecordIterator *pIterator;<BR>&nbsp;AcDbAttributeDefinition *pAttDef;<BR>&nbsp;AcDbAttribute *pAtt;<BR>&nbsp;AcDbObjectId blkId;<BR>&nbsp;AcDbObjectId insrtId;</P>
<P>&nbsp;char *pTagPrompt;</P>
<P>&nbsp;AcGePoint3d insPt;<BR>&nbsp;AcGePoint3d basePt;</P>
<P>&nbsp;int retCode;</P>
<P>&nbsp;retCode = acedGetString(0, "\nEnter Block Name: ", blkName);<BR>&nbsp;if(retCode != RTNORM || blkName == '\0')<BR>&nbsp;{<BR>&nbsp;&nbsp;acutPrintf("\nInvalid block name.");<BR>&nbsp;&nbsp;return;<BR>&nbsp;}</P>
<P>&nbsp;pCurDb = acdbHostApplicationServices()-&gt;workingDatabase();</P>
<P>&nbsp;// Check to see if the block table<BR>&nbsp;// has blkName</P>
<P>&nbsp;pCurDb-&gt;getBlockTable(pBlkTable, AcDb::kForRead);<BR>&nbsp;if(!pBlkTable-&gt;has(blkName))<BR>&nbsp;{<BR>&nbsp;&nbsp;acutPrintf("\nBlock definition %s not found. ", blkName);<BR>&nbsp;&nbsp;pBlkTable-&gt;close();<BR>&nbsp;&nbsp;return;<BR>&nbsp;}</P>
<P>&nbsp;// Get the AcDbObjectId of the block<BR>&nbsp;// definition.<BR>&nbsp;pBlkTable-&gt;getAt(blkName, blkId);</P>
<P>&nbsp;pBlkTable-&gt;getAt(ACDB_MODEL_SPACE, pBlkTableRecord, AcDb::kForWrite);<BR>&nbsp;pBlkTable-&gt;close();<BR>&nbsp;<BR>&nbsp;acedInitGet(RSG_NONULL, NULL);<BR>&nbsp;acedGetPoint(NULL, "\nPick insertion point: ", asDblArray(insPt));</P>
<P>&nbsp;pInsrtObj = new AcDbBlockReference(insPt, blkId);</P>
<P>&nbsp;// Here is where you can set scale, rotation and other<BR>&nbsp;// properties to the block entity. If you want to<BR>&nbsp;// see the AcDbBlockReference class for more details.<BR>&nbsp;pBlkTableRecord-&gt;appendAcDbEntity(insrtId, pInsrtObj);</P>
<P><BR>&nbsp;acdbOpenObject(pBlkDefRecord, blkId, AcDb::kForRead);<BR>&nbsp;// Now check to see if the Block Definition<BR>&nbsp;// has attributes. If it does we will add<BR>&nbsp;// a Block Table Record Iterator to step through<BR>&nbsp;// the entities and find the Attribute Definitions.</P>
<P>&nbsp;if(pBlkDefRecord-&gt;hasAttributeDefinitions())<BR>&nbsp;{<BR>&nbsp;&nbsp;pBlkDefRecord-&gt;newIterator(pIterator);</P>
<P>&nbsp;&nbsp;for(pIterator-&gt;start(); !pIterator-&gt;done(); pIterator-&gt;step())<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;pIterator-&gt;getEntity(pEnt, AcDb::kForRead);<BR>&nbsp;&nbsp;&nbsp;// Check to see if the entity is an<BR>&nbsp;&nbsp;&nbsp;// attribute definition.<BR>&nbsp;&nbsp;&nbsp;pAttDef = AcDbAttributeDefinition::cast(pEnt);<BR>&nbsp;&nbsp;&nbsp;if(pAttDef != NULL &amp;&amp; !pAttDef-&gt;isConstant())<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;// If it is and its not constant<BR>&nbsp;&nbsp;&nbsp;&nbsp;// create a new attribute<BR>&nbsp;&nbsp;&nbsp;&nbsp;pAtt = new AcDbAttribute();<BR>&nbsp;&nbsp;&nbsp;&nbsp;// setPropertiesFrom will copy<BR>&nbsp;&nbsp;&nbsp;&nbsp;// Color, Layer, Linetype,Linetype scale and<BR>&nbsp;&nbsp;&nbsp;&nbsp;// Visibility.<BR>&nbsp;&nbsp;&nbsp;&nbsp;pAtt-&gt;setPropertiesFrom(pAttDef);<BR>&nbsp;&nbsp;&nbsp;&nbsp;// setup more properties from the attribute<BR>&nbsp;&nbsp;&nbsp;&nbsp;// definition<BR>&nbsp;&nbsp;&nbsp;&nbsp;pAtt-&gt;setInvisible(pAttDef-&gt;isInvisible());<BR>&nbsp;&nbsp;&nbsp;&nbsp;basePt = pAttDef-&gt;position();<BR>&nbsp;&nbsp;&nbsp;&nbsp;basePt += pInsrtObj-&gt;position().asVector();<BR>&nbsp;&nbsp;&nbsp;&nbsp;pAtt-&gt;setPosition(basePt);<BR>&nbsp;&nbsp;&nbsp;&nbsp;pAtt-&gt;setHeight(pAttDef-&gt;height());<BR>&nbsp;&nbsp;&nbsp;&nbsp;pAtt-&gt;setRotation(pAttDef-&gt;rotation());</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;// Take note how we get the tag.<BR>&nbsp;&nbsp;&nbsp;&nbsp;pTagPrompt = pAttDef-&gt;tag();<BR>&nbsp;&nbsp;&nbsp;&nbsp;pAtt-&gt;setTag(pTagPrompt);<BR>&nbsp;&nbsp;&nbsp;&nbsp;free(pTagPrompt);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;// Normally you would prompt the user<BR>&nbsp;&nbsp;&nbsp;&nbsp;// and ask for input values.<BR>&nbsp;&nbsp;&nbsp;&nbsp;pTagPrompt = pAttDef-&gt;prompt();<BR>&nbsp;&nbsp;&nbsp;&nbsp;acutPrintf("%s%s", "\n", pTagPrompt);<BR>&nbsp;&nbsp;&nbsp;&nbsp;free(pTagPrompt);<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;// The setFieldLength is not required<BR>&nbsp;&nbsp;&nbsp;&nbsp;// even though it is listed in the<BR>&nbsp;&nbsp;&nbsp;&nbsp;// documentation.<BR>&nbsp;&nbsp;&nbsp;&nbsp;pAtt-&gt;setFieldLength(25);<BR>&nbsp;&nbsp;&nbsp;&nbsp;// setTextString is the value the<BR>&nbsp;&nbsp;&nbsp;&nbsp;// attribute receives which would<BR>&nbsp;&nbsp;&nbsp;&nbsp;// normally be a user input value.<BR>&nbsp;&nbsp;&nbsp;&nbsp;pAtt-&gt;setTextString("This is a test");</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;pInsrtObj-&gt;appendAttribute(pAtt);<BR>&nbsp;&nbsp;&nbsp;&nbsp;pAtt-&gt;close();<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;pEnt-&gt;close();<BR>&nbsp;&nbsp;}// for<BR>&nbsp;&nbsp;delete pIterator;</P>
<P>}// if has attribute definitions</P>
<P>&nbsp;&nbsp;// Note that we close the Model Space<BR>&nbsp;// block table record after we have added<BR>&nbsp;// our attributes.<BR>&nbsp;pBlkTableRecord-&gt;close();<BR>&nbsp;pInsrtObj-&gt;close();<BR>}</P>
页: [1]
查看完整版本: [求助]如何获取用insert插入的属性块的各属性值(块名已知)