背景

最近开发了一个六自由度机械臂调姿平台的控制软件,集成了API激光跟踪仪和KUKA机器人,实现了根据产品的测量位姿驱动仿真环境中模型并且实现模型间的碰撞检测。
其中KUKA机器人的控制可以参考笔者以前的博客:
https://blog.csdn.net/iamqianrenzhan/article/details/103115580
关于API激光跟踪仪的基本使用方法,将会在后续博客中介绍。本文主要是关于OpenCasCAD的使用,以及在qt中的配置。

OpenCasCAD简介

Open CASCADE(简称OCC)平台是由法国Matra Datavision公司开发的CAD/CAE/CAM软件平台。开源OCC对象库是一个面向对象C++类库,用于快速开发设计领域的专业应用程序。
OCC主要用于开发二维和三维几何建模应用程序,包括通用的或专业的计算机辅助设计CAD系统、制造或分析领域的应用程序、仿真应用程序或图形演示工具。OCC通过有机组织的C++库文件提供了六个模块。可视化模块作为OCC的核心部分,是可视化技术的具体体现。

OpenCasCAD的安装与自行编译

官网提供:可以在OpenCasCAD网站下载最新版
https://www.opencascade.com/content/latest-release
如果官网下载太慢,可以下载笔者上传的版本。
https://download.csdn.net/download/iamqianrenzhan/11982847
这个是安装版本,下载后可以直接使用。因为该安装版本也包含源码,也可以自行编译。自行编译参考笔者以前的博客:
https://blog.csdn.net/iamqianrenzhan/article/details/79558781
或者直接参考官网
https://www.opencascade.com/doc/occt-7.4.0/overview/html/occt_dev_guides__building_msvc.html
如果要用qt进行开发,最好在编译时把qt的版本修改为自己所用的版本。

Qt工程的建立

Qt工程主要参考了github上一个工程:
https://github.com/eryar/occQt

在pro文件中

QT       +=  openglINCLUDEPATH += C:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\incLIBS += -LC:\OpenCASCADE-7.4.0-vc14-64\opencascade-7.4.0\win64\vc14\libDEFINES += WNTLIBS += -lTKernel -lTKMath -lTKV3d -lTKOpenGl \-lTKBRep -lTKIGES -lTKSTL -lTKVRML -lTKSTEP -lTKSTEPAttr -lTKSTEP209 \-lTKSTEPBase -lTKGeomBase -lTKGeomAlgo -lTKG3d -lTKG2d \-lTKXSBase -lTKShHealing -lTKHLR -lTKTopAlgo -lTKMesh -lTKPrim \-lTKCDF -lTKBool -lTKBO -lTKFillet -lTKOffset -lTKLCAF -lTKService

模型导入

新建一个管理模型导入导出的类:

class Standard_EXPORT ImportExport
{public:ImportExport();public :static void ReadIGES(const Handle(AIS_InteractiveContext)& anInteractiveContext,Handle(AIS_Shape)& aShape);static void ReadSTEP(const Handle(AIS_InteractiveContext)& anInteractiveContext,Handle(AIS_Shape)& aShape);
};

目前这个类中只有两种格式模式的导入,后续会增加多种格式的导入和导出。

Stp文件导入

回传aShape参数是为了分别对导入的模型进行颜色设置,删除,移动等操作。

void ImportExport::ReadSTEP(const Handle(AIS_InteractiveContext)& anInteractiveContext,Handle(AIS_Shape)& aShape)
{//Handle(TopTools_HSequenceOfShape) aSequence = ImportExport::ReadSTEP();Handle(TopTools_HSequenceOfShape) aSequence= new TopTools_HSequenceOfShape();QString fileName = QFileDialog::getOpenFileName(nullptr,QObject::tr("read a stp file"),"E:",QObject::tr("STEP Files(*.stp *.step)"));qDebug() << fileName;if(fileName.isNull()){qDebug() << "没有选择文件";std::cout << "没有选择文件" <<std::endl;return;}std::string test = fileName.toStdString();  //必须要把toStdString和data函数分开写。Standard_CString aFileName = test.data();//IFSelect_ReturnStatus ReturnStatus = ReadSTEP(aFileName,aSequence);aSequence->Clear();// create additional log fileSTEPControl_Reader aReader;IFSelect_ReturnStatus status = aReader.ReadFile(aFileName);if(status !=IFSelect_RetDone){switch (status){case IFSelect_RetError:std::cout << "Not a valid Step file" << std::endl;break;case IFSelect_RetFail :std::cout << "Reading has failed" << std::endl;break;case IFSelect_RetVoid :std::cout << "Nothing to transfer" << std::endl;break;default:break;}return;}aReader.WS()->MapReader()->SetTraceLevel(2); // increase default trace levelStandard_Boolean failsonly = Standard_False;aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);// Root transfersStandard_Integer nbr = aReader.NbRootsForTransfer();aReader.PrintCheckTransfer (failsonly, IFSelect_ItemsByEntity);for ( Standard_Integer n = 1; n<=nbr; n++){Standard_Boolean ok = aReader.TransferRoot(n);}// Collecting resulting entitiesStandard_Integer nbs = aReader.NbShapes();if (nbs == 0) return;for (Standard_Integer i=1; i<=nbs; i++)aSequence->Append(aReader.Shape(i));//Handle_AIS_Shape aShape;if (!aSequence.IsNull()){for(int i=1;i<= aSequence->Length();i++){aShape = new AIS_Shape(aSequence->Value(i));anInteractiveContext->SetColor(aShape,Quantity_NOC_YELLOW,Standard_False);anInteractiveContext->SetMaterial(aShape,Graphic3d_NOM_PLASTIC,Standard_False);anInteractiveContext->SetDisplayMode(aShape,1,Standard_False);//anInteractiveContext->SetTransparency(aShape,0.2,Standard_False);anInteractiveContext->Display(aShape, Standard_False);}}
}

Igs文件导入

void ImportExport::ReadIGES(const Handle(AIS_InteractiveContext)& anInteractiveContext,Handle(AIS_Shape)& aShape)
{//Handle(TopTools_HSequenceOfShape) aSequence = ImportExport::ReadIGES();Handle(TopTools_HSequenceOfShape) aSequence = new TopTools_HSequenceOfShape();QString fileName = QFileDialog::getOpenFileName(nullptr,QObject::tr("read a igs file"),"E:",QObject::tr("IGES Files(*.iges *.igs)"));if(fileName.isNull()){qDebug() << "没有选择文件";std::cout << "没有选择文件" << std::endl;return;}qDebug() << fileName;std::string test = fileName.toStdString();  //必须要把toStdString和data函数分开写。Standard_CString aFileName = test.data();//Standard_Integer status = ReadIGES(aFileName,aSequence);IGESControl_Reader Reader;Standard_Integer status = Reader.ReadFile(aFileName);if (status != IFSelect_RetDone){std::cout << "Error : The file is not read" << std::endl;qDebug() << "Error : The file is not read";return;}Reader.TransferRoots();TopoDS_Shape tShape = Reader.OneShape();aSequence->Append(tShape);//Handle_AIS_Shape aShape;if (!aSequence.IsNull()){for(int i=1;i<= aSequence->Length();i++){aShape = new AIS_Shape(aSequence->Value(i));anInteractiveContext->SetColor(aShape,Quantity_NOC_YELLOW,Standard_False);anInteractiveContext->SetMaterial(aShape,Graphic3d_NOM_PLASTIC,Standard_False);anInteractiveContext->SetDisplayMode(aShape,1,Standard_False);//anInteractiveContext->SetTransparency(aShape,0.2,Standard_False);anInteractiveContext->Display(aShape, Standard_False);}}
}

位姿驱动

这里提供两种方法,一种是提供12个参数的其次RT矩阵,一个是提供6个参数的ABCXYZ参数。

void OccView::SetModelLocation(Handle(AIS_Shape)& aShape,gp_Trsf trsf)
{Handle_AIS_InteractiveObject Current(aShape);if(!Current.IsNull()){myContext->SetLocation(Current,trsf);myContext->Update(Current,Standard_True);  //等价于这句话 myContext->UpdateCurrentViewer();//窗口更新}
}//设置当前对象的位置
void OccView::OccView::SetModelLocation_Matrix(Handle(AIS_Shape)& aShape, double* matrixTemp)
{gp_Trsf trsf;trsf.SetValues(matrixTemp[0],matrixTemp[1],matrixTemp[2], matrixTemp[3],matrixTemp[4],matrixTemp[5],matrixTemp[6], matrixTemp[7],matrixTemp[8],matrixTemp[9],matrixTemp[10],matrixTemp[11]);SetModelLocation(aShape,trsf);
}//通过YPR角度设置当前对象的位置
void OccView::SetModelLocation_Euler(Handle(AIS_Shape)& aShape, double* pTemp)
{pTemp[0] = pTemp[0]/180*M_PI;pTemp[1] = pTemp[1]/180*M_PI;pTemp[2] = pTemp[2]/180*M_PI;//设置欧拉角gp_Trsf aTrsf_Rotation;gp_Quaternion aQ;aQ.SetEulerAngles(gp_YawPitchRoll,pTemp[0],pTemp[1],pTemp[2]);aTrsf_Rotation.SetRotation(aQ);//设置平移向量gp_Trsf aTrsf_Translation;gp_Vec theVectorOfTrans(pTemp[3],pTemp[4],pTemp[5]);aTrsf_Translation.SetTranslation(theVectorOfTrans);gp_Trsf trsf = aTrsf_Translation * aTrsf_Rotation;SetModelLocation(aShape,trsf);
}

碰撞检测

这里碰撞检测使用bool交操作,对两个模型进行bool交操作,根据交集结果进行是否碰撞判断。

bool OccView::IsCommon()
{//从AIS类型获得TOPO类型if(m_AISTargetBody.IsNull() || m_AISMoveBody.IsNull()){qDebug() << "no model imported";return false;}m_TppoTargetBody = m_AISTargetBody->Shape();m_TopoMoveBody = m_AISMoveBody->Shape();//没有实时性要求,采用bool求交的方法Standard_Boolean bRunParallel;Standard_Real aFuzzyValue;BRepAlgoAPI_Common aBuilder;// perpare the argumentsTopTools_ListOfShape aLS;TopTools_ListOfShape aLT;aLS.Append(m_TppoTargetBody_cal);  //xx   m_TppoTargetBodyaLT.Append(m_TopoMoveBody_cal);  //yy   m_TopoMoveBodybRunParallel=Standard_True;aFuzzyValue=2.1e-5;// set the argumentsaBuilder.SetArguments(aLS);
aBuilder.SetTools(aLT);aBuilder.SetRunParallel(bRunParallel);aBuilder.SetFuzzyValue(aFuzzyValue);// run the algorithmaBuilder.Build();if (aBuilder.HasErrors()) {std::ofstream fout;fout.open("errormessage.txt",std::ios_base::app);aBuilder.DumpErrors(fout);fout.flush();fout.close();qDebug() << "error";return false;}// result of the operation aRconst TopoDS_Shape& aR=aBuilder.Shape();Bnd_Box B;BRepBndLib::Add(aR, B);double Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;if(!B.IsVoid()){B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);}int nb = aR.NbChildren();if(!B.IsVoid())   //交集部分的最小包围box存在,则说明碰撞{std::cout << "common" << std::endl;return true;}else{std::cout << "no common" << std::endl;return false;}
}

最终效果展示


Windows系统下QT+OpenCasCAD仿真开发相关推荐

  1. Windows系统下安装配置 MinGW-w64 开发环境

    MinGW.MinGW-w64 简介 MinGW(全称为,Minimalist GNU for Windows),它实际上是将经典的开源 C语言编译器 GCC 移植到了 Windows 平台下,并且包 ...

  2. Windows系统下多显示器模式开发

    转载:开发日记地址 http://blog.sina.com.cn/s/blog_4078ccd60100049a.html 这几天研究了一下Windows系统的多显示器模式的编程,实现了Window ...

  3. php larval框架运行环境,4种Windows系统下Laravel框架的开发环境安装及部署方法详解...

    1.准备工作 1.1PHP集成环境 这里我们使用的是XAMPP,XAMPP是一个功能强大的建站集成软件包,采用一键安装的方式,包含PHP7.0.Mysql.Tomcat等.最新版下载地址:PHP 5. ...

  4. Windows系统下Qt代码的QMake和CMake简单说明

    在学习Qt编程时,一般书籍中会介绍Qt代码的编译流程,在命令行中操作的基本流程如下: 1.cd到代码文件夹,运行qmake -project指令 在文件夹内会生成*.pro文件,该文件主要是描述了当前 ...

  5. windows系统下的Qt安装

    Qt Creator下载和安装(详细教程) 简介 Qt是跨平台的图形开发库,目前由Digia全资子公司 Qt Company 独立运营,官方网址:  http://www.qt.io/  也可以访问Q ...

  6. 如何正确入门Windows系统下驱动开发领域?

    [作者] 猪头三 作者网站: http://www.x86asm.com 原文链接: http://blog.csdn.net/Code_GodFather/...0/5975901.aspx [贡献 ...

  7. Windows系统下基于开源软件的多物理场仿真

    Windows系统下基于开源软件的多物理场仿真实践技术应用 随着计算机技术的发展,计算机仿真技术日益成为继实验和理论之后的第三种重要研究和设计手段.真实世界中遇到的问题往往是固体力学,流体力学,热,电 ...

  8. Java windows系统下JDK开发环境配置和环境变量配置

    一.JDK下载地址 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 二.Ecli ...

  9. Windows系统下基于开源软件的多物理场仿真实践技术

    目录 专题一:多物理场有限元方法 专题二:前处理 专题三:单物理场举例 专题四:多物理场耦合 专题五:编程进阶 随着计算机技术的发展,计算机仿真技术日益成为继实验和理论之后的第三种重要研究和设计手段. ...

最新文章

  1. Web前端经典面试试题(二)
  2. 怎么计算一组数据的波动_数据分析(一):数据描述统计
  3. php和mysql入门_PHP和MySQL入门(10)
  4. CC2530的串口实验
  5. 动态asp网页批量生成静态html网页问题
  6. leetcode c程序总提示主函数_Matlab系列之函数嵌套
  7. Django05-2:路由分发/命名空间/伪静态/虚拟环境/django版本区别
  8. 关于推送系统设计的一些总结与思考(三)
  9. C++类设计的一些心得
  10. WEB页面性能指标与建议
  11. Python 3.6模拟输入并爬取百度前10页密切相关链接
  12. fmea第五版pfmea表格_FMEA第五版中文版.pdf
  13. Revit (6) - Teigha - LayerTable
  14. 软件测试报告 图书管理系统,图书管理系统报告 图书馆管理系统报告
  15. win10 oracle安装
  16. 设计模式(Java)—Facade模式
  17. Spring Guide:Securing a Web Application(中文大概意思)
  18. c语言pow的作用,c语言中pow函数的用法是什么?
  19. JSON简介与解析方法(超级详细)
  20. matlab第三章笔记

热门文章

  1. 阿里云总裁胡晓明:人工智能要去泡沫化,下一站将是“产业AI”
  2. 曹丕《燕歌行二首》其一赏析
  3. linux主机安全加固,linux主机安全加固方案.doc
  4. 计算机怎样发现路由器上u盘,竟然能当路由器 优盘技巧解锁更多姿势
  5. Matlab重复测量的方差分析,两因素重复测量方差分析,史上最详细SPSS教程!
  6. 专访光庭:传统测绘与众包模式融合,高精地图呼唤新的算法与平台
  7. 计算机考试准考证怎么一页打下来
  8. 赶紧趁着金三银四的尾巴来学习,钻五直接面阿里。阿里教你怎么玩转限流方案。
  9. Worksheet对象应用大全(1)-应用基础
  10. ijkplayer加速播放