autoCAD数据库读写
作者:互联网
数据库写入
获取cad的安装路径
static bool GetCadPath(CString & strCadPath)
{
//获取cad的安装路径
DWORD dRet = GetModuleFileName(acedGetAcadWinApp()->m_hInstance, strCadPath.GetBuffer(_MAX_PATH), _MAX_PATH);
if (0 == dRet)
{
return false;
}
strCadPath.ReleaseBuffer();
return true;
}
写入数据
static void MyTestCreateDwg()
{
AcDbDatabase * pDb = new AcDbDatabase(); //创建一个数据库
AcDbBlockTable * pBlkTbl = NULL;
pDb->getSymbolTable(pBlkTbl, AcDb::kForRead); //获取块表
AcDbBlockTableRecord * pBlkTblRcd = NULL;
pBlkTbl->getAt(ACDB_MODEL_SPACE, pBlkTblRcd, AcDb::kForWrite); //获取块记录表
pBlkTbl->close(); //关闭块表
AcGePoint3d ptS(10, 10, 0);
AcGePoint3d ptE(30,30,0);
AcDbLine * pLine = new AcDbLine(ptS, ptE); //创建一条直线(实体)
pBlkTblRcd->appendAcDbEntity(pLine); //将直线加入到块记录表中
pLine->close();
pBlkTblRcd->close();
CString strCadPath;
if (GetCadPath(strCadPath)) //得到cad安装的路径
{
strCadPath = strCadPath.Left(strCadPath.GetLength() - 8); //减去 acad.exe
acutPrintf(strCadPath);
pDb->saveAs(strCadPath+_T("Test.dwg"));
}
delete pDb;
}
.
.
.
数据读取
static void MyTestReadDwg()
{
AcDbDatabase * pDb = new AcDbDatabase(false); //创建一个数据库 对象为空
CString strPath;
GetCadPath(strPath);
strPath = strPath.Left(strPath.GetLength() - 8);
CString strDwgPath = strPath + _T("Test.dwg");
pDb->readDwgFile(strDwgPath); //数据库中读取信息
//获取指定数据库中的所有实体进行遍历
AcDbObjectIdArray allEntIds = Help::GetAllEntityIds(pDb);
for (int i = 0; i < allEntIds.length(); i++)
{
AcDbEntity * pEnt = NULL;
if (acdbOpenObject(pEnt,allEntIds[i],AcDb::kForRead) == Acad::eOk)
{
CString str = pEnt->isA()->name();
acutPrintf(str);
//acutPrintf(_T("类名称:%s"), (pEnt->isA())->name());
pEnt->close();
}
}
delete pDb;
}
标签:pEnt,autoCAD,读写,CString,pDb,strCadPath,数据库,strPath 来源: https://blog.csdn.net/qq_42972267/article/details/121757109