chili3d 笔记17 c++ 编译hlr 带隐藏线工程图

这个要注册不然emscripten编译不起来

---------------


行不通


----------------



结构体

 using LineSegment = std::pair<gp_Pnt, gp_Pnt>;using LineSegmentList = std::vector<LineSegment>;
EMSCRIPTEN_BINDINGS(Shape_Projection) {value_object<LineSegment>("LineSegment").field("first", &LineSegment::first).field("second", &LineSegment::second);// 绑定 LineSegmentList (std::vector<LineSegment>)register_vector<LineSegment>("LineSegmentList");class_<ProjectionResult>("ProjectionResult").property("visible", &ProjectionResult::visible).property("hidden", &ProjectionResult::hidden);class_<ShapeProjection>("ShapeProjection").class_function("projection", &ShapeProjection::GetProjectionEdges);}

 


printf无效,要用cout

deepwiki写occ代码真的强

视图偏移还有点问题

import { IApplication, Logger, PubSub, ShapeNode } from "chili-core";
import { getProjectionEdges, gp_Pnt, LineSegmentList, OccShape, ProjectionResult2 } from "chili-wasm";interface Segment {first: gp_Pnt;second: gp_Pnt;
}export class njsgcs_drawingView extends HTMLElement {private viewportCanvas2d: HTMLCanvasElement | null = null;private app: IApplication | null = null;constructor() {super();PubSub.default.sub("njsgcs_drawview", async (app: IApplication) => {Logger.info("njsgcs_drawview event triggered");if (this.viewportCanvas2d) {this.removeChild(this.viewportCanvas2d);this.viewportCanvas2d = null;}this.app = app;const canvas = this.createCanvas();this.appendChild(canvas);});}private drawProjectionEdges(ctx: CanvasRenderingContext2D, projection: ProjectionResult2) {// 清除画布ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);// 获取所有线段并合并用于自动缩放计算const allSegments = [...this.toArray(projection.f_visible),...this.toArray(projection.f_hidden),...this.toArray(projection.s_visible),...this.toArray(projection.s_hidden),...this.toArray(projection.t_visible),...this.toArray(projection.t_hidden),];// 自动计算缩放和偏移const { minX, maxX, minY, maxY } = this.calculateBounds(allSegments);const margin = 50;const availableWidth = ctx.canvas.width - 2 * margin;const availableHeight = ctx.canvas.height - 2 * margin;const scaleX = availableWidth / (maxX - minX || 1);const scaleY = availableHeight / (maxY - minY || 1);const scale = Math.min(scaleX, scaleY) * 0.9; // 留点边距const offsetX = ctx.canvas.width / 2;const offsetY = ctx.canvas.height / 2;// 定义各视图偏移const views = [{name: 'front',segmentsVisible: this.toArray(projection.f_visible),segmentsHidden: this.toArray(projection.f_hidden),offset: { x: -availableWidth / 3, y: 0 },},{name: 'side',segmentsVisible: this.toArray(projection.s_visible),segmentsHidden: this.toArray(projection.s_hidden),offset: { x: 0, y: 0 },},{name: 'top',segmentsVisible: this.toArray(projection.t_visible),segmentsHidden: this.toArray(projection.t_hidden),offset: { x: availableWidth / 3, y: 0 },},];// 绘制每个视图for (const view of views) {// 实线:可见线this.drawSegments(ctx,view.segmentsVisible,false,scale,offsetX + view.offset.x,offsetY + view.offset.y);// 虚线:隐藏线this.drawSegments(ctx,view.segmentsHidden,true,scale,offsetX + view.offset.x,offsetY + view.offset.y);}}  private calculateBounds(segments: Segment[]) {let minX = Infinity;let maxX = -Infinity;let minY = Infinity;let maxY = -Infinity;for (const segment of segments) {if (segment && segment.first && segment.second) {const points = [segment.first, segment.second];for (const p of points) {minX = Math.min(minX, p.x);maxX = Math.max(maxX, p.x);minY = Math.min(minY, p.y);maxY = Math.max(maxY, p.y);}}}return {minX: minX === Infinity ? 0 : minX,maxX: maxX === -Infinity ? 0 : maxX,minY: minY === Infinity ? 0 : minY,maxY: maxY === -Infinity ? 0 : maxY,};}private drawSegments(ctx: CanvasRenderingContext2D,segments: Segment[],isHidden: boolean,scale: number,offsetX: number,offsetY: number) {ctx.strokeStyle = isHidden ? "gray" : "black";ctx.lineWidth = isHidden ? 1 : 2;ctx.setLineDash(isHidden ? [5, 5] : []);for (const segment of segments) {if (segment && segment.first && segment.second) {ctx.beginPath();ctx.moveTo(segment.first.x * scale + offsetX,-segment.first.y * scale + offsetY);ctx.lineTo(segment.second.x * scale + offsetX,-segment.second.y * scale + offsetY);ctx.stroke();}}}private toArray(segmentList: LineSegmentList): Segment[] {const result = [];for (let i = 0; i < segmentList.size(); i++) {const segment = segmentList.get(i);if (segment) {result.push(segment);}}return result;}private createCanvas(): HTMLCanvasElement {if (!this.viewportCanvas2d) {this.viewportCanvas2d = document.createElement("canvas");this.viewportCanvas2d.width = 900;this.viewportCanvas2d.height = 600;this.viewportCanvas2d.style.border = "1px solid #000";const ctx = this.viewportCanvas2d.getContext("2d");if (ctx) {const document = this.app!.activeView?.document;if (!document) return this.viewportCanvas2d;const geometries = document.selection.getSelectedNodes();const entities = geometries.filter((x) => x instanceof ShapeNode);for (const entity of entities) {const shapeResult = entity.shape;if (shapeResult.isOk) {const shape = shapeResult.value; // 获取IShape  // 检查是否为OccShape实例  if (shape instanceof OccShape) {const topoShape = shape.shape; // 访问TopoDS_Shape  const ProjectionEdges=getProjectionEdges(topoShape);this.drawProjectionEdges(ctx,ProjectionEdges)}}}}}return this.viewportCanvas2d!;}}customElements.define("njsgcs-drawing-view", njsgcs_drawingView);
import { LineSegmentList, TopoDS_Shape } from "../lib/chili-wasm";
export { LineSegmentList, ProjectionResult2 };
interface ProjectionResult2 {f_visible:LineSegmentList ,f_hidden: LineSegmentList ,s_visible: LineSegmentList ,s_hidden: LineSegmentList ,t_visible: LineSegmentList ,t_hidden:LineSegmentList ,}
export function getProjectionEdges(shape: TopoDS_Shape,): { f_visible: LineSegmentList; f_hidden: LineSegmentList,s_visible: LineSegmentList; s_hidden: LineSegmentList,t_visible: LineSegmentList; t_hidden: LineSegmentList} {console.info("test1");const f_result = wasm.ShapeProjection.projection(shape, new wasm.gp_Dir(0, 1, 0));console.info("first:"+f_result.visible.get(0)?.first);const s_result = wasm.ShapeProjection.projection(shape, new wasm.gp_Dir( 1,0, 0));const t_result = wasm.ShapeProjection.projection(shape, new wasm.gp_Dir( 0, 0,1));return {f_visible: f_result.visible,f_hidden: f_result.hidden,s_visible: s_result.visible,s_hidden: s_result.hidden,t_visible: t_result.visible,t_hidden: t_result.hidden,};
}
#include <BRepPrimAPI_MakeBox.hxx>  
#include <BRepPrimAPI_MakeCylinder.hxx>  
#include <BRepAlgoAPI_Cut.hxx>  
#include <gp_Pnt.hxx>  
#include <gp_Dir.hxx>  
#include <gp_Ax2.hxx>  
#include <HLRBRep_Algo.hxx>  
#include <HLRBRep_HLRToShape.hxx>  
#include <HLRAlgo_Projector.hxx>  
#include <BRepAdaptor_Curve.hxx>  
#include <GCPnts_UniformDeflection.hxx>  
#include <TopExp_Explorer.hxx>  
#include <TopoDS.hxx>  
#include <vector>  
#include <emscripten/bind.h>
#include <tuple>
#include <BRep_Tool.hxx>
#include <TopExp_Explorer.hxx>
#include <TopoDS_Edge.hxx>
#include <Geom_Line.hxx>
using namespace emscripten;
std::vector<std::pair<gp_Pnt, gp_Pnt>> ExtractLineSegments(const TopoDS_Shape& shape) {  std::vector<std::pair<gp_Pnt, gp_Pnt>> lineSegments;  for (TopExp_Explorer edgeExplorer(shape, TopAbs_EDGE); edgeExplorer.More(); edgeExplorer.Next()) {  TopoDS_Edge edge = TopoDS::Edge(edgeExplorer.Current());  // 优先使用顶点方法  TopoDS_Vertex aFirst, aLast;  TopExp::Vertices(edge, aFirst, aLast, Standard_True);  if (!aFirst.IsNull() && !aLast.IsNull()) {  gp_Pnt startPnt = BRep_Tool::Pnt(aFirst);  gp_Pnt endPnt = BRep_Tool::Pnt(aLast);  lineSegments.emplace_back(startPnt, endPnt);  //std::cout << "startPnt: X=" << startPnt.X() << " Y=" << startPnt.Y() << " Z=" << startPnt.Z() << std::endl;} }  return lineSegments;  
}
// Convert 3D edge to 2D points  
struct ProjectionResult {std::vector<std::pair<gp_Pnt, gp_Pnt>> visible;std::vector<std::pair<gp_Pnt, gp_Pnt>> hidden;ProjectionResult(const std::vector<std::pair<gp_Pnt, gp_Pnt>>& vis,const std::vector<std::pair<gp_Pnt, gp_Pnt>>& hid) : visible(vis), hidden(hid) {}
};class ShapeProjection {  public:  static ProjectionResult GetProjectionEdges(const TopoDS_Shape& shape, const gp_Dir& direction) {  // Create projector  gp_Ax3 viewAxis(gp_Pnt(0, 0, 0), direction);  gp_Trsf transformation;  transformation.SetTransformation(viewAxis);  HLRAlgo_Projector projector(transformation, Standard_False, 0.0);// Create HLR algorithm  Handle(HLRBRep_Algo) hlr_algo = new HLRBRep_Algo();  hlr_algo->Add(shape);  hlr_algo->Projector(projector);  hlr_algo->Update();  hlr_algo->Hide();  // Extract visible and hidden edges  HLRBRep_HLRToShape hlr_to_shape(hlr_algo);  TopoDS_Shape visible_edges = hlr_to_shape.VCompound();  TopoDS_Shape hidden_edges = hlr_to_shape.HCompound();  auto visible_line_segments = ExtractLineSegments(visible_edges);auto hidden_line_segments = ExtractLineSegments(hidden_edges);  return ProjectionResult(visible_line_segments, hidden_line_segments); }  };  using LineSegment = std::pair<gp_Pnt, gp_Pnt>;using LineSegmentList = std::vector<LineSegment>;
EMSCRIPTEN_BINDINGS(Shape_Projection) {value_object<LineSegment>("LineSegment").field("first", &LineSegment::first).field("second", &LineSegment::second);register_vector<LineSegment>("LineSegmentList");class_<ProjectionResult>("ProjectionResult").property("visible", &ProjectionResult::visible).property("hidden", &ProjectionResult::hidden);class_<ShapeProjection>("ShapeProjection").class_function("projection", &ShapeProjection::GetProjectionEdges);}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/diannao/86113.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Java开发日记】说一说 SpringBoot 中 CommandLineRunner

目录 1、CommandLineRunner SpringBoot中CommandLineRunner的作用 简单例子 多个类实现CommandLineRunner接口执行顺序的保证 通过实现Ordered接口实现控制执行顺序 通过Order注解实现控制执行顺序 Order 作用 2、ApplicationRunner 3、传递参数 4、源码跟踪 run()方…

为什么React列表项需要key?(React key)(稳定的唯一标识key有助于React虚拟DOM优化重绘大型列表)

文章目录 1. **帮助 React 识别列表项的变化**2. **性能优化**3. **避免组件状态混乱**4. **为什么使用 rpid 作为 key**5. **不好的做法示例**6. **✅ 正确的做法** 在 React 中添加 key{item.rpid} 是非常重要的&#xff0c;主要有以下几个原因&#xff1a; 1. 帮助 React 识…

算法笔记2

1.字符串拼接最好用StringBuilder&#xff0c;不用String 2.创建List<>类型的数组并创建内存 List arr[] new ArrayList[26]; Arrays.setAll(arr, i -> new ArrayList<>());

DeepSeek09-open-webui使用

Open WebUI 完全指南&#xff1a;从安装到知识库搭建与异常处理 最后更新&#xff1a;2025年6月7日 | 适用版本&#xff1a;Open WebUI v0.6.x 一、安装部署 1.1 系统要求 **Python 3.12 **&#xff08;严格版本要求&#xff0c;更高版本3.13不兼容&#xff09;Node.js 20.x内…

前端面试五之vue2基础

1.属性绑定v-bind&#xff08;&#xff1a;&#xff09; v-bind 是 Vue 2 中用于动态绑定属性的核心指令&#xff0c;它支持多种语法和用法&#xff0c;能够灵活地绑定 DOM 属性、组件 prop&#xff0c;甚至动态属性名。通过 v-bind&#xff0c;可以实现数据与视图之间的高效同…

408第一季 - 数据结构 - 栈与队列

栈 闲聊 栈是一个线性表 栈的特点是后进先出 然后是一个公式 比如123要入栈&#xff0c;一共有5种排列组合的出栈 栈的数组实现 这里有两种情况&#xff0c;&#xff0c;一个是有下标为-1的&#xff0c;一个没有 代码不用看&#xff0c;真题不会考 栈的链式存储结构 L ->…

Linux(14)——库的制作与原理

库制作与原理技术文章大纲 库的基本概念与分类 定义&#xff1a;库&#xff08;Library&#xff09;在编程中的核心作用与意义分类&#xff1a;静态库&#xff08;Static Library&#xff09;、动态库&#xff08;Dynamic Library&#xff09;的差异与应用场景常见示例&#…

2025政务服务便民热线创新发展会议顺利召开,张晨博士受邀分享

5月28日&#xff0c;由新华社中国经济信息社、新华社广东分社联合主办的2025政务服务便民热线创新发展暨“人工智能热线”会议在广州举行。会议围绕“人工智能与新质热线”主题&#xff0c;邀请全国的12345政务服务便民热线主管部门负责人、省市热线负责人和专家学者&#xff0…

AI驱动的B端页面革命:智能布局、数据洞察的底层技术解析

摘要 ** 当企业 B 端系统的页面还在依赖设计师反复调整布局&#xff0c;靠人工熬夜分析数据时&#xff0c;竞争对手已借助 AI 实现页面的自动优化与智能决策。为何有的 B 端系统界面混乱&#xff0c;操作繁琐&#xff0c;而 AI 赋能的页面却能精准适配用户需求&#xff0c;秒…

大故障:阿里云核心域名爆炸了

大故障&#xff1a;阿里云核心域名被拖走了 今天早上许多群里出现网站故障的讨论&#xff0c;比如 cnblogs 全国访问一片红&#xff0c;一看原来是阿里云又出故障了。 今天早上许多群里出现网站故障的讨论&#xff0c;比如 cnblogs 全国访问一片红&#xff0c;一看原来是阿里云…

第1讲、包管理和环境管理工具Conda 全面介绍

1. Conda 的背景与核心概念 1.1 什么是 Conda&#xff1f; Conda 是一个开源的、跨平台的、语言无关的包管理和环境管理系统。它最初由 Anaconda 公司开发&#xff0c;旨在解决 Python 数据科学家面临的包管理挑战&#xff0c;但现在已经发展成为一个适用于多种编程语言的通用…

第4天:RNN应用(心脏病预测)

&#x1f368; 本文为&#x1f517;365天深度学习训练营 中的学习记录博客&#x1f356; 原作者&#xff1a;K同学啊 目标 具体实现 &#xff08;一&#xff09;环境 语言环境&#xff1a;Python 3.10 编 译 器: PyCharm 框 架: Pytorch &#xff08;二&#xff09;具体步骤…

STM32学习笔记:外部中断(EXTI)原理与应用详解

前言 在嵌入式系统开发中&#xff0c;中断机制是提高系统实时性和效率的重要手段。相比传统的51单片机&#xff0c;STM32微控制器提供了更为丰富和灵活的外部中断资源。本文将全面介绍STM32的外部中断(EXTI)功能&#xff0c;包括其工作原理、配置方法和实际应用技巧。 一、外…

嵌入式知识篇---Zigbee串口

在 Python 中&#xff0c;serial和pyserial是经常被提及的两个库&#xff0c;它们在串口通信方面有着紧密的联系&#xff0c;但又存在一些差异。下面将对它们进行详细介绍&#xff0c;并给出各自的适用场景。 1. 基本概念 pyserial&#xff1a;它是 Python 里专门用于串口通信…

vue中的派发事件与广播事件,及广播事件应用于哪些场景和一个表单验证例子

在 Vue 2.X 中&#xff0c;$dispatch 和 $broadcast 方法已经被废弃。官方认为基于组件树结构的事件流方式难以理解&#xff0c;并且在组件结构扩展时容易变得脆弱。因此&#xff0c;Vue 2.X 推荐使用其他方式来实现组件间的通信&#xff0c;例如通过 $emit 和 $on 方法&#x…

阿里云事件总线 EventBridge 正式商业化,构建智能化时代的企业级云上事件枢纽

作者&#xff1a;肯梦、稚柳 产品演进历程&#xff1a;在技术浪潮中的成长之路 早在 2018 年&#xff0c;Gartner 评估报告便将事件驱动模型&#xff08;Event-Driven Model&#xff09;列为十大战略技术趋势之一&#xff0c;指出事件驱动架构&#xff08;EDA&#xff0c;Eve…

《前端面试题:BFC(块级格式化上下文)》

前端BFC完全指南&#xff1a;布局魔法与面试必备 &#x1f38b; 端午安康&#xff01; 各位前端探险家&#xff0c;端午节快乐&#xff01;&#x1f96e; 愿你的代码如龙舟竞渡般乘风破浪&#xff0c;样式如香糯粽子般完美包裹&#xff01;今天我们来解锁CSS中的布局魔法——B…

dvwa10——XSS(DOM)

XSS攻击&#xff1a; DOM型XSS 只在浏览器前端攻击触发&#xff1a;修改url片段代码不存储 反射型XSS 经过服务器攻击触发&#xff1a;可能通过提交恶意表单&#xff0c;连接触发代码不存储 存储型XSS 经由服务器攻击触发&#xff1a;可能通过提交恶意表单&#xff0c;连…

跨平台资源下载工具:res-downloader 的使用体验

一款基于 Go Wails 的跨平台资源下载工具&#xff0c;简洁易用&#xff0c;支持多种资源嗅探与下载。res-downloader 一款开源免费的下载软件(开源无毒、放心使用)&#xff01;支持Win10、Win11、Mac系统.支持视频、音频、图片、m3u8等网络资源下载.支持视频号、小程序、抖音、…

AOSP CachedAppOptimizer中的冻结和内存压缩功能

AOSP CachedAppOptimizer&#xff1a;应用进程长期处于 Cached 状态的内存压缩和冻结优化管控 冻结和内存压缩两个功能独立触发&#xff0c;可以单独触发也可以组合触发&#xff0c;默认顺序&#xff1a;先压缩&#xff0c;后冻结 public class OomAdjuster { protected b…