2012-04-13 22:36:00|?次阅读|上传:wustguangh【已有?条评论】发表评论
关键词:ObjectArx, C/C++, AutoCAD|来源:唯设编程网
//加载图层到图形数据库 void CLayerOperator::LoadLayer(const LayerInfo& layerInfo, AcDbDatabase* pDb){ //获得层表指针 AcDbLayerTable *pLayerTbl; AcDbLayerTableRecord *pLayerTblRcd; pDb->getLayerTable(pLayerTbl,AcDb::kForWrite); //1.创建新的层表记录,或者打开已经存在的层表表记录 AcDbObjectId layerTblRcdId; if(pLayerTbl->has(layerInfo.layerName.c_str())){ pLayerTbl->getAt(layerInfo.layerName.c_str(),layerTblRcdId); }else{ pLayerTblRcd=new AcDbLayerTableRecord(); pLayerTblRcd->setName(layerInfo.layerName.c_str()); pLayerTbl->add(layerTblRcdId,pLayerTblRcd); pLayerTblRcd->close(); } acdbOpenObject(pLayerTblRcd,layerTblRcdId,AcDb::kForWrite); //2.设置层表记录的颜色 AcCmColor color; Adesk::UInt16 colorIndex=layerInfo.colorIndex; color.setColorIndex(colorIndex); pLayerTblRcd->setColor(color); //3.设置线型 AcDbObjectId lineTypeId; lineTypeId=LoadLineType(layerInfo.typeName.c_str()); pLayerTblRcd->setLinetypeObjectId(lineTypeId); //4.设置线宽 pLayerTblRcd->setLineWeight(layerInfo.weight); pLayerTblRcd->close(); pLayerTbl->close(); }
读者可以使用ObjectArx的AddIn工具栏添加测试命令,下面是对图层操作类CLayerOperator进行测试的相关代码,前面的文章已经详细介绍了添加命令函数的方法,本文不再赘述。
// - CHAP4._NewLayer command (do not rename) static void CHAP4_NewLayer(void) { //提示用户输入新建图层的名称 ACHAR layerName[BUFSIZ]={0}; if(acedGetString(Adesk::kFalse,_T(" 输入新图层的名称:"), layerName)!=RTNORM){ return; } CLayerOperator::NewLayer(layerName); } // - CHAP4._LayerColor command (do not rename) static void CHAP4_LayerColor(void) { //提示用户输入要修改的图层名称 ACHAR layerName[BUFSIZ]; if(acedGetString(Adesk::kFalse,_T(" 输入图层的名称:"), layerName)!=RTNORM){ return; } CLayerOperator::SetLayerColor(layerName); } // - CHAP4._DelLayer command (do not rename) static void CHAP4_DelLayer(void) { //提示用户输入要修改的图层名称 ACHAR layerName[BUFSIZ]; if(acedGetString(Adesk::kFalse,_T(" 输入图层的名称:"), layerName)!=RTNORM){ return; } CLayerOperator::DelLayer(layerName); } // - CHAP4._ExportLayer command (do not rename) static void CHAP4_ExportLayer(void) { CFileDialog dlgFile(FALSE, _T(".lay"), _T("layer"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("layer files(*.lay)|*.lay||")); if(dlgFile.DoModal()==IDOK){ CString flilePath=dlgFile.GetPathName(); CLayerOperator::ExportLayers(flilePath.GetBuffer()); flilePath.ReleaseBuffer(); } } // - CHAP4._ImportLayer command (do not rename) static void CHAP4_ImportLayer(void) { CFileDialog dlgFile(TRUE, _T(".lay"), _T("layer"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("layer files(*.lay)|*.lay||")); if(dlgFile.DoModal()==IDOK){ CString flilePath=dlgFile.GetPathName(); CLayerOperator::ImportLayers(flilePath.GetBuffer()); flilePath.ReleaseBuffer(); } } // - CHAP4._LoadLineType command (do not rename) static void CHAP4_LoadLineType(void) { //提示用户输入要修改的图层名称 ACHAR lineType[BUFSIZ]; if(acedGetString(Adesk::kFalse,_T(" 输入线型的名称:"), lineType)!=RTNORM){ return; } CLayerOperator::LoadLineType(lineType); } };
好了,使用ObjectArx2010对AutoCAD2010图层进行操作就介绍完了,本程序的所有代码在Win7+VC2008 SP1+ObjectArx2010+AutoCAD2010编译并运行通过,如果你还有补充和意见,欢迎在留言区进行讨论。