首页 > 编程语言 >取STL最大连通区域并写入体积信息python实现

取STL最大连通区域并写入体积信息python实现

时间:2024-06-03 10:54:35浏览次数:10  
标签:STL component args 写入 python mesh file largest

import trimesh
import numpy as np
import argparse
from stl import Mesh

def main(input_file, output_file, num, volume_info):
    # 加载STL文件
    your_mesh = trimesh.load_mesh(input_file)
    # 分割成连通域
    connected_components = your_mesh.split()

    # 找到最大的num个连通域
    largest_components = sorted(connected_components, key=lambda component: component.area, reverse=True)[:num]

    # 计算所有连通域的面数总和
    total_faces = sum([comp.faces.shape[0] for comp in largest_components])

    # 创建一个足够大的mesh.Mesh对象来保存所有连通域
    combined_mesh = Mesh(np.zeros(total_faces, dtype=Mesh.dtype))

    # 当前在combined_mesh中的面的索引
    current_face_index = 0

    for largest_component in largest_components:
        for i, f in enumerate(largest_component.faces):
            for j in range(3):
                combined_mesh.vectors[current_face_index + i][j] = largest_component.vertices[f[j], :]
        current_face_index += largest_component.faces.shape[0]

    # 添加元数据
    combined_mesh.metadata = {'volume': volume_info}

    # 保存所有连通域到一个新的STL文件
    combined_mesh.save(output_file)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Process some integers.')
    parser.add_argument('input_file', type=str, help='The input STL file')
    parser.add_argument('output_file', type=str, help='The output STL file')
    parser.add_argument('num', type=int, help='The number of largest components to keep')
    parser.add_argument('volume_info', type=str, help='Volume information to be added to the STL metadata')
    args = parser.parse_args()
    
    main(args.input_file, args.output_file, args.num, args.volume_info)

标签:STL,component,args,写入,python,mesh,file,largest
From: https://www.cnblogs.com/cupwym/p/18228331

相关文章

  • python对接zabbix API
    frompyzabbix.apiimportZabbixAPIwithZabbixAPI(url='http://192.168.1.10:8080',user='admin',password='admin')aszapi:hosts=zapi.host.get(#获取所有字段output=["hostid","host"],......
  • python对excel文件中指定表格的指定列数据进行去重复操作。
    importpandasaspd#读取Excel文件df_all=pd.read_excel('域名管理系统.xlsx',sheet_name=None,engine='openpyxl')#确保'01流水'表存在if'01流水'indf_all:#提取第1列第2行至第1000行的数据并去重df_two=df_all['01流水']un......
  • python函数
    一、什么是函数定义:函数是组织好,可重复使用,用来实现单一,或关联功能的代码段二、pycharm中的表结构项目,包(init)或目录,py文件,py文件包含多个函数或类等三、函数的有哪些优点?1、降低代码冗余2、增加代码的复用性,提高开发效率3、提高程序的拓展性4、封装:就是把代码片段放在函......
  • 【Python】成功解决TypeError: string indices must be integers
    【Python】成功解决TypeError:stringindicesmustbeintegers 下滑即可查看博客内容......
  • 【Python】成功解决TypeError: ‘method’ object is not subscriptable
    【Python】成功解决TypeError:‘method’objectisnotsubscriptable 下滑即可查看博客内容......
  • python 提取手机号
    importre#导入正则表达式处理模块defget_phone(text):'''使用正则表达式提取文本中的手机号:paramtext:原始文本:return:手机号数组'''phones=re.findall(r'(13\d{9}|14[5|7]\d{8}|15\d{9}|166{\d{8}|17[3|6|7]{\d{8}|18\d{9}......
  • python 执行js 代码 一些库
    在Python中执行JavaScript代码,有几种常见的方法和库可以使用:PyExecJS:这是一个直接在Python中执行JavaScript代码的库。它支持多个JavaScript运行时(如Node.js、PhantomJS、JScript等)。importexecjsjs_code="""functionhello(){return'Hello,World......
  • 【Python】生成html文档-使用dominate
    原文地址:https://www.cnblogs.com/kaerxifa/p/13035376.htmldominate简介dominate是一个使用优雅的DOMAPI创建和操作HTML文档的Python库。使用它能非常简洁地编写纯Python的HTML页面,这消除了学习另一种模板语言的需要,利用Python更强大的特性。 首先安装依赖:pipinstall......
  • [ Python ] 常用运算符对应的魔法方法
    https://www.cnblogs.com/yeungchie/Python中的运算符丰富多样,它们可以分为多个类别,包括算术运算符、比较运算符、逻辑运算符、位运算符、身份运算符、成员运算符等。每个运算符都有其对应的魔法方法(也称为特殊方法或dunder方法,即双下划线方法),这些方法在特定情况下会被Python调用......
  • 利用PlugLink平台实现Python自动化办公
    利用PlugLink平台实现Python自动化办公自动化技术已经成为提升效率和减少人力成本的关键。特别是利用AI和Python语言的强大功能,企业可以实现高度定制化的自动化工作流程。PlugLink作为一个开源的办公自动化平台,正是为了满足这一需求而生。本文将通过一个具体的Python案例,介......