在ARX中如何定义一个复杂块,望高手指点
在ARX中如何定义一个复杂块,然后插入模型空间中?.望高手指点, qq 172675612 Acad::ErrorStatus createBlockRecord (const char *name) {<BR> // First, check if a block of the same name already exists<BR> // by verifying in the current database block table.<BR> AcDbBlockTable *pBlockTable ;<BR> // Open the block table for read<BR> Acad::ErrorStatus es ;<BR> if ( (es =acdbHostApplicationServices ()->workingDatabase ()->getBlockTable (pBlockTable, AcDb::kForRead)) != Acad::eOk )<BR> return (es) ;if ( pBlockTable->has (name) == Adesk::kTrue ) {<BR> pBlockTable->close () ;<BR> return (Acad::eDuplicateKey) ;<BR> }<BR> // Now we know the block does not exist, so we create it<BR> // using the name passed in.<BR> AcDbBlockTableRecord *pBlockTableRecord =new AcDbBlockTableRecord ;<BR> pBlockTableRecord->setName (name) ;<BR> // To keep it simple, we use the origin for the insertion point<BR> pBlockTableRecord->setOrigin (AcGePoint3d::kOrigin) ;<BR> // Open the block table for write<BR> // since we are adding a new block definition<BR> if ( (es =pBlockTable->upgradeOpen ()) != Acad::eOk ) {<BR> delete pBlockTableRecord ;<BR> pBlockTable->close () ;<BR> return (es) ;<BR> }<BR> // Add the new block table record to the block table.<BR> // For now, the block table record is empty.<BR> if ( (es =pBlockTable->add (pBlockTableRecord)) != Acad::eOk ) {<BR> // The block table record has not been added<BR> // to the block table, so we have to delete it.<BR> pBlockTable->close();<BR> delete pBlockTableRecord;<BR> return (es) ;<BR> }<BR> pBlockTable->close () ;<BR> // Now the block table record is in the database, but is empty<BR> // (has no sub-entity).<BR> // Note that after having been added to the database, an object or an entity<BR> // is implicitely opened for write.<BR> //<BR> // So we create the sub entities to append to the block<BR> // which will represent a "happy face":<BR> // the block should consist of a round yellow face (circle)<BR> // two blue eyes (circles) and a red mouth (arc)<BR> AcDbCircle *pFace =new AcDbCircle (AcGePoint3d::kOrigin, AcGeVector3d::kZAxis, 1.0) ;<BR> AcDbCircle *pLeftEye =new AcDbCircle (AcGePoint3d (0.33, 0.25, 0.0), AcGeVector3d::kZAxis, 0.1) ;<BR> AcDbCircle *pRightEye =new AcDbCircle (AcGePoint3d (-0.33, 0.25, 0.0), AcGeVector3d::kZAxis, 0.1) ;<BR> AcDbArc *pMouth =new AcDbArc (AcGePoint3d (0, 0.5, 0), 1.0, 3.141592 + (3.141592 * 0.3), 3.141592 + (3.141592 * 0.7)) ;<BR> // Set the color property.<BR> pFace->setColorIndex (2) ;<BR> pLeftEye->setColorIndex (5) ;<BR> pRightEye->setColorIndex (5) ;<BR> pMouth->setColorIndex (1) ;<BR> // add the entities to the new block table record<BR> if ( (es =pBlockTableRecord->appendAcDbEntity (pFace)) != Acad::eOk ) {<BR> delete pFace ;<BR> delete pLeftEye ;<BR> delete pRightEye ;<BR> delete pMouth ;<BR> pBlockTableRecord->erase () ;<BR> pBlockTableRecord->close () ;<BR> return (es) ;<BR> }<BR> pFace->close () ;
if ( (es =pBlockTableRecord->appendAcDbEntity (pLeftEye)) != Acad::eOk ) {<BR> delete pLeftEye ;<BR> delete pRightEye ;<BR> delete pMouth ;<BR> pBlockTableRecord->erase () ;<BR> pBlockTableRecord->close () ;<BR> return (es) ;<BR> }<BR> pLeftEye->close () ;
if ( (es =pBlockTableRecord->appendAcDbEntity (pRightEye)) != Acad::eOk ) {<BR> delete pRightEye ;<BR> delete pMouth ;<BR> pBlockTableRecord->erase () ;<BR> pBlockTableRecord->close () ;<BR> return (es) ;<BR> }<BR> pRightEye->close () ;
if ( (es =pBlockTableRecord->appendAcDbEntity (pMouth)) != Acad::eOk ) {<BR> delete pMouth ;<BR> pBlockTableRecord->erase () ;<BR> pBlockTableRecord->close () ;<BR> return (es) ;<BR> }<BR> pMouth->close () ;
pBlockTableRecord->close () ;
return (Acad::eOk) ;<BR>}
插入块 输入块的名称
void insrtBlk()<BR>{<BR> char blkName;<BR> AcDbDatabase *pCurDb;<BR> AcDbBlockTable *pBlkTable;<BR> AcDbBlockTableRecord *pBlkTableRecord;<BR> AcDbBlockReference *pInsrtObj;<BR> AcDbObjectId blkId;
AcGePoint3d insPt;
int retCode;
retCode = acedGetString(0, "\nEnter Block Name: ", blkName);<BR> if(retCode != RTNORM || blkName == '\0')<BR> {<BR> acutPrintf("\nInvalid block name.");<BR> return;<BR> }
pCurDb = acdbHostApplicationServices()->workingDatabase();
// Check to see if the block table<BR> // has blkName
pCurDb->getBlockTable(pBlkTable, AcDb::kForRead);<BR> if(!pBlkTable->has(blkName))<BR> {<BR> acutPrintf("\nBlock definition %s not found. ", blkName);<BR> pBlkTable->close();<BR> return;<BR> }
// Get the AcDbObjectId of the block<BR> // definition.<BR> pBlkTable->getAt(blkName, blkId);
pBlkTable->getAt(ACDB_MODEL_SPACE, pBlkTableRecord, AcDb::kForWrite);<BR> pBlkTable->close();<BR> <BR> acedInitGet(RSG_NONULL, NULL);<BR> acedGetPoint(NULL, "\nPick insertion point: ", asDblArray(insPt));
pInsrtObj = new AcDbBlockReference(insPt, blkId);
// Here is where you can set scale, rotation and other<BR> // properties to the block entity. If you want to<BR> // see the AcDbBlockReference class for more details.
pBlkTableRecord->appendAcDbEntity(blkId, pInsrtObj);
pBlkTableRecord->close();<BR> pInsrtObj->close();<BR>} // This is command 'INSERTBLK'<BR>void defineblockwithattrib()<BR>{<BR> AcDbObjectId blockId;<BR> AcGePoint3d basePoint(0.0,0.0,0.0);<BR> double textHeight=3.5;<BR> double textAngle=0.0;
int retCode = 0;<BR> AcDbDatabase *pCurDb;<BR> AcDbBlockTable *pBlockTable = NULL;<BR> AcDbBlockTableRecord *pBlockRecord = new AcDbBlockTableRecord;<BR> AcDbObjectId entityId;<BR> AcGeMatrix3d ucsMat;
//第一步:设置块定义的块名和基点<BR> pBlockRecord->setName("ROUGHNESS");<BR> pBlockRecord->setOrigin(basePoint);<BR> //写状态下打开块表<BR> pCurDb = acdbHostApplicationServices()->workingDatabase();<BR> pCurDb->getBlockTable(pBlockTable,AcDb::kForWrite);
//第二步:增加块表记录到块表<BR> pBlockTable->add(blockId,pBlockRecord);
//第三步:创建粗糙度标注符号实体<BR> AcGePoint2d plinePt;<BR> AcGePoint2dArray plinePtArr;
plinePt.x=1.4*tan(PI/6.0)*textHeight;<BR> plinePt.y=1.4*textHeight;<BR> plinePtArr.append(plinePt);
plinePt.x=-1.4*tan(PI/6.0)*textHeight;<BR> plinePt.y=1.4*textHeight;<BR> plinePtArr.append(plinePt);
plinePt.x=0.0;<BR> plinePt.y=0.0;<BR> plinePtArr.append(plinePt);
plinePt.x=2.8*tan(PI/6)*textHeight;<BR> plinePt.y=2.8*textHeight;<BR> plinePtArr.append(plinePt);
AcDbPolyline *pLine = new AcDbPolyline(4);<BR> for(int idx=0;idx<4;idx++)<BR> {<BR> plinePt=plinePtArr.at(idx);<BR> pLine->addVertexAt(idx,plinePt);<BR> }
//把粗糙度标注符号实体加入块表记录中
pBlockRecord->appendAcDbEntity(entityId,pLine);<BR> pLine->close();
<BR> //第四步:创建属性定义<BR> AcDbAttributeDefinition *pAttdef<BR> =new AcDbAttributeDefinition;
//设置属性定义值
//定义属性的位置<BR> AcGePoint3d attposition(-5.0,6.0,0.0);<BR> pAttdef->setPosition(attposition);<BR> pAttdef->setHeight(textHeight);<BR> pAttdef->setRotation(textAngle);<BR> pAttdef->setHorizontalMode(AcDb::kTextLeft);<BR> pAttdef->setVerticalMode(AcDb::kTextBase);<BR> pAttdef->setPrompt("粗糙度");<BR> pAttdef->setTextString("3.2");<BR> pAttdef->setTag("粗糙度");<BR> pAttdef->setInvisible(Adesk::kFalse);<BR> pAttdef->setVerifiable(Adesk::kFalse);<BR> pAttdef->setPreset(Adesk::kFalse);<BR> pAttdef->setConstant(Adesk::kFalse);<BR> pAttdef->setFieldLength(4);
//把属性定义加入块中
pBlockRecord->appendAcDbEntity(entityId,pAttdef);<BR> pAttdef->close();<BR> pBlockRecord->close();<BR> pBlockTable->close();<BR> return; <BR>}
// This is command 'INSBLOCK'<BR>void insblock()<BR>{<BR> // TODO: Implement the command<BR> char blkName;<BR> AcDbDatabase *pCurDb;<BR> AcDbBlockTable *pBlkTable;<BR> AcDbBlockTableRecord *pBlkTableRecord;<BR> AcDbBlockTableRecord *pBlkDefRecord;<BR> AcDbBlockReference *pInsrtObj;<BR> AcDbEntity *pEnt;<BR> AcDbBlockTableRecordIterator *pIterator;<BR> AcDbAttributeDefinition *pAttDef;<BR> AcDbAttribute *pAtt;<BR> AcDbObjectId blkId;<BR> AcDbObjectId insrtId;<BR> char *pTagPrompt;<BR> AcGePoint3d insPt;<BR> AcGePoint3d basePt;<BR> int retCode;<BR>//第一步:判断块是不是存在<BR> retCode=acedGetString(0,"\nEnter Block Name:",blkName);<BR> if(retCode != RTNORM || blkName=='\0')<BR> {<BR> acutPrintf("\n Invalid block name");<BR> return;<BR> }
pCurDb = acdbHostApplicationServices()->workingDatabase();<BR> pCurDb->getBlockTable(pBlkTable,AcDb::kForRead);<BR> if(!pBlkTable->has(blkName))<BR> {<BR> acutPrintf("\nBlock definition %s not found.",blkName);<BR> pBlkTable->close();<BR> return;<BR> }
//Get the AcDbObjectId of the block definition.<BR> pBlkTable->getAt(blkName,blkId);<BR> //Get the block record of the model space to add the <BR> //block reference<BR> pBlkTable->getAt(ACDB_MODEL_SPACE,pBlkTableRecord,AcDb::kForWrite);<BR> pBlkTable->close();
//第二步:创建块参考实体<BR> acedInitGet(RSG_NONULL,NULL);<BR> acedGetPoint(NULL,"\nPick insertion point:",asDblArray(insPt));<BR> pInsrtObj = new AcDbBlockReference(insPt,blkId);
//here is where you can set scale.rotation and other<BR> //properties to the block entity. if you want to <BR> //see the AcDbBlockReference class for more details
// pBlkTableRecord->appendAcDbEntity(insrtId,pInsrtObj);
//第三步:根据块中的属性定义来定义一个属性实体,并把属性实体加入块参考对象中<BR> acdbOpenObject(pBlkDefRecord,blkId,AcDb::kForWrite);<BR> //Now check to see if the block Definition<BR> //has attributes.if if does we will add <BR> //a Block Table Record Iterator to step through<BR> //the entity and find the attribute Definitions.
if(pBlkDefRecord->hasAttributeDefinitions())<BR> {<BR> pBlkDefRecord->newIterator(pIterator);<BR> //check to see if the entity is a attribue definition.<BR> for(pIterator->start();!pIterator->done();<BR> pIterator->step())<BR> {<BR> pIterator->getEntity(pEnt,AcDb::kForRead);<BR> //check to see if the entity is an attribute definition<BR> pAttDef = AcDbAttributeDefinition::cast(pEnt);<BR> if(pAttDef != NULL && !pAttDef->isConstant())<BR> {<BR> //if it is and it's not constant<BR> //create a new attribute<BR> pAtt = new AcDbAttribute();
//setPropertiesFrom will copy Color.<BR> //Layer,Linetype,Linetype Scale and Visiblity<BR> pAtt->setPropertiesFrom(pAttDef);<BR> //setup more properties from the attribute definition<BR> pAtt->setInvisible(pAttDef->isInvisible());<BR> basePt = pAttDef->position();<BR> basePt += pInsrtObj->position().asVector();<BR> pAtt->setPosition(basePt);<BR> pAtt->setHeight(pAttDef->height());<BR> pAtt->setRotation(pAttDef->rotation());
//take note how we get the tag.<BR> pTagPrompt = pAttDef->tag();<BR> pAtt->setTag(pTagPrompt);<BR> free(pTagPrompt);<BR> //normally you would prompt the user<BR> //and ask for input values<BR> pTagPrompt = pAttDef->prompt();<BR> acutPrintf("%s%s","\n",pTagPrompt);<BR> free(pTagPrompt);
//The setFieldLength is not required<BR> //even though it is listed in the documentation<BR> pAtt->setFieldLength(25);
//setTextString is the value the <BR> //attribute recieves which would<BR> //normally be a user input value.<BR> pAtt->setTextString("6.4");<BR> //将属性加入块参考对象中去<BR> pInsrtObj->appendAttribute(pAtt);<BR> pAtt->close();<BR> }//end if<BR> pEnt->close();<BR> }//end for<BR> } //end if<BR> pBlkTableRecord->appendAcDbEntity(insrtId,pInsrtObj);<BR> delete pIterator;<BR> //Note that we close the Model space block<BR> //table record after we have added our attribute<BR> pBlkTableRecord->close();<BR> pBlkDefRecord->close();<BR> pInsrtObj->close();
}
页:
[1]