读取DWG文件
Mcad::ErrorStatus readDwgFile(LPCTSTR pszFileName, int shmode = 0x40, bool bAllowCPConversion = false, LPCTSTR wszPassword = NULL, int rc = Mcad::kReadAll, byte* pInData = NULL, long lInDataLength = 0);
参数 |
说明 |
LPCTSTR pszFileName |
DWG文件路径 |
int shmode = 0x40 |
暂没有使用 |
bool bAllowCPConversion = false |
暂没有使用 |
LPCTSTR wszPassword = NULL |
暂没有使用 |
int rc = Mcad::kReadAll |
指定需要读取的内容,不需要读取的内容可以不读取,这样能提高读取dwg文件的速度,可以取如下值: enum ReadContent { kReadAll = 0xFFFFFFFF, kReadNull = 0x0, // 下面数据都不读取 kReadExData = 0x1, kReadHandle = 0x2, kReadBlockRefPreviewIcon = 0x4, kReadBlockLayoutName = 0x8, kNamedObjectsDictionary = 0x10, // 是否读对象字典,如果不读,字典下所有数据都不会读取. kReadGroupDict = 0x20, // 是否读取组字典 kReadLayoutDict = 0x40, kReadMlineStyleDict = 0x80, kReadImageDict = 0x100, kReadXrecord = 0x200, kReadDrawOrder = 0x400, kReadxData = 0x800, kReadExRecord = 0x1000, kReadExternalReference = 0x2000, kReadCustomEntity = 0x4000 // 是否读取自定义实体,如果该为项为真,则自动让kReadHandle值也为真,因为自定义实体需要名柄支持. }; |
byte* pInData = NULL |
如果从内存打开文件,pInData指向内存数据。 |
long lInDataLength = 0 |
内存数据的长度。 |
如果成功返回Mcad::eOk,如果传递的数据非法则返回Mcad::eInvalidInput
例如:打开一个DWG文件,遍历数据库,得到块记录中属性文本
CTestDlg* pDlg = (CTestDlg*)AfxGetApp()->GetMainWnd(); CPreviewFileDialog openDlg(TRUE,_T("dwg"),NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, //_T("dwg(*.dwg) | *.dwg||"), _T("DWG files (*.dwg)|*.dwg|DXF files (*.dxf)|*.dxf|Jpg files (*.jpg)|*.jpg|BMP files (*.bmp)|*.bmp||"), pDlg); CString sAppPath = CTestDlgApp::GetAppPath(); CString sInitialDir = AfxGetApp()->GetProfileString(_T("MxDWGTestDlg"),_T("OpenDir"),sAppPath ); CString sFileName; openDlg.m_ofn.lpstrInitialDir= sInitialDir; //设置打开文件的缺省路径 if(openDlg.DoModal() == IDOK) { sFileName = openDlg.GetPathName(); } else { return; } // 打开文件。 McDbDatabase dataBase; if(dataBase.readDwgFile(sFileName) != Mcad::eOk) { AfxMessageBox(_T("读取文件失败")); return; } // 下面读取图中指定块的属性。 McDbBlockTableRecordPointer spBlkRec(MCDB_MODEL_SPACE,&dataBase,McDb::kForRead); if(spBlkRec.openStatus() != Mcad::eOk) { return; } // 生成遍历数据库用的遍历器. McDbBlockTableRecordIterator* pIterator = NULL; spBlkRec->newIterator(pIterator); if(pIterator == NULL) return; // 遍历数据库 for(;!pIterator->done();pIterator->step() ) { McDbObjectId id; pIterator->getEntityId(id); McDbObjectPointer<McDbBlockReference> spBlkRef(id,McDb::kForRead); if(spBlkRef.openStatus() != Mcad::eOk) continue; // 读取块引用的属性。 AcDbObjectIterator* pAttribIter = spBlkRef->attributeIterator(); if(pAttribIter != NULL) { acutPrintf(_T("n 块的属性值为:")); for(;!pAttribIter->done();pAttribIter->step() ) { AcDbObjectPointer<AcDbAttribute> spAttribute(pAttribIter->objectId(),AcDb::kForRead); acutPrintf(_T("n %s"),spAttribute->textStringConst()); } acutPrintf(_T("n")); delete pAttribIter; } } delete pIterator;