Pythonocc/Opencascade | 通过点沿直线创建管道,剖面不改变法向

13

我的最终目标如下:

我有一个庞大的数据点集,表示将通过逐层打印来打印零件的方式。我需要在这些点之间创建一条线,并沿着该线挤出一个圆形(以便重建将来打印的部件)。

我最初尝试使用样条曲线,但它会尝试创建平滑的线条,完全不遵循这些点。我尝试更改minDeg和maxDeg选项,但这仍然无法足够帮助我创建所需的实际曲线。

请参见此处的样条曲线结果

请参见此处的实际路径(上面的样条曲线是内部填充的一部分)

因此,我尝试每次仅在两个点之间创建样条曲线,然后在创建线时将它们全部加在一起。这看起来很有前途,因为现在我确实得到了锐利的拐角和穿过精确点的直线。但是,现在当我尝试沿着它挤压时,挤压剖面的法线不会随着导线的角度而改变。

这是我尝试的最后一个方法的结果

我已经花费了过去的四天时间来解决这个问题,在许多论坛和问题中尝试了很多,但在pythonocc(opencascade)的世界中感到完全迷失。

我的代码如下:

from __future__ import print_function

from OCC.gp import gp_Pnt, gp_Ax2, gp_Dir, gp_Circ
from OCC.GeomAPI import GeomAPI_PointsToBSpline
from OCC.TColgp import TColgp_Array1OfPnt
from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeEdge,         
BRepBuilderAPI_MakeWire, BRepBuilderAPI_MakeFace
from OCC.BRepOffsetAPI import BRepOffsetAPI_MakePipe

from OCC.Display.SimpleGui import init_display
display, start_display, add_menu, add_function_to_menu = init_display()

def pipe():
# the bspline path, must be a wire
# This will later be in a for loop but this is merely to validate the method         
using three different points.

array = TColgp_Array1OfPnt(1,2)
makeWire = BRepBuilderAPI_MakeWire()

point1 = gp_Pnt(0,0,0)
point2 = gp_Pnt(0,0,1)
array.SetValue(1, point1)
array.SetValue(2, point2)
spline = GeomAPI_PointsToBSpline(array).Curve()
edge = BRepBuilderAPI_MakeEdge(spline).Edge()

makeWire.Add(edge)

point1 = gp_Pnt(0, 0, 1)
point2 = gp_Pnt(0, 1, 2)
array.SetValue(1, point1)
array.SetValue(2, point2)
spline = GeomAPI_PointsToBSpline(array).Curve()
edge = BRepBuilderAPI_MakeEdge(spline).Edge()

makeWire.Add(edge)

point1 = gp_Pnt(0, 1, 2)
point2 = gp_Pnt(0, 2, 2)
array.SetValue(1, point1)
array.SetValue(2, point2)
spline = GeomAPI_PointsToBSpline(array).Curve()
edge = BRepBuilderAPI_MakeEdge(spline).Edge()

makeWire.Add(edge)

makeWire.Build()
wire = makeWire.Wire()

# the bspline profile. Profile mist be a wire/face
point = gp_Pnt(0,0,0)
dir = gp_Dir(0,0,1)
circle = gp_Circ(gp_Ax2(point,dir), 0.2)
profile_edge = BRepBuilderAPI_MakeEdge(circle).Edge()
profile_wire = BRepBuilderAPI_MakeWire(profile_edge).Wire()
profile_face = BRepBuilderAPI_MakeFace(profile_wire).Face()

# pipe
pipe = BRepOffsetAPI_MakePipe(wire, profile_face).Shape()

display.DisplayShape(profile_edge, update=False)
display.DisplayShape(wire, update=True)
display.DisplayShape(pipe, update=True)

if __name__ == '__main__':
pipe()
start_display()
1个回答

6
尽管线的边缘连接在一起,但它们并不平滑过渡。BrepOffsetAPI_MakePipe

通过沿着线骨架扫描形状 Profile 来构建管道。脊柱与剖面所成的角度沿着管道的长度保持不变。警告:脊柱必须是 G1 连续的;也就是说,在线的两个边缘的连接点上,左侧和右侧的切向量必须具有相同的方向,但不一定具有相同的大小。

另一个连续性描述可以在 这里 找到,我们需要切线(G1)。如果两个相邻的曲线在端点处不相切,则扫描将无法保持相同的角度(由脊柱和剖面组成)。

pipeNot

最简单的解决方案是切断管道。

pipeChop

def pipe(point1, point2):
    makeWire = BRepBuilderAPI_MakeWire()
    edge = BRepBuilderAPI_MakeEdge(point1, point2).Edge()
    makeWire.Add(edge)
    makeWire.Build()
    wire = makeWire.Wire()

    dir = gp_Dir(point2.X() - point1.X(), point2.Y() - point1.Y(), point2.Z() - point1.Z())
    circle = gp_Circ(gp_Ax2(point1,dir), 0.2)
    profile_edge = BRepBuilderAPI_MakeEdge(circle).Edge()
    profile_wire = BRepBuilderAPI_MakeWire(profile_edge).Wire()
    profile_face = BRepBuilderAPI_MakeFace(profile_wire).Face()
    pipe = BRepOffsetAPI_MakePipe(wire, profile_face).Shape()
    display.DisplayShape(pipe, update=True)

if __name__ == '__main__':
    pipe(gp_Pnt(0,0,0), gp_Pnt(0,0,1))
    pipe(gp_Pnt(0,0,1), gp_Pnt(0,1,2))     
    pipe(gp_Pnt(0,1,2), gp_Pnt(0,2,2))
    start_display()

我们可以添加球来填补间隙。

pipeSphere

from OCC.BRepPrimAPI import BRepPrimAPI_MakeSphere

def sphere(centre, radius):
    sphere = BRepPrimAPI_MakeSphere (centre, radius).Shape()
    display.DisplayShape(sphere, update=True)

def pipe(point1, point2):
    ...

if __name__ == '__main__':
    pipe(gp_Pnt(0,0,0), gp_Pnt(0,0,1))
    sphere(gp_Pnt(0,0,1), 0.2)
    pipe(gp_Pnt(0,0,1), gp_Pnt(0,1,2))     
    sphere(gp_Pnt(0,1,2), 0.2)
    pipe(gp_Pnt(0,1,2), gp_Pnt(0,2,2))
    start_display()

或者,您可以实现一个圆角算法,例如ChFi2d类提供的算法。鉴于激光打印的背景和算法的平面特性,我已将点映射到xy平面。

pipeFill

from OCC.ChFi2d import ChFi2d_AnaFilletAlgo

def filletEdges(ed1, ed2):
    radius = 0.3
    f = ChFi2d_AnaFilletAlgo()
    f.Init(ed1,ed2,gp_Pln())
    f.Perform(radius)
    return f.Result(ed1, ed2)

def pipe():
    # the points
    p1 = gp_Pnt(0,0,0)
    p2 = gp_Pnt(0,1,0)
    p3 = gp_Pnt(1,2,0)
    p4 = gp_Pnt(2,2,0)
    # the edges
    ed1 = BRepBuilderAPI_MakeEdge(p1,p2).Edge()
    ed2 = BRepBuilderAPI_MakeEdge(p2,p3).Edge()
    ed3 = BRepBuilderAPI_MakeEdge(p3,p4).Edge()
    # inbetween
    fillet12 = filletEdges(ed1, ed2)
    fillet23 = filletEdges(ed2, ed3) 
    # the wire
    makeWire = BRepBuilderAPI_MakeWire()
    makeWire.Add(ed1)
    makeWire.Add(fillet12)
    makeWire.Add(ed2)
    makeWire.Add(fillet23)
    makeWire.Add(ed3)
    makeWire.Build()
    wire = makeWire.Wire()
    # the pipe
    dir = gp_Dir(0,1,0)
    circle = gp_Circ(gp_Ax2(p1,dir), 0.2)
    profile_edge = BRepBuilderAPI_MakeEdge(circle).Edge()
    profile_wire = BRepBuilderAPI_MakeWire(profile_edge).Wire()
    profile_face = BRepBuilderAPI_MakeFace(profile_wire).Face()
    pipe = BRepOffsetAPI_MakePipe(wire, profile_face).Shape()
    display.DisplayShape(pipe, update=True)

if __name__ == '__main__':
    pipe()
    start_display()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接