pythonocc常见问题集锦

总目录 >> PythonOCC入门进阶到实战(目前已更新入门篇、基础篇和进阶篇)

如果你有什么不懂,欢迎加入pythonocc中文社区:860536842
工作招募,一起来实现国产云端CAD—> 几何算法开发工程师(三维CAD)

在线文档: https://docs.qq.com/doc/Bn4shC4LUrL94tF82G4A4mik3hICVM0QiLYO3IQmKC2Cjyb924XTsd1cMhD11MsYQr3XBTqn1

pythonocc 常见问题集锦

你在使用过程中可能面临着各种各样的问题,此部分的问题,一是来源于我自己遇到的和网友询问我的,二是从pythonocc社区上搬运过来的(因为那里是全英文,很多人可能看不懂,我会尽力去翻译),我会尽量更新最新版


hello.
你好
I’d like to know what function is available if I import IFSelect_RetDone and IFSelect_ItemsByEntity class from OCC.IFSelect module.
我想知道如果我从OCC.IFSelect模块中导入IFSelect_RetDone和IFSelect_ItemsByEntity函数 会得到什么功能?
There is no description about those classes in module index page of pythonocc.org .
在pythonocc官方主页中没有相关描述
thank you


解答:See the official opencascade documentation https://www.opencascade.com/doc/occt-6.9.0/refman/html/_i_f_select___return_status_8hxx.html and https://www.opencascade.com/doc/occt-6.9.1/refman/html/_i_f_select___print_count_8hxx.html
去看官方opencascade文档,链接如上


sir,
I’ve trying to detect the RGB value in my project. I used AIS_ColoredShape class to color different faces and now i wanna detect the color of clicked face.
先生,在我的项目里,我想去测试我的RGB值,我使用了AIS_ColoredShape模块给面上了不同的色,现在我想获取 被点击的面的颜色
So if you know any method to do the same please help me out. I’m an under graduate and is beginner in OCC please respond.
因此如果你知道任何解决这个问题的方法请帮助我,我是一个毕业生,同时也是OCC的入门者,请回复,谢谢你了
THANK YOU!!


解答:Just use the Color() method inherited from AIS_InteractiveObject
使用 AIS_InteractiveObject的Color()函数


I want to calculate the point of intersection with line and surface.
我想去计算线和面的交点
Line is given by a point and direction.
线是通过一个点和一个方向来绘制的
Surface is given by the location and surface data.
面是通过位置和面的信息构成的
If the surface is plane (no curvature), I can calculate the point of intersection by IntAna_IntConicQuad. But, this method can’t be applied for curved surface.
如果这个面是平面(没有曲率),我能通过IntAna_IntConicQuad来计算出交点。但是,这个方法不适用于曲面
In addition, I want to the normal vector at the point of intersection on the surface.
此外,我想知道 点在该面上的法向量

import sys, time, os
import random

from OCC.Display.SimpleGui import init_display
from OCC.gp import gp_Pnt, gp_Ax1, gp_Ax2, gp_Ax3, gp_Vec, gp_Dir
from OCC.gp import gp_Pln, gp_Lin, gp_Translation, gp_Trsf
from OCC.Geom           import Geom_Plane, Geom_Surface, Geom_BSplineSurface
from OCC.Geom           import Geom_Curve, Geom_Line
from OCC.GeomAPI        import GeomAPI_PointsToBSplineSurface
from OCC.GeomAbs        import GeomAbs_C0, GeomAbs_C1, GeomAbs_G1, GeomAbs_G2
from OCC.GeomLProp      import GeomLProp_SurfaceTool
from OCC.TColgp         import TColgp_Array1OfPnt, TColgp_Array2OfPnt
from OCC.TColStd        import TColStd_Array1OfReal, TColStd_Array1OfInteger
from OCC.TopLoc         import TopLoc_Location
from OCC.TopoDS         import TopoDS_Vertex, TopoDS_Builder, TopoDS_Face
from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeFace
from OCC.BRep           import BRep_Tool
from OCCUtils.Construct import make_plane, make_line, make_edge, make_vertex
from OCCUtils.Construct import dir_to_vec, vec_to_dir
from OCCUtils.Common    import project_point_on_plane

def make_face(px, py, pz):
    nx, ny = px.shape
    print (px.shape, pz.shape)
    pnt_2d = TColgp_Array2OfPnt(1, nx, 1, ny)
    print (pnt_2d.LowerRow(), pnt_2d.UpperRow())
    print (pnt_2d.LowerCol(), pnt_2d.UpperCol())
    for row in range (pnt_2d.LowerRow(), pnt_2d.UpperRow()+1):
        for col in range (pnt_2d.LowerCol(), pnt_2d.UpperCol()+1):
            i,j = row-1, col-1
            pnt = gp_Pnt (px[i, j], py[i, j], pz[i, j])
            pnt_2d.SetValue(row, col, pnt)
            #print (row, col, i, j, px[i, j], py[i, j], pz[i, j])
    curve = GeomAPI_PointsToBSplineSurface(pnt_2d, 3, 8, GeomAbs_G2, 0.001).Surface()
    return curve

if __name__ == '__main__':
    lxy = [-100, -100, 100, 100]
    nxy = [300, 400]
    px  = np.linspace (lxy[0], lxy[2], nxy[0])
    py  = np.linspace (lxy[1], lxy[3], nxy[1])
    print (px[0], px[-1], px.shape)
    print (py[0], py[-1], py.shape)
    mesh = np.meshgrid (px, py)
    surf = mesh[0]**2/500 + mesh[1]**2/600 + mesh[0]*mesh[1]/20000 # arbitrary function or data
    spl_geom = make_face (*mesh, surf)
    spl_objt = spl_geom.GetObject()
    spl_face = BRepBuilderAPI_MakeFace(spl_geom, 0, 1, 0, 1, 0.001).Face()
    
    ax0 = gp_Ax3()
    ax1 = gp_Ax3(gp_Pnt(0, 0, 100), gp_Dir(0, 0.1, 1))
    trf_face = gp_Trsf ()
    trf_face.SetTransformation(ax1, ax0)
    loc_face = TopLoc_Location (trf_face) 
    spl_face.Location(loc_face)
    spl_surf = BRep_Tool.Surface(spl_face)
    print (spl_geom, spl_objt)
    print (spl_face, spl_surf)

    p00, p01 = gp_Pnt(), gp_Pnt() 
    p10, p11 = gp_Pnt(), gp_Pnt() 
    pu, pv   = random.uniform(0,1), random.uniform(0,1) 
    pnt = gp_Pnt()
    ray = Geom_Line (gp_Lin (pnt, gp_Dir(0, -0.1, 1)))
    GeomLProp_SurfaceTool.Value (spl_surf, 0.0, 0.0, p00)
    GeomLProp_SurfaceTool.Value (spl_surf, 0.0, 1.0, p01)
    GeomLProp_SurfaceTool.Value (spl_surf, 1.0, 0.0, p10)
    GeomLProp_SurfaceTool.Value (spl_surf, 1.0, 1.0, p11)
    GeomLProp_SurfaceTool.Value (spl_surf, pu , pv , pnt)
    print (p00)
    print (p01)
    print (p10)
    print (p11)
    print (pu, pv, pnt)
    
    display, start_display, add_menu, add_function_to_menu = init_display()
    display.DisplayShape (spl_face)
    display.DisplayShape (gp_Pnt())
    display.DisplayShape (ray)

    display.FitAll()
    start_display()

解答:Try GeomAPI_IntCS.
试一试GeomAPI_IntCS模块
Then, use the method GeomAPI_IntCS::Parameters to get the (u,v) parameters on the surface. Evaluate the u- and v-direction first derivatives at that point and then take the cross product to get the normal.
使用这个方法(GeomAPI_IntCS),你就可以获得面的uv参数信息,评估u向和v向,在该点 微分,然后叉乘(注:一种数学运算)获取法向信息。

import numpy as np
import sys, time, os
import random

from OCC.Display.SimpleGui import init_display
from OCC.gp import gp_Pnt, gp_Ax1, gp_Ax2, gp_Ax3, gp_Vec, gp_Dir
from OCC.gp import gp_Pln, gp_Lin, gp_Translation, gp_Trsf
from OCC.Geom           import Geom_Plane, Geom_Surface, Geom_BSplineSurface
from OCC.Geom           import Geom_Curve, Geom_Line
from OCC.GeomAPI        import GeomAPI_PointsToBSplineSurface, GeomAPI_IntCS
from OCC.GeomAbs        import GeomAbs_C0, GeomAbs_C1, GeomAbs_G1, GeomAbs_G2
from OCC.GeomLProp      import GeomLProp_SurfaceTool
from OCC.TColgp         import TColgp_Array1OfPnt, TColgp_Array2OfPnt
from OCC.TColStd        import TColStd_Array1OfReal, TColStd_Array1OfInteger
from OCC.TopLoc         import TopLoc_Location
from OCC.TopoDS         import TopoDS_Vertex, TopoDS_Builder, TopoDS_Face
from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeFace
from OCC.BRep           import BRep_Tool
from OCCUtils.Construct import make_plane, make_line, make_edge, make_vertex
from OCCUtils.Construct import dir_to_vec, vec_to_dir
from OCCUtils.Common    import project_point_on_plane

def make_face(px, py, pz):
    nx, ny = px.shape
    print (px.shape, pz.shape)
    pnt_2d = TColgp_Array2OfPnt(1, nx, 1, ny)
    print (pnt_2d.LowerRow(), pnt_2d.UpperRow())
    print (pnt_2d.LowerCol(), pnt_2d.UpperCol())
    for row in range (pnt_2d.LowerRow(), pnt_2d.UpperRow()+1):
        for col in range (pnt_2d.LowerCol(), pnt_2d.UpperCol()+1):
            i,j = row-1, col-1
            pnt = gp_Pnt (px[i, j], py[i, j], pz[i, j])
            pnt_2d.SetValue(row, col, pnt)
            #print (row, col, i, j, px[i, j], py[i, j], pz[i, j])
    curve = GeomAPI_PointsToBSplineSurface(pnt_2d, 3, 8, GeomAbs_G2, 0.001).Surface()
    return curve

def reflection (h_surf, pnt, vec):
    ray = Geom_Line (gp_Lin (pnt, vec_to_dir(vec.Normalized())))
    uvw = GeomAPI_IntCS (ray.GetHandle(), h_surf).Parameters(1)
    u, v, w = uvw
    p, vx, vy = gp_Pnt(), gp_Vec(), gp_Vec()
    GeomLProp_SurfaceTool.D1 (h_surf, u, v, p, vx, vy)
    vz = vx.Crossed(vy)
    vx.Normalize()
    vy.Normalize()
    vz.Normalize()
    v = v0.Mirrored(gp_Ax2(pnt, vec_to_dir(vz)))
    return p, v
    
if __name__ == '__main__':
    lxy = [-100, -100, 100, 100]
    nxy = [300, 400]
    px  = np.linspace (lxy[0], lxy[2], nxy[0])
    py  = np.linspace (lxy[1], lxy[3], nxy[1])
    print (px[0], px[-1], px.shape)
    print (py[0], py[-1], py.shape)
    mesh = np.meshgrid (px, py)
    surf = mesh[0]**2/500 + mesh[1]**2/600 + mesh[0]*mesh[1]/20000 # arbitrary function or data
    spl_geom = make_face (*mesh, surf)
    spl_objt = spl_geom.GetObject()
    spl_face = BRepBuilderAPI_MakeFace(spl_geom, 0, 1, 0, 1, 0.001).Face()
    
    ax0 = gp_Ax3()
    ax1 = gp_Ax3(gp_Pnt(0, 0, 100), gp_Dir(0, 0, 1))
    trf_face = gp_Trsf ()
    trf_face.SetTransformation(ax1, ax0)
    loc_face = TopLoc_Location (trf_face) 
    spl_face.Location(loc_face)
    spl_surf = BRep_Tool.Surface(spl_face)
    print (spl_geom, spl_objt)
    print (spl_face, spl_surf)

    p0, v0 = gp_Pnt(), gp_Vec(0, -0.1, 1.0).Normalized()
    p1, v1 = reflection (spl_surf, p0, v0)
    p2 = gp_Pnt((gp_Vec(p1.XYZ()) + v1*50).XYZ())
    ray01 = make_edge (p0, p1)
    ray12 = make_edge (p1, p2)
    
    display, start_display, add_menu, add_function_to_menu = init_display()
    display.DisplayShape (spl_face)
    display.DisplayVector (v0*10, p0)
    display.DisplayVector (v1*10, p1)
    display.DisplayShape (ray01)
    display.DisplayShape (ray12)

    display.FitAll()
    start_display()

© 版权声明
THE END
喜欢就支持一下吧
点赞102 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容