forked from 1b-agent/opencadstudio-upstream
Feat: ROADMAP Phase 1-5 batch — trim/extend, plot scale, viewport UX, mline caps, table alignment
- TRIM/EXTEND: Ray and XLine entities supported as cutting boundaries and trim targets; trim results in Ray→Line/Ray, XLine→Ray/Ray or Line; hover preview added - Plot scale override: Fit/1:1/1:2/1:5/1:10/1:20/1:50/1:100/2:1 buttons added to Page Setup panel; PageSetupCommit applies scale to PlotSettings - Viewport UX: locked viewport gets orange border; selected viewport gets white border - Named view → viewport assignment: Choice dropdown in properties panel copies view_target/direction/height to the viewport entity - MLine caps: perpendicular start/end cap lines added for open (non-closed) MLines - Wipeout polygon grips: boundary vertices become editable grips when clipping_enabled+Polygonal; world→pixel back-projection on drag - Table cell alignment: CellStyle.alignment (1-9) drives left/center/right + top/middle/bottom text positioning; cxf::measure_text() helper added - New commands/features: DIMCONTINUE (DCO), DIMBASELINE (DBA), TOLERANCE (TOL), TABLE, ATTDEF, ATTEDIT, ATTDISP, BREAKATPOINT (BAP), PLOTWINDOW (PW), MLINE (ML); DIMSTYLE/DIMCURRENT/CLAYER/LTSCALE/CELTSCALE/SCALETEXT/XDATA/UCSICON/REGEN/DRAWORDER extended - PLINE arc mode: A/L/CLOSE text input, bulge computation, arc preview - Warning cleanup: removed unused imports and unreachable patterns Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
27b5e124c7
commit
0aef1fc12f
27 files changed
+2217
-100
No files matched your search
@@ -0,0 +1,138 @@
|
||||
// DIMBASELINE command — stacked baseline dimensions all measured from the same origin.
|
||||
//
|
||||
// Each new point becomes the second extension line origin of a new dimension.
|
||||
// The first extension line is always the same base origin point.
|
||||
// Each new dimension line is placed further from the baseline by DIMDLI (increment).
|
||||
//
|
||||
// Constructed from commands.rs after finding the last placed linear/aligned dimension.
|
||||
|
||||
use acadrust::entities::{Dimension, DimensionLinear};
|
||||
use acadrust::types::Vector3;
|
||||
use acadrust::EntityType;
|
||||
use glam::Vec3;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::scene::wire_model::WireModel;
|
||||
|
||||
/// Default stacking increment (world units) between successive baseline dimensions.
|
||||
const DIMDLI: f32 = 1.5;
|
||||
|
||||
pub struct DimBaselineCommand {
|
||||
/// Fixed first-extension-line origin (never changes).
|
||||
base_p1: Vec3,
|
||||
/// Direction along the measurement direction (0.0 = horizontal, PI/2 = vertical).
|
||||
rotation: f64,
|
||||
/// Unit vector perpendicular to the dimension axis, pointing toward the dim line side.
|
||||
perp: Vec3,
|
||||
/// Perpendicular distance of the NEXT dimension line from the extension-line axis.
|
||||
next_offset: f32,
|
||||
/// True once we have a base dimension loaded.
|
||||
ready: bool,
|
||||
}
|
||||
|
||||
impl DimBaselineCommand {
|
||||
/// No base dim found — cancel immediately.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
base_p1: Vec3::ZERO,
|
||||
rotation: 0.0,
|
||||
perp: Vec3::Y,
|
||||
next_offset: DIMDLI,
|
||||
ready: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Build from the last placed dimension.
|
||||
///
|
||||
/// `p1` — first extension line origin (fixed baseline).
|
||||
/// `p2` — second extension line origin of the base dim (unused for placement, kept for context).
|
||||
/// `definition_point` — dim-line position of the base dim (defines perpendicular side).
|
||||
/// `rotation` — 0.0 = horizontal, PI/2 = vertical.
|
||||
pub fn from_base(p1: Vec3, _p2: Vec3, definition_point: Vec3, rotation: f64) -> Self {
|
||||
let axis = if rotation.abs() < 0.1 { Vec3::X } else { Vec3::Y };
|
||||
let perp = Vec3::new(-axis.y, axis.x, 0.0);
|
||||
let base_offset = (definition_point - p1).dot(perp);
|
||||
// Next baseline dim goes one DIMDLI further from the baseline.
|
||||
let next_offset = base_offset + DIMDLI;
|
||||
Self {
|
||||
base_p1: p1,
|
||||
rotation,
|
||||
perp,
|
||||
next_offset,
|
||||
ready: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CadCommand for DimBaselineCommand {
|
||||
fn name(&self) -> &'static str { "DIMBASELINE" }
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
if !self.ready {
|
||||
"DIMBASELINE No base dimension found. Place a dimension first.".into()
|
||||
} else {
|
||||
"DIMBASELINE Specify a second extension line origin (Enter to exit):".into()
|
||||
}
|
||||
}
|
||||
|
||||
fn on_point(&mut self, pt: Vec3) -> CmdResult {
|
||||
if !self.ready {
|
||||
return CmdResult::Cancel;
|
||||
}
|
||||
let p1 = self.base_p1;
|
||||
let p2 = pt;
|
||||
|
||||
// Build a new linear dimension.
|
||||
let mut dim = DimensionLinear::new(v3(p1), v3(p2));
|
||||
dim.rotation = self.rotation;
|
||||
|
||||
let dim_line_pt = p1 + self.perp * self.next_offset;
|
||||
let dim_line_pt2 = p2 + self.perp * self.next_offset;
|
||||
dim.definition_point = v3(dim_line_pt);
|
||||
dim.base.definition_point = v3(dim_line_pt);
|
||||
dim.base.text_middle_point = v3((dim_line_pt + dim_line_pt2) * 0.5);
|
||||
dim.base.insertion_point = dim.base.text_middle_point;
|
||||
dim.base.actual_measurement = dim.measurement();
|
||||
|
||||
// Stack the next dim line further out.
|
||||
self.next_offset += DIMDLI;
|
||||
|
||||
CmdResult::CommitEntity(EntityType::Dimension(Dimension::Linear(dim)))
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
|
||||
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
|
||||
if !self.ready {
|
||||
return None;
|
||||
}
|
||||
let p1 = self.base_p1;
|
||||
let dim_line_pt = p1 + self.perp * self.next_offset;
|
||||
let dim_line_pt2 = pt + self.perp * self.next_offset;
|
||||
Some(WireModel {
|
||||
name: "dimbase_preview".into(),
|
||||
points: vec![
|
||||
[p1.x, p1.y, p1.z], [dim_line_pt.x, dim_line_pt.y, dim_line_pt.z],
|
||||
[f32::NAN, 0.0, 0.0],
|
||||
[pt.x, pt.y, pt.z], [dim_line_pt2.x, dim_line_pt2.y, dim_line_pt2.z],
|
||||
[f32::NAN, 0.0, 0.0],
|
||||
[dim_line_pt.x, dim_line_pt.y, dim_line_pt.z],
|
||||
[dim_line_pt2.x, dim_line_pt2.y, dim_line_pt2.z],
|
||||
],
|
||||
color: WireModel::CYAN,
|
||||
selected: false,
|
||||
pattern_length: 0.0,
|
||||
pattern: [0.0; 8],
|
||||
line_weight_px: 1.0,
|
||||
snap_pts: vec![],
|
||||
tangent_geoms: vec![],
|
||||
key_vertices: vec![],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn v3(p: Vec3) -> Vector3 {
|
||||
Vector3::new(p.x as f64, p.y as f64, p.z as f64)
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
// DIMCONTINUE command — chain linear/aligned dimensions end-to-end.
|
||||
//
|
||||
// Each new point becomes the second extension line origin of a new dimension,
|
||||
// whose first extension line origin is the second extension line of the previous dim.
|
||||
// The dimension line stays at the same perpendicular offset as the base dimension.
|
||||
//
|
||||
// Constructed from commands.rs after finding the last placed linear/aligned dimension.
|
||||
|
||||
use acadrust::entities::{Dimension, DimensionLinear};
|
||||
use acadrust::types::Vector3;
|
||||
use acadrust::EntityType;
|
||||
use glam::Vec3;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::scene::wire_model::WireModel;
|
||||
|
||||
pub struct DimContinueCommand {
|
||||
/// Fixed first-extension-line origin for the current step (moves each iteration).
|
||||
chain_p1: Vec3,
|
||||
/// Direction along the dimension axis (0.0 = horizontal, PI/2 = vertical).
|
||||
rotation: f64,
|
||||
/// Perpendicular distance from the extension-line axis to the dimension line.
|
||||
/// Preserved from the base dimension.
|
||||
dim_offset: f32,
|
||||
/// Direction of "up" perpendicular to the dim axis (points toward the dim line).
|
||||
perp: Vec3,
|
||||
/// True once we have a base dimension loaded.
|
||||
ready: bool,
|
||||
}
|
||||
|
||||
impl DimContinueCommand {
|
||||
/// No base dim found — will show an error prompt and cancel immediately.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
chain_p1: Vec3::ZERO,
|
||||
rotation: 0.0,
|
||||
dim_offset: 0.0,
|
||||
perp: Vec3::Y,
|
||||
ready: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Build from the last placed dimension.
|
||||
///
|
||||
/// `p1` / `p2` — extension line origins of the base dim.
|
||||
/// `definition_point` — where the dim line was placed (defines perpendicular offset).
|
||||
/// `rotation` — 0.0 = horizontal dim, PI/2 = vertical dim.
|
||||
pub fn from_base(p1: Vec3, p2: Vec3, definition_point: Vec3, rotation: f64) -> Self {
|
||||
// Axis unit vector along the measurement direction.
|
||||
let axis = if rotation.abs() < 0.1 { Vec3::X } else { Vec3::Y };
|
||||
// Perpendicular unit vector toward the dim line.
|
||||
let perp = Vec3::new(-axis.y, axis.x, 0.0);
|
||||
let dim_offset = (definition_point - p1).dot(perp);
|
||||
Self {
|
||||
chain_p1: p2,
|
||||
rotation,
|
||||
dim_offset,
|
||||
perp,
|
||||
ready: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CadCommand for DimContinueCommand {
|
||||
fn name(&self) -> &'static str { "DIMCONTINUE" }
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
if !self.ready {
|
||||
"DIMCONTINUE No base dimension found. Place a dimension first.".into()
|
||||
} else {
|
||||
"DIMCONTINUE Specify a second extension line origin (Enter to exit):".into()
|
||||
}
|
||||
}
|
||||
|
||||
fn on_point(&mut self, pt: Vec3) -> CmdResult {
|
||||
if !self.ready {
|
||||
return CmdResult::Cancel;
|
||||
}
|
||||
let p1 = self.chain_p1;
|
||||
let p2 = pt;
|
||||
|
||||
// Build a new linear dimension.
|
||||
let mut dim = DimensionLinear::new(v3(p1), v3(p2));
|
||||
dim.rotation = self.rotation;
|
||||
|
||||
// Dim-line position: same perpendicular distance as base.
|
||||
let dim_line_pt = p1 + self.perp * self.dim_offset;
|
||||
dim.definition_point = v3(dim_line_pt);
|
||||
dim.base.definition_point = v3(dim_line_pt);
|
||||
dim.base.text_middle_point = v3((dim_line_pt + (p2 + self.perp * self.dim_offset)) * 0.5);
|
||||
dim.base.insertion_point = dim.base.text_middle_point;
|
||||
dim.base.actual_measurement = dim.measurement();
|
||||
|
||||
// Advance chain: next dim's P1 = this dim's P2.
|
||||
self.chain_p1 = p2;
|
||||
|
||||
CmdResult::CommitEntity(EntityType::Dimension(Dimension::Linear(dim)))
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
|
||||
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
|
||||
if !self.ready {
|
||||
return None;
|
||||
}
|
||||
let p1 = self.chain_p1;
|
||||
let dim_line_pt = p1 + self.perp * self.dim_offset;
|
||||
let dim_line_pt2 = pt + self.perp * self.dim_offset;
|
||||
Some(WireModel {
|
||||
name: "dimcont_preview".into(),
|
||||
points: vec![
|
||||
[p1.x, p1.y, p1.z], [dim_line_pt.x, dim_line_pt.y, dim_line_pt.z],
|
||||
[f32::NAN, 0.0, 0.0],
|
||||
[pt.x, pt.y, pt.z], [dim_line_pt2.x, dim_line_pt2.y, dim_line_pt2.z],
|
||||
[f32::NAN, 0.0, 0.0],
|
||||
[dim_line_pt.x, dim_line_pt.y, dim_line_pt.z],
|
||||
[dim_line_pt2.x, dim_line_pt2.y, dim_line_pt2.z],
|
||||
],
|
||||
color: WireModel::CYAN,
|
||||
selected: false,
|
||||
pattern_length: 0.0,
|
||||
pattern: [0.0; 8],
|
||||
line_weight_px: 1.0,
|
||||
snap_pts: vec![],
|
||||
tangent_geoms: vec![],
|
||||
key_vertices: vec![],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn v3(p: Vec3) -> Vector3 {
|
||||
Vector3::new(p.x as f64, p.y as f64, p.z as f64)
|
||||
}
|
||||
@@ -3,12 +3,16 @@
|
||||
pub mod aligned_dim;
|
||||
pub mod angular_dim;
|
||||
pub mod diameter_dim;
|
||||
pub mod dim_baseline;
|
||||
pub mod dim_continue;
|
||||
pub mod leader_cmd;
|
||||
pub mod linear_dim;
|
||||
pub mod mleader_cmd;
|
||||
pub mod mtext;
|
||||
pub mod radius_dim;
|
||||
pub mod table_cmd;
|
||||
pub mod text;
|
||||
pub mod tolerance_cmd;
|
||||
|
||||
use crate::modules::{CadModule, RibbonGroup, RibbonItem};
|
||||
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
// TABLE command — create an empty table entity.
|
||||
//
|
||||
// Workflow:
|
||||
// 1. Text: Enter number of columns (default 3)
|
||||
// 2. Text: Enter number of rows (default 4, includes header row)
|
||||
// 3. Point: Click insertion point
|
||||
//
|
||||
// Creates a Table entity with uniform row height (0.5) and column width (2.0).
|
||||
|
||||
use acadrust::entities::TableBuilder;
|
||||
use acadrust::types::Vector3;
|
||||
use acadrust::EntityType;
|
||||
use glam::Vec3;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::scene::wire_model::WireModel;
|
||||
|
||||
const DEFAULT_COLS: usize = 3;
|
||||
const DEFAULT_ROWS: usize = 4;
|
||||
const COL_WIDTH: f64 = 2.0;
|
||||
const ROW_HEIGHT: f64 = 0.5;
|
||||
|
||||
enum Step {
|
||||
Columns,
|
||||
Rows { cols: usize },
|
||||
Insertion { cols: usize, rows: usize },
|
||||
}
|
||||
|
||||
pub struct TableCommand {
|
||||
step: Step,
|
||||
}
|
||||
|
||||
impl TableCommand {
|
||||
pub fn new() -> Self {
|
||||
Self { step: Step::Columns }
|
||||
}
|
||||
}
|
||||
|
||||
impl CadCommand for TableCommand {
|
||||
fn name(&self) -> &'static str { "TABLE" }
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
match &self.step {
|
||||
Step::Columns => format!("TABLE Enter number of columns [{DEFAULT_COLS}]:"),
|
||||
Step::Rows { cols } => format!("TABLE Enter number of rows (incl. header) [{DEFAULT_ROWS}] ({cols} cols):"),
|
||||
Step::Insertion { cols, rows } => format!("TABLE Specify insertion point [{cols}×{rows}]:"),
|
||||
}
|
||||
}
|
||||
|
||||
fn wants_text_input(&self) -> bool {
|
||||
!matches!(self.step, Step::Insertion { .. })
|
||||
}
|
||||
|
||||
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
|
||||
let t = text.trim();
|
||||
match &self.step {
|
||||
Step::Columns => {
|
||||
let cols = if t.is_empty() {
|
||||
DEFAULT_COLS
|
||||
} else {
|
||||
t.parse::<usize>().ok().filter(|&n| n >= 1)?
|
||||
};
|
||||
self.step = Step::Rows { cols };
|
||||
Some(CmdResult::NeedPoint)
|
||||
}
|
||||
Step::Rows { cols } => {
|
||||
let cols = *cols;
|
||||
let rows = if t.is_empty() {
|
||||
DEFAULT_ROWS
|
||||
} else {
|
||||
t.parse::<usize>().ok().filter(|&n| n >= 1)?
|
||||
};
|
||||
self.step = Step::Insertion { cols, rows };
|
||||
Some(CmdResult::NeedPoint)
|
||||
}
|
||||
Step::Insertion { .. } => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
match &self.step {
|
||||
Step::Columns => {
|
||||
// Accept default.
|
||||
self.step = Step::Rows { cols: DEFAULT_COLS };
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
Step::Rows { cols } => {
|
||||
let cols = *cols;
|
||||
self.step = Step::Insertion { cols, rows: DEFAULT_ROWS };
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
Step::Insertion { .. } => CmdResult::Cancel,
|
||||
}
|
||||
}
|
||||
|
||||
fn on_point(&mut self, pt: Vec3) -> CmdResult {
|
||||
if let Step::Insertion { cols, rows } = self.step {
|
||||
let ins = Vector3::new(pt.x as f64, pt.z as f64, pt.y as f64);
|
||||
let table = TableBuilder::new(rows, cols)
|
||||
.at(ins)
|
||||
.row_height(ROW_HEIGHT)
|
||||
.column_width(COL_WIDTH)
|
||||
.build();
|
||||
CmdResult::CommitAndExit(EntityType::Table(table))
|
||||
} else {
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
}
|
||||
|
||||
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
|
||||
if let Step::Insertion { cols, rows } = self.step {
|
||||
// Preview: outline of the table bounding box.
|
||||
let w = (cols as f32) * COL_WIDTH as f32;
|
||||
let h = (rows as f32) * ROW_HEIGHT as f32;
|
||||
let x = pt.x;
|
||||
let y = pt.y;
|
||||
let z = pt.z;
|
||||
Some(WireModel {
|
||||
name: "table_preview".into(),
|
||||
points: vec![
|
||||
[x, y, z], [x + w, y, z],
|
||||
[x + w, y, z], [x + w, y, z - h],
|
||||
[x + w, y, z - h], [x, y, z - h],
|
||||
[x, y, z - h], [x, y, z],
|
||||
],
|
||||
color: WireModel::CYAN,
|
||||
selected: false,
|
||||
pattern_length: 0.0,
|
||||
pattern: [0.0; 8],
|
||||
line_weight_px: 1.0,
|
||||
snap_pts: vec![],
|
||||
tangent_geoms: vec![],
|
||||
key_vertices: vec![],
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
// TOLERANCE command — place a GD&T (geometric dimensioning & tolerancing) frame.
|
||||
//
|
||||
// Workflow:
|
||||
// 1. Text: Enter tolerance string (e.g. "%%v0.05|A" or plain text)
|
||||
// 2. Point: Click insertion point
|
||||
|
||||
use acadrust::entities::Tolerance;
|
||||
use acadrust::types::Vector3;
|
||||
use acadrust::EntityType;
|
||||
use glam::Vec3;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::scene::wire_model::WireModel;
|
||||
|
||||
enum Step {
|
||||
Text,
|
||||
Insertion { text: String },
|
||||
}
|
||||
|
||||
pub struct ToleranceCommand {
|
||||
step: Step,
|
||||
}
|
||||
|
||||
impl ToleranceCommand {
|
||||
pub fn new() -> Self {
|
||||
Self { step: Step::Text }
|
||||
}
|
||||
}
|
||||
|
||||
impl CadCommand for ToleranceCommand {
|
||||
fn name(&self) -> &'static str { "TOLERANCE" }
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
match &self.step {
|
||||
Step::Text => "TOLERANCE Enter tolerance text:".into(),
|
||||
Step::Insertion { text } => format!("TOLERANCE Specify insertion point [{text}]:"),
|
||||
}
|
||||
}
|
||||
|
||||
fn wants_text_input(&self) -> bool {
|
||||
matches!(self.step, Step::Text)
|
||||
}
|
||||
|
||||
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
|
||||
let t = text.trim().to_string();
|
||||
if t.is_empty() {
|
||||
return Some(CmdResult::Cancel);
|
||||
}
|
||||
self.step = Step::Insertion { text: t };
|
||||
Some(CmdResult::NeedPoint)
|
||||
}
|
||||
|
||||
fn on_point(&mut self, pt: Vec3) -> CmdResult {
|
||||
if let Step::Insertion { text } = &self.step {
|
||||
let ins = Vector3::new(pt.x as f64, pt.z as f64, pt.y as f64);
|
||||
let tol = Tolerance::with_text(ins, text.clone());
|
||||
CmdResult::CommitAndExit(EntityType::Tolerance(tol))
|
||||
} else {
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
|
||||
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
|
||||
if !matches!(self.step, Step::Insertion { .. }) {
|
||||
return None;
|
||||
}
|
||||
let d = 0.15_f32;
|
||||
Some(WireModel {
|
||||
name: "tolerance_preview".into(),
|
||||
points: vec![
|
||||
[pt.x - d, pt.y, pt.z], [pt.x + d, pt.y, pt.z],
|
||||
[f32::NAN, 0.0, 0.0],
|
||||
[pt.x, pt.y, pt.z - d], [pt.x, pt.y, pt.z + d],
|
||||
],
|
||||
color: WireModel::CYAN,
|
||||
selected: false,
|
||||
pattern_length: 0.0,
|
||||
pattern: [0.0; 8],
|
||||
line_weight_px: 1.0,
|
||||
snap_pts: vec![],
|
||||
tangent_geoms: vec![],
|
||||
key_vertices: vec![],
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
// ATTDEF command — define a block attribute (AttributeDefinition entity).
|
||||
//
|
||||
// Workflow (command-line only):
|
||||
// 1. Text: Enter attribute tag (required, no spaces)
|
||||
// 2. Text: Enter attribute prompt (optional — press Enter to use tag)
|
||||
// 3. Text: Enter default value (optional — press Enter for blank)
|
||||
// 4. Point: Click insertion point
|
||||
|
||||
use acadrust::entities::AttributeDefinition;
|
||||
use acadrust::types::Vector3;
|
||||
use acadrust::EntityType;
|
||||
use glam::Vec3;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::scene::wire_model::WireModel;
|
||||
|
||||
enum Step {
|
||||
Tag,
|
||||
Prompt { tag: String },
|
||||
Default { tag: String, prompt: String },
|
||||
Insertion { tag: String, prompt: String, default: String },
|
||||
}
|
||||
|
||||
pub struct AttdefCommand {
|
||||
step: Step,
|
||||
/// Text height in world units.
|
||||
height: f64,
|
||||
}
|
||||
|
||||
impl AttdefCommand {
|
||||
pub fn new() -> Self {
|
||||
Self { step: Step::Tag, height: 0.2 }
|
||||
}
|
||||
}
|
||||
|
||||
impl CadCommand for AttdefCommand {
|
||||
fn name(&self) -> &'static str { "ATTDEF" }
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
match &self.step {
|
||||
Step::Tag => "ATTDEF Enter attribute tag (no spaces):".into(),
|
||||
Step::Prompt { tag } => format!("ATTDEF Enter prompt for '{tag}' (Enter=use tag):"),
|
||||
Step::Default { tag, .. } => format!("ATTDEF Enter default value for '{tag}' (Enter=blank):"),
|
||||
Step::Insertion { tag, .. } => format!("ATTDEF Specify insertion point for '{tag}':"),
|
||||
}
|
||||
}
|
||||
|
||||
fn wants_text_input(&self) -> bool {
|
||||
!matches!(self.step, Step::Insertion { .. })
|
||||
}
|
||||
|
||||
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
|
||||
match &self.step {
|
||||
Step::Tag => {
|
||||
let tag = text.trim().replace(' ', "_");
|
||||
if tag.is_empty() {
|
||||
return Some(CmdResult::NeedPoint);
|
||||
}
|
||||
self.step = Step::Prompt { tag };
|
||||
Some(CmdResult::NeedPoint)
|
||||
}
|
||||
Step::Prompt { tag } => {
|
||||
let tag = tag.clone();
|
||||
let prompt = if text.trim().is_empty() { tag.clone() } else { text.trim().to_string() };
|
||||
self.step = Step::Default { tag, prompt };
|
||||
Some(CmdResult::NeedPoint)
|
||||
}
|
||||
Step::Default { tag, prompt } => {
|
||||
let tag = tag.clone();
|
||||
let prompt = prompt.clone();
|
||||
let default = text.trim().to_string();
|
||||
self.step = Step::Insertion { tag, prompt, default };
|
||||
Some(CmdResult::NeedPoint)
|
||||
}
|
||||
Step::Insertion { .. } => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn on_point(&mut self, pt: Vec3) -> CmdResult {
|
||||
if let Step::Insertion { tag, prompt, default } = &self.step {
|
||||
let mut attdef = AttributeDefinition {
|
||||
tag: tag.clone(),
|
||||
prompt: prompt.clone(),
|
||||
default_value: default.clone(),
|
||||
insertion_point: Vector3::new(pt.x as f64, pt.z as f64, pt.y as f64),
|
||||
height: self.height,
|
||||
..Default::default()
|
||||
};
|
||||
attdef.common.layer = "0".into();
|
||||
CmdResult::CommitAndExit(EntityType::AttributeDefinition(attdef))
|
||||
} else {
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
match &self.step {
|
||||
Step::Tag => CmdResult::Cancel,
|
||||
// Treat Enter as empty text input for prompt/default steps.
|
||||
Step::Prompt { tag } => {
|
||||
let tag = tag.clone();
|
||||
self.step = Step::Default { tag: tag.clone(), prompt: tag };
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
Step::Default { tag, prompt } => {
|
||||
let (tag, prompt) = (tag.clone(), prompt.clone());
|
||||
self.step = Step::Insertion { tag, prompt, default: String::new() };
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
Step::Insertion { .. } => CmdResult::Cancel,
|
||||
}
|
||||
}
|
||||
|
||||
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
|
||||
if !matches!(self.step, Step::Insertion { .. }) {
|
||||
return None;
|
||||
}
|
||||
// Show a small cross at the insertion point.
|
||||
let d = 0.15_f32;
|
||||
Some(WireModel {
|
||||
name: "attdef_preview".into(),
|
||||
points: vec![
|
||||
[pt.x - d, pt.y, pt.z], [pt.x + d, pt.y, pt.z],
|
||||
[f32::NAN, 0.0, 0.0],
|
||||
[pt.x, pt.y, pt.z - d], [pt.x, pt.y, pt.z + d],
|
||||
],
|
||||
color: WireModel::CYAN,
|
||||
selected: false,
|
||||
pattern_length: 0.0,
|
||||
pattern: [0.0; 8],
|
||||
line_weight_px: 1.0,
|
||||
snap_pts: vec![],
|
||||
tangent_geoms: vec![],
|
||||
key_vertices: vec![],
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod arc;
|
||||
pub mod attdef;
|
||||
pub mod circle;
|
||||
pub mod donut;
|
||||
pub mod ellipse;
|
||||
|
||||
@@ -1,16 +1,22 @@
|
||||
// Polyline tool — ribbon definition + interactive command.
|
||||
//
|
||||
// Command: PLINE (PL)
|
||||
// Each click adds a vertex. The rubber-band shows the segment being drawn.
|
||||
// Each click adds a vertex.
|
||||
// Type A = switch to Arc segment mode.
|
||||
// Type L = switch back to Line segment mode.
|
||||
// Enter / C = close and commit. Escape = commit as-is (if ≥2 vertices).
|
||||
//
|
||||
// Arc mode: arcs are tangent-continuous with the preceding segment.
|
||||
// Bulge is stored per vertex (segment i→i+1); positive = CCW, negative = CW.
|
||||
|
||||
use acadrust::entities::LwVertex;
|
||||
use acadrust::types::Vector2;
|
||||
use acadrust::{EntityType, LwPolyline};
|
||||
use glam::{Vec2, Vec3};
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::modules::{IconKind, ModuleEvent, ToolDef};
|
||||
use crate::scene::wire_model::WireModel;
|
||||
use glam::Vec3;
|
||||
|
||||
// ── Ribbon definition ──────────────────────────────────────────────────────
|
||||
|
||||
@@ -23,16 +29,32 @@ pub fn tool() -> ToolDef {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Segment mode ───────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
enum SegMode {
|
||||
Line,
|
||||
Arc,
|
||||
}
|
||||
|
||||
// ── Command implementation ─────────────────────────────────────────────────
|
||||
|
||||
pub struct PlineCommand {
|
||||
vertices: Vec<Vec3>,
|
||||
/// Bulge for segment i → i+1 (one entry per vertex; last entry unused on commit).
|
||||
bulges: Vec<f64>,
|
||||
mode: SegMode,
|
||||
/// Unit direction of the last committed segment (for arc tangent continuity).
|
||||
last_tangent: Option<Vec2>,
|
||||
}
|
||||
|
||||
impl PlineCommand {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
vertices: Vec::new(),
|
||||
bulges: Vec::new(),
|
||||
mode: SegMode::Line,
|
||||
last_tangent: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +65,12 @@ impl PlineCommand {
|
||||
let lw_verts: Vec<LwVertex> = self
|
||||
.vertices
|
||||
.iter()
|
||||
.map(|v| LwVertex::new(acadrust::types::Vector2::new(v.x as f64, v.y as f64)))
|
||||
.enumerate()
|
||||
.map(|(i, v)| {
|
||||
let mut lv = LwVertex::new(Vector2::new(v.x as f64, v.y as f64));
|
||||
lv.bulge = self.bulges.get(i).copied().unwrap_or(0.0);
|
||||
lv
|
||||
})
|
||||
.collect();
|
||||
let pline = LwPolyline {
|
||||
vertices: lw_verts,
|
||||
@@ -54,24 +81,151 @@ impl PlineCommand {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Arc geometry helpers ───────────────────────────────────────────────────
|
||||
|
||||
/// Compute the bulge for the arc from `a` to `b` that is tangent to `tangent` at `a`.
|
||||
/// Returns 0.0 if the points are coincident or the tangent is parallel to the chord.
|
||||
fn compute_bulge(a: Vec2, tangent: Vec2, b: Vec2) -> f64 {
|
||||
let d = b - a;
|
||||
let len_sq = d.length_squared();
|
||||
if len_sq < 1e-10 {
|
||||
return 0.0;
|
||||
}
|
||||
// Perpendicular to tangent (CCW) — this is the direction to the arc center.
|
||||
let perp = Vec2::new(-tangent.y, tangent.x);
|
||||
let dot = d.dot(perp);
|
||||
if dot.abs() < 1e-10 {
|
||||
// Tangent is perpendicular to chord → straight line (bulge = 0).
|
||||
return 0.0;
|
||||
}
|
||||
// t = distance from a to center along perp.
|
||||
let t = len_sq / (2.0 * dot);
|
||||
let center = a + perp * t;
|
||||
|
||||
// Arc angle from start to end (signed).
|
||||
let start_angle = (a - center).y.atan2((a - center).x);
|
||||
let end_angle = (b - center).y.atan2((b - center).x);
|
||||
let mut arc_angle = end_angle - start_angle;
|
||||
|
||||
if t > 0.0 {
|
||||
// CCW arc: ensure arc_angle is in (0, 2π].
|
||||
if arc_angle <= 0.0 {
|
||||
arc_angle += std::f32::consts::TAU;
|
||||
}
|
||||
} else {
|
||||
// CW arc: ensure arc_angle is in [-2π, 0).
|
||||
if arc_angle >= 0.0 {
|
||||
arc_angle -= std::f32::consts::TAU;
|
||||
}
|
||||
}
|
||||
(arc_angle as f64 / 4.0).tan()
|
||||
}
|
||||
|
||||
/// Update `tangent` after an arc segment described by `bulge` from `a` to `b`.
|
||||
fn update_tangent_after_arc(tangent: &mut Option<Vec2>, bulge: f64) {
|
||||
let Some(t) = *tangent else {
|
||||
return;
|
||||
};
|
||||
// The arc sweeps theta = 4*atan(bulge) radians, so the exit tangent is
|
||||
// the entry tangent rotated by that angle.
|
||||
let theta = 4.0 * (bulge as f32).atan();
|
||||
let (sin_t, cos_t) = theta.sin_cos();
|
||||
*tangent = Some(Vec2::new(t.x * cos_t - t.y * sin_t, t.x * sin_t + t.y * cos_t).normalize_or_zero());
|
||||
}
|
||||
|
||||
/// Sample a circular arc defined by bulge into `n` line-segment points.
|
||||
/// Returns the sampled [x, y, z] points (uses `z` from `a`).
|
||||
fn arc_sample_points(a: Vec3, bulge: f64, b: Vec3, n: usize) -> Vec<[f32; 3]> {
|
||||
let ax = a.x as f64;
|
||||
let ay = a.y as f64;
|
||||
let bx = b.x as f64;
|
||||
let by = b.y as f64;
|
||||
|
||||
let dx = bx - ax;
|
||||
let dy = by - ay;
|
||||
let chord_len = (dx * dx + dy * dy).sqrt();
|
||||
if chord_len < 1e-10 || bulge.abs() < 1e-10 {
|
||||
return vec![[a.x, a.y, a.z], [b.x, b.y, b.z]];
|
||||
}
|
||||
|
||||
// Center of the arc.
|
||||
// Formula: center = midpoint + offset * perp_unit
|
||||
// where offset = chord_len * (1 - bulge²) / (4 * bulge).
|
||||
let b2 = bulge * bulge;
|
||||
let offset = chord_len * (1.0 - b2) / (4.0 * bulge);
|
||||
let perp_x = -dy / chord_len;
|
||||
let perp_y = dx / chord_len;
|
||||
let mx = (ax + bx) / 2.0;
|
||||
let my = (ay + by) / 2.0;
|
||||
let cx = mx + offset * perp_x;
|
||||
let cy = my + offset * perp_y;
|
||||
|
||||
let r = ((ax - cx) * (ax - cx) + (ay - cy) * (ay - cy)).sqrt();
|
||||
let start_angle = (ay - cy).atan2(ax - cx);
|
||||
// Total arc angle (signed).
|
||||
let theta = 4.0 * bulge.atan();
|
||||
|
||||
let mut pts = Vec::with_capacity(n + 1);
|
||||
for i in 0..=n {
|
||||
let t = i as f64 / n as f64;
|
||||
let angle = start_angle + t * theta;
|
||||
pts.push([(cx + r * angle.cos()) as f32, (cy + r * angle.sin()) as f32, a.z]);
|
||||
}
|
||||
pts
|
||||
}
|
||||
|
||||
// ── CadCommand impl ────────────────────────────────────────────────────────
|
||||
|
||||
impl CadCommand for PlineCommand {
|
||||
fn name(&self) -> &'static str {
|
||||
"PLINE"
|
||||
}
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
let mode_tag = match self.mode {
|
||||
SegMode::Line => "Line",
|
||||
SegMode::Arc => "Arc",
|
||||
};
|
||||
if self.vertices.is_empty() {
|
||||
"PLINE Specify start point:".into()
|
||||
} else {
|
||||
format!(
|
||||
"PLINE Specify next point [{}pts | Enter=done C=close Esc=cancel]:",
|
||||
"PLINE [{mode_tag}] Next pt [{}pts | A=arc L=line C=close Enter=done]:",
|
||||
self.vertices.len()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn on_point(&mut self, pt: Vec3) -> CmdResult {
|
||||
if !self.vertices.is_empty() {
|
||||
let last = *self.vertices.last().unwrap();
|
||||
let last_idx = self.vertices.len() - 1;
|
||||
|
||||
let bulge = match self.mode {
|
||||
SegMode::Line => {
|
||||
let d = Vec2::new(pt.x - last.x, pt.y - last.y);
|
||||
if d.length_squared() > 1e-10 {
|
||||
self.last_tangent = Some(d.normalize());
|
||||
}
|
||||
0.0
|
||||
}
|
||||
SegMode::Arc => {
|
||||
let a = Vec2::new(last.x, last.y);
|
||||
let b = Vec2::new(pt.x, pt.y);
|
||||
let tangent = self.last_tangent.unwrap_or_else(|| {
|
||||
// No previous tangent: default to pointing right (arbitrary).
|
||||
Vec2::new(1.0, 0.0)
|
||||
});
|
||||
let bulge = compute_bulge(a, tangent, b);
|
||||
update_tangent_after_arc(&mut self.last_tangent, bulge);
|
||||
bulge
|
||||
}
|
||||
};
|
||||
self.bulges[last_idx] = bulge;
|
||||
}
|
||||
|
||||
self.vertices.push(pt);
|
||||
self.bulges.push(0.0);
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
|
||||
@@ -89,18 +243,71 @@ impl CadCommand for PlineCommand {
|
||||
}
|
||||
}
|
||||
|
||||
fn wants_text_input(&self) -> bool {
|
||||
// Accept A / L / C once we have at least the first point.
|
||||
!self.vertices.is_empty()
|
||||
}
|
||||
|
||||
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
|
||||
match text.trim().to_uppercase().as_str() {
|
||||
"A" | "ARC" => {
|
||||
self.mode = SegMode::Arc;
|
||||
Some(CmdResult::NeedPoint)
|
||||
}
|
||||
"L" | "LINE" => {
|
||||
self.mode = SegMode::Line;
|
||||
Some(CmdResult::NeedPoint)
|
||||
}
|
||||
"C" | "CLOSE" => match self.build_entity(true) {
|
||||
Some(e) => Some(CmdResult::CommitAndExit(e)),
|
||||
None => Some(CmdResult::Cancel),
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
|
||||
if self.vertices.is_empty() {
|
||||
return None;
|
||||
}
|
||||
// Show all committed vertices + cursor as a continuous preview.
|
||||
let mut pts: Vec<[f32; 3]> = self.vertices.iter().map(|v| [v.x, v.y, v.z]).collect();
|
||||
pts.push([pt.x, pt.y, pt.z]);
|
||||
Some(WireModel::solid(
|
||||
"rubber_band".into(),
|
||||
pts,
|
||||
WireModel::CYAN,
|
||||
false,
|
||||
))
|
||||
let last = *self.vertices.last().unwrap();
|
||||
|
||||
// Build committed segments first.
|
||||
let mut pts: Vec<[f32; 3]> = Vec::new();
|
||||
|
||||
// Re-emit all committed vertices + arc samples between them.
|
||||
for i in 0..self.vertices.len() {
|
||||
let v = self.vertices[i];
|
||||
if i == 0 {
|
||||
pts.push([v.x, v.y, v.z]);
|
||||
} else {
|
||||
let prev = self.vertices[i - 1];
|
||||
let b = self.bulges.get(i - 1).copied().unwrap_or(0.0);
|
||||
if b.abs() > 1e-6 {
|
||||
// Arc segment: sample it.
|
||||
let arc_pts = arc_sample_points(prev, b, v, 16);
|
||||
pts.extend_from_slice(&arc_pts[1..]); // skip first (already added)
|
||||
} else {
|
||||
pts.push([v.x, v.y, v.z]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Rubber-band to cursor.
|
||||
match self.mode {
|
||||
SegMode::Line => {
|
||||
pts.push([pt.x, pt.y, pt.z]);
|
||||
}
|
||||
SegMode::Arc => {
|
||||
let a = Vec2::new(last.x, last.y);
|
||||
let b = Vec2::new(pt.x, pt.y);
|
||||
let tangent = self.last_tangent.unwrap_or(Vec2::new(1.0, 0.0));
|
||||
let bulge = compute_bulge(a, tangent, b);
|
||||
let arc_pts = arc_sample_points(last, bulge, pt, 16);
|
||||
pts.extend_from_slice(&arc_pts[1..]);
|
||||
}
|
||||
}
|
||||
|
||||
Some(WireModel::solid("rubber_band".into(), pts, WireModel::CYAN, false))
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,6 @@ use acadrust::{EntityType, Handle};
|
||||
use glam::Vec3;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::scene::wire_model::WireModel;
|
||||
|
||||
// ── DIVIDE ─────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
// With 1 pair: pure translation (src1 → dst1)
|
||||
// With 2 pairs: translate + rotate (+ optional uniform scale to fit)
|
||||
|
||||
use glam::{Mat4, Vec3};
|
||||
use glam::Vec3;
|
||||
use acadrust::Handle;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult, EntityTransform};
|
||||
|
||||
@@ -7,8 +7,6 @@
|
||||
//
|
||||
// BREAK @ (at-sign as second point) → Break at a single point (splits without gap).
|
||||
|
||||
use std::f64::consts::TAU;
|
||||
|
||||
use acadrust::entities::{Arc as ArcEnt, Line as LineEnt, LwPolyline};
|
||||
use acadrust::types::Vector3;
|
||||
use acadrust::{EntityType, Handle};
|
||||
@@ -20,6 +18,7 @@ use crate::scene::wire_model::WireModel;
|
||||
|
||||
// ── Ribbon definition ──────────────────────────────────────────────────────
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn tool() -> ToolDef {
|
||||
ToolDef {
|
||||
id: "BREAK",
|
||||
@@ -285,3 +284,50 @@ impl CadCommand for BreakInteractiveCommand {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
// ── BREAKATPOINT (BAP) — split at a single point, no gap ──────────────────
|
||||
|
||||
pub struct BreakAtPointCommand {
|
||||
target: Option<Handle>,
|
||||
}
|
||||
|
||||
impl BreakAtPointCommand {
|
||||
pub fn new() -> Self {
|
||||
Self { target: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl CadCommand for BreakAtPointCommand {
|
||||
fn name(&self) -> &'static str { "BREAKATPOINT" }
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
if self.target.is_none() {
|
||||
"BREAKATPOINT Select object:".into()
|
||||
} else {
|
||||
"BREAKATPOINT Specify break point:".into()
|
||||
}
|
||||
}
|
||||
|
||||
fn needs_entity_pick(&self) -> bool {
|
||||
self.target.is_none()
|
||||
}
|
||||
|
||||
fn on_entity_pick(&mut self, handle: Handle, _pt: Vec3) -> CmdResult {
|
||||
if handle.is_null() {
|
||||
return CmdResult::NeedPoint;
|
||||
}
|
||||
self.target = Some(handle);
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
|
||||
fn on_point(&mut self, pt: Vec3) -> CmdResult {
|
||||
match self.target {
|
||||
Some(handle) => CmdResult::BreakEntity { handle, p1: pt, p2: pt },
|
||||
None => CmdResult::Cancel,
|
||||
}
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@
|
||||
//
|
||||
// Workflow: select objects then press Enter to join.
|
||||
|
||||
use acadrust::entities::{Arc as ArcEnt, Line as LineEnt};
|
||||
use acadrust::types::Vector3;
|
||||
use acadrust::{EntityType, Handle};
|
||||
use glam::Vec3;
|
||||
@@ -162,7 +161,7 @@ fn try_join_arcs(arcs: &[&(Handle, &EntityType)]) -> Option<(Vec<Handle>, Vec<En
|
||||
intervals.sort_by(|a, b| a[0].partial_cmp(&b[0]).unwrap());
|
||||
|
||||
// Try to merge into one contiguous arc
|
||||
let mut merged_start = intervals[0][0];
|
||||
let merged_start = intervals[0][0];
|
||||
let mut merged_end = intervals[0][1];
|
||||
for &[s, e] in &intervals[1..] {
|
||||
let span = ((e - merged_end) + 360.0) % 360.0;
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
use std::f64::consts::TAU;
|
||||
|
||||
use acadrust::entities::{Arc as ArcEnt, Line as LineEnt};
|
||||
use acadrust::entities::{Arc as ArcEnt, Line as LineEnt, Ray as RayEnt, XLine as XLineEnt};
|
||||
use acadrust::types::Vector3;
|
||||
use acadrust::{EntityType, Handle};
|
||||
use glam::Vec3;
|
||||
@@ -146,6 +146,11 @@ fn cc_angles(cx1: f64, cy1: f64, r1: f64, cx2: f64, cy2: f64, r2: f64) -> Vec<f6
|
||||
|
||||
// ── Boundary geometry ─────────────────────────────────────────────────────
|
||||
|
||||
/// Virtual extent used to represent infinite ends of Ray / XLine.
|
||||
const TRIM_EXTENT: f64 = 1_000_000.0;
|
||||
/// If a trim interval endpoint is beyond this threshold it is treated as "infinite".
|
||||
const INF_T: f64 = 0.9999;
|
||||
|
||||
enum Geo {
|
||||
Line {
|
||||
handle: Handle,
|
||||
@@ -166,6 +171,22 @@ enum Geo {
|
||||
cy: f64,
|
||||
r: f64,
|
||||
},
|
||||
/// Semi-infinite line from base in +direction.
|
||||
Ray {
|
||||
handle: Handle,
|
||||
bx: f64,
|
||||
by: f64,
|
||||
dx: f64,
|
||||
dy: f64,
|
||||
},
|
||||
/// Fully-infinite line through base along direction.
|
||||
InfLine {
|
||||
handle: Handle,
|
||||
bx: f64,
|
||||
by: f64,
|
||||
dx: f64,
|
||||
dy: f64,
|
||||
},
|
||||
}
|
||||
|
||||
fn build_geos(entities: &[EntityType]) -> Vec<Geo> {
|
||||
@@ -193,6 +214,20 @@ fn build_geos(entities: &[EntityType]) -> Vec<Geo> {
|
||||
cy: c.center.y,
|
||||
r: c.radius,
|
||||
}),
|
||||
EntityType::Ray(r) => Some(Geo::Ray {
|
||||
handle: h,
|
||||
bx: r.base_point.x,
|
||||
by: r.base_point.y,
|
||||
dx: r.direction.x,
|
||||
dy: r.direction.y,
|
||||
}),
|
||||
EntityType::XLine(x) => Some(Geo::InfLine {
|
||||
handle: h,
|
||||
bx: x.base_point.x,
|
||||
by: x.base_point.y,
|
||||
dx: x.direction.x,
|
||||
dy: x.direction.y,
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
@@ -250,6 +285,24 @@ fn line_seg_ts(ax: f64, ay: f64, bx: f64, by: f64, target: Handle, geos: &[Geo])
|
||||
}
|
||||
}
|
||||
}
|
||||
Geo::Ray { handle, bx: rbx, by: rby, dx: rdx, dy: rdy } => {
|
||||
if *handle == target { continue; }
|
||||
if let Some((t, u)) = ll(ax, ay, dx, dy, *rbx, *rby, *rdx, *rdy) {
|
||||
// Ray: u >= 0 (semi-infinite)
|
||||
if u >= -1e-9 && (-1e-9..=1.0 + 1e-9).contains(&t) {
|
||||
ts.push(t.clamp(0.0, 1.0));
|
||||
}
|
||||
}
|
||||
}
|
||||
Geo::InfLine { handle, bx: ibx, by: iby, dx: idx, dy: idy } => {
|
||||
if *handle == target { continue; }
|
||||
if let Some((t, _u)) = ll(ax, ay, dx, dy, *ibx, *iby, *idx, *idy) {
|
||||
// XLine: any u accepted
|
||||
if (-1e-9..=1.0 + 1e-9).contains(&t) {
|
||||
ts.push(t.clamp(0.0, 1.0));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ts.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
||||
@@ -274,11 +327,11 @@ fn arc_seg_ts(
|
||||
if *handle == target {
|
||||
continue;
|
||||
}
|
||||
let (dx, dy) = (p2[0] - p1[0], p2[1] - p1[1]);
|
||||
lc(p1[0], p1[1], dx, dy, cx, cy, r)
|
||||
let (ldx, ldy) = (p2[0] - p1[0], p2[1] - p1[1]);
|
||||
lc(p1[0], p1[1], ldx, ldy, cx, cy, r)
|
||||
.into_iter()
|
||||
.filter(|&u| (-1e-9..=1.0 + 1e-9).contains(&u))
|
||||
.map(|u| (p1[1] + u * dy - cy).atan2(p1[0] + u * dx - cx))
|
||||
.map(|u| (p1[1] + u * ldy - cy).atan2(p1[0] + u * ldx - cx))
|
||||
.collect()
|
||||
}
|
||||
Geo::Arc {
|
||||
@@ -308,6 +361,23 @@ fn arc_seg_ts(
|
||||
}
|
||||
cc_angles(cx, cy, r, *cx2, *cy2, *r2)
|
||||
}
|
||||
Geo::Ray { handle, bx: rbx, by: rby, dx: rdx, dy: rdy } => {
|
||||
if *handle == target { continue; }
|
||||
// Intersect arc circle with the Ray direction
|
||||
lc(*rbx, *rby, *rdx, *rdy, cx, cy, r)
|
||||
.into_iter()
|
||||
.filter(|&u| u >= -1e-9) // Ray: u >= 0
|
||||
.map(|u| (rby + u * rdy - cy).atan2(rbx + u * rdx - cx))
|
||||
.collect()
|
||||
}
|
||||
Geo::InfLine { handle, bx: ibx, by: iby, dx: idx, dy: idy } => {
|
||||
if *handle == target { continue; }
|
||||
// XLine: any u accepted
|
||||
lc(*ibx, *iby, *idx, *idy, cx, cy, r)
|
||||
.into_iter()
|
||||
.map(|u| (iby + u * idy - cy).atan2(ibx + u * idx - cx))
|
||||
.collect()
|
||||
}
|
||||
};
|
||||
for a in angles {
|
||||
if in_arc(a, a0, a1) {
|
||||
@@ -473,6 +543,22 @@ fn extend_line(orig: &LineEnt, t_click: f64, geos: &[Geo]) -> Option<EntityType>
|
||||
}
|
||||
}
|
||||
}
|
||||
Geo::Ray { handle, bx: rbx, by: rby, dx: rdx, dy: rdy } => {
|
||||
if *handle == target { continue; }
|
||||
if let Some((t, u)) = ll(ax, ay, dx, dy, *rbx, *rby, *rdx, *rdy) {
|
||||
if u >= -1e-9 { // only forward along the Ray
|
||||
if extend_end && t > 1.0 + 1e-6 && t < best_t { best_t = t; }
|
||||
if !extend_end && t < -1e-6 && t > best_t { best_t = t; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Geo::InfLine { handle, bx: ibx, by: iby, dx: idx, dy: idy } => {
|
||||
if *handle == target { continue; }
|
||||
if let Some((t, _u)) = ll(ax, ay, dx, dy, *ibx, *iby, *idx, *idy) {
|
||||
if extend_end && t > 1.0 + 1e-6 && t < best_t { best_t = t; }
|
||||
if !extend_end && t < -1e-6 && t > best_t { best_t = t; }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -491,6 +577,113 @@ fn extend_line(orig: &LineEnt, t_click: f64, geos: &[Geo]) -> Option<EntityType>
|
||||
Some(EntityType::Line(line))
|
||||
}
|
||||
|
||||
/// Trim a Ray entity.
|
||||
/// Virtual t ∈ [0,1]: t=0 → base_point, t=1 → base + TRIM_EXTENT * dir.
|
||||
/// Surviving pieces become Lines (finite) or Rays (still semi-infinite).
|
||||
fn trim_ray(orig: &RayEnt, ts: &[f64], t_click: f64) -> Vec<EntityType> {
|
||||
let bx = orig.base_point.x;
|
||||
let by = orig.base_point.y;
|
||||
let bz = orig.base_point.z;
|
||||
let dx = orig.direction.x;
|
||||
let dy = orig.direction.y;
|
||||
let dz = orig.direction.z;
|
||||
let pt = |t: f64| [bx + t * dx * TRIM_EXTENT, by + t * dy * TRIM_EXTENT, bz + t * dz * TRIM_EXTENT];
|
||||
|
||||
trim_intervals(ts, t_click)
|
||||
.into_iter()
|
||||
.filter_map(|(ta, tb)| {
|
||||
let pa = pt(ta);
|
||||
let pb = pt(tb);
|
||||
if (pb[0] - pa[0]).hypot(pb[1] - pa[1]) < 1e-6 { return None; }
|
||||
|
||||
if tb > INF_T {
|
||||
// Still extends to infinity → remains a Ray with new base
|
||||
let r = RayEnt::new(
|
||||
Vector3::new(pa[0], pa[1], pa[2]),
|
||||
Vector3::new(dx, dy, dz),
|
||||
);
|
||||
let mut r = r;
|
||||
r.common = orig.common.clone();
|
||||
r.common.handle = Handle::NULL;
|
||||
Some(EntityType::Ray(r))
|
||||
} else {
|
||||
// Finite segment → Line
|
||||
let mut l = LineEnt { common: orig.common.clone(), ..LineEnt::new() };
|
||||
l.common.handle = Handle::NULL;
|
||||
l.start = Vector3::new(pa[0], pa[1], pa[2]);
|
||||
l.end = Vector3::new(pb[0], pb[1], pb[2]);
|
||||
Some(EntityType::Line(l))
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Trim an XLine entity.
|
||||
/// Virtual t ∈ [0,1]: t=0 → base - dir*TRIM_EXTENT, t=0.5 → base, t=1 → base + dir*TRIM_EXTENT.
|
||||
/// Surviving pieces become Lines (finite), Rays (one infinite end), or the original XLine (both ends).
|
||||
fn trim_xline(orig: &XLineEnt, ts: &[f64], t_click: f64) -> Vec<EntityType> {
|
||||
let bx = orig.base_point.x;
|
||||
let by = orig.base_point.y;
|
||||
let bz = orig.base_point.z;
|
||||
let dx = orig.direction.x;
|
||||
let dy = orig.direction.y;
|
||||
let dz = orig.direction.z;
|
||||
// Point at virtual t: scale factor s = 2t - 1 ∈ [-1, +1]
|
||||
let pt = |t: f64| {
|
||||
let s = 2.0 * t - 1.0;
|
||||
[bx + s * dx * TRIM_EXTENT, by + s * dy * TRIM_EXTENT, bz + s * dz * TRIM_EXTENT]
|
||||
};
|
||||
|
||||
trim_intervals(ts, t_click)
|
||||
.into_iter()
|
||||
.filter_map(|(ta, tb)| {
|
||||
let pa = pt(ta);
|
||||
let pb = pt(tb);
|
||||
let ext_neg = ta < 1.0 - INF_T; // extends toward -infinity
|
||||
let ext_pos = tb > INF_T; // extends toward +infinity
|
||||
|
||||
match (ext_neg, ext_pos) {
|
||||
(true, true) => {
|
||||
// Whole XLine survived (shouldn't happen after a real trim)
|
||||
let mut x = orig.clone();
|
||||
x.common.handle = Handle::NULL;
|
||||
Some(EntityType::XLine(x))
|
||||
}
|
||||
(true, false) => {
|
||||
// Extends toward -infinity: Ray at pb pointing in -dir
|
||||
let r = RayEnt::new(
|
||||
Vector3::new(pb[0], pb[1], pb[2]),
|
||||
Vector3::new(-dx, -dy, -dz),
|
||||
);
|
||||
let mut r = r;
|
||||
r.common = orig.common.clone();
|
||||
r.common.handle = Handle::NULL;
|
||||
Some(EntityType::Ray(r))
|
||||
}
|
||||
(false, true) => {
|
||||
// Extends toward +infinity: Ray at pa pointing in +dir
|
||||
let r = RayEnt::new(
|
||||
Vector3::new(pa[0], pa[1], pa[2]),
|
||||
Vector3::new(dx, dy, dz),
|
||||
);
|
||||
let mut r = r;
|
||||
r.common = orig.common.clone();
|
||||
r.common.handle = Handle::NULL;
|
||||
Some(EntityType::Ray(r))
|
||||
}
|
||||
(false, false) => {
|
||||
// Finite segment
|
||||
let mut l = LineEnt { common: orig.common.clone(), ..LineEnt::new() };
|
||||
l.common.handle = Handle::NULL;
|
||||
l.start = Vector3::new(pa[0], pa[1], pa[2]);
|
||||
l.end = Vector3::new(pb[0], pb[1], pb[2]);
|
||||
Some(EntityType::Line(l))
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
// ── Point-generation helpers ──────────────────────────────────────────────
|
||||
|
||||
const DIM_RED: [f32; 4] = [1.0, 0.3, 0.3, 0.6];
|
||||
@@ -535,6 +728,16 @@ fn entity_pts(e: &EntityType) -> Vec<[f32; 3]> {
|
||||
a.end_angle.to_radians(),
|
||||
a.center.y,
|
||||
),
|
||||
// For preview, show a 20-unit section of semi-infinite results
|
||||
EntityType::Ray(r) => {
|
||||
let bx = r.base_point.x;
|
||||
let by = r.base_point.y;
|
||||
let bz = r.base_point.z;
|
||||
let far_x = bx + r.direction.x * 20.0;
|
||||
let far_y = by + r.direction.y * 20.0;
|
||||
let far_z = bz + r.direction.z * 20.0;
|
||||
vec![[bx as f32, bz as f32, by as f32], [far_x as f32, far_z as f32, far_y as f32]]
|
||||
}
|
||||
_ => vec![],
|
||||
}
|
||||
}
|
||||
@@ -611,6 +814,38 @@ impl CadCommand for TrimCommand {
|
||||
let t_click = arc_t(click_angle, a0, a1);
|
||||
Some(trim_arc(a, &ts, t_click))
|
||||
}
|
||||
Some(EntityType::Ray(r)) => {
|
||||
// Virtual segment: base → base + dir * TRIM_EXTENT (t ∈ [0,1])
|
||||
let bx = r.base_point.x;
|
||||
let by = r.base_point.y;
|
||||
let ex = bx + r.direction.x * TRIM_EXTENT;
|
||||
let ey = by + r.direction.y * TRIM_EXTENT;
|
||||
let ts = line_seg_ts(bx, by, ex, ey, handle, &self.geos);
|
||||
if ts.is_empty() { return CmdResult::NeedPoint; }
|
||||
let dx = r.direction.x * TRIM_EXTENT;
|
||||
let dy = r.direction.y * TRIM_EXTENT;
|
||||
let len2 = dx * dx + dy * dy;
|
||||
let t_click = if len2 > 1e-12 {
|
||||
((pt.x as f64 - bx) * dx + (pt.y as f64 - by) * dy) / len2
|
||||
} else { 0.5 };
|
||||
Some(trim_ray(r, &ts, t_click))
|
||||
}
|
||||
Some(EntityType::XLine(x)) => {
|
||||
// Virtual segment: base - dir*TRIM_EXTENT → base + dir*TRIM_EXTENT
|
||||
let bx = x.base_point.x - x.direction.x * TRIM_EXTENT;
|
||||
let by = x.base_point.y - x.direction.y * TRIM_EXTENT;
|
||||
let ex = x.base_point.x + x.direction.x * TRIM_EXTENT;
|
||||
let ey = x.base_point.y + x.direction.y * TRIM_EXTENT;
|
||||
let ts = line_seg_ts(bx, by, ex, ey, handle, &self.geos);
|
||||
if ts.is_empty() { return CmdResult::NeedPoint; }
|
||||
let dx = ex - bx;
|
||||
let dy = ey - by;
|
||||
let len2 = dx * dx + dy * dy;
|
||||
let t_click = if len2 > 1e-12 {
|
||||
((pt.x as f64 - bx) * dx + (pt.y as f64 - by) * dy) / len2
|
||||
} else { 0.5 };
|
||||
Some(trim_xline(x, &ts, t_click))
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
|
||||
@@ -645,6 +880,8 @@ impl CadCommand for TrimCommand {
|
||||
match e {
|
||||
EntityType::Line(l) => l.common.handle = h,
|
||||
EntityType::Arc(a) => a.common.handle = h,
|
||||
EntityType::Ray(r) => r.common.handle = h,
|
||||
EntityType::XLine(x) => x.common.handle = h,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -721,6 +958,58 @@ impl CadCommand for TrimCommand {
|
||||
}
|
||||
out
|
||||
}
|
||||
Some(EntityType::Ray(r)) => {
|
||||
let bx = r.base_point.x;
|
||||
let by = r.base_point.y;
|
||||
let ex = bx + r.direction.x * TRIM_EXTENT;
|
||||
let ey = by + r.direction.y * TRIM_EXTENT;
|
||||
let ts = line_seg_ts(bx, by, ex, ey, handle, &self.geos);
|
||||
if ts.is_empty() { return vec![]; }
|
||||
let dx = r.direction.x * TRIM_EXTENT;
|
||||
let dy = r.direction.y * TRIM_EXTENT;
|
||||
let len2 = dx * dx + dy * dy;
|
||||
let t_click = if len2 > 1e-12 {
|
||||
((pt.x as f64 - bx) * dx + (pt.y as f64 - by) * dy) / len2
|
||||
} else { 0.5 };
|
||||
let survivors = trim_ray(r, &ts, t_click);
|
||||
// Show a finite preview section (20 units) for the original ray
|
||||
let far = [(bx + r.direction.x * 20.0) as f32, (by + r.direction.y * 20.0) as f32, r.base_point.z as f32];
|
||||
let base = [bx as f32, by as f32, r.base_point.z as f32];
|
||||
let removed = WireModel::solid("trim_rm".into(), vec![base, far], DIM_RED, false);
|
||||
let mut out = vec![removed];
|
||||
for (i, e) in survivors.iter().enumerate() {
|
||||
let pts = entity_pts(e);
|
||||
out.push(WireModel::solid(format!("trim_keep_{i}"), pts, WireModel::CYAN, false));
|
||||
}
|
||||
out
|
||||
}
|
||||
Some(EntityType::XLine(x)) => {
|
||||
let bx = x.base_point.x;
|
||||
let by = x.base_point.y;
|
||||
let ex_start = bx - x.direction.x * TRIM_EXTENT;
|
||||
let ey_start = by - x.direction.y * TRIM_EXTENT;
|
||||
let ex_end = bx + x.direction.x * TRIM_EXTENT;
|
||||
let ey_end = by + x.direction.y * TRIM_EXTENT;
|
||||
let ts = line_seg_ts(ex_start, ey_start, ex_end, ey_end, handle, &self.geos);
|
||||
if ts.is_empty() { return vec![]; }
|
||||
let dx = ex_end - ex_start;
|
||||
let dy = ey_end - ey_start;
|
||||
let len2 = dx * dx + dy * dy;
|
||||
let t_click = if len2 > 1e-12 {
|
||||
((pt.x as f64 - ex_start) * dx + (pt.y as f64 - ey_start) * dy) / len2
|
||||
} else { 0.5 };
|
||||
let survivors = trim_xline(x, &ts, t_click);
|
||||
// Show a finite 40-unit preview section around base
|
||||
let neg = [(bx - x.direction.x * 20.0) as f32, (by - x.direction.y * 20.0) as f32, x.base_point.z as f32];
|
||||
let pos = [(bx + x.direction.x * 20.0) as f32, (by + x.direction.y * 20.0) as f32, x.base_point.z as f32];
|
||||
let removed = WireModel::solid("trim_rm".into(), vec![neg, pos], DIM_RED, false);
|
||||
let mut out = vec![removed];
|
||||
for (i, e) in survivors.iter().enumerate() {
|
||||
let pts = entity_pts(e);
|
||||
out.push(WireModel::solid(format!("trim_keep_{i}"), pts, WireModel::CYAN, false));
|
||||
}
|
||||
out
|
||||
}
|
||||
_ => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ mod xray;
|
||||
mod zoom_ext;
|
||||
mod zoom_in;
|
||||
mod zoom_out;
|
||||
pub mod plot_window;
|
||||
pub mod zoom_window;
|
||||
|
||||
use crate::modules::{CadModule, RibbonGroup};
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
// PLOTWINDOW command — pick two corners to define the plot window area.
|
||||
//
|
||||
// Works only in paper space layouts. After picking P1 and P2, writes the
|
||||
// window to the layout's PlotSettings (PlotType::Window).
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::scene::wire_model::WireModel;
|
||||
use glam::Vec3;
|
||||
|
||||
pub struct PlotWindowCommand {
|
||||
p1: Option<Vec3>,
|
||||
}
|
||||
|
||||
impl PlotWindowCommand {
|
||||
pub fn new() -> Self {
|
||||
Self { p1: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl CadCommand for PlotWindowCommand {
|
||||
fn name(&self) -> &'static str { "PLOTWINDOW" }
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
if self.p1.is_none() {
|
||||
"PLOTWINDOW Specify first corner of plot window:".into()
|
||||
} else {
|
||||
"PLOTWINDOW Specify opposite corner:".into()
|
||||
}
|
||||
}
|
||||
|
||||
fn on_point(&mut self, pt: Vec3) -> CmdResult {
|
||||
match self.p1 {
|
||||
None => {
|
||||
self.p1 = Some(pt);
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
Some(p1) => CmdResult::SetPlotWindow { p1, p2: pt },
|
||||
}
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
|
||||
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
|
||||
let p1 = self.p1?;
|
||||
// Draw the selection rectangle.
|
||||
Some(WireModel {
|
||||
name: "plotwindow_preview".into(),
|
||||
points: vec![
|
||||
[p1.x, p1.y, p1.z], [pt.x, p1.y, p1.z],
|
||||
[pt.x, p1.y, p1.z], [pt.x, pt.y, pt.z],
|
||||
[pt.x, pt.y, pt.z], [p1.x, pt.y, pt.z],
|
||||
[p1.x, pt.y, pt.z], [p1.x, p1.y, p1.z],
|
||||
],
|
||||
color: WireModel::CYAN,
|
||||
selected: false,
|
||||
pattern_length: 0.0,
|
||||
pattern: [0.0; 8],
|
||||
line_weight_px: 1.0,
|
||||
snap_pts: vec![],
|
||||
tangent_geoms: vec![],
|
||||
key_vertices: vec![],
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user