- UID
- 803046
- 积分
- 11
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2020-8-12
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
各位大神:
这段代码来自网上的例子,为啥我执行的时候结果永远是选择了0个实体?
- void MyTest::test(void)
- {
- ads_name ent;
- ads_point pt;
- if (acedEntSel(_T("/n请选择对象:"), ent, pt) != RTNORM)
- { return; }
- AcDbObjectId entId;
- acdbGetObjectId(entId, ent);
- AcDbEntity* pEnt = NULL;
- acdbOpenObject(pEnt, entId, AcDb::kForWrite);
- if (pEnt->isKindOf(AcDbPolyline::desc()))
- {
- AcDbPolyline* pPoly = AcDbPolyline::cast(pEnt);
-
- AcDbObjectIdArray ObjectIdArray; // 选择到的实体ID集合
- MyTest::SelectEntInPoly(pPoly, ObjectIdArray, "CP", 1);
- acutPrintf(L"\n选择到%d个实体.", ObjectIdArray.length());
- }
- pEnt->close();
-
-
- }
- bool MyTest::SelectEntInPoly(AcDbPolyline* pPline, AcDbObjectIdArray& ObjectIdArray,
- const char* selectMode, double approxEps)
- {
- // 判断selectMode的有效性
- if (strcmp(selectMode, "CP") != 0 && strcmp(selectMode, "WP") != 0)
- {
- acedAlert(L"函数SelectEntInPline中,指定了无效的选择模式!");
- return false;
- }
- // 清除数组中所有的ObjectId
- for (int i = 0; i < ObjectIdArray.length(); i++)
- {
- ObjectIdArray.removeAt(i);
- }
-
- AcGeCurve2d* pGeCurve;// 多段线对应的几何曲线
- Adesk::Boolean bClosed = pPline->isClosed(); // 多段线是否闭合
- if (bClosed != Adesk::kTrue) // 确保多段线作为选择边界时是闭合的
- {
- pPline->setClosed(!bClosed);
- }
- // 创建对应的几何类曲线
- MyTest::PolyToGeCurve(pPline, pGeCurve);
- // 获得几何曲线的样本点
- AcGePoint2dArray SamplePtArray; // 存储曲线的样本点
- AcGeDoubleArray ParamArray; // 存储样本点对应的参数值
- AcGePoint2d ptStart, ptEnd; // 几何曲线的起点和终点
- Adesk::Boolean bRet = pGeCurve->hasStartPoint(ptStart);
- bRet = pGeCurve->hasEndPoint(ptEnd);
- double valueSt = pGeCurve->paramOf(ptStart);
- double valueEn = pGeCurve->paramOf(ptEnd);
- pGeCurve->getSamplePoints(valueSt, valueEn, approxEps,
- SamplePtArray, ParamArray);
- delete pGeCurve; // 在函数PolyToGeCurve中分配了内存
- // 确保样本点的起点和终点不重合
- AcGeTol tol;
- tol.setEqualPoint(0.01);
- AcGePoint2d ptFirst = SamplePtArray[0];
- AcGePoint2d ptLast = SamplePtArray[SamplePtArray.length() - 1];
- if (ptFirst.isEqualTo(ptLast))
- {
- SamplePtArray.removeLast();
- }
- // 根据样本点创建结果缓冲区链表
- struct resbuf* rb;
- rb = MyTest::BuildRbFromPtArray(SamplePtArray);
- // 使用acedSSGet函数创建选择集
- ads_name ssName; // 选择集名称
-
- ACHAR* selectMode1 = Convert::ConvertCharPtrToAcharPtr(selectMode);
- //int rt = acedSSGet(selectMode, rb, NULL, NULL, ssName);
- int rt = acedSSGet(selectMode1,NULL, NULL, rb, ssName);
-
- if (rt != RTNORM)
- {
- MessageBox(NULL, L"在这里停止", L"在这里停止!!!", MB_OKCANCEL);
- acutRelRb(rb); // 释放结果缓冲区链表
- return false;
- }
- else {
- MessageBox(NULL, L"这里没执行", L"这里没执行!!!", MB_OKCANCEL);
- }
- MessageBox(NULL, L"这里也没执行", L"这里也没执行!!!", MB_OKCANCEL);
- // 将选择集中所有的对象添加到ObjectIdArray
- //long length;
- Adesk::Int32 length;
- acedSSLength(ssName, &length);
- for (int i = 0; i < length; i++)
- {
-
- // 获得指定元素的ObjectId
- ads_name ent;
- acedSSName(ssName, i, ent);
- AcDbObjectId objId;
- acdbGetObjectId(objId, ent);
- //获得指向当前元素的指针
- AcDbEntity* pEnt;
- Acad::ErrorStatus es = acdbOpenAcDbEntity(pEnt, objId, AcDb::kForRead);
- //选择到作为边界的多段线了,直接跳过该次循环
- if (es == Acad::eWasOpenForWrite)
- {
- continue;
- }
-
- ObjectIdArray.append(pEnt->objectId());
- pEnt->close();
- }
- // 释放内存
- acutRelRb(rb);// 释放结果缓冲区链表
- acedSSFree(ssName); // 删除选择集
- return true;
- }
- //bool MyTest::PolyToGeCurve(const AcDbPolyline*& pPline, AcGeCurve2d*& pGeCurve)
- bool MyTest::PolyToGeCurve(const AcDbPolyline* pPline, AcGeCurve2d*& pGeCurve)
- {
- int nSegs; // 多段线的段数
- AcGeLineSeg2d line, * pLine; // 几何曲线的直线段部分
- AcGeCircArc2d arc, * pArc; // 几何曲线的圆弧部分
- AcGeVoidPointerArray geCurves; // 指向组成几何曲线各分段的指针数组
- nSegs = pPline->numVerts() - 1;
- //根据多段线创建对应的分段几何曲线
- for (int i = 0; i < nSegs; i++)
- {
- if (pPline->segType(i) == AcDbPolyline::kLine)
- {
- pPline->getLineSegAt(i, line);
- pLine = new AcGeLineSeg2d(line);
- geCurves.append(pLine);
- }
- else if (pPline->segType(i) == AcDbPolyline::kArc)
- {
- pPline->getArcSegAt(i, arc);
- pArc = new AcGeCircArc2d(arc);
- geCurves.append(pArc);
- }
- }
- // 处理闭合多段线最后一段是圆弧的情况
- if (pPline->isClosed() && pPline->segType(nSegs) == AcDbPolyline::kArc)
- {
- pPline->getArcSegAt(nSegs, arc);
- pArc = new AcGeCircArc2d(arc);
- pArc->setAngles(arc.startAng(), arc.endAng() - (arc.endAng() - arc.startAng()) / 100);
- geCurves.append(pArc);
- }
- // 根据分段的几何曲线创建对应的复合曲线
- if (geCurves.length() == 1)
- {
- pGeCurve = (AcGeCurve2d*)geCurves[0];
- }
- else
- {
- pGeCurve = new AcGeCompositeCurve2d(geCurves);
- }
- // 释放动态分配的内存
- if (geCurves.length() > 1)
- {
- for (INT i = 0; i < geCurves.length(); i++)
- {
- delete geCurves;
- }
- }
- return true;
- }
- struct resbuf* MyTest::BuildRbFromPtArray(const AcGePoint2dArray& arrPoints)
- {
- struct resbuf* retRb = NULL;
- int count = arrPoints.length();
- if (count <= 1)
- {
- acedAlert(L"函数BuildBbFromPtArray中,点数组包含元素个数不足!");
- return retRb;
- }
- // 使用第一个点来构建结果缓冲区链表的头节点
- ads_point adsPt;
- adsPt[X] = arrPoints[0].x;
- adsPt[Y] = arrPoints[0].y;
- retRb = acutBuildList(RTPOINT, adsPt, RTNONE);
- struct resbuf* nextRb = retRb; // 辅助指针
- for (int i = 1; i < count; i++) // 注意:不考虑第一个元素,因此i从1开始
- {
- adsPt[X] = arrPoints.x;
- adsPt[Y] = arrPoints.y;
- // 动态创建新的节点,并将其链接到原来的链表尾部
- nextRb->rbnext = acutBuildList(RTPOINT, adsPt, RTNONE);
- nextRb = nextRb->rbnext;
-
- }
- return retRb;
- }
-
|
|