forked from 1b-agent/opencadstudio-upstream
Complete baseline dimension workflow
This commit is contained in:
1 parent
c6f6c00220
commit
980a7d611b
2 files changed
+646
-197
No files matched your search
+22
-15
@@ -659,23 +659,30 @@ impl OpenCADStudio {
|
|||||||
|
|
||||||
"DIMBASELINE" => {
|
"DIMBASELINE" => {
|
||||||
use crate::modules::annotate::dim_baseline::DimBaselineCommand;
|
use crate::modules::annotate::dim_baseline::DimBaselineCommand;
|
||||||
let cmd = if let Some((p1, p2, dp, rot, trot)) =
|
let scene = &self.tabs[i].scene;
|
||||||
find_last_linear_dim(&self.tabs[i].scene)
|
let recent = scene
|
||||||
{
|
.last_created_dimension
|
||||||
let doc = &self.tabs[i].scene.document;
|
.and_then(|handle| scene.document.get_entity(handle))
|
||||||
let dimdli = doc
|
.cloned();
|
||||||
.dim_styles
|
let dimdli_by_style = scene
|
||||||
.iter()
|
.document
|
||||||
.find(|s| {
|
.dim_styles
|
||||||
s.name
|
.iter()
|
||||||
.eq_ignore_ascii_case(&doc.header.current_dimstyle_name)
|
.map(|style| (style.name.to_ascii_lowercase(), style.dimdli))
|
||||||
})
|
.collect();
|
||||||
.map(|s| s.dimdli as f32)
|
let fallback_dimdli = if scene.document.header.measurement == 1 {
|
||||||
.unwrap_or(1.5);
|
3.75
|
||||||
DimBaselineCommand::from_base(p1, p2, dp, rot, trot, dimdli)
|
|
||||||
} else {
|
} else {
|
||||||
DimBaselineCommand::new()
|
0.38
|
||||||
};
|
};
|
||||||
|
let current_style_name = scene.document.header.current_dimstyle_name.clone();
|
||||||
|
let cmd = DimBaselineCommand::new(
|
||||||
|
recent,
|
||||||
|
dimdli_by_style,
|
||||||
|
current_style_name,
|
||||||
|
fallback_dimdli,
|
||||||
|
self.dimension_continue_mode == 1,
|
||||||
|
);
|
||||||
self.command_line.push_info(&cmd.prompt());
|
self.command_line.push_info(&cmd.prompt());
|
||||||
self.tabs[i].active_cmd = Some(Box::new(cmd));
|
self.tabs[i].active_cmd = Some(Box::new(cmd));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,18 @@
|
|||||||
// DIMBASELINE command — stacked baseline dimensions all measured from the same origin.
|
//! Repeating baseline dimensions from a session base or an explicitly picked base.
|
||||||
//
|
|
||||||
// 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 std::collections::HashMap;
|
||||||
|
|
||||||
|
use acadrust::entities::{
|
||||||
|
Dimension, DimensionAligned, DimensionAngular2Ln, DimensionAngular3Pt, DimensionBase,
|
||||||
|
DimensionLinear, DimensionOrdinate,
|
||||||
|
};
|
||||||
use acadrust::types::Vector3;
|
use acadrust::types::Vector3;
|
||||||
use acadrust::EntityType;
|
use acadrust::{EntityType, Handle};
|
||||||
use glam::{DVec3, Vec3};
|
use glam::DVec3;
|
||||||
|
|
||||||
use crate::command::{CadCommand, CmdResult};
|
use crate::command::{CadCommand, CmdOption, CmdResult, DimensionAssociationInput};
|
||||||
use crate::modules::{IconKind, ModuleEvent, ToolDef};
|
use crate::modules::{IconKind, ModuleEvent, ToolDef};
|
||||||
use crate::scene::model::wire_model::WireModel;
|
use crate::scene::model::wire_model::WireModel;
|
||||||
use crate::t;
|
|
||||||
|
|
||||||
pub const ICON: IconKind = IconKind::Svg(include_bytes!("../../../assets/icons/dim_baseline.svg"));
|
pub const ICON: IconKind = IconKind::Svg(include_bytes!("../../../assets/icons/dim_baseline.svg"));
|
||||||
|
|
||||||
@@ -27,100 +25,302 @@ pub fn tool() -> ToolDef {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Fallback stacking increment (world units) when no DimStyle is available.
|
#[derive(Clone)]
|
||||||
const DEFAULT_DIMDLI: f64 = 1.5;
|
struct SourceStyle {
|
||||||
|
layer: String,
|
||||||
|
style_name: String,
|
||||||
|
normal: Vector3,
|
||||||
|
text_rotation: f64,
|
||||||
|
horizontal_direction: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SourceStyle {
|
||||||
|
fn from_base(base: &DimensionBase) -> Self {
|
||||||
|
Self {
|
||||||
|
layer: base.common.layer.clone(),
|
||||||
|
style_name: base.style_name.clone(),
|
||||||
|
normal: base.normal,
|
||||||
|
text_rotation: base.text_rotation,
|
||||||
|
horizontal_direction: base.horizontal_direction,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply(&self, base: &mut DimensionBase) {
|
||||||
|
base.common.layer = self.layer.clone();
|
||||||
|
base.style_name = self.style_name.clone();
|
||||||
|
base.normal = self.normal;
|
||||||
|
base.text_rotation = self.text_rotation;
|
||||||
|
base.horizontal_direction = self.horizontal_direction;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum BaselineKind {
|
||||||
|
Linear {
|
||||||
|
fixed: DVec3,
|
||||||
|
rotation: f64,
|
||||||
|
aligned: bool,
|
||||||
|
perpendicular: DVec3,
|
||||||
|
next_offset: f64,
|
||||||
|
increment: f64,
|
||||||
|
},
|
||||||
|
Angular2Ln {
|
||||||
|
source: DimensionAngular2Ln,
|
||||||
|
vertex: DVec3,
|
||||||
|
fixed_ray: DVec3,
|
||||||
|
next_radius: f64,
|
||||||
|
increment: f64,
|
||||||
|
},
|
||||||
|
Angular3Pt {
|
||||||
|
source: DimensionAngular3Pt,
|
||||||
|
vertex: DVec3,
|
||||||
|
fixed_ray: DVec3,
|
||||||
|
next_radius: f64,
|
||||||
|
increment: f64,
|
||||||
|
},
|
||||||
|
Ordinate {
|
||||||
|
source: DimensionOrdinate,
|
||||||
|
leader_delta: DVec3,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
struct BaselineState {
|
||||||
|
kind: BaselineKind,
|
||||||
|
style: SourceStyle,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct DimBaselineCommand {
|
pub struct DimBaselineCommand {
|
||||||
/// Fixed first-extension-line origin (never changes).
|
base: Option<BaselineState>,
|
||||||
base_p1: DVec3,
|
injected: Option<EntityType>,
|
||||||
/// Direction along the measurement direction (0.0 = horizontal, PI/2 = vertical).
|
dimdli_by_style: HashMap<String, f64>,
|
||||||
rotation: f64,
|
current_style_name: String,
|
||||||
/// Text reading rotation inherited from the base dim (keeps a UCS-aligned
|
fallback_dimdli: f64,
|
||||||
/// chain's text consistent with the originating DIMLINEAR). 0 = natural.
|
preserve_base_style: bool,
|
||||||
text_rotation: f64,
|
committed: usize,
|
||||||
/// Unit vector perpendicular to the dimension axis, pointing toward the dim line side.
|
|
||||||
perp: DVec3,
|
|
||||||
/// Perpendicular distance of the NEXT dimension line from the extension-line axis.
|
|
||||||
next_offset: f64,
|
|
||||||
/// Stacking increment from the active DimStyle (DIMDLI).
|
|
||||||
dimdli: f64,
|
|
||||||
/// True once we have a base dimension loaded.
|
|
||||||
ready: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DimBaselineCommand {
|
impl DimBaselineCommand {
|
||||||
/// No base dim found — cancel immediately.
|
pub fn new(
|
||||||
pub fn new() -> Self {
|
recent: Option<EntityType>,
|
||||||
Self {
|
dimdli_by_style: HashMap<String, f64>,
|
||||||
base_p1: DVec3::ZERO,
|
current_style_name: String,
|
||||||
rotation: 0.0,
|
fallback_dimdli: f64,
|
||||||
text_rotation: 0.0,
|
preserve_base_style: bool,
|
||||||
perp: DVec3::Y,
|
|
||||||
next_offset: DEFAULT_DIMDLI,
|
|
||||||
dimdli: DEFAULT_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.
|
|
||||||
/// `dimdli` — DimStyle stacking increment (use [`DEFAULT_DIMDLI`] when no style is active).
|
|
||||||
pub fn from_base(
|
|
||||||
p1: Vec3,
|
|
||||||
_p2: Vec3,
|
|
||||||
definition_point: Vec3,
|
|
||||||
rotation: f64,
|
|
||||||
text_rotation: f64,
|
|
||||||
dimdli: f32,
|
|
||||||
) -> Self {
|
) -> Self {
|
||||||
// The base dim's points enter as f32 from the caller; promote to f64 so
|
let mut command = Self {
|
||||||
// all committed-coordinate math downstream stays exact.
|
base: None,
|
||||||
let p1 = p1.as_dvec3();
|
injected: None,
|
||||||
let definition_point = definition_point.as_dvec3();
|
dimdli_by_style,
|
||||||
// Measurement axis = the base dim's rotation angle (any angle, incl. a
|
current_style_name,
|
||||||
// UCS-aligned one), not a world horizontal/vertical.
|
fallback_dimdli,
|
||||||
let axis = DVec3::new(rotation.cos(), rotation.sin(), 0.0);
|
preserve_base_style,
|
||||||
let perp = DVec3::new(-axis.y, axis.x, 0.0);
|
committed: 0,
|
||||||
let base_offset = (definition_point - p1).dot(perp);
|
|
||||||
let dimdli = dimdli as f64;
|
|
||||||
let dimdli = if dimdli.abs() < 1e-6 {
|
|
||||||
DEFAULT_DIMDLI
|
|
||||||
} else {
|
|
||||||
dimdli.abs()
|
|
||||||
};
|
};
|
||||||
// Stack each baseline outward — in the SIGN direction of the base dim's
|
if let Some(entity) = recent {
|
||||||
// own offset. When the base dim sits on the -perp side (below / left of
|
let _ = command.install_base(&entity, None);
|
||||||
// the points) a positive increment would march the stack back toward and
|
}
|
||||||
// across the points; the signed increment keeps it moving away. (#181)
|
command
|
||||||
let dir = if base_offset >= 0.0 { 1.0 } else { -1.0 };
|
}
|
||||||
let dimdli = dimdli * dir;
|
|
||||||
let next_offset = base_offset + dimdli;
|
fn effective_increment(&self, style_name: &str) -> f64 {
|
||||||
Self {
|
let style_name = if self.preserve_base_style {
|
||||||
base_p1: p1,
|
style_name
|
||||||
rotation,
|
} else {
|
||||||
text_rotation,
|
&self.current_style_name
|
||||||
perp,
|
};
|
||||||
next_offset,
|
self.dimdli_by_style
|
||||||
dimdli,
|
.get(&style_name.to_ascii_lowercase())
|
||||||
ready: true,
|
.copied()
|
||||||
|
.filter(|value| value.is_finite() && value.abs() > 1.0e-9)
|
||||||
|
.unwrap_or(self.fallback_dimdli)
|
||||||
|
.abs()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn install_base(&mut self, entity: &EntityType, pick: Option<DVec3>) -> bool {
|
||||||
|
let EntityType::Dimension(dimension) = entity else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
let style = SourceStyle::from_base(dimension.base());
|
||||||
|
let increment = self.effective_increment(&style.style_name);
|
||||||
|
let kind = match dimension {
|
||||||
|
Dimension::Linear(source) => {
|
||||||
|
let (fixed, _) = nearest_end(source.first_point, source.second_point, pick);
|
||||||
|
linear_base(
|
||||||
|
fixed,
|
||||||
|
source.rotation,
|
||||||
|
false,
|
||||||
|
dv(source.base.definition_point),
|
||||||
|
increment,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Dimension::Aligned(source) => {
|
||||||
|
let (fixed, other) = nearest_end(source.first_point, source.second_point, pick);
|
||||||
|
linear_base(
|
||||||
|
fixed,
|
||||||
|
(other - fixed).y.atan2((other - fixed).x),
|
||||||
|
true,
|
||||||
|
dv(source.base.definition_point),
|
||||||
|
increment,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Dimension::Angular2Ln(source) => {
|
||||||
|
let vertex = angular2_vertex(source);
|
||||||
|
let first = dv(source.second_point) - vertex;
|
||||||
|
let second = dv(source.definition_point) - vertex;
|
||||||
|
let fixed_ray = nearest_direction(first, second, vertex, pick);
|
||||||
|
let radius = (dv(source.dimension_arc) - vertex).length().max(1.0e-9);
|
||||||
|
BaselineKind::Angular2Ln {
|
||||||
|
source: source.clone(),
|
||||||
|
vertex,
|
||||||
|
fixed_ray,
|
||||||
|
next_radius: radius + increment,
|
||||||
|
increment,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Dimension::Angular3Pt(source) => {
|
||||||
|
let vertex = dv(source.angle_vertex);
|
||||||
|
let first = dv(source.first_point) - vertex;
|
||||||
|
let second = dv(source.second_point) - vertex;
|
||||||
|
let fixed_ray = nearest_direction(first, second, vertex, pick);
|
||||||
|
let radius = (dv(source.definition_point) - vertex).length().max(1.0e-9);
|
||||||
|
BaselineKind::Angular3Pt {
|
||||||
|
source: source.clone(),
|
||||||
|
vertex,
|
||||||
|
fixed_ray,
|
||||||
|
next_radius: radius + increment,
|
||||||
|
increment,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Dimension::Ordinate(source) => BaselineKind::Ordinate {
|
||||||
|
source: source.clone(),
|
||||||
|
leader_delta: dv(source.leader_endpoint) - dv(source.feature_location),
|
||||||
|
},
|
||||||
|
_ => return false,
|
||||||
|
};
|
||||||
|
self.base = Some(BaselineState { kind, style });
|
||||||
|
self.committed = 0;
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_dimension(&self, point: DVec3) -> Option<Dimension> {
|
||||||
|
let state = self.base.as_ref()?;
|
||||||
|
let mut dimension = match &state.kind {
|
||||||
|
BaselineKind::Linear {
|
||||||
|
fixed,
|
||||||
|
rotation,
|
||||||
|
aligned,
|
||||||
|
perpendicular,
|
||||||
|
next_offset,
|
||||||
|
..
|
||||||
|
} => build_linear(
|
||||||
|
*fixed,
|
||||||
|
point,
|
||||||
|
*rotation,
|
||||||
|
*aligned,
|
||||||
|
*perpendicular,
|
||||||
|
*next_offset,
|
||||||
|
),
|
||||||
|
BaselineKind::Angular2Ln {
|
||||||
|
source,
|
||||||
|
vertex,
|
||||||
|
fixed_ray,
|
||||||
|
next_radius,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
let moving = point - *vertex;
|
||||||
|
if moving.length_squared() <= 1.0e-18 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let mut result = source.clone();
|
||||||
|
result.first_point = v3(*vertex);
|
||||||
|
result.second_point = v3(*vertex + normalized_or(*fixed_ray, DVec3::X));
|
||||||
|
result.angle_vertex = v3(*vertex);
|
||||||
|
result.definition_point = v3(point);
|
||||||
|
result.dimension_arc = v3(
|
||||||
|
*vertex + angular_bisector(*fixed_ray, moving) * *next_radius,
|
||||||
|
);
|
||||||
|
result.base.definition_point = result.dimension_arc;
|
||||||
|
result.base.text_middle_point = result.dimension_arc;
|
||||||
|
result.base.insertion_point = result.dimension_arc;
|
||||||
|
result.base.actual_measurement = result.measurement_degrees();
|
||||||
|
Dimension::Angular2Ln(result)
|
||||||
|
}
|
||||||
|
BaselineKind::Angular3Pt {
|
||||||
|
source,
|
||||||
|
vertex,
|
||||||
|
fixed_ray,
|
||||||
|
next_radius,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
let moving = point - *vertex;
|
||||||
|
if moving.length_squared() <= 1.0e-18 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let mut result = source.clone();
|
||||||
|
result.angle_vertex = v3(*vertex);
|
||||||
|
result.first_point = v3(*vertex + normalized_or(*fixed_ray, DVec3::X));
|
||||||
|
result.second_point = v3(point);
|
||||||
|
result.definition_point = v3(
|
||||||
|
*vertex + angular_bisector(*fixed_ray, moving) * *next_radius,
|
||||||
|
);
|
||||||
|
result.base.definition_point = result.definition_point;
|
||||||
|
result.base.text_middle_point = result.definition_point;
|
||||||
|
result.base.insertion_point = result.definition_point;
|
||||||
|
result.base.actual_measurement = result.measurement_degrees();
|
||||||
|
Dimension::Angular3Pt(result)
|
||||||
|
}
|
||||||
|
BaselineKind::Ordinate {
|
||||||
|
source,
|
||||||
|
leader_delta,
|
||||||
|
} => {
|
||||||
|
let mut result = DimensionOrdinate::new(
|
||||||
|
v3(point),
|
||||||
|
v3(point + *leader_delta),
|
||||||
|
source.is_ordinate_type_x,
|
||||||
|
);
|
||||||
|
result.definition_point = source.definition_point;
|
||||||
|
result.base.definition_point = source.base.definition_point;
|
||||||
|
result.base.text_middle_point = result.leader_endpoint;
|
||||||
|
result.base.insertion_point = result.leader_endpoint;
|
||||||
|
Dimension::Ordinate(result)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
state.style.apply(dimension.base_mut());
|
||||||
|
dimension.base_mut().common.handle = Handle::NULL;
|
||||||
|
dimension.base_mut().block_name.clear();
|
||||||
|
Some(dimension)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn advance(&mut self, direction: f64) {
|
||||||
|
let Some(state) = self.base.as_mut() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
match &mut state.kind {
|
||||||
|
BaselineKind::Linear {
|
||||||
|
next_offset,
|
||||||
|
increment,
|
||||||
|
..
|
||||||
|
} => *next_offset += *increment * direction,
|
||||||
|
BaselineKind::Angular2Ln {
|
||||||
|
next_radius,
|
||||||
|
increment,
|
||||||
|
..
|
||||||
|
}
|
||||||
|
| BaselineKind::Angular3Pt {
|
||||||
|
next_radius,
|
||||||
|
increment,
|
||||||
|
..
|
||||||
|
} => *next_radius = (*next_radius + *increment * direction).max(1.0e-9),
|
||||||
|
BaselineKind::Ordinate { .. } => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl DimBaselineCommand {
|
fn undo_one(&mut self) -> Option<CmdResult> {
|
||||||
/// The two dimension-line endpoints for the new baseline at `p2`: the fixed
|
if self.committed == 0 {
|
||||||
/// base origin and `p2` each projected INDEPENDENTLY onto the dim line (at
|
return Some(CmdResult::NeedPoint);
|
||||||
/// the current stacking offset). Projecting both keeps the line straight
|
}
|
||||||
/// even when the two origins aren't level — a shared offset tilts it. (#181)
|
self.committed -= 1;
|
||||||
fn dim_line_pts(&self, p2: DVec3) -> (DVec3, DVec3) {
|
self.advance(-1.0);
|
||||||
let target = self.base_p1.dot(self.perp) + self.next_offset;
|
Some(CmdResult::UndoDocument)
|
||||||
let d1 = self.base_p1 + self.perp * (target - self.base_p1.dot(self.perp));
|
|
||||||
let d2 = p2 + self.perp * (target - p2.dot(self.perp));
|
|
||||||
(d1, d2)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,104 +330,346 @@ impl CadCommand for DimBaselineCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn prompt(&self) -> String {
|
fn prompt(&self) -> String {
|
||||||
if !self.ready {
|
if self.base.is_none() {
|
||||||
t!("DIMBASELINE No base dimension found. Place a dimension first.").into_owned()
|
"DIMBASELINE Select base dimension (Enter to exit):".to_string()
|
||||||
} else {
|
} else {
|
||||||
t!("DIMBASELINE Specify a second extension line origin (Enter to exit):").into_owned()
|
"DIMBASELINE Specify next extension origin [Select/Undo] <Select>:".to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_point(&mut self, pt: DVec3) -> CmdResult {
|
fn options(&self) -> Vec<CmdOption> {
|
||||||
if !self.ready {
|
self.base.as_ref().map_or_else(Vec::new, |_| {
|
||||||
return CmdResult::Cancel;
|
vec![CmdOption::new("Select", "S"), CmdOption::new("Undo", "U")]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn needs_entity_pick(&self) -> bool {
|
||||||
|
self.base.is_none()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn entity_pick_highlights_hover(&self) -> bool {
|
||||||
|
self.base.is_none()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_before_entity_pick(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_picked_entity(&mut self, entity: EntityType) {
|
||||||
|
self.injected = Some(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_entity_pick(&mut self, _handle: Handle, point: DVec3) -> CmdResult {
|
||||||
|
let Some(entity) = self.injected.take() else {
|
||||||
|
return CmdResult::NeedPoint;
|
||||||
|
};
|
||||||
|
let _ = self.install_base(&entity, Some(point));
|
||||||
|
CmdResult::NeedPoint
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_point(&mut self, point: DVec3) -> CmdResult {
|
||||||
|
let Some(dimension) = self.build_dimension(point) else {
|
||||||
|
return CmdResult::NeedPoint;
|
||||||
|
};
|
||||||
|
self.committed += 1;
|
||||||
|
self.advance(1.0);
|
||||||
|
CmdResult::CommitDimension {
|
||||||
|
entity: EntityType::Dimension(dimension),
|
||||||
|
association: DimensionAssociationInput::Infer(None),
|
||||||
|
preserve_base_style: self.preserve_base_style,
|
||||||
}
|
}
|
||||||
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;
|
|
||||||
if self.text_rotation.abs() > 1e-9 {
|
|
||||||
dim.base.text_rotation = self.text_rotation;
|
|
||||||
}
|
|
||||||
|
|
||||||
let (dim_line_pt, dim_line_pt2) = self.dim_line_pts(p2);
|
|
||||||
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 += self.dimdli;
|
|
||||||
|
|
||||||
CmdResult::CommitEntity(EntityType::Dimension(Dimension::Linear(dim)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_enter(&mut self) -> CmdResult {
|
fn on_enter(&mut self) -> CmdResult {
|
||||||
CmdResult::Cancel
|
if self.base.take().is_some() {
|
||||||
}
|
self.committed = 0;
|
||||||
|
CmdResult::NeedPoint
|
||||||
fn on_mouse_move(&mut self, pt: DVec3) -> Option<WireModel> {
|
} else {
|
||||||
if !self.ready {
|
CmdResult::Cancel
|
||||||
return None;
|
|
||||||
}
|
}
|
||||||
// dim_line_pts stays in f64; downcast only here, into the preview's
|
}
|
||||||
// [f32;3] GPU buffer — correct pixel-space, never touches committed geometry.
|
|
||||||
let (dim_line_pt, dim_line_pt2) = self.dim_line_pts(pt);
|
fn wants_text_input(&self) -> bool {
|
||||||
let p1 = self.base_p1.as_vec3();
|
self.base.is_some()
|
||||||
let dim_line_pt = dim_line_pt.as_vec3();
|
}
|
||||||
let dim_line_pt2 = dim_line_pt2.as_vec3();
|
|
||||||
let pt = pt.as_vec3();
|
fn point_step_accepts_keywords(&self) -> bool {
|
||||||
Some(WireModel {
|
self.base.is_some()
|
||||||
point_marker: None,
|
}
|
||||||
taper_widths: Vec::new(),
|
|
||||||
pattern_stations: Vec::new(),
|
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
|
||||||
world_width: 0.0,
|
match text.trim().to_ascii_uppercase().as_str() {
|
||||||
depth_override: None,
|
"S" | "SELECT" => {
|
||||||
display_visible: true,
|
self.base = None;
|
||||||
plot_visible: true,
|
self.committed = 0;
|
||||||
fill_is_3d: false,
|
Some(CmdResult::NeedPoint)
|
||||||
fill_is_2d_solid: false,
|
}
|
||||||
render_instance: None,
|
"U" | "UNDO" => self.undo_one(),
|
||||||
pick_tris: Vec::new(),
|
_ => Some(CmdResult::NeedPoint),
|
||||||
pick_tris_low: Vec::new(),
|
}
|
||||||
dash_from_start: false,
|
}
|
||||||
dash_align_end: None,
|
|
||||||
text_verts: Vec::new(),
|
fn on_undo_step(&mut self) -> Option<CmdResult> {
|
||||||
name: "dimbase_preview".into(),
|
self.undo_one()
|
||||||
points: vec![
|
}
|
||||||
[p1.x, p1.y, p1.z],
|
|
||||||
[dim_line_pt.x, dim_line_pt.y, dim_line_pt.z],
|
fn on_mouse_move(&mut self, point: DVec3) -> Option<WireModel> {
|
||||||
[f32::NAN, 0.0, 0.0],
|
self.build_dimension(point).map(|dimension| preview_for_dimension(&dimension))
|
||||||
[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],
|
|
||||||
],
|
|
||||||
points_low: Vec::new(),
|
|
||||||
color: WireModel::CYAN,
|
|
||||||
selected: false,
|
|
||||||
pattern_length: 0.0,
|
|
||||||
pattern: [0.0; 8],
|
|
||||||
line_weight_px: 1.0,
|
|
||||||
snap_pts: vec![],
|
|
||||||
tangent_geoms: vec![],
|
|
||||||
aci: 0,
|
|
||||||
key_vertices: vec![],
|
|
||||||
aabb: WireModel::UNBOUNDED_AABB,
|
|
||||||
plinegen: true,
|
|
||||||
fill_tris: vec![],
|
|
||||||
fill_tris_low: Vec::new(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn v3(p: DVec3) -> Vector3 {
|
fn linear_base(
|
||||||
Vector3::new(p.x, p.y, p.z)
|
fixed: DVec3,
|
||||||
|
rotation: f64,
|
||||||
|
aligned: bool,
|
||||||
|
definition: DVec3,
|
||||||
|
increment: f64,
|
||||||
|
) -> BaselineKind {
|
||||||
|
let perpendicular = DVec3::new(-rotation.sin(), rotation.cos(), 0.0);
|
||||||
|
let offset = (definition - fixed).dot(perpendicular);
|
||||||
|
let increment = increment * if offset >= 0.0 { 1.0 } else { -1.0 };
|
||||||
|
BaselineKind::Linear {
|
||||||
|
fixed,
|
||||||
|
rotation,
|
||||||
|
aligned,
|
||||||
|
perpendicular,
|
||||||
|
next_offset: offset + increment,
|
||||||
|
increment,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn build_linear(
|
||||||
|
fixed: DVec3,
|
||||||
|
point: DVec3,
|
||||||
|
rotation: f64,
|
||||||
|
aligned: bool,
|
||||||
|
perpendicular: DVec3,
|
||||||
|
offset: f64,
|
||||||
|
) -> Dimension {
|
||||||
|
let target = fixed.dot(perpendicular) + offset;
|
||||||
|
let first_line = fixed + perpendicular * (target - fixed.dot(perpendicular));
|
||||||
|
let second_line = point + perpendicular * (target - point.dot(perpendicular));
|
||||||
|
if aligned {
|
||||||
|
let mut result = DimensionAligned::new(v3(fixed), v3(point));
|
||||||
|
result.definition_point = v3(first_line);
|
||||||
|
result.base.definition_point = v3(first_line);
|
||||||
|
result.base.text_middle_point = v3((first_line + second_line) * 0.5);
|
||||||
|
result.base.insertion_point = result.base.text_middle_point;
|
||||||
|
result.base.actual_measurement = result.measurement();
|
||||||
|
Dimension::Aligned(result)
|
||||||
|
} else {
|
||||||
|
let mut result = DimensionLinear::new(v3(fixed), v3(point));
|
||||||
|
result.rotation = rotation;
|
||||||
|
result.definition_point = v3(first_line);
|
||||||
|
result.base.definition_point = v3(first_line);
|
||||||
|
result.base.text_middle_point = v3((first_line + second_line) * 0.5);
|
||||||
|
result.base.insertion_point = result.base.text_middle_point;
|
||||||
|
result.base.actual_measurement = result.measurement();
|
||||||
|
Dimension::Linear(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ── Autocomplete registry ─────────────────────────────────
|
fn nearest_end(first: Vector3, second: Vector3, pick: Option<DVec3>) -> (DVec3, DVec3) {
|
||||||
inventory::submit!(crate::command::CommandRegistration { names: &["DIMBASELINE"] }); // DimBaselineCommand
|
let first = dv(first);
|
||||||
|
let second = dv(second);
|
||||||
|
if pick.is_some_and(|point| point.distance_squared(second) < point.distance_squared(first)) {
|
||||||
|
(second, first)
|
||||||
|
} else {
|
||||||
|
(first, second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn nearest_direction(first: DVec3, second: DVec3, vertex: DVec3, pick: Option<DVec3>) -> DVec3 {
|
||||||
|
if let Some(point) = pick {
|
||||||
|
let direction = normalized_or(point - vertex, DVec3::X);
|
||||||
|
if normalized_or(second, DVec3::Y).dot(direction)
|
||||||
|
> normalized_or(first, DVec3::X).dot(direction)
|
||||||
|
{
|
||||||
|
return second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
first
|
||||||
|
}
|
||||||
|
|
||||||
|
fn angular2_vertex(source: &DimensionAngular2Ln) -> DVec3 {
|
||||||
|
let first = dv(source.first_point);
|
||||||
|
let first_direction = dv(source.second_point) - first;
|
||||||
|
let second = dv(source.angle_vertex);
|
||||||
|
let second_direction = dv(source.definition_point) - second;
|
||||||
|
line_intersection(first, first_direction, second, second_direction).unwrap_or(second)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn line_intersection(a: DVec3, b: DVec3, c: DVec3, d: DVec3) -> Option<DVec3> {
|
||||||
|
let cross = b.x * d.y - b.y * d.x;
|
||||||
|
if cross.abs() <= 1.0e-12 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let delta = c - a;
|
||||||
|
Some(a + b * ((delta.x * d.y - delta.y * d.x) / cross))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn angular_bisector(first: DVec3, second: DVec3) -> DVec3 {
|
||||||
|
let first = normalized_or(first, DVec3::X);
|
||||||
|
normalized_or(
|
||||||
|
first + normalized_or(second, DVec3::Y),
|
||||||
|
DVec3::new(-first.y, first.x, 0.0),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn normalized_or(value: DVec3, fallback: DVec3) -> DVec3 {
|
||||||
|
if value.length_squared() > 1.0e-18 {
|
||||||
|
value.normalize()
|
||||||
|
} else {
|
||||||
|
fallback
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn preview_for_dimension(dimension: &Dimension) -> WireModel {
|
||||||
|
let mut points = Vec::<[f32; 3]>::new();
|
||||||
|
match dimension {
|
||||||
|
Dimension::Linear(dimension) => linear_preview(
|
||||||
|
&mut points,
|
||||||
|
dv(dimension.first_point),
|
||||||
|
dv(dimension.second_point),
|
||||||
|
dimension.rotation,
|
||||||
|
dv(dimension.definition_point),
|
||||||
|
),
|
||||||
|
Dimension::Aligned(dimension) => {
|
||||||
|
let first = dv(dimension.first_point);
|
||||||
|
let second = dv(dimension.second_point);
|
||||||
|
let delta = second - first;
|
||||||
|
linear_preview(
|
||||||
|
&mut points,
|
||||||
|
first,
|
||||||
|
second,
|
||||||
|
delta.y.atan2(delta.x),
|
||||||
|
dv(dimension.definition_point),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Dimension::Angular2Ln(dimension) => angular_preview(
|
||||||
|
&mut points,
|
||||||
|
angular2_vertex(dimension),
|
||||||
|
dv(dimension.second_point) - angular2_vertex(dimension),
|
||||||
|
dv(dimension.definition_point) - angular2_vertex(dimension),
|
||||||
|
dv(dimension.dimension_arc),
|
||||||
|
),
|
||||||
|
Dimension::Angular3Pt(dimension) => angular_preview(
|
||||||
|
&mut points,
|
||||||
|
dv(dimension.angle_vertex),
|
||||||
|
dv(dimension.first_point) - dv(dimension.angle_vertex),
|
||||||
|
dv(dimension.second_point) - dv(dimension.angle_vertex),
|
||||||
|
dv(dimension.definition_point),
|
||||||
|
),
|
||||||
|
Dimension::Ordinate(dimension) => segment(
|
||||||
|
&mut points,
|
||||||
|
dv(dimension.feature_location),
|
||||||
|
dv(dimension.leader_endpoint),
|
||||||
|
),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
WireModel {
|
||||||
|
point_marker: None,
|
||||||
|
taper_widths: Vec::new(),
|
||||||
|
pattern_stations: Vec::new(),
|
||||||
|
world_width: 0.0,
|
||||||
|
depth_override: None,
|
||||||
|
display_visible: true,
|
||||||
|
plot_visible: true,
|
||||||
|
fill_is_3d: false,
|
||||||
|
fill_is_2d_solid: false,
|
||||||
|
render_instance: None,
|
||||||
|
pick_tris: Vec::new(),
|
||||||
|
pick_tris_low: Vec::new(),
|
||||||
|
dash_from_start: false,
|
||||||
|
dash_align_end: None,
|
||||||
|
text_verts: Vec::new(),
|
||||||
|
name: "dimbaseline_preview".into(),
|
||||||
|
points,
|
||||||
|
points_low: Vec::new(),
|
||||||
|
color: WireModel::CYAN,
|
||||||
|
selected: false,
|
||||||
|
pattern_length: 0.0,
|
||||||
|
pattern: [0.0; 8],
|
||||||
|
line_weight_px: 1.0,
|
||||||
|
snap_pts: Vec::new(),
|
||||||
|
tangent_geoms: Vec::new(),
|
||||||
|
aci: 0,
|
||||||
|
key_vertices: Vec::new(),
|
||||||
|
aabb: WireModel::UNBOUNDED_AABB,
|
||||||
|
plinegen: true,
|
||||||
|
fill_tris: Vec::new(),
|
||||||
|
fill_tris_low: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn linear_preview(
|
||||||
|
points: &mut Vec<[f32; 3]>,
|
||||||
|
first: DVec3,
|
||||||
|
second: DVec3,
|
||||||
|
rotation: f64,
|
||||||
|
definition: DVec3,
|
||||||
|
) {
|
||||||
|
let perpendicular = DVec3::new(-rotation.sin(), rotation.cos(), 0.0);
|
||||||
|
let target = definition.dot(perpendicular);
|
||||||
|
let first_line = first + perpendicular * (target - first.dot(perpendicular));
|
||||||
|
let second_line = second + perpendicular * (target - second.dot(perpendicular));
|
||||||
|
segment(points, first, first_line);
|
||||||
|
segment(points, second, second_line);
|
||||||
|
segment(points, first_line, second_line);
|
||||||
|
append_arrows(points, first_line, second_line);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn segment(points: &mut Vec<[f32; 3]>, first: DVec3, second: DVec3) {
|
||||||
|
points.push(first.as_vec3().to_array());
|
||||||
|
points.push(second.as_vec3().to_array());
|
||||||
|
points.push([f32::NAN, 0.0, 0.0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn append_arrows(points: &mut Vec<[f32; 3]>, first: DVec3, second: DVec3) {
|
||||||
|
let axis = normalized_or(second - first, DVec3::X);
|
||||||
|
let normal = DVec3::new(-axis.y, axis.x, 0.0);
|
||||||
|
let size = first.distance(second).clamp(1.0, 100.0) * 0.035;
|
||||||
|
for (tip, inward) in [(first, axis), (second, -axis)] {
|
||||||
|
let back = tip + inward * size;
|
||||||
|
segment(points, tip, back + normal * size * 0.4);
|
||||||
|
segment(points, tip, back - normal * size * 0.4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn angular_preview(
|
||||||
|
points: &mut Vec<[f32; 3]>,
|
||||||
|
vertex: DVec3,
|
||||||
|
first: DVec3,
|
||||||
|
second: DVec3,
|
||||||
|
arc_point: DVec3,
|
||||||
|
) {
|
||||||
|
let radius = (arc_point - vertex).length().max(1.0e-9);
|
||||||
|
let start = first.y.atan2(first.x);
|
||||||
|
let mut sweep = second.y.atan2(second.x) - start;
|
||||||
|
while sweep <= -std::f64::consts::PI {
|
||||||
|
sweep += std::f64::consts::TAU;
|
||||||
|
}
|
||||||
|
while sweep > std::f64::consts::PI {
|
||||||
|
sweep -= std::f64::consts::TAU;
|
||||||
|
}
|
||||||
|
segment(points, vertex, vertex + normalized_or(first, DVec3::X) * radius);
|
||||||
|
segment(points, vertex, vertex + normalized_or(second, DVec3::Y) * radius);
|
||||||
|
for index in 0..=24 {
|
||||||
|
let angle = start + sweep * index as f64 / 24.0;
|
||||||
|
points.push(
|
||||||
|
(vertex + DVec3::new(angle.cos(), angle.sin(), 0.0) * radius)
|
||||||
|
.as_vec3()
|
||||||
|
.to_array(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn dv(point: Vector3) -> DVec3 {
|
||||||
|
DVec3::new(point.x, point.y, point.z)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn v3(point: DVec3) -> Vector3 {
|
||||||
|
Vector3::new(point.x, point.y, point.z)
|
||||||
|
}
|
||||||
|
|
||||||
|
inventory::submit!(crate::command::CommandRegistration { names: &["DIMBASELINE"] });
|
||||||
Reference in new issue
Block a user