ObjectARX代码片段
转载:http://www.handytech.cn/blog/article_67.htm 一 在ARX中禁用AutoCAD的某个命令 以LINE命令为例,在程序中加入下面的一句即可禁用LINE命令: acedCommand(RTSTR, "undefine", RTSTR, "line", RTNONE); 下面的语句则可恢复LINE命令的定义: acedCommand(RTSTR, "redefine", RTSTR, "line", RTNONE); 二 在对话框中预览DWG文件 使用acdbDisplayPreviewFromDwg函数,具体的方法为: char fileName[100]; strcpy(fileName, "C:\\test.dwg"); bool es; HWND pWnd; CFrameWnd *pFrame = (CFrameWnd*)GetDlgItem(IDC_PICTURE); es = acdbDisplayPreviewFromDwg(fileName, pFrame->m_hWnd); 上面的代码将在一个Picture控件中显示指定的图形。 另外,需要包含“dbmain.h”头文件。 四 获得当前数据库 在ARX编程中,经常需要使用当前数据库,例如需要获得当前图形中设置的文字样式、标注样式等。 要获得当前数据库,都可以直接使用下面的方法: AcDbTextStyleTable *pTextStyleTAble; AcDbObjectId textstyleId; textstyleId=acdbHostApplicationServices()->workingDatabase()->textstyle(); 如果用acadCurDwg来代替acdbHostApplicationServices()->workingDatabase(),也能得到同样的结果。 六 在ARX打开文件 在AutoCAD中打开图形,并且显示在图形窗口中,可以使用acedSyncFileOpen()函数。需要注意的是,这个函数只能在单文档模式中工作, 用户可以在AutoCAD“选项”对话框的“系统”选项卡中进行设置,或者在主函数中添加下面的语句: acrxDynamicLinker->registerAppNotMDIAware(pkt); 具体的函数如下: //加载模板文件 void LoadTemplate() { char fname[50]; strcpy(fname,"E:\\TMCAD\\TMCADtukuang\\A3.DWG"); #ifndef _ACAD2000 Acad::ErrorStatuses; es = acedSyncFileOpen(fname); #else acDocManager->appContextOpenDocument(fname); #endif }下面的方法则打开指定位置的DWG文件。 void OpenDoc( void *pData) { AcApDocument* pDoc = acDocManager->curDocument(); if (acDocManager->isApplicationContext()) { acDocManager->appContextOpenDocument((const char *)pData); } else { acutPrintf("\nERROR To Open Doc!\n"); } } // This is command 'OPEN1' void ZffOPENopen1() { // 直接打开系统中存在的某个图形文件G:\AutoCAD图形\wen2.dwg static char pData[] = "G:\\AutoCAD图形\\wen2.dwg"; acDocManager->executeInApplicationContext(OpenDoc, (void *)pData); } // Function name : SetCurLayer // Description : 设置当前层 // Return type : Acad::ErrorStatus // Argument : const char* lpLayerName // Argument : AcDbDatabase* pDb Acad::ErrorStatus SetCurLayer( const char * lpLayerName, AcDbDatabase * pDb ) { AcDbDatabase * pCurDb = pDb; if (pCurDb == NULL) pCurDb = acdbHostApplicationServices() -> workingDatabase(); AcDbLayerTableRecordPointer spRecord(lpLayerName, pCurDb, AcDb::kForRead); Acad::ErrorStatus es = spRecord.openStatus(); if (es == Acad::eOk) { es = pCurDb -> setClayer(spRecord -> objectId()); } return es; } //功能描述:选择指定图层上的所有实体! // 函数名称:selectEntityInLayer // 函数类型:Acad::ErrorStatus // 返回值: 正常:Acad::eOk // 功能描述:选择指定图层上的所有实体! // 函数作者:Darcy // 创建日期:200X-XX-XX // 参数列表: // 变量名:nLayerName 变量类型:const char* 变量说明:(输入)图层名 // 变量名:nIDs 变量类型:AcDbObjectIdArray& 变量说明:(输出)图层中实体的对象标识符集合 Acad::ErrorStatus selectEntityInLayer( const char * nLayerName,AcDbObjectIdArray & nIDs) { Acad::ErrorStatus es = Acad::eOk; ads_name ents; struct resbuf * rb; rb = acutNewRb(AcDb::kDxfLayerName); rb -> restype = 8 ; rb -> resval.rstring = ( char * )nLayerName; rb -> rbnext = NULL; acedSSGet( " X " ,NULL,NULL,rb,ents); long entNums = 0 ; acedSSLength(ents, & entNums); if (entNums == 0 ) es = Acad::eInvalidInput; else { for ( long a = 0 ; a < entNums ; a ++ ) { AcDbObjectId objId; ads_name ent; acedSSName(ents,a,ent); acdbGetObjectId(objId, ent); nIDs.append(objId); } } acedSSFree(ents); acutRelRb(rb); return es; } 生成新组 //生成新组(sGroupName) //追加数组中所有实体到该组中 //组名字 , Id数组 int createGroup(CString sGroupName, const AcDbObjectIdArray *idArr) { AcDbGroup *pGroup = new AcDbGroup((LPSTR)(LPCTSTR)sGroupName); AcDbObjectId groupObjectId; AcDbDictionary *pGroupDict = NULL; acdbHostApplicationServices()->workingDatabase() ->getGroupDictionary(pGroupDict, AcDb::kForWrite); pGroupDict->setAt(sGroupName, pGroup, groupObjectId); pGroupDict->close(); pGroup->close(); acdbOpenObject(pGroup, groupObjectId, AcDb::kForWrite); for (int i = 0; i < idArr->length(); i++) { groupObjectId = idArr->at(i); pGroup->append(groupObjectId); } pGroup->close(); return TRUE; } 功能:新建一个图层 参数:LayerName -- 图层名,LayerColor -- 颜色名 返回:图层ID //========================================================== AcDbObjectId CreateNewLayer(CString LayerName, AcCmColor LayerColor) { // 获得当前图形数据库的符号表 AcDbLayerTable *pLayerTable; acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLayerTable, AcDb::kForWrite); // 生成新的图层表记录 AcDbLayerTableRecord *pLayerTableRecord = new AcDbLayerTableRecord; pLayerTableRecord->setName(LayerName); // 设置图层名 pLayerTableRecord->setColor(LayerColor); // 设置图层颜色 AcDbObjectId layerId; pLayerTable->add(layerId,pLayerTableRecord); // 关闭图层表和图层表记录 pLayerTable->close(); pLayerTableRecord->close(); return layerId; } //========================================================== 功能:在指定图层上新建一条直线 参数:见注释 返回:直线ID //========================================================== AcDbObjectId CreateLine( double x1,double y1,double z1, // 起点坐标 double x2,double y2,double z2, // 终点坐标 AcDbObjectId layer) // 直线所在图层 { AcGePoint3d StartPt(x1,y1,z1); // 起点 AcGePoint3d EndPt(x2,y2,z2); // 终点 AcDbLine *pLine = new AcDbLine(StartPt,EndPt); // 获得当前图形数据库的符号表 AcDbBlockTable *pBlockTable; acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBlockTable, AcDb::kForRead); // 获得符号表中的模型空间块表记录指针,用于添加对象 AcDbBlockTableRecord *pBlockTableRecord; pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite); pBlockTable->close(); // 将直线添加到模型空间块表记录中 AcDbObjectId lineId; pLine->setLayer(layer,Adesk::kTrue); // 设置直线所在图层 pBlockTableRecord->appendAcDbEntity(lineId,pLine); // 关闭块表记录指针和直线指针 pBlockTableRecord->close(); pLine->close(); // 返回直线ID号 return lineId; } //函数名称:getPointAtDistInGeCurve //函数类型:Acad::ErrorStatus //返回值: //功能描述:返回曲线上距起点某距离值处的点。 //变量名:pGeCurve 变量类型:const AcGeCurve3d * 变量说明: //变量名:dist 变量类型:double 变量说明: //变量名:point 变量类型:AcGePoint3d& 变量说明: //备 注: Acad::ErrorStatus getPointAtDistInGeCurve(const AcGeCurve3d * pGeCurve,double dist,AcGePoint3d& point) { Acad::ErrorStatus es = Acad::eOk; if ( pGeCurve != NULL ) { AcGePoint3d spt; double pa=0.,datumParam=0.; //距离从起点起算! Adesk::Boolean posParamDir=Adesk::kTrue; pGeCurve->hasStartPoint(spt); datumParam = pGeCurve->paramOf(spt);; pa = pGeCurve->paramAtLength( datumParam, dist, posParamDir ); point=pGeCurve->evalPoint(pa); } else es = Acad::eInvalidInput; return es; } 判断点是否在圆弧上 BOOL IsAtArc(CAD_POINT firstPt,CAD_POINT secondPt, double radius,double direct,int More,CAD_POINT thePt) { CAD_POINT centerPt,sectionPt; CAD_POINT startPt,endPt; double startAngle,endAngle,chordAngle,vertAngle; double intLine,chordLine; sectionPt.x = (firstPt.x + secondPt.x)/2; sectionPt.y = (firstPt.y + secondPt.y)/2; chordLine = sqrt( pow( (secondPt.x-firstPt.x),2 ) + pow( (secondPt.y-firstPt.y),2 ) ); intLine = sqrt((radius*radius-chordLine*chordLine/4) ); chordAngle = ads_angle(asDblArray(firstPt),asDblArray(secondPt)); //弦线的角度 if(direct>=0)//左偏 { startPt=firstPt; endPt=secondPt; vertAngle=chordAngle+Pai/2; } else if(direct<0)//右偏 { startPt=secondPt; endPt=firstPt; vertAngle=chordAngle-Pai/2; } if(More<=0)//小圆弧 { centerPt.x=sectionPt.x+intLine*cos(vertAngle); centerPt.y=sectionPt.y+intLine*sin(vertAngle); } else//大圆弧 { centerPt.x=sectionPt.x-intLine*cos(vertAngle); centerPt.y=sectionPt.y-intLine*sin(vertAngle); } if(fabs(centerPt.distanceTo(thePt)-radius)>1.0E-8) return FALSE; startAngle = ads_angle(asDblArray(centerPt),asDblArray(startPt)); endAngle = ads_angle(asDblArray(centerPt),asDblArray(endPt)); AcDbArc *pArc=new AcDbArc(centerPt,radius,startAngle,endAngle); AcDbLine *pLine=new AcDbLine(centerPt,thePt); AcGePoint3dArray Points; pLine->intersectWith(pArc,AcDb::kOnBothOperands,Points); if(Points.isEmpty()) return FALSE; return TRUE; }查看完整版本: ObjectARX代码片段
Tags: