arx & c++ 常用函数代码
1改变实体颜色
Acad::ErrorStatus
changeColor(AcDbObjectId entId, Adesk::UInt16 newColor)
{
AcDbEntity *pEntity;
acdbOpenObject(pEntity, entId,
AcDb::kForWrite);
pEntity->setColorIndex(newColor);
pEntity->close();
return Acad::eOk;
}
2打开model空间
bool OpenWorkingModelSpace()
{
Acad::ErrorStatus es ;
AcDbBlockTable *pBlockTbl ;
AcDbBlockTableRecord *pp ;
//AcDbObjectId pObjectid;
// Get the block table
if ( (es =acdbHostApplicationServices ()->workingDatabase ()->getBlockTable (pBlockTbl, AcDb::kForRead)) != Acad::eOk )
{
acutPrintf ("\nCouldn't open the block table!") ;
return false;
}
// Get the Model Space record and open it for read.
if ( (es =pBlockTbl->getAt (ACDB_MODEL_SPACE, pp, AcDb::kForWrite)) != Acad::eOk )
{
acutPrintf ("\nCouldn't get Model Space! Drawing corrupt.\n") ;
pBlockTbl->close () ;
return false;
}
pBlockTbl->close ();
//pp即为model的指针
//使用pp
pp->close();
return true;
}
3创建textstyle
bool CreateTextStyle()
{
Acad::ErrorStatus es;
AcDbTextStyleTable * pTextStyleTable;
es= acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pTextStyleTable, AcDb::kForWrite);
if (es != Acad::eOk)
{
delete pTextStyleTable;
return false;
}
if(pTextStyleTable->has("textstyle name"))
{
//delete pTextStyleRecord;
pTextStyleTable->close();
return true;
}
AcDbTextStyleTableRecord * pTextStyleRecord = new AcDbTextStyleTableRecord();
if (pTextStyleRecord == NULL)
return false;
es = pTextStyleRecord->setName("Mytablestyle");
//es = pTextStyleRecord->setFont("@宋体",Adesk::kFalse,Adesk::kFalse,936,FIXED_PITCH );
es = pTextStyleRecord->setBigFontFileName("gbcbig.shx");
es = pTextStyleRecord->setFileName("txt.shx");
//es = pTextStyleRecord->setTextSize(2.2);
//设置属性
if (es != Acad::eOk)
{
delete pTextStyleRecord;
return false;
}
es = pTextStyleTable->add(pTextStyleRecord);
if (es != Acad::eOk)
{
acutPrintf("not add id");
delete pTextStyleRecord;
return false;
}
pTextStyleTable->close();
pTextStyleRecord->close();
return true;
}
4增加blockref到model
bool AddblockrefToModel()
{
if(pMS == NULL)
return false;
AcDbBlockTable *pBlockTable ;
AcDbObjectId pTitileBlockId;
// Open the block table for read
Acad::ErrorStatus es ;
if ( (es =acdbHostApplicationServices ()->workingDatabase ()->getBlockTable (pBlockTable, AcDb::kForRead)) != Acad::eOk )
return false ;
if ( pBlockTable->getAt("xxx",pTitileBlockId) == Adesk::kTrue )
{
return false ;
}
pBlockTable->close ();
AcDbBlockReference * pTitileBlockRef = new AcDbBlockReference(AcGePoint3d(xx ,yy,zz),pTitileBlockId);
AcGeScale3d pscale(scale);
pTitileBlockRef->setScaleFactors(pscale);
//设置属性
pMS->appendAcDbEntity(pTitileBlockRef);
pTitileBlockRef->close();
return true;
}
5遍历block中的属性 // 与遍历block中的实体相似
AcDbObjectIterator * pBlkRefAttItr = pBlkRef -> 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("\nFailed to open attribute");
delete pBlkRefAttItr;
continue;
}
if (strcmp(pAtt->tag(),"TITLE:") == 0)
{
CString title = pAtt->textString();
if (strcmp(title,"PROGRESS(D)") == 0)
{ //操作
}
else if (strcmp(title,"PROGRESS(P)") == 0)
{
//操作
}
}
pAtt->close();
}
6公差标注设置函数:
void SetDimtpAndDimtm( double tp, double tm)
{
AcDbDimStyleTable *pDimStyleTbl;
acdbCurDwg()->getDimStyleTable(pDimStyleTbl,AcDb::kForRead);
AcDbDimStyleTableRecord *pDimStyleTblRcd;
pDimStyleTbl->getAt("",pDimStyleTblRcd,AcDb::kForWrite);
if (fabs(tp) == fabs(tm))
{
pDimStyleTblRcd->setDimtfac(1.0)
}
else pDimStyleTblRcd->setDimtfac(0.5);
if (tp == 0.0 && tm == 0.0)
{
pDimStyleTblRcd->setDimtol(0);
}
else
{
pDimStyleTblRcd->setDimtp(tp);
pDimStyleTblRcd->setDimtol(1);
pDimStyleTblRcd->setDimtm(tm);
}
pDimStyleTblRcd->close();
pDimStyleTbl->close();
}
Acad::ErrorStatus
changeColor(AcDbObjectId entId, Adesk::UInt16 newColor)
{
AcDbEntity *pEntity;
acdbOpenObject(pEntity, entId,
AcDb::kForWrite);
pEntity->setColorIndex(newColor);
pEntity->close();
return Acad::eOk;
}
2打开model空间
bool OpenWorkingModelSpace()
{
Acad::ErrorStatus es ;
AcDbBlockTable *pBlockTbl ;
AcDbBlockTableRecord *pp ;
//AcDbObjectId pObjectid;
// Get the block table
if ( (es =acdbHostApplicationServices ()->workingDatabase ()->getBlockTable (pBlockTbl, AcDb::kForRead)) != Acad::eOk )
{
acutPrintf ("\nCouldn't open the block table!") ;
return false;
}
// Get the Model Space record and open it for read.
if ( (es =pBlockTbl->getAt (ACDB_MODEL_SPACE, pp, AcDb::kForWrite)) != Acad::eOk )
{
acutPrintf ("\nCouldn't get Model Space! Drawing corrupt.\n") ;
pBlockTbl->close () ;
return false;
}
pBlockTbl->close ();
//pp即为model的指针
//使用pp
pp->close();
return true;
}
3创建textstyle
bool CreateTextStyle()
{
Acad::ErrorStatus es;
AcDbTextStyleTable * pTextStyleTable;
es= acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pTextStyleTable, AcDb::kForWrite);
if (es != Acad::eOk)
{
delete pTextStyleTable;
return false;
}
if(pTextStyleTable->has("textstyle name"))
{
//delete pTextStyleRecord;
pTextStyleTable->close();
return true;
}
AcDbTextStyleTableRecord * pTextStyleRecord = new AcDbTextStyleTableRecord();
if (pTextStyleRecord == NULL)
return false;
es = pTextStyleRecord->setName("Mytablestyle");
//es = pTextStyleRecord->setFont("@宋体",Adesk::kFalse,Adesk::kFalse,936,FIXED_PITCH );
es = pTextStyleRecord->setBigFontFileName("gbcbig.shx");
es = pTextStyleRecord->setFileName("txt.shx");
//es = pTextStyleRecord->setTextSize(2.2);
//设置属性
if (es != Acad::eOk)
{
delete pTextStyleRecord;
return false;
}
es = pTextStyleTable->add(pTextStyleRecord);
if (es != Acad::eOk)
{
acutPrintf("not add id");
delete pTextStyleRecord;
return false;
}
pTextStyleTable->close();
pTextStyleRecord->close();
return true;
}
4增加blockref到model
bool AddblockrefToModel()
{
if(pMS == NULL)
return false;
AcDbBlockTable *pBlockTable ;
AcDbObjectId pTitileBlockId;
// Open the block table for read
Acad::ErrorStatus es ;
if ( (es =acdbHostApplicationServices ()->workingDatabase ()->getBlockTable (pBlockTable, AcDb::kForRead)) != Acad::eOk )
return false ;
if ( pBlockTable->getAt("xxx",pTitileBlockId) == Adesk::kTrue )
{
return false ;
}
pBlockTable->close ();
AcDbBlockReference * pTitileBlockRef = new AcDbBlockReference(AcGePoint3d(xx ,yy,zz),pTitileBlockId);
AcGeScale3d pscale(scale);
pTitileBlockRef->setScaleFactors(pscale);
//设置属性
pMS->appendAcDbEntity(pTitileBlockRef);
pTitileBlockRef->close();
return true;
}
5遍历block中的属性 // 与遍历block中的实体相似
AcDbObjectIterator * pBlkRefAttItr = pBlkRef -> 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("\nFailed to open attribute");
delete pBlkRefAttItr;
continue;
}
if (strcmp(pAtt->tag(),"TITLE:") == 0)
{
CString title = pAtt->textString();
if (strcmp(title,"PROGRESS(D)") == 0)
{ //操作
}
else if (strcmp(title,"PROGRESS(P)") == 0)
{
//操作
}
}
pAtt->close();
}
6公差标注设置函数:
void SetDimtpAndDimtm( double tp, double tm)
{
AcDbDimStyleTable *pDimStyleTbl;
acdbCurDwg()->getDimStyleTable(pDimStyleTbl,AcDb::kForRead);
AcDbDimStyleTableRecord *pDimStyleTblRcd;
pDimStyleTbl->getAt("",pDimStyleTblRcd,AcDb::kForWrite);
if (fabs(tp) == fabs(tm))
{
pDimStyleTblRcd->setDimtfac(1.0)
}
else pDimStyleTblRcd->setDimtfac(0.5);
if (tp == 0.0 && tm == 0.0)
{
pDimStyleTblRcd->setDimtol(0);
}
else
{
pDimStyleTblRcd->setDimtp(tp);
pDimStyleTblRcd->setDimtol(1);
pDimStyleTblRcd->setDimtm(tm);
}
pDimStyleTblRcd->close();
pDimStyleTbl->close();
}