ObjectARX 2025(C++)绘制样条曲线 Spline【其中一种方式】

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

环境

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

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

实现

Spline.hpp

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

private:
    static void drawSpline(AcDbSpline* pSplineObj);
};

Spline.cpp

#include "stdafx.h"
#include "Spline.hpp"

void Spline::drawSpline(AcDbSpline* pSplineObj)
{
    AcDbBlockTable* pBlockTable = nullptr;
    acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable, AcDb::kForRead);
    AcDbBlockTableRecord* pBlockTableRecord = nullptr;
    pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite);
    pBlockTableRecord->appendAcDbEntity(pSplineObj);
    pBlockTableRecord->close();
    pBlockTable->close();
}

void Spline::work()
{
    // 点、阶数、拟合公差
    {
        // 点
        AcGePoint3d pt1(0, 0, 0);
        AcGePoint3d pt2(10, 10, 0);
        AcGePoint3d pt3(20, 0, 0);
        AcGePoint3d pt4(30, 10, 0);
        AcGePoint3dArray pts;
        pts.append(pt1);
        pts.append(pt2);
        pts.append(pt3);
        pts.append(pt4);

        // 阶数
        // 不指定默认就是 4 阶,阶数越高,曲线越光滑,但计算量更大
        int order = 4;

        // 拟合公差
        // 非负值,不指定默认也为 0
        // 值越小,越是尽可能地通过指定点
        // 值越大,越平滑,但是可能偏离控制点
        double fitTolerance = 0;

        AcDbSpline* pSplineObj = new AcDbSpline(pts, order, fitTolerance);
        Spline::drawSpline(pSplineObj);
        pSplineObj->close();
    }
}

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

调试运行
file

ObjectARX 2025(C++)绘制样条曲线 Spline【其中一种方式】
Scroll to top

目录

打开目录