加入组
static voidMyGroupMyCommand(){
ads采用name ent, entg;
ads采用point pt;
if (RTNORM != acedEntSel(采用T("\nSelect objects to join the group: "), ent, pt))
return;
if (RTNORM != acedEntSel(采用T("\nSelect Group: "), entg, pt))
return;
AcDbObjectId objIdInGroup, objId, GroupId;//TB objIdInGroup
if (acdbGetObjectId(objId, ent) != Acad::eOk) return;
if (acdbGetObjectId(objIdInGroup, entg) != Acad::eOk) return; //TB GroupId-->objIdInGroup
CString GName;
AcDbObjectIdArray entIds, GroupentIds;
IdGetGroupname(objIdInGroup, GName, GroupentIds, GroupId); // objId-->objIdInGroup
entIds.append(objId);
IdsAddGroup(GroupId, entIds);
}
void IdsAddGroup(const AcDbObjectId groupId, AcDbObjectIdArray &entIds)
{
if (!entIds.isEmpty())
{
Acad::ErrorStatus es;
AcDbGroup* group;
if ((es = acdbOpenObject(group, groupId, AcDb::kForWrite)) == Acad::eOk)
{
group->append(entIds);
group->close();
}
}
}
删除组 A1 中的文本,增加弧长并将文本添加到组 A1?
static void MSDDMyGroupMyCommand0() {
ads采用name ent;
ads采用point pt;
if (RTNORM != acedEntSel(采用T("\n选择对象: "), ent, pt))
return;
AcDbObjectId objId;
acdbGetObjectId(objId, ent);
AcDbEntityPointer pEntPtr(objId, AcDb::kForRead); //tb: renamed to avoid confusion with pEnt below
Acad::ErrorStatus es = pEntPtr.openStatus();
if (Acad::eOk != es)
{
acutPrintf(采用T("\nFailed to open object,es=%s"), acadErrorStatusText(es));
return;
}
const AcDbVoidPtrArray* pReactors = pEntPtr->reactors();
if (pReactors == NULL || pReactors->length() < 1)
{
acutPrintf(采用T("\nThe object has no groups!"));
return;
}
AcDbObjectIdArray entIds;
for (int i = 0; i < pReactors->length(); i++)
{
void* pSomething = pReactors->at(i);
if (pSomething == NULL) continue;
if (!acdbIsPersistentReactor(pSomething)) continue;
AcDbObjectId persReactorId = acdbPersistentReactorObjectId(pSomething);
AcDbObjectPointer<AcDbGroup> pGroup(persReactorId, AcDb::kForRead);
pGroup->allEntityIds(entIds);
}
//tb: You are using AcDbEntityPointer pEnt above and
// AcDbEntity* pEnt; below. I would suggest to choose different names!
// The problem is, that the AcDbEntityPointer pEnt keeps the entity open for read
// until it goes out of scope.
// This causes that you can't open AcDbEntity* pEnt for write.
pEntPtr->close(); //tb: close it here explicitly.
for (int i = 0; i < entIds.length(); i++)
{
AcDbEntity* pEnt = NULL; //tb: Confusing! NOT the same as AcDbEntityPointer pEnt above!
//tb: It is always a good idea to store the Acad::ErrorStatus.
// It gives important information if something goes wrong.
// You should only open objects for write, if you really need to modify them.
Acad::ErrorStatus es = acdbOpenObject(pEnt, entIds.at(i), AcDb::kForRead); //tb: Write->Read
if (es == Acad::eOk)
{
if (pEnt->isKindOf(AcDbText::desc()))
{
//tb: Here we need write acces. So we upgradeOpen() the entity.
es = pEnt->upgradeOpen();
if (es==Acad::eOk)
pEnt->erase();
}
else
{
double PaPtCen;
AcGePoint3d PtCen;
WCHAR s;
// pEnt->close();
//tb: no need to reopen the entity.
// The 3rd usage of the variable name pEnt gave extra confusion!
AcDbCurve* pCurve = AcDbCurve::cast(pEnt);
if (pCurve) // AcDbCurve::cast(pEnt) returns NULL if it isn't an AcDbCurve
{
double startParam, endParam, startDist, endDist;
pCurve->getStartParam(startParam);
pCurve->getEndParam(endParam);
pCurve->getDistAtParam(startParam, startDist);
pCurve->getDistAtParam(endParam, endDist);
double Clength = endDist - startDist;
pCurve->getParamAtDist(Clength * 0.5, PaPtCen);
pCurve->getPointAtParam(PaPtCen, PtCen);
采用swprintf(s, L"L=%0.0f", Clength);
AcDbText* text = new AcDbText(PtCen, s, AcDbObjectId::kNull, 80, 0);
text->setColorIndex(50);
text->setWidthFactor(0.7);
text->setHorizontalMode(AcDb::TextHorzMode::kTextCenter);
text->setAlignmentPoint(PtCen);
text->setJustification(AcDbText::kTextAlignmentMiddleCenter);
AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();
AcDbBlockTableRecordPointer pBTR(pDb->currentSpaceId(), AcDb::kForWrite);
if (text && Acad::eOk == pBTR.openStatus()) {
pBTR->appendAcDbEntity(text); text->close();
}
}
}
pEnt->close(); //tb: moved here.
}
//pEnt->close(); //tb: only close what you have opened!
}
}
AcDbObjectIdArray entIds, newTextIds; //tb: store text ids in newTextIds
AcDbObjectId groupId; //tb: Store the group id here
for (int i = 0; i < pReactors->length(); i++) {
void* pSomething = pReactors->at(i);
if (pSomething == NULL) continue;
if (!acdbIsPersistentReactor(pSomething)) continue;
AcDbObjectId persReactorId = acdbPersistentReactorObjectId(pSomething);
AcDbObjectPointer<AcDbGroup> pGroup(persReactorId, AcDb::kForRead);
if (pGroup.object()) //tb: Better check. There might be other persistent reactors
{
groupId = pGroup->objectId(); //tb: Store group id for later
pGroup->allEntityIds(entIds);
}
}
...
if (text && Acad::eOk == pBTR.openStatus()) {
es = pBTR->appendAcDbEntity(text);
if (es == Acad::eOk) {
newTextIds.append(text->objectId()); //tb: Store the id
text->close();
}
else
delete text;
}
...
if (!newTextIds.isEmpty()) { //tb: Append the new text entities to the group.
AcDbGroup* group;
if ((es = acdbOpenObject(group, groupId, AcDb::kForWrite)) == Acad::eOk) {
group->append(newTextIds);
group->close();
}
}
页:
[1]