C# OCCT Winform 界面搭建

2024-06-19 13:20

本文主要是介绍C# OCCT Winform 界面搭建,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

1.创建一个WInform项目

2.代码总览

代码解析

3.添加模型到场景

4.鼠标交互


1.创建一个WInform项目

2.代码总览

using Macad.Occt.Helper;
using Macad.Occt;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Kitware.VTK;
using static System.Net.Mime.MediaTypeNames;namespace OCCTTestProject
{public class OCCTView : Form{private V3d_Viewer _viewer;private AIS_InteractiveContext _context;private WNT_Window _window;private V3d_View _view;protected int myXmin;protected int myYmin;protected int myXmax;protected int myYmax;public OCCTView( ){//InitWindow();}public void InitWindow(){// 创建 OpenGL 驱动var ocGraphicDriver = Graphic3d.CreateOpenGlDriver();// 初始化 Viewer_viewer = new V3d_Viewer(ocGraphicDriver);_viewer.SetDefaultViewSize(1000.0);_viewer.SetDefaultLights();_viewer.SetDefaultViewProj(V3d_TypeOfOrientation.XnegYposZpos);_viewer.SetDefaultBackgroundColor(new Quantity_Color(Quantity_NameOfColor.BLACK));_viewer.SetDefaultVisualization(V3d_TypeOfVisualization.ZBUFFER);_viewer.SetLightOn();// 创建 WNT_Window 并绑定到 Panel 控件_window = new WNT_Window(this.Handle);_window.SetBackground(new Quantity_Color(Quantity_NameOfColor.BLACK));_window.Map();// 创建 View_view = _viewer.CreateView();_view.SetWindow(_window);//AIS_AnimationCamera AisAnimationCamera = new AIS_AnimationCamera(new TCollection_AsciiString("ViewCamera"), _view);// 设置视图属性// 设置小坐标轴 1.位置左下角 2.xyz 字体颜色 3.大小 4.箭头粗细_view.TriedronDisplay(Aspect_TypeOfTriedronPosition.LEFT_LOWER, new Quantity_Color(Quantity_NameOfColor.RED), 0.2, V3d_TypeOfVisualization.WIREFRAME);_view.SetBackgroundColor(new Quantity_Color(Quantity_NameOfColor.RED4));_view.SetBgGradientColors(new Quantity_Color(Quantity_NameOfColor.SKYBLUE), new Quantity_Color(Quantity_NameOfColor.WHEAT), Aspect_GradientFillMethod.Vertical, false);// 配置渲染参数var renderParams = _view.ChangeRenderingParams();renderParams.NbMsaaSamples = 4;renderParams.IsAntialiasingEnabled = true;renderParams.TransparencyMethod = Graphic3d_RenderTransparentMethod.BLEND_OIT;renderParams.Method = Graphic3d_RenderingMode.RASTERIZATION;renderParams.RaytracingDepth = 3;renderParams.IsShadowEnabled = true;renderParams.IsReflectionEnabled = true;renderParams.IsTransparentShadowEnabled = true;// 关闭计算模式并更新视图_view.SetComputedMode(false);_view.Update();_view.MustBeResized();_view.SetImmediateUpdate(false);_view.Redraw();// 创建交互上下文_context = new AIS_InteractiveContext(_viewer);_context.UpdateCurrentViewer();_view.MustBeResized();_view.Redraw();}private void RenderShape(){// 创建一个简单的立方体var box = new BRepPrimAPI_MakeBox(100.0, 100.0, 100.0).Shape();var aisShape = new AIS_Shape(box);// 显示形状_context.Display(aisShape, 1, 1, true); // 实体// _context.Display(aisShape, true); // 线框var sphere = new BRepPrimAPI_MakeSphere(new Pnt(100,100,150),30).Shape();var aissphere = new AIS_Shape(sphere);// 显示形状_context.Display(aissphere,1, 1,true); // 实体// _context.Display(aissphere, true); // 线框// 调整视图_view.FitAll();_view.MustBeResized();_view.Redraw();}protected override void OnResize(EventArgs e){base.OnResize(e);if (_view != null){_view.MustBeResized();_view.Redraw();}}protected override void OnMouseDown(MouseEventArgs e){if(e.Button ==MouseButtons.Left){}else if(e.Button == MouseButtons.Right){SetDegenerateModeOn();StartRotation(e.X, e.Y);}myXmin = e.X; myYmin = e.Y;myXmax = e.X; myYmax = e.Y;}protected override void OnMouseUp(MouseEventArgs e){}protected override void OnMouseMove(MouseEventArgs e){if(e.Button == MouseButtons.Left){Zoom(myXmax, myYmax, e.X, e.Y);myXmax = e.X; myYmax = e.Y;}else if(e.Button == MouseButtons.Right){Rotation(e.X, e.Y);}else if(e.Button == MouseButtons.Middle){Pan(e.X - myXmax, myYmax - e.Y);myXmax = e.X; myYmax = e.Y;}else{Move(e.X, e.Y);}_view.Redraw();}protected void Move(int x, int y){_context.MoveTo(x, y, _view, true);}protected void Zoom(int x1, int y1, int x2, int y2){_view.Zoom(x1, y1, x2,y2);}protected void Rotation(int x, int y){_view.Rotation(x, y);}protected void StartRotation(int x, int y){_view.StartRotation(x, y);}protected void SetDegenerateModeOn(){_view.SetComputedMode(false);_view.Redraw();}protected void Pan(int x, int y){_view.Pan(x,y);}}
}

代码解析

1.OCCT 与 Winform控件建立联系 是通过 WNT_Window  

_window = new WNT_Window(this.Handle);

因为this 是继承了 Form 所以相当于把Form 做为显示界面,和vtk 使用panel 的方式不同。Form 想要作为子控件使用需要通过MDI 方式嵌入到主Form中。

  public Form1(){InitializeComponent();oCCTView = new OCCTView();oCCTView.MdiParent = this;oCCTView.Show();this.WindowState = FormWindowState.Maximized;oCCTView.WindowState = FormWindowState.Maximized;}private void Form1_Load(object sender, EventArgs e){oCCTView.InitWindow();}

  oCCTView.InitWindow(); 这里不放在Load 会报错,原因未知。

2.V3d_Viewer

①SetDefaultViewProj 当前视角

SetDefaultViewProj(V3d_TypeOfOrientation.XnegZpos)

SetDefaultViewProj(V3d_TypeOfOrientation.XnegYposZpos)

②SetLightOn 设置平行光、点光源或聚光灯 

关闭光源时

 _viewer.SetLightOff();

③V3d_View

TriedronDisplay:小坐标轴设置

参数:1.坐标轴位置  2.xyz 字体颜色  3.坐标轴大小  4.箭头粗细

SetBackgroundColor:在不使用SetBgGradientColors情况下设置单一背景色

 _view.SetBackgroundColor(new Quantity_Color(Quantity_NameOfColor.YELLOW));

SetBgGradientColors:创建渐变色背景。

1.渐变起点色

2.渐变终点色

3.渐变方向:水平,垂直,对角线,中心扩散

中心渐变

 renderParams.NbMsaaSamples 抗锯齿 MSAA 是一种抗锯齿技术,用于减少图像中的锯齿边缘。较高的采样数会提供更好的图像质量,但也会增加计算负担。

 renderParams.NbMsaaSamples = 0;             renderParams.NbMsaaSamples = 4;

RaytracingDepth:光线追踪的递归深度。光线追踪是一种渲染技术,通过模拟光线的传播路径来生成图像。较高的深度可以生成更真实的图像,但会增加计算复杂度。

Redraw:更新场景。

MustBeResized:标记视图需要调整大小,并在下次更新时重新计算视图的投影矩阵和其他尺寸相关的参数

3.添加模型到场景

 // 创建一个简单的立方体var box = new BRepPrimAPI_MakeBox(100.0, 100.0, 100.0).Shape();var aisShape = new AIS_Shape(box);// 显示形状_context.Display(aisShape, 1, 1, true); // 实体// _context.Display(aisShape, true); // 线框var sphere = new BRepPrimAPI_MakeSphere(new Pnt(100,100,150),30).Shape();var aissphere = new AIS_Shape(sphere);// 显示形状_context.Display(aissphere,1, 1,true); // 实体// _context.Display(aissphere, true); // 线框// 调整视图_view.FitAll();_view.MustBeResized();_view.Redraw();

线框:

 _view.FitAll(); 调整模型在场景合适位置。

4.鼠标交互

因为 OCCTView  是一个Form 因此直接使用Form的鼠标交互

   protected override void OnResize(EventArgs e){base.OnResize(e);if (_view != null){_view.MustBeResized();_view.Redraw();}}protected override void OnMouseDown(MouseEventArgs e){if(e.Button ==MouseButtons.Left){}else if(e.Button == MouseButtons.Right){SetDegenerateModeOn();StartRotation(e.X, e.Y);}myXmin = e.X; myYmin = e.Y;myXmax = e.X; myYmax = e.Y;}protected override void OnMouseUp(MouseEventArgs e){}protected override void OnMouseMove(MouseEventArgs e){if(e.Button == MouseButtons.Left){Zoom(myXmax, myYmax, e.X, e.Y);myXmax = e.X; myYmax = e.Y;}else if(e.Button == MouseButtons.Right){Rotation(e.X, e.Y);}else if(e.Button == MouseButtons.Middle){Pan(e.X - myXmax, myYmax - e.Y);myXmax = e.X; myYmax = e.Y;}else{Move(e.X, e.Y);}_view.Redraw();}protected void Move(int x, int y){_context.MoveTo(x, y, _view, true);}protected void Zoom(int x1, int y1, int x2, int y2){_view.Zoom(x1, y1, x2,y2);}protected void Rotation(int x, int y){_view.Rotation(x, y);}protected void StartRotation(int x, int y){_view.StartRotation(x, y);}protected void SetDegenerateModeOn(){_view.SetComputedMode(false);_view.Redraw();}protected void Pan(int x, int y){_view.Pan(x,y);}

我也刚刚接触 OCCT 也在学习中,希望互相交流。

这篇关于C# OCCT Winform 界面搭建的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1075147

相关文章

C# 中变量未赋值能用吗,各种类型的初始值是什么

对于一个局部变量,如果未赋值,是不能使用的 对于属性,未赋值,也能使用有系统默认值,默认值如下: 对于 int 类型,默认值是 0;对于 int? 类型,默认值是 null;对于 bool 类型,默认值是 false;对于 bool? 类型,默认值是 null;对于 string 类型,默认值是 null;对于 string? 类型,哈哈,没有这种写法,会出错;对于 DateTime 类型,默

一份LLM资源清单围观技术大佬的日常;手把手教你在美国搭建「百万卡」AI数据中心;为啥大模型做不好简单的数学计算? | ShowMeAI日报

👀日报&周刊合集 | 🎡ShowMeAI官网 | 🧡 点赞关注评论拜托啦! 1. 为啥大模型做不好简单的数学计算?从大模型高考数学成绩不及格说起 司南评测体系 OpenCompass 选取 7 个大模型 (6 个开源模型+ GPT-4o),组织参与了 2024 年高考「新课标I卷」的语文、数学、英语考试,然后由经验丰富的判卷老师评判得分。 结果如上图所

C#中,decimal类型使用

在Microsoft SQL Server中numeric类型,在C#中使用的时候,需要用decimal类型与其对应,不能使用int等类型。 SQL:numeric C#:decimal

【网络安全的神秘世界】搭建dvwa靶场

🌝博客主页:泥菩萨 💖专栏:Linux探索之旅 | 网络安全的神秘世界 | 专接本 | 每天学会一个渗透测试工具 下载DVWA https://github.com/digininja/DVWA/blob/master/README.zh.md 安装DVWA 安装phpstudy https://editor.csdn.net/md/?articleId=1399043

cocospod 搭建环境和使用

iOS 最新版 CocoaPods 的安装流程 1.移除现有Ruby默认源 $gem sources --remove https://rubygems.org/ 2.使用新的源 $gem sources -a https://ruby.taobao.org/ 3.验证新源是否替换成功 $gem sources -l 4.安装CocoaPods (1)  $sudo gem

算法与数据结构面试宝典——回溯算法详解(C#,C++)

文章目录 1. 回溯算法的定义及应用场景2. 回溯算法的基本思想3. 递推关系式与回溯算法的建立4. 状态转移方法5. 边界条件与结束条件6. 算法的具体实现过程7. 回溯算法在C#,C++中的实际应用案例C#示例C++示例 8. 总结回溯算法的主要特点与应用价值 回溯算法是一种通过尝试各种可能的组合来找到所有解的算法。这种算法通常用于解决组合问题,如排列、组合、棋盘游

Apache2.4+PHP7.2环境搭建

Editplus生成码:http://www.jb51.net/tools/editplus/ 阿帕奇下载地址:https://www.apachehaus.com/cgi-bin/download.plx PHP下载地址:http://windows.php.net/download#php-7.2 1.打开阿帕奇的下载地址,点击下载。

【Qt6.3 基础教程 17】 Qt布局管理详解:创建直观和响应式UI界面

文章目录 前言布局管理的基础为什么需要布局管理器? 盒布局:水平和垂直排列小部件示例:创建水平盒布局 栅格布局:在网格中对齐小部件示例:创建栅格布局 表单布局:为表单创建标签和字段示例:创建表单布局 调整空间和伸缩性示例:增加弹性空间 总结 前言 当您开始使用Qt设计用户界面(UI)时,理解布局管理是至关重要的。布局管理不仅关系到UI的外观,更直接影响用户交互的体验。本篇博

C# 命名管道中客户端访问服务器时,出现“对路径的访问被拒绝”

先还原一下我出现错误的情景:我用C#控制台写了一个命名管道服务器,然后用ASP.NET写了一个客户端访问服务器,运行之后出现了下面的错误: 原因:服务器端的访问权限不够,所以是服务器端的问题,需要增加访问权限。(网上很多都说是文件夹的权限不够,情况不同,不适用于我这种情况) 解决办法: (1)在服务器端相应地方添加以下代码。 PipeSecurity pse = new PipeSec

如何通过示例将旧版 C# 转换为 C# 12

随着 C# 的不断发展,每个新版本都会引入强大的新功能,从而提高语言的功能和可读性。通过从旧版本的 C# 迁移到 C# 12,您可以获得更高效、更易于维护和更具表现力的代码。 由于代码库遗留、公司限制以及对旧语言功能的熟悉,许多开发人员仍在使用旧版本的 C#。升级似乎很困难,但现代版本的 C# 具有显著的优势,例如更好的性能、增强的功能和更高的安全性。 通过增量重构、试点项目和团队培训逐步