天天看點

pythonocc常見問題集錦

總目錄 >> PythonOCC入門進階到實戰(目前已更新入門篇、基礎篇和進階篇)

如果你有什麼不懂,歡迎加入pythonocc中文社群:860536842

工作招募,一起來實作國産雲端CAD—> 幾何算法開發工程師(三維CAD)

線上文檔: https://docs.qq.com/doc/Bn4shC4LUrL94tF82G4A4mik3hICVM0QiLYO3IQmKC2Cjyb924XTsd1cMhD11MsYQr3XBTqn1

pythonocc 常見問題集錦

你在使用過程中可能面臨着各種各樣的問題,此部分的問題,一是來源于我自己遇到的和網友詢問我的,二是從pythonocc社群上搬運過來的(因為那裡是全英文,很多人可能看不懂,我會盡力去翻譯),我會盡量更新最新版

  • 問題1. Issue on OCC.IFSelect module(cityofstaaars commented on 11 Aug)

    OCC.IFSelect 的問題(2018.8.11由cityofstaaars 提出)

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文檔,連結如上

  • 問題2.how to detect the RGB value of the face

    如何獲得面的RGB值?(2018.7.16)

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()函數

  • 問題3.How to calculate the point of intersection with line and surface?

    如何計算線和面的交點?(2018.4.19)

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()
           
  • 問題4Extract advanced face names from STEP file