admin 发表于 2024-3-5 08:11:41

AcDbSection 从正面创建实体的 2D 轮廓

void ClearCollection(AcArray<AcDbEntity *> &arr)
{
    for (AcDbEntity *ent : arr) {
      if (ent) {
            if (!ent->database())
                delete ent;
            else
                ent->close();
      }
    }
    arr.setLogicalLength(0);
}

Acad::ErrorStatus appendWithColor(AcArray<AcDbEntity *> &arr,
                                  AcDbBlockTableRecord *btr, uint16采用t col)
{
    Acad::ErrorStatus esRet = Acad::eOk, es;
    for (AcDbEntity *ent : arr) {
      if (ent != nullptr) {
            // Note that my implementation of ClearCollection() just close()s
            // DB-resident entities Non resident entities will be deleted.
            ent->setDatabaseDefaults(btr->database());
            ent->setColorIndex(col);
            es = btr->appendAcDbEntity(ent);
            // we don't close ent!
            if (es) esRet = es;
      }
    }
    return esRet;
}

void cgsa采用section()
{
    Acad::ErrorStatus es;
    AcDb3dSolid *solid = nullptr;
    ads采用name ename;
    ads采用point pt;
    if (acedEntSel(L"\nPick a solid: ", ename, pt) != RTNORM) return;
    AcDbObjectId idSolid;
    es = acdbGetObjectId(idSolid, ename);
    if (!es) {
      es = acdbOpenObject(solid, idSolid);
      if (!es) {
      }
    }
    else
      return;

    AcDbExtents extents;
    solid->getGeomExtents(extents);

    auto minX = extents.minPoint().x;
    auto minY = extents.minPoint().y;
    auto minZ = extents.minPoint().z;
    auto maxX = extents.maxPoint().x;
    auto maxY = extents.maxPoint().y;
    auto maxZ = extents.maxPoint().z;
    AcGePoint3dArray sectVertices;
    sectVertices.append(AcGePoint3d(minX, minY, minZ));
    sectVertices.append(AcGePoint3d(minX, maxY, minZ));
    AcDbDatabase *db = solid->database();
    AcDbSection *pSection = new AcDbSection(sectVertices, AcGeVector3d::kZAxis);
    pSection->setDatabaseDefaults(db);
    AcDbSection &section = *pSection;
    AcDbSectionSettings* pSettings;
    section.getSettings(pSettings, AcDb::kForWrite);
    section.setState(AcDbSection::kPlane);
        es = pSection->setHeight(AcDbSection::kHeightAboveSectionLine, 100);
    es = pSection->setHeight(AcDbSection::kHeightBelowSectionLine, -100);

    AcArray<AcDbEntity *> intBoundaryEnts;
    AcArray<AcDbEntity *> intFillEnts;
    AcArray<AcDbEntity *> backgroundEnts;
    AcArray<AcDbEntity *> foregroundEnts;
    AcArray<AcDbEntity *> curveTangencyEnts;

    es = section.generateSectionGeometry(solid, intBoundaryEnts, intFillEnts,
                                       backgroundEnts, foregroundEnts,
                                       curveTangencyEnts);
    if (es == Acad::eOk) {
      AcGeMatrix3d mat;
      mat.setToTranslation(AcGeVector3d(1000, 0, 0));

      AcDbObjectId idModelSpace = acdbSymUtil()->blockModelSpaceId(db);
      AcDbBlockTableRecord *modelspace;
      es = acdbOpenObject(modelspace, idModelSpace, AcDb::kForWrite);
      if (!es) {
            es = modelspace->appendAcDbEntity(pSection);
            if (!es)
                pSection->close();
            else
                delete pSection;
            appendWithColor(intBoundaryEnts, modelspace, 1);
            appendWithColor(intFillEnts, modelspace, 2);
            appendWithColor(backgroundEnts, modelspace, 3);
            appendWithColor(foregroundEnts, modelspace, 4);
            appendWithColor(curveTangencyEnts, modelspace, 5);
            modelspace->close();
      }
    }

    ClearCollection(intBoundaryEnts);
    ClearCollection(intFillEnts);
    ClearCollection(backgroundEnts);
    ClearCollection(foregroundEnts);
    ClearCollection(curveTangencyEnts);
}
页: [1]
查看完整版本: AcDbSection 从正面创建实体的 2D 轮廓