找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 430|回复: 1

[求助] 为啥选择到的实体数量是0个

[复制链接]

已领礼包: 2个

财富等级: 恭喜发财

发表于 2020-8-24 10:47:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
各位大神:
这段代码来自网上的例子,为啥我执行的时候结果永远是选择了0个实体?

  1. void MyTest::test(void)
  2. {
  3.         ads_name ent;     
  4.         ads_point pt;      
  5.         if (acedEntSel(_T("/n请选择对象:"), ent, pt) != RTNORM)
  6.         { return; }   
  7.         AcDbObjectId entId;      
  8.         acdbGetObjectId(entId, ent);      
  9.         AcDbEntity* pEnt = NULL;      
  10.         acdbOpenObject(pEnt, entId, AcDb::kForWrite);  
  11.         if (pEnt->isKindOf(AcDbPolyline::desc()))
  12.         {
  13.                 AcDbPolyline* pPoly = AcDbPolyline::cast(pEnt);
  14.                
  15.                 AcDbObjectIdArray ObjectIdArray; // 选择到的实体ID集合
  16.                 MyTest::SelectEntInPoly(pPoly, ObjectIdArray, "CP", 1);
  17.                 acutPrintf(L"\n选择到%d个实体.", ObjectIdArray.length());
  18.         }
  19.         pEnt->close();
  20.        
  21.        
  22. }

  23. bool MyTest::SelectEntInPoly(AcDbPolyline* pPline, AcDbObjectIdArray& ObjectIdArray,
  24.                                                  const char* selectMode, double approxEps)
  25. {
  26.          // 判断selectMode的有效性
  27.          if (strcmp(selectMode, "CP") != 0 && strcmp(selectMode, "WP") != 0)
  28.          {

  29.                  acedAlert(L"函数SelectEntInPline中,指定了无效的选择模式!");
  30.                  return false;
  31.          }
  32.          // 清除数组中所有的ObjectId
  33.          for (int i = 0; i < ObjectIdArray.length(); i++)
  34.          {
  35.                  ObjectIdArray.removeAt(i);
  36.          }
  37.          
  38.          AcGeCurve2d* pGeCurve;// 多段线对应的几何曲线
  39.          Adesk::Boolean bClosed = pPline->isClosed(); // 多段线是否闭合
  40.          if (bClosed != Adesk::kTrue) // 确保多段线作为选择边界时是闭合的
  41.          {
  42.                  pPline->setClosed(!bClosed);
  43.          }
  44.          // 创建对应的几何类曲线
  45.          MyTest::PolyToGeCurve(pPline, pGeCurve);
  46.          // 获得几何曲线的样本点
  47.          AcGePoint2dArray SamplePtArray; // 存储曲线的样本点
  48.          AcGeDoubleArray ParamArray; // 存储样本点对应的参数值

  49.          AcGePoint2d ptStart, ptEnd; // 几何曲线的起点和终点
  50.          Adesk::Boolean bRet = pGeCurve->hasStartPoint(ptStart);
  51.          bRet = pGeCurve->hasEndPoint(ptEnd);

  52.          double valueSt = pGeCurve->paramOf(ptStart);
  53.          double valueEn = pGeCurve->paramOf(ptEnd);

  54.          pGeCurve->getSamplePoints(valueSt, valueEn, approxEps,
  55.                  SamplePtArray, ParamArray);
  56.          delete pGeCurve; // 在函数PolyToGeCurve中分配了内存
  57.          // 确保样本点的起点和终点不重合
  58.          AcGeTol tol;
  59.          tol.setEqualPoint(0.01);
  60.          AcGePoint2d ptFirst = SamplePtArray[0];
  61.          AcGePoint2d ptLast = SamplePtArray[SamplePtArray.length() - 1];
  62.          if (ptFirst.isEqualTo(ptLast))
  63.          {
  64.                  SamplePtArray.removeLast();
  65.          }
  66.          // 根据样本点创建结果缓冲区链表
  67.          struct resbuf* rb;
  68.          rb = MyTest::BuildRbFromPtArray(SamplePtArray);
  69.          // 使用acedSSGet函数创建选择集
  70.          ads_name ssName; // 选择集名称
  71.        
  72.          ACHAR* selectMode1 = Convert::ConvertCharPtrToAcharPtr(selectMode);         
  73.          //int rt = acedSSGet(selectMode, rb, NULL, NULL, ssName);
  74.          int rt = acedSSGet(selectMode1,NULL, NULL,   rb, ssName);
  75.          
  76.          if (rt != RTNORM)
  77.          {
  78.                  MessageBox(NULL, L"在这里停止", L"在这里停止!!!", MB_OKCANCEL);
  79.                  acutRelRb(rb); // 释放结果缓冲区链表
  80.                  return false;
  81.          }
  82.          else {
  83.                  MessageBox(NULL, L"这里没执行", L"这里没执行!!!", MB_OKCANCEL);
  84.          }
  85.          MessageBox(NULL, L"这里也没执行", L"这里也没执行!!!", MB_OKCANCEL);
  86.          // 将选择集中所有的对象添加到ObjectIdArray
  87.          //long length;
  88.          Adesk::Int32  length;
  89.          acedSSLength(ssName, &length);
  90.          for (int i = 0; i < length; i++)
  91.          {
  92.                
  93.                  // 获得指定元素的ObjectId
  94.                  ads_name ent;
  95.                  acedSSName(ssName, i, ent);
  96.                  AcDbObjectId objId;
  97.                  acdbGetObjectId(objId, ent);
  98.                  //获得指向当前元素的指针
  99.                  AcDbEntity* pEnt;
  100.                  Acad::ErrorStatus es = acdbOpenAcDbEntity(pEnt, objId, AcDb::kForRead);
  101.                  //选择到作为边界的多段线了,直接跳过该次循环
  102.                  if (es == Acad::eWasOpenForWrite)
  103.                  {
  104.                          continue;
  105.                  }
  106.                
  107.                  ObjectIdArray.append(pEnt->objectId());
  108.                  pEnt->close();
  109.          }
  110.          // 释放内存
  111.          acutRelRb(rb);// 释放结果缓冲区链表
  112.          acedSSFree(ssName); // 删除选择集
  113.          return true;
  114. }

  115. //bool MyTest::PolyToGeCurve(const AcDbPolyline*& pPline, AcGeCurve2d*& pGeCurve)
  116. bool MyTest::PolyToGeCurve(const AcDbPolyline* pPline, AcGeCurve2d*& pGeCurve)
  117. {
  118.          int nSegs; // 多段线的段数
  119.          AcGeLineSeg2d line, * pLine; // 几何曲线的直线段部分
  120.          AcGeCircArc2d arc, * pArc; // 几何曲线的圆弧部分
  121.          AcGeVoidPointerArray geCurves; // 指向组成几何曲线各分段的指针数组
  122.          nSegs = pPline->numVerts() - 1;
  123.          //根据多段线创建对应的分段几何曲线
  124.          for (int i = 0; i < nSegs; i++)
  125.          {
  126.                  if (pPline->segType(i) == AcDbPolyline::kLine)
  127.                  {
  128.                          pPline->getLineSegAt(i, line);
  129.                          pLine = new AcGeLineSeg2d(line);
  130.                          geCurves.append(pLine);
  131.                  }
  132.                  else if (pPline->segType(i) == AcDbPolyline::kArc)
  133.                  {
  134.                          pPline->getArcSegAt(i, arc);
  135.                          pArc = new AcGeCircArc2d(arc);
  136.                          geCurves.append(pArc);
  137.                  }
  138.          }
  139.          // 处理闭合多段线最后一段是圆弧的情况
  140.          if (pPline->isClosed() && pPline->segType(nSegs) == AcDbPolyline::kArc)
  141.             {
  142.                  pPline->getArcSegAt(nSegs, arc);
  143.                  pArc = new AcGeCircArc2d(arc);
  144.                  pArc->setAngles(arc.startAng(), arc.endAng() - (arc.endAng() - arc.startAng()) / 100);                         
  145.                  geCurves.append(pArc);
  146.             }
  147.          // 根据分段的几何曲线创建对应的复合曲线
  148.          if (geCurves.length() == 1)
  149.             {
  150.                  pGeCurve = (AcGeCurve2d*)geCurves[0];
  151.             }
  152.          else
  153.             {
  154.                  pGeCurve = new AcGeCompositeCurve2d(geCurves);
  155.             }
  156.          // 释放动态分配的内存
  157.          if (geCurves.length() > 1)
  158.          {
  159.                  for (INT i = 0; i < geCurves.length(); i++)
  160.                  {
  161.                          delete geCurves;
  162.                  }
  163.          }
  164.          return true;
  165. }


  166. struct resbuf* MyTest::BuildRbFromPtArray(const AcGePoint2dArray& arrPoints)
  167. {
  168.          struct resbuf* retRb = NULL;
  169.          int count = arrPoints.length();
  170.          if (count <= 1)
  171.          {
  172.                  acedAlert(L"函数BuildBbFromPtArray中,点数组包含元素个数不足!");
  173.                  return retRb;
  174.          }
  175.          // 使用第一个点来构建结果缓冲区链表的头节点
  176.          ads_point adsPt;
  177.          adsPt[X] = arrPoints[0].x;
  178.          adsPt[Y] = arrPoints[0].y;
  179.          retRb = acutBuildList(RTPOINT, adsPt, RTNONE);
  180.          struct resbuf* nextRb = retRb; // 辅助指针
  181.          for (int i = 1; i < count; i++) // 注意:不考虑第一个元素,因此i从1开始
  182.          {
  183.                  adsPt[X] = arrPoints.x;
  184.                  adsPt[Y] = arrPoints.y;
  185.                  // 动态创建新的节点,并将其链接到原来的链表尾部
  186.                  nextRb->rbnext = acutBuildList(RTPOINT, adsPt, RTNONE);
  187.                  nextRb = nextRb->rbnext;
  188.                
  189.          }
  190.          return retRb;
  191. }
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 4365个

财富等级: 富可敌国

发表于 2020-8-25 00:12:48 | 显示全部楼层
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-5-26 03:56 , Processed in 0.164141 second(s), 29 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表