ObjectARX选择集快还是遍历块表记录获取实体objectid快?

这是根据拾取点来获取 点实体AcDbPoint 的结果,明显遍历更快。

虽然有点以偏概全,但对于条件固定的选择,使用遍历是个更高效率的选择。

 

 

 以下是测试源码。

	static void getpoint1()
	{
		ads_name ss;
		ads_point pt;
		if (RTNORM!=acedGetPoint(NULL,_T("\n选择点: "),pt))
		{
			return;
		}
		clock_t start,finish;
		double totaltime;
		start=clock();
		resbuf *rb=acutBuildList(RTDXF0,_T("point"),10,pt,RTNONE);
		if (RTNORM != acedSSGet(_T("X"),NULL,NULL,rb,ss))
		{
			acutRelRb(rb);
			acutPrintf(_T("\n未选择到对象!"));
			finish=clock();
			totaltime=(double)(finish-start)/CLOCKS_PER_SEC;    
			acutPrintf(_T("\n选择失败耗时: %0.9lf"),totaltime);
			return;
		}
		acutRelRb(rb);
		finish=clock();
		totaltime=(double)(finish-start)/CLOCKS_PER_SEC;    
		acutPrintf(_T("\n选择完成耗时: %0.9lf"),totaltime);
		Adesk::Int32 nsslen=0;
		acedSSLength(ss,&nsslen);
		AcDbObjectIdArray objIds;
		for (int i=0;i<nsslen;i++)
		{
			ads_name ent;
			acedSSName(ss,i,ent);
			AcDbObjectId objId;
			acdbGetObjectId(objId,ent);
			if (!objId.isNull())
			{
				objIds.append(objId);
			}
		}
		acedSSFree(ss);
		finish=clock();
		totaltime=(double)(finish-start)/CLOCKS_PER_SEC;    
		acutPrintf(_T("\n转ID集合耗时: %0.9lf"),totaltime);

		if (objIds.length()>0)
		{
			acutPrintf(_T("\n选择到%d个对象!"),objIds.length());
		}
	}
	static void getpoint2()
	{
		ads_point pt;
		if (RTNORM!=acedGetPoint(NULL,_T("\n选择点: "),pt))
		{
			return;
		}

		clock_t start,finish;
		double totaltime;
		start=clock();

		AcGePoint3d p1=asPnt3d(pt);
		AcDbBlockTablePointer pBlkTbl(curDoc()->database(),AcDb::kForRead);
		if (Acad::eOk != pBlkTbl.openStatus())
		{
			acutPrintf(_T("\n打开块表失败,错误码= %s"),acadErrorStatusText(pBlkTbl.openStatus()));
			return;
		}
		AcDbBlockTableIterator *pBlkTblIter=NULL;
		Acad::ErrorStatus es= pBlkTbl->newIterator(pBlkTblIter);
		if (pBlkTblIter==NULL)
		{
			acutPrintf(_T("\n打开块表失败,错误码= %s"),acadErrorStatusText(es));
			return;
		}
		AcDbObjectIdArray objIds;
		for (pBlkTblIter->start();!pBlkTblIter->done();pBlkTblIter->step())
		{
			AcDbBlockTableRecord *pBlkRcd=NULL;
			es= pBlkTblIter->getRecord(pBlkRcd,AcDb::kForRead);
			if (Acad::eOk != es)
			{
				continue;
			}
			if (!pBlkRcd->isLayout())
			{
				pBlkRcd->close();
				continue;
			}
			AcDbBlockTableRecordIterator *pBlkRcdIter=NULL;
			pBlkRcd->newIterator(pBlkRcdIter);
			if (pBlkRcdIter==NULL)
			{
				pBlkRcd->close();
				continue;
			}
			for (pBlkRcdIter->start();!pBlkRcdIter->done();pBlkRcdIter->step())
			{
				AcDbEntity *pEnt=NULL;
				pBlkRcdIter->getEntity(pEnt,AcDb::kForRead);
				if (pEnt == NULL)
				{
					continue;
				}
				if (pEnt->isKindOf(AcDbPoint::desc()))
				{
					AcDbPoint *pPoint=AcDbPoint::cast(pEnt);
					if (pPoint!=NULL)
					{
						AcGePoint3d ptTemp=pPoint->position();
						if (ptTemp.isEqualTo(p1))
						{
							objIds.append(pPoint->objectId());
						}
					}
				}
				pEnt->close();
			}
			delete pBlkRcdIter;
			pBlkRcd->close();
		}
		delete pBlkTblIter;

		finish=clock();
		totaltime=(double)(finish-start)/CLOCKS_PER_SEC;    
		acutPrintf(_T("\n遍历耗时: %0.9lf"),totaltime);

		if (objIds.length()>0)
		{
			acutPrintf(_T("\n选择到%d个对象!"),objIds.length());
		}
	}

  

posted @ 2020-10-24 16:19  edata  阅读(1178)  评论(0编辑  收藏  举报