first commit

This commit is contained in:
Hakan Seven committed 2026-03-22 14:56:27 +03:00
1 parent d52c6440ff
commit d2fdac7f1e
274 files changed
+89330

No files matched your search

+159
View File
@@ -0,0 +1,159 @@
use acadrust::entities::{Dimension, DimensionAngular3Pt};
use acadrust::types::Vector3;
use acadrust::EntityType;
use crate::command::{CadCommand, CmdResult};
use crate::modules::{IconKind, ModuleEvent, ToolDef};
use crate::scene::wire_model::WireModel;
use glam::Vec3;
pub const ICON: IconKind = IconKind::Glyph("∠");
pub fn tool() -> ToolDef {
ToolDef {
id: "DIMANGULAR",
label: "Angular",
icon: ICON,
event: ModuleEvent::Command("DIMANGULAR".to_string()),
}
}
enum Step {
Vertex,
FirstRay(Vec3),
SecondRay { vertex: Vec3, first: Vec3 },
ArcPoint { vertex: Vec3, first: Vec3, second: Vec3 },
}
pub struct AngularDimensionCommand {
step: Step,
}
impl AngularDimensionCommand {
pub fn new() -> Self {
Self { step: Step::Vertex }
}
}
impl CadCommand for AngularDimensionCommand {
fn name(&self) -> &'static str {
"DIMANGULAR"
}
fn prompt(&self) -> String {
match self.step {
Step::Vertex => "DIMANGULAR Specify angle vertex:".into(),
Step::FirstRay(_) => "DIMANGULAR Specify first extension line point:".into(),
Step::SecondRay { .. } => "DIMANGULAR Specify second extension line point:".into(),
Step::ArcPoint { .. } => "DIMANGULAR Specify dimension arc location:".into(),
}
}
fn on_point(&mut self, pt: Vec3) -> CmdResult {
match self.step {
Step::Vertex => {
self.step = Step::FirstRay(pt);
CmdResult::NeedPoint
}
Step::FirstRay(vertex) => {
self.step = Step::SecondRay { vertex, first: pt };
CmdResult::NeedPoint
}
Step::SecondRay { vertex, first } => {
self.step = Step::ArcPoint {
vertex,
first,
second: pt,
};
CmdResult::NeedPoint
}
Step::ArcPoint {
vertex,
first,
second,
} => {
let mut dim = DimensionAngular3Pt::new(v3(vertex), v3(first), v3(second));
dim.definition_point = v3(pt);
dim.base.definition_point = v3(pt);
dim.base.text_middle_point = v3(pt);
dim.base.insertion_point = v3(pt);
dim.base.actual_measurement = dim.measurement_degrees();
CmdResult::CommitAndExit(EntityType::Dimension(Dimension::Angular3Pt(dim)))
}
}
}
fn on_enter(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_escape(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
match self.step {
Step::Vertex => None,
Step::FirstRay(vertex) => Some(preview_wire(vec![vertex, pt])),
Step::SecondRay { vertex, first } => {
Some(preview_wire(vec![vertex, first, Vec3::new(f32::NAN, f32::NAN, f32::NAN), vertex, pt]))
}
Step::ArcPoint {
vertex,
first,
second,
} => Some(preview_wire(angular_preview(vertex, first, second, pt))),
}
}
}
fn v3(pt: Vec3) -> Vector3 {
Vector3::new(pt.x as f64, pt.y as f64, pt.z as f64)
}
fn preview_wire(points: Vec<Vec3>) -> WireModel {
WireModel {
name: "dimangular_preview".to_string(),
points: points.into_iter().map(|p| [p.x, p.y, p.z]).collect(),
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 angular_preview(vertex: Vec3, first: Vec3, second: Vec3, arc_pt: Vec3) -> Vec<Vec3> {
let mut points = vec![
vertex,
first,
Vec3::new(f32::NAN, f32::NAN, f32::NAN),
vertex,
second,
Vec3::new(f32::NAN, f32::NAN, f32::NAN),
];
let r = vertex.distance(arc_pt);
if r <= 1e-6 {
return points;
}
let a0 = (first.y - vertex.y).atan2(first.x - vertex.x);
let mut a1 = (second.y - vertex.y).atan2(second.x - vertex.x);
let mut delta = a1 - a0;
while delta <= 0.0 {
delta += std::f32::consts::TAU;
}
if delta > std::f32::consts::PI {
a1 -= std::f32::consts::TAU;
delta = a1 - a0;
}
let steps = 24;
for i in 0..=steps {
let t = i as f32 / steps as f32;
let a = a0 + delta * t;
points.push(vertex + Vec3::new(a.cos() * r, a.sin() * r, 0.0));
}
points
}
+216
View File
@@ -0,0 +1,216 @@
// LEADER command
//
// Flow:
// 1. CollectPoints — click arrowhead, then bend points; Enter (≥2) to finish
// 2. AskCreationType— wants_text_input; N/T/B/TL → default Text on blank Enter
// 3. AskAnnotation — text string (Text) or block name (Block); blank = skip
// → commit Leader [+ MText | + Insert]
use acadrust::entities::{Insert, Leader, LeaderCreationType, MText};
use acadrust::types::Vector3;
use acadrust::EntityType;
use glam::Vec3;
use crate::command::{CadCommand, CmdResult};
use crate::modules::{IconKind, ModuleEvent, ToolDef};
use crate::scene::wire_model::WireModel;
pub const ICON: IconKind = IconKind::Glyph("↗");
pub fn tool() -> ToolDef {
ToolDef {
id: "LEADER",
label: "Leader",
icon: ICON,
event: ModuleEvent::Command("LEADER".to_string()),
}
}
enum Step {
CollectPoints { verts: Vec<Vec3> },
AskCreationType { verts: Vec<Vec3> },
AskText { verts: Vec<Vec3> },
AskBlock { verts: Vec<Vec3> },
}
pub struct LeaderCommand {
step: Step,
}
impl LeaderCommand {
pub fn new() -> Self {
Self { step: Step::CollectPoints { verts: Vec::new() } }
}
}
impl CadCommand for LeaderCommand {
fn name(&self) -> &'static str { "LEADER" }
fn prompt(&self) -> String {
match &self.step {
Step::CollectPoints { verts } if verts.is_empty() =>
"LEADER Specify arrowhead point:".into(),
Step::CollectPoints { verts } =>
format!("LEADER Specify next point [{} pts — Enter to finish]:", verts.len()),
Step::AskCreationType { .. } =>
"LEADER Annotation type [None/Text/Block/Tolerance] <Text>:".into(),
Step::AskText { verts } =>
format!("LEADER Annotation text [{} pts — blank = skip]:", verts.len()),
Step::AskBlock { verts } =>
format!("LEADER Block name [{} pts — blank = skip]:", verts.len()),
}
}
fn wants_text_input(&self) -> bool {
!matches!(self.step, Step::CollectPoints { .. })
}
fn on_point(&mut self, pt: Vec3) -> CmdResult {
if let Step::CollectPoints { verts } = &mut self.step {
verts.push(pt);
}
CmdResult::NeedPoint
}
fn on_enter(&mut self) -> CmdResult {
if let Step::CollectPoints { verts } = &self.step {
if verts.len() < 2 { return CmdResult::Cancel; }
let verts = verts.clone();
self.step = Step::AskCreationType { verts };
CmdResult::NeedPoint
} else {
CmdResult::Cancel
}
}
fn on_text_input(&mut self, raw: &str) -> Option<CmdResult> {
let text = raw.trim();
match &self.step {
Step::AskCreationType { verts } => {
let verts = verts.clone();
match parse_ct(text) {
LeaderCreationType::NoAnnotation | LeaderCreationType::WithTolerance => {
let ct = parse_ct(text);
Some(CmdResult::CommitAndExit(EntityType::Leader(build_leader(&verts, ct))))
}
LeaderCreationType::WithBlock => {
self.step = Step::AskBlock { verts };
Some(CmdResult::NeedPoint)
}
LeaderCreationType::WithText => {
self.step = Step::AskText { verts };
Some(CmdResult::NeedPoint)
}
}
}
Step::AskText { verts } => {
let verts = verts.clone();
let leader = build_leader(&verts, LeaderCreationType::WithText);
if text.is_empty() {
return Some(CmdResult::CommitAndExit(EntityType::Leader(leader)));
}
let mtext = build_mtext(text, landing_pt(&verts, leader.text_height), leader.text_height);
Some(CmdResult::ReplaceMany(
vec![],
vec![EntityType::Leader(leader), EntityType::MText(mtext)],
))
}
Step::AskBlock { verts } => {
let verts = verts.clone();
let leader = build_leader(&verts, LeaderCreationType::WithBlock);
if text.is_empty() {
return Some(CmdResult::CommitAndExit(EntityType::Leader(leader)));
}
let ins = Insert::new(text, v3(landing_pt(&verts, leader.text_height)));
Some(CmdResult::ReplaceMany(
vec![],
vec![EntityType::Leader(leader), EntityType::Insert(ins)],
))
}
Step::CollectPoints { .. } => None,
}
}
fn on_escape(&mut self) -> CmdResult { CmdResult::Cancel }
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
if let Step::CollectPoints { verts } = &self.step {
if verts.is_empty() { return None; }
let mut pts = verts.clone();
pts.push(pt);
Some(preview_wire(&pts))
} else {
None
}
}
}
// ── Helpers ─────────────────────────────────────────────────────────────────
fn parse_ct(s: &str) -> LeaderCreationType {
match s.to_ascii_uppercase().as_str() {
"N" | "NONE" => LeaderCreationType::NoAnnotation,
"B" | "BLOCK" => LeaderCreationType::WithBlock,
"TL"| "TOLERANCE" => LeaderCreationType::WithTolerance,
_ => LeaderCreationType::WithText,
}
}
fn v3(p: Vec3) -> Vector3 { Vector3::new(p.x as f64, p.y as f64, p.z as f64) }
fn build_leader(verts: &[Vec3], ct: LeaderCreationType) -> Leader {
let mut l = Leader::from_vertices(verts.iter().map(|p| v3(*p)).collect());
l.creation_type = ct;
l.hookline_enabled = !matches!(ct, LeaderCreationType::NoAnnotation);
l
}
fn landing_pt(verts: &[Vec3], text_height: f64) -> Vec3 {
let last = *verts.last().unwrap();
let prev = verts[verts.len() - 2];
let sign = if last.x >= prev.x { 1.0_f32 } else { -1.0_f32 };
Vec3::new(last.x + sign * text_height as f32 * 1.5, last.y, last.z)
}
fn build_mtext(text: &str, pos: Vec3, height: f64) -> MText {
let mut m = MText::new();
m.value = text.to_string();
m.insertion_point = v3(pos);
m.height = height;
m
}
fn preview_wire(pts: &[Vec3]) -> WireModel {
let mut points: Vec<[f32; 3]> = pts.iter().map(|p| [p.x, p.y, p.z]).collect();
if pts.len() >= 2 {
let [w1, w2] = arrowhead_wings(pts[0], pts[1], 2.0);
points.push([f32::NAN; 3]);
points.push([w1.x, w1.y, w1.z]);
points.push([pts[0].x, pts[0].y, pts[0].z]);
points.push([w2.x, w2.y, w2.z]);
}
WireModel {
name: "leader_preview".into(),
points,
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![],
}
}
pub fn arrowhead_wings(tip: Vec3, next: Vec3, size: f32) -> [Vec3; 2] {
let d = next - tip;
let len = (d.x * d.x + d.y * d.y).sqrt().max(1e-9);
let (dx, dy) = (d.x / len, d.y / len);
let angle = std::f32::consts::PI / 6.0;
let (s, c) = angle.sin_cos();
[
Vec3::new(tip.x + (dx*c - dy*s)*size, tip.y + (dx*s + dy*c)*size, tip.z),
Vec3::new(tip.x + (dx*c + dy*s)*size, tip.y + (-dx*s + dy*c)*size, tip.z),
]
}
+160
View File
@@ -0,0 +1,160 @@
use acadrust::entities::{Dimension, DimensionLinear};
use acadrust::types::Vector3;
use acadrust::EntityType;
use crate::command::{CadCommand, CmdResult};
use crate::modules::{IconKind, ModuleEvent, ToolDef};
use crate::scene::wire_model::WireModel;
use glam::Vec3;
pub const ICON: IconKind = IconKind::Glyph("⟷");
pub fn tool() -> ToolDef {
ToolDef {
id: "DIMLINEAR",
label: "Linear",
icon: ICON,
event: ModuleEvent::Command("DIMLINEAR".to_string()),
}
}
enum Step {
FirstPoint,
SecondPoint(Vec3),
DimensionLine { first: Vec3, second: Vec3 },
}
pub struct LinearDimensionCommand {
step: Step,
}
impl LinearDimensionCommand {
pub fn new() -> Self {
Self {
step: Step::FirstPoint,
}
}
}
impl CadCommand for LinearDimensionCommand {
fn name(&self) -> &'static str {
"DIMLINEAR"
}
fn prompt(&self) -> String {
match self.step {
Step::FirstPoint => "DIMLINEAR Specify first extension line origin:".into(),
Step::SecondPoint(_) => "DIMLINEAR Specify second extension line origin:".into(),
Step::DimensionLine { .. } => "DIMLINEAR Specify dimension line location:".into(),
}
}
fn on_point(&mut self, pt: Vec3) -> CmdResult {
match self.step {
Step::FirstPoint => {
self.step = Step::SecondPoint(pt);
CmdResult::NeedPoint
}
Step::SecondPoint(first) => {
self.step = Step::DimensionLine { first, second: pt };
CmdResult::NeedPoint
}
Step::DimensionLine { first, second } => {
let mut dim = DimensionLinear::new(v3(first), v3(second));
dim.rotation = if (second.y - first.y).abs() > (second.x - first.x).abs() {
std::f64::consts::FRAC_PI_2
} else {
0.0
};
dim.definition_point = v3(pt);
dim.base.definition_point = v3(pt);
dim.base.text_middle_point = v3(linear_text_pos(first, second, pt));
dim.base.insertion_point = dim.base.text_middle_point;
dim.base.actual_measurement = dim.measurement();
CmdResult::CommitAndExit(EntityType::Dimension(Dimension::Linear(dim)))
}
}
}
fn on_enter(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_escape(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
match self.step {
Step::FirstPoint => None,
Step::SecondPoint(first) => Some(preview_wire(vec![first, pt])),
Step::DimensionLine { first, second } => {
Some(preview_wire(linear_dimension_preview(first, second, pt)))
}
}
}
}
fn v3(pt: Vec3) -> Vector3 {
Vector3::new(pt.x as f64, pt.y as f64, pt.z as f64)
}
fn preview_wire(points: Vec<Vec3>) -> WireModel {
WireModel {
name: "dimlinear_preview".to_string(),
points: points.into_iter().map(|p| [p.x, p.y, p.z]).collect(),
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 linear_dimension_preview(first: Vec3, second: Vec3, def: Vec3) -> Vec<Vec3> {
let axis = {
let d = second - first;
if d.length_squared() <= 1e-12 {
Vec3::X
} else if d.x.abs() >= d.y.abs() {
Vec3::X
} else {
Vec3::Y
}
};
let perp = Vec3::new(-axis.y, axis.x, 0.0);
let offset = (def - first).dot(perp);
let d1 = first + perp * offset;
let d2 = second + perp * offset;
vec![
first,
d1,
Vec3::new(f32::NAN, f32::NAN, f32::NAN),
second,
d2,
Vec3::new(f32::NAN, f32::NAN, f32::NAN),
d1,
d2,
]
}
fn linear_text_pos(first: Vec3, second: Vec3, def: Vec3) -> Vec3 {
let axis = {
let d = second - first;
if d.length_squared() <= 1e-12 {
Vec3::X
} else if d.x.abs() >= d.y.abs() {
Vec3::X
} else {
Vec3::Y
}
};
let perp = Vec3::new(-axis.y, axis.x, 0.0);
let offset = (def - first).dot(perp);
let d1 = first + perp * offset;
let d2 = second + perp * offset;
(d1 + d2) * 0.5 + perp * 0.15
}
+169
View File
@@ -0,0 +1,169 @@
// MLEADER command
//
// Flow:
// 1. CollectPoints — click arrowhead, then bend points; Enter (≥2) to finish
// 2. AskText — wants_text_input; blank Enter = no text
// → commit single MultiLeader entity
use acadrust::entities::MultiLeader;
use acadrust::types::Vector3;
use acadrust::EntityType;
use glam::Vec3;
use crate::command::{CadCommand, CmdResult};
use crate::modules::{IconKind, ModuleEvent, ToolDef};
use crate::scene::wire_model::WireModel;
pub const ICON: IconKind = IconKind::Glyph("↙");
pub fn tool() -> ToolDef {
ToolDef {
id: "MLEADER",
label: "MLeader",
icon: ICON,
event: ModuleEvent::Command("MLEADER".to_string()),
}
}
enum Step {
CollectPoints { verts: Vec<Vec3> },
AskText { verts: Vec<Vec3> },
}
pub struct MLeaderCommand {
step: Step,
}
impl MLeaderCommand {
pub fn new() -> Self {
Self { step: Step::CollectPoints { verts: Vec::new() } }
}
}
impl CadCommand for MLeaderCommand {
fn name(&self) -> &'static str { "MLEADER" }
fn prompt(&self) -> String {
match &self.step {
Step::CollectPoints { verts } if verts.is_empty() =>
"MLEADER Specify arrowhead point:".into(),
Step::CollectPoints { verts } =>
format!("MLEADER Specify next point [{} pts — Enter to finish]:", verts.len()),
Step::AskText { verts } =>
format!("MLEADER Enter annotation text [{} pts — blank = no text]:", verts.len()),
}
}
fn wants_text_input(&self) -> bool {
matches!(self.step, Step::AskText { .. })
}
fn on_point(&mut self, pt: Vec3) -> CmdResult {
if let Step::CollectPoints { verts } = &mut self.step {
verts.push(pt);
}
CmdResult::NeedPoint
}
fn on_enter(&mut self) -> CmdResult {
if let Step::CollectPoints { verts } = &self.step {
if verts.len() < 2 { return CmdResult::Cancel; }
let verts = verts.clone();
self.step = Step::AskText { verts };
CmdResult::NeedPoint
} else {
CmdResult::Cancel
}
}
fn on_text_input(&mut self, raw: &str) -> Option<CmdResult> {
if let Step::AskText { verts } = &self.step {
let text = raw.trim();
let ml = build_mleader(text, verts);
Some(CmdResult::CommitAndExit(EntityType::MultiLeader(ml)))
} else {
None
}
}
fn on_escape(&mut self) -> CmdResult { CmdResult::Cancel }
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
if let Step::CollectPoints { verts } = &self.step {
if verts.is_empty() { return None; }
let mut pts = verts.clone();
pts.push(pt);
Some(preview_wire(&pts))
} else {
None
}
}
}
// ── Helpers ─────────────────────────────────────────────────────────────────
fn v3(p: Vec3) -> Vector3 { Vector3::new(p.x as f64, p.y as f64, p.z as f64) }
fn build_mleader(text: &str, verts: &[Vec3]) -> MultiLeader {
// Last vertex = content/text location; remaining = leader line points
let (leader_pts, content_pt) = verts.split_at(verts.len() - 1);
let content_pt = content_pt[0];
let leader_v3: Vec<Vector3> = leader_pts.iter().map(|p| v3(*p)).collect();
let content_v3 = v3(content_pt);
let mut ml = MultiLeader::with_text(text, content_v3, leader_v3);
// Match Leader entity defaults
ml.text_height = 2.5;
ml.context.text_height = 2.5;
ml.arrowhead_size = 2.5;
ml.dogleg_length = 2.5;
// Direction: from last leader pt toward content
if let (Some(last_leader), Some(root)) = (leader_pts.last(), ml.context.leader_roots.first_mut()) {
let dx = (content_pt.x - last_leader.x) as f64;
let dy = (content_pt.y - last_leader.y) as f64;
let len = (dx * dx + dy * dy).sqrt().max(1e-9);
root.direction = Vector3::new(dx / len, dy / len, 0.0);
root.connection_point = content_v3;
root.landing_distance = 2.5;
}
ml
}
fn preview_wire(pts: &[Vec3]) -> WireModel {
let mut points: Vec<[f32; 3]> = pts.iter().map(|p| [p.x, p.y, p.z]).collect();
if pts.len() >= 2 {
let [w1, w2] = arrowhead_wings(pts[0], pts[1], 2.0);
points.push([f32::NAN; 3]);
points.push([w1.x, w1.y, w1.z]);
points.push([pts[0].x, pts[0].y, pts[0].z]);
points.push([w2.x, w2.y, w2.z]);
}
WireModel {
name: "mleader_preview".into(),
points,
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 arrowhead_wings(tip: Vec3, next: Vec3, size: f32) -> [Vec3; 2] {
let d = next - tip;
let len = (d.x * d.x + d.y * d.y).sqrt().max(1e-9);
let (dx, dy) = (d.x / len, d.y / len);
let angle = std::f32::consts::PI / 6.0;
let (s, c) = angle.sin_cos();
[
Vec3::new(tip.x + (dx*c - dy*s)*size, tip.y + (dx*s + dy*c)*size, tip.z),
Vec3::new(tip.x + (dx*c + dy*s)*size, tip.y + (-dx*s + dy*c)*size, tip.z),
]
}
+73
View File
@@ -0,0 +1,73 @@
// Annotate module — dimension and text annotation tools.
pub mod angular_dim;
pub mod leader_cmd;
pub mod linear_dim;
pub mod mleader_cmd;
pub mod mtext;
pub mod radius_dim;
pub mod text;
use crate::modules::{CadModule, RibbonGroup, RibbonItem};
pub struct AnnotateModule;
impl CadModule for AnnotateModule {
fn id(&self) -> &'static str {
"annotate"
}
fn title(&self) -> &'static str {
"Annotate"
}
fn ribbon_groups(&self) -> Vec<RibbonGroup> {
vec![
RibbonGroup {
title: "Text",
tools: vec![
RibbonItem::LargeDropdown {
id: "ANNOTATE_TEXT",
label: "Text",
icon: text::ICON,
items: vec![
(text::tool().id, text::tool().label, text::tool().icon),
(mtext::tool().id, mtext::tool().label, mtext::tool().icon),
],
default: "TEXT",
},
],
},
RibbonGroup {
title: "Dimensions",
tools: vec![
RibbonItem::LargeDropdown {
id: "ANNOTATE_DIM",
label: "Dimensions",
icon: linear_dim::ICON,
items: vec![
(linear_dim::tool().id, linear_dim::tool().label, linear_dim::tool().icon),
(radius_dim::tool().id, radius_dim::tool().label, radius_dim::tool().icon),
(angular_dim::tool().id, angular_dim::tool().label, angular_dim::tool().icon),
],
default: "DIMLINEAR",
},
],
},
RibbonGroup {
title: "Leaders",
tools: vec![
RibbonItem::LargeDropdown {
id: "ANNOTATE_LEADER",
label: "Leader",
icon: leader_cmd::ICON,
items: vec![
(mleader_cmd::tool().id, mleader_cmd::tool().label, mleader_cmd::tool().icon),
(leader_cmd::tool().id, leader_cmd::tool().label, leader_cmd::tool().icon),
],
default: "MLEADER",
},
],
},
]
}
}
+89
View File
@@ -0,0 +1,89 @@
use acadrust::types::Vector3;
use acadrust::{EntityType, MText};
use crate::command::{CadCommand, CmdResult};
use crate::modules::{IconKind, ModuleEvent, ToolDef};
use crate::scene::wire_model::WireModel;
use glam::Vec3;
pub const ICON: IconKind = IconKind::Svg(include_bytes!("../../../assets/icons/mtext.svg"));
pub fn tool() -> ToolDef {
ToolDef {
id: "MTEXT",
label: "MText",
icon: ICON,
event: ModuleEvent::Command("MTEXT".to_string()),
}
}
enum Step {
InsertPoint,
WaitText(Vec3),
}
pub struct MTextCommand {
step: Step,
}
impl MTextCommand {
pub fn new() -> Self {
Self {
step: Step::InsertPoint,
}
}
}
impl CadCommand for MTextCommand {
fn name(&self) -> &'static str {
"MTEXT"
}
fn prompt(&self) -> String {
match &self.step {
Step::InsertPoint => "MTEXT Specify insertion point:".into(),
Step::WaitText(pos) => format!(
"MTEXT Type text, press Enter [at {:.2},{:.2}]:",
pos.x, pos.z
),
}
}
fn on_point(&mut self, pt: Vec3) -> CmdResult {
self.step = Step::WaitText(pt);
CmdResult::NeedPoint
}
fn on_enter(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_escape(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn wants_text_input(&self) -> bool {
matches!(self.step, Step::WaitText(_))
}
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
if let Step::WaitText(pos) = &self.step {
if text.trim().is_empty() {
return Some(CmdResult::Cancel);
}
let mt = MText {
insertion_point: Vector3::new(pos.x as f64, pos.y as f64, pos.z as f64),
value: text.to_string(),
height: 0.25,
rectangle_width: text.len() as f64 * 0.15,
..Default::default()
};
Some(CmdResult::CommitEntity(EntityType::MText(mt)))
} else {
None
}
}
fn on_mouse_move(&mut self, _pt: Vec3) -> Option<WireModel> {
None
}
}
+114
View File
@@ -0,0 +1,114 @@
use acadrust::entities::{Dimension, DimensionRadius};
use acadrust::types::Vector3;
use acadrust::EntityType;
use crate::command::{CadCommand, CmdResult};
use crate::modules::{IconKind, ModuleEvent, ToolDef};
use crate::scene::wire_model::WireModel;
use glam::Vec3;
pub const ICON: IconKind = IconKind::Glyph("⌀");
pub fn tool() -> ToolDef {
ToolDef {
id: "DIMRADIUS",
label: "Radius",
icon: ICON,
event: ModuleEvent::Command("DIMRADIUS".to_string()),
}
}
enum Step {
CenterPoint,
RadiusPoint(Vec3),
TextPoint { center: Vec3, point: Vec3 },
}
pub struct RadiusDimensionCommand {
step: Step,
}
impl RadiusDimensionCommand {
pub fn new() -> Self {
Self {
step: Step::CenterPoint,
}
}
}
impl CadCommand for RadiusDimensionCommand {
fn name(&self) -> &'static str {
"DIMRADIUS"
}
fn prompt(&self) -> String {
match self.step {
Step::CenterPoint => "DIMRADIUS Specify center point:".into(),
Step::RadiusPoint(_) => "DIMRADIUS Specify radius point:".into(),
Step::TextPoint { .. } => "DIMRADIUS Specify dimension line location:".into(),
}
}
fn on_point(&mut self, pt: Vec3) -> CmdResult {
match self.step {
Step::CenterPoint => {
self.step = Step::RadiusPoint(pt);
CmdResult::NeedPoint
}
Step::RadiusPoint(center) => {
self.step = Step::TextPoint { center, point: pt };
CmdResult::NeedPoint
}
Step::TextPoint { center, point } => {
let mut dim = DimensionRadius::new(v3(center), v3(point));
dim.base.definition_point = v3(point);
dim.base.text_middle_point = v3(pt);
dim.base.insertion_point = v3(pt);
dim.leader_length = point.distance(pt) as f64;
dim.base.actual_measurement = dim.measurement();
CmdResult::CommitAndExit(EntityType::Dimension(Dimension::Radius(dim)))
}
}
}
fn on_enter(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_escape(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
match self.step {
Step::CenterPoint => None,
Step::RadiusPoint(center) => Some(preview_wire(vec![center, pt])),
Step::TextPoint { center, point } => Some(preview_wire(vec![
center,
point,
Vec3::new(f32::NAN, f32::NAN, f32::NAN),
point,
pt,
])),
}
}
}
fn v3(pt: Vec3) -> Vector3 {
Vector3::new(pt.x as f64, pt.y as f64, pt.z as f64)
}
fn preview_wire(points: Vec<Vec3>) -> WireModel {
WireModel {
name: "dimradius_preview".to_string(),
points: points.into_iter().map(|p| [p.x, p.y, p.z]).collect(),
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![],
}
}
+84
View File
@@ -0,0 +1,84 @@
use acadrust::types::Vector3;
use acadrust::{EntityType, Text};
use crate::command::{CadCommand, CmdResult};
use crate::modules::{IconKind, ModuleEvent, ToolDef};
use crate::scene::wire_model::WireModel;
use glam::Vec3;
pub const ICON: IconKind = IconKind::Svg(include_bytes!("../../../assets/icons/text.svg"));
pub fn tool() -> ToolDef {
ToolDef {
id: "TEXT",
label: "Text",
icon: ICON,
event: ModuleEvent::Command("TEXT".to_string()),
}
}
enum Step {
InsertPoint,
WaitText(Vec3),
}
pub struct TextCommand {
step: Step,
}
impl TextCommand {
pub fn new() -> Self {
Self {
step: Step::InsertPoint,
}
}
}
impl CadCommand for TextCommand {
fn name(&self) -> &'static str {
"TEXT"
}
fn prompt(&self) -> String {
match &self.step {
Step::InsertPoint => "TEXT Specify insertion point:".into(),
Step::WaitText(pos) => format!(
"TEXT Type text, press Enter [at {:.2},{:.2}]:",
pos.x, pos.z
),
}
}
fn on_point(&mut self, pt: Vec3) -> CmdResult {
self.step = Step::WaitText(pt);
CmdResult::NeedPoint
}
fn on_enter(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_escape(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn wants_text_input(&self) -> bool {
matches!(self.step, Step::WaitText(_))
}
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
if let Step::WaitText(pos) = &self.step {
if text.trim().is_empty() {
return Some(CmdResult::Cancel);
}
let t = Text::with_value(text, Vector3::new(pos.x as f64, pos.y as f64, pos.z as f64))
.with_height(0.25);
Some(CmdResult::CommitEntity(EntityType::Text(t)))
} else {
None
}
}
fn on_mouse_move(&mut self, _pt: Vec3) -> Option<WireModel> {
None
}
}