Help Me Insert Block

Help Me Insert Block

Anonymous
Not applicable
1,676 Views
7 Replies
Message 1 of 8

Help Me Insert Block

Anonymous
Not applicable

Hi everyone!

I'm coding for Insert Block from file DWG to Current Drawing autocad. But error.

anyone Modify my code

Thanks

void Block()
{
ACHAR* szName = _T("luanuoc");
ACHAR *fileName = _T("C:\\procad\\dwg\\luanuoc.dwg");
AcDbDatabase *pDb = acdbHostApplicationServices()->workingDatabase();
AcDbDatabase *pBlockDb = new AcDbDatabase(false,true);
pBlockDb ->readDwgFile(fileName,_SH_DENYRW,true);
AcDbBlockTable *BlkTable;
AcDbObjectId BlkId;
//AcDbObjectIdArray Ids = new AcDbObjectIdArray();
//BlkId =pBlockDb ->blockTableId();
BlkTable ->getAt(ACDB_MODEL_SPACE,BlkId,AcDb::kForRead);
pDb->insert(BlkId,szName,pBlockDb,true);
if (BlkTable->has(BlkId))
{
ads_point p;

acedGetPoint(NULL,"Chon diem chen",p);
AcGePoint3d Inspoint(p[X],p[Y],p[Z]);
AcDbBlockReference *pBlockobj = new AcDbBlockReference;
AcDbBlockTableRecord *pBlkTablerec;
pBlockobj ->setBlockTableRecord(BlkId);
pBlockobj ->setPosition(Inspoint);
pBlockobj ->setScaleFactors(1);
BlkTable ->getAt(ACDB_MODEL_SPACE,pBlkTablerec,AcDb::kForWrite);
BlkTable ->close();
pBlkTablerec ->appendAcDbEntity(pBlockobj);
pBlkTablerec ->close();
pBlockobj ->close();


}
}

0 Likes
Accepted solutions (1)
1,677 Views
7 Replies
Replies (7)
Message 2 of 8

owenwengerd
Advisor
Advisor

What is the error?

--
Owen Wengerd
ManuSoft
0 Likes
Message 3 of 8

Anonymous
Not applicable

Error Insert Block. Can you help me?

0 Likes
Message 4 of 8

owenwengerd
Advisor
Advisor

Sorry, I don't understand what the problem is.

--
Owen Wengerd
ManuSoft
0 Likes
Message 5 of 8

Alexander.Rivilis
Mentor
Mentor

@Anonymous wrote:

Error Insert Block. Can you help me?


Owen asked you about error code. IMHO it is a Fatal Error because of using non-initialize variable BlkTable in line:

BlkTable ->getAt(ACDB_MODEL_SPACE,BlkId,AcDb::kForRead);

 

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 6 of 8

Anonymous
Not applicable

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();
}
}

 

0 Likes
Message 7 of 8

Alexander.Rivilis
Mentor
Mentor
Accepted solution
You forgot after creation AcDbBlockReference add all AcDbAttribute's to it (with help of AcDbAttribute::setAttributeFromBlock method)

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 8 of 8

Anonymous
Not applicable

I Fixed this error.

Thank you very much.

0 Likes