admin 发表于 2024-3-16 09:19:46

ObjectARX2015 + vs2012 创建各种尺寸标注

// 引入必要的头文件
#include "dbdimvars.h"
#include "dbdimdata.h"
#include "dbdimline.h"
#include "dbdimrotated.h"
#include "dbdimrnd.h"
#include "dbobjptr.h"
#include "dbents.h"

// 假设已获得两个点,代表标注的起点和终点
AcGePoint3d startPt, endPt;

// 获取当前工作数据库
AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();

// 锁定数据库以防止并发修改
AcDbVoidPtrArray objectsToLock;
objectsToLock.append(pDb);
pDb->lockObjects(objectsToLock);

try {
    // 创建一个新的尺寸标注对象
    AcDbDimLinear* pDim = new AcDbDimLinear();

    // 设置尺寸线的起点和终点
    pDim->setDefpoint(startPt, AcDbDim::kFirstExtensionPoint);
    pDim->setDefpoint(endPt, AcDbDim::kSecondExtensionPoint);

    // 关联到实体,例如一条直线
    AcDbLine* pLine = new AcDbLine(startPt, endPt);
    pDim->associateEntity(pLine->objectId());

    // 设置标注的其他属性,如标注样式、文本位置、角度等
    pDim->setTextStyle(/* 标注样式名 */);
    pDim->setRotation(/* 标注旋转角度 */);
    pDim->setTextPosition(AcDbDim::kAboveDimLineCenterAligned);

    // 插入到当前图形空间
    AcDbBlockTableRecord* pBlockRec = NULL;
    pDb->getActiveSpaceId().openObject(pBlockRec, AcDb::kForWrite);
    Acad::ErrorStatus es = pBlockRec->appendAcDbEntity(pDim);
    if (es != Acad::eOk) {
      // 处理错误
    }

    // 提交事务
    pDb->databaseDefaults().dimstyle(OBJECTID采用OF采用DIMSTYLE采用TO采用USE);
    pDim->setDatabaseDefaults();
    pDim->close();
    pLine->erase(true);
    pBlockRec->close();
} catch (...) {
    // 清理异常,撤销操作
}

// 解锁数据库
pDb->unlockObjects(objectsToLock);

// 提交事务(确保在命令结束前提交)
Acad::ErrorStatus es = acdbCurTransaction()->commit();
if (es != Acad::eOk) {
    // 处理提交失败
}

// 释放资源
acdbHostApplicationServices()->workingDatabase()->release(pDb);

admin 发表于 2024-3-16 09:24:35

线性对齐标注 (AcDbAlignedDimension):

Cpp
// 假设有两条线的端点定义了对齐方向
AcGePoint3d basePt, definingLineStart, definingLineEnd;

AcDbAlignedDimension *pAlignedDim = new AcDbAlignedDimension;
pAlignedDim->setDefiningLine(definingLineStart, definingLineEnd); // 设置定义线
pAlignedDim->setBasePoint(basePt); // 设置基准点
// 然后同线性标注一样设置关联实体、标注样式、文本位置等属性
// 并插入到块记录中
旋转标注 (AcDbRotatedDimension):

Cpp
AcGePoint3d dimLineOrigin, leaderEndPoint;
double rotationAngle;

AcDbRotatedDimension *pRotatedDim = new AcDbRotatedDimension;
pRotatedDim->setDefpoint(dimLineOrigin); // 设置维度线原点
pRotatedDim->setLeaderEndPoint(leaderEndPoint); // 设置引线端点
pRotatedDim->setRotation(rotationAngle); // 设置旋转角度
// 同样设置关联实体、标注样式等属性,并插入到块记录中
径向标注 (AcDbRadialDimension):

Cpp
AcGePoint3d centerPt, chordPt;

AcDbRadialDimension *pRadialDim = new AcDbRadialDimension;
pRadialDim->setCenter(centerPt); // 设置圆心点
pRadialDim->setChordPoint(chordPt); // 设置弦上的点
// 接着设置关联实体、标注样式等属性,并插入到块记录中
直径标注 (AcDbDiametricDimension):

Cpp
AcGePoint3d arcMidPt, chordPt;

AcDbDiametricDimension *pDiametricDim = new AcDbDiametricDimension;
pDiametricDim->setArcMidPoint(arcMidPt); // 设置圆弧中点
pDiametricDim->setChordPoint(chordPt); // 设置通过圆弧的弦上的点
// 同样完成其他属性设置和插入操作
两线角度标注 (AcDb2LineAngularDimension):

Cpp
AcGePoint3d line1Start, line1End, line2Start, line2End;

AcDb2LineAngularDimension *p2LineAngDim = new AcDb2LineAngularDimension;
p2LineAngDim->setLine1StartPoint(line1Start);
p2LineAngDim->setLine1EndPoint(line1End);
p2LineAngDim->setLine2StartPoint(line2Start);
p2LineAngDim->setLine2EndPoint(line2End);
// 完成后续属性设置和插入
三点角度标注 (AcDb3PointAngularDimension):

Cpp
AcGePoint3d pt1, pt2, pt3;

AcDb3PointAngularDimension *p3PointAngDim = new AcDb3PointAngularDimension;
p3PointAngDim->setPoint1(pt1);
p3PointAngDim->setPoint2(pt2);
p3PointAngDim->setPoint3(pt3);
// 其他属性设置和插入操作
坐标标注 (AcDbOrdinateDimension):

Cpp
AcGePoint3d originPt, definedPt;

AcDbOrdinateDimension *pOrdinateDim = new AcDbOrdinateDimension;
pOrdinateDim->setOrigin(originPt); // 设置原点
pOrdinateDim->setDefinedPoint(definedPt); // 设置被定义点
// 指定坐标类型(X, Y 或 Z),以及其他属性设置和插入操作

admin 发表于 2024-3-16 09:25:10

//当前项目中注册一个命令 MyCreateDimAligned
    static void MidasMyGroupMyCreateDimAligned()
    {
      CGeometryOper m采用geometryOper;
      // 指定起始点位置
      AcGePoint3d pt1(200, 160, 0);
      AcGePoint3d pt2= m采用geometryOper.RelativePoint(pt1, -40, 0);
      AcGePoint3d pt3 = m采用geometryOper.PolarPoint(pt2, 7 * m采用geometryOper.PI() / 6, 20);
      AcGePoint3d pt4 = m采用geometryOper.RelativePoint(pt3, 6, -10);
      AcGePoint3d pt5 = m采用geometryOper.RelativePoint(pt1, 0, -20);
      // 绘制外轮廓线
      CCreateEnt::CreateLine(pt1, pt2);
      CCreateEnt::CreateLine(pt2, pt3);
      CCreateEnt::CreateLine(pt3, pt4);
      CCreateEnt::CreateLine(pt4, pt5);
      CCreateEnt::CreateLine(pt5, pt1);
      // 绘制圆形
      AcGePoint3d ptCenter1, ptCenter2;
      ptCenter1 = m采用geometryOper.RelativePoint(pt3, 16, 0);
      ptCenter2 = m采用geometryOper.RelativePoint(ptCenter1, 25, 0);
      CCreateEnt::CreateCircle(ptCenter1, 3);
      CCreateEnt::CreateCircle(ptCenter2, 4);

      AcGePoint3d ptTemp1, ptTemp2;
      // 水平标注
      ptTemp1 = m采用geometryOper.RelativePoint(pt1, -20, 3);
      CCreateEnt::CreateDimRotated(pt1, pt2, ptTemp1, 0);
      // 垂直标注
      ptTemp1 = m采用geometryOper.RelativePoint(pt1, 4, 10);
      CCreateEnt::CreateDimRotated(pt1, pt5, ptTemp1,
            m采用geometryOper.PI() / 2);
      // 转角标注
      ptTemp1 = m采用geometryOper.RelativePoint(pt3, -3, -6);
      CCreateEnt::CreateDimRotated(pt3, pt4, ptTemp1, 7 * m采用geometryOper.PI() / 4);
      // 对齐标注
      ptTemp1 = m采用geometryOper.RelativePoint(pt2, -3, 4);
      CCreateEnt::CreateDimAligned(pt2, pt3, ptTemp1, AcGeVector3d(4, 10, 0), 采用T("new position"));
      // 角度标注
      ptTemp1 = m采用geometryOper.RelativePoint(pt5, -5, 5);
      CCreateEnt::CreateDim3PtAngular(pt5, pt1, pt4, ptTemp1);
      // 半径标注
      ptTemp1 = m采用geometryOper.PolarPoint(ptCenter1, m采用geometryOper.PI() / 4, 3);
      CCreateEnt::CreateDimRadial(ptCenter1, ptTemp1, -3);
      // 直径标注
      ptTemp1 = m采用geometryOper.PolarPoint(ptCenter2,
            m采用geometryOper.PI() / 4, 4);
      ptTemp2 = m采用geometryOper.PolarPoint(ptCenter2,
            m采用geometryOper.PI() / 4, -4);
      CCreateEnt::CreateDimDiametric(ptTemp1, ptTemp2, 0);
      // 坐标标注
      CCreateEnt::CreateDimOrdinate(ptCenter2, AcGeVector3d(0, -10, 0),
            AcGeVector3d(10, 0, 0));
    }
页: [1]
查看完整版本: ObjectARX2015 + vs2012 创建各种尺寸标注