From 0469853a2cceb3d7d36bacd9061ae9838c399a15 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Tue, 3 Mar 2026 15:46:26 -0700 Subject: [PATCH] refactor: Adopt gp_Ax2 for sketch plane and geometry definition Co-authored-by: aider (gemini/gemini-2.5-pro) --- src/ApplicationController.cpp | 61 ++++++++++++----------------------- src/ApplicationController.h | 6 ++-- 2 files changed, 23 insertions(+), 44 deletions(-) diff --git a/src/ApplicationController.cpp b/src/ApplicationController.cpp index f551cb3..5c81ad1 100644 --- a/src/ApplicationController.cpp +++ b/src/ApplicationController.cpp @@ -16,9 +16,11 @@ #include #include #include +#include #include #include #include +#include #include #include @@ -124,26 +126,12 @@ void ApplicationController::beginSketchCreation() emit planeSelectionModeStarted(); } -void ApplicationController::onPlaneSelected(ViewportWidget::SketchPlane plane) +void ApplicationController::onPlaneSelected(const gp_Ax2& plane) { auto feature = new SketchFeature("Sketch"); m_activeSketch = feature; - switch (plane) { - case ViewportWidget::SketchPlane::XY: - feature->setPlane(SketchFeature::SketchPlane::XY); - break; - case ViewportWidget::SketchPlane::XZ: - feature->setPlane(SketchFeature::SketchPlane::XZ); - break; - case ViewportWidget::SketchPlane::YZ: - feature->setPlane(SketchFeature::SketchPlane::YZ); - break; - case ViewportWidget::SketchPlane::NONE: - delete feature; - m_activeSketch = nullptr; - return; - } + feature->setPlane(plane); m_document->addFeature(feature); emit sketchModeStarted(plane); @@ -165,23 +153,21 @@ void ApplicationController::addRectangle(const gp_Pnt& corner1, const gp_Pnt& co if (m_activeSketch) { m_activeSketch->addObject(new SketchRectangle(corner1, corner2)); - gp_Pnt c2, c4; - auto plane = m_activeSketch->plane(); - if (plane == SketchFeature::SketchPlane::XY) { - c2.SetX(corner2.X()); c2.SetY(corner1.Y()); c2.SetZ(corner1.Z()); - c4.SetX(corner1.X()); c4.SetY(corner2.Y()); c4.SetZ(corner1.Z()); - } else if (plane == SketchFeature::SketchPlane::XZ) { - c2.SetX(corner2.X()); c2.SetY(corner1.Y()); c2.SetZ(corner1.Z()); - c4.SetX(corner1.X()); c4.SetY(corner1.Y()); c4.SetZ(corner2.Z()); - } else { // YZ - c2.SetX(corner1.X()); c2.SetY(corner2.Y()); c2.SetZ(corner1.Z()); - c4.SetX(corner1.X()); c4.SetY(corner1.Y()); c4.SetZ(corner2.Z()); - } + const auto& plane = m_activeSketch->plane(); - TopoDS_Edge e1 = BRepBuilderAPI_MakeEdge(corner1, c2); - TopoDS_Edge e2 = BRepBuilderAPI_MakeEdge(c2, corner2); - TopoDS_Edge e3 = BRepBuilderAPI_MakeEdge(corner2, c4); - TopoDS_Edge e4 = BRepBuilderAPI_MakeEdge(c4, corner1); + gp_Pnt2d p1_2d = ElCLib::To2d(corner1, plane); + gp_Pnt2d p3_2d = ElCLib::To2d(corner2, plane); + + gp_Pnt2d p2_2d(p3_2d.X(), p1_2d.Y()); + gp_Pnt2d p4_2d(p1_2d.X(), p3_2d.Y()); + + gp_Pnt p2_3d = ElCLib::To3d(p2_2d, plane); + gp_Pnt p4_3d = ElCLib::To3d(p4_2d, plane); + + TopoDS_Edge e1 = BRepBuilderAPI_MakeEdge(corner1, p2_3d); + TopoDS_Edge e2 = BRepBuilderAPI_MakeEdge(p2_3d, corner2); + TopoDS_Edge e3 = BRepBuilderAPI_MakeEdge(corner2, p4_3d); + TopoDS_Edge e4 = BRepBuilderAPI_MakeEdge(p4_3d, corner1); BRepBuilderAPI_MakeWire wireMaker(e1, e2, e3, e4); if (wireMaker.IsDone()) { @@ -195,15 +181,8 @@ void ApplicationController::addCircle(const gp_Pnt& center, double radius) if (m_activeSketch) { m_activeSketch->addObject(new SketchCircle(center, radius)); - gp_Ax2 axis; - auto plane = m_activeSketch->plane(); - if (plane == SketchFeature::SketchPlane::XY) { - axis = gp_Ax2(center, gp::DZ()); - } else if (plane == SketchFeature::SketchPlane::XZ) { - axis = gp_Ax2(center, gp::DY()); - } else { // YZ - axis = gp_Ax2(center, gp::DX()); - } + const auto& sketchPlane = m_activeSketch->plane(); + gp_Ax2 axis(center, sketchPlane.Direction()); gp_Circ circle(axis, radius); TopoDS_Edge edge = BRepBuilderAPI_MakeEdge(circle); diff --git a/src/ApplicationController.h b/src/ApplicationController.h index 7e36ff7..58651ae 100644 --- a/src/ApplicationController.h +++ b/src/ApplicationController.h @@ -9,8 +9,8 @@ #define APPLICATIONCONTROLLER_H #include -#include "ViewportWidget.h" // For SketchPlane enum #include +#include class Document; class MainWindow; @@ -47,12 +47,12 @@ public slots: bool saveDocumentAs(); void beginSketchCreation(); - void onPlaneSelected(ViewportWidget::SketchPlane plane); + void onPlaneSelected(const gp_Ax2& plane); void endSketch(); signals: void planeSelectionModeStarted(); - void sketchModeStarted(ViewportWidget::SketchPlane plane); + void sketchModeStarted(const gp_Ax2& plane); void sketchModeEnded(); void currentFileChanged(const QString& path); void activeToolChanged(ToolType tool);