forked from 1b-agent/opencadstudio-upstream
fix(dimension): repair QDIM workflows
This commit is contained in:
commit
c206c19759
33 files changed
+3572
-1261
No files matched your search
@@ -135,6 +135,8 @@ impl CadCommand for AlignedDimensionCommand {
|
||||
CmdResult::CommitDimension {
|
||||
entity: self.plane.place_entity(EntityType::Dimension(Dimension::Aligned(dim))),
|
||||
association: DimensionAssociationInput::Infer(self.source_handle),
|
||||
preserve_base_style: false,
|
||||
continue_command: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,6 +234,8 @@ impl AngularDimensionCommand {
|
||||
CmdResult::CommitDimension {
|
||||
entity,
|
||||
association: DimensionAssociationInput::Explicit(self.source_refs.clone()),
|
||||
preserve_base_style: false,
|
||||
continue_command: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -265,6 +265,8 @@ impl ArcLengthDimensionCommand {
|
||||
CmdResult::CommitDimension {
|
||||
entity: plane.place_entity(EntityType::Dimension(Dimension::Arc(dimension))),
|
||||
association,
|
||||
preserve_base_style: false,
|
||||
continue_command: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,6 +113,8 @@ impl CadCommand for DiameterDimensionCommand {
|
||||
entity: source_plane
|
||||
.place_entity(EntityType::Dimension(Dimension::Diameter(dim))),
|
||||
association: DimensionAssociationInput::Infer(self.source_handle),
|
||||
preserve_base_style: false,
|
||||
continue_command: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,20 +1,19 @@
|
||||
// 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::entities::{
|
||||
Dimension, DimensionAngular2Ln, DimensionAngular3Pt, DimensionBase, DimensionLinear,
|
||||
DimensionOrdinate,
|
||||
};
|
||||
use acadrust::types::Vector3;
|
||||
use acadrust::EntityType;
|
||||
use glam::{DVec3, Vec3};
|
||||
use acadrust::{EntityType, Handle};
|
||||
use cadkernel::geom2d::{
|
||||
arc_span, closest_point, intersect, nearest_of, Arc, Curve, Ray, Tolerance, Transform, Vec2,
|
||||
XLine,
|
||||
};
|
||||
use cadkernel::space::Plane;
|
||||
use glam::DVec3;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::command::{CadCommand, CmdOption, CmdResult, DimensionAssociationInput};
|
||||
use crate::modules::{IconKind, ModuleEvent, ToolDef};
|
||||
use crate::scene::model::wire_model::WireModel;
|
||||
use crate::t;
|
||||
|
||||
pub const ICON: IconKind = IconKind::Svg(include_bytes!("../../../assets/icons/dim_continue.svg"));
|
||||
|
||||
@@ -27,74 +26,292 @@ pub fn tool() -> ToolDef {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DimContinueCommand {
|
||||
/// Fixed first-extension-line origin for the current step (moves each iteration).
|
||||
chain_p1: DVec3,
|
||||
/// Direction along the dimension axis (0.0 = horizontal, PI/2 = vertical).
|
||||
rotation: f64,
|
||||
/// Text reading rotation inherited from the base dim so a UCS-aligned chain
|
||||
/// keeps its text consistent with the originating DIMLINEAR. 0 = natural.
|
||||
#[derive(Clone)]
|
||||
struct SourceStyle {
|
||||
layer: String,
|
||||
style_name: String,
|
||||
normal: Vector3,
|
||||
text_rotation: f64,
|
||||
/// Absolute perpendicular coordinate (dot with `perp`) of the base
|
||||
/// dimension line. Each continued dim line is projected onto this exact
|
||||
/// coordinate so the whole chain stays collinear — even when the extension
|
||||
/// origins sit at different perpendicular positions (extension lines of
|
||||
/// different lengths). (#151)
|
||||
dim_line_perp: f64,
|
||||
/// Direction of "up" perpendicular to the dim axis (points toward the dim line).
|
||||
perp: DVec3,
|
||||
/// True once we have a base dimension loaded.
|
||||
ready: bool,
|
||||
horizontal_direction: f64,
|
||||
}
|
||||
|
||||
impl DimContinueCommand {
|
||||
/// No base dim found — will show an error prompt and cancel immediately.
|
||||
pub fn new() -> Self {
|
||||
impl SourceStyle {
|
||||
fn from_base(base: &DimensionBase) -> Self {
|
||||
Self {
|
||||
chain_p1: DVec3::ZERO,
|
||||
rotation: 0.0,
|
||||
text_rotation: 0.0,
|
||||
dim_line_perp: 0.0,
|
||||
perp: DVec3::Y,
|
||||
ready: false,
|
||||
layer: base.common.layer.clone(),
|
||||
style_name: base.style_name.clone(),
|
||||
normal: base.normal,
|
||||
text_rotation: base.text_rotation,
|
||||
horizontal_direction: base.horizontal_direction,
|
||||
}
|
||||
}
|
||||
|
||||
/// Build from the last placed dimension.
|
||||
///
|
||||
/// `p2` — second extension line origin of the base dim (the chain starts
|
||||
/// here). `p1` is unused — the dim line position comes from
|
||||
/// `definition_point` alone — but kept for signature parity with the
|
||||
/// shared `find_last_linear_dim` tuple (DIMBASELINE uses p1).
|
||||
/// `definition_point` — where the dim line was placed; its perpendicular
|
||||
/// coordinate is the line the whole chain stays collinear with.
|
||||
/// `rotation` — 0.0 = horizontal dim, PI/2 = vertical dim.
|
||||
pub fn from_base(
|
||||
_p1: Vec3,
|
||||
p2: Vec3,
|
||||
definition_point: Vec3,
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum ContinueKind {
|
||||
Linear {
|
||||
current: Vec2,
|
||||
rotation: f64,
|
||||
text_rotation: f64,
|
||||
) -> Self {
|
||||
// Widen the base-dim points to f64 so the committed chain math runs in
|
||||
// full precision. (The base points arrive as f32 from the entity, but
|
||||
// every derived committed coordinate must stay f64 from here on.)
|
||||
let p2 = p2.as_dvec3();
|
||||
let definition_point = definition_point.as_dvec3();
|
||||
// Axis unit vector along the measurement direction — the base dim's
|
||||
// rotation angle (any angle, incl. a UCS-aligned one), not a world H/V.
|
||||
let axis = DVec3::new(rotation.cos(), rotation.sin(), 0.0);
|
||||
// Perpendicular unit vector toward the dim line.
|
||||
let perp = DVec3::new(-axis.y, axis.x, 0.0);
|
||||
let dim_line_perp = definition_point.dot(perp);
|
||||
Self {
|
||||
chain_p1: p2,
|
||||
rotation,
|
||||
text_rotation,
|
||||
dim_line_perp,
|
||||
perp,
|
||||
ready: true,
|
||||
perpendicular: Vec2,
|
||||
line_coordinate: f64,
|
||||
},
|
||||
Angular2Ln {
|
||||
vertex: Vec2,
|
||||
current: Vec2,
|
||||
radius: f64,
|
||||
},
|
||||
Angular3Pt {
|
||||
vertex: Vec2,
|
||||
current: Vec2,
|
||||
radius: f64,
|
||||
},
|
||||
Ordinate {
|
||||
definition: Vec2,
|
||||
leader: Vec2,
|
||||
is_x: bool,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct ContinueState {
|
||||
kind: ContinueKind,
|
||||
style: SourceStyle,
|
||||
plane: Plane,
|
||||
}
|
||||
|
||||
pub struct DimContinueCommand {
|
||||
base: Option<ContinueState>,
|
||||
injected: Option<EntityType>,
|
||||
history: Vec<ContinueState>,
|
||||
preserve_base_style: bool,
|
||||
}
|
||||
|
||||
impl DimContinueCommand {
|
||||
pub fn new(recent: Option<EntityType>, preserve_base_style: bool) -> Self {
|
||||
let mut command = Self {
|
||||
base: None,
|
||||
injected: None,
|
||||
history: Vec::new(),
|
||||
preserve_base_style,
|
||||
};
|
||||
if let Some(entity) = recent {
|
||||
let _ = command.install_base(&entity, None);
|
||||
}
|
||||
command
|
||||
}
|
||||
|
||||
fn install_base(&mut self, entity: &EntityType, pick_world: Option<DVec3>) -> bool {
|
||||
let EntityType::Dimension(dimension) = entity else {
|
||||
return false;
|
||||
};
|
||||
let style = SourceStyle::from_base(dimension.base());
|
||||
let plane = plane_from_normal(dimension.base().definition_point, style.normal);
|
||||
let pick = pick_world.and_then(|point| plane.project(point.to_array()).map(Vec2::from));
|
||||
let kind = match dimension {
|
||||
Dimension::Linear(source) => {
|
||||
let rotation = source.rotation;
|
||||
let perpendicular = Vec2::new(-rotation.sin(), rotation.cos());
|
||||
ContinueKind::Linear {
|
||||
current: selected_linear_end(
|
||||
project(&plane, source.first_point),
|
||||
project(&plane, source.second_point),
|
||||
pick,
|
||||
),
|
||||
rotation,
|
||||
perpendicular,
|
||||
line_coordinate: project(&plane, source.base.definition_point)
|
||||
.dot(perpendicular),
|
||||
}
|
||||
}
|
||||
Dimension::Aligned(source) => {
|
||||
let first = project(&plane, source.first_point);
|
||||
let second = project(&plane, source.second_point);
|
||||
let Some(direction) = (second - first).normalize() else {
|
||||
return false;
|
||||
};
|
||||
let rotation = direction.angle();
|
||||
let perpendicular = direction.perpendicular();
|
||||
ContinueKind::Linear {
|
||||
current: selected_linear_end(first, second, pick),
|
||||
rotation,
|
||||
perpendicular,
|
||||
line_coordinate: project(&plane, source.base.definition_point)
|
||||
.dot(perpendicular),
|
||||
}
|
||||
}
|
||||
Dimension::Angular2Ln(source) => {
|
||||
let lines = [
|
||||
[
|
||||
project(&plane, source.first_point),
|
||||
project(&plane, source.second_point),
|
||||
],
|
||||
[
|
||||
project(&plane, source.angle_vertex),
|
||||
project(&plane, source.definition_point),
|
||||
],
|
||||
];
|
||||
let vertex = angular2_vertex(lines).unwrap_or(lines[1][0]);
|
||||
ContinueKind::Angular2Ln {
|
||||
vertex,
|
||||
current: selected_angular_end(vertex, [lines[0][1], lines[1][1]], pick),
|
||||
radius: project(&plane, source.dimension_arc)
|
||||
.distance(vertex)
|
||||
.max(1.0e-9),
|
||||
}
|
||||
}
|
||||
Dimension::Angular3Pt(source) => {
|
||||
let vertex = project(&plane, source.angle_vertex);
|
||||
ContinueKind::Angular3Pt {
|
||||
vertex,
|
||||
current: selected_angular_end(
|
||||
vertex,
|
||||
[
|
||||
project(&plane, source.first_point),
|
||||
project(&plane, source.second_point),
|
||||
],
|
||||
pick,
|
||||
),
|
||||
radius: project(&plane, source.definition_point)
|
||||
.distance(vertex)
|
||||
.max(1.0e-9),
|
||||
}
|
||||
}
|
||||
Dimension::Ordinate(source) => ContinueKind::Ordinate {
|
||||
definition: project(&plane, source.definition_point),
|
||||
leader: project(&plane, source.leader_endpoint),
|
||||
is_x: source.is_ordinate_type_x,
|
||||
},
|
||||
_ => return false,
|
||||
};
|
||||
self.base = Some(ContinueState { kind, style, plane });
|
||||
true
|
||||
}
|
||||
|
||||
fn build_dimension(&self, point_world: DVec3) -> Option<Dimension> {
|
||||
let state = self.base.as_ref()?;
|
||||
let point = state.plane.project(point_world.to_array()).map(Vec2::from)?;
|
||||
let mut dimension = match &state.kind {
|
||||
ContinueKind::Linear {
|
||||
current,
|
||||
rotation,
|
||||
perpendicular,
|
||||
line_coordinate,
|
||||
} => build_linear(
|
||||
state.plane,
|
||||
*current,
|
||||
point,
|
||||
*rotation,
|
||||
*perpendicular,
|
||||
*line_coordinate,
|
||||
),
|
||||
ContinueKind::Angular2Ln {
|
||||
vertex,
|
||||
current,
|
||||
radius,
|
||||
} => {
|
||||
let previous = *current - *vertex;
|
||||
let moving = point - *vertex;
|
||||
let (definition, text) =
|
||||
angular_definition_and_text(*vertex, previous, moving, *radius)?;
|
||||
let mut result = DimensionAngular2Ln::default();
|
||||
result.first_point = world(state.plane, *vertex);
|
||||
result.second_point = world(state.plane, *current);
|
||||
result.angle_vertex = world(state.plane, *vertex);
|
||||
result.definition_point = world(state.plane, point);
|
||||
result.dimension_arc = world(state.plane, definition);
|
||||
result.base.definition_point = result.dimension_arc;
|
||||
result.base.text_middle_point = world(state.plane, text);
|
||||
result.base.insertion_point = result.base.text_middle_point;
|
||||
Dimension::Angular2Ln(result)
|
||||
}
|
||||
ContinueKind::Angular3Pt {
|
||||
vertex,
|
||||
current,
|
||||
radius,
|
||||
} => {
|
||||
let previous = *current - *vertex;
|
||||
let moving = point - *vertex;
|
||||
let (definition, text) =
|
||||
angular_definition_and_text(*vertex, previous, moving, *radius)?;
|
||||
let mut result = DimensionAngular3Pt::new(
|
||||
world(state.plane, *vertex),
|
||||
world(state.plane, *current),
|
||||
world(state.plane, point),
|
||||
);
|
||||
result.definition_point = world(state.plane, definition);
|
||||
result.base.definition_point = result.definition_point;
|
||||
result.base.text_middle_point = world(state.plane, text);
|
||||
result.base.insertion_point = result.base.text_middle_point;
|
||||
Dimension::Angular3Pt(result)
|
||||
}
|
||||
ContinueKind::Ordinate {
|
||||
definition,
|
||||
leader,
|
||||
is_x,
|
||||
} => {
|
||||
let to_dimension = Transform::rotation(-state.style.horizontal_direction);
|
||||
let from_dimension = Transform::rotation(state.style.horizontal_direction);
|
||||
let feature = Vec2::from(to_dimension.apply_point(point.into()));
|
||||
let source_leader = Vec2::from(to_dimension.apply_point((*leader).into()));
|
||||
let leader = if *is_x {
|
||||
Vec2::new(feature.x, source_leader.y)
|
||||
} else {
|
||||
Vec2::new(source_leader.x, feature.y)
|
||||
};
|
||||
let leader = Vec2::from(from_dimension.apply_point(leader.into()));
|
||||
let mut result = DimensionOrdinate::new(
|
||||
world(state.plane, point),
|
||||
world(state.plane, leader),
|
||||
*is_x,
|
||||
);
|
||||
result.definition_point = world(state.plane, *definition);
|
||||
result.base.definition_point = result.definition_point;
|
||||
result.base.text_middle_point = result.leader_endpoint;
|
||||
result.base.insertion_point = result.base.text_middle_point;
|
||||
Dimension::Ordinate(result)
|
||||
}
|
||||
};
|
||||
state.style.apply(dimension.base_mut());
|
||||
refresh_measurement(&mut dimension, state.plane);
|
||||
dimension.base_mut().common.handle = Handle::NULL;
|
||||
dimension.base_mut().block_name.clear();
|
||||
Some(dimension)
|
||||
}
|
||||
|
||||
fn advance(&mut self, point_world: DVec3) {
|
||||
let Some(point) = self
|
||||
.base
|
||||
.as_ref()
|
||||
.and_then(|state| state.plane.project(point_world.to_array()))
|
||||
.map(Vec2::from)
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let Some(state) = self.base.as_mut() else {
|
||||
return;
|
||||
};
|
||||
match &mut state.kind {
|
||||
ContinueKind::Linear { current, .. }
|
||||
| ContinueKind::Angular2Ln { current, .. }
|
||||
| ContinueKind::Angular3Pt { current, .. } => *current = point,
|
||||
ContinueKind::Ordinate { .. } => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn undo_one(&mut self) -> CmdResult {
|
||||
let Some(previous) = self.history.pop() else {
|
||||
return CmdResult::NeedPoint;
|
||||
};
|
||||
self.base = Some(previous);
|
||||
CmdResult::UndoDocument
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,110 +321,521 @@ impl CadCommand for DimContinueCommand {
|
||||
}
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
if !self.ready {
|
||||
t!("DIMCONTINUE No base dimension found. Place a dimension first.").into_owned()
|
||||
if self.base.is_none() {
|
||||
"DIMCONTINUE Select continued dimension:".to_string()
|
||||
} else if self
|
||||
.base
|
||||
.as_ref()
|
||||
.is_some_and(|base| matches!(&base.kind, ContinueKind::Ordinate { .. }))
|
||||
{
|
||||
"DIMCONTINUE Specify feature location [Undo/Select] <Select>:".to_string()
|
||||
} else {
|
||||
t!("DIMCONTINUE Specify a second extension line origin (Enter to exit):").into_owned()
|
||||
"DIMCONTINUE Specify second extension line origin [Select/Undo] <Select>:"
|
||||
.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
fn on_point(&mut self, pt: DVec3) -> CmdResult {
|
||||
if !self.ready {
|
||||
return CmdResult::Cancel;
|
||||
fn options(&self) -> Vec<CmdOption> {
|
||||
self.base.as_ref().map_or_else(Vec::new, |base| {
|
||||
if matches!(&base.kind, ContinueKind::Ordinate { .. }) {
|
||||
vec![CmdOption::new("Undo", "U"), CmdOption::new("Select", "S")]
|
||||
} else {
|
||||
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;
|
||||
};
|
||||
if let Some(base) = self.base.clone() {
|
||||
self.history.push(base);
|
||||
}
|
||||
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;
|
||||
if self.text_rotation.abs() > 1e-9 {
|
||||
dim.base.text_rotation = self.text_rotation;
|
||||
self.advance(point);
|
||||
CmdResult::CommitDimension {
|
||||
entity: EntityType::Dimension(dimension),
|
||||
association: DimensionAssociationInput::Infer(None),
|
||||
preserve_base_style: self.preserve_base_style,
|
||||
continue_command: true,
|
||||
}
|
||||
|
||||
// Dim line stays collinear with the base: project both extension
|
||||
// origins onto the base dim line's absolute perpendicular coordinate.
|
||||
// (A fixed offset from p1 drifts off the base line whenever p1 and p2
|
||||
// sit at different perpendicular positions.) (#151)
|
||||
let d1 = p1 + self.perp * (self.dim_line_perp - p1.dot(self.perp));
|
||||
let d2 = p2 + self.perp * (self.dim_line_perp - p2.dot(self.perp));
|
||||
dim.definition_point = v3(d1);
|
||||
dim.base.definition_point = v3(d1);
|
||||
dim.base.text_middle_point = v3((d1 + d2) * 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: DVec3) -> Option<WireModel> {
|
||||
if !self.ready {
|
||||
return None;
|
||||
if self.base.take().is_some() {
|
||||
CmdResult::NeedPoint
|
||||
} else {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
// Preview / rubber-band geometry feeds an f32 GPU buffer, so drop to f32
|
||||
// at this screen-only boundary.
|
||||
let pt = pt.as_vec3();
|
||||
let p1 = self.chain_p1.as_vec3();
|
||||
let perp = self.perp.as_vec3();
|
||||
let dim_line_perp = self.dim_line_perp as f32;
|
||||
let dim_line_pt = p1 + perp * (dim_line_perp - p1.dot(perp));
|
||||
let dim_line_pt2 = pt + perp * (dim_line_perp - pt.dot(perp));
|
||||
Some(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: "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],
|
||||
],
|
||||
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 wants_text_input(&self) -> bool {
|
||||
self.base.is_some()
|
||||
}
|
||||
|
||||
fn point_step_accepts_keywords(&self) -> bool {
|
||||
self.base.is_some()
|
||||
}
|
||||
|
||||
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
|
||||
match text.trim().to_ascii_uppercase().as_str() {
|
||||
"S" | "SELECT" => {
|
||||
self.base = None;
|
||||
Some(CmdResult::NeedPoint)
|
||||
}
|
||||
"U" | "UNDO" => Some(self.undo_one()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn on_undo_step(&mut self) -> Option<CmdResult> {
|
||||
if self.history.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(self.undo_one())
|
||||
}
|
||||
}
|
||||
|
||||
fn on_mouse_move(&mut self, point: DVec3) -> Option<WireModel> {
|
||||
self.build_dimension(point)
|
||||
.map(|dimension| preview_for_dimension(&dimension))
|
||||
}
|
||||
}
|
||||
|
||||
fn v3(p: DVec3) -> Vector3 {
|
||||
Vector3::new(p.x, p.y, p.z)
|
||||
fn build_linear(
|
||||
plane: Plane,
|
||||
first: Vec2,
|
||||
second: Vec2,
|
||||
rotation: f64,
|
||||
perpendicular: Vec2,
|
||||
line_coordinate: f64,
|
||||
) -> Dimension {
|
||||
let first_line = project_to_line(first, perpendicular, line_coordinate);
|
||||
let second_line = project_to_line(second, perpendicular, line_coordinate);
|
||||
let mut result = DimensionLinear::new(world(plane, first), world(plane, second));
|
||||
result.rotation = rotation;
|
||||
result.definition_point = world(plane, second_line);
|
||||
result.base.definition_point = result.definition_point;
|
||||
result.base.text_middle_point = world(plane, first_line.lerp(second_line, 0.5));
|
||||
result.base.insertion_point = result.base.text_middle_point;
|
||||
Dimension::Linear(result)
|
||||
}
|
||||
|
||||
fn selected_linear_end(first: Vec2, second: Vec2, pick: Option<Vec2>) -> Vec2 {
|
||||
pick.map_or(second, |point| {
|
||||
if point.distance_squared(first) <= point.distance_squared(second) {
|
||||
first
|
||||
} else {
|
||||
second
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// ── Autocomplete registry ─────────────────────────────────
|
||||
inventory::submit!(crate::command::CommandRegistration { names: &["DIMCONTINUE"] }); // DimContinueCommand
|
||||
fn selected_angular_end(vertex: Vec2, ends: [Vec2; 2], pick: Option<Vec2>) -> Vec2 {
|
||||
let Some(pick) = pick else {
|
||||
return ends[0];
|
||||
};
|
||||
let rays = ends.map(|end| {
|
||||
Curve::Ray(Ray {
|
||||
origin: vertex.into(),
|
||||
direction: (end - vertex).into(),
|
||||
})
|
||||
});
|
||||
nearest_of(rays.iter(), pick.into())
|
||||
.map(|(index, _)| ends[index])
|
||||
.unwrap_or(ends[0])
|
||||
}
|
||||
|
||||
fn angular2_vertex(lines: [[Vec2; 2]; 2]) -> Option<Vec2> {
|
||||
let first = Curve::XLine(XLine {
|
||||
base: lines[0][0].into(),
|
||||
direction: (lines[0][1] - lines[0][0]).into(),
|
||||
});
|
||||
let second = Curve::XLine(XLine {
|
||||
base: lines[1][0].into(),
|
||||
direction: (lines[1][1] - lines[1][0]).into(),
|
||||
});
|
||||
let scale = (lines[0][1] - lines[0][0])
|
||||
.length()
|
||||
.max((lines[1][1] - lines[1][0]).length())
|
||||
.max(1.0);
|
||||
intersect(&first, &second, Tolerance::new(scale * 1.0e-9))
|
||||
.first()
|
||||
.map(|crossing| Vec2::from(crossing.point))
|
||||
}
|
||||
|
||||
fn angular_definition_and_text(
|
||||
vertex: Vec2,
|
||||
previous: Vec2,
|
||||
moving: Vec2,
|
||||
radius: f64,
|
||||
) -> Option<(Vec2, Vec2)> {
|
||||
let arc = angular_arc(vertex, previous, moving, radius)?;
|
||||
Some((
|
||||
Vec2::from(arc.point_at(2.0 / 3.0)),
|
||||
Vec2::from(arc.point_at(0.5)),
|
||||
))
|
||||
}
|
||||
|
||||
fn angular_arc(vertex: Vec2, first: Vec2, second: Vec2, radius: f64) -> Option<Curve> {
|
||||
let first = first.normalize()?;
|
||||
let second = second.normalize()?;
|
||||
if first.distance_squared(second) <= 1.0e-18 {
|
||||
return None;
|
||||
}
|
||||
let first_angle = first.angle();
|
||||
let second_angle = second.angle();
|
||||
let (start_angle, end_angle) =
|
||||
if arc_span(first_angle, second_angle) <= std::f64::consts::PI {
|
||||
(first_angle, second_angle)
|
||||
} else {
|
||||
(second_angle, first_angle)
|
||||
};
|
||||
Some(Curve::Arc(Arc {
|
||||
centre: vertex.into(),
|
||||
radius,
|
||||
start_angle,
|
||||
end_angle,
|
||||
}))
|
||||
}
|
||||
|
||||
fn refresh_measurement(dimension: &mut Dimension, plane: Plane) {
|
||||
match dimension {
|
||||
Dimension::Linear(value) => {
|
||||
let first = project(&plane, value.first_point);
|
||||
let second = project(&plane, value.second_point);
|
||||
let axis = Vec2::new(value.rotation.cos(), value.rotation.sin());
|
||||
value.base.actual_measurement = (second - first).dot(axis).abs();
|
||||
}
|
||||
Dimension::Angular2Ln(value) => {
|
||||
value.base.actual_measurement = value.measurement_degrees()
|
||||
}
|
||||
Dimension::Angular3Pt(value) => {
|
||||
value.base.actual_measurement = value.measurement_degrees()
|
||||
}
|
||||
Dimension::Ordinate(value) => value.refresh_measurement(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn preview_for_dimension(dimension: &Dimension) -> WireModel {
|
||||
let mut points = Vec::<[f64; 3]>::new();
|
||||
match dimension {
|
||||
Dimension::Linear(value) => {
|
||||
let plane = plane_from_normal(value.first_point, value.base.normal);
|
||||
linear_preview(
|
||||
&mut points,
|
||||
plane,
|
||||
project(&plane, value.first_point),
|
||||
project(&plane, value.second_point),
|
||||
value.rotation,
|
||||
project(&plane, value.definition_point),
|
||||
project(&plane, value.base.text_middle_point),
|
||||
value.base.actual_measurement,
|
||||
);
|
||||
}
|
||||
Dimension::Angular2Ln(value) => {
|
||||
let plane = plane_from_normal(value.first_point, value.base.normal);
|
||||
let lines = [
|
||||
[
|
||||
project(&plane, value.first_point),
|
||||
project(&plane, value.second_point),
|
||||
],
|
||||
[
|
||||
project(&plane, value.angle_vertex),
|
||||
project(&plane, value.definition_point),
|
||||
],
|
||||
];
|
||||
let vertex = angular2_vertex(lines).unwrap_or(lines[1][0]);
|
||||
angular_preview(
|
||||
&mut points,
|
||||
plane,
|
||||
vertex,
|
||||
lines[0][1] - vertex,
|
||||
lines[1][1] - vertex,
|
||||
project(&plane, value.dimension_arc),
|
||||
project(&plane, value.base.text_middle_point),
|
||||
value.base.actual_measurement,
|
||||
);
|
||||
}
|
||||
Dimension::Angular3Pt(value) => {
|
||||
let plane = plane_from_normal(value.angle_vertex, value.base.normal);
|
||||
let vertex = project(&plane, value.angle_vertex);
|
||||
angular_preview(
|
||||
&mut points,
|
||||
plane,
|
||||
vertex,
|
||||
project(&plane, value.first_point) - vertex,
|
||||
project(&plane, value.second_point) - vertex,
|
||||
project(&plane, value.definition_point),
|
||||
project(&plane, value.base.text_middle_point),
|
||||
value.base.actual_measurement,
|
||||
);
|
||||
}
|
||||
Dimension::Ordinate(value) => {
|
||||
let plane = plane_from_normal(value.definition_point, value.base.normal);
|
||||
ordinate_preview(
|
||||
&mut points,
|
||||
plane,
|
||||
project(&plane, value.feature_location),
|
||||
project(&plane, value.leader_endpoint),
|
||||
value.base.actual_measurement,
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
WireModel::solid_f64(
|
||||
"dimcontinue_preview".to_string(),
|
||||
points,
|
||||
WireModel::CYAN,
|
||||
false,
|
||||
)
|
||||
}
|
||||
|
||||
fn linear_preview(
|
||||
points: &mut Vec<[f64; 3]>,
|
||||
plane: Plane,
|
||||
first: Vec2,
|
||||
second: Vec2,
|
||||
rotation: f64,
|
||||
definition: Vec2,
|
||||
text: Vec2,
|
||||
measurement: f64,
|
||||
) {
|
||||
let axis = Vec2::new(rotation.cos(), rotation.sin());
|
||||
let perpendicular = axis.perpendicular();
|
||||
let coordinate = definition.dot(perpendicular);
|
||||
let first_line = project_to_line(first, perpendicular, coordinate);
|
||||
let second_line = project_to_line(second, perpendicular, coordinate);
|
||||
segment(points, plane, first, first_line);
|
||||
segment(points, plane, second, second_line);
|
||||
segment(points, plane, first_line, second_line);
|
||||
arrow(
|
||||
points,
|
||||
plane,
|
||||
first_line,
|
||||
axis,
|
||||
perpendicular,
|
||||
first_line.distance(second_line),
|
||||
);
|
||||
arrow(
|
||||
points,
|
||||
plane,
|
||||
second_line,
|
||||
-axis,
|
||||
perpendicular,
|
||||
first_line.distance(second_line),
|
||||
);
|
||||
text_box(points, plane, text, axis, perpendicular, measurement);
|
||||
}
|
||||
|
||||
fn angular_preview(
|
||||
points: &mut Vec<[f64; 3]>,
|
||||
plane: Plane,
|
||||
vertex: Vec2,
|
||||
first: Vec2,
|
||||
second: Vec2,
|
||||
arc_point: Vec2,
|
||||
text: Vec2,
|
||||
measurement: f64,
|
||||
) {
|
||||
let radius = arc_point.distance(vertex).max(1.0e-9);
|
||||
let Some(first_direction) = first.normalize() else {
|
||||
return;
|
||||
};
|
||||
let Some(second_direction) = second.normalize() else {
|
||||
return;
|
||||
};
|
||||
let Some(arc) = angular_arc(vertex, first, second, radius) else {
|
||||
return;
|
||||
};
|
||||
segment(points, plane, vertex, vertex + first_direction * radius);
|
||||
segment(points, plane, vertex, vertex + second_direction * radius);
|
||||
for index in 0..=24 {
|
||||
push_local(points, plane, Vec2::from(arc.point_at(index as f64 / 24.0)));
|
||||
}
|
||||
points.push([f64::NAN; 3]);
|
||||
let start = Vec2::from(arc.point_at(0.0));
|
||||
let start_next = Vec2::from(arc.point_at(1.0 / 1024.0));
|
||||
let end = Vec2::from(arc.point_at(1.0));
|
||||
let end_previous = Vec2::from(arc.point_at(1.0 - 1.0 / 1024.0));
|
||||
arrow(
|
||||
points,
|
||||
plane,
|
||||
start,
|
||||
(start_next - start).normalize().unwrap_or(first_direction),
|
||||
first_direction,
|
||||
radius,
|
||||
);
|
||||
arrow(
|
||||
points,
|
||||
plane,
|
||||
end,
|
||||
(end_previous - end).normalize().unwrap_or(-second_direction),
|
||||
second_direction,
|
||||
radius,
|
||||
);
|
||||
let before_middle = Vec2::from(arc.point_at(0.5 - 1.0 / 1024.0));
|
||||
let after_middle = Vec2::from(arc.point_at(0.5 + 1.0 / 1024.0));
|
||||
let text_axis = (after_middle - before_middle)
|
||||
.normalize()
|
||||
.unwrap_or(Vec2::new(1.0, 0.0));
|
||||
text_box(
|
||||
points,
|
||||
plane,
|
||||
text,
|
||||
text_axis,
|
||||
text_axis.perpendicular(),
|
||||
measurement,
|
||||
);
|
||||
}
|
||||
|
||||
fn ordinate_preview(
|
||||
points: &mut Vec<[f64; 3]>,
|
||||
plane: Plane,
|
||||
feature: Vec2,
|
||||
leader: Vec2,
|
||||
measurement: f64,
|
||||
) {
|
||||
segment(points, plane, feature, leader);
|
||||
let axis = (leader - feature)
|
||||
.normalize()
|
||||
.unwrap_or(Vec2::new(0.0, 1.0));
|
||||
let perpendicular = axis.perpendicular();
|
||||
arrow(
|
||||
points,
|
||||
plane,
|
||||
feature,
|
||||
axis,
|
||||
perpendicular,
|
||||
feature.distance(leader),
|
||||
);
|
||||
text_box(
|
||||
points,
|
||||
plane,
|
||||
leader,
|
||||
perpendicular,
|
||||
axis,
|
||||
measurement,
|
||||
);
|
||||
}
|
||||
|
||||
fn arrow(
|
||||
points: &mut Vec<[f64; 3]>,
|
||||
plane: Plane,
|
||||
tip: Vec2,
|
||||
inward: Vec2,
|
||||
perpendicular: Vec2,
|
||||
span: f64,
|
||||
) {
|
||||
let inward = inward.normalize().unwrap_or(Vec2::new(1.0, 0.0));
|
||||
let perpendicular = perpendicular
|
||||
.normalize()
|
||||
.unwrap_or(Vec2::new(0.0, 1.0));
|
||||
let size = span.clamp(1.0, 100.0) * 0.035;
|
||||
let back = tip + inward * size;
|
||||
segment(
|
||||
points,
|
||||
plane,
|
||||
tip,
|
||||
back + perpendicular * size * 0.4,
|
||||
);
|
||||
segment(
|
||||
points,
|
||||
plane,
|
||||
tip,
|
||||
back - perpendicular * size * 0.4,
|
||||
);
|
||||
}
|
||||
|
||||
fn text_box(
|
||||
points: &mut Vec<[f64; 3]>,
|
||||
plane: Plane,
|
||||
center: Vec2,
|
||||
axis: Vec2,
|
||||
perpendicular: Vec2,
|
||||
measurement: f64,
|
||||
) {
|
||||
let digits = if measurement.abs() < 1.0 {
|
||||
1.0
|
||||
} else {
|
||||
measurement.abs().log10().floor() + 1.0
|
||||
};
|
||||
let half_width = (digits + 2.0) * 0.12;
|
||||
let half_height = 0.16;
|
||||
let a = center - axis * half_width - perpendicular * half_height;
|
||||
let b = center + axis * half_width - perpendicular * half_height;
|
||||
let c = center + axis * half_width + perpendicular * half_height;
|
||||
let d = center - axis * half_width + perpendicular * half_height;
|
||||
for point in [a, b, c, d, a] {
|
||||
push_local(points, plane, point);
|
||||
}
|
||||
points.push([f64::NAN; 3]);
|
||||
}
|
||||
|
||||
fn project_to_line(point: Vec2, perpendicular: Vec2, coordinate: f64) -> Vec2 {
|
||||
let line = Curve::XLine(XLine {
|
||||
base: (perpendicular * coordinate).into(),
|
||||
direction: perpendicular.perpendicular().into(),
|
||||
});
|
||||
Vec2::from(closest_point(&line, point.into()).point)
|
||||
}
|
||||
|
||||
fn plane_from_normal(origin: Vector3, normal: Vector3) -> Plane {
|
||||
let (x_axis, y_axis) =
|
||||
crate::scene::view::transform::ocs_axes((normal.x, normal.y, normal.z));
|
||||
Plane::from_axes(
|
||||
point(origin),
|
||||
[x_axis.0, x_axis.1, x_axis.2],
|
||||
[y_axis.0, y_axis.1, y_axis.2],
|
||||
)
|
||||
}
|
||||
|
||||
fn project(plane: &Plane, value: Vector3) -> Vec2 {
|
||||
Vec2::from(plane.project(point(value)).unwrap_or([0.0; 2]))
|
||||
}
|
||||
|
||||
fn world(plane: Plane, value: Vec2) -> Vector3 {
|
||||
let value = plane.point_at(value.into());
|
||||
Vector3::new(value[0], value[1], value[2])
|
||||
}
|
||||
|
||||
fn point(value: Vector3) -> [f64; 3] {
|
||||
[value.x, value.y, value.z]
|
||||
}
|
||||
|
||||
fn segment(points: &mut Vec<[f64; 3]>, plane: Plane, first: Vec2, second: Vec2) {
|
||||
push_local(points, plane, first);
|
||||
push_local(points, plane, second);
|
||||
points.push([f64::NAN; 3]);
|
||||
}
|
||||
|
||||
fn push_local(points: &mut Vec<[f64; 3]>, plane: Plane, point: Vec2) {
|
||||
points.push(plane.point_at(point.into()));
|
||||
}
|
||||
|
||||
inventory::submit!(crate::command::CommandRegistration { names: &["DIMCONTINUE"] });
|
||||
@@ -139,6 +139,8 @@ impl JoggedRadiusDimensionCommand {
|
||||
dimension,
|
||||
))),
|
||||
association,
|
||||
preserve_base_style: false,
|
||||
continue_command: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,6 +187,8 @@ impl CadCommand for LinearDimensionCommand {
|
||||
CmdResult::CommitDimension {
|
||||
entity,
|
||||
association: DimensionAssociationInput::Infer(self.source_handle),
|
||||
preserve_base_style: false,
|
||||
continue_command: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,6 +116,8 @@ impl CadCommand for OrdinateDimCommand {
|
||||
Dimension::Ordinate(dim),
|
||||
)),
|
||||
association: DimensionAssociationInput::Infer(None),
|
||||
preserve_base_style: false,
|
||||
continue_command: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,10 +85,8 @@ impl QdimCommand {
|
||||
for crossing in intersect(&curves[first].curve, &curves[second].curve, Tolerance::default()) {
|
||||
hits.push(Candidate {
|
||||
point: DVec3::new(crossing.point[0], crossing.point[1], 0.0),
|
||||
// An intersection depends on two source curves. The
|
||||
// current association record stores one source per
|
||||
// extension origin, so do not bind it to an
|
||||
// unrelated endpoint marker.
|
||||
// Intersections depend on two curves, but each
|
||||
// extension origin stores one source.
|
||||
sources: Vec::new(),
|
||||
});
|
||||
}
|
||||
@@ -180,8 +178,11 @@ impl QdimCommand {
|
||||
let datum = self.datum.unwrap_or(points[0].point);
|
||||
let datum_candidate = Candidate {
|
||||
point: datum,
|
||||
sources: points.iter().min_by(|first, second| first.point.distance(datum).total_cmp(&second.point.distance(datum)))
|
||||
.map(|point| point.sources.clone()).unwrap_or_default(),
|
||||
sources: points
|
||||
.iter()
|
||||
.find(|point| point.point.distance(datum) <= tolerance)
|
||||
.map(|point| point.sources.clone())
|
||||
.unwrap_or_default(),
|
||||
};
|
||||
points.iter().filter(|point| point.point.distance(datum) > tolerance)
|
||||
.map(|point| self.linear_dimension(&datum_candidate, point, place, horizontal)).collect()
|
||||
@@ -347,13 +348,16 @@ impl CadCommand for QdimCommand {
|
||||
if self.step == Step::Edit {
|
||||
let size = self.dim_spacing * 0.12;
|
||||
return self.candidates().into_iter().enumerate().flat_map(|(index, candidate)| {
|
||||
let point = self.plane.to_world(candidate.point);
|
||||
let x_start = self.plane.to_world(candidate.point - DVec3::X * size);
|
||||
let x_end = self.plane.to_world(candidate.point + DVec3::X * size);
|
||||
let y_start = self.plane.to_world(candidate.point - DVec3::Y * size);
|
||||
let y_end = self.plane.to_world(candidate.point + DVec3::Y * size);
|
||||
[
|
||||
WireModel::solid_f64(format!("qdim_edit_{index}_x"), vec![
|
||||
[point.x - size, point.y, point.z], [point.x + size, point.y, point.z],
|
||||
x_start.to_array(), x_end.to_array(),
|
||||
], WireModel::CYAN, false),
|
||||
WireModel::solid_f64(format!("qdim_edit_{index}_y"), vec![
|
||||
[point.x, point.y - size, point.z], [point.x, point.y + size, point.z],
|
||||
y_start.to_array(), y_end.to_array(),
|
||||
], WireModel::CYAN, false),
|
||||
]
|
||||
}).collect();
|
||||
|
||||
@@ -111,6 +111,8 @@ impl CadCommand for RadiusDimensionCommand {
|
||||
CmdResult::CommitDimension {
|
||||
entity: source_plane.place_entity(EntityType::Dimension(Dimension::Radius(dim))),
|
||||
association: DimensionAssociationInput::Infer(self.source_handle),
|
||||
preserve_base_style: false,
|
||||
continue_command: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -356,6 +356,38 @@ fn dim_seg(a: Vector3, b: Vector3, common: &acadrust::entities::EntityCommon) ->
|
||||
})
|
||||
}
|
||||
|
||||
fn dim_geom_entities(
|
||||
geometry: &crate::scene::convert::tessellate::DimGeom,
|
||||
ext_common: &acadrust::entities::EntityCommon,
|
||||
dim_common: &acadrust::entities::EntityCommon,
|
||||
) -> Vec<EntityType> {
|
||||
let mut entities = Vec::new();
|
||||
let point = |value: [f32; 3]| {
|
||||
Vector3::new(value[0] as f64, value[1] as f64, value[2] as f64)
|
||||
};
|
||||
for (points, common) in [
|
||||
(geometry.ext_lines.as_slice(), ext_common),
|
||||
(geometry.dim_lines.as_slice(), dim_common),
|
||||
] {
|
||||
for run in points.split(|point| point[0].is_nan()) {
|
||||
for pair in run.windows(2) {
|
||||
entities.push(dim_seg(point(pair[0]), point(pair[1]), common));
|
||||
}
|
||||
}
|
||||
}
|
||||
for triangle in geometry.arrow_fill.chunks_exact(3) {
|
||||
let mut solid = acadrust::entities::Solid::triangle(
|
||||
point(triangle[0]),
|
||||
point(triangle[1]),
|
||||
point(triangle[2]),
|
||||
);
|
||||
solid.common = dim_common.clone();
|
||||
solid.common.handle = Handle::NULL;
|
||||
entities.push(EntityType::Solid(solid));
|
||||
}
|
||||
entities
|
||||
}
|
||||
|
||||
/// A dimension-line terminator at `tip`, body extending back along the unit
|
||||
/// vector `(dx,dy)` (toward the dim line). When DIMTSZ>0 it's an oblique 45°
|
||||
/// tick; otherwise a closed *filled* arrowhead (DXF SOLID) of length DIMASZ —
|
||||
@@ -597,80 +629,23 @@ struct DimMetrics {
|
||||
dimclrt: i16,
|
||||
dimlwd: i16,
|
||||
dimlwe: i16,
|
||||
/// Resolved terminator shapes for the first / second end (DIMTSZ tick,
|
||||
/// DIMBLK/DIMBLK1/DIMBLK2 per DIMSAH, else closed-filled), so the bake
|
||||
/// reproduces the style's actual arrow type.
|
||||
dimltype: Handle,
|
||||
dimltex1: Handle,
|
||||
arrow1: crate::scene::convert::tessellate::ArrowKind,
|
||||
arrow2: crate::scene::convert::tessellate::ArrowKind,
|
||||
}
|
||||
|
||||
/// Metrics from the dim's style, mirroring what the live renderer applies, so a
|
||||
/// baked block reproduces the same gaps, arrow type, suppression, colours and
|
||||
/// lineweights: DIMASZ (arrow), DIMCEN (centre mark), DIMEXO/DIMEXE (extension
|
||||
/// gap/overshoot), DIMTSZ (oblique tick; >0 = ticks not arrows), DIMDLE (dim
|
||||
/// line overshoot past ticks), DIMSE1/2 + DIMSD1/2 (extension / dim-line
|
||||
/// suppression), DIMCLRD/E/T (colours) and DIMLWD/E (lineweights).
|
||||
/// Resolve style metrics used by saved dimension geometry.
|
||||
fn dim_metrics(dim: &Dimension, doc: &CadDocument) -> DimMetrics {
|
||||
let name = dim.base().style_name.as_str();
|
||||
let mut effective_style = doc.dim_styles.iter().find(|s| {
|
||||
s.name.eq_ignore_ascii_case(name)
|
||||
|| (name.trim().is_empty() && s.name.eq_ignore_ascii_case("Standard"))
|
||||
}).cloned();
|
||||
if let Some(style) = &mut effective_style {
|
||||
use crate::entities::dim_override as ov;
|
||||
let data = &dim.base().common.extended_data;
|
||||
macro_rules! real {
|
||||
($field:ident, $code:ident) => {
|
||||
if let Some(value) = ov::real(data, ov::$code) {
|
||||
style.$field = value;
|
||||
}
|
||||
};
|
||||
}
|
||||
macro_rules! int {
|
||||
($field:ident, $code:ident) => {
|
||||
if let Some(value) = ov::int(data, ov::$code) {
|
||||
style.$field = value;
|
||||
}
|
||||
};
|
||||
}
|
||||
macro_rules! flag {
|
||||
($field:ident, $code:ident) => {
|
||||
if let Some(value) = ov::int(data, ov::$code) {
|
||||
style.$field = value != 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
macro_rules! handle {
|
||||
($field:ident, $code:ident) => {
|
||||
if let Some(value) = ov::handle(data, ov::$code) {
|
||||
style.$field = value;
|
||||
}
|
||||
};
|
||||
}
|
||||
real!(dimscale, DIMSCALE);
|
||||
real!(dimasz, DIMASZ);
|
||||
real!(dimcen, DIMCEN);
|
||||
real!(dimexo, DIMEXO);
|
||||
real!(dimexe, DIMEXE);
|
||||
real!(dimtsz, DIMTSZ);
|
||||
real!(dimdle, DIMDLE);
|
||||
real!(dimfxl, DIMFXL);
|
||||
flag!(dimfxlon, DIMFXLON);
|
||||
flag!(dimse1, DIMSE1);
|
||||
flag!(dimse2, DIMSE2);
|
||||
flag!(dimsd1, DIMSD1);
|
||||
flag!(dimsd2, DIMSD2);
|
||||
flag!(dimsoxd, DIMSOXD);
|
||||
flag!(dimsah, DIMSAH);
|
||||
int!(dimclrd, DIMCLRD);
|
||||
int!(dimclre, DIMCLRE);
|
||||
int!(dimclrt, DIMCLRT);
|
||||
int!(dimlwd, DIMLWD);
|
||||
int!(dimlwe, DIMLWE);
|
||||
handle!(dimblk, DIMBLK);
|
||||
handle!(dimblk1, DIMBLK1);
|
||||
handle!(dimblk2, DIMBLK2);
|
||||
}
|
||||
let effective_style = doc
|
||||
.dim_styles
|
||||
.iter()
|
||||
.find(|style| {
|
||||
style.name.eq_ignore_ascii_case(name)
|
||||
|| (name.trim().is_empty() && style.name.eq_ignore_ascii_case("Standard"))
|
||||
})
|
||||
.map(|style| crate::entities::dimension::resolved_dimension_style(style, dim, doc));
|
||||
let style = effective_style.as_ref();
|
||||
let scale = style
|
||||
.map(|s| if s.dimscale > 1e-6 { s.dimscale } else { 1.0 })
|
||||
@@ -679,14 +654,14 @@ fn dim_metrics(dim: &Dimension, doc: &CadDocument) -> DimMetrics {
|
||||
let dimasz = style.map(|s| s.dimasz * scale).unwrap_or(0.18 * scale).max(1e-6);
|
||||
let dimtsz = style.map(|s| s.dimtsz * scale).unwrap_or(0.0);
|
||||
let asz = dimasz as f32;
|
||||
// Resolve the terminator shapes exactly like the live render: ticks when
|
||||
// DIMTSZ>0, otherwise the DIMBLK / DIMBLK1+DIMBLK2 (per DIMSAH) arrow blocks,
|
||||
// else closed-filled.
|
||||
let (arrow1, arrow2) = if dimtsz > 1e-9 {
|
||||
let t = ArrowKind::Tick { size: dimtsz as f32 };
|
||||
(t.clone(), t)
|
||||
} else if let Some(s) = style {
|
||||
if matches!(dim, Dimension::Diameter(_)) {
|
||||
if matches!(dim, Dimension::Radius(_) | Dimension::LargeRadial(_)) {
|
||||
let arrow = arrow_from_block(doc, s.dimldrblk, asz);
|
||||
(arrow.clone(), arrow)
|
||||
} else if matches!(dim, Dimension::Diameter(_)) {
|
||||
use crate::entities::dim_override as ov;
|
||||
let data = &dim.base().common.extended_data;
|
||||
let first = ov::handle(data, ov::DIMBLK1)
|
||||
@@ -723,6 +698,8 @@ fn dim_metrics(dim: &Dimension, doc: &CadDocument) -> DimMetrics {
|
||||
dimclrt: style.map(|s| s.dimclrt).unwrap_or(0),
|
||||
dimlwd: style.map(|s| s.dimlwd).unwrap_or(-2),
|
||||
dimlwe: style.map(|s| s.dimlwe).unwrap_or(-2),
|
||||
dimltype: style.map(|s| s.dimltex_handle).unwrap_or(Handle::NULL),
|
||||
dimltex1: style.map(|s| s.dimltex1_handle).unwrap_or(Handle::NULL),
|
||||
arrow1,
|
||||
arrow2,
|
||||
}
|
||||
@@ -744,6 +721,21 @@ fn dim_common(base: &acadrust::entities::EntityCommon, clr: i16, lw: i16) -> aca
|
||||
c
|
||||
}
|
||||
|
||||
fn with_dim_linetype(
|
||||
mut common: acadrust::entities::EntityCommon,
|
||||
doc: &CadDocument,
|
||||
handle: Handle,
|
||||
) -> acadrust::entities::EntityCommon {
|
||||
common.linetype = doc
|
||||
.line_types
|
||||
.iter()
|
||||
.find(|line_type| line_type.handle == handle)
|
||||
.map(|line_type| line_type.name.clone())
|
||||
.unwrap_or_else(|| "Continuous".to_string());
|
||||
common.linetype_handle = (!handle.is_null()).then_some(handle);
|
||||
common
|
||||
}
|
||||
|
||||
/// Baked geometry for an angular dimension, matching the live render exactly:
|
||||
/// an extension line from each measured ray point flush out to the dimension
|
||||
/// arc, the swept arc, and a terminator at each arc end. (The live angular path
|
||||
@@ -806,49 +798,6 @@ fn angular_block_segs(
|
||||
out
|
||||
}
|
||||
|
||||
fn jogged_radial_break(
|
||||
chord: Vector3,
|
||||
jog: Vector3,
|
||||
override_center: Vector3,
|
||||
jog_angle: f64,
|
||||
) -> (Vector3, Vector3) {
|
||||
let radial = norm2(
|
||||
chord.x - override_center.x,
|
||||
chord.y - override_center.y,
|
||||
1.0,
|
||||
0.0,
|
||||
);
|
||||
let (sin, cos) = jog_angle.sin_cos();
|
||||
let transverse = (
|
||||
radial.0 * cos - radial.1 * sin,
|
||||
radial.0 * sin + radial.1 * cos,
|
||||
);
|
||||
let length = ((chord.x - override_center.x).powi(2)
|
||||
+ (chord.y - override_center.y).powi(2))
|
||||
.sqrt();
|
||||
let half = (length * 0.04).max(1e-6);
|
||||
let first = Vector3::new(
|
||||
jog.x - transverse.0 * half,
|
||||
jog.y - transverse.1 * half,
|
||||
jog.z,
|
||||
);
|
||||
let second = Vector3::new(
|
||||
jog.x + transverse.0 * half,
|
||||
jog.y + transverse.1 * half,
|
||||
jog.z,
|
||||
);
|
||||
let distance_squared = |point: Vector3| {
|
||||
(point.x - chord.x).powi(2)
|
||||
+ (point.y - chord.y).powi(2)
|
||||
+ (point.z - chord.z).powi(2)
|
||||
};
|
||||
if distance_squared(first) <= distance_squared(second) {
|
||||
(first, second)
|
||||
} else {
|
||||
(second, first)
|
||||
}
|
||||
}
|
||||
|
||||
/// The text anchor for a radial leader: the saved text middle point when set,
|
||||
/// else the midpoint of `a` and `b` — mirroring the live `dimension_text_position`.
|
||||
fn dim_text_anchor(
|
||||
@@ -1246,38 +1195,14 @@ fn explode_dimension(dim: &Dimension, doc: &CadDocument) -> Vec<EntityType> {
|
||||
));
|
||||
}
|
||||
}
|
||||
Dimension::LargeRadial(d) => {
|
||||
let (near, far) = jogged_radial_break(
|
||||
d.chord_point,
|
||||
d.jog_point,
|
||||
d.override_center,
|
||||
d.jog_angle,
|
||||
);
|
||||
if !met.dimsd1 {
|
||||
result.push(make_seg(&d.chord_point, &near, &dim_c));
|
||||
result.push(make_seg(&near, &far, &dim_c));
|
||||
result.push(make_seg(&far, &d.override_center, &dim_c));
|
||||
Dimension::LargeRadial(_) => {
|
||||
if let Some(geometry) =
|
||||
crate::entities::dimension::baked_large_radial_geometry(dim, doc)
|
||||
{
|
||||
let radial_ext_c = with_dim_linetype(ext_c.clone(), doc, met.dimltex1);
|
||||
let radial_dim_c = with_dim_linetype(dim_c.clone(), doc, met.dimltype);
|
||||
result.extend(dim_geom_entities(&geometry, &radial_ext_c, &radial_dim_c));
|
||||
}
|
||||
let len = ((near.x - d.chord_point.x).powi(2)
|
||||
+ (near.y - d.chord_point.y).powi(2))
|
||||
.sqrt()
|
||||
.max(1e-12);
|
||||
result.extend(dim_terminator(
|
||||
d.chord_point,
|
||||
(near.x - d.chord_point.x) / len,
|
||||
(near.y - d.chord_point.y) / len,
|
||||
&met.arrow1,
|
||||
&dim_c,
|
||||
));
|
||||
let radius = ((d.definition_point.x - d.chord_point.x).powi(2)
|
||||
+ (d.definition_point.y - d.chord_point.y).powi(2))
|
||||
.sqrt();
|
||||
result.extend(dim_center_mark(
|
||||
d.definition_point,
|
||||
met.dimcen,
|
||||
radius,
|
||||
&dim_c,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1371,23 +1296,7 @@ fn next_dimension_block_name(doc: &CadDocument, next: &mut u64) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// Bake an anonymous `*D<n>` geometry block for every DIMENSION that doesn't
|
||||
/// already own one, so the file is valid for AutoCAD-family readers.
|
||||
///
|
||||
/// OCS renders dimensions by re-tessellating them on the fly and never
|
||||
/// materialises the `*D` block that a DWG `DIMENSION` is supposed to reference
|
||||
/// (the lines / arrows / text that AutoCAD actually draws). A dimension created
|
||||
/// in OCS therefore goes out referencing a block that doesn't exist, and the
|
||||
/// writer emits a null block handle — strict readers (DWG TrueView, QCAD) drop
|
||||
/// the dimension or demand a recovery, and lenient ones (BricsCAD) regenerate it
|
||||
/// at a different position. Call this on the document about to be written so each
|
||||
/// such dimension gets a real block built from its exploded geometry (extension
|
||||
/// lines + dimension line + measurement text, the same decomposition EXPLODE
|
||||
/// uses) and its `block_name` points at it.
|
||||
///
|
||||
/// Dimensions that already reference an existing block (e.g. imported from a real
|
||||
/// DWG, or copied via the `*D`-cloning copy path) are left untouched so their
|
||||
/// original graphics are preserved.
|
||||
/// Build missing anonymous dimension geometry blocks before writing.
|
||||
pub fn bake_dimension_blocks(doc: &mut CadDocument) {
|
||||
// Keep group-10 in step and find missing blocks in one entity pass. Existing
|
||||
// `*D` blocks are the save cache: only invalidated/new dimensions enter the
|
||||
@@ -1451,35 +1360,13 @@ pub fn bake_dimension_blocks(doc: &mut CadDocument) {
|
||||
|
||||
if let Some(EntityType::Dimension(d)) = doc.get_entity_mut(handle) {
|
||||
d.base_mut().block_name = name;
|
||||
// The block we just baked holds the dimension graphics in absolute
|
||||
// WCS, so the DWG group-12 insertion point (base.insertion_point)
|
||||
// MUST be the origin. A reader that positions the *D block by that
|
||||
// point (BricsCAD / ODA) otherwise draws it shifted by the offset,
|
||||
// while OCS — which renders the block in place — shows it correctly.
|
||||
// OCS's dimension commands seed insertion_point with the text
|
||||
// anchor; reset it here so the saved dimension lands identically in
|
||||
// every application. (#181)
|
||||
// Baked geometry uses absolute coordinates.
|
||||
d.base_mut().insertion_point = Vector3::new(0.0, 0.0, 0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Drop a dimension's baked `*D` block so the next save regenerates it from the
|
||||
/// dimension's current definition points / text / style.
|
||||
///
|
||||
/// OCS renders a dimension live from its definition points, but exports a baked
|
||||
/// `*D` block that other applications (BricsCAD / ODA) draw instead. An in-place
|
||||
/// edit — grip drag, DIMTEDIT, restyle, text edit, DIMSPACE — changes the
|
||||
/// definition points while leaving the old block, so without this the export
|
||||
/// keeps the pre-edit graphics and the dimension appears wrong everywhere but in
|
||||
/// OCS. Call this after any such edit; [`bake_dimension_blocks`] then rebuilds a
|
||||
/// fresh block on save.
|
||||
///
|
||||
/// The transform path (MOVE / COPY / PASTE) keeps its own block in sync via
|
||||
/// `define_transformed_block` and must NOT call this. The removed block's record
|
||||
/// and owned entities are deleted too, so re-baking on every edit can't
|
||||
/// accumulate orphan `*D` blocks. No-op when the dimension has no baked block
|
||||
/// yet (the pending path bakes it fresh on save). (#181)
|
||||
/// Drop baked dimension geometry so the next save regenerates it.
|
||||
pub fn invalidate_dim_block(doc: &mut CadDocument, handle: Handle) {
|
||||
let bn = match doc.get_entity(handle) {
|
||||
Some(EntityType::Dimension(d)) => d.base().block_name.clone(),
|
||||
|
||||
Reference in new issue
Block a user