pythonOCC 将二维坐标转化为三维坐标

OCC 当中提供了多种方式转换

直接转换为三维坐标

使用 V3d_View.ProjReferenceAxe()会返回有6个元素的元组,前三位分别对应 XYZ

例子 self._display.View.ProjReferenceAxe()

但是,这种方式转换的坐标让人有点摸不着头脑,不推荐

 

通过求交点获取

这种方式会把鼠标限制与某一个面上,方便

def ConvertPos(self, x:int, y:int, PlaneOfTheView:gp_Pln = None):
        '''
            Convert 2d pos to 3d pos
        '''
        try:
            X,Y,Z,VX,VY,VZ = self._display.View.ConvertWithProj(x, y)
            P1 = gp_Pnt()
            Vp2 = gp_Vec()
            P1.SetCoord(X, Y, Z)
            Vp2.SetCoord(VX,VY,VZ)
            gpLin = gp_Lin(P1, gp_Dir(Vp2))
            aCurve = Geom_Line(gpLin)

            if PlaneOfTheView is None:
                PlaneOfTheView = Geom_Plane(self.activity_plane)
            CS = GeomAPI_IntCS(aCurve, PlaneOfTheView)
            if CS.IsDone():
                point = CS.Point(1)
                return point.X(), point.Y(), point.Z()
        except Exception as e:
            logging.error(e)

这个函数已经合入了我的  potato-pythonocc ,欢迎前来试用

posted @ 2023-08-16 16:46  Ctrl+c_Ctrl+v程序员  阅读(89)  评论(0编辑  收藏  举报