OpenCasCade学习笔记(三):加载显示STEP格式图片,并实现平移、缩放和旋转操作
生活随笔
收集整理的這篇文章主要介紹了
OpenCasCade学习笔记(三):加载显示STEP格式图片,并实现平移、缩放和旋转操作
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
OpenCasCade學習筆記(三):加載顯示STEP格式圖片,并實現(xiàn)平移、縮放和旋轉(zhuǎn)操作
C3DWidget.h
#pragma once#include <QtWidgets/QApplication> #include <QtWidgets/QWidget> #include "qevent.h" #include "stable.h"#define BUTTON_OFF 0 #define BUTTON_ON 1class C3DWidget : public QWidget {Q_OBJECTpublic:C3DWidget(QWidget *parent = Q_NULLPTR);protected://!三維場景轉(zhuǎn)換模式enum CurrentAction3d{CurAction3d_Nothing,CurAction3d_DynamicPanning, //平移CurAction3d_DynamicZooming, //縮放CurAction3d_DynamicRotation //旋轉(zhuǎn)};//!覆寫繪圖事件void paintEvent(QPaintEvent *);//!覆寫窗口尺寸變化事件void resizeEvent(QResizeEvent *);//!返回窗口的繪制引擎QPaintEngine *paintEngine() const;//!覆寫鍵盤按鍵按下事件//void keyPressEvent(QKeyEvent *event);//!覆寫鍵盤按鍵釋放事件//void keyReleaseEvent(QKeyEvent *event);//!覆寫鼠標按鍵按下事件void mousePressEvent(QMouseEvent *event);//!覆寫鼠標按鍵釋放事件void mouseReleaseEvent(QMouseEvent *event);//!覆寫鼠標移動事件void mouseMoveEvent(QMouseEvent *event);//!覆寫鼠標滾輪事件void wheelEvent(QWheelEvent *event);//加載step模型void loadSTEP();//顯示STEP模型void displaySTEP();//把屏幕坐標轉(zhuǎn)換為窗口坐標gp_Pnt ConvertClickToPoint(Standard_Real theX, Standard_Real theY, Handle(V3d_View) theView);void DrawLineByMouse(gp_Pnt thePntStart, gp_Pnt thePntEnd);private://!交互式上下文能夠管理一個或多個查看器(viewer)中的圖形行為和交互式對象的選擇Handle(AIS_InteractiveContext) m_context;//!定義查看器(viewer)類型對象上的服務(wù)Handle(V3d_Viewer) m_viewer;//!創(chuàng)建一個視圖Handle(V3d_View) m_view;//!創(chuàng)建3d接口定義圖形驅(qū)動程序Handle(Graphic3d_GraphicDriver) m_graphic_driver;//存儲STEP模型形狀TopoDS_Shape m_Shape;Handle_AIS_Shape myAISShape;//線的模型Handle_AIS_Shape myLine;Standard_Integer m_x_max; //!記錄鼠標平移坐標XStandard_Integer m_y_max; //!記錄鼠標平移坐標YCurrentAction3d m_current_mode; //!三維場景轉(zhuǎn)換模式(平移\縮放\旋轉(zhuǎn))bool leftMouseBtn = BUTTON_OFF; //記錄鼠標左鍵狀態(tài)gp_Pnt m_line_start_point; //記錄鼠標畫線開始點//gp_Pnt m_line_end_point; //記錄鼠標畫線結(jié)束點 // bool draw_line = false; };C3DWidget.cpp
#include "C3DWidget.h" #include "ProjLib.hxx" #include "ELSLib.hxx" #include "GC_MakeSegment.hxx" #include "BRepBuilderAPI_MakeEdge.hxx" #include <gl\GLU.h> #include <GeomAPI_IntCS.hxx> #include "qdebug.h"C3DWidget::C3DWidget(QWidget *parent): QWidget(parent) {//若交互式上下文為空,則創(chuàng)建對象if (m_context.IsNull()){//此對象提供與X server的連接,在Windows和Mac OS中不起作用Handle(Aspect_DisplayConnection) m_display_donnection = new Aspect_DisplayConnection();//創(chuàng)建OpenGl圖形驅(qū)動if (m_graphic_driver.IsNull()){m_graphic_driver = new OpenGl_GraphicDriver(m_display_donnection);}//獲取QWidget的窗口系統(tǒng)標識符WId window_handle = (WId)winId();//創(chuàng)建Windows NT 窗口Handle(WNT_Window) wind = new WNT_Window((Aspect_Handle)window_handle);//創(chuàng)建3D查看器m_viewer = new V3d_Viewer(m_graphic_driver, Standard_ExtString("viewer3d"));//創(chuàng)建視圖m_view = m_viewer->CreateView();m_view->SetWindow(wind);//打開窗口if (!wind->IsMapped()){wind->Map();}m_context = new AIS_InteractiveContext(m_viewer); //創(chuàng)建交互式上下文//配置查看器的光照m_viewer->SetDefaultLights();m_viewer->SetLightOn();//設(shè)置視圖的背景顏色為灰色m_view->SetBackgroundColor(Quantity_NOC_GRAY60);m_view->MustBeResized();//顯示直角坐標系,可以配置在窗口顯示位置、文字顏色、大小、樣式m_view->TriedronDisplay(Aspect_TOTP_LEFT_LOWER, Quantity_NOC_GOLD, 0.08, V3d_ZBUFFER);//設(shè)置顯示模式m_context->SetDisplayMode(AIS_Shaded, Standard_True);loadSTEP();displaySTEP();}//配置QWidgetsetAttribute(Qt::WA_PaintOnScreen);setAttribute(Qt::WA_NoSystemBackground);setBackgroundRole(QPalette::NoRole); //無背景setFocusPolicy(Qt::StrongFocus);setAttribute(Qt::WA_PaintOnScreen);setAttribute(Qt::WA_NoSystemBackground);setMouseTracking(true); //開啟鼠標位置追蹤 }void C3DWidget::paintEvent(QPaintEvent *) {//if(draw_line == false)m_view->Redraw(); }void C3DWidget::resizeEvent(QResizeEvent *) {if (!m_view.IsNull()){m_view->MustBeResized();} }QPaintEngine *C3DWidget::paintEngine() const {return 0; }void C3DWidget::loadSTEP() {/*將STEP文件轉(zhuǎn)換為OCCT形狀:加載文件,檢查文件一致性,設(shè)置翻譯參數(shù),執(zhí)行翻譯,獲取結(jié)果。*///load the fileSTEPControl_Reader reader;IFSelect_ReturnStatus stat = reader.ReadFile("sample.STEP");switch (stat){case IFSelect_RetVoid:std::cout << "normal execution which created nothing, or no data to process!" << std::endl;break;case IFSelect_RetDone:break;case IFSelect_RetError:std::cout << "error in command or input data, no execution!" << std::endl;break;case IFSelect_RetFail:std::cout << "execution was run and has failed!" << std::endl;break;case IFSelect_RetStop:std::cout << "indicates end or stop (such as Raise)!" << std::endl;break;default:break;}if (stat != IFSelect_RetDone)return;//Selecting STEP entities for translation : The whole file//加載文件Standard_Integer NbRoots = reader.NbRootsForTransfer();Standard_Integer num = reader.TransferRoots();//Mapping STEP entities to Open CASCADE Technology shapesm_Shape = reader.OneShape(); }void C3DWidget::displaySTEP() {//display STEP myAISShape = new AIS_Shape(m_Shape);myAISShape->SetDisplayMode(AIS_Shaded);m_context->Display(myAISShape, true); }void C3DWidget::mousePressEvent(QMouseEvent *event) {if (((event->buttons() & Qt::MidButton) && (QApplication::keyboardModifiers() == Qt::CTRL)))//平移{m_current_mode = CurAction3d_DynamicPanning;m_x_max = event->pos().x(); //記錄起始X位置m_y_max = event->pos().y(); //記錄起始Y位置}else if (event->buttons() & Qt::MidButton) //旋轉(zhuǎn){m_current_mode = CurAction3d_DynamicRotation;m_view->StartRotation(event->pos().x(), event->pos().y());}if (event->buttons() & Qt::LeftButton){leftMouseBtn = BUTTON_ON;//m_line_start_point = ConvertClickToPoint(event->pos().x(), event->pos().x(), m_view);} }void C3DWidget::mouseReleaseEvent(QMouseEvent *event) {GLdouble res[3];GLdouble mm[16], pm[16]; GLint vp[4];GLfloat pix;m_current_mode = CurAction3d_Nothing;if (leftMouseBtn == BUTTON_ON){leftMouseBtn = BUTTON_OFF;//GLdouble x = event->pos().x();//GLdouble y = event->pos().y();//GLint viewport[4];//GLdouble modleview[16];//GLdouble projection[16];獲取必要的矩陣//glPushMatrix();//glLoadIdentity();//glLoadMatrixd(m_view->Camera()->OrientationMatrix());//glGetIntegerv(GL_VIEWPORT, viewport);//獲取視口//glGetDoublev(GL_MODELVIEW_MATRIX, modleview);//獲取模型視圖矩陣//glGetDoublev(GL_PROJECTION_MATRIX, projection);//獲取投影矩陣//glPopMatrix();將屏幕坐標轉(zhuǎn)換為窗口坐標//GLdouble winX = x;//GLdouble winY = viewport[3] - y;//GLdouble winZ1 = 0;//GLdouble winZ2 = 10;//double x1, y1, z1, x2, y2, z2;坐標變換//gluUnProject(winX, winY, winZ1, modleview, projection, viewport, &x1, &y1, &z1);//gluUnProject(winX, winY, winZ2, modleview, projection, viewport, &x2, &y2, &z2);//生成直線//Handle(Geom_Line) aLine = new Geom_Line(gp_Pnt(x1, y1, z1), gp_Dir(x2 - x1, y2 - y1, z2 - z1));//GeomAPI_IntCS intCS(aLine, aSurface);//gp_Pnt aPnt = intCS.Point(1);//qDebug() << aPnt.X() << aPnt.Y() << aPnt.Z();//====================方案二=====================獲取模型視圖矩陣//glGetDoublev(GL_MODELVIEW_MATRIX, mm);獲取投影矩陣//glGetDoublev(GL_PROJECTION_MATRIX, pm);獲取視口//glGetIntegerv(GL_VIEWPORT, vp);讀取緩沖區(qū)的深度分量//glReadPixels(event->pos().x(), event->pos().y(), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &pix);//GLfloat depthrange[2] = { 0,0 };//glGetFloatv(GL_DEPTH_RANGE, depthrange);//if (pix == depthrange[1])// qDebug() << "false";反投影//gluUnProject(event->pos().x(), event->pos().y(), pix, mm, pm, vp, &res[0], &res[1], &res[2]);qDebug() << res[0] << res[1] << res[2];} }void C3DWidget::mouseMoveEvent(QMouseEvent *event) {switch (m_current_mode){case CurAction3d_DynamicPanning://執(zhí)行平移m_view->Pan(event->pos().x() - m_x_max, m_y_max - event->pos().y());m_x_max = event->pos().x();m_y_max = event->pos().y();break;case CurAction3d_DynamicRotation://執(zhí)行旋轉(zhuǎn)m_view->Rotation(event->pos().x(), event->pos().y());break;}//if (leftMouseBtn == BUTTON_ON)//{// gp_Pnt m_line_end_point = ConvertClickToPoint(event->pos().x(), event->pos().x(), m_view);// DrawLineByMouse(m_line_start_point, m_line_end_point);//} }void C3DWidget::wheelEvent(QWheelEvent *event) {m_view->StartZoomAtPoint(event->pos().x(), event->pos().y());m_view->ZoomAtPoint(0, 0, event->angleDelta().y(), 0); //執(zhí)行縮放 }gp_Pnt C3DWidget::ConvertClickToPoint(Standard_Real theX, Standard_Real theY, Handle(V3d_View) theView) {Standard_Real XEye, YEye, ZEye, XAt, YAt, ZAt;theView->Eye(XEye, YEye, ZEye);theView->At(XAt, YAt, ZAt);gp_Pnt EyePoint(XEye, YEye, ZEye);gp_Pnt AtPoint(XAt, YAt, ZAt);gp_Vec EyeVector(EyePoint, AtPoint);gp_Dir EyeDir(EyeVector);gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);Standard_Real X, Y, Z;theView->Convert(int(theX), int(theY), X, Y, Z);gp_Pnt ConvertedPoint(X, Y, Z);gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(),ConvertedPointOnPlane.Y(),PlaneOfTheView);return ResultPoint; }void C3DWidget::DrawLineByMouse(gp_Pnt thePntStart, gp_Pnt thePntEnd) {//檢查傳入?yún)?shù)if (thePntStart.IsEqual(thePntEnd, 1e-3))return;//構(gòu)建拓撲線段Handle(Geom_TrimmedCurve) aSegment =GC_MakeSegment(thePntStart, thePntEnd);TopoDS_Edge aEdge = BRepBuilderAPI_MakeEdge(aSegment);//將構(gòu)建的拓撲線段設(shè)置至AIS_Shape形狀中myAISShape->SetShape(aEdge);//移除前面繪畫的舊線段, 繪制新線段。m_context->Remove(myAISShape, true);m_context->Display(myAISShape, false);//更新Viewm_context->UpdateCurrentViewer(); }stable.h
#pragma once#include <AIS_InteractiveContext.hxx> #include <AIS_InteractiveObject.hxx> #include <AIS_ListOfInteractive.hxx> #include <AIS_ListIteratorOfListOfInteractive.hxx> #include <AIS_Shape.hxx> #include <AIS_Trihedron.hxx>#include <Aspect_Background.hxx> #include <Aspect_TypeOfline.hxx> #include <Aspect_WidthOfline.hxx> #include <Aspect_Window.hxx> #include <Bnd_Box2d.hxx> #include <BndLib_Add2dCurve.hxx> #include <BRep_Builder.hxx> #include <BRep_Tool.hxx>#include <BRepBuilderAPI.hxx> #include <BRepAlgo.hxx> #include <BRepTools.hxx> #include <BRepPrimAPI_MakeBox.hxx> #include <BRepPrimAPI_MakeCylinder.hxx> #include <Standard_DefineHandle.hxx> #include <DsgPrs_LengthPresentation.hxx> #include <GCE2d_MakeSegment.hxx> #include <GCPnts_TangentialDeflection.hxx> #include <Geom_CartesianPoint.hxx> #include <Geom_Axis2Placement.hxx> #include <Geom_CartesianPoint.hxx> #include <Geom_Line.hxx> #include <Geom_Surface.hxx> #include <Geom2d_BezierCurve.hxx> #include <Geom2d_BSplineCurve.hxx> #include <Geom2d_Curve.hxx> #include <Geom2d_TrimmedCurve.hxx> #include <Geom2dAdaptor_Curve.hxx> #include <GeomAbs_CurveType.hxx> #include <GeomAdaptor_Curve.hxx> #include <GeomTools_Curve2dSet.hxx> #include <gp_Ax2d.hxx> #include <gp_Circ2d.hxx> #include <gp_Dir2d.hxx> #include <gp_Lin2d.hxx> #include <gp_Pnt2d.hxx> #include <gp_Vec.hxx> #include <gp_Vec2d.hxx> #include <MMgt_TShared.hxx> #include <OSD_Environment.hxx> #include <Precision.hxx> #include <Prs3d_Drawer.hxx> #include <Prs3d_IsoAspect.hxx> #include <Prs3d_LineAspect.hxx> #include <Prs3d_Text.hxx> #include <Quantity_Factor.hxx> #include <Quantity_Length.hxx> #include <Quantity_NameOfColor.hxx> #include <Quantity_PhysicalQuantity.hxx> #include <Quantity_PlaneAngle.hxx> #include <Quantity_TypeOfColor.hxx> #include <SelectMgr_EntityOwner.hxx> #include <SelectMgr_SelectableObject.hxx> #include <SelectMgr_Selection.hxx> #include <SelectMgr_SelectionManager.hxx> #include <Standard_Boolean.hxx> #include <Standard_CString.hxx> #include <Standard_ErrorHandler.hxx> #include <Standard_Integer.hxx> #include <Standard_IStream.hxx> #include <Standard_Macro.hxx> #include <Standard_NotImplemented.hxx> #include <Standard_OStream.hxx> #include <Standard_Real.hxx> #include <StdPrs_Curve.hxx> #include <StdPrs_Point.hxx> #include <StdPrs_PoleCurve.hxx> #include <TCollection_AsciiString.hxx> #include <TColgp_Array1OfPnt2d.hxx> #include <TColgp_HArray1OfPnt2d.hxx> #include <TCollection_AsciiString.hxx> #include <TColStd_HSequenceOfTransient.hxx> #include <TColStd_MapIteratorOfMapOfTransient.hxx> #include <TColStd_MapOfTransient.hxx> #include <TopExp_Explorer.hxx> #include <TopoDS.hxx> #include <TopoDS_Compound.hxx> #include <TopoDS_ListIteratorOfListOfShape.hxx> #include <TopoDS_Shape.hxx> #include <TopoDS_Solid.hxx> #include <TopoDS_Vertex.hxx> #include <TopExp.hxx> #include <TopTools_ListIteratorOfListOfShape.hxx> #include <TopTools_HSequenceOfShape.hxx> #include <TopTools_DataMapOfShapeInteger.hxx> #include <UnitsAPI.hxx> #include <V3d_View.hxx> #include <V3d_Viewer.hxx> #include <WNT_Window.hxx> #include <OpenGl_GraphicDriver.hxx> // specific STEP #include <STEPControl_Controller.hxx> #include <STEPControl_Reader.hxx> #include <STEPControl_Writer.hxx>main.cpp
#include "C3DWidget.h"int main(int argc, char *argv[]) {QApplication a(argc, argv);C3DWidget w;w.show();return a.exec(); }總結(jié)
以上是生活随笔為你收集整理的OpenCasCade学习笔记(三):加载显示STEP格式图片,并实现平移、缩放和旋转操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js室内地图开发_概述-室内地图 JS
- 下一篇: 数据分箱技术Binning