After I made a hatch object, added it to the database, set the style, and so on, it is not associative to the object. 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 void CreateHatch() { 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[4]; AcDbObjectId lineId, cirId, hatchId; AcDbObjectIdArray dbObjIds; AcDbLine *line; vertexPts[0].set(2.0, 2.0, 0.0); vertexPts[1].set(8.0, 2.0, 0.0); vertexPts[2].set(8.0, 8.0, 0.0); vertexPts[3].set(2.0, 8.0, 0.0); for (int i = 0; i < 4; i++) { line = new AcDbLine(); line->setStartPoint(vertexPts[i]); 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 (circl
e) 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[i], AcDb::kForWrite)==Acad::eOk) { pEnt->addPersistentReactor(hatchId); pEnt->close(); } } }
And here is the commonly used postToDb method:
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(); }

Leave a Reply to Augusto GoncalvesCancel reply