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')

相关推荐

  1. python实现3d

    2024-07-11 17:26:06       32 阅读
  2. 数学中的辅助量、中间量、指示变量

    2024-07-11 17:26:06       26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 17:26:06       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 17:26:06       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 17:26:06       58 阅读
  4. Python语言-面向对象

    2024-07-11 17:26:06       69 阅读

热门阅读

  1. 实现前端登录注册功能(有源码)

    2024-07-11 17:26:06       22 阅读
  2. 编程语言:智能制造领域的智慧引擎

    2024-07-11 17:26:06       22 阅读
  3. 通过配置IP路由解决多网卡配置同网段IP的问题

    2024-07-11 17:26:06       23 阅读
  4. ArduPilot开源代码之OpticalFlow_backend

    2024-07-11 17:26:06       23 阅读
  5. Postman API测试覆盖率:全面评估指南

    2024-07-11 17:26:06       22 阅读
  6. OpenHarmony移植小型系统exynos4412(三)

    2024-07-11 17:26:06       22 阅读
  7. 达梦数据库系列—26. DSC主备搭建

    2024-07-11 17:26:06       19 阅读
  8. Mybatis进阶の常用配置&级联查询

    2024-07-11 17:26:06       22 阅读
  9. 【MyBatis】MyBatis 理论 40 问(二)

    2024-07-11 17:26:06       21 阅读
  10. Android --- Kotlin学习之路:Okhttp 同步异步网络请求

    2024-07-11 17:26:06       19 阅读
  11. tomcat

    tomcat

    2024-07-11 17:26:06      16 阅读
  12. 探索 GraphRAG:图结构与生成式模型的融合

    2024-07-11 17:26:06       23 阅读