第二章:Pythonocc官方demo 案例25(质心 静力矩获取)

源代码:

from __future__ import print_function
import sys

from OCC.Core.BRepGProp import brepgprop_LinearProperties
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeSphere
from OCC.Display.SimpleGui import init_display
from OCC.Display.backend import get_qt_modules
from OCC.Core.GProp import GProp_GProps
from OCC.Core.TopAbs import TopAbs_SOLID, TopAbs_EDGE, TopAbs_FACE
from OCC.Core.TopLoc import TopLoc_Location
from OCC.Core.gp import gp_Trsf, gp_Vec

display, start_display, add_menu, add_function_to_menu = init_display("qt-pyqt5")
QtCore, QtGui, QtWidgets, QtOpenGL = get_qt_modules()

from OCC.Display.qtDisplay import qtViewer3d

print("Usage: press G to find the linear properties for volume, face, edge, vertex...")


def get_occ_viewer():
    """

    Returns
    -------

    qtViewer3d

    """
    app = QtWidgets.QApplication.instance()  # checks if QApplication already exists
    if not app:
        app = QtWidgets.QApplication(sys.argv)
    widgets = app.topLevelWidgets()
    for wi in widgets:
        if hasattr(wi, "_menus"):  # OCC.Display.SimpleGui.MainWindow
            viewer = wi.findChild(qtViewer3d, "qt_viewer_3d")
            return viewer


def on_select(shapes):
    """

    Parameters
    ----------
    shape : TopoDS_Shape

    """
    g1 = GProp_GProps()

    for shape in shapes:
        brepgprop_LinearProperties(shape, g1)
        mass = g1.Mass()
        centre_of_mass = g1.CentreOfMass()
        com_x = centre_of_mass.X()
        com_y = centre_of_mass.Y()
        com_z = centre_of_mass.Z()
        static_moments = g1.StaticMoments()
        print("shape {shape}: \n mass: {mass}"
              "\n center of mass: {com_x}, {com_y}, {com_z}"
              "\n static moments: {static_moments}".format(**vars()))


def also_on_select(shapes):
    for shape in shapes:
        if shape.ShapeType() == TopAbs_SOLID:
            print("solid selected")
        if shape.ShapeType() == TopAbs_EDGE:
            print("edge selected")
        if shape.ShapeType() == TopAbs_FACE:
            print("face selected")

def location_from_vector(x, y, z):
    trsf = gp_Trsf()
    trsf.SetTranslation(gp_Vec(x, y, z))
    loc = TopLoc_Location(trsf)
    return loc

cube = BRepPrimAPI_MakeBox(100, 100, 100).Shape()
sphere = BRepPrimAPI_MakeSphere(100).Shape()
sphere.Move(location_from_vector(500, 0, 0))

display.DisplayShape(cube)
display.DisplayShape(sphere)

viewer = get_occ_viewer()
viewer.sig_topods_selected.connect(on_select)
viewer.sig_topods_selected.connect(also_on_select)

display.FitAll()
start_display()

运行效果:质心 静力矩获取
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宁波莱布尼茨信息技术有限公司

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值