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
繁體地址,請注明出處:http://hk.pswp.cn/diannao/86113.shtml
英文地址,請注明出處:http://en.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…