ObjectARX 2025(C++)绘制圆弧 AcDbArc

最近更新于 2025-03-23 10:16

环境

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

环境配置参考:https://blog.iyatt.com/?p=16480

实现

Arc.hpp

#pragma once
class Arc
{
public:
    static void work();

private:
    static void drawArc(AcDbArc* arcObj);
};

Arc.cpp

#include "stdafx.h"
#include "Arc.hpp"

void Arc::drawArc(AcDbArc* arcObj)
{
    AcDbBlockTable* pBlockTable = nullptr;
    acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable, AcDb::kForRead);
    AcDbBlockTableRecord* pBlockTableRecord = nullptr;
    pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite);
    pBlockTableRecord->appendAcDbEntity(arcObj);
    pBlockTableRecord->close();
    pBlockTable->close();
}

void Arc::work()
{
    // 二维平面向量
    AcGeVector3d normal(0, 0, 1);

    // 圆心、半径、起始角度、结束角度
    {
        AcGePoint3d center(0, 0, 0);
        double radius = 50;
        double startAngle = 0;
        double endAngle = 3.1415926 * 3 / 2;

        AcDbArc* arcObj = new AcDbArc(center, normal, radius, startAngle, endAngle);
        Arc::drawArc(arcObj);
        arcObj->close();
    }

    // 三点
    {
        AcGePoint2d startPt(0, 0);
        AcGePoint2d endPt(50, 50);
        AcGePoint2d otherPt(100, 0);

        // 获取圆心、半径
        AcGeCircArc2d geArc(startPt, endPt, otherPt);
        AcGePoint2d center = geArc.center();
        double radius = geArc.radius();

        // 获取起始角度、结束角度
        AcGeVector2d startVec(startPt.x - center.x, startPt.y - center.y);
        double startAngle = startVec.angle();
        AcGeVector2d endVec(endPt.x - center.x, endPt.y - center.y);
        double endAngle = endVec.angle();

        AcDbArc* arcObj = new AcDbArc(AcGePoint3d(center.x, center.y, 0), normal, radius, startAngle, endAngle);
        Arc::drawArc(arcObj);
        arcObj->close();
    }

    // 圆心、起点、角度
    {
        AcGePoint2d startPt(20, 20);
        AcGePoint2d center(0, 0);
        double angle = 3.1415926 * 0.5;

        // 计算半径
        double radius = startPt.distanceTo(center);

        // 起点、终点角度
        AcGeVector2d startVec(startPt.x - center.x, startPt.y - center.y);
        double startAngle = startVec.angle();
        double endAngle = startAngle + angle;

        AcDbArc* arcObj = new AcDbArc(AcGePoint3d(center.x, center.y, 0), normal, radius, startAngle, endAngle);
        Arc::drawArc(arcObj);
        arcObj->close();
    }

    // 起点、终点、圆心
    {
        AcGePoint2d startPt(25, 25);
        AcGePoint2d endPt(50, 100);
        AcGePoint2d center(0, 0);

        // 计算半径
        double radius = startPt.distanceTo(center);

        // 起点、终点角度
        AcGeVector2d startVec(startPt.x - center.x, startPt.y - center.y);
        double startAngle = startVec.angle();
        AcGeVector2d endVec(endPt.x - center.x, endPt.y - center.y);
        double endAngle = endVec.angle();

        AcDbArc* arcObj = new AcDbArc(AcGePoint3d(center.x, center.y, 0), normal, radius, startAngle, endAngle);
        Arc::drawArc(arcObj);
        arcObj->close();
    }
}

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

调试运行
file

ObjectARX 2025(C++)绘制圆弧 AcDbArc
Scroll to top

目录

打开目录