forked from 1b-agent/opencadstudio-upstream
style: apply cargo fmt across workspace
This commit is contained in:
1 parent
85710e5454
commit
9cf9ff2c5c
531 files changed
+57740
-33906
No files matched your search
+216
-79
@@ -1,12 +1,10 @@
|
||||
use super::*;
|
||||
use codec::entities::{
|
||||
CenterLineAssociation, CenterLineSource, CenterLineSourceKind,
|
||||
};
|
||||
use crate::command::WorkingPlane;
|
||||
use codec::entities::{CenterLineAssociation, CenterLineSource, CenterLineSourceKind};
|
||||
use codec::objects::{XRecordEntry, XRecordValue};
|
||||
use codec::types::Vector3;
|
||||
use kernel::geom2d::{centerline_between, closest_point, Curve, Line as KernelLine};
|
||||
use glam::DVec3;
|
||||
use crate::command::WorkingPlane;
|
||||
use kernel::geom2d::{centerline_between, closest_point, Curve, Line as KernelLine};
|
||||
|
||||
const SETTINGS_RECORD: &str = "OPEN_CAD_CENTERLINE_SETTINGS";
|
||||
|
||||
@@ -46,10 +44,8 @@ fn dvec(point: Vector3) -> DVec3 {
|
||||
}
|
||||
|
||||
fn ocs_point(point: (f64, f64, f64), normal: Vector3) -> DVec3 {
|
||||
let point = crate::scene::view::transform::ocs_point_to_wcs(
|
||||
point,
|
||||
(normal.x, normal.y, normal.z),
|
||||
);
|
||||
let point =
|
||||
crate::scene::view::transform::ocs_point_to_wcs(point, (normal.x, normal.y, normal.z));
|
||||
DVec3::new(point.0, point.1, point.2)
|
||||
}
|
||||
|
||||
@@ -97,8 +93,11 @@ pub(crate) fn picked_source(
|
||||
((a.x - b.x).hypot(a.y - b.y) > 1.0e-10).then_some((start, a, b))
|
||||
})
|
||||
.min_by(|(_, a, b), (_, c, d)| {
|
||||
segment_distance(pick_2d, [a.x, a.y], [b.x, b.y])
|
||||
.total_cmp(&segment_distance(pick_2d, [c.x, c.y], [d.x, d.y]))
|
||||
segment_distance(pick_2d, [a.x, a.y], [b.x, b.y]).total_cmp(&segment_distance(
|
||||
pick_2d,
|
||||
[c.x, c.y],
|
||||
[d.x, d.y],
|
||||
))
|
||||
})
|
||||
.map(|(index, a, b)| {
|
||||
let start = ocs_point((a.x, a.y, polyline.elevation), polyline.normal);
|
||||
@@ -129,8 +128,11 @@ pub(crate) fn picked_source(
|
||||
((a.x - b.x).hypot(a.y - b.y) > 1.0e-10).then_some((start, a, b))
|
||||
})
|
||||
.min_by(|(_, a, b), (_, c, d)| {
|
||||
segment_distance(pick_2d, [a.x, a.y], [b.x, b.y])
|
||||
.total_cmp(&segment_distance(pick_2d, [c.x, c.y], [d.x, d.y]))
|
||||
segment_distance(pick_2d, [a.x, a.y], [b.x, b.y]).total_cmp(&segment_distance(
|
||||
pick_2d,
|
||||
[c.x, c.y],
|
||||
[d.x, d.y],
|
||||
))
|
||||
})
|
||||
.map(|(index, a, b)| {
|
||||
let start = ocs_point((a.x, a.y, polyline.elevation), polyline.normal);
|
||||
@@ -151,16 +153,29 @@ pub(crate) fn picked_source(
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_source(document: &codec::CadDocument, source: &CenterLineSource) -> Option<(DVec3, DVec3)> {
|
||||
fn resolve_source(
|
||||
document: &codec::CadDocument,
|
||||
source: &CenterLineSource,
|
||||
) -> Option<(DVec3, DVec3)> {
|
||||
let entity = document.get_entity(source.handle)?;
|
||||
let (_, start, end) = picked_source(entity, source.handle, dvec(source.pick_point))?;
|
||||
match source.kind {
|
||||
CenterLineSourceKind::Line => matches!(entity, EntityType::Line(_)).then_some((start, end)),
|
||||
CenterLineSourceKind::LwPolylineSegment => {
|
||||
let EntityType::LwPolyline(polyline) = entity else { return None; };
|
||||
let EntityType::LwPolyline(polyline) = entity else {
|
||||
return None;
|
||||
};
|
||||
let index = usize::try_from(source.segment_index).ok()?;
|
||||
let end_index = if index + 1 < polyline.vertices.len() { index + 1 } else if polyline.is_closed { 0 } else { return None; };
|
||||
if polyline.vertices.get(index)?.bulge.abs() > 1.0e-12 { return None; }
|
||||
let end_index = if index + 1 < polyline.vertices.len() {
|
||||
index + 1
|
||||
} else if polyline.is_closed {
|
||||
0
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
if polyline.vertices.get(index)?.bulge.abs() > 1.0e-12 {
|
||||
return None;
|
||||
}
|
||||
let a = polyline.vertices[index].location;
|
||||
let b = polyline.vertices[end_index].location;
|
||||
Some((
|
||||
@@ -169,10 +184,20 @@ fn resolve_source(document: &codec::CadDocument, source: &CenterLineSource) -> O
|
||||
))
|
||||
}
|
||||
CenterLineSourceKind::Polyline2DSegment => {
|
||||
let EntityType::Polyline2D(polyline) = entity else { return None; };
|
||||
let EntityType::Polyline2D(polyline) = entity else {
|
||||
return None;
|
||||
};
|
||||
let index = usize::try_from(source.segment_index).ok()?;
|
||||
let end_index = if index + 1 < polyline.vertices.len() { index + 1 } else if polyline.is_closed() { 0 } else { return None; };
|
||||
if polyline.vertices.get(index)?.bulge.abs() > 1.0e-12 { return None; }
|
||||
let end_index = if index + 1 < polyline.vertices.len() {
|
||||
index + 1
|
||||
} else if polyline.is_closed() {
|
||||
0
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
if polyline.vertices.get(index)?.bulge.abs() > 1.0e-12 {
|
||||
return None;
|
||||
}
|
||||
let a = polyline.vertices[index].location;
|
||||
let b = polyline.vertices[end_index].location;
|
||||
Some((
|
||||
@@ -209,8 +234,14 @@ pub(crate) fn construct_line(
|
||||
let first_pick = plane.to_local(dvec(association.first.pick_point));
|
||||
let second_pick = plane.to_local(dvec(association.second.pick_point));
|
||||
let geometry = centerline_between(
|
||||
KernelLine { start: [first_start.x, first_start.y], end: [first_end.x, first_end.y] },
|
||||
KernelLine { start: [second_start.x, second_start.y], end: [second_end.x, second_end.y] },
|
||||
KernelLine {
|
||||
start: [first_start.x, first_start.y],
|
||||
end: [first_end.x, first_end.y],
|
||||
},
|
||||
KernelLine {
|
||||
start: [second_start.x, second_start.y],
|
||||
end: [second_end.x, second_end.y],
|
||||
},
|
||||
[first_pick.x, first_pick.y],
|
||||
[second_pick.x, second_pick.y],
|
||||
association.start_extension + association.start_length_adjustment,
|
||||
@@ -241,23 +272,60 @@ impl Scene {
|
||||
if matches!(self.document.header.insertion_units, 4..=7 | 11..=17) {
|
||||
defaults.extension = 3.5;
|
||||
}
|
||||
let Some(owner) = self.centerline_settings_owner() else { return defaults; };
|
||||
let Some(record) = self.document.xrecord(owner, SETTINGS_RECORD) else { return defaults; };
|
||||
let value = |code| record.entries.iter().find(|entry| entry.code == code).map(|entry| &entry.value);
|
||||
let Some(owner) = self.centerline_settings_owner() else {
|
||||
return defaults;
|
||||
};
|
||||
let Some(record) = self.document.xrecord(owner, SETTINGS_RECORD) else {
|
||||
return defaults;
|
||||
};
|
||||
let value = |code| {
|
||||
record
|
||||
.entries
|
||||
.iter()
|
||||
.find(|entry| entry.code == code)
|
||||
.map(|entry| &entry.value)
|
||||
};
|
||||
CenterLineSettings {
|
||||
extension: value(40).and_then(XRecordValue::as_double).unwrap_or(defaults.extension),
|
||||
layer: value(1).and_then(XRecordValue::as_string).unwrap_or(&defaults.layer).to_owned(),
|
||||
linetype: value(2).and_then(XRecordValue::as_string).unwrap_or(&defaults.linetype).to_owned(),
|
||||
linetype_scale: value(41).and_then(XRecordValue::as_double).unwrap_or(defaults.linetype_scale),
|
||||
linetype_file: value(3).and_then(XRecordValue::as_string).unwrap_or(&defaults.linetype_file).to_owned(),
|
||||
cross_size: value(4).and_then(XRecordValue::as_string).unwrap_or(&defaults.cross_size).to_owned(),
|
||||
cross_gap: value(5).and_then(XRecordValue::as_string).unwrap_or(&defaults.cross_gap).to_owned(),
|
||||
mark_extensions: value(290).and_then(XRecordValue::as_bool).unwrap_or(defaults.mark_extensions),
|
||||
extension: value(40)
|
||||
.and_then(XRecordValue::as_double)
|
||||
.unwrap_or(defaults.extension),
|
||||
layer: value(1)
|
||||
.and_then(XRecordValue::as_string)
|
||||
.unwrap_or(&defaults.layer)
|
||||
.to_owned(),
|
||||
linetype: value(2)
|
||||
.and_then(XRecordValue::as_string)
|
||||
.unwrap_or(&defaults.linetype)
|
||||
.to_owned(),
|
||||
linetype_scale: value(41)
|
||||
.and_then(XRecordValue::as_double)
|
||||
.unwrap_or(defaults.linetype_scale),
|
||||
linetype_file: value(3)
|
||||
.and_then(XRecordValue::as_string)
|
||||
.unwrap_or(&defaults.linetype_file)
|
||||
.to_owned(),
|
||||
cross_size: value(4)
|
||||
.and_then(XRecordValue::as_string)
|
||||
.unwrap_or(&defaults.cross_size)
|
||||
.to_owned(),
|
||||
cross_gap: value(5)
|
||||
.and_then(XRecordValue::as_string)
|
||||
.unwrap_or(&defaults.cross_gap)
|
||||
.to_owned(),
|
||||
mark_extensions: value(290)
|
||||
.and_then(XRecordValue::as_bool)
|
||||
.unwrap_or(defaults.mark_extensions),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn set_centerline_setting(&mut self, name: &str, value: &str) -> Result<String, String> {
|
||||
let Some(owner) = self.centerline_settings_owner() else { return Err("No drawing settings owner.".to_owned()); };
|
||||
pub(crate) fn set_centerline_setting(
|
||||
&mut self,
|
||||
name: &str,
|
||||
value: &str,
|
||||
) -> Result<String, String> {
|
||||
let Some(owner) = self.centerline_settings_owner() else {
|
||||
return Err("No drawing settings owner.".to_owned());
|
||||
};
|
||||
if name == "CENTERLTYPE"
|
||||
&& !value.eq_ignore_ascii_case("Current")
|
||||
&& !self.document.line_types.contains(value)
|
||||
@@ -269,27 +337,35 @@ impl Scene {
|
||||
{
|
||||
let source = std::fs::read_to_string(value)
|
||||
.map_err(|error| format!("CENTERLTYPEFILE: {error}"))?;
|
||||
crate::io::linetypes::populate_document_from_source(
|
||||
&mut self.document,
|
||||
&source,
|
||||
);
|
||||
crate::io::linetypes::populate_document_from_source(&mut self.document, &source);
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
return Err("CENTERLTYPEFILE is unavailable in the web build.".to_owned());
|
||||
}
|
||||
self.document.ensure_xrecord(owner, SETTINGS_RECORD);
|
||||
let record = self.document.xrecord_mut(owner, SETTINGS_RECORD).ok_or_else(|| "Cannot store centerline settings.".to_owned())?;
|
||||
let record = self
|
||||
.document
|
||||
.xrecord_mut(owner, SETTINGS_RECORD)
|
||||
.ok_or_else(|| "Cannot store centerline settings.".to_owned())?;
|
||||
let (code, replacement, display) = match name {
|
||||
"CENTEREXE" => {
|
||||
let number = value.parse::<f64>().map_err(|_| "CENTEREXE requires a non-negative number.".to_owned())?;
|
||||
if !number.is_finite() || number < 0.0 { return Err("CENTEREXE requires a non-negative number.".to_owned()); }
|
||||
let number = value
|
||||
.parse::<f64>()
|
||||
.map_err(|_| "CENTEREXE requires a non-negative number.".to_owned())?;
|
||||
if !number.is_finite() || number < 0.0 {
|
||||
return Err("CENTEREXE requires a non-negative number.".to_owned());
|
||||
}
|
||||
(40, XRecordValue::Double(number), number.to_string())
|
||||
}
|
||||
"CENTERLAYER" => (1, XRecordValue::String(value.to_owned()), value.to_owned()),
|
||||
"CENTERLTYPE" => (2, XRecordValue::String(value.to_owned()), value.to_owned()),
|
||||
"CENTERLTSCALE" => {
|
||||
let number = value.parse::<f64>().map_err(|_| "CENTERLTSCALE requires a non-zero number.".to_owned())?;
|
||||
if !number.is_finite() || number == 0.0 { return Err("CENTERLTSCALE requires a non-zero number.".to_owned()); }
|
||||
let number = value
|
||||
.parse::<f64>()
|
||||
.map_err(|_| "CENTERLTSCALE requires a non-zero number.".to_owned())?;
|
||||
if !number.is_finite() || number == 0.0 {
|
||||
return Err("CENTERLTSCALE requires a non-zero number.".to_owned());
|
||||
}
|
||||
(41, XRecordValue::Double(number), number.to_string())
|
||||
}
|
||||
"CENTERLTYPEFILE" => (3, XRecordValue::String(value.to_owned()), value.to_owned()),
|
||||
@@ -297,14 +373,24 @@ impl Scene {
|
||||
let trimmed = value.trim();
|
||||
let valid = trimmed.eq_ignore_ascii_case("ByLineType")
|
||||
|| trimmed.strip_suffix(['x', 'X']).is_some_and(|number| {
|
||||
number.parse::<f64>().is_ok_and(|value| value.is_finite() && value > 0.0)
|
||||
number
|
||||
.parse::<f64>()
|
||||
.is_ok_and(|value| value.is_finite() && value > 0.0)
|
||||
})
|
||||
|| trimmed.parse::<f64>().is_ok_and(|value| value.is_finite() && value > 0.0);
|
||||
|| trimmed
|
||||
.parse::<f64>()
|
||||
.is_ok_and(|value| value.is_finite() && value > 0.0);
|
||||
if !valid {
|
||||
return Err(format!("{name} requires a positive length, a positive x factor, or ByLineType."));
|
||||
return Err(format!(
|
||||
"{name} requires a positive length, a positive x factor, or ByLineType."
|
||||
));
|
||||
}
|
||||
let code = if name == "CENTERCROSSSIZE" { 4 } else { 5 };
|
||||
(code, XRecordValue::String(trimmed.to_owned()), trimmed.to_owned())
|
||||
(
|
||||
code,
|
||||
XRecordValue::String(trimmed.to_owned()),
|
||||
trimmed.to_owned(),
|
||||
)
|
||||
}
|
||||
"CENTERMARKEXE" => {
|
||||
let enabled = match value.trim().to_ascii_lowercase().as_str() {
|
||||
@@ -312,27 +398,47 @@ impl Scene {
|
||||
"0" | "off" | "no" | "false" => false,
|
||||
_ => return Err("CENTERMARKEXE requires On/Off or 1/0.".to_owned()),
|
||||
};
|
||||
(290, XRecordValue::Bool(enabled), if enabled { "1" } else { "0" }.to_owned())
|
||||
(
|
||||
290,
|
||||
XRecordValue::Bool(enabled),
|
||||
if enabled { "1" } else { "0" }.to_owned(),
|
||||
)
|
||||
}
|
||||
_ => return Err(format!("Unknown centerline setting: {name}")),
|
||||
};
|
||||
if let Some(entry) = record.entries.iter_mut().find(|entry| entry.code == code) {
|
||||
entry.value = replacement;
|
||||
} else {
|
||||
record.entries.push(XRecordEntry { code, value: replacement });
|
||||
record.entries.push(XRecordEntry {
|
||||
code,
|
||||
value: replacement,
|
||||
});
|
||||
}
|
||||
Ok(format!("{name} = {display}"))
|
||||
}
|
||||
|
||||
pub(crate) fn refresh_associative_centerlines(&mut self, changes: &[(Handle, ChangeKind)]) -> Vec<(Handle, ChangeKind)> {
|
||||
pub(crate) fn refresh_associative_centerlines(
|
||||
&mut self,
|
||||
changes: &[(Handle, ChangeKind)],
|
||||
) -> Vec<(Handle, ChangeKind)> {
|
||||
let changed: rustc_hash::FxHashSet<_> = changes.iter().map(|(handle, _)| *handle).collect();
|
||||
if changed.is_empty() { return Vec::new(); }
|
||||
let candidates: Vec<_> = self.document.entities().filter_map(|entity| {
|
||||
let EntityType::Line(line) = entity else { return None; };
|
||||
let association = CenterLineAssociation::read(&line.common.extended_data)?;
|
||||
(association.associated && (changed.contains(&association.first.handle) || changed.contains(&association.second.handle)))
|
||||
if changed.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
let candidates: Vec<_> = self
|
||||
.document
|
||||
.entities()
|
||||
.filter_map(|entity| {
|
||||
let EntityType::Line(line) = entity else {
|
||||
return None;
|
||||
};
|
||||
let association = CenterLineAssociation::read(&line.common.extended_data)?;
|
||||
(association.associated
|
||||
&& (changed.contains(&association.first.handle)
|
||||
|| changed.contains(&association.second.handle)))
|
||||
.then_some((line.common.handle, association))
|
||||
}).collect();
|
||||
})
|
||||
.collect();
|
||||
let mut result = Vec::new();
|
||||
for (handle, mut association) in candidates {
|
||||
if self.is_recording_undo() {
|
||||
@@ -361,15 +467,20 @@ impl Scene {
|
||||
|
||||
pub(crate) fn reset_centerlines(&mut self, handles: &[Handle]) -> usize {
|
||||
let extension = self.centerline_settings().extension;
|
||||
let candidates: Vec<_> = handles.iter().filter_map(|handle| {
|
||||
let EntityType::Line(line) = self.document.get_entity(*handle)? else { return None; };
|
||||
let mut association = CenterLineAssociation::read(&line.common.extended_data)?;
|
||||
association.start_extension = extension;
|
||||
association.end_extension = extension;
|
||||
association.start_length_adjustment = 0.0;
|
||||
association.end_length_adjustment = 0.0;
|
||||
rebuild_line(&self.document, &association).map(|line| (*handle, association, line))
|
||||
}).collect();
|
||||
let candidates: Vec<_> = handles
|
||||
.iter()
|
||||
.filter_map(|handle| {
|
||||
let EntityType::Line(line) = self.document.get_entity(*handle)? else {
|
||||
return None;
|
||||
};
|
||||
let mut association = CenterLineAssociation::read(&line.common.extended_data)?;
|
||||
association.start_extension = extension;
|
||||
association.end_extension = extension;
|
||||
association.start_length_adjustment = 0.0;
|
||||
association.end_length_adjustment = 0.0;
|
||||
rebuild_line(&self.document, &association).map(|line| (*handle, association, line))
|
||||
})
|
||||
.collect();
|
||||
for (handle, association, rebuilt) in &candidates {
|
||||
if self.is_recording_undo() {
|
||||
let before = self.document.get_entity_arc(*handle);
|
||||
@@ -383,20 +494,34 @@ impl Scene {
|
||||
}
|
||||
}
|
||||
if !candidates.is_empty() {
|
||||
let changes: Vec<_> = candidates.iter().map(|(handle, _, _)| (*handle, ChangeKind::Modified)).collect();
|
||||
let changes: Vec<_> = candidates
|
||||
.iter()
|
||||
.map(|(handle, _, _)| (*handle, ChangeKind::Modified))
|
||||
.collect();
|
||||
self.bump_entities(&changes);
|
||||
}
|
||||
candidates.len()
|
||||
}
|
||||
|
||||
pub(crate) fn set_centerline_association(&mut self, handles: &[Handle], associated: bool) -> usize {
|
||||
let candidates: Vec<_> = handles.iter().filter_map(|handle| {
|
||||
let EntityType::Line(line) = self.document.get_entity(*handle)? else { return None; };
|
||||
let mut association = CenterLineAssociation::read(&line.common.extended_data)?;
|
||||
association.associated = associated;
|
||||
let rebuilt = associated.then(|| rebuild_line(&self.document, &association)).flatten();
|
||||
(!associated || rebuilt.is_some()).then_some((*handle, association, rebuilt))
|
||||
}).collect();
|
||||
pub(crate) fn set_centerline_association(
|
||||
&mut self,
|
||||
handles: &[Handle],
|
||||
associated: bool,
|
||||
) -> usize {
|
||||
let candidates: Vec<_> = handles
|
||||
.iter()
|
||||
.filter_map(|handle| {
|
||||
let EntityType::Line(line) = self.document.get_entity(*handle)? else {
|
||||
return None;
|
||||
};
|
||||
let mut association = CenterLineAssociation::read(&line.common.extended_data)?;
|
||||
association.associated = associated;
|
||||
let rebuilt = associated
|
||||
.then(|| rebuild_line(&self.document, &association))
|
||||
.flatten();
|
||||
(!associated || rebuilt.is_some()).then_some((*handle, association, rebuilt))
|
||||
})
|
||||
.collect();
|
||||
for (handle, association, rebuilt) in &candidates {
|
||||
if self.is_recording_undo() {
|
||||
let before = self.document.get_entity_arc(*handle);
|
||||
@@ -413,22 +538,34 @@ impl Scene {
|
||||
}
|
||||
}
|
||||
if !candidates.is_empty() {
|
||||
let changes: Vec<_> = candidates.iter().map(|(handle, _, _)| (*handle, ChangeKind::Modified)).collect();
|
||||
let changes: Vec<_> = candidates
|
||||
.iter()
|
||||
.map(|(handle, _, _)| (*handle, ChangeKind::Modified))
|
||||
.collect();
|
||||
self.bump_entities(&changes);
|
||||
}
|
||||
candidates.len()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn resolve_center_measure(specification: &str, diameter: f64, fallback_factor: f64) -> f64 {
|
||||
pub(crate) fn resolve_center_measure(
|
||||
specification: &str,
|
||||
diameter: f64,
|
||||
fallback_factor: f64,
|
||||
) -> f64 {
|
||||
let text = specification.trim();
|
||||
if text.eq_ignore_ascii_case("ByLineType") {
|
||||
return diameter * fallback_factor;
|
||||
}
|
||||
if let Some(factor) = text.strip_suffix(['x', 'X']).and_then(|value| value.parse::<f64>().ok()) {
|
||||
if let Some(factor) = text
|
||||
.strip_suffix(['x', 'X'])
|
||||
.and_then(|value| value.parse::<f64>().ok())
|
||||
{
|
||||
return diameter * factor.max(0.0);
|
||||
}
|
||||
text.parse::<f64>().unwrap_or(diameter * fallback_factor).max(0.0)
|
||||
text.parse::<f64>()
|
||||
.unwrap_or(diameter * fallback_factor)
|
||||
.max(0.0)
|
||||
}
|
||||
|
||||
pub(crate) fn center_measure_is_relative(specification: &str) -> bool {
|
||||
|
||||
Reference in new issue
Block a user