Thanks, I fixed this error.
But I have this code for create Block with two Attributes. Then Insert this Block into drawing.
But erro they missing Attributes when i insertBlock:
1. I use Function TaoBLKDCC2 for create Block and add to Database( this very good)
2. Then Use function InsertKH1 for Insert Block with name DCC21 into Drawing with two Attributes( this function error, not have attributes
Can you help me? thank very much!
void
ProcadUtil::TaoBLKDCC2()
{
AcDbBlockTable *pBlkTbl;
acdbHostApplicationServices()->workingDatabase() ->getBlockTable(pBlkTbl, AcDb::kForWrite);
AcDbBlockTableRecord *pBlkTblRcd;
pBlkTblRcd = new AcDbBlockTableRecord();
/*char blkName[40];
if (acedGetString(Adesk::kFalse, "\nNhap ten Block:", blkName) != RTNORM)
{
pBlkTbl->close();
delete pBlkTblRcd;
return;
} */
pBlkTblRcd->setName("DCC21");
AcDbObjectId blkDefId;
pBlkTbl->add(blkDefId, pBlkTblRcd);
pBlkTbl->close();
AcGePoint3d ptStart, ptEnd;
ptStart =ProcadUtil::PolarPoint(AcGePoint3d::kOrigin,3*ProcadUtil::PI()/4,1.41421);
ptEnd =ProcadUtil::PolarPoint(ptStart,0,2);
AcDbLine *pLine1 = new AcDbLine(ptStart, ptEnd);
ptStart =ProcadUtil::PolarPoint(ptEnd,3*ProcadUtil::PI()/2,2);
AcDbLine *pLine2 = new AcDbLine(ptEnd, ptStart);
ptEnd =ProcadUtil::PolarPoint(ptStart,ProcadUtil::PI(),2);
AcDbLine *pLine3 = new AcDbLine(ptStart, ptEnd);
ptStart =ProcadUtil::PolarPoint(ptEnd,ProcadUtil::PI()/2,2);
AcDbLine *pLine4 = new AcDbLine(ptEnd, ptStart);
ptStart=ProcadUtil::PolarPoint(AcGePoint3d::kOrigin,0,1);
ptEnd =ProcadUtil::PolarPoint(ptStart,0,0.68);
AcDbLine *pLine5 = new AcDbLine(ptStart, ptEnd);
ptStart=ProcadUtil::PolarPoint(AcGePoint3d::kOrigin,ProcadUtil::PI()/2,1);
ptEnd =ProcadUtil::PolarPoint(ptStart,ProcadUtil::PI()/2,0.68);
AcDbLine *pLine6 = new AcDbLine(ptStart, ptEnd);
ptStart=ProcadUtil::PolarPoint(AcGePoint3d::kOrigin,ProcadUtil::PI(),1);
ptEnd =ProcadUtil::PolarPoint(ptStart,ProcadUtil::PI(),0.68);
AcDbLine *pLine7 = new AcDbLine(ptStart, ptEnd);
ptStart=ProcadUtil::PolarPoint(AcGePoint3d::kOrigin,3*ProcadUtil::PI()/2,1);
ptEnd =ProcadUtil::PolarPoint(ptStart,3*ProcadUtil::PI()/2,0.68);
AcDbLine *pLine8 = new AcDbLine(ptStart, ptEnd);
ptStart=ProcadUtil::PolarPoint(AcGePoint3d::kOrigin,0,2.68);
ptEnd =ProcadUtil::PolarPoint(ptStart,0,8);
AcDbLine *pLine9 = new AcDbLine(ptStart, ptEnd);
AcGeVector3d vecNormal(0, 0, 1);
AcDbCircle *pCircle = new AcDbCircle(AcGePoint3d::kOrigin,vecNormal, 0.1);
AcGePoint3d BasePoint = ProcadUtil::asAc3d(ptStart);
AcDbAttributeDefinition *pAttDef1 = new AcDbAttributeDefinition;
//pAttDef1->setPosition(ProcadUtil::PolarPoint(ptStart,ProcadUtil::PI()/2,0.5));
ptStart = ProcadUtil::PolarPoint(BasePoint,ProcadUtil::PI()/2,0.5);
pAttDef1->setPosition(ptStart);
pAttDef1->setPrompt("TENDIEM");
pAttDef1->setTag("TENDIEM");
//pAttDef1->setTextStyle(_T("chudcc2"));
pAttDef1->setHeight(2);
//pAttDef1->setAlignmentPoint()
pAttDef1->setHorizontalMode(AcDb::TextHorzMode::kTextLeft);
pAttDef1->setVerticalMode(AcDb::TextVertMode::kTextBase);
pAttDef1->setLayer("TenDiem");
AcDbAttributeDefinition *pAttDef2 = AcDbAttributeDefinition::cast(pAttDef1->clone()) ;
//pAttDef1->setPosition(ProcadUtil::PolarPoint(ptStart,ProcadUtil::PI()/2,0.5));
ptStart = ProcadUtil::PolarPoint(BasePoint,3*ProcadUtil::PI()/2,2.5);
pAttDef2->setPosition(ptStart);
pAttDef2->setPrompt("DOCAO");
pAttDef2->setTag("DOCAO");
//pAttDef1->setTextStyle("chudcc2");
pAttDef2->setHeight(2);
//pAttDef1->setAlignmentPoint()
pAttDef2->setHorizontalMode(AcDb::TextHorzMode::kTextLeft);
pAttDef2->setVerticalMode(AcDb::TextVertMode::kTextBase);
pAttDef2->setLayer("DoCao");
AcDbObjectId entId;
pBlkTblRcd->appendAcDbEntity(entId, pLine1);
pBlkTblRcd->appendAcDbEntity(entId, pLine2);
pBlkTblRcd->appendAcDbEntity(entId, pLine3);
pBlkTblRcd->appendAcDbEntity(entId, pLine4);
pBlkTblRcd->appendAcDbEntity(entId, pLine5);
pBlkTblRcd->appendAcDbEntity(entId, pLine6);
pBlkTblRcd->appendAcDbEntity(entId, pLine7);
pBlkTblRcd->appendAcDbEntity(entId, pLine8);
pBlkTblRcd->appendAcDbEntity(entId, pLine9);
pBlkTblRcd->appendAcDbEntity(entId, pCircle);
pBlkTblRcd->appendAcDbEntity(entId, pAttDef1);
pBlkTblRcd->appendAcDbEntity(entId, pAttDef2);
pLine1->close();
pLine2->close();
pLine3->close();
pLine4->close();
pLine5->close();
pLine6->close();
pLine7->close();
pLine8->close();
pLine9->close();
pCircle->close();
pAttDef1->close();
pAttDef2->close();
pBlkTblRcd->close();
}
//Ham convert diem ads_point -> AcGePoint3d
AcGePoint3d
ProcadUtil::asPnt3d(ads_point &pt)
{
AcGePoint3d pInsert;
pInsert[X] = pt[X];
pInsert[Y] = pt[Y];
pInsert[Z] = pt[Z];
return(pInsert);
}
//-------------------------
AcGePoint3d
ProcadUtil::PolarPoint(const AcGePoint3d& pt, double angle,double distance)
{
ads_point ptForm, ptTo;
ptForm[X] = pt.x;
ptForm[Y] = pt.y;
ptForm[Z] = pt.z;
acutPolar(ptForm, angle, distance, ptTo);
return ProcadUtil::asPnt3d(ptTo);
}
double
ProcadUtil::PI()
{
return 4 * atan(1.0);
}
AcGePoint3d
ProcadUtil::asAc3d(const AcGePoint3d& pt)
{
AcGePoint3d p1;
p1[X]=pt[X];
p1[Y]=pt[Y];
p1[Z]=pt[Z];
return p1 ;
}
void
ProcadUtil::InsertKH1(TCHAR *szBlock, TCHAR *szDwg, double x, double y, double elev,int stt, double Tile,double goc)
{
AcGePoint3d Inspoint(x,y,elev);
AcDbBlockTable *pBlkTable=NULL;
AcDbBlockTableRecord *pBlkTablerec;
AcDbDatabase *pCurDb = NULL;
AcGeScale3d TileScale(Tile,Tile,Tile);
pCurDb = acdbHostApplicationServices()->workingDatabase();
pCurDb ->getSymbolTable(pBlkTable,AcDb::kForRead);
AcDbObjectId BlkId;
pBlkTable ->getAt(szBlock,BlkId);
if(pBlkTable->has(szBlock))
{
AcDbBlockReference *pBlockobj = new AcDbBlockReference;
pBlockobj ->setBlockTableRecord(BlkId);
pBlockobj ->setPosition(Inspoint);
pBlockobj ->setScaleFactors(Tile);
pBlkTable ->getAt(ACDB_MODEL_SPACE,pBlkTablerec,AcDb::kForWrite);
pBlkTable ->close();
AcDbObjectId pEntId;
pBlkTablerec ->appendAcDbEntity(pEntId,pBlockobj);
pBlkTablerec ->close();
AcDbObjectIterator *pBlkRefAttItr=pBlockobj->attributeIterator();
for (pBlkRefAttItr->start(); !pBlkRefAttItr->done();pBlkRefAttItr->step())
{
AcDbObjectId attObjId;
attObjId = pBlkRefAttItr->objectId();
AcDbAttribute *pAtt = NULL;
Acad::ErrorStatus es = acdbOpenObject(pAtt, attObjId, AcDb::kForRead);
if (es != Acad::eOk)
{
acutPrintf(_T("\nLoi khong mo duoc attribute"));
delete pBlkRefAttItr;
continue;
}
if (strcmp(pAtt->tag(),"TENDIEM") == 0)
{
pAtt->setTextString("DC12");
}
if (strcmp(pAtt->tag(),"DOCAO:") == 0)
{
pAtt->setTextString("12.63");
}
pBlockobj->appendAttribute(pAtt);
pAtt->close();
}
delete pBlkRefAttItr;
pBlockobj->close();
}
}