ObjectARX 2025(C++)创建带属性的块定义 AcDbAttributeDefinition

最近更新于 2025-04-07 08:46

环境

AutoCAD 机械版 2025
ObjectARX 2025
VS 2022(ISO C++14)

环境配置参考:https://blog.iyatt.com/?p=16480
块属性使用参考:https://blog.iyatt.com/?p=19800

实现

CreateBlockWithAttribute.hpp

#pragma once
class CreateBlockWithAttribute
{
public:
    static void createBlock();
};

CreateBlockWithAttribute.cpp

#include "stdafx.h"
#include "CreateBlockWithAttribute.hpp"

void CreateBlockWithAttribute::createBlock()
{
    // 获取数据库块表
    AcDbBlockTable* pBlockTable = nullptr;
    acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable, AcDb::kForWrite);

    // 创建新的块表记录
    AcDbBlockTableRecord* pBlockTableRecord = new AcDbBlockTableRecord();

    // 用户输入块名称
    AcString blockName;
    if (acedGetString(Adesk::kFalse, L"\n请输入图块名称", blockName) != RTNORM)
    {
        pBlockTable->close();
        delete pBlockTableRecord;
        return;
    }
    pBlockTableRecord->setName(blockName.kACharPtr());

    // 将块表记录添加到块表中
    pBlockTable->add(pBlockTableRecord);
    pBlockTable->close();

    // 向块记录中添加实体
    AcDbCircle* pCircle = new AcDbCircle(AcGePoint3d::kOrigin, AcGeVector3d(0, 0, 1), 10); // 圆
    AcDbAttributeDefinition* pAttributeDefinition = new AcDbAttributeDefinition(
        AcGePoint3d::kOrigin, // 插入点:原点
        L"1", //默认
        L"序号", // 标记
        L"序号数值" // 提示
    );
    pAttributeDefinition->setHorizontalMode(AcDb::kTextCenter); // 水平居中
    pAttributeDefinition->setVerticalMode(AcDb::kTextVertMid); // 垂直居中

    pBlockTableRecord->appendAcDbEntity(pCircle);
    pBlockTableRecord->appendAcDbEntity(pAttributeDefinition);

    pCircle->close();
    pAttributeDefinition->close();
    pBlockTableRecord->close();
}

在 acrxEntryPoint.cpp 中引用头文件,并在 On_kInitAppMsg 方法里调用
file

调试运行,输入要块名进行创建
file

可以插入使用
file

ObjectARX 2025(C++)创建带属性的块定义 AcDbAttributeDefinition
Scroll to top

目录

打开目录