ObjectARX 2025(C++)实现创建单行/多行文字

最近更新于 2024-08-10 23:39

Table of Contents

环境

AutoCAD 机械版 2025
ObjectARX 2025
VS 2022

环境配置参考:https://blog.iyatt.com/?p=16480
自定义类使用方法参考:https://blog.iyatt.com/?p=16635

实现

Test.hpp

#pragma once

class Test
{
public:
    static void init();
    static void unload();

private:
    /**
     * @brief 获取块表记录
     */
    static void getBlockTableRecord();

    /**
     * @brief 释放块表记录及图形对象
     * @param pEntity 图形对象
     */
    static void freeBlockTableRecord(AcDbEntity* pEntity);

    /**
     * @brief 接受用户的位置和内容输入
     * @param isMultiLine 控制是否多行内容输入的标志
     * @return 输入成功失败状态
     */
    static bool input(bool isMultiLine = false);

    /**
     * @brief 创建单行文字
     */
    static void createSingleLineText();

    /**
     * @brief 创建多行文字
     */
    static void createMultiLineText();

private:
    static AcDbBlockTable* mpBlockTable; // 块表
    static AcDbBlockTableRecord* mpBlockTableRecord; // 块表记录
    static ads_point mInsertPt; // 插入位置
    static AcString mTextString; // 插入字符串
};

Test.cpp

#include "StdAfx.h"
#include "Test.hpp"

void Test::init()
{
    acedRegCmds->addCommand(L"IYATTyxTest", L"CSLT", L"CSLT", ACRX_CMD_MODAL, Test::createSingleLineText);
    acedRegCmds->addCommand(L"IYATTyxTest", L"CMLT", L"CMLT", ACRX_CMD_MODAL, Test::createMultiLineText);
}

void Test::unload()
{
    acedRegCmds->removeGroup(L"IYATTyxTest");
}

AcDbBlockTable* Test::mpBlockTable = nullptr;
AcDbBlockTableRecord* Test::mpBlockTableRecord = nullptr;

void Test::getBlockTableRecord()
{
    if (mpBlockTable == nullptr)
    {
        acdbHostApplicationServices()->workingDatabase()->getBlockTable(mpBlockTable);
    }
    if (mpBlockTableRecord == nullptr)
    {
        mpBlockTable->getAt(ACDB_MODEL_SPACE, mpBlockTableRecord, AcDb::kForWrite);
    }
}

void Test::freeBlockTableRecord(AcDbEntity* pEntity)
{
    if (pEntity != nullptr)
    {
        pEntity->close();
        pEntity = nullptr;
    }
    if (mpBlockTable != nullptr)
    {
        mpBlockTable->close();
        mpBlockTable = nullptr;
    }
    if (mpBlockTableRecord != nullptr)
    {
        mpBlockTableRecord->close();
        mpBlockTableRecord = nullptr;
    }
}

ads_point Test::mInsertPt = { 0, 0, 0 };
AcString Test::mTextString;

bool Test::input(bool isMultiLine)
{
    if (acedGetPoint(nullptr, L"请选择插入位置:\n", mInsertPt) != RTNORM)
    {
        return false;
    }

    mTextString.setEmpty(); // 清空输入字符串缓存

    if (isMultiLine) // 多行输入
    {
        AcString line; // 单行输入缓存
        while (true)
        {
            if (acedGetString(Adesk::kTrue, L"\n请输入多行文本内容(空行结束):\n", line) != RTNORM)
            {
                return false;
            }
            if (line.empty()) // 空行停止输入
            {
                break;
            }
            mTextString += "\n";
            mTextString += line;
        }
    }
    else
    {
        if (acedGetString(Adesk::kTrue, L"\n请输入单行文本内容:\n", mTextString) != RTNORM)
        {
            return false;
        }
    }

    return true;
}

void Test::createSingleLineText()
{
    getBlockTableRecord();
    if (!Test::input())
    {
        acutPrintf(L"\n获取用户输入失败,取消操作。\n");
        return;
    }

    AcDbText* pText = new AcDbText(asPnt3d(mInsertPt), mTextString.kACharPtr()); // 创建单行文字对象
    mpBlockTableRecord->appendAcDbEntity(pText); // 添加到块表记录中

    freeBlockTableRecord(pText);
}

void Test::createMultiLineText()
{
    getBlockTableRecord();
    if (!Test::input(true))
    {
        acutPrintf(L"\n获取用户输入失败,取消操作。\n");
        return;
    }

    AcDbMText* pMText = new AcDbMText(); // 创建多行文字对象
    pMText->setContents(mTextString.kACharPtr());
    pMText->setLocation(asPnt3d(mInsertPt));
    mpBlockTableRecord->appendAcDbEntity(pMText); // 添加到块表记录中

    freeBlockTableRecord(pMText);
}

测试 CSLT 命令创建单行文本
file

file

测试 CMLT 命令创建多行文本
file

(公差用大括号括起来,\H指定公差部分相对字体的大小比例,\C指定颜色值,\S指定公差内容。多行文字中会自动转换格式显示。)
file

ObjectARX 2025(C++)实现创建单行/多行文字
Scroll to top