评论

收藏

[C++] ObjectArxC++使用Xdata对自定义实体进行过滤筛选和批量操作

编程语言 编程语言 发布于:2021-08-10 14:23 | 阅读数:214 | 评论:0

扩充数据又分为Xdata(eXtension data)和 Xrecord(eXtension record)两种。Xdata有一个重要功能是可以对自定义实体进行过滤筛选,然后对筛选出来的数据进行批量操作。
使用Xdata对自定义实体进行批量操作过程如下。
主要思路是:
  • 创建自定义实体时增加扩展数据Xdata,同一种自定义实体可以添加不同的Xdata。
  • 批量处理时,根据Xdata扩展数据对数据进行过滤筛选。
代码示例如下:
  • 创建自定义实体时增加扩展数据Xdata
//zhaoanan 增加扩展数据Xdata
//将里程实体加入到块表记录中
            //CZnUtility::addRailSlope(cthRailSlope, "ZDK1+222.45", "ZDK2+123.11", "20", 20.00, cthRailSlope->CString2double("30"), "123", cthRailSlope->position().x + 20, cthRailSlope->position().x + 50, 1);
AcDbObjectId dimensionId = CDwgDatabaseUtil::PostToModelSpace(cthRailSlope, m_pDb);
AcDbEntity* pEnt=NULL;
if (mysys->OpenAcDbEntity(dimensionId,pEnt,AcDb::kForRead))
{
  CthRailSlope *pSlopeEnt = CthRailSlope::cast(pEnt);
  pSlopeEnt->upgradeOpen();
  //SlopeType=SlopeHead代表坡度头,SlopeType=SlopeBody代表坡度段
  CString mSlopeType = _T("SlopeHead");
  pSlopeEnt->setValue(_T("SlopeType"), mSlopeType);
  mysys->SetXdata(pSlopeEnt, mSlopeType, _T("CthRailSlope"));
  pSlopeEnt->downgradeOpen();
  //关闭对象
  if (pEnt != nullptr) {
    pEnt->close();
  }
}

//将里程实体加入到块表记录中
//cthRailSlope->AddRailSlope("ZDK1+222.45", "ZDK2+123.11", "20", 20.00, "30", cthRailSlope->position().x + 20, cthRailSlope->position().x + 50, 1);
AcDbObjectId pEntId = CDwgDatabaseUtil::PostToModelSpace(cthRailCurves, m_pDb);
//创建图层
CUserSystem mysys;
//打开编辑图元
AcDbEntity* pEnt=NULL;
if (mysys.OpenAcDbEntity(pEntId, pEnt, AcDb::kForRead))
{
  CthEntity *pthEnt=CthEntity::cast(pEnt);
  if (pthEnt != NULL)
  {
    pthEnt->upgradeOpen();
    //增加扩展数据到数据字典,坡度方向、坡度、长度、里程、竖曲线半径
    //设置类型和数值
    pthEnt->setValue(_T("限速"), m_vlmtv);
    pthEnt->setValue(_T("名称"), m_curves_name);
    pthEnt->setValue(_T("长度"), m_length);
    pthEnt->setValue(_T("半径"), m_radius);
    pthEnt->setValue(_T("左里程"), m_left_kmpost);
    pthEnt->setValue(_T("右里程"), m_right_kmpost);
    //格式化曲线长度,格式化为保留两位小数
    CString m_m_direction_str;
    m_m_direction_str.Format(_T("%d"),m_direction);
    pthEnt->setValue(_T("方向"), m_m_direction_str);
    //CurvesType=CurvesHead代表曲线头,CurvesType=CurvesBody代表曲线段 add by zhaoanan 20210809 begin
    CString mSlopeType = _T("CurvesBody");
    pthEnt->setValue(_T("CurvesType"), mSlopeType);
    mysys.SetXdata(pthEnt, mSlopeType, _T("CthRailCurves"));
    //CurvesType=CurvesHead代表曲线头,CurvesType=CurvesBody代表曲线段 add by zhaoanan 20210809 end
    pthEnt->downgradeOpen();
  }
}
批量处理时,根据Xdata扩展数据对数据进行过滤筛选
//zhaoanan 删除全图所有坡度段
// - zhfzndraw_thdeleteallslopes command (do not rename)
//删除全图所有坡度段
static void zhfzndraw_thdeleteallslopes(void)
{
    AcDbDatabase * m_pDb = acdbHostApplicationServices()->workingDatabase();
    //添加块定义
    acutPrintf(_T("\n删除全图所有坡度段")) ;
    CUserSystem *mysys = new CUserSystem();
    mysys->m_pDb = m_pDb;

    //声明左边插入点
    //struct resbuf *pRb ;
    //pRb = acutBuildList(RTDXF0,_T("CthRailSlope"),0) ;
    struct resbuf eb1,eb2,eb3;
    char sbuf1[128],sbuf2[128];

    eb1.restype=0;//实体类型
    strcpy(sbuf1, "CthRailSlope");
    eb1.resval.rstring=CZnUtility::ConvertCharPtrToAcharPtr(sbuf1);//实体为坡度段
    eb1.rbnext = &eb2;
    eb2.restype = -3;//使用扩展数据Xdata进行过滤
    eb2.rbnext = &eb3;
    eb3.restype = AcDb::kDxfRegAppName;//扩展数据中的字符串DXF码
    strcpy(sbuf2, "SlopeBody");
    eb3.resval.rstring=CZnUtility::ConvertCharPtrToAcharPtr(sbuf2); //扩展数据中的字符串
    eb3.rbnext=NULL;

    long iNum = 0 ;
    CLongArray iArrayHandle;
    iNum = mysys->GetSSEntHandle(_T("X"), NULL, NULL, &eb1, iArrayHandle) ;
    //acutRelRb(pRb) ;
    for (long i =0;i<iNum;i++)
    {        
        long handleDi = iArrayHandle.GetAt(i);
        AcDbEntity * pEnt = NULL ;
        CthEntity * pthEnt = NULL;
        if(mysys->OpenAcDbEntity(handleDi, pEnt, AcDb::kForRead))
        {
            //指定坡度端点
            if(pEnt->isKindOf(CthRailSlope::desc()))
            {
                pthEnt = CthRailSlope::cast(pEnt) ;
                pthEnt->upgradeOpen();
                try 
                {
                    /*- 3删除公里标对象*/
                    pthEnt->upgradeOpen();
                    pthEnt->erase();
                    pthEnt->downgradeOpen();
                    pthEnt->close();
                }
                catch (...)
                {
                    ads_printf(_T("\nunknown CException in zhfzndraw_thdeleteallslopes")) ;
                } 
            }
            if(pEnt!=NULL)
            {
                pEnt->close();
            }
        }
        //AcDbObjectId dimensionId = CDwgDatabaseUtil::PostToModelSpace(cthRailSlope, m_pDb);
    }
    acedUpdateDisplay();
    acutPrintf(_T("\n全图所有坡度段已删除!")) ;
    //释放mysys空间
    if (mysys != nullptr)
    {
        delete mysys;
    }
}

参考意见和网页地址:
  • https://blog.csdn.net/weixin_42366571/article/details/104229107
  • https://blog.csdn.net/weixin_42366571/article/details/104364849


关注下面的标签,发现更多相似文章