生活随笔
收集整理的這篇文章主要介紹了
PythonOcc实战——step文件导入、格式转换、动画展示
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文主要是實現從occ模塊中導入step文件,并將其中各個零件的模型格式進行轉換,最終實現在occ三維環境中動畫展示各個零件遠動狀態。
第一步,在自制Pyqt5的UI中實現加載連桿機構模型。
import sys
import random
from PyQt5
.QtWidgets
import QMainWindow
, QAction
, qApp
, QApplication
from SelfMadeQTviewer3D
import qtViewer3d
from OCC
.Extend
.DataExchange
import read_step_file
from OCC
.Extend
.TopologyUtils
import TopologyExplorer
from OCC
.Core
.Quantity
import Quantity_Color
, Quantity_TOC_RGB
class Example(QMainWindow
):def __init__(self
):super().__init__
()self
.canva
= qtViewer3d
(self
)self
.solidset
={}self
.initUI
()def initUI(self
):exitAct
= QAction
('&Exit', self
)exitAct
.setShortcut
('Ctrl+Q')exitAct
.setStatusTip
('Exit application')exitAct
.triggered
.connect
(qApp
.quit
)FourBaraction
= QAction
('&FourBar_Animation', self
) FourBaraction
.triggered
.connect
(self
.FourBar_Ani
) self
.statusBar
()menubar
= self
.menuBar
() fileMenu
= menubar
.addMenu
('&File')fileMenu
.addAction
(exitAct
)AnimationMenu
= menubar
.addMenu
('&Animation')AnimationMenu
.addAction
(FourBaraction
)self
.setGeometry
(100, 50, 1600, 1000)self
.setWindowTitle
('PyOcc Packed In Pyqt5')self
.setCentralWidget
(self
.canva
)self
.show
()def STEP_shape(self
):stpshp
= read_step_file
('FourLinkedBar_ABAQUS.stp')return TopologyExplorer
(stpshp
)def FourBar_Ani(self
):self
.canva
._display
.EraseAll
()self
.STEPshp
= self
.STEP_shape
()i
=0for solid
in self
.STEPshp
.solids
():color
= Quantity_Color
(random
.random
(), random
.random
(),random
.random
(),Quantity_TOC_RGB
)self
.canva
._display
.DisplayColoredShape
(solid
, color
)self
.canva
._display
.FitAll
()if __name__
== '__main__':app
= QApplication
(sys
.argv
)ex
= Example
()sys
.exit
(app
.exec_
())
簡單四連桿機構模型step文件導入之后,結果如下圖所示:
實際上,代碼中的變量solid是一種Topo_的CAD模型格式,該CAD模型格式不能直接在qtViewer3d中做動畫展示,必須經過格式轉化,轉化成AIS_格式:
import sys
import time
from math
import pi
from PyQt5
.QtWidgets
import QMainWindow
, QAction
, qApp
, QApplication
from SelfMadeQTviewer3D
import qtViewer3d
from OCC
.Extend
.DataExchange
import read_step_file
from OCC
.Extend
.TopologyUtils
import TopologyExplorer
from OCC
.Core
.AIS
import AIS_Shape
class Example(QMainWindow
):def __init__(self
):super().__init__
()self
.canva
= qtViewer3d
(self
)self
.solidset
={}self
.initUI
()def initUI(self
):exitAct
= QAction
('&Exit', self
)exitAct
.setShortcut
('Ctrl+Q')exitAct
.setStatusTip
('Exit application')exitAct
.triggered
.connect
(qApp
.quit
)FourBaraction
= QAction
('&FourBar_Animation', self
) FourBaraction
.triggered
.connect
(self
.FourBar_Ani
) self
.statusBar
()menubar
= self
.menuBar
()fileMenu
= menubar
.addMenu
('&File')fileMenu
.addAction
(exitAct
)AnimationMenu
= menubar
.addMenu
('&Animation')AnimationMenu
.addAction
(FourBaraction
)self
.setGeometry
(100, 50, 1600, 1000)self
.setWindowTitle
('PyOcc Packed In Pyqt5')self
.setCentralWidget
(self
.canva
)self
.show
()def STEP_shape(self
):stpshp
= read_step_file
('FourLinkedBar_ABAQUS.stp')return TopologyExplorer
(stpshp
)def FourBar_Ani(self
):self
.canva
._display
.EraseAll
()self
.STEPshp
= self
.STEP_shape
()i
=0for solid
in self
.STEPshp
.solids
():i
=i
+1if i
==1:self
.Bar_450
= AIS_Shape
(solid
)if i
==2:self
.Bar_150
= AIS_Shape
(solid
)if i
==3:self
.Bar_500
= AIS_Shape
(solid
)self
.canva
._display
.Context
.Display
(self
.Bar_450
, False)self
.canva
._display
.Context
.Display
(self
.Bar_150
, False)self
.canva
._display
.Context
.Display
(self
.Bar_500
, False)self
.canva
._display
.FitAll
()if __name__
== '__main__':app
= QApplication
(sys
.argv
)ex
= Example
()sys
.exit
(app
.exec_
())
簡單四連桿機構模型step文件導入并經過模型文件格式轉換之后,結果如下圖所示:
四連桿機構的運動形式是一種具有給定運動形式,在動畫展示的過程中各個零件的運動狀態是要滿足一定的函數關系。要研究的連桿機構基本參數為:R1=150,R2=450,R3=500,底部固定點之間距離R0=400。本連桿機構可以解析的得到運動狀態的表達式:
{10cos?γ=9cos?α+8?3cos?θ10sin?γ=9sin?α?3sin?θ\left\{ \begin{array}{c} 10\cos\gamma = 9\cos\alpha+8-3\cos\theta \\ 10\sin\gamma = 9\sin\alpha-3\sin\theta \end{array} \right. {10cosγ=9cosα+8?3cosθ10sinγ=9sinα?3sinθ?
其中的γ\gammaγ和α\alphaα是代求的角度值,θ∈[0,2π)\theta\in[0,2\pi)θ∈[0,2π)。
通過求解這個方程組,得到
{α=arccos?3cos?θ?873?48cos?θ?arccos?9?8cos?θ373?48cos?θγ=arccos?9sin?α?3sin?θ10\left\{ \begin{array}{c} \alpha = \arccos\frac{3\cos\theta-8}{\sqrt{73-48\cos\theta}}-\arccos\frac{9-8\cos\theta}{3\sqrt{73-48\cos\theta}}\\ \gamma = \arccos\frac{9\sin\alpha-3\sin\theta}{10} \end{array} \right. {α=arccos73?48cosθ?3cosθ?8??arccos373?48cosθ?9?8cosθ?γ=arccos109sinα?3sinθ??
import sys
import time
from math
import pi
import numpy
as np
import random
from OCC
.Core
.gp
import gp_Ax1
, gp_Pnt
, gp_Dir
, gp_Trsf
, gp_Vec
from OCC
.Core
.TopLoc
import TopLoc_Location
from PyQt5
.QtWidgets
import QMainWindow
, QAction
, qApp
, QApplication
from OCC
.Core
.BRepPrimAPI
import BRepPrimAPI_MakeBox
from SelfMadeQTviewer3D
import qtViewer3d
from OCC
.Extend
.DataExchange
import read_step_file
from OCC
.Extend
.TopologyUtils
import TopologyExplorer
from OCC
.Core
.Quantity
import Quantity_Color
, Quantity_TOC_RGB
from OCC
.Core
.GProp
import GProp_GProps
from OCC
.Core
.BRepGProp
import brepgprop_VolumeProperties
from OCC
.Core
.TopLoc
import TopLoc_Location
from OCC
.Core
.AIS
import AIS_Shape
class Example(QMainWindow
):def __init__(self
):super().__init__
()self
.canva
= qtViewer3d
(self
)self
.solidset
={}step
= 0.005startP
=0.5*np
.piEndP
=2*np
.pi
+0.5*np
.piself
.theta2Ani
=np
.linspace
(startP
, EndP
, int((EndP
-startP
)/step
), endpoint
=True)self
.alpha2Ani
=self
.AlphaTracef
(self
.theta2Ani
)self
.gamma2Ani
=self
.GammaTracef
(self
.alpha2Ani
,self
.theta2Ani
)self
.XX2Ani
=self
.XTracef
(self
.theta2Ani
,self
.alpha2Ani
)self
.YY2Ani
=self
.YTracef
(self
.theta2Ani
,self
.alpha2Ani
)self
.ax1
= gp_Ax1
(gp_Pnt
(400., 0., 0.), gp_Dir
(0., 0., 1.))self
.ax2
= gp_Ax1
(gp_Pnt
(0., 0., 0.), gp_Dir
(0., 0., 1.))self
.B1_trsf
= gp_Trsf
()self
.B2_trsf
= gp_Trsf
()self
.B3_trsf1
= gp_Trsf
()self
.B3_trsf2
= gp_Trsf
()self
.initUI
()def initUI(self
):exitAct
= QAction
('&Exit', self
)exitAct
.setShortcut
('Ctrl+Q')exitAct
.setStatusTip
('Exit application')exitAct
.triggered
.connect
(qApp
.quit
)FourBaraction
= QAction
('&FourBar_Animation', self
) FourBaraction
.triggered
.connect
(self
.FourBar_Ani
) self
.statusBar
()menubar
= self
.menuBar
()fileMenu
= menubar
.addMenu
('&File')fileMenu
.addAction
(exitAct
)AnimationMenu
= menubar
.addMenu
('&Animation')AnimationMenu
.addAction
(FourBaraction
)self
.setGeometry
(300, 50, 1600, 1000)self
.setWindowTitle
('PyOcc Packed In Pyqt5')self
.setCentralWidget
(self
.canva
)self
.show
()def STEP_shape(self
):stpshp
= read_step_file
('FourLinkedBar_ABAQUS.stp')return TopologyExplorer
(stpshp
)def FourBar_Ani(self
):self
.canva
._display
.EraseAll
()self
.STEPshp
= self
.STEP_shape
()i
=0for solid
in self
.STEPshp
.solids
():i
=i
+1if i
==1:self
.Bar_450
= AIS_Shape
(solid
)if i
==2:self
.Bar_150
= AIS_Shape
(solid
)if i
==3:self
.Bar_500
= AIS_Shape
(solid
)self
.canva
._display
.Context
.Display
(self
.Bar_450
, False)self
.canva
._display
.Context
.Display
(self
.Bar_150
, False)self
.canva
._display
.Context
.Display
(self
.Bar_500
, False)self
.canva
._display
.FitAll
()for j
in range(len(self
.theta2Ani
)):self
.B1_trsf
.SetRotation
(self
.ax1
, self
.alpha2Ani
[j
]-self
.alpha2Ani
[0])loc1
= TopLoc_Location
(self
.B1_trsf
)self
.canva
._display
.Context
.SetLocation
(self
.Bar_450
, loc1
)self
.B2_trsf
.SetRotation
(self
.ax2
, self
.theta2Ani
[j
]-self
.theta2Ani
[0])loc2
= TopLoc_Location
(self
.B2_trsf
)self
.canva
._display
.Context
.SetLocation
(self
.Bar_150
, loc2
)self
.ax3
= gp_Ax1
(gp_Pnt
(self
.XX2Ani
[j
], self
.YY2Ani
[j
], 0), gp_Dir
(0., 0., 1.))self
.B3_trsf1
.SetRotation
(self
.ax3
,self
.gamma2Ani
[j
]-self
.gamma2Ani
[0])self
.B3_trsf2
.SetTranslation
(gp_Vec
(self
.XX2Ani
[j
]-self
.XX2Ani
[0],self
.YY2Ani
[j
]-self
.YY2Ani
[0],0))loc3
= TopLoc_Location
(self
.B3_trsf1
*self
.B3_trsf2
)self
.canva
._display
.Context
.SetLocation
(self
.Bar_500
, loc3
)self
.canva
._display
.Context
.UpdateCurrentViewer
()def AlphaTracef(self
,x
):return np
.where
(x
/(2*np
.pi
)-np
.trunc
(x
/(2*np
.pi
))<=0.5,np
.arccos
((3*np
.cos
(x
)-8)/(np
.sqrt
(73-48*np
.cos
(x
))))-np
.arccos
((9-8*np
.cos
(x
))/(3*np
.sqrt
(73-48*np
.cos
(x
)))),2*np
.pi
-np
.arccos
((3*np
.cos
(x
)-8)/(np
.sqrt
(73-48*np
.cos
(x
))))-np
.arccos
((9-8*np
.cos
(x
))/(3*np
.sqrt
(73-48*np
.cos
(x
)))))def GammaTracef(self
,data_alpha
,x
):return np
.arccos
(0.1*(9*np
.cos
(data_alpha
)+8-3*np
.cos
(x
)))def XTracef(self
,data_theta
,data_alpha
):return 0.5*(150*np
.cos
(data_theta
)+400+450*np
.cos
(data_alpha
))def YTracef(self
,data_theta
,data_alpha
):return 0.5*(150*np
.sin
(data_theta
)+450*np
.sin
(data_alpha
))if __name__
== '__main__':app
= QApplication
(sys
.argv
)ex
= Example
()sys
.exit
(app
.exec_
())
這是一種暫時妥協的方法。由于類似于Solidworks這樣的3維軟件可以做到定義約束和連接關系之后,可以直接鼠標拖動控制連桿機構運動。還是要繼續深入學習occ中的ais連接函數的應用。
總結
以上是生活随笔為你收集整理的PythonOcc实战——step文件导入、格式转换、动画展示的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。