问题我已经通过广大无私人事的帮助终于完满的解决了,再次公布出来给大家共同分享,同时感谢帮助我的朋友们!
Acad::ErrorStatus insertBolck(const char *blockName) { //将块从文件读出,添加到当前数据库中 AcDbDatabase *pNewDb =new AcDbDatabase(Adesk::kFalse);
Acad::ErrorStatus es = pNewDb->readDwgFile("C:\\Documents and Settings\\pengsl\\My Documents\\ARX\\Step01\\TEL_CD.dwg" , _SH_DENYNO,false); if (es!=Acad::eOk) { acutPrintf("\nThe file cannot be opend"); return es; }
AcDbDatabase *pDb =acdbHostApplicationServices()->workingDatabase (); // pDb->insert(AcGeMatrix3d::kIdentity, pNewDb );这样添加的块是打散的 ,而且也没有自定义属性。 AcDbObjectId blockId; if((es=pDb->insert(blockId, "TEL_CD",pNewDb, true))==Acad::eOk) { acutPrintf("\ninsert ok\n"); } else { acutPrintf("\nInsert failed"); delete pNewDb; return es; }
//将块从当前数据库中添加到图纸中并且显示出来,此时块还不具备自定义属性 AcGePoint3d ptOrg(10,10,0); AcGeScale3d XrefScale(10,10,1);
AcDbObjectId blockTelCdId; AcDbBlockTable *pBlockTable; pDb->getSymbolTable(pBlockTable, AcDb::kForRead); pBlockTable->getAt("TEL_CD", blockTelCdId);
AcDbBlockReference *pBlkRef = new AcDbBlockReference; pBlkRef->setBlockTableRecord(blockTelCdId); pBlkRef->setPosition(ptOrg); pBlkRef->setRotation(0.0);//设定转角 pBlkRef->setScaleFactors(XrefScale);//设定放大比例
AcDbBlockTableRecord *pBlockTableRecord; pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite); pBlockTable->close(); AcDbObjectId newEntId; if((es = pBlockTableRecord->appendAcDbEntity(newEntId, pBlkRef)) != Acad::eOk) { acutPrintf("\n something is wrong"); } //处理新加块的自定义属性值 AcDbBlockTableRecord *pBlockDef; acdbOpenObject(pBlockDef, blockTelCdId, AcDb::kForWrite); AcDbBlockTableRecordIterator *pIterator; pBlockDef->newIterator(pIterator); AcGePoint3d basePoint; AcDbEntity *pEnt; AcDbAttributeDefinition *pAttdef; for (pIterator->start(); !pIterator->done(); pIterator->step())//将source.dwg中所有的Attibute进行遍历 { pIterator->getEntity(pEnt, AcDb::kForRead); pAttdef = AcDbAttributeDefinition::cast(pEnt); if (pAttdef != NULL && !pAttdef->isConstant()) { AcDbAttribute *pAtt = new AcDbAttribute(); pAtt->setPropertiesFrom(pAttdef); pAtt->setInvisible(pAttdef->isInvisible()); basePoint = pAttdef->position(); basePoint += pBlkRef->position().asVector(); pAtt->setPosition(basePoint); pAtt->setHeight(pAttdef->height()); pAtt->setRotation(pAttdef->rotation()); pAtt->setTag("Tag"); pAtt->setFieldLength(25); char *pStr = pAttdef->tag(); pAtt->setTag(pStr); acutDelString(pStr); pAtt->setFieldLength(pAttdef->fieldLength()); pAtt->setTextString("J001/zg01/003"); AcDbObjectId attId; pBlkRef->appendAttribute(attId, pAtt); pAtt->close(); } pEnt->close(); // use pEnt... pAttdef might be NULL } delete pIterator; pBlockDef->close(); pBlockTableRecord->close(); pBlkRef->close();
delete pNewDb; return Acad::eOk; } |