几何尺寸与公差论坛

 找回密码
 注册
查看: 72|回复: 4

坐标系的建立

  [复制链接]
发表于 7 天前 | 显示全部楼层 |阅读模式
gp_Ax2(const gp_Pnt& P, const gp_Dir& N, const gp_Dir& Vx)
 楼主| 发表于 7 天前 | 显示全部楼层
gp_Ax2(const gp_Pnt& P, const gp_Dir& N, const gp_Dir& Vx)
: axis(P, N),       // 创建基本坐标轴 P+N(即 Z 轴)
  vydir(N),         // 暂时用 N 初始化 Y 轴方向
  vxdir(N)          // 暂时用 N 初始化 X 轴方向
{
    vxdir.CrossCross(Vx, N);   // vxdir = (Vx × N) × N,即构造正交 X 方向
    vydir.Cross(vxdir);        // vydir = vxdir × N,构造正交 Y 方向
}
 楼主| 发表于 7 天前 | 显示全部楼层
if N and Vx are parallel
 楼主| 发表于 7 天前 | 显示全部楼层
#include <gp_Pnt.hxx>
#include <gp_Dir.hxx>
#include <gp_Ax2.hxx>
#include <Standard.hxx>
#include <iostream>

struct SafeAx2Result {
    gp_Ax2 axis;
    bool vxAdjusted = false; // 是否自动修正了 VX
};

// 判断两个方向是否平行(带容差)
bool AreDirsParallel(const gp_Dir& d1, const gp_Dir& d2, double angularTol = 1e-6) {
    return d1.IsParallel(d2, angularTol);
}

// 选择一个与 Z 方向不平行的备用 VX 方向
gp_Dir GetFallbackVx(const gp_Dir& zDir, double angularTol = 1e-6) {
    const gp_Dir fallbackX(1, 0, 0);
    const gp_Dir fallbackY(0, 1, 0);
    const gp_Dir fallbackZ(0, 0, 1); // 备用备用…

    if (!AreDirsParallel(zDir, fallbackX, angularTol)) return fallbackX;
    if (!AreDirsParallel(zDir, fallbackY, angularTol)) return fallbackY;

    std::cerr << "[Error] Z direction is parallel to fallback directions. Using Z default!" << std::endl;
    return fallbackZ; // 实在不行返回 Z 本身(不建议)
}

// 主函数:安全创建 gp_Ax2
SafeAx2Result MakeSafeAx2(const gp_Pnt& origin, const gp_Dir& zDir, const gp_Dir& vxHint, double angularTol = 1e-6)
{
    gp_Dir finalVx = vxHint;
    bool adjusted = false;

    if (AreDirsParallel(zDir, vxHint, angularTol))
    {
        adjusted = true;
        finalVx = GetFallbackVx(zDir, angularTol);
        std::cerr << "[Warning] Vx is parallel to Z. Adjusted Vx to fallback direction." << std::endl;
    }

    SafeAx2Result result;
    result.axis = gp_Ax2(origin, zDir, finalVx);
    result.vxAdjusted = adjusted;
    return result;
}
 楼主| 发表于 7 天前 | 显示全部楼层
gp_Pnt center(0, 0, 0);
gp_Dir zAxis(0, 0, 1);
gp_Dir vxHint(0, 0, 1); // 故意平行,触发修正

SafeAx2Result safeAxisResult = MakeSafeAx2(center, zAxis, vxHint);

if (safeAxisResult.vxAdjusted) {
    std::cout << "VX direction was automatically adjusted to avoid parallelism.\n";
}

gp_Ax2 myAxis = safeAxisResult.axis;
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2025-4-25 19:37 , Processed in 0.038140 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表