|
void ZffCHAP2AddRegion()
{
// 使用选择集,提示用户选择作为面域边界的对象
ads_name ss;
int rt = acedSSGet(NULL, NULL, NULL, NULL, ss); // 提示用户
选择对象
AcDbObjectIdArray objIds;
// 根据选择集中的对象构建边界曲线的ID数组
if (rt == RTNORM)
{
long length;
acedSSLength(ss, &length); // 获得选择集中的对象个数
for (int i = 0; i < length; i++)
{
ads_name ent;
acedSSName(ss, i, ent);
AcDbObjectId objId;
acdbGetObjectId(objId, ent);
objIds.append(objId);
}
}
acedSSFree(ss); // 及时释放选择集
AcDbObjectIdArray regionIds;
regionIds = CCreateEnt::CreateRegion(objIds);
int number = regionIds.length();
if (number > 0)
{
acutPrintf("\n已经创建%d个面域!", number);
}
else
{
acutPrintf("\n创建0个面域!");
}
}
创建面域:
AcDbObjectIdArray CCreateEnt::CreateRegion(const AcDbObjectIdArray& curveIds)
{
AcDbObjectIdArray regionIds; // 生成的面域的ID数组
AcDbVoidPtrArray curves; // 指向作为面域边界的曲线的指针的数组
AcDbVoidPtrArray regions; // 指向创建的面域对象的指针的数组
AcDbEntity *pEnt; // 临时指针,用来关闭边界曲线
AcDbRegion *pRegion; // 临时对象,用来将面域添加到模型空间
// 用curveIds初始化curves
for (int i = 0; i < curveIds.length(); i++)
{
acdbOpenAcDbEntity(pEnt, curveIds.at(i), AcDb::kForRead);
if (pEnt->isKindOf(AcDbCurve::desc()))
{
curves.append(static_cast<void*>(pEnt));
}
}
Acad::ErrorStatus es = AcDbRegion::createFromCurves(curves,
regions);
if (es == Acad::eOk)
{
// 将生成的面域添加到模型空间
for (i = 0; i < regions.length(); i++)
{
// 将空指针(可指向任何类型)转化为指向面域的指针
pRegion = static_cast<AcDbRegion*>(regions[i]);
pRegion->setDatabaseDefaults();
AcDbObjectId regionId;
regionId = CCreateEnt::PostToModelSpace(pRegion);
regionIds.append(regionId);
}
}
else // 如果创建不成功,也要删除已经生成的面域
{
for (i = 0; i < regions.length(); i++)
{
delete (AcRxObject*)regions[i];
}
}
// 关闭作为边界的对象
for (i = 0; i < curves.length(); i++)
{
pEnt = static_cast<AcDbEntity*>(curves[i]);
pEnt->close();
}
return regionIds;
} |
|