static AcDbObjectId LoadEntity(AcDbEntity* entity)
{
AcDbBlockTable* pBlockTable;
acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable,AcDb::kForRead);
AcDbBlockTableRecord* pBlockTableRecord;
pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite);
AcDbObjectId Id;
pBlockTableRecord->appendAcDbEntity(Id,entity);
pBlockTable->close();
pBlockTableRecord->close();
entity->close();
return Id;
}
////是否是自由模式,返回样本实体所在的层(非自由选择模式)
static bool IsFreeMode(CString& strLayer)
{
////样本
ads_name sName;
ads_point sPt;
acedInitGet(RSG_NONULL,_T("F"));
int ret = acedEntSel(_T("\n选择样本曲线[自由选择(F)]<回车自由选择>:"),sName,sPt);
bool isFree = false;////是否自由选择
acutPrintf(_T("ret is %d\n"),ret);
////只要没选实体,都是自由模式
if(ret == RTKWORD || ret == RTERROR)
{
/*CString kword ;
acedGetInput(kword.GetBuffer(1));
kword.ReleaseBuffer();
if(kword.CompareNoCase(_T("F")) == 0)
{
isFree = true;
}*/
isFree = true;
}
else if(ret == RTNORM)
{
isFree = false;
AcDbObjectId entId;
if(Acad::eOk != acdbGetObjectId(entId,sName))
{
acutPrintf(_T("\n获取样本实体ID失败"));
return isFree;
}
AcDbEntity* pEnt;
if(Acad::eOk != acdbOpenObject(pEnt,entId,AcDb::OpenMode::kForRead))
{
acutPrintf(_T("\n打开样本实体失败"));
return isFree;
}
////获取层名称
strLayer = pEnt->layer();
pEnt->close();
}
return isFree;
}
static void GetAreaLen(double& sumLen,double& sumArea,long& lineNum,const int& mode)
{
CString strMode ;
bool isFree = false;
CString strLayer;
isFree = IsFreeMode(strLayer);
ads_name ssName;
resbuf* strFilter = NULL;
////根据选择的模式构造过滤器字符串
if(isFree)
{
acutPrintf(_T("\n您选择了自由模式\n"));
strFilter = acutBuildList(-4,_T("<or"),
RTDXF0,_T("LINE"),//直线
RTDXF0,_T("ARC"),//圆弧
RTDXF0,_T("LWPOLYLINE"),//轻量多段线
RTDXF0,_T("POLYLINE"),//二维多段线
RTDXF0,_T("SPLINE"),//样条曲线
RTDXF0,_T("CIRCLE"),////圆
RTDXF0,_T("ELLIPSE"),////圆
-4,_T("or>"),RTNONE);
}
////加上样本曲线所在的层进行过滤
else
{
acutPrintf(_T("\n您选择了样本模式"));
acutPrintf(_T("\n根据图层%s进行过滤。"),strLayer);
strFilter = acutBuildList(-4,_T("<and"),
-4,_T("<or"),
RTDXF0,_T("LINE"),//直线
RTDXF0,_T("ARC"),//圆弧
RTDXF0,_T("LWPOLYLINE"),//轻量多段线
RTDXF0,_T("POLYLINE"),//二维多段线
RTDXF0,_T("SPLINE"),//样条曲线
RTDXF0,_T("CIRCLE"),////圆
RTDXF0,_T("ELLIPSE"),////圆
-4,_T("or>"),
8,strLayer,////样本所在的层名称
-4,_T("and>"),
RTNONE);
}
if(mode == 1)////计算长度
{
acutPrintf(_T("\n选择要计算长度的曲线:"));
}
else
{
acutPrintf(_T("\n选择要计算面积的曲线:"));
}
////根据过滤条件选择实体
if(acedSSGet(NULL,NULL,NULL,strFilter,ssName) != RTNORM)
{
acutPrintf(_T("\n未找到符合条件的实体"));
return;
}
long len;
acedSSLength(ssName,&len);
lineNum = len;////赋值实体数量
AcDbEntity* pEnt = NULL;
AcDbCurve* pCur = NULL;
ads_name en;////每个实体名称
double param = 0.0;
double dist = 0.0;//长度
double area = 0.0;//面积
for(int i = 0;i < len; i++)
{
if(acedSSName(ssName,i,en) != RTNORM)
{
acutPrintf(_T("\n获取第%d个实体名称出错"),i);
acutRelRb(strFilter);
acedSSFree(ssName);
return;
}
AcDbObjectId enId;
if(Acad::eOk != acdbGetObjectId(enId,en))
{
acutPrintf(_T("\n获取第%d个实体ID出错"),i);
acutRelRb(strFilter);
acedSSFree(ssName);
return;
}
if(Acad::eOk != acdbOpenObject(pEnt,enId,AcDb::OpenMode::kForRead))
{
acutPrintf(_T("\n获取第%d个实体ID出错"),i);
acutRelRb(strFilter);
acedSSFree(ssName);
return;
}
if(!pEnt->isKindOf(AcDbCurve::desc()))
{
acutPrintf(_T("\n发现了不是曲线类型的实体,程序退出"));
pEnt->close();
acutRelRb(strFilter);
acedSSFree(ssName);
return;
}
pCur = (AcDbCurve*)pEnt;
pCur->getEndParam(param);
pCur->getDistAtParam(param,dist);
pCur->getArea(area);
sumLen += dist;
sumArea += area;
acutPrintf(_T("\n第%d个曲线的长度是%.2f,面积是%.2f"),i,dist,area);
pCur->close();
}
acutRelRb(strFilter);
acedSSFree(ssName);
}
static void TESTsumarea()
{
double len = 0.0;
double area = 0.0;
long lineN = 0;
GetAreaLen(len,area,lineN,0);
acutPrintf(_T("\n共选择:%ld个曲线,总面积:%.2f。"),lineN,area);
}
static void TESTsumlen()
{
double len = 0.0;
double area = 0.0;
long lineN = 0;
GetAreaLen(len,area,lineN,0);
acutPrintf(_T("\n共选择:%ld个曲线,总长度:%.2f。"),lineN,len);
}