UG二次开发NXOpen-Python(七) 点坐标的转换


NXOpen-python 点坐标的转换

  UG中有各种坐标系,比如说绝对坐标系、WCS、草图坐标系等等,当我们在UG软件中画草图时,我们输入的坐标是草图坐标,这没有什么问题,但当我们需要通过代码画草图时,问题就来了,例如,我们要在草图中画一个圆心坐标(10,10)的圆,在UG软件中,我们可以直接输入(10,10),但当我们通过代码完成这一动作时,如果还把圆心坐标设为(10,10)那就大错特错,因为NXOpen中的Point3d是以绝对坐标系构造的,如果我们要在代码中完成圆的创建,就要把圆心坐标转换为绝对坐标系中的坐标:

  通过journal也可以看出,绝对坐标系中的圆心坐标为(20.0,0.0,14.142),与我们输入的坐标(10,10)相差甚远,那么怎样转换呢?NXOpen-Python中的NXOpen.UF对uf_mtx.h中的函数进行了包装,对于上述问题,我们需要用到UFMtx4函数,NXOpen-Python将这些函数包装在class NXOpen.UF.Mtx4类中

import NXOpen
import NXOpen.UF

theSession: NXOpen.Session = NXOpen.Session.GetSession()
theUfSession: NXOpen.UF.UFSession = NXOpen.UF.UFSession.GetUFSession()
workPart: NXOpen.Part = theSession.Parts.Work
lw: NXOpen.ListingWindow = theSession.ListingWindow
lw.Open()
mtx4: NXOpen.UF.Mtx4 = theUfSession.Mtx4
mtx3: NXOpen.UF.Mtx3 = theUfSession.Mtx3

sketch: NXOpen.Sketch = list(workPart.Sketches)[
    0]  # 实际编程时在创建草图时就可以直接获取该草图对象,不用像这样
lw.WriteLine(str(sketch.Origin))
lw.WriteLine(str(sketch.Orientation.Element))
matrix_sketch = mtx4.Initialize(
    1.0,
    [sketch.Origin.X, sketch.Origin.Y, sketch.Origin.Z],
    mtx3.Transpose([sketch.Orientation.Element.Xx, sketch.Orientation.Element.Xy,
                    sketch.Orientation.Element.Xz, sketch.Orientation.Element.Yx,
                    sketch.Orientation.Element.Yy, sketch.Orientation.Element.Yz,
                    sketch.Orientation.Element.Zx, sketch.Orientation.Element.Zy,
                    sketch.Orientation.Element.Zz]))
lw.WriteLine('草图坐标(10,10)对应的绝对坐标系中坐标为' + str(mtx4.Vec3Multiply([10.0, 10.0, 0.0],
                                                               matrix_sketch)))

输出信息如下:

[X=20,Y=0,Z=0]
[Xx=-0,Xy=0.70710678118654757,Xz=0.70710678118654757,Yx=0,Yy=-0.70710678118654757,Yz=0.70710678118654757,Zx=1,Zy=0,Zz=0]
草图坐标(10,10)对应的绝对坐标系中坐标为[20.0, 0.0, 14.142135623730951]

可以看出,坐标与journal中一致。

如果坐标系只涉及旋转,不涉及平移,则Mtx3就能处理,如果坐标系原点也发生了改变,则需要借助Mtx4

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM