Pyqt+Mimics优化计算机辅助设计工作流
生活随笔
收集整理的這篇文章主要介紹了
Pyqt+Mimics优化计算机辅助设计工作流
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Pyqt+Mimics優化計算機輔助設計工作流
- Pyqt魔改Mimics
- 環境配置
- UI設計
- 代碼編寫
- 文件頭,需要引用的一些庫、定義
- 坐標間距離計算、方向、位移、延申函數
- QT UI 轉化后的代碼
- UI修飾
- 事件響應函數
- 主程序入口
- 優化工作流
Pyqt魔改Mimics
你好! 你是否想過,使用腳本來減少Mimics Innovation Suit 的很多繁瑣操作?但是在優化工作流的過程中,頻繁修改代碼,有什么辦法避免呢?
環境配置
安裝mimics20.0后,
UI設計
使用qtcreator編輯設計UI。
如還沒有qtcreator,在https://www.qt.io/下載安裝qtcreator。
代碼編寫
在mimics的Scripting Guide查閱mimics API,將制作導板需要重復操作的部分轉化為代碼,有些使用for循環順序操作,有些將之順序列出,逐條執行。
文件頭,需要引用的一些庫、定義
# Import the numpy library that is useful for some operations import numpy as np import math import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QApplication, QDialog from PyQt5.QtCore import *# Define a shortcut md = mimics.data ma = mimics.analyze mi = mimics.indicate_coordinate guide_board_landmarks = ["AStart","AEnd","BStart","BEnd"]坐標間距離計算、方向、位移、延申函數
def pp_length(a,b):x=a[0]-b[0]y=a[1]-b[1]z=a[2]-b[2]return math.sqrt(x*x+y*y+z*z)def pp_direction(a,b):l=pp_length(a,b)if l==0:return (0,0,0)x=(b[0]-a[0])/ly=(b[1]-a[1])/lz=(b[2]-a[2])/lreturn (x,y,z)def pp_add(a,b):x=a[0]+b[0]y=a[1]+b[1]z=a[2]+b[2]return (x,y,z)def pp_extend(a,b,l):d=pp_direction(a,b)return (b[0]+d[0]*l,b[1]+d[1]*l,b[2]+d[2]*l)QT UI 轉化后的代碼
from PyQt5 import QtCore, QtGui, QtWidgetsclass Ui_Dialog(object):def setupUi(self, dialog):dialog.setObjectName("Dialog")dialog.resize(400, 300)self.verticalLayoutWidget = QtWidgets.QWidget(dialog)self.verticalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 401, 301))self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)self.verticalLayout.setContentsMargins(0, 0, 0, 0)self.verticalLayout.setObjectName("verticalLayout")self.horizontalLayout_8 = QtWidgets.QHBoxLayout()self.horizontalLayout_8.setObjectName("horizontalLayout_8")self.label_2 = QtWidgets.QLabel(self.verticalLayoutWidget)self.label_2.setAlignment(QtCore.Qt.AlignCenter)self.label_2.setObjectName("label_2")self.horizontalLayout_8.addWidget(self.label_2)self.lineEdit = QtWidgets.QLineEdit(self.verticalLayoutWidget) size_policy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)size_policy.setHorizontalStretch(0)size_policy.setVerticalStretch(0)size_policy.setHeightForWidth(self.lineEdit.sizePolicy().hasHeightForWidth())self.lineEdit.setSizePolicy(size_policy)self.lineEdit.setMaximumSize(QtCore.QSize(16777215, 16777215))self.lineEdit.setFrame(True)self.lineEdit.setObjectName("lineEdit")self.horizontalLayout_8.addWidget(self.lineEdit)self.verticalLayout.addLayout(self.horizontalLayout_8)self.horizontalLayout_7 = QtWidgets.QHBoxLayout()self.horizontalLayout_7.setObjectName("horizontalLayout_7")self.label_1 = QtWidgets.QLabel(self.verticalLayoutWidget)self.label_1.setAlignment(QtCore.Qt.AlignCenter)self.label_1.setObjectName("label_1")self.horizontalLayout_7.addWidget(self.label_1)self.lineEdit_1 = QtWidgets.QLineEdit(self.verticalLayoutWidget)self.lineEdit_1.setSizePolicy(size_policy)self.lineEdit_1.setMaximumSize(QtCore.QSize(16777215, 16777215))self.lineEdit_1.setFrame(True)self.lineEdit_1.setObjectName("lineEdit")self.horizontalLayout_7.addWidget(self.lineEdit_1)self.verticalLayout.addLayout(self.horizontalLayout_7)self.horizontalLayout_6 = QtWidgets.QHBoxLayout()self.horizontalLayout_6.setObjectName("horizontalLayout_6")self.label = QtWidgets.QLabel(self.verticalLayoutWidget)self.label.setAlignment(QtCore.Qt.AlignCenter)self.label.setObjectName("label")self.horizontalLayout_6.addWidget(self.label)self.lineEdit_2 = QtWidgets.QLineEdit(self.verticalLayoutWidget)self.lineEdit_2.setSizePolicy(size_policy)self.lineEdit_2.setMaximumSize(QtCore.QSize(16777215, 16777215))self.lineEdit_2.setFrame(True)self.lineEdit_2.setObjectName("lineEdit")self.horizontalLayout_6.addWidget(self.lineEdit_2) self.verticalLayout.addLayout(self.horizontalLayout_6) self.horizontalLayout_5 = QtWidgets.QHBoxLayout()self.horizontalLayout_5.setObjectName("horizontalLayout_5")self.label_4 = QtWidgets.QLabel(self.verticalLayoutWidget)self.label_4.setAlignment(QtCore.Qt.AlignCenter)self.label_4.setObjectName("label_4")self.horizontalLayout_5.addWidget(self.label_4)self.lineEdit_3 = QtWidgets.QLineEdit(self.verticalLayoutWidget)self.lineEdit_3.setSizePolicy(size_policy)self.lineEdit_3.setMaximumSize(QtCore.QSize(16777215, 16777215))self.lineEdit_3.setFrame(True)self.lineEdit_3.setObjectName("lineEdit")self.horizontalLayout_5.addWidget(self.lineEdit_3) self.verticalLayout.addLayout(self.horizontalLayout_5) self.horizontalLayout_4 = QtWidgets.QHBoxLayout()self.horizontalLayout_4.setObjectName("horizontalLayout_4")self.label_3 = QtWidgets.QLabel(self.verticalLayoutWidget)self.label_3.setAlignment(QtCore.Qt.AlignCenter)self.label_3.setObjectName("label_3")self.horizontalLayout_4.addWidget(self.label_3)self.BtnCreatePoints = QtWidgets.QPushButton(self.verticalLayoutWidget)self.BtnCreatePoints.setObjectName("ButtonCreatePoints")self.horizontalLayout_4.addWidget(self.BtnCreatePoints)self.BtnDelPoints = QtWidgets.QPushButton(self.verticalLayoutWidget)self.BtnDelPoints.setObjectName("ButtonDelPoints")self.horizontalLayout_4.addWidget(self.BtnDelPoints)self.BtnHidePoints = QtWidgets.QPushButton(self.verticalLayoutWidget)self.BtnHidePoints.setObjectName("ButtonHidePoints")self.horizontalLayout_4.addWidget(self.BtnHidePoints)self.BtnShowPoints = QtWidgets.QPushButton(self.verticalLayoutWidget)self.BtnShowPoints.setObjectName("ButtonShowPoints")self.horizontalLayout_4.addWidget(self.BtnShowPoints)self.verticalLayout.addLayout(self.horizontalLayout_4)self.horizontalLayout_3 = QtWidgets.QHBoxLayout()self.horizontalLayout_3.setObjectName("horizontalLayout_3")self.label_5 = QtWidgets.QLabel(self.verticalLayoutWidget)self.label_5.setAlignment(QtCore.Qt.AlignCenter)self.label_5.setObjectName("label_5")self.horizontalLayout_3.addWidget(self.label_5)self.GenerateSphereCylinder = QtWidgets.QPushButton(self.verticalLayoutWidget)self.GenerateSphereCylinder.setObjectName("GenerateSphereCylinder")self.horizontalLayout_3.addWidget(self.GenerateSphereCylinder)self.DelSphereCylinder = QtWidgets.QPushButton(self.verticalLayoutWidget)self.DelSphereCylinder.setObjectName("DelSphereCylinder")self.horizontalLayout_3.addWidget(self.DelSphereCylinder)self.BooleanUnion = QtWidgets.QPushButton(self.verticalLayoutWidget)self.BooleanUnion.setObjectName("BooleanUnion")self.horizontalLayout_3.addWidget(self.BooleanUnion)self.BooleanMinus = QtWidgets.QPushButton(self.verticalLayoutWidget)self.BooleanMinus.setObjectName("BooleanMinus")self.horizontalLayout_3.addWidget(self.BooleanMinus)self.verticalLayout.addLayout(self.horizontalLayout_3)self.translate_ui(dialog)# QtCore.QMetaObject.connectSlotsByName(dialog)self.BtnCreatePoints.clicked.connect(self.on_BtnCreatePoints_clicked)self.BtnDelPoints.clicked.connect(self.on_BtnDelPoints_clicked)self.BtnHidePoints.clicked.connect(self.on_BtnHidePoints_clicked)self.BtnShowPoints.clicked.connect(self.on_BtnShowPoints_clicked)self.GenerateSphereCylinder.clicked.connect(self.on_GenerateSphereCylinder_clicked)self.DelSphereCylinder.clicked.connect(self.on_DelSphereCylinder_clicked)self.BooleanUnion.clicked.connect(self.on_BooleanUnion_clicked)self.BooleanMinus.clicked.connect(self.on_BooleanMinus_clicked)UI修飾
def translate_ui(self, dialog):_translate = QtCore.QCoreApplication.translatedialog.setWindowTitle(_translate("Dialog", "制作導板"))self.label_3.setText(_translate("Dialog", "編輯關鍵點"))self.BtnCreatePoints.setText(_translate("Dialog", "創建點"))self.BtnDelPoints.setText(_translate("Dialog", "刪除點"))self.BtnHidePoints.setText(_translate("Dialog", "隱藏點"))self.BtnShowPoints.setText(_translate("Dialog", "顯示點"))self.lineEdit_2.setText("25")self.lineEdit_1.setText("5")self.lineEdit.setText("10")self.lineEdit_3.setText("10")self.label_1.setText(_translate("Dialog", "導管內直徑"))self.label_2.setText(_translate("Dialog", "導管外直徑"))self.label.setText(_translate("Dialog", "導管長度"))self.label_4.setText(_translate("Dialog", "底球膨脹"))self.label_5.setText(_translate("Dialog", "生成導板"))self.GenerateSphereCylinder.setText(_translate("Dialog", "生成組件"))self.DelSphereCylinder.setText(_translate("Dialog", "刪除組件"))self.BooleanUnion.setText(_translate("Dialog", "布爾運算并集"))self.BooleanMinus.setText(_translate("Dialog", "布爾運算差集"))事件響應函數
def on_BtnCreatePoints_clicked(self):for point in guide_board_landmarks:p = mimics.analyze.indicate_point(title=point,message= "Please indicate a point on the {}".format(point))p.name = pointprint("success BtnCreatePoints")def on_BtnDelPoints_clicked(self):for point in guide_board_landmarks:p=mimics.data.points.find(point)if p:mimics.data.analytical_primitives.delete(p)print("success BtnDelPoints")def on_BtnHidePoints_clicked(self):for point in guide_board_landmarks:p=mimics.data.points.find(point)if p:p.visible=Falseprint("success BtnHidePoints")def on_BtnShowPoints_clicked(self):for point in guide_board_landmarks:p=mimics.data.points.find(point)if p:p.visible=Trueprint("success BtnShowPoints")def on_GenerateSphereCylinder_clicked(self):l_extend=float(self.lineEdit_2.text())d_in=float(self.lineEdit_1.text())d_out=float(self.lineEdit.text())d_s=d_out+float(self.lineEdit_3.text())pp_extA=mimics.analyze.create_point(pp_extend(mimics.data.points.find("AStart"),mimics.data.points.find("AEnd"),l_extend))pp_extA.name="PP_EXTA"pp_extB=mimics.analyze.create_point(pp_extend(mimics.data.points.find("BStart"),mimics.data.points.find("BEnd"),l_extend))pp_extB.name="PP_EXTB"C_A_O = mimics.analyze.create_cylinder_points_radius(point1=mimics.data.points.find("AEnd"),point2=mimics.data.points.find("PP_EXTA"),radius=d_out/2)C_A_O.name = "cylinderAO"S_A = mimics.analyze.create_sphere_center_radius(center=mimics.data.points.find("PP_EXTA"),radius=d_out/2)S_A.name = "sphereA"S_A = mimics.analyze.create_sphere_center_radius(center=mimics.data.points.find("AEnd"),radius=d_s/2)S_A.name = "sphereAA"C_B_O =mimics.analyze.create_cylinder_points_radius(point1=mimics.data.points.find("BEnd"),point2=mimics.data.points.find("PP_EXTB"),radius=d_out/2)C_B_O.name = "cylinderBO"S_B = mimics.analyze.create_sphere_center_radius(center=mimics.data.points.find("PP_EXTB"),radius=d_out/2)S_B.name = "sphereB"S_B = mimics.analyze.create_sphere_center_radius(center=mimics.data.points.find("BEnd"),radius=d_s/2)S_B.name = "sphereBB"C_A = mimics.analyze.create_cylinder_points_radius(point1=pp_extend(mimics.data.points.find("AStart"),mimics.data.points.find("PP_EXTA"),30),point2=pp_extend(mimics.data.points.find("AEnd"),mimics.data.points.find("AStart"),30),radius=d_in/2)C_A.name = "cylinderA"C_B = mimics.analyze.create_cylinder_points_radius(point1=pp_extend(mimics.data.points.find("BStart"),mimics.data.points.find("PP_EXTB"),30),point2=pp_extend(mimics.data.points.find("BEnd"),mimics.data.points.find("BStart"),30),radius=d_in/2)C_B.name = "cylinderB"C_AB = mimics.analyze.create_cylinder_points_radius(point1=mimics.data.points.find("PP_EXTA"),point2=mimics.data.points.find("PP_EXTB"),radius=d_out/2)C_AB.name = "cylinderAB"print("success Sphere")def on_DelSphereCylinder_clicked(self):for obj in ["PP_EXTA","PP_EXTB","cylinderAO","cylinderBO","cylinderA","cylinderB","sphereA","sphereB","sphereAA","sphereBB","cylinderAB",]:p=mimics.data.analytical_primitives.find(obj)if p:mimics.data.analytical_primitives.delete(p)print("success DelSphereCylinde")def on_BooleanUnion_clicked(self):obj1=mimics.data.analytical_primitives.find("cylinderAO")obj2=mimics.data.analytical_primitives.find("sphereA")u1=mimics.simulate.boolean_3d_unite(obj1,obj2)obj3=mimics.data.analytical_primitives.find("sphereAA")u2=mimics.simulate.boolean_3d_unite(obj3,u1)obj4=mimics.data.analytical_primitives.find("cylinderAB")u3=mimics.simulate.boolean_3d_unite(obj4,u2)obj5=mimics.data.analytical_primitives.find("cylinderBO")u4=mimics.simulate.boolean_3d_unite(obj5,u3)obj6=mimics.data.analytical_primitives.find("sphereB")u5=mimics.simulate.boolean_3d_unite(obj6,u4)obj7=mimics.data.analytical_primitives.find("sphereBB")u6=mimics.simulate.boolean_3d_unite(obj7,u5)mimics.data.parts.delete(u1)mimics.data.parts.delete(u2)mimics.data.parts.delete(u3)mimics.data.parts.delete(u4)mimics.data.parts.delete(u5)u6.name="GuideBoardUnionResult"print("success Union")def on_BooleanMinus_clicked(self):obj1=mimics.data.parts.find("GuideBoardUnionResult")obj2=mimics.data.analytical_primitives.find("cylinderA")m1=mimics.simulate.boolean_3d_minus(obj1,obj2)obj3=mimics.data.analytical_primitives.find("cylinderB")m2=mimics.simulate.boolean_3d_minus(m1,obj3)obj4=mimics.data.parts.find("Indicated Bone")m3=mimics.simulate.boolean_3d_minus(m2,obj4)mimics.data.parts.delete(m1)mimics.data.parts.delete(m2)m3.name="GuideBoard"print("success Minus")主程序入口
if __name__ == '__main__':app = QApplication(sys.argv)MainDialog = QDialog()myDialog = Ui_Dialog()myDialog.setupUi(MainDialog)MainDialog.show()sys.exit(app.exec_())優化工作流
通過本文提出的方法可以減少設計中的固定操作,將需要多次鼠標點擊才能完成的操作優化為單次點擊即可完成,減少重復勞動,提高效率。優化3D打印導板的設計過程:從一個團隊數天時間,縮減到一個人數小時。
總結
以上是生活随笔為你收集整理的Pyqt+Mimics优化计算机辅助设计工作流的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 明日方舟抽卡模拟器wiki_明日方舟抽卡
- 下一篇: 快递驿站APP开发重点功能介绍