Teigha
Dionysen

Teigha是一个软件开发工具包(SDK),由Open Design Alliance (ODA)开发和维护。

// 创建一个完整的块并添加到指定图层
void createBlockWithEntities(OdDbDatabase* pDb,
const OdString& blockName,
const OdString& layerName)
{
try {
// 1. 首先创建图层
OdDbLayerTablePtr pLayerTable = pDb->getLayerTableId().safeOpenObject(OdDb::kForWrite);
OdDbLayerTableRecordPtr pLayer;

if (!pLayerTable->has(layerName))
{
pLayer = OdDbLayerTableRecord::createObject();
pLayer->setName(layerName);
// 设置图层属性
pLayer->setColor(OdCmColor(OdCmEntityColor::kRed)); // 红色
pLayer->setIsOff(false);
pLayer->setIsFrozen(false);
pLayer->setIsLocked(false);

pLayerTable->add(pLayer);
}
else
{
pLayer = pLayerTable->getAt(layerName).safeOpenObject(OdDb::kForWrite);
}

// 2. 创建块表记录
OdDbBlockTablePtr pBlockTable = pDb->getBlockTableId().safeOpenObject(OdDb::kForWrite);
OdDbBlockTableRecordPtr pBlockRecord;

if (!pBlockTable->has(blockName))
{
pBlockRecord = OdDbBlockTableRecord::createObject();
pBlockRecord->setName(blockName);
pBlockTable->add(pBlockRecord);
}
else
{
pBlockRecord = pBlockTable->getAt(blockName).safeOpenObject(OdDb::kForWrite);
}

// 3. 创建各种图元并添加到块中

// 3.1 创建直线
OdDbLinePtr pLine = OdDbLine::createObject();
pLine->setStartPoint(OdGePoint3d(0, 0, 0));
pLine->setEndPoint(OdGePoint3d(100, 0, 0));
pLine->setLayer(layerName);
pBlockRecord->appendOdDbEntity(pLine);

// 3.2 创建矩形(使用多段线)
OdDbPolylinePtr pRect = OdDbPolyline::createObject();
pRect->setLayer(layerName);

// 添加矩形的四个顶点
pRect->addVertexAt(0, OdGePoint2d(0, 0));
pRect->addVertexAt(1, OdGePoint2d(100, 0));
pRect->addVertexAt(2, OdGePoint2d(100, 50));
pRect->addVertexAt(3, OdGePoint2d(0, 50));
pRect->setClosed(true); // 闭合多段线

pBlockRecord->appendOdDbEntity(pRect);

// 3.3 创建长方体(使用3DSOLID)
OdDb3dSolidPtr pBox = OdDb3dSolid::createObject();
pBox->setLayer(layerName);

// 创建长方体
pBox->createBox(100, 50, 30); // 长100,宽50,高30
// 移动到指定位置
pBox->transformBy(OdGeMatrix3d::translation(OdGeVector3d(0, 0, 0)));

pBlockRecord->appendOdDbEntity(pBox);

// 3.4 创建圆
OdDbCirclePtr pCircle = OdDbCircle::createObject();
pCircle->setCenter(OdGePoint3d(50, 25, 0));
pCircle->setRadius(25);
pCircle->setLayer(layerName);

pBlockRecord->appendOdDbEntity(pCircle);

// 4. 创建块引用(在模型空间中插入块)
OdDbBlockTableRecordPtr pModelSpace = pDb->getModelSpaceId().safeOpenObject(OdDb::kForWrite);

OdDbBlockReferencePtr pBlockRef = OdDbBlockReference::createObject();
pBlockRef->setBlockTableRecord(pBlockRecord->objectId());
pBlockRef->setPosition(OdGePoint3d(0, 0, 0));
pBlockRef->setLayer(layerName);

// 可以设置缩放和旋转
pBlockRef->setScaleFactors(OdGeScale3d(1.0));
pBlockRef->setRotation(0.0);

pModelSpace->appendOdDbEntity(pBlockRef);
}
catch (const OdError& e)
{
// 错误处理
throw e;
}
}

// 使用示例
void example()
{
OdDbDatabase* pDb = /* 获取数据库指针 */;

// 创建一个名为"MyBlock"的块,并将其添加到名为"MyLayer"的图层中
createBlockWithEntities(pDb, L"MyBlock", L"MyLayer");
}
显示评论