newer 发表于 2021-1-21 00:07:04

AcDbHatch associativity

AcDbHatch associativity

问题:

After I made a hatch object, added it to the database, set the style, and so on, it is not associative to the object. What must I do to make it associative?

解答:
Use the hatch object that was just created as a persistent reactor for the entities to which it is hatching. The following sample demonstrates how to create a hatch and associate it to the boundary entities (a rectangle and a circle).


static Acad::ErrorStatus postToDb(AcDbEntity* ent, AcDbObjectId& objId)
{
Acad::ErrorStatus es;
AcDbBlockTable* pBlockTable;
AcDbBlockTableRecord* pSpaceRecord;
if (ent==NULL)
return Acad::eNullObjectPointer;
if (acdbHostApplicationServices()->workingDatabase()==NULL)
return Acad::eNoDatabase;
if ((es = acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable, AcDb::kForRead))!=Acad::eOk)
return es;
if ((es =pBlockTable->getAt(ACDB_MODEL_SPACE,pSpaceRecord,AcDb::kForWrite)) != Acad::eOk)
{
pBlockTable->close();
return es;
}
pBlockTable->close();
if ((es = pSpaceRecord->appendAcDbEntity(objId, ent)) != Acad::eOk)
{ pSpaceRecord->close();
return es;
}
pSpaceRecord->close();
return ent->close();
}


static void Test()
{
AcDbHatch* pHatch = new AcDbHatch();
// Set hatch plane
AcGeVector3d normal(0.0, 0.0, 1.0);
pHatch->setNormal(normal);
pHatch->setElevation(0.0);
// Set hatch pattern to ANSI31 predefined type //
pHatch->setPattern(AcDbHatch::kPreDefined, _T("ANSI31"));
// Set Associativity
pHatch->setAssociative(Adesk::kTrue);
// Construct database AcDbLines
AcGePoint3d vertexPts;
AcDbObjectId lineId, cirId, hatchId;
AcDbObjectIdArray dbObjIds;
AcDbLine *line;
vertexPts.set(2.0, 2.0, 0.0);
vertexPts.set(8.0, 2.0, 0.0);
vertexPts.set(8.0, 8.0, 0.0);
vertexPts.set(2.0, 8.0, 0.0);
for (int i = 0; i < 4; i++)
{
line = new AcDbLine();
line->setStartPoint(vertexPts);
line->setEndPoint(vertexPts[(i == 3) ? 0 : i+1]);
postToDb(line, lineId);
dbObjIds.append(lineId);
}
// Append an external rectangular loop to hatch boundary //
pHatch->appendLoop(AcDbHatch::kExternal, dbObjIds);
// Create a AcDbCircle and post it to database //
AcGePoint3d cenPt(5.0, 5.0, 0.0);
normal.set(0.0, 0.0, 1.0);
AcDbCircle *circle = new AcDbCircle();
circle->setNormal(normal);
circle->setCenter(cenPt);
circle->setRadius(1.0);
postToDb(circle, cirId);
dbObjIds.setLogicalLength(0);
dbObjIds.append(cirId);
// Append an internal loop (circle) to hatch boundary //
pHatch->appendLoop(AcDbHatch::kDefault, dbObjIds);
// Elaborate hatch lines
pHatch->evaluateHatch();
// Get all associative source boundary object Ids for later use. //
dbObjIds.setLogicalLength(0);
pHatch->getAssocObjIds(dbObjIds);
// Post hatch entity to database
postToDb(pHatch, hatchId);
// Attach hatchId to all source boundary objects for notification. //
AcDbEntity *pEnt;
int numObjs = dbObjIds.length();
for (int i = 0; i < numObjs; i++)
{
if (acdbOpenAcDbEntity(pEnt, dbObjIds,AcDb::kForWrite)==Acad::eOk)
{
   pEnt->addPersistentReactor(hatchId);
   pEnt->close();
}
}
}

页: [1]
查看完整版本: AcDbHatch associativity