Python实现管线建模 - 偏心变径管、 弯头

偏心变径管

        偏心变径管是一种用于连接不同直径管道的管件,其特点是两端的中心线不在同一条直线上。偏心变径管常用于水平管道系统,以确保管道内部流体的平稳流动和避免积水。

        偏心变径管的一侧是平的,这使得两端的管道在一侧是对齐的,而另一侧存在高度差。这种结构设计使其在某些特定场景中具有独特的优势,如下图所示。

        应用场景

        水平管道系统:

        在水平管道中,使用偏心变径管可以避免积水或沉积物的堆积。平的一侧通常放置在下方,使液体流动顺畅,防止在变径处形成气袋。

        泵的入口和出口:

        在泵的入口使用偏心变径管,可以避免泵入口处的气穴现象。出口处使用偏心变径管,有助于保持流体压力的稳定。

        流体流动的平稳过渡:

        当管道系统中需要从大管径过渡到小管径时,偏心变径管可以提供平稳的流动过渡,减少流体阻力和压力损失。

将粗管和细管直接拼接在一起,构成最简单的

import trimesh
import numpy as np

def create_eccentric_reducer(diameter1=1.0, diameter2=0.5, height1=3.0, height2=2.0, offset=0.25):
    # 创建粗圆柱体
    cylinder1 = trimesh.creation.cylinder(radius=diameter1 / 2, height=height1, sections=32)
    cylinder1.apply_translation([0, 0, height1 / 2])

    # 创建细圆柱体
    cylinder2 = trimesh.creation.cylinder(radius=diameter2 / 2, height=height2, sections=32)
    cylinder2.apply_translation([offset, 0, height1 + height2 / 2])

    # 合并两个圆柱体
    combined_mesh = trimesh.util.concatenate([cylinder1, cylinder2])

    return combined_mesh

# 创建偏心异径管
eccentric_reducer = create_eccentric_reducer()

# 导出偏心异径管模型
eccentric_reducer.export('eccentric_reducer.obj')

不过这样把两个管直接拼在一起,效果很生硬、很不美观。。。

那么我们可以在两条管线之间增加一个过渡的锥体,用来实现平滑连接两条管线的效果。

import trimesh
import numpy as np

def create_smooth_eccentric_reducer(diameter1=1.0, diameter2=0.5, height1=2.0, height2=1.5, transition_length=1.0, offset=0.25):

    # 创建粗圆柱体
    cylinder1 = trimesh.creation.cylinder(radius=diameter1 / 2, height=height1, sections=32)
    cylinder1.apply_translation([0, 0, height1 / 2])
    
    # 创建细圆柱体
    cylinder2 = trimesh.creation.cylinder(radius=diameter2 / 2, height=height2, sections=32)
    cylinder2.apply_translation([offset, 0, height1 + transition_length + height2 / 2])
    
    # 创建过渡部分
    # 计算过渡部分的顶点
    angles = np.linspace(0, 2 * np.pi, 32)
    top_radius = diameter1 / 2
    bottom_radius = diameter2 / 2
    top_circle = np.column_stack((np.cos(angles) * top_radius, np.sin(angles) * top_radius, np.full(32, height1)))
    bottom_circle = np.column_stack((np.cos(angles) * bottom_radius + offset, np.sin(angles) * bottom_radius, np.full(32, height1 + transition_length)))
    
    # 创建过渡部分的面
    vertices = np.vstack((top_circle, bottom_circle))
    faces = []
    for i in range(31):
        faces.append([i, i + 1, 32 + i + 1])
        faces.append([i, 32 + i + 1, 32 + i])
    faces.append([31, 0, 32])  # 闭合环形部分
    faces.append([31, 32, 63])
    transition_mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
    
    # 组合所有部分
    combined_mesh = trimesh.util.concatenate([cylinder1, transition_mesh, cylinder2])
    return combined_mesh

# 创建偏心变径管
eccentric_reducer = create_smooth_eccentric_reducer()

# 导出模型
eccentric_reducer.export('smooth_eccentric_reducer.obj')

弯  头

        管线弯头(转折)是管道系统中用于改变流体流动方向的重要连接件。它们通常用于管道拐弯处的连接,使管道能够按照所需的角度进行转弯。

        我们可以用两条管线拼接的方式来简单地表示一个弯头:


import trimesh
import numpy as np


# 简单转折
def create_cylinder_between_points(start, end, radius, sections=32):
    # 计算长度和方向
    vector = np.array(end) - np.array(start)
    length = np.linalg.norm(vector)
    direction = vector / length

    # 创建圆柱体
    cylinder = trimesh.creation.cylinder(radius=radius, height=length, sections=sections)

    # 计算旋转矩阵
    z_axis = np.array([0, 0, 1])
    rotation_axis = np.cross(z_axis, direction)
    rotation_angle = np.arccos(np.dot(z_axis, direction))
    rotation_matrix = trimesh.transformations.rotation_matrix(rotation_angle, rotation_axis)

    # 应用旋转和平移
    cylinder.apply_transform(rotation_matrix)
    cylinder.apply_translation(start + vector / 2)

    return cylinder

def create_elbow_fitting():
    # 定义管线的起终点坐标和管径
    start_A, end_A = [0, 0, 0], [1, 0, 0]  # 管线A
    start_B, end_B = [1, 0, 0], [1, 1, 0]  # 管线B
    radius_A = 0.5 / 2
    radius_B = 0.5 / 2

    # 分别创建管线
    cylinder_A = create_cylinder_between_points(start_A, end_A, radius_A)
    cylinder_B = create_cylinder_between_points(start_B, end_B, radius_B)

    # 组合管线
    elbow_fitting = trimesh.util.concatenate([cylinder_A, cylinder_B])
    
    return elbow_fitting

# 创建转折模型
elbow_fitting = create_elbow_fitting()

# 可视化模型
elbow_fitting.show()

# 导出模型
elbow_fitting.export('wantou1.obj')

        不过这种表示方法的缺点很明显,管线转折处会有明显的缝隙。

        我们可以给转折点填充一个球体(球体直径 = 管线管径),用来避免转折点的缝隙:

import trimesh
import numpy as np


# 90°转折+球填充
def create_cylinder_between_points(start, end, radius, sections=32):
    # 计算长度和方向
    vector = np.array(end) - np.array(start)
    length = np.linalg.norm(vector)
    direction = vector / length

    # 创建圆柱体
    cylinder = trimesh.creation.cylinder(radius=radius, height=length, sections=sections)

    # 计算旋转矩阵
    z_axis = np.array([0, 0, 1])
    rotation_axis = np.cross(z_axis, direction)
    rotation_angle = np.arccos(np.dot(z_axis, direction))
    rotation_matrix = trimesh.transformations.rotation_matrix(rotation_angle, rotation_axis)

    # 应用旋转和平移
    cylinder.apply_transform(rotation_matrix)
    cylinder.apply_translation(start + vector / 2)

    return cylinder

def create_elbow_fitting():
    # 定义管线的起终点坐标和管径
    start_A, end_A = [0, 0, 0], [1, 0, 0]  # 管线A
    start_B, end_B = [1, 0, 0], [1, 1, 0]  # 管线B
    radius_A = 0.5 / 2
    radius_B = 0.5 / 2

    # 创建直管部分
    cylinder_A = create_cylinder_between_points(start_A, end_A, radius_A)
    cylinder_B = create_cylinder_between_points(start_B, end_B, radius_B)

    # 创建球体来填补转折处的缝隙
    sphere_radius = max(radius_A, radius_B)
    sphere = trimesh.creation.icosphere(subdivisions=3, radius=sphere_radius)
    sphere.apply_translation([1, 0, 0])

    # 组合所有部分
    elbow_fitting = trimesh.util.concatenate([cylinder_A, sphere, cylinder_B])
    
    return elbow_fitting

# 创建转折模型
elbow_fitting = create_elbow_fitting()

# 可视化模型
elbow_fitting.show()

# 导出模型
elbow_fitting.export('wantou2.obj')

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值