UG NX 二次开发 移动工作坐标系(WCS) VB.NET

 项目重点:

  1. CoordinateSystem(坐标系)

  2. Origin(原点)

  3. Rotate(旋转)

  4. SetCoordinateSystem(设置坐标系)

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

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

1、CoordinateSystem(坐标系)

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

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

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

2、Origin(原点)

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

3、Rotate(旋转)

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

displayPart.WCS.Rotate(WCS.Axis.ZAxis, 30)

4、SetCoordinateSystem(设置坐标系)

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

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

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

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

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


示例代码:

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

Option Strict Off
Imports System
Imports NXOpen
 
Module Module1
 
	Sub Main()
 
	Dim theSession As Session = Session.GetSession()
        Dim workPart As Part = theSession.Parts.Work
        Dim displayPart As Part = theSession.Parts.Display
 
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()
 
        If IsNothing(displayPart) Then
            '需要活动部件
            Return
        End If
 
        '保存当前WPS位置和方位
        Dim oldOrigin As Point3d = displayPart.WCS.Origin
        Dim oldOrientation As CartesianCoordinateSystem = displayPart.WCS.CoordinateSystem
 
        'WPS坐标沿X+方向移动10
        Dim newOrigin As New Point3d(oldOrigin.X + 10, oldOrigin.Y, oldOrigin.Z)
 
        '移动WPS,.SetOriginAndMatrix方法不会保存旧的WPS
        displayPart.WCS.SetOriginAndMatrix(newOrigin, oldOrientation.Orientation.Element)
 
        MsgBox("Notice the WCS has moved in the +X (absolute) direction")
 
        '旋转WPS
        displayPart.WCS.Rotate(WCS.Axis.ZAxis, 32)
        displayPart.WCS.Rotate(WCS.Axis.YAxis, 16)
        displayPart.WCS.Rotate(WCS.Axis.XAxis, 8)
 
        MsgBox("Notice the WCS has been rotated")
 
        '移动到绝对坐标系
        Dim absXform As Xform = displayPart.Xforms.CreateXform(SmartObject.UpdateOption.WithinModeling, 1)
        Dim absCsys As CartesianCoordinateSystem = displayPart.CoordinateSystems.CreateCoordinateSystem(absXform, SmartObject.UpdateOption.WithinModeling)
 
        'SetCoordinateSystem方法返回一个CartesianCoordinateSystem对象,并在图形中创建NX坐标系。
        '将它赋给一个变量,如果你以后想引用它(重复使用它,删除它,等待)
        Dim csys2 As CartesianCoordinateSystem = displayPart.WCS.SetCoordinateSystem(absCsys)
 
        'lw.WriteLine(csys2.Orientation.Element.ToString)
        MsgBox("WCS回到绝对位置,并且上一个位置已保存为NX坐标系对象(在按“确定”后,保存的csys对象可能不可见")
 
        Dim pt1 As Point3d = csys2.Origin
        Dim startPt As Point = displayPart.Points.CreatePoint(pt1)
        Dim vec1 As New Vector3d(csys2.Orientation.Element.Xx, csys2.Orientation.Element.Xy, csys2.Orientation.Element.Xz)
 
        Dim dir2 As Direction = displayPart.Directions.CreateDirection(displayPart.Points.CreatePoint(pt1), vec1)
        Dim dist2 As Scalar = displayPart.Scalars.CreateScalar(2, Scalar.DimensionalityType.Length, SmartObject.UpdateOption.WithinModeling)
        Dim offset2 As Offset = displayPart.Offsets.CreateOffset(dir2, dist2, SmartObject.UpdateOption.WithinModeling)
        Dim endPoint As Point = displayPart.Points.CreatePoint(offset2, startPt, SmartObject.UpdateOption.WithinModeling)
 
        Dim rotX As Scalar = displayPart.Scalars.CreateScalar(30, Scalar.DimensionalityType.Angle, SmartObject.UpdateOption.WithinModeling)
        Dim rotY As Scalar = displayPart.Scalars.CreateScalar(0, Scalar.DimensionalityType.Angle, SmartObject.UpdateOption.WithinModeling)
        Dim rotZ As Scalar = displayPart.Scalars.CreateScalar(0, Scalar.DimensionalityType.Angle, SmartObject.UpdateOption.WithinModeling)
 
 
        Dim myXform As Xform = displayPart.Xforms.CreateXform(csys2, startPt, endPoint, rotX, rotY, rotZ, 0, SmartObject.UpdateOption.WithinModeling, 1)
        Dim newCsys As CartesianCoordinateSystem = displayPart.CoordinateSystems.CreateCoordinateSystem(myXform, SmartObject.UpdateOption.WithinModeling)
 
        'SetCoordinateSystemCartesianaAtCsys方法返回CartesianaCoordinatesSystem对象,但不会在图形窗口中创建NX坐标系对象。
        Dim csys3 As CartesianCoordinateSystem = displayPart.WCS.SetCoordinateSystemCartesianAtCsys(newCsys)
 
        'lw.WriteLine(csys3.Orientation.Element.ToString)
 
        MsgBox("最后,WCS已沿先前保存的Csys’X方向偏移了2个单位,并且已绕先前的Csys‘X轴旋转")
 
	End Sub
 
End Module

UG(现在称为Siemens NX)是一款高级的计算机辅助设计、制造和工程分析软件。二次开发通常使用NX Open API来实现,可以通过多种编程语言,如C++, C#, Java等来进行。在UG中进行二次开发时,如果您希望编写代码来按照工作坐标(Work Coordinate System, WCS)摆正模型,可以使用NX Open来调用相应的功能。 以下是一个使用NX Open的C++示例代码片段,演示如何在UG/NX环境中设置工作坐标系: ```cpp #include <NXOpen/NXException.hxx> #include <NXOpen/Session.hxx> #include <NXOpen/BasePart.hxx> #include <NXOpen/Part.hxx> #include <NXOpen/PartCollection.hxx> #include <NXOpen/DisplayableObject.hxx> #include <NXOpen/libnxopencpp_features.hxx> using namespace NXOpen; void SetWcsToFace(BasePart* thePart, Tag faceTag) { try { // 获取会话对象 Session *session = Session::GetSession(); // 通过faceTag找到对应的面 Part *part = session->Parts()->Work(); DisplayableObject *face = part->FindObject(faceTag); // 获取面的中心点 Point3d centerPoint; face->GetClosestPointToFaceCenter(centerPoint); // 设置工作坐标系到面的中心点 Matrix3x3 rotation; PartCollection *partCollection = session->Parts(); Part *workPart = partCollection->Work(); Point3d origin(0.0, 0.0, 0.0); workPart->SetOrigin(origin, rotation, centerPoint, 1, 1, 1); // 可能还需要设置方向 // ... } catch (const NXException& ex) { // 错误处理 ex.PrintErrorMessage(); } } ``` 请注意,上述代码是用C++语言编写的,并且可能需要根据您的实际需求和环境进行调整。NX Open API非常灵活,允许您通过多种方式实现所需功能。设置WCS到特定位置或方向通常涉及到获取模型元素的位置信息(例如,面的中心点),然后使用这些信息来更新WCS的位置和方向。 在实际使用中,您可能还需要确保您的代码能够在正确的工作环境中运行,并且所有的NX Open API函数调用都符合您的具体版本和API规范。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

UG NXOpen学徒

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

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

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

打赏作者

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

抵扣说明:

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

余额充值