创建具有属性定义的块表记录
AutoCAD 块是存储在块表记录中的图元集合。每个块都有一个对象,后跟一个或多个对象,并以一个对象结尾(请参阅“实体所有权”下的插图)。AcDbBlockBeginAcDbEntityAcDbBlockEnd 块可以包含属性定义,这些定义是用于创建属性的模板。属性是与块关联的信息文本。根据用户提供的设置,在将块插入到图形中时,可能会也可能不会复制属性值。通常,应用程序会在运行时提示用户输入属性值。 创建块表记录
当您关闭块表记录时,块开始和块结束对象会自动添加到块中。 以下示例创建一个名为 ASDK-BLOCK-WITH-ATTR并将其添加到块表中。接下来,它创建一个圆形实体并将其添加到新的块表记录中。它创建两个属性定义实体(第二个是第一个实体的克隆),并将它们附加到同一个块表记录 void
defineBlockWithAttributes(
AcDbObjectId& blockId, // This is a returned value.
const AcGePoint3d& basePoint,
double textHeight,
double textAngle)
{
int retCode = 0;
AcDbBlockTable *pBlockTable = NULL;
AcDbBlockTableRecord* pBlockRecord = new AcDbBlockTableRecord;
AcDbObjectId entityId;
// Step 1: Set the block name and base point of the
// block definition.
//
pBlockRecord->setName("ASDK-BLOCK-WITH-ATTR");
pBlockRecord->setOrigin(basePoint);
// Open the block table for write.
//
acdbHostApplicationServices()->workingDatabase()
->getSymbolTable(pBlockTable, AcDb::kForWrite);
// Step 2: Add the block table record to block table.
//
pBlockTable->add(blockId, pBlockRecord);
// Step 3: Create a circle entity.
//
AcDbCircle *pCircle = new AcDbCircle;
pCircle->setCenter(basePoint);
pCircle->setRadius(textHeight * 4.0);
pCircle->setColorIndex(3);
// Append the circle entity to the block record.
//
pBlockRecord->appendAcDbEntity(entityId, pCircle);
pCircle->close();
// Step 4: Create an attribute definition entity.
//
AcDbAttributeDefinition *pAttdef
= new AcDbAttributeDefinition;
// Set the attribute definition values.
//
pAttdef->setPosition(basePoint);
pAttdef->setHeight(textHeight);
pAttdef->setRotation(textAngle);
// For horizontal modes other than AcDb::kTextLeft
// and vertical modes other than AcDb::kTextBase,
// you may need to call setAlignmentPoint(). See the
// AcDbText::setAlignmentPoint() documentation for details.
pAttdef->setHorizontalMode(AcDb::kTextLeft);
pAttdef->setVerticalMode(AcDb::kTextBase);
pAttdef->setPrompt("Prompt");
pAttdef->setTextString("DEFAULT");
pAttdef->setTag("Tag");
pAttdef->setInvisible(Adesk::kFalse);
pAttdef->setVerifiable(Adesk::kFalse);
pAttdef->setPreset(Adesk::kFalse);
pAttdef->setConstant(Adesk::kFalse);
pAttdef->setFieldLength(25);
// Append the attribute definition to the block.
//
pBlockRecord->appendAcDbEntity(entityId, pAttdef);
// The second attribute definition is a little easier
// because we are cloning the first one.
//
AcDbAttributeDefinition *pAttdef2
= AcDbAttributeDefinition::cast(pAttdef->clone());
// Set the values that are specific to the
// second attribute definition.
//
AcGePoint3d tempPt(basePoint);
tempPt.y -= pAttdef2->height();
pAttdef2->setPosition(tempPt);
pAttdef2->setColorIndex(1); // Red
pAttdef2->setConstant(Adesk::kTrue);
// Append the second attribute definition to the block.
//
pBlockRecord->appendAcDbEntity(entityId, pAttdef2);
pAttdef->close();
pAttdef2->close();
pBlockRecord->close();
pBlockTable->close();
return;
}
父主题: |
|Archiver|CAD开发者社区
( 苏ICP备2022047690号-1 苏公网安备32011402011833)
GMT+8, 2025-10-31 05:54
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.