python中使用occ库编辑成函数放入窗口程序时时发生闪退

python中使用occ库编辑成函数放入窗口程序时时发生闪退
窗口程序运行时,点击相应按钮,三维模型会呈现出来然后瞬间消失

img

下面是编辑的相关函数(确认数据计算部分无误)

#三维模型
def py3d(ui):
    data3 = solve(ui)
    c1 = -data3[2] / 2
    c2 = data3[2] / 2
    c5 = -data3[5] / 2
    c6 = data3[5] / 2
    d7 = data3[3] + data3[4]
    c9 = -data3[0] / 2
    c10 = data3[0] / 2
    d11 = data3[1] + data3[3] + data3[4]

    time.sleep(0.5)
    # 三维模型建立
    E11 = BRepBuilderAPI_MakeEdge(gp_Pnt(c1, 0., 0.), gp_Pnt(c2, 0., 0.)).Edge()
    E12 = BRepBuilderAPI_MakeEdge(gp_Pnt(c2, 0., 0.), gp_Pnt(c2, data3[3], 0.)).Edge()
    E13 = BRepBuilderAPI_MakeEdge(gp_Pnt(c2, data3[3], 0.), gp_Pnt(c6, data3[3], 0.)).Edge()
    E14 = BRepBuilderAPI_MakeEdge(gp_Pnt(c6, data3[3], 0.), gp_Pnt(c6, d7, 0.)).Edge()
    E15 = BRepBuilderAPI_MakeEdge(gp_Pnt(c6, d7, 0.), gp_Pnt(c10, d7, 0.)).Edge()
    E16 = BRepBuilderAPI_MakeEdge(gp_Pnt(c10, d7, 0.), gp_Pnt(c10, d11, 0.)).Edge()
    E17 = BRepBuilderAPI_MakeEdge(gp_Pnt(c10, d11, 0.), gp_Pnt(c9, d11, 0.)).Edge()
    E18 = BRepBuilderAPI_MakeEdge(gp_Pnt(c9, d11, 0.), gp_Pnt(c9, d7, 0.)).Edge()
    E19 = BRepBuilderAPI_MakeEdge(gp_Pnt(c9, d7, 0.), gp_Pnt(c5, d7, 0.)).Edge()
    E110 = BRepBuilderAPI_MakeEdge(gp_Pnt(c5, d7, 0.), gp_Pnt(c5, data3[3], 0.)).Edge()
    E111 = BRepBuilderAPI_MakeEdge(gp_Pnt(c5, data3[3], 0.), gp_Pnt(c1, data3[3], 0.)).Edge()
    E112 = BRepBuilderAPI_MakeEdge(gp_Pnt(c1, data3[3], 0.), gp_Pnt(c1, 0., 0.)).Edge()

    W1 = BRepBuilderAPI_MakeWire(E11, E12, E13, E14)
    W2 = BRepBuilderAPI_MakeWire(E15, E16, E17, E18)
    W3 = BRepBuilderAPI_MakeWire(E19, E110, E111, E112)

    W = BRepBuilderAPI_MakeWire()
    W.Add(W1.Wire())
    W.Add(W2.Wire())
    W.Add(W3.Wire())

    S = BRepPrimAPI_MakePrism(BRepBuilderAPI_MakeFace(W.Wire()).Face(), gp_Vec(0., 0, 30000))

    from OCC.Display.SimpleGui import init_display

    display, start_display, add_menu, add_function_to_menu = init_display()
    display.DisplayShape(W.Shape(), update=True)
    display.DisplayShape(S.Shape(), update=True)

    start_display()


下面是窗口主函数

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    ui.pushButton_2.clicked.connect(partial(solve, ui))
    ui.pushButton_3.clicked.connect(partial(clear, ui))
    ui.pushButton_4.clicked.connect(partial(py3d, ui))
    ui.pushButton_5.clicked.connect(partial(pyccad, ui))


    sys.exit(app.exec_())

但是相同的模型背景在单独的py文件可以正常运行

from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire, BRepBuilderAPI_MakeFace
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakePrism
from OCC.Core.gp import gp_Pnt, gp_Vec
from scipy.optimize import minimize
import numpy as np



def fun():  # 定义目标函数
    v = lambda x: x[0] * x[1] + x[2] * x[3] + x[4] * x[5]
    return v


def constraint(args):  # 定义约束条件函数
    a1, a2, a4, a6, a7, a8, a9, a10, l, h, q = args
    # a1 = 0.5, a2 = 1/12, a3 = (q*l*l/8.0), a4 = 1000000, a5 = 1500.0, a6 = 60.0, a7 = 24.0
    # a8 = 400.0, a9 = 32.0, a10 = 80.0
    cons = ({'type': 'eq', 'fun': lambda x: x[0] + x[3] + x[5] - h},  # 等式约束
            {'type': 'ineq', 'fun': lambda x: -(x[0] - a6 * x[1])},
            {'type': 'ineq', 'fun': lambda x: -(x[2] - a7 * x[3])},
            {'type': 'ineq', 'fun': lambda x: -(x[2] - a8)},
            {'type': 'ineq', 'fun': lambda x: -(x[4] - a9 * x[5])},
            {'type': 'ineq', 'fun': lambda x: -((a4 * (q * l * l / 8.0) * (
                    x[3] + a1 * x[0] + a1 * (x[4] * x[5] * (x[5] + x[0]) - x[2] * x[3] * (x[3] + x[0])) / (
                    x[2] * x[3] + x[4] * x[5] + x[0] * x[1]))) / (a2 * (
                    x[2] * (x[3] ** 3) + x[4] * (x[5] ** 3) + x[1] * (x[0] ** 3)) + x[2] * x[3] * ((a1 * (
                    x[3] + x[0]) + a1 * (x[4] * x[5] * (x[5] + x[0]) - x[2] * x[3] * (x[3] + x[0])) / (x[2] * x[3] +
                                                                                                       x[4] * x[5] +
                                                                                                       x[0] * x[
                                                                                                           1])) ** 2) +
                                                                  x[4] * x[5] * ((a1 * (x[5] + x[0]) - a1 * (
                            x[4] * x[5] * (x[5] + x[0]) - x[2] * x[3] * (x[3] + x[0])) / (x[2] * x[3] + x[4] * x[
                        5] + x[0] * x[1])) ** 2) + x[0] * x[1] * ((a1 * (
                            x[4] * x[5] * (x[5] + x[0]) - x[2] * x[3] * (x[3] + x[0])) / (
                                                                           x[2] * x[3] + x[4] * x[5] + x[0] * x[
                                                                       1])) ** 2)) - a10)},
            {'type': 'ineq', 'fun': lambda x: -((a4 * (q * l * l / 8.0) * (
                    x[5] + a1 * x[0] - a1 * (x[4] * x[5] * (x[5] + x[0]) - x[2] * x[3] * (x[3] + x[0])) / (
                    x[2] * x[3] + x[4] * x[5] + x[0] * x[1]))) / (a2 * (
                    x[2] * (x[3] ** 3) + x[4] * (x[5] ** 3) + x[1] * (x[0] ** 3)) + x[2] * x[3] * ((a1 * (
                    x[3] + x[0]) + a1 * (x[4] * x[5] * (x[5] + x[0]) - x[2] * x[3] * (x[3] + x[0])) / (x[2] * x[3] +
                                                                                                       x[4] * x[5] +
                                                                                                       x[0] * x[
                                                                                                           1])) ** 2) +
                                                                  x[4] * x[5] * ((a1 * (x[5] + x[0]) - a1 * (
                            x[4] * x[5] * (x[5] + x[0]) - x[2] * x[3] * (x[3] + x[0])) / (x[2] * x[3] + x[4] * x[
                        5] + x[0] * x[1])) ** 2) + x[0] * x[1] * ((a1 * (
                            x[4] * x[5] * (x[5] + x[0]) - x[2] * x[3] * (x[3] + x[0])) / (
                                                                           x[2] * x[3] + x[4] * x[5] + x[0] * x[
                                                                       1])) ** 2)) - a10)},
            {'type': 'ineq', 'fun': lambda x: (x[0])},
            {'type': 'ineq', 'fun': lambda x: (x[1])},
            {'type': 'ineq', 'fun': lambda x: (x[2])},
            {'type': 'ineq', 'fun': lambda x: (x[3])},
            {'type': 'ineq', 'fun': lambda x: (x[4])},
            {'type': 'ineq', 'fun': lambda x: (x[5])},
            )
    return cons


# 输入约束条件参数和常数
l1 = float(input("请输入跨径"))
#    梁高h单位为mm
h1 = float((l1 / 20) * (10 ** 3))
#    均匀荷载q = 10 kN*m
q1 = float(35)
# 变量x[0]----hw 腹板计算高度
#    x[1]----tw 腹板厚度
#    x[2]----b1 上翼缘宽度
#    x[3]----t1 上翼缘厚度
#    x[4]----b2 下翼缘宽度
#    x[5]----t2 下翼缘厚度
#    均匀荷载q = 10 kN*m

# 定义边界约束
b = (0.0, None)
bnds = (b, b, b, b, b, b)
# 定义约束条件
args2 = (0.5, 1 / 12, 1000000, 60.0, 24.0, 800.0, 32.0, 80.0, l1, h1,
         q1)  # a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, l, h, q  # 定义约束条件中的参数
cons = constraint(args2)

# 求解优化问题
x0 = np.asarray((1430.0, 26.0, 700.0, 30.0, 840.0, 40.0))  # 定义搜索的初值
res = minimize(fun(), x0, method='SLSQP', constraints=cons, bounds=bnds)

b1 = round(res.x[2], 2)
t1 = round(res.x[3], 2)
b2 = round(res.x[4], 2)
t2 = round(res.x[5], 2)
hw = round(res.x[0], 2)
tw = round(res.x[1], 2)

c1 = -b2 / 2
c2 = b2 / 2
c5 = -tw / 2
c6 = tw / 2
d7 = t2 + hw
c9 = -b1 / 2
c10 = b1 / 2
d11 = t1 + t2 + hw

#三维模型建立
E11 = BRepBuilderAPI_MakeEdge(gp_Pnt(c1, 0., 0.), gp_Pnt(c2, 0., 0.)).Edge()
E12 = BRepBuilderAPI_MakeEdge(gp_Pnt(c2, 0., 0.), gp_Pnt(c2, t2, 0.)).Edge()
E13 = BRepBuilderAPI_MakeEdge(gp_Pnt(c2, t2, 0.), gp_Pnt(c6, t2, 0.)).Edge()
E14 = BRepBuilderAPI_MakeEdge(gp_Pnt(c6, t2, 0.), gp_Pnt(c6, d7, 0.)).Edge()
E15 = BRepBuilderAPI_MakeEdge(gp_Pnt(c6, d7, 0.), gp_Pnt(c10, d7, 0.)).Edge()
E16 = BRepBuilderAPI_MakeEdge(gp_Pnt(c10, d7, 0.), gp_Pnt(c10, d11, 0.)).Edge()
E17 = BRepBuilderAPI_MakeEdge(gp_Pnt(c10, d11, 0.), gp_Pnt(c9, d11, 0.)).Edge()
E18 = BRepBuilderAPI_MakeEdge(gp_Pnt(c9, d11, 0.), gp_Pnt(c9, d7, 0.)).Edge()
E19 = BRepBuilderAPI_MakeEdge(gp_Pnt(c9, d7, 0.), gp_Pnt(c5, d7, 0.)).Edge()
E110 = BRepBuilderAPI_MakeEdge(gp_Pnt(c5, d7, 0.), gp_Pnt(c5, t2, 0.)).Edge()
E111 = BRepBuilderAPI_MakeEdge(gp_Pnt(c5, t2, 0.), gp_Pnt(c1, t2, 0.)).Edge()
E112 = BRepBuilderAPI_MakeEdge(gp_Pnt(c1, t2, 0.), gp_Pnt(c1, 0., 0.)).Edge()

W1 = BRepBuilderAPI_MakeWire(E11, E12, E13, E14)
W2 = BRepBuilderAPI_MakeWire(E15, E16, E17, E18)
W3 = BRepBuilderAPI_MakeWire(E19, E110, E111, E112)

W = BRepBuilderAPI_MakeWire()
W.Add(W1.Wire())
W.Add(W2.Wire())
W.Add(W3.Wire())

S =BRepPrimAPI_MakePrism(BRepBuilderAPI_MakeFace(W.Wire()).Face(),gp_Vec(0.,0,30000))

if __name__ == "__main__":
    from OCC.Display.SimpleGui import init_display

    display, start_display, add_menu, add_function_to_menu = init_display()
    display.DisplayShape(W.Shape(), update=True)
    display.DisplayShape(S.Shape(), update=True)

    start_display()

根据你提供的代码,可能发生闪退的原因如下:

  1. 在窗口程序中调用py3d函数时,该函数内部调用了OCC库,并且在函数最后使用了start_display()函数,这个函数会启动OCC的图形界面

出现窗口程序闪退的原因可能有很多,这里提供一些可能的解决方案:

确认程序的依赖项是否正确安装
请确保在运行窗口程序之前,您已正确安装了与occ库相关的依赖项。例如,如果您正在使用conda作为Python环境管理器,请尝试执行以下命令来安装必要的依赖项:

img

如果您没有使用conda,则需要使用其他方法来安装这些依赖项。请注意,vtk版本必须是8.1.2,因为occ库只支持这个版本。

确认程序的路径设置是否正确
请确认您的程序中的路径设置是否正确,特别是在导入occ库时。如果您没有正确设置occ库的路径,可能会导致程序无法正常运行或闪退。

例如,如果您使用以下方式导入occ库:

img

请确认您已将OCC库所在的路径添加到PYTHONPATH环境变量中。您可以尝试在程序开头添加以下代码:

img

请将/path/to/occ替换为您的OCC库所在的路径。

确认程序是否正确设置了视图和渲染
请确认您的程序是否正确设置了视图和渲染。如果您没有正确设置视图和渲染,可能会导致模型无法正常呈现或者瞬间消失。

例如,如果您使用pyqtgraph来渲染模型,请确保您已正确设置视图和渲染,如下所示:

img

请根据您的程序和库的实际情况,正确设置视图和渲染器。

总之,窗口程序闪退的原因可能有很多,这里提供了一些可能的解决方案。如果您尝试了上面的解决方案仍然无法解决问题,请提供更多的错误信息和代码,以便更好地帮助您解决问题。