pythonocc 计算周长,面积,体积

from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Pln
from OCC.Core.GC import GC_MakeSegment
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge,BRepBuilderAPI_MakePolygon, BRepBuilderAPI_MakeFace
from OCC.Core.BRepGProp import brepgprop_LinearProperties, brepgprop_SurfaceProperties, brepgprop_VolumeProperties
from OCC.Core.GProp import GProp_GProps

from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox

 

def GetTrapezoid2dShape(x0, x1, x2, x3, z0, z1, aPlane):
    # ZOX plane
    # x0 is bottom left, x1 is bottom right, x2 is top right, x3 is top left
    # z0 is bottom, z1 is top
    aP1 = gp_Pnt(x0, 0.0, z0)
    aP2 = gp_Pnt(x1, 0.0, z0)
    aP3 = gp_Pnt(x2, 0.0, z1)
    aP4 = gp_Pnt(x3, 0.0, z1)
    aPolygon = BRepBuilderAPI_MakePolygon(aP1, aP2, aP3, aP4, True)
    return BRepBuilderAPI_MakeFace(aPlane, aPolygon.Wire()).Shape()

def GetBoxShape(box_corner, width = 1.0, height = 2.0, depth = 3.0):
    # width: 宽,dx  height:长,dy  depth:高,dz box_corner:box的左下角的点
    box = BRepPrimAPI_MakeBox(box_corner, width, height, depth).Shape()

    return box

if __name__ == '__main__':
    
    aP1 = gp_Pnt(0, 0.0, 1)
    aP2 = gp_Pnt(13, 0.0, 1)
    aSegment12 = GC_MakeSegment(aP1, aP2)
    aEdge12 = BRepBuilderAPI_MakeEdge(aSegment12.Value()).Edge()
    system = GProp_GProps()
    brepgprop_LinearProperties(aEdge12, system)
    print("aEdge12.Mass(): ", system.Mass())   # 周长
    
    p0 = gp_Pnt()
    vnorm = gp_Dir(0, 1, 0)
    aPlane = gp_Pln(p0, vnorm)
    
    aTrapezoidShape = shape2d.GetTrapezoid2dShape(-15, 15, 5, -5, 0, 10, aPlane)
    brepgprop_SurfaceProperties(aTrapezoidShape, system)
    print("aTrapezoidShape.Mass(): ", system.Mass()) # 面积

    aFilmShape = shape3d.GetBoxShape(gp_Pnt(-50, -50, 0), 100, 100, 1)
    brepgprop_VolumeProperties(aFilmShape, system)
    print("aTrapezoidShape.Mass(): ", system.Mass()) # 体积

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是计算周长面积体积的代码示例: 引用中的代码是使用printf函数输出计算结果,而引用中的代码是使用Python编写的模型类,可以计算圆的周长面积体积,并画出这个圆。引用中的代码是使用C语言编写的程序,可以输入圆的半径和圆柱的高,计算圆的周长面积和圆柱的体积,并输出结果。 以下是使用Python编写的计算周长面积体积的代码示例: ``` import math class Circle: def __init__(self, radius): self.radius = radius def circumference(self): return 2 * math.pi * self.radius def area(self): return math.pi * self.radius ** 2 def volume(self): return 4 / 3 * math.pi * self.radius ** 3 # 示例用法 circle = Circle(5) print("圆的周长:", circle.circumference()) print("圆的面积:", circle.area()) print("球体的体积:", circle.volume()) ``` 以上代码定义了一个Circle类,包含了圆的半径和计算周长面积体积的方法。示例用法中创建了一个半径为5的圆,并输出了计算结果。 以下是使用C语言编写的计算周长面积体积的代码示例: ``` #include <stdio.h> int main() { float radius, height, circumference, area, volume; printf("请输入圆的半径:"); scanf("%f", &radius); printf("请输入圆柱的高:"); scanf("%f", &height); circumference = 2 * 3.14 * radius; area = 3.14 * radius * radius; volume = area * height; printf("圆的周长:%.2f\n", circumference); printf("圆的面积:%.2f\n", area); printf("圆柱的体积:%.2f\n", volume); return 0; } ``` 以上代码使用scanf函数输入圆的半径和圆柱的高,计算圆的周长面积和圆柱的体积,并使用printf函数输出结果。注意输出结果时要保留小数点后两位数字。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值