UG NX 二次开发 移动工作坐标系(WCS) C#

项目重点:

  1. CoordinateSystem(坐标系)

  2. Origin(原点)

  3. Rotate(旋转)

  4. SetCoordinateSystem(设置坐标系)

  5. SetCoordinateSystemCartesianAtCsys(在csys设置笛卡尔坐标系)

  6. SetOriginAndMatrix(设置原点和矩阵)

1、CoordinateSystem(坐标系)

CoordinateSystem 属性是只读的,它返回一个笛卡尔坐标系对象,该对象表示 WCS 在空间中的方向(定义每个轴方向的 X、Y 和 Z 向量)。

//将方向矩阵写入信息窗口
lw.WriteLine(dispPart.WCS.CoordinateSystem.Orientation.Element.ToString());

//访问单个矩阵的方向元素,在本实例中是X轴元素
lw.WriteLine(dispPart.WCS.CoordinateSystem.Orientation.Element.Xx.ToString());
lw.WriteLine(dispPart.WCS.CoordinateSystem.Orientation.Element.Xy.ToString());
lw.WriteLine(dispPart.WCS.CoordinateSystem.Orientation.Element.Xz.ToString());

2、Origin(原点)

Origin 属性表示一个 Point3d 对象,该属性是可读写的;随意传入新的 Point3d 以更改 WCS 的位置。

3、Rotate(旋转)

Rotate方法为您提供了一种围绕其轴之一旋转 WCS 的简单方法。如果我们想让 WCS 绕其 Z 轴旋转 30 度,代码如下所示:

dispPart.WCS.Rotate(WCS.Axis.ZAxis, 30);

4、SetCoordinateSystem(设置坐标系)

SetCoordinateSystem采用笛卡尔坐标系作为参数,并返回一个笛卡尔坐标系,它表示 WCS 的先前位置。除了返回对象供程序员使用外,它还会在 NX 图形窗口中创建坐标系对象。如果用户不需要或不需要此对象,则可以在代码末尾将其删除,也可以使用不在图形窗口中创建对象的其他方法之一。

5、SetCoordinateSystemCartesianAtCsys(在csys设置笛卡尔坐标系)

它还将笛卡尔坐标系统作为参数,并返回表示先前 WCS 位置/方向的笛卡尔坐标系统对象。与前面讨论的方法的主要区别在于,此方法不会在图形窗口中创建坐标系对象。

6、SetOriginAndMatrix(设置原点和矩阵)

此方法采用两个参数:Point3d 结构和 Matrix3x3 结构。此方法不返回任何值,也不在图形窗口中创建任何对象。许多其他 NX 对象(如 Arc)将原点和方向矩阵公开为属性;这样可以轻松地从另一个对象读取这些值并将其直接分配给 WCS。


示例代码:

(代码可在操作记录中直接运行)

using System;
using NXOpen;
using System.Windows.Forms;

sealed class Module1
{
    public static void Main()
    {
        Session theSession = Session.GetSession();
        Part workPart = theSession.Parts.Work;
        Part displayPart = theSession.Parts.Display;
        ListingWindow lw = theSession.ListingWindow;
        lw.Open();
        if (ReferenceEquals(displayPart, null))
        {
            //需要活动部件
            return;
        }

        //保存当前WPS位置和方位
        Point3d oldOrigin = displayPart.WCS.Origin;
        CartesianCoordinateSystem oldOrientation = displayPart.WCS.CoordinateSystem;

        //WPS坐标沿X+方向移动10
        Point3d newOrigin = new Point3d(oldOrigin.X + 10, oldOrigin.Y, oldOrigin.Z);
 
        //移动WPS,.SetOriginAndMatrix方法不会保存旧的WPS
        displayPart.WCS.SetOriginAndMatrix(newOrigin, oldOrientation.Orientation.Element);
        MessageBox.Show("请注意,WCS已沿 + X(绝对)方向移动10");

        //旋转WPS
        displayPart.WCS.Rotate(WCS.Axis.ZAxis, 32);
        displayPart.WCS.Rotate(WCS.Axis.YAxis, 16);
        displayPart.WCS.Rotate(WCS.Axis.XAxis, 8);
        MessageBox.Show("请注意,WCS已旋转");

        //移动到绝对坐标系
        Xform absXform = displayPart.Xforms.CreateXform(SmartObject.UpdateOption.WithinModeling, 1);
        CartesianCoordinateSystem absCsys = displayPart.CoordinateSystems.CreateCoordinateSystem(absXform, SmartObject.UpdateOption.WithinModeling);

        //SetCoordinateSystem方法返回一个CartesianCoordinateSystem对象,并在图形中创建NX坐标系。
        //将它赋给一个变量,如果你以后想引用它(重复使用它,删除它,等待)
        CartesianCoordinateSystem csys2 = displayPart.WCS.SetCoordinateSystem(absCsys);
        lw.WriteLine(csys2.Orientation.Element.ToString());
        MessageBox.Show("WCS回到绝对位置,并且上一个位置已保存为NX坐标系对象(在按“确定”后,保存的csys对象可能不可见");
        Point3d pt1 = csys2.Origin;
        Point startPt = displayPart.Points.CreatePoint(pt1);
        Vector3d vec1 = new Vector3d(csys2.Orientation.Element.Xx, csys2.Orientation.Element.Xy, csys2.Orientation.Element.Xz);

        Direction dir2 = displayPart.Directions.CreateDirection(displayPart.Points.CreatePoint(pt1), vec1);
        Scalar dist2 = displayPart.Scalars.CreateScalar(2, Scalar.DimensionalityType.Length, SmartObject.UpdateOption.WithinModeling);
        Offset offset2 = displayPart.Offsets.CreateOffset(dir2, dist2, SmartObject.UpdateOption.WithinModeling);
        Point endPoint = displayPart.Points.CreatePoint(offset2, startPt, SmartObject.UpdateOption.WithinModeling);

        Scalar rotX = displayPart.Scalars.CreateScalar(30, Scalar.DimensionalityType.Angle, SmartObject.UpdateOption.WithinModeling);
        Scalar rotY = displayPart.Scalars.CreateScalar(0, Scalar.DimensionalityType.Angle, SmartObject.UpdateOption.WithinModeling);
        Scalar rotZ = displayPart.Scalars.CreateScalar(0, Scalar.DimensionalityType.Angle, SmartObject.UpdateOption.WithinModeling);

        Xform myXform = displayPart.Xforms.CreateXform(csys2, startPt, endPoint, rotX, rotY, rotZ, 0, SmartObject.UpdateOption.WithinModeling, 1);
        CartesianCoordinateSystem newCsys = displayPart.CoordinateSystems.CreateCoordinateSystem(myXform, SmartObject.UpdateOption.WithinModeling);

        //SetCoordinateSystemCartesianaAtCsys方法返回CartesianaCoordinatesSystem对象,但不会在图形窗口中创建NX坐标系对象。
        CartesianCoordinateSystem csys3 = displayPart.WCS.SetCoordinateSystemCartesianAtCsys(newCsys);

        lw.WriteLine(csys3.Orientation.Element.ToString());

        MessageBox.Show("最后,WCS已沿先前保存的Csys’X方向偏移了2个单位,并且已绕先前的Csys‘X轴旋转");

    }

    public static int GetUnloadOption(string arg)
    {
        return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
    }


}

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
UG/NX软件中,可以使用C++代码创建一个块,并在相对坐标系下定义其位置。以下是一个示例代码,可以在相对坐标系创建一个块: ```cpp #include <uf_defs.h> #include <uf_modl.h> #include <uf_curve.h> // 定义相对坐标系的原点和方向 static double origin[3] = {0.0, 0.0, 0.0}; static double x_direction[3] = {1.0, 0.0, 0.0}; static double y_direction[3] = {0.0, 1.0, 0.0}; static double z_direction[3] = {0.0, 0.0, 1.0}; // 创建块的函数 void create_block(double length, double width, double height, double position[3]) { tag_t wcs_tag = NULL_TAG; tag_t blk_tag = NULL_TAG; UF_VEC3D translation = {position[0], position[1], position[2]}; // 获取当前工作坐标系 UF_CSYS_ask_wcs(&wcs_tag); // 在当前工作坐标系创建一个块 UF_MODL_create_block1(UF_NULLSIGN, origin, x_direction, y_direction, length, width, height, &blk_tag); // 将块移动到指定的位置 UF_MODL_translate_object(blk_tag, &wcs_tag, &translation, &blk_tag); } int main(int argc, char *argv[]) { double length = 10.0; double width = 5.0; double height = 2.0; double position[3] = {1.0, 2.0, 3.0}; // 初始化UG/NX API UF_initialize(); // 创建块 create_block(length, width, height, position); // 退出UG/NX API UF_terminate(); return 0; } ``` 在上面的代码中,create_block()函数将在当前工作坐标系创建一个长为length、宽为width、高为height的块,并将其移动到位置为position的位置。块的位置是使用UF_MODL_translate_object()函数实现的,该函数将对象(在本例中为块)从一个坐标系移动到另一个坐标系。 请注意,上述示例代码仅供参考,并可能需要根据您的具体情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UG NXOpen学徒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值