fn warning(code: &str, subject: &str, message: String, subjects: Vec<String>) -> Problem {
Problem {
id: format!("p:{code}:{subject}"),
code: code.into(),
severity: "warning".into(),
message,
subjects,
..Default::default()
}
}
pub mod angular;
pub(crate) mod authored;
pub(crate) mod blends;
mod bosses;
mod callouts;
mod edges;
pub(crate) use edges::c_form;
mod envelope;
mod established;
pub(crate) use established::retain as retain_established;
mod flats;
pub(crate) mod footprints;
pub mod gdt;
mod grooves;
pub(crate) mod hole_marks;
mod holes;
pub(crate) mod size_datums;
pub(crate) use holes::{grid_lattice, toward_viewer};
pub(crate) mod inspection;
pub(crate) mod interior;
pub(crate) use interior::{finish_attached_openings, finish_openings, openings_need_settling};
pub(crate) fn finish_pmi_witness_routes(analysis: &Analysis, plan: &mut crate::model::Plan) {
if placement::pmi_witness_retry(analysis) {
let spec = plan.resolved_spec().into_owned();
let compact = plan.sheet.dimension_spacing == crate::model::DimensionSpacing::Compact;
with_compact(compact, || {
callout_routes::finish(analysis, &spec, plan);
hole_marks::attach(analysis, &spec, plan);
size_datums::attach(analysis, &spec, plan);
});
}
}
mod label_lanes;
mod modelled_threads;
pub(crate) use modelled_threads::{possible_threads, refused as refused_threads};
mod wording;
pub(crate) use pmi_dimensions::basic as is_basic;
pub(crate) mod callout_routes;
pub(crate) mod contacts;
mod leaders;
pub(crate) use label_lanes::{compact_inventory, follow_lanes, in_lane, settle_inventory};
pub(crate) use leaders::{default_nominal_intent, sheet_route_limits};
pub(crate) use leaders::{
reserve_radius_leaders, restore_proven_arc_tips, settle_after_furniture, settle_radius_leaders,
};
pub(crate) use placement::collisions_cached as label_collisions;
pub(crate) use placement::nearest_shared_witnesses;
pub(crate) use placement::pin_bounds;
mod gussets;
pub(crate) mod levels;
mod notches;
mod oriented_slots;
mod pads;
pub(crate) mod pin_geometry;
mod placement;
pub(crate) use placement::displaced_linear_fields;
mod plates;
pub(crate) mod pmi_dimensions;
pub(crate) mod pmi_policy;
pub mod precision;
mod profile_angles;
pub use profile_angles::{CornerMode, PARAMETER as PROFILE_ANGLE};
mod profiled_bores;
pub(crate) mod recesses;
mod redundancy;
mod schemes;
mod sections;
mod slots;
pub(crate) use slots::located_end_centres;
mod springs;
mod steps;
mod stock;
mod tolerance;
mod turned;
use std::collections::{BTreeMap, BTreeSet};
use std::sync::OnceLock;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::items::{Op, State};
use crate::model::{Analysis, DimensionSource, Invalid, PlannedSheet, PlannedView, Problem, Spec};
use crate::output::{Annotation, Claim, Curve, HAlign, Item, TextRun, VAlign};
use crate::text::{Font, Segment};
pub use leaders::clear_routes;
pub use placement::label_rows;
pub use placement::{AlignedDemand, LabelRows};
use placement::{Frame, across, moved, to_page};
pub use placement::{LabelInk, label_ink};
pub(crate) use tolerance::{check as check_tolerances, choices as fit_choices};
pub(crate) fn check_views(spec: &Spec) -> Result<(), Invalid> {
for (id, entry) in &spec.dimensions {
if let Some(v) = entry
.place
.as_ref()
.and_then(|p| p.view.as_ref())
.or(entry.view.as_ref())
&& !NAMES.contains(&v.as_str())
&& !crate::detail::NAMES.contains(&v.as_str())
&& !crate::sections::NAMES.contains(&v.as_str())
{
return Err(Invalid {
path: None,
also: Vec::new(),
message: format!(
"The view of {id:?} is {v:?}: a dimension is carried by a principal view."
),
choices: NAMES.map(String::from).to_vec(),
});
}
}
Ok(())
}
fn carries(w: &Want, v: &str) -> bool {
if crate::detail::NAMES.contains(&v)
|| (v.starts_with("section_") && crate::sections::NAMES.contains(&v))
{
return w.views.contains(&v);
}
let Some((_, toward)) = TOWARD.iter().find(|(n, _)| *n == v) else {
return false;
};
w.views.contains(&v)
&& match &w.shape {
Shape::Linear { axis, .. } => {
toward[*axis] == 0.0
|| w.turning
.is_some_and(|t| (0..3).any(|j| j != t && j != *axis && toward[j] == 0.0))
}
Shape::Callout { .. } | Shape::Angular { .. } | Shape::Aligned { .. } => true,
}
}
fn chosen_views(
wants: &mut [Want],
spec: &Spec,
frames: &BTreeMap<&str, Frame>,
) -> Result<Vec<Problem>, Invalid> {
let mut problems = Vec::new();
for w in wants.iter_mut() {
let Some(v) = spec.dimensions.get(&w.id).and_then(|e| {
e.place
.as_ref()
.and_then(|p| p.view.as_deref())
.or(e.view.as_deref())
}) else {
continue;
};
let able: Vec<&'static str> = w.views.iter().copied().filter(|c| carries(w, c)).collect();
let Some(&v) = able.iter().find(|c| **c == v) else {
return Err(Invalid {
path: None,
also: Vec::new(),
message: format!(
"{} cannot be carried by the {v} view (dimensions/{}/view): it is drawn only in a view that sees it across.",
w.id,
w.id.replace('~', "~0").replace('/', "~1")
),
choices: able.iter().map(|c| c.to_string()).collect(),
});
};
if frames.contains_key(v) {
w.views.retain(|c| *c != v);
w.views.insert(0, v);
w.basis += &format!("; the spec places it in the {v} view");
} else {
let pinned = spec
.dimensions
.get(&w.id)
.and_then(|e| e.place.as_ref())
.is_some_and(|p| p.pinned);
if pinned {
w.views.retain(|c| *c == v);
}
let (why, how, fix) = if spec.views.get(v).is_some_and(|s| s.omit) {
(
"omitted",
format!(
"Restore the view (remove views/{v}/omit) or remove the dimension's view."
),
vec![Op::remove(format!("views/{v}/omit"))],
)
} else {
(
if idle_dropped() == Some(v) {
"left out as adding only its outline"
} else {
"left out as the turned part's repeated profile"
},
format!(
"Keep the view (set views/{v} to {{}}) or remove the dimension's view."
),
Vec::new(),
)
};
let outcome = if pinned {
"its pinned placement is retained unplaced"
} else {
"it is drawn in its default view instead"
};
problems.push(Problem {
id: format!("p:dimension_view_not_shown:{}", w.id),
code: "dimension_view_not_shown".into(),
severity: "warning".into(),
message: format!(
"The spec places {} in the {v} view, which is {why}, so {outcome}. {how}",
wording_of(w)
),
subjects: vec![w.id.clone()],
fix,
..Default::default()
});
}
}
Ok(problems)
}
pub const TEXT: f64 = 3.0;
pub const ARROW: f64 = 2.7;
pub(crate) const ARROW_HALF: f64 = ARROW / 6.0;
pub(crate) const GAP: f64 = 1.0;
const OVERSHOOT: f64 = 2.0;
pub const FIRST_NORMAL: f64 = 10.0;
pub const LANE_NORMAL: f64 = 7.0;
pub const FIRST_COMPACT: f64 = 7.0;
pub const LANE_COMPACT: f64 = 5.5;
const _: () = assert!(
LANE_COMPACT - (LIFT + TEXT) >= 1.5 - 1e-9
&& LANE_COMPACT > OVERSHOOT
&& FIRST_COMPACT > GAP + OVERSHOOT
);
thread_local! {
static COMPACT: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
static IDLE_VIEW: std::cell::Cell<Option<Option<&'static str>>> = const { std::cell::Cell::new(None) };
}
pub fn compact() -> bool {
COMPACT.get()
}
pub fn first_line() -> f64 {
if compact() {
FIRST_COMPACT
} else {
FIRST_NORMAL
}
}
pub fn lane_pitch() -> f64 {
if compact() { LANE_COMPACT } else { LANE_NORMAL }
}
pub(crate) fn spaced<T>(spec: &crate::model::Spec, run: impl FnOnce() -> T) -> T {
let compact = match spec.sheet.dimension_spacing {
Some(crate::model::DimensionSpacing::Compact) => true,
Some(crate::model::DimensionSpacing::Normal) => false,
None => compact(),
};
let gutter = spec
.sheet
.frame_gutter
.unwrap_or_else(crate::layout::gutter);
crate::layout::with_gutter(gutter, || with_compact(compact, run))
}
pub(crate) fn with_compact<T>(compact: bool, run: impl FnOnce() -> T) -> T {
struct Restore(bool);
impl Drop for Restore {
fn drop(&mut self) {
COMPACT.set(self.0);
}
}
let _restore = Restore(COMPACT.replace(compact));
run()
}
pub(crate) fn with_idle_view<T>(dropped: Option<&'static str>, run: impl FnOnce() -> T) -> T {
struct Restore(Option<Option<&'static str>>);
impl Drop for Restore {
fn drop(&mut self) {
IDLE_VIEW.set(self.0);
}
}
let _restore = Restore(IDLE_VIEW.replace(Some(dropped)));
run()
}
const TOKEN_GAP: f64 = 0.45 * TEXT;
const LIFT: f64 = 1.0;
pub(crate) const CAP: f64 = 0.7 * TEXT;
const SHOULDER: f64 = 3.0;
pub const FONT: &str = "IBMPlexMono-Regular.ttf";
const MONO: &[u8] = include_bytes!("../../fonts/IBMPlexMono-Regular.ttf");
pub(crate) fn font() -> &'static Font {
static FONT: OnceLock<Font> = OnceLock::new();
FONT.get_or_init(|| Font::new(MONO.to_vec()).expect("the bundled font parses"))
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CandidateRegion {
Interior,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PlannedDimension {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub callout_offset: Option<[f64; 2]>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub region: Option<CandidateRegion>,
pub id: String,
pub kind: String,
pub view: String,
pub value: f64,
pub label: String,
pub terms: Vec<String>,
pub measurements: Vec<Measurement>,
pub default: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub from_spec: Vec<String>,
pub basis: String,
pub state: crate::items::State,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub placed: Option<Placed>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub reference: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub determined_by: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Measurement {
pub feature: String,
pub parameter: String,
pub value: f64,
}
pub(crate) fn ink_box(d: &PlannedDimension) -> Option<[f64; 4]> {
let p = d.placed.as_ref()?;
Some(placement::ink_points(p).fold(
[
f64::INFINITY,
f64::INFINITY,
f64::NEG_INFINITY,
f64::NEG_INFINITY,
],
|b, q| {
[
b[0].min(q[0]),
b[1].min(q[1]),
b[2].max(q[0]),
b[3].max(q[1]),
]
},
))
}
pub(crate) fn leader_on_extension(dimensions: &[PlannedDimension]) -> BTreeSet<Vec<String>> {
let extensions: Vec<_> = dimensions
.iter()
.filter_map(|d| {
let p = d.placed.as_ref()?;
(p.lines.len() >= 3 && p.arrows.len() == 2).then(|| (&d.id, &p.lines[..2]))
})
.collect();
let mut pairs = BTreeSet::new();
for d in dimensions.iter().filter(|d| d.kind == "callout") {
let Some(p) = &d.placed else { continue };
if p.lines.is_empty() || (p.lines.len() >= 3 && p.arrows.len() == 2) {
continue;
}
for (id, lines) in &extensions {
if **id != d.id && leaders::meets_extensions(lines, &p.lines, p.label_box) {
let mut subjects = vec![d.id.clone(), (*id).clone()];
subjects.sort();
pairs.insert(subjects);
}
}
}
pairs
}
pub(crate) fn crowded_labels(plan: &crate::model::Plan) -> usize {
contacts::contacts(plan)
.iter()
.filter(|p| p.code == "label_crowded")
.count()
}
pub(crate) fn converging_terminations(analysis: &Analysis, plan: &crate::model::Plan) -> bool {
let cache = footprints::Cache::default();
plan.views.iter().any(|view| {
let tips: Vec<_> = plan
.dimensions
.iter()
.filter(|d| d.view == view.id && d.kind == "callout")
.filter_map(|d| d.placed.as_ref().map(|p| (d, p)))
.flat_map(|(d, p)| p.arrows.iter().map(move |a| (d.id.as_str(), a[0])))
.chain(
plan.gdt
.iter()
.filter(|g| g.view == view.id)
.filter_map(|g| g.placed.as_ref().map(|p| (g, p)))
.flat_map(|(g, p)| p.arrows.iter().map(move |a| (g.id.as_str(), a[0]))),
)
.collect();
cache.get(analysis, view).converging_terminations(&tips)
})
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Placed {
pub lines: Vec<[[f64; 2]; 2]>,
pub arrows: Vec<[[f64; 2]; 2]>,
pub label_at: [f64; 2],
pub rotation: f64,
pub label_box: [f64; 4],
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Omitted {
pub id: String,
pub label: String,
pub basis: String,
pub state: crate::items::State,
}
pub fn fmt_in(v: f64, units: crate::units::Units) -> String {
units.format(v)
}
pub fn fmt(v: f64) -> String {
crate::nominal::format(v)
}
type P3 = [f64; 3];
type P2 = [f64; 2];
pub type Planned = (Vec<PlannedDimension>, Vec<Omitted>, Vec<Problem>);
const NAMES: [&str; 3] = ["front", "plan", "side"];
const AXES: [&str; 3] = ["x", "y", "z"];
const TOWARD: [(&str, P3); 3] = [
("front", [0.0, -1.0, 0.0]),
("plan", [0.0, 0.0, 1.0]),
("side", [1.0, 0.0, 0.0]),
];
pub fn end_on(k: usize) -> &'static str {
TOWARD
.iter()
.find(|(_, t)| t[k].abs() == 1.0)
.map(|(n, _)| *n)
.expect("each axis has a view")
}
#[derive(Clone, Debug, Serialize)]
enum Shape {
Linear {
a: P3,
b: P3,
axis: usize,
a_edge: bool,
b_edge: bool,
order: u8,
},
Aligned { a: P3, b: P3 },
Callout {
sites: Vec<(P3, f64)>,
arcs: Vec<crate::arc::ProfileArc>,
},
Angular {
angle: angular::Angle,
#[serde(skip_serializing_if = "Vec::is_empty")]
alternates: Vec<angular::Angle>,
},
}
#[derive(Clone, Debug, Serialize)]
struct Want {
#[serde(skip_serializing_if = "crate::units::Units::is_mm")]
units: crate::units::Units,
id: String,
views: Vec<&'static str>,
value: f64,
terms: Vec<String>,
measurements: Vec<Measurement>,
basis: String,
shape: Shape,
turning: Option<usize>,
}
fn wording_of(w: &Want) -> String {
crate::wording::item(&w.id, "", &w.terms.join(" "))
}
pub(crate) fn ink_in_box(d: &PlannedDimension, b: [f64; 4]) -> bool {
d.placed
.as_ref()
.is_some_and(|p| placement::boxes_overlap(p.label_box, b) || placement::ink_through(p, b))
}
pub(crate) fn text_contact(a: &Placed, b: &Placed) -> bool {
placement::boxes_overlap(a.label_box, b.label_box)
|| placement::ink_through(a, b.label_box)
|| placement::ink_through(b, a.label_box)
}
pub(crate) fn is_envelope_extent(id: &str) -> bool {
matches!(
id,
"d:envelope/width.length"
| "d:envelope/depth.length"
| "d:envelope/height.length"
| "d:envelope/od.diameter"
)
}
fn want_id(feature: &str, parameter: &str) -> String {
format!("d:{feature}/{parameter}")
}
fn add(a: P3, b: P3) -> P3 {
[a[0] + b[0], a[1] + b[1], a[2] + b[2]]
}
fn scale3(a: P3, s: f64) -> P3 {
[a[0] * s, a[1] * s, a[2] * s]
}
fn point(v: Option<&Value>) -> Option<P3> {
let c: Vec<f64> = v?
.as_array()?
.iter()
.map(Value::as_f64)
.collect::<Option<_>>()?;
<[f64; 3]>::try_from(c).ok()
}
struct QFrame([[f64; 3]; 4]);
impl QFrame {
fn point(&self, l: P3) -> P3 {
(0..3).fold(self.0[0], |p, j| add(p, scale3(self.0[j + 1], l[j])))
}
fn letter(&self, axis: &Value) -> Option<P3> {
let k = AXES.iter().position(|a| axis.as_str() == Some(a))?;
Some(self.0[k + 1])
}
}
fn key(x: f64) -> i64 {
crate::bridge::holes::size_key(x)
}
struct Context<'a> {
analysis: &'a Analysis,
spec: &'a Spec,
frame: QFrame,
turned: Option<turned::Turned>,
}
struct Planner {
kinds: &'static [&'static str],
wants: fn(&Context) -> (Vec<Want>, Vec<Problem>),
}
const PLANNERS: [Planner; 21] = [
turned::PLANNER,
envelope::PLANNER,
stock::PLANNER,
springs::PLANNER,
holes::PLANNER,
slots::PLANNER,
bosses::PLANNER,
edges::PLANNER,
notches::PLANNER,
blends::PLANNER,
grooves::PLANNER,
flats::PLANNER,
steps::PLANNER,
levels::PLANNER,
recesses::PLANNER,
plates::PLANNER,
pads::PLANNER,
gussets::PLANNER,
oriented_slots::PLANNER,
profiled_bores::PLANNER,
modelled_threads::PLANNER,
];
fn wants(
analysis: &Analysis,
) -> (
Vec<Want>,
Vec<Problem>,
BTreeMap<String, pmi_dimensions::Note>,
) {
let mut result = crate::planning::memo(analysis, "wants", Vec::new(), || {
wants_uncached(
analysis,
&Spec {
units: crate::units::Units::Mm,
..Spec::default()
},
)
});
bosses::prefer_established(analysis, &mut result.0);
result
}
pub(crate) fn nominal_inspection_views(
analysis: &Analysis,
) -> std::collections::BTreeSet<&'static str> {
if !crate::nominal::geometry_eligible(analysis)
|| analysis.features.iter().any(|f| f.kind == "turned_step")
{
return Default::default();
}
let recesses = has_new_native_recess_dimensions(analysis);
let hole_count = analysis
.features
.iter()
.filter(|f| f.kind == "hole")
.count();
wants(analysis)
.0
.iter()
.filter(|w| {
if !matches!(w.shape, Shape::Callout { .. }) {
return false;
}
let established_bore = hole_count < crate::hole_table::MIN_HOLES
&& w.measurements
.iter()
.any(|m| m.parameter == "bore.diameter")
&& w.measurements.iter().all(|m| {
fmt(m.value)
.split_once('.')
.is_none_or(|(_, tail)| tail.len() <= 1)
});
established_bore
|| (!recesses
&& w.measurements.iter().any(|m| {
!m.feature.starts_with("f:section_recess:") && fmt(m.value).contains('.')
}))
})
.filter_map(|w| w.views.first().copied())
.collect()
}
pub(crate) fn has_new_native_recess_dimensions(analysis: &Analysis) -> bool {
analysis
.recognition
.as_ref()
.is_some_and(|r| r.recogniser.starts_with("quiddity-rust "))
&& has_recess_dimensions(analysis)
}
pub(crate) fn has_recess_dimensions(analysis: &Analysis) -> bool {
let (canonical, established, comparison, ..) = crate::planning::policy_key(analysis);
crate::planning::memo(
analysis,
"recess-presence",
vec![
u8::from(canonical),
u8::from(established),
u8::from(comparison),
],
|| has_recess_dimensions_uncached(analysis),
)
}
fn has_recess_dimensions_uncached(analysis: &Analysis) -> bool {
wants(analysis).0.iter().any(|w| {
w.measurements
.iter()
.any(|m| m.feature.starts_with("f:section_recess:"))
})
}
fn wants_uncached(
analysis: &Analysis,
spec: &Spec,
) -> (
Vec<Want>,
Vec<Problem>,
BTreeMap<String, pmi_dimensions::Note>,
) {
let fmt = |v| crate::dims::fmt_in(v, spec.units);
let frame = QFrame(analysis.recognition.as_ref().map_or(
[[0.0; 3], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
|r| r.frame,
));
let cx = Context {
analysis,
spec,
turned: turned::turned(analysis, &frame),
frame,
};
let mut out = Vec::new();
let mut problems = Vec::new();
for p in &PLANNERS {
let (w, more) = (p.wants)(&cx);
out.extend(w);
problems.extend(more);
}
blends::count_oblique(analysis, &mut out, &mut problems, spec.units);
if let Some((axis, datum)) = turned::axial_datum(analysis) {
for w in &mut out {
if w.measurements
.iter()
.any(|m| m.parameter.starts_with("location.length."))
&& let Shape::Linear {
a,
b,
axis: k,
order,
..
} = &mut w.shape
&& *k == axis
{
a[axis] = datum;
w.value = (b[axis] - datum).abs();
w.terms = vec![fmt(w.value)];
for m in &mut w.measurements {
m.value = w.value;
}
*order = 1;
w.basis = format!(
"axial location in {} from the profile's common inspection end",
AXES[axis]
);
}
}
}
if cx.turned.is_none() {
holes::round_origins(analysis, spec, &mut out);
}
flats::share_locations(&cx, &mut out);
let stated: BTreeSet<String> = out
.iter()
.flat_map(|w| w.measurements.iter().map(|m| m.feature.clone()))
.collect();
problems.extend(
not_ported(analysis)
.into_iter()
.filter(|p| !p.subjects.iter().any(|s| stated.contains(s))),
);
for w in &mut out {
w.units = spec.units;
}
problems.extend(pmi_policy::origins(analysis, spec, &mut out));
pmi_policy::drop_on_origin(analysis, spec, &mut out);
let (own, more, notes) = if pmi_policy::strict(analysis, spec) {
pmi_policy::strict_callouts(analysis, &mut out);
if spec.sheet.references == Some(true) {
let indices: Vec<_> = out
.iter()
.enumerate()
.filter(|(_, w)| matches!(w.shape, Shape::Callout { .. }))
.map(|(i, _)| i)
.collect();
let mut callouts: Vec<_> = indices.iter().map(|&i| out[i].clone()).collect();
let lowered = pmi_dimensions::lower(&cx, &mut callouts);
for (i, w) in indices.into_iter().zip(callouts) {
out[i] = w;
}
lowered
} else {
pmi_dimensions::lower(&cx, &mut [])
}
} else {
pmi_dimensions::lower(&cx, &mut out)
};
out.extend(own.into_iter().map(|mut w| {
w.units = spec.units;
w
}));
problems.extend(more);
(out, problems, notes)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Disposition {
Unsupported,
Deferred,
EvidenceOnly,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DispositionReason {
ConsumerSemanticsUnsupported,
ConsumerSemanticsDeferred,
HoleCalloutEvidence,
DimensionsNotPorted,
StepLevelProjectionEvidence,
RiserProjectionEvidence,
GeometryOnlyCritique,
HoleRecognitionEvidence,
RecognitionRefused,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FeatureDisposition {
pub subject: String,
pub disposition: Disposition,
pub reason_code: DispositionReason,
pub severity: String,
}
const UNDRAFTED: &[(&str, Disposition, DispositionReason)] = &[
(
"CircularFacePattern",
Disposition::Deferred,
DispositionReason::ConsumerSemanticsDeferred,
),
(
"CounterSink",
Disposition::EvidenceOnly,
DispositionReason::HoleCalloutEvidence,
),
(
"FreeformSurface",
Disposition::Deferred,
DispositionReason::ConsumerSemanticsDeferred,
),
(
"InteriorVoid",
Disposition::Deferred,
DispositionReason::ConsumerSemanticsDeferred,
),
(
"ObliqueThroughStep",
Disposition::Deferred,
DispositionReason::ConsumerSemanticsDeferred,
),
(
"OrientedSlotArray",
Disposition::Deferred,
DispositionReason::ConsumerSemanticsDeferred,
),
(
"OrientedSlotGrid",
Disposition::Deferred,
DispositionReason::ConsumerSemanticsDeferred,
),
(
"SheetMetalBody",
Disposition::Deferred,
DispositionReason::ConsumerSemanticsDeferred,
),
(
"ThinWallBody",
Disposition::Deferred,
DispositionReason::ConsumerSemanticsDeferred,
),
];
pub(crate) fn stated_by(
analysis: &Analysis,
f: &crate::bridge::Feature,
) -> Vec<(String, &'static str)> {
match f.kind.as_str() {
"annular_groove" => crate::lint::grooves::annular_owners(analysis, f)
.unwrap_or_default()
.into_iter()
.map(|(_, o, p)| (o, p))
.collect(),
"v_channel" => {
crate::lint::notch_evidence::channel_owner(&crate::notches::requirements(analysis), f)
.map(|n| vec![(n.clone(), "notch.angle"), (n, "notch_depth.length")])
.unwrap_or_default()
}
_ => Vec::new(),
}
}
fn restate(analysis: &Analysis, spec: &Spec, wants: &mut Vec<Want>, problems: &mut Vec<Problem>) {
let omits = |w: &Want| spec.dimensions.get(&w.id).is_some_and(|d| d.omit);
if spec.dimension_source != DimensionSource::Auto || !wants.iter().any(omits) {
return;
}
let drawn = |(o, p): &(String, &str)| {
wants.iter().any(|w| {
!omits(w)
&& w.measurements
.iter()
.any(|m| m.feature == *o && m.parameter == *p)
})
};
let lost: Vec<_> = analysis
.features
.iter()
.filter(|f| {
let by = stated_by(analysis, f);
!by.is_empty() && !by.iter().all(drawn)
})
.collect();
if lost.is_empty() {
return;
}
let frame = QFrame(analysis.recognition.as_ref().map_or(
[[0.0; 3], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
|r| r.frame,
));
let cx = Context {
analysis,
spec,
turned: turned::turned(analysis, &frame),
frame,
};
let mut own = Vec::new();
for f in lost {
let ws = if f.kind == "annular_groove" {
grooves::annular(&cx, f)
} else {
notches::channel(&cx, f)
};
match ws {
Ok(ws) => own.extend(ws),
Err(why) => problems.push(grooves::not_dimensioned(f, &why)),
}
}
let _ = pmi_policy::origins(analysis, spec, &mut own);
pmi_policy::drop_on_origin(analysis, spec, &mut own);
wants.extend(own);
}
pub(crate) fn undrafted(f: &crate::bridge::Feature) -> bool {
UNDRAFTED
.iter()
.any(|(subtype, ..)| *subtype == f.fingerprint.subtype)
}
pub(crate) fn feature_dispositions(analysis: &Analysis) -> Vec<FeatureDisposition> {
let (wants, ..) = wants(analysis);
let stated: BTreeSet<_> = wants
.iter()
.flat_map(|w| &w.measurements)
.map(|m| m.feature.as_str())
.collect();
let mut out = Vec::new();
let mut add = |subject: String, disposition, reason_code| {
out.push(FeatureDisposition {
subject,
disposition,
reason_code,
severity: "info".into(),
})
};
for f in &analysis.features {
if let Some((_, disposition, reason)) = UNDRAFTED
.iter()
.find(|(subtype, ..)| *subtype == f.fingerprint.subtype)
{
add(f.id.clone(), *disposition, *reason);
} else if !stated.contains(f.id.as_str())
&& !PLANNERS.iter().any(|p| p.kinds.contains(&f.kind.as_str()))
{
let (disposition, reason) = if f.kind == "angled_step" {
(
Disposition::Unsupported,
DispositionReason::ConsumerSemanticsUnsupported,
)
} else {
(
Disposition::Deferred,
DispositionReason::DimensionsNotPorted,
)
};
add(f.id.clone(), disposition, reason);
}
}
if let Some(r) = &analysis.recognition {
for (i, record) in r.evidence.iter().enumerate() {
let reason = match record.subtype.as_str() {
"FaceLevel" => DispositionReason::StepLevelProjectionEvidence,
"RiserEvidence" => DispositionReason::RiserProjectionEvidence,
"RepeatingRadialProfile" => DispositionReason::GeometryOnlyCritique,
"HoleSpec" => DispositionReason::HoleRecognitionEvidence,
_ => continue,
};
add(
format!("recognition:evidence:{i}"),
Disposition::EvidenceOnly,
reason,
);
}
for (i, _) in r.refusals.iter().enumerate() {
add(
format!("recognition:refusal:{i}"),
Disposition::Unsupported,
DispositionReason::RecognitionRefused,
);
}
}
out
}
fn not_ported(analysis: &Analysis) -> Vec<Problem> {
analysis
.features
.iter()
.filter(|f| !PLANNERS.iter().any(|p| p.kinds.contains(&f.kind.as_str())))
.filter(|f| !undrafted(f))
.map(|f| {
unsupported(f).unwrap_or_else(|| Problem {
id: format!("p:dimensions_not_ported:{}", f.id),
code: "dimensions_not_ported".into(),
severity: "info".into(),
message: format!(
"{} is not dimensioned: no dimensions for {} are ported yet.",
crate::wording::capital(&crate::wording::feature(f)),
f.kind.replace('_', " ")
),
subjects: vec![f.id.clone()],
..Default::default()
})
})
.collect()
}
fn unsupported(f: &crate::bridge::Feature) -> Option<Problem> {
if f.kind != "angled_step" {
return None;
}
let code = "angled_step_requirement_unsupported";
Some(Problem {
id: format!("p:{code}:{}", f.id),
code: code.into(),
severity: "warning".into(),
message: format!(
"{} is not represented by Draftwright dimensions; review and define its \
manufacturing geometry outside automatic drawing approval.",
crate::wording::capital(&crate::wording::feature(f))
),
subjects: vec![f.id.clone()],
..Default::default()
})
}
pub(crate) fn levenshtein(a: &str, b: &str) -> usize {
let b: Vec<char> = b.chars().collect();
let mut row: Vec<usize> = (0..=b.len()).collect();
for (i, ca) in a.chars().enumerate() {
let mut prev = row[0];
row[0] = i + 1;
for (j, cb) in b.iter().enumerate() {
let cur = row[j + 1];
row[j + 1] = (prev + usize::from(ca != *cb)).min(row[j] + 1).min(cur + 1);
prev = cur;
}
}
row[b.len()]
}
pub(crate) fn inspection_datum(a: &Analysis) -> Option<(usize, f64)> {
turned::axial_datum(a)
}
pub(crate) fn detail_levels(a: &Analysis) -> Vec<(String, f64)> {
let (canonical, established, comparison, ..) = crate::planning::policy_key(a);
crate::planning::memo(
a,
"detail-linear-levels",
vec![
u8::from(canonical),
u8::from(established),
u8::from(comparison),
],
|| detail_levels_uncached(a),
)
}
fn detail_levels_uncached(a: &Analysis) -> Vec<(String, f64)> {
wants(a)
.0
.into_iter()
.filter_map(|w| match w.shape {
Shape::Linear { .. } => Some((w.id, w.value)),
_ => None,
})
.collect()
}
fn detail_routes(a: &Analysis, wants: &mut [Want], views: &[PlannedView]) {
for v in views {
let Some(d) = &v.detail else { continue };
let Some(&name) = crate::detail::NAMES
.iter()
.find(|n| v.id == format!("v:{n}"))
else {
continue;
};
let inside =
|p: P3| (0..3).all(|k| d.bounds[0][k] - 1e-6 <= p[k] && p[k] <= d.bounds[1][k] + 1e-6);
for w in wants.iter_mut() {
let explicit = d.dimensions.contains(&w.id);
let blend = w.measurements.iter().any(|m| {
matches!(
m.parameter.as_str(),
"blend.radius" | "fillet.radius" | "cylindrical_profile.radius"
)
});
let sites = match &w.shape {
Shape::Callout { sites, .. } => sites.iter().any(|(p, _)| inside(*p)),
_ => false,
};
let feature = w
.measurements
.iter()
.any(|m| crate::dims::feature_origin(a, &m.feature).is_some_and(inside));
if !explicit && !(sites && blend && d.kind == "turned-head") && !(d.authored && feature)
{
continue;
}
match &mut w.shape {
Shape::Linear {
a: pa,
b: pb,
axis,
a_edge,
b_edge,
..
} => {
let (mut next_a, mut next_b) = (*pa, *pb);
if w.id.ends_with("/step.length")
&& d.kind == "turned-head"
&& *axis == 0
&& let Some(band) = w
.measurements
.first()
.and_then(|m| a.features.iter().find(|f| f.id == m.feature))
.and_then(|f| {
crate::lint::turned::band(&crate::bridge::holes::frame(a), f).ok()
})
{
next_a[2] = band.on[2] + band.diameter / 2.0;
next_b[2] = next_a[2];
}
if !inside(next_a) || !inside(next_b) {
continue;
}
(*pa, *pb) = (next_a, next_b);
if w.id.ends_with("/step.length") && *axis == 0 {
*a_edge = false;
*b_edge = false;
}
}
Shape::Callout { sites, .. } => {
sites.retain(|(p, _)| inside(*p));
if sites.is_empty() {
continue;
}
}
Shape::Aligned { a, b } => {
if !inside(*a) || !inside(*b) {
continue;
}
}
Shape::Angular { .. } => continue,
}
w.views.insert(0, name);
w.basis += &format!("; recovered at enlarged scale in {name}");
}
}
}
fn route_sites(analysis: &Analysis, w: &mut Want, view: &str) {
if (w.id.starts_with("d:pmi:") || w.id.starts_with("d:specify_general:"))
&& let Some(crate::pmi::Reading::Read(report)) = &analysis.pmi
{
let ids = report.identities();
if let Some(points) = report.records.iter().find_map(|r| {
(w.measurements.iter().any(|m| {
ids.ids.get(&r.source_id).is_some_and(|id| {
&m.feature == id || m.feature == pmi_dimensions::general_feature(id)
})
}))
.then(|| {
let g = r.geometry.as_ref()?;
let preferred = match (g.axis.as_deref(), g.view.as_deref()) {
(Some("X" | "Y"), Some("plan")) => "plan",
(Some("Z" | "Y"), Some("side")) | (Some("Y"), _) => "side",
_ => "front",
};
let legacy_plan = g.axis.as_deref() == Some("Y")
&& view == "plan"
&& !matches!(g.view.as_deref(), Some("side" | "plan"));
(view != preferred && !legacy_plan)
.then(|| g.view_witnesses.get(view).copied())
.flatten()
})
.flatten()
}) && let Shape::Linear { a, b, .. } = &mut w.shape
{
[*a, *b] = points;
}
}
bosses::route_profile(analysis, w, view);
turned::route_profile(analysis, w, view);
edges::route_profile(analysis, w, view);
}
fn route(
wants: &mut [Want],
frames: &BTreeMap<&str, Frame>,
analysis: &Analysis,
) -> (Vec<Option<&'static str>>, Vec<Vec<&'static str>>) {
let [lo, hi] = analysis.envelope;
let mut chosen: Vec<Option<&'static str>> = Vec::new();
let mut end_on: Vec<Vec<&'static str>> = Vec::new();
for w in wants.iter_mut() {
let mut view = None;
let mut along = Vec::new();
for v in w.views.clone() {
let Some(f) = frames.get(v) else { continue };
if let Shape::Linear { a, b, axis, .. } = &mut w.shape
&& across(f, *axis).is_none()
{
let radial = w
.turning
.and_then(|t| (0..3).find(|&j| j != t && j != *axis && across(f, j).is_some()));
let Some(j) = radial else {
along.push(v);
continue;
};
*a = lo;
*b = lo;
b[j] = hi[j];
*axis = j;
}
view = Some(v);
break;
}
if let Some(v) = view {
route_sites(analysis, w, v);
}
if let Some(v) = view
&& v != w.views[0]
{
let why = if frames.contains_key(w.views[0]) {
"looks along it"
} else {
"is omitted"
};
w.basis += &format!(
"; the {} view {why}, so the {v} view carries it",
w.views[0]
);
}
chosen.push(view);
end_on.push(along);
}
(chosen, end_on)
}
pub(crate) fn reduction_preserves_requirements(
analysis: &Analysis,
spec: &Spec,
kept: &[&str],
excluded: &BTreeSet<String>,
) -> bool {
let (wants, _, _) = pmi_policy::wants(analysis, spec);
wants
.iter()
.filter(|w| {
!excluded.contains(&w.id) && !spec.dimensions.get(&w.id).is_some_and(|d| d.omit)
})
.all(|w| {
!NAMES.iter().any(|v| carries(w, v))
|| kept
.iter()
.any(|v| carries(w, v) && !spec.views.get(*v).is_some_and(|v| v.omit))
})
}
pub(crate) fn idle_view(
analysis: &Analysis,
spec: &Spec,
demand: &crate::demand::Demand,
) -> Option<([&'static str; 2], &'static str, String)> {
let [allowed, ..] = idle_spec(spec);
if crate::demand::turned_views(demand).is_some() || !allowed {
return None;
}
let v = match IDLE_VIEW.get() {
Some(given) => given?,
None => idle_decision(analysis, spec).dropped?,
};
let other = if v == "plan" { "side" } else { "plan" };
Some((
["front", other],
v,
format!(
"no dimension needs the {v} view (the front and {other} views carry them all) and it would add only its outline (naming views/{v} keeps it)"
),
))
}
#[derive(Clone, Copy, Default)]
pub(crate) struct IdleDecision {
pub dropped: Option<&'static str>,
pub paper: Option<(usize, usize)>,
}
pub(crate) fn idle_decision(analysis: &Analysis, spec: &Spec) -> IdleDecision {
let Some(v) = idle_candidate(analysis, spec) else {
return IdleDecision::default();
};
let mut named = spec.clone();
named.sheet.size = None;
named.sheet.scale = None;
named.sheet.dimension_spacing = None;
named.views.insert(v.into(), Default::default());
for d in named.dimensions.values_mut() {
d.omit = false;
d.place = None;
}
for a in named.annotations.values_mut() {
a.place = None;
}
let mut key = serde_json::to_vec(&named).expect("planning inputs serialize");
if crate::placement_intent::has_pins(&named) {
key.push(0xff);
key.extend(
serde_json::to_vec(&crate::planning::policy_key(analysis))
.expect("view policy serializes"),
);
}
crate::planning::memo(analysis, "idle-view", key, || {
let shown = crate::planning::isolated(analysis, || {
with_compact(false, || {
crate::model::plan_escalated(analysis, &crate::model::migrate(&named, analysis)?)
})
});
let Ok(shown) = shown else {
return IdleDecision::default();
};
let page = spec
.pages()
.iter()
.position(|p| p.0 == shown.sheet.size)
.unwrap_or(usize::MAX);
IdleDecision {
dropped: (!shown.dimensions.iter().any(|d| d.view == format!("v:{v}"))).then_some(v),
paper: Some((page, 1 + shown.sheets.len())),
}
})
}
pub(crate) fn idle_decided<T>(analysis: &Analysis, spec: &Spec, run: impl FnOnce() -> T) -> T {
if IDLE_VIEW.get().is_some() {
return run();
}
with_idle_view(idle_decision(analysis, spec).dropped, run)
}
pub(crate) fn idle_dropped() -> Option<&'static str> {
IDLE_VIEW.get().flatten()
}
pub(crate) fn idle_given() -> Option<Option<&'static str>> {
IDLE_VIEW.get()
}
fn idle_candidate(analysis: &Analysis, spec: &Spec) -> Option<&'static str> {
let [allowed, plan_kept, side_kept] = idle_spec(spec);
let demand = crate::demand::read(analysis);
if crate::demand::turned_views(&demand).is_some() || !allowed {
return None;
}
let referred = |v: &str| if v == "plan" { plan_kept } else { side_kept };
let pmi = crate::pmi_lowering::views(analysis);
let axis = prism_axis(analysis);
let round = |v: &str| {
analysis.edge_ink.as_ref().is_some_and(|ink| {
ink.curved
.iter()
.filter_map(|c| c.cylinder.as_ref())
.any(|c| (0..3).any(|k| (c.axis[k].abs() - 1.0).abs() < 1e-6 && end_on(k) == v))
})
};
["plan", "side"].into_iter().find(|v| {
!pmi.contains(v)
&& !(demand.section.is_some() && *v == "plan")
&& !referred(v)
&& axis.is_none_or(|k| end_on(k) != *v)
&& !round(v)
})
}
pub(crate) fn idle_spec(spec: &Spec) -> [bool; 3] {
let referred = |v: &str| {
spec.dimensions.values().any(|d| {
d.view.as_deref() == Some(v)
|| d.place.as_ref().and_then(|p| p.view.as_deref()) == Some(v)
}) || spec
.annotations
.values()
.any(|a| a.place.as_ref().and_then(|p| p.view.as_deref()) == Some(v))
|| spec.views.values().any(|s| {
s.detail
.as_ref()
.is_some_and(|d| d.from.as_deref() == Some(v))
|| s.place.as_ref().is_some_and(|p| p.relative_to == v)
})
};
[
spec.dimension_source != DimensionSource::Authored
&& spec.dimension_schemes.is_empty()
&& !NAMES.iter().any(|v| spec.views.contains_key(*v)),
referred("plan"),
referred("side"),
]
}
fn prism_axis(analysis: &Analysis) -> Option<usize> {
let ink = analysis.edge_ink.as_ref()?;
let tol = 2.0 * crate::model::projected_ink::GRID;
let flat = |e: &[[f64; 3]], ks: &[usize]| {
ks.iter()
.all(|&k| e.iter().all(|p| (p[k] - e[0][k]).abs() <= tol))
};
(0..3).find(|&k| {
let across: Vec<usize> = (0..3).filter(|&j| j != k).collect();
let square = |c: f64| c.abs() < 1e-6 || (c.abs() - 1.0).abs() < 1e-6;
!ink.edges.is_empty()
&& ink
.edges
.iter()
.all(|e| e.is_empty() || flat(e, &[k]) || flat(e, &across))
&& ink.planes.iter().all(|f| square(f.normal[k]))
&& ink.curved.iter().all(|c| {
c.cylinder
.as_ref()
.is_some_and(|c| (c.axis[k].abs() - 1.0).abs() < 1e-6)
})
})
}
pub(crate) fn callout_bands(
analysis: &Analysis,
spec: &Spec,
) -> Result<Vec<crate::layout::CalloutBand>, Invalid> {
if !spec.dimensions.values().any(|d| {
d.place
.as_ref()
.and_then(crate::placement_intent::side_hint)
.is_some()
}) {
return Ok(Vec::new());
}
let (mut wants, ..) = pmi_policy::wants(analysis, spec);
callouts::apply(analysis, spec, &mut wants);
let (scheme_covered, _) = schemes::apply(analysis, spec, &mut wants)?;
authored::append(analysis, spec, &mut wants)?;
if !scheme_covered.is_empty() {
wants.retain(|w| !scheme_covered.contains_key(&w.id));
}
let mut kept: Vec<_> = wants
.into_iter()
.map(|w| (w, false, None, Vec::new()))
.collect();
precision::labels(spec, kept.iter_mut().map(|(w, ..)| w))?;
let mut given = tolerance::apply(analysis, spec, &mut kept, &[], &mut Vec::new())?;
wording::apply(spec, &mut kept, &mut given)?;
Ok(kept
.iter()
.filter_map(|(w, ..)| {
if !matches!(w.shape, Shape::Callout { .. }) {
return None;
}
let entry = spec.dimensions.get(&w.id)?;
if entry.omit {
return None;
}
let place = entry.place.as_ref()?;
let side = crate::placement_intent::side_hint(place)?;
let view = place
.view
.as_deref()
.or(entry.view.as_deref())
.unwrap_or(w.views[0]);
let view = *NAMES.iter().find(|n| **n == view)?;
if spec.views.get(view).is_some_and(|v| v.omit) {
return None;
}
use crate::placement_intent::Side;
let ink = label_ink(&w.terms, font());
let (side, depth) = match side {
Side::Left => ("left", ink.max[0] - ink.min[0] + SHOULDER),
Side::Right => ("right", ink.max[0] - ink.min[0] + SHOULDER),
Side::Above => ("above", (ink.max[1] - ink.min[1]).max(TEXT) + LIFT),
Side::Below => ("below", (ink.max[1] - ink.min[1]).max(TEXT) + LIFT),
};
Some(crate::layout::CalloutBand {
view,
side,
depth: first_line() + depth + 2.0,
})
})
.collect())
}
pub fn lanes(analysis: &Analysis, views: &[(&str, P3, P3)]) -> crate::layout::Lanes {
lanes_excluding(analysis, views, &BTreeSet::new(), &Spec::default())
.expect("default display policy cannot conflict")
.0
}
pub(crate) fn lanes_excluding(
analysis: &Analysis,
views: &[(&str, P3, P3)],
excluded: &BTreeSet<String>,
spec: &Spec,
) -> Result<(crate::layout::Lanes, Vec<AlignedDemand>), Invalid> {
lane_policy(analysis, views, excluded, spec, true)
}
pub(crate) fn initial_lanes(
analysis: &Analysis,
views: &[(&str, P3, P3)],
excluded: &BTreeSet<String>,
spec: &Spec,
) -> Result<(crate::layout::Lanes, Vec<AlignedDemand>), Invalid> {
lane_policy(analysis, views, excluded, spec, false)
}
fn lane_policy(
analysis: &Analysis,
views: &[(&str, P3, P3)],
excluded: &BTreeSet<String>,
spec: &Spec,
callout_columns: bool,
) -> Result<(crate::layout::Lanes, Vec<AlignedDemand>), Invalid> {
let key = serde_json::to_vec(&(views, excluded, spec, callout_columns))
.expect("lane inputs serialize");
crate::planning::memo(analysis, "lanes", key, || {
lanes_uncached(analysis, views, excluded, spec, callout_columns)
})
}
fn lanes_uncached(
analysis: &Analysis,
views: &[(&str, P3, P3)],
excluded: &BTreeSet<String>,
spec: &Spec,
callout_columns: bool,
) -> Result<(crate::layout::Lanes, Vec<AlignedDemand>), Invalid> {
let (mut wants, mut problems, notes) = pmi_policy::wants(analysis, spec);
callouts::apply(analysis, spec, &mut wants);
let (scheme_covered, scheme_problems) = schemes::apply(analysis, spec, &mut wants)?;
problems.extend(scheme_problems);
problems.extend(authored::append(analysis, spec, &mut wants)?);
if scheme_covered.is_empty() {
wants.retain(|w| !excluded.contains(&w.id));
} else {
wants.retain(|w| !excluded.contains(&w.id) && !scheme_covered.contains_key(&w.id));
}
let frames: BTreeMap<&str, Frame> = views
.iter()
.filter(|(name, ..)| NAMES.contains(name))
.map(|(name, direction, up)| {
(
*name,
Frame::looking(analysis, *direction, *up, [0.0; 2], 1.0),
)
})
.collect();
if spec.dimensions.values().any(|d| d.between.is_some()) {
problems.extend(chosen_views(&mut wants, spec, &frames)?);
}
let recesses = wants.iter().any(|w| {
w.measurements
.iter()
.any(|m| m.feature.starts_with("f:section_recess:"))
});
let mut stations = wants.clone();
profile_angles::apply(analysis, spec, &mut stations)?;
wants.extend(stations.into_iter().skip(wants.len()).filter(|w| {
let Shape::Linear { axis, .. } = w.shape else {
return false;
};
!excluded.contains(&w.id)
&& (recesses
|| w.views.iter().any(|v| {
frames.get(v).is_some_and(|f| {
placement::across(f, axis).is_some_and(|horizontal| {
placement::station_strip(f, w, horizontal)
.is_some_and(|side| side != placement::strip(v, horizontal))
})
})
}))
}));
plates::prefer_readable(analysis, &mut wants, &frames, None);
let (chosen, _) = route(&mut wants, &frames, analysis);
let wants: Vec<_> = wants
.into_iter()
.zip(chosen)
.filter(|(w, _)| !w.measurements.iter().any(|m| m.parameter == "blend.radius"))
.collect();
let (mut wants, chosen): (Vec<_>, Vec<_>) = wants.into_iter().unzip();
let plate = |w: &Want| {
w.measurements
.iter()
.any(|m| m.parameter == "thickness.length")
};
let baseline = wants
.iter()
.any(|w| w.id.ends_with("/step_position.length"));
let conveyed = |w: &Want| baseline && w.id.ends_with("/step.length");
let shared_bands = wants.iter().any(|w| {
w.measurements
.iter()
.any(|m| m.feature.starts_with("f:section_recess:"))
});
let mut owner = if wants.iter().any(plate) || baseline || shared_bands {
let merged = placement::owners(analysis, spec, &wants, &chosen);
wants
.iter()
.enumerate()
.map(|(i, w)| {
if shared_bands || plate(w) || conveyed(w) {
merged[i]
} else {
None
}
})
.collect()
} else {
vec![None; wants.len()]
};
if wants
.iter()
.zip(&owner)
.any(|(w, owner)| plate(w) && owner.is_none())
{
plates::default_terms(analysis, &mut wants, &chosen);
}
let mut kept: Vec<_> = wants
.iter()
.cloned()
.zip(chosen.iter().copied())
.map(|(w, view)| (w, true, view, Vec::new()))
.collect();
let mut omitted = Vec::new();
plates::consolidate(analysis, &mut kept, &mut omitted);
bosses::consolidate(analysis, &mut kept, &mut omitted);
pmi_dimensions::yield_to_spec(spec, ¬es, &mut kept, &mut problems);
pmi_dimensions::hide_fit_limits(spec, ¬es, &mut kept);
precision::labels(spec, kept.iter_mut().map(|(w, ..)| w))?;
if let Ok(mut given) = tolerance::apply(analysis, spec, &mut kept, &omitted, &mut problems) {
holes::consolidate(analysis, &mut kept, &mut given, &mut omitted);
plates::share(analysis, &mut kept, &mut given, &mut omitted);
share_statements(analysis, &mut kept, &mut given, &mut omitted, true);
let before_pmi = omitted.len();
share_pmi(
analysis,
spec.sheet.references == Some(true),
&mut kept,
&mut given,
&mut omitted,
spec.units,
);
let pmi_covered: BTreeSet<String> =
omitted[before_pmi..].iter().map(|o| o.id.clone()).collect();
let references =
redundancy::consolidate(analysis, spec, &mut kept, &mut given, &mut omitted);
for o in &omitted {
if (references.contains_key(&o.id) || pmi_covered.contains(&o.id))
&& let State::Covered { by } = &o.state
&& let (Some(i), Some(j)) = (
wants.iter().position(|w| w.id == o.id),
wants.iter().position(|w| w.id == *by),
)
{
owner[i] = Some(j);
}
}
wording::apply(spec, &mut kept, &mut given)?;
}
for w in &mut wants {
if let Some((printed, ..)) = kept.iter().find(|(p, ..)| p.id == w.id) {
w.terms.clone_from(&printed.terms);
}
}
let (sides, lane) = placement::lanes(
analysis,
&frames,
wants.iter().zip(chosen.iter().copied()),
&owner,
);
let mut bands = placement::band_lanes(
chosen
.iter()
.zip(sides.iter().copied())
.zip(lane.iter().copied())
.enumerate()
.filter(|(i, _)| {
owner[*i].is_none()
&& (!shared_bands || matches!(wants[*i].shape, Shape::Linear { .. }))
})
.map(|(_, item)| item),
);
let front_linear = wants
.iter()
.enumerate()
.filter(|(i, w)| {
owner[*i].is_none()
&& chosen[*i] == Some("front")
&& sides[*i] == Some(placement::Side::Below)
&& matches!(w.shape, Shape::Linear { .. })
})
.map(|(i, _)| lane[i] + 1)
.max()
.unwrap_or(0);
let plan_linear = wants
.iter()
.enumerate()
.filter(|(i, w)| {
owner[*i].is_none()
&& chosen[*i] == Some("plan")
&& sides[*i] == Some(placement::Side::Above)
&& matches!(w.shape, Shape::Linear { .. })
})
.map(|(i, _)| lane[i] + 1)
.max()
.unwrap_or(0);
if wants
.iter()
.any(|w| crate::dims::is_envelope_extent(&w.id) && w.id.ends_with(".length"))
&& turned::turned(analysis, &QFrame(crate::bridge::holes::frame(analysis))).is_some()
{
bands.plate_front_below = bands.plate_front_below.max(front_linear);
}
for (i, w) in wants.iter().enumerate() {
if chosen[i] == Some("front")
&& matches!(w.shape, Shape::Callout { .. })
&& w.measurements
.iter()
.any(|m| m.parameter == "step.diameter")
{
let ink = label_ink(&w.terms, font());
bands.callout_front_left = bands.callout_front_left.max(leaders::shelf_width(&ink));
}
if owner[i].is_some()
|| !w
.measurements
.iter()
.any(|m| m.parameter == "thickness.length")
{
continue;
}
match (chosen[i], sides[i]) {
(Some("front"), Some(placement::Side::Left)) => {
bands.plate_front_left = bands.plate_front_left.max(lane[i] + 1);
let ink = label_ink(&w.terms, font());
bands.plate_front_top = bands
.plate_front_top
.max(ink.max[0] - ink.min[0] + ARROW + 3.0);
}
(Some("front"), Some(placement::Side::Below)) => {
bands.plate_front_below = bands.plate_front_below.max(front_linear)
}
(Some("side"), Some(placement::Side::Above | placement::Side::Below)) => {
bands.plate_side_left = ARROW + 2.0
}
(Some("side"), Some(placement::Side::Right)) => {
let ink = label_ink(&w.terms, font());
bands.plate_side_top = bands
.plate_side_top
.max(ink.max[0] - ink.min[0] + ARROW + 3.0);
}
(Some("plan"), Some(placement::Side::Above)) => {
bands.plate_plan_above = bands.plate_plan_above.max(plan_linear)
}
_ => {}
}
}
let notch_lanes = wants
.iter()
.zip(&chosen)
.filter(|(w, view)| {
**view == Some("plan")
&& (spec.dimension_source == DimensionSource::Auto
|| spec.dimensions.contains_key(&w.id))
&& w.measurements
.iter()
.any(|m| m.feature.starts_with("notch:"))
&& matches!(w.shape, Shape::Linear { axis, .. }
if placement::across(&frames["plan"], axis) == Some(false))
})
.count();
if notch_lanes > 0 {
let base = ((crate::layout::dim_pad() - crate::layout::lane_band(1)).max(0.) / lane_pitch())
.ceil() as usize
+ 1;
bands.authored_plan_left = bands.authored_plan_left.max(base + notch_lanes);
}
bands.recess_lanes = wants.iter().any(|w| {
w.measurements
.iter()
.any(|m| m.feature.starts_with("f:section_recess:"))
});
if bands.recess_lanes {
let footprints: BTreeMap<_, _> = frames
.iter()
.map(|(name, f)| (*name, footprints::Footprints::new(analysis, f, name)))
.collect();
let mut extra = BTreeMap::new();
for (i, w) in wants.iter().enumerate() {
if owner[i].is_some() || !matches!(w.shape, Shape::Linear { .. }) {
continue;
}
let (Some(view), Some(side)) = (chosen[i], sides[i]) else {
continue;
};
let Some(p) = placement::place(&frames[view], w, side, lane[i]) else {
continue;
};
if p.lines.iter().take(2).any(|line| {
footprints[view]
.crossed_extension(*line, &w.measurements)
.iter()
.any(|id| id.starts_with("f:section_recess:"))
}) {
*extra.entry((view, side)).or_insert(0) += 1;
}
}
for ((view, side), count) in extra {
let target = match (view, side) {
("front", placement::Side::Below) => &mut bands.plate_front_above,
("front", placement::Side::Above) => &mut bands.front_below,
("front", placement::Side::Left) => &mut bands.front_right,
("front", placement::Side::Right) => &mut bands.plate_front_left,
("plan", placement::Side::Below) => &mut bands.plan_above,
("plan", placement::Side::Above) => &mut bands.plate_plan_below,
("plan", placement::Side::Left) => &mut bands.plate_plan_right,
("plan", placement::Side::Right) => &mut bands.plan_left,
("side", placement::Side::Below) => &mut bands.side_above,
("side", placement::Side::Above) => &mut bands.side_below,
("side", placement::Side::Left) => &mut bands.side_right,
("side", placement::Side::Right) => &mut bands.plate_side_left_lanes,
_ => continue,
};
*target += count;
}
}
for (i, w) in wants.iter().enumerate() {
if callout_columns
&& matches!(w.shape, Shape::Callout { .. })
&& w.measurements
.iter()
.any(|m| m.feature.starts_with("f:section_recess:"))
&& let Some(view) = chosen[i]
&& let Some(k) = NAMES.iter().position(|v| *v == view)
{
let ink = label_ink(&w.terms, font());
bands.recess_callouts[k] = bands.recess_callouts[k]
.max(first_line() + SHOULDER + ink.max[0] - ink.min[0] + 2.0);
}
}
for (i, w) in wants.iter().enumerate() {
if callout_columns
&& owner[i].is_none()
&& matches!(w.shape, Shape::Linear { .. })
&& w.measurements
.iter()
.any(|m| m.feature.starts_with("f:section_recess:"))
&& let Some(k) = chosen[i].and_then(|view| NAMES.iter().position(|v| *v == view))
{
let ink = label_ink(&w.terms, font());
bands.recess_callouts[k] =
bands.recess_callouts[k].max(ink.max[0] - ink.min[0] + ARROW + 3.0);
}
}
if shared_bands
&& !excluded.is_empty()
&& wants.iter().enumerate().any(|(i, w)| {
chosen[i] == Some("front")
&& matches!(w.shape, Shape::Linear { axis: 0, .. })
&& w.measurements
.iter()
.any(|m| m.parameter.contains("location"))
})
{
bands.plate_front_above = bands.plate_front_above.max(1);
}
if shared_bands {
bands.recess_chain_scale = recess_chain_scale(analysis);
bands.plate_front_below = bands.plate_front_below.max(front_linear);
bands.plate_plan_above = bands.plate_plan_above.max(plan_linear);
}
let mut aligned = Vec::new();
for (i, w) in wants.iter().enumerate() {
if !matches!(w.shape, Shape::Aligned { .. }) || owner[i].is_some() {
continue;
}
if let (Some(view), Some(side)) = (chosen[i], sides[i]) {
let mut display = w.clone();
precision::labels(spec, std::iter::once(&mut display))?;
aligned.push(AlignedDemand::new(
&frames[view],
&display,
view,
side,
lane[i],
));
}
}
Ok((bands, aligned))
}
pub(crate) fn reserve_linear_intent(
analysis: &Analysis,
spec: &Spec,
views: &[(&str, P3, P3)],
excluded: &BTreeSet<String>,
bands: &mut crate::layout::Lanes,
) -> Result<Vec<crate::layout::CalloutBand>, Invalid> {
if !spec.dimensions.values().any(|d| {
d.place
.as_ref()
.is_some_and(|p| p.side.is_some() || p.lane.is_some())
}) {
return Ok(Vec::new());
}
let (mut wants, _, _) = pmi_policy::wants(analysis, spec);
callouts::apply(analysis, spec, &mut wants);
let (scheme_covered, _) = schemes::apply(analysis, spec, &mut wants)?;
authored::append(analysis, spec, &mut wants)?;
let mut printed: Vec<_> = wants
.into_iter()
.map(|w| (w, false, None, Vec::new()))
.collect();
precision::labels(spec, printed.iter_mut().map(|(w, ..)| w))?;
let mut given = tolerance::apply(analysis, spec, &mut printed, &[], &mut Vec::new())?;
wording::apply(spec, &mut printed, &mut given)?;
let mut wants: Vec<_> = printed.into_iter().map(|(w, ..)| w).collect();
if scheme_covered.is_empty() {
wants.retain(|w| !excluded.contains(&w.id));
} else {
wants.retain(|w| !excluded.contains(&w.id) && !scheme_covered.contains_key(&w.id));
}
let frames: BTreeMap<&str, Frame> = views
.iter()
.filter(|(name, ..)| NAMES.contains(name))
.map(|(name, direction, up)| {
(
*name,
Frame::looking(analysis, *direction, *up, [0.0; 2], 1.0),
)
})
.collect();
chosen_views(&mut wants, spec, &frames)?;
plates::prefer_readable(analysis, &mut wants, &frames, Some(spec));
let (chosen, _) = route(&mut wants, &frames, analysis);
let owner = placement::owners(analysis, spec, &wants, &chosen);
let (sides, lane) = placement::lanes(
analysis,
&frames,
wants.iter().zip(chosen.iter().copied()),
&owner,
);
let mut occupied: BTreeMap<&str, Vec<Vec<[P2; 2]>>> = BTreeMap::new();
let mut requested = Vec::new();
for (i, w) in wants.iter().enumerate() {
if !matches!(w.shape, Shape::Linear { .. }) || owner[i].is_some() {
continue;
}
let (Some(view), Some(side)) = (chosen[i], sides[i]) else {
continue;
};
if let Some(p) = spec
.dimensions
.get(&w.id)
.filter(|d| !d.omit)
.and_then(|d| d.place.as_ref())
.filter(|p| p.side.is_some() || p.lane.is_some())
{
if let Some(ink) = placement::intent(&frames[view], w, p, side, lane[i], 0.0) {
let span =
crate::output::linear_ink(&ink.lines).map_or(0.0, |l| l.span[1] - l.span[0]);
requested.push((i, p.lane.unwrap_or(lane[i]), span));
}
} else if let Some(ink) = placement::place(&frames[view], w, side, lane[i]) {
occupied.entry(view).or_default().push(ink.lines);
}
}
requested.sort_by(|a, b| {
(chosen[a.0], a.1)
.cmp(&(chosen[b.0], b.1))
.then(a.2.total_cmp(&b.2))
.then(a.0.cmp(&b.0))
});
let mut resolved_lanes = BTreeMap::new();
for (i, requested, _) in requested {
let view = chosen[i].expect("routed linear");
let side = sides[i].expect("linear strip");
let p = spec.dimensions[&wants[i].id]
.place
.as_ref()
.expect("explicit place");
let taken = occupied.entry(view).or_default();
for n in requested..requested + if p.pinned { 1 } else { 64 } {
let mut trial = p.clone();
trial.lane = Some(n);
let Some(ink) = placement::intent(&frames[view], &wants[i], &trial, side, lane[i], 0.0)
else {
break;
};
if p.pinned
|| !taken
.iter()
.any(|q| crate::output::shared_dimension_line(&ink.lines, q))
{
taken.push(ink.lines);
resolved_lanes.insert(i, n);
break;
}
}
}
let mut measured = Vec::new();
for (i, w) in wants.iter().enumerate() {
let Some(p) = spec
.dimensions
.get(&w.id)
.filter(|d| !d.omit)
.and_then(|d| d.place.as_ref())
.filter(|p| p.side.is_some() || p.lane.is_some())
else {
continue;
};
if !matches!(w.shape, Shape::Linear { .. }) {
continue;
}
let (Some(view), Some(default_side)) = (chosen[i], sides[i]) else {
continue;
};
let side = p.side.map_or(default_side, |s| match s {
crate::placement_intent::Side::Above => placement::Side::Above,
crate::placement_intent::Side::Below => placement::Side::Below,
crate::placement_intent::Side::Left => placement::Side::Left,
crate::placement_intent::Side::Right => placement::Side::Right,
});
let band = match (view, side) {
("front", placement::Side::Above) => &mut bands.plate_front_above,
("front", placement::Side::Below) => &mut bands.plate_front_below,
("front", placement::Side::Left) => &mut bands.plate_front_left,
("front", placement::Side::Right) => &mut bands.front_right,
("plan", placement::Side::Above) => &mut bands.plate_plan_above,
("plan", placement::Side::Below) => &mut bands.plate_plan_below,
("plan", placement::Side::Left) => &mut bands.authored_plan_left,
("plan", placement::Side::Right) => &mut bands.plate_plan_right,
("side", placement::Side::Above) => &mut bands.side_above,
("side", placement::Side::Below) => &mut bands.side_below,
("side", placement::Side::Left) => &mut bands.plate_side_left_lanes,
("side", placement::Side::Right) => &mut bands.side_right,
_ => continue,
};
*band = (*band).max(
resolved_lanes
.get(&i)
.copied()
.unwrap_or_else(|| p.lane.unwrap_or(lane[i]))
+ 1,
);
let mut intent = p.clone();
intent.lane = Some(
resolved_lanes
.get(&i)
.copied()
.unwrap_or_else(|| p.lane.unwrap_or(lane[i])),
);
if let Some(ink) = placement::intent(&frames[view], w, &intent, default_side, lane[i], 0.0)
{
let mut bounds = ink.label_box;
for point in ink.lines.iter().chain(&ink.arrows).flatten() {
for k in 0..2 {
bounds[k] = bounds[k].min(point[k]);
bounds[k + 2] = bounds[k + 2].max(point[k]);
}
}
let outline = frames[view].outline();
let (name, depth) = match side {
placement::Side::Left => ("left", outline[0] - bounds[0]),
placement::Side::Right => ("right", bounds[2] - outline[2]),
placement::Side::Below => ("below", outline[1] - bounds[1]),
placement::Side::Above => ("above", bounds[3] - outline[3]),
};
measured.push(crate::layout::CalloutBand {
view,
side: name,
depth,
});
}
}
Ok(measured)
}
pub(crate) fn authored_placement_clear(spec: &Spec, dims: &[PlannedDimension]) -> bool {
dims.iter().filter(|d| oblique_authored(spec, d)).all(|d| {
d.placed.as_ref().is_some_and(|p| {
dims.iter()
.filter(|q| q.id != d.id)
.filter_map(|q| q.placed.as_ref())
.all(|q| {
placement::annotations_clear(p, q) && placement::dimension_lines_clear(p, q)
})
})
})
}
fn oblique_authored(spec: &Spec, d: &PlannedDimension) -> bool {
spec.dimensions.get(&d.id).is_some_and(|entry| {
entry.between.is_some()
&& (d.kind == "angular"
|| (d.kind == "linear" && entry.along.as_deref().is_none_or(|a| a == "true")))
})
}
pub(crate) fn authored_placement_conflicts(
spec: &Spec,
dims: &[PlannedDimension],
) -> BTreeSet<(String, String)> {
let mut conflicts = BTreeSet::new();
if !spec
.dimensions
.values()
.any(|entry| entry.between.is_some())
{
return conflicts;
}
for d in dims.iter().filter(|d| oblique_authored(spec, d)) {
let Some(p) = &d.placed else {
conflicts.insert((d.id.clone(), d.id.clone()));
continue;
};
for q in dims.iter().filter(|q| q.id != d.id) {
if let Some(other) = &q.placed
&& (!placement::annotations_clear(p, other)
|| !placement::dimension_lines_clear(p, other))
{
let (a, b) = if d.id < q.id {
(&d.id, &q.id)
} else {
(&q.id, &d.id)
};
conflicts.insert((a.clone(), b.clone()));
}
}
}
conflicts
}
pub(crate) fn recess_chain_scale(analysis: &Analysis) -> f64 {
let (canonical, established, comparison, ..) = crate::planning::policy_key(analysis);
crate::planning::memo(
analysis,
"recess-readable-scale",
vec![
u8::from(canonical),
u8::from(established),
u8::from(comparison),
],
|| recess_chain_scale_uncached(analysis),
)
}
fn recess_chain_scale_uncached(analysis: &Analysis) -> f64 {
if !gdt::demand(analysis, &|_| true).is_empty() {
return 0.0;
}
let (items, ..) = wants(analysis);
let native = analysis
.recognition
.as_ref()
.is_some_and(|r| r.recogniser.starts_with("quiddity-rust "));
if !items.iter().any(|w| {
w.measurements
.iter()
.any(|m| m.feature.starts_with("f:section_recess:"))
}) {
return 0.0;
}
items
.iter()
.filter_map(|w| {
if w.value > 0.0
&& w.measurements.iter().any(|m| {
m.feature.starts_with("f:section_recess:")
&& m.parameter == "channel_width.length"
})
{
return native.then_some(crate::sheet_fit::MIN_OPENING / w.value);
}
let feature =
w.id.strip_suffix("/slot_length.length")
.or_else(|| w.id.strip_suffix("/slot_centres.length"))?;
if !items
.iter()
.any(|other| other.id == format!("{feature}/location_slot.length"))
|| w.value <= 0.0
{
return None;
}
let ink = label_ink(&w.terms, font());
Some((ink.max[0] - ink.min[0] + 2.0 * ARROW + 2.0) / w.value)
})
.fold(0.0, f64::max)
}
pub(crate) fn labels_on_spans(dims: &[PlannedDimension]) -> bool {
dims.iter().filter(|d| d.kind == "linear").all(|d| {
d.placed.as_ref().is_none_or(|p| {
!label_lanes::off_span(
p,
dims.iter()
.filter(|q| q.id != d.id && q.kind == "linear" && q.view == d.view)
.filter_map(|q| q.placed.as_ref()),
)
})
})
}
pub(crate) fn extension_contacts(dims: &[PlannedDimension]) -> usize {
let extensions: Vec<_> = dims
.iter()
.enumerate()
.filter_map(|(i, d)| {
let p = d.placed.as_ref()?;
(d.kind == "linear" && p.arrows.len() == 2 && p.lines.len() >= 3).then_some((i, p))
})
.flat_map(|(i, p)| p.lines[..2].iter().map(move |e| (i, *e)))
.collect();
dims.iter()
.filter(|d| d.kind == "callout")
.filter_map(|d| d.placed.as_ref())
.map(|p| leaders::extension_contacts(&extensions, &p.lines, p.label_box).0)
.sum()
}
fn unknown_dimensions(spec: &Spec, ids: &BTreeSet<&str>) -> Result<(), Invalid> {
let missing = spec
.dimensions
.keys()
.filter(|id| !ids.contains(id.as_str()))
.map(|id| {
let mut near: Vec<&str> = ids.iter().copied().collect();
near.sort_by_key(|c| (levenshtein(id, c), *c));
near.truncate(5);
let stem = id.rsplit_once('@').map(|(s, _)| s);
let reads = stem.filter(|s| ids.contains(s));
let mut choices: Vec<String> = reads.iter().map(|s| s.to_string()).collect();
for c in near {
if !choices.iter().any(|x| x == c) {
choices.push(c.into());
}
}
choices.truncate(5);
let message = match reads {
Some(s) => format!(
"There is no dimension {id:?} on this part: a dimension's ID no longer names its view (draftwright.spec/2), so it reads as {s:?}."
),
None => format!("There is no dimension {id:?} on this part."),
};
Invalid {
message,
choices,
path: None,
also: Vec::new(),
}
.at("dimensions", id)
});
Invalid::first_of(missing).map_or(Ok(()), Err)
}
pub(crate) fn check_known(analysis: &Analysis, spec: &Spec) -> Result<(), Invalid> {
if spec.dimensions.is_empty() {
return Ok(());
}
let (mut wants, _, _) = pmi_policy::wants(analysis, spec);
callouts::apply(analysis, spec, &mut wants);
schemes::apply(analysis, spec, &mut wants)?;
authored::append(analysis, spec, &mut wants)?;
profile_angles::apply(analysis, spec, &mut wants)?;
Ok(())
}
pub fn plan(
analysis: &Analysis,
spec: &Spec,
sheet: &PlannedSheet,
views: &[PlannedView],
) -> Result<Planned, Invalid> {
plan_with_obstacles(
analysis,
spec,
sheet,
views,
&[],
&[],
&footprints::Cache::default(),
)
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn plan_with_obstacles(
analysis: &Analysis,
spec: &Spec,
sheet: &PlannedSheet,
views: &[PlannedView],
obstacles: &[PlannedDimension],
held: &[PlannedDimension],
cache: &footprints::Cache,
) -> Result<Planned, Invalid> {
plan_with_obstacles_excluding(
analysis,
spec,
sheet,
views,
obstacles,
held,
cache,
&BTreeSet::new(),
)
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn plan_with_obstacles_excluding(
analysis: &Analysis,
spec: &Spec,
sheet: &PlannedSheet,
views: &[PlannedView],
obstacles: &[PlannedDimension],
held: &[PlannedDimension],
cache: &footprints::Cache,
excluded: &BTreeSet<String>,
) -> Result<Planned, Invalid> {
Ok(plan_inventory(
analysis, spec, sheet, views, obstacles, held, cache, excluded,
)?
.0)
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn plan_inventory(
analysis: &Analysis,
spec: &Spec,
sheet: &PlannedSheet,
views: &[PlannedView],
obstacles: &[PlannedDimension],
held: &[PlannedDimension],
cache: &footprints::Cache,
excluded: &BTreeSet<String>,
) -> Result<(Planned, Vec<PlannedDimension>), Invalid> {
if !crate::planning::enabled(analysis) {
return plan_inventory_uncached(
analysis, spec, sheet, views, obstacles, held, cache, excluded,
);
}
let mut geometry_spec = spec.clone();
if !has_recess_dimensions(analysis) {
geometry_spec.hole_table = crate::hole_table::Setting::Auto;
}
geometry_spec.sheet.title.clear();
geometry_spec.sheet.size = None;
geometry_spec.sheet.scale = None;
geometry_spec.views.retain(|name, view| {
let inactive = crate::detail::NAMES.contains(&name.as_str())
&& *view
== crate::model::ViewSpec {
omit: true,
..Default::default()
}
&& !views.iter().any(|v| v.id.strip_prefix("v:") == Some(name));
let named = spec.dimensions.values().any(|d| {
d.view.as_ref() == Some(name)
|| d.place.as_ref().and_then(|p| p.view.as_ref()) == Some(name)
}) || spec
.annotations
.values()
.any(|d| d.place.as_ref().and_then(|p| p.view.as_ref()) == Some(name));
!inactive || named
});
let mut geometry_sheet = sheet.clone();
geometry_sheet.title.clear();
geometry_sheet.title_cells.clear();
geometry_sheet.basis.clear();
geometry_sheet.arrangement.clear();
geometry_sheet.default = false;
geometry_sheet.fill = 0.0;
let mut geometry_views = views.to_vec();
for view in &mut geometry_views {
view.basis.clear();
}
use sha2::{Digest, Sha256};
let analysis_key: [u8; 32] =
crate::planning::memo(analysis, "analysis_digest", Vec::new(), || {
Sha256::digest(serde_json::to_vec(analysis).expect("Analysis serializes")).into()
});
let key = serde_json::to_vec(&(
analysis_key,
crate::planning::policy_key(analysis),
geometry_spec,
geometry_sheet,
geometry_views,
obstacles,
held,
excluded,
))
.expect("dimension placement inputs serialize");
crate::planning::memo(analysis, "dimension_placement", key.clone(), || {
type Inventory = Result<(Planned, Vec<PlannedDimension>), Invalid>;
static MEMO: crate::planning::BoundedMemo<Inventory> =
crate::planning::BoundedMemo::new(64);
let digest = crate::planning::spaced_digest(&Sha256::digest(&key).into());
if let Some(result) = MEMO.get(&digest) {
return result;
}
let result = plan_inventory_uncached(
analysis, spec, sheet, views, obstacles, held, cache, excluded,
);
MEMO.insert(digest, result.clone());
result
})
}
#[allow(clippy::too_many_arguments)]
fn plan_inventory_uncached(
analysis: &Analysis,
spec: &Spec,
sheet: &PlannedSheet,
views: &[PlannedView],
obstacles: &[PlannedDimension],
held: &[PlannedDimension],
cache: &footprints::Cache,
excluded: &BTreeSet<String>,
) -> Result<(Planned, Vec<PlannedDimension>), Invalid> {
let angles = |plan: &Planned| {
plan.0
.iter()
.filter(|d| {
d.placed.is_some()
&& d.measurements
.iter()
.any(|m| m.parameter == profile_angles::PARAMETER)
})
.count()
};
let findings = |plan: &Planned| {
plan.2
.iter()
.filter(|p| {
matches!(
p.code.as_str(),
"annotation_off_sheet"
| "annotation_over_title_block"
| "annotation_overlap"
| "annotation_ink_overlap"
| "annotation_over_view"
| "annotation_line_over_view"
| "annotation_leader_crossing"
| "annotation_outside_area"
| "dimension_line_shared"
| "dimension_text_stray"
| "leader_through_feature"
| "extension_through_feature"
)
})
.count()
+ leader_on_extension(&plan.0).len()
};
let mut profile_first = false;
let mut first = plan_inventory_ordered(
analysis, spec, sheet, views, obstacles, held, cache, excluded, 0, false,
)?;
if first.0.0.iter().any(|d| {
d.placed.is_none()
&& d.measurements
.iter()
.any(|m| m.parameter == profile_angles::PARAMETER)
}) {
let early = plan_inventory_ordered(
analysis, spec, sheet, views, obstacles, held, cache, excluded, 0, true,
)?;
if angles(&early.0) > angles(&first.0) && findings(&early.0) <= findings(&first.0) {
first = early;
profile_first = true;
}
}
let new = |d: &PlannedDimension| {
!d.measurements.is_empty()
&& d.measurements
.iter()
.all(|m| m.feature.starts_with("f:section_recess:"))
};
if !first.0.0.iter().any(|d| d.kind == "linear" && new(d))
|| !first.0.0.iter().any(|d| d.kind == "callout" && new(d))
|| spec.dimensions.values().any(|d| d.place.is_some())
{
return Ok(first);
}
let score = |plan: &Planned| {
let (dims, _, problems) = plan;
let hard = problems
.iter()
.filter(|p| {
matches!(
p.code.as_str(),
"annotation_off_sheet"
| "annotation_over_title_block"
| "annotation_overlap"
| "annotation_ink_overlap"
| "annotation_over_view"
| "annotation_line_over_view"
| "dimension_line_shared"
| "dimension_text_stray"
)
})
.count();
let extensions: Vec<_> = dims
.iter()
.enumerate()
.filter_map(|(i, d)| {
d.placed
.as_ref()
.filter(|p| d.kind == "linear" && p.lines.len() >= 3)
.map(|p| (i, p))
})
.flat_map(|(i, p)| p.lines[..2].iter().map(move |e| (i, *e)))
.collect();
let contacts: usize = dims
.iter()
.filter(|d| d.kind == "callout")
.filter_map(|d| d.placed.as_ref())
.map(|p| leaders::extension_contacts(&extensions, &p.lines, p.label_box).0)
.sum();
let span = dims
.iter()
.filter(|d| d.kind == "linear")
.filter_map(|d| d.placed.as_ref().map(|p| (d, p)))
.filter(|(d, p)| {
label_lanes::off_span(
p,
dims.iter()
.filter(|q| q.id != d.id && q.view == d.view && q.kind == "linear")
.filter_map(|q| q.placed.as_ref()),
)
})
.count();
let mut crossings = BTreeSet::new();
for p in problems {
if p.code == "leader_through_feature" {
crossings.insert((p.code.clone(), p.subjects.join("|")));
}
if p.code == "extension_through_feature" {
crossings.insert((
p.subjects[0].clone(),
p.id.rsplit(":line:").next().unwrap_or_default().into(),
));
}
}
let ink = problems
.iter()
.filter(|p| {
matches!(
p.code.as_str(),
"annotation_leader_crossing" | "annotation_outside_area"
)
})
.count();
let length: f64 = dims
.iter()
.filter(|d| d.kind == "callout")
.filter_map(|d| d.placed.as_ref())
.flat_map(|p| &p.lines)
.map(|[a, b]| (a[0] - b[0]).hypot(a[1] - b[1]))
.sum();
(
hard,
span,
contacts,
crossings.len(),
ink,
(length * 1000.0).round() as u64,
)
};
let native = analysis
.recognition
.as_ref()
.is_some_and(|r| r.recogniser.starts_with("quiddity-rust "));
let new_crossing = |plan: &Planned| {
plan.2.iter().any(|p| {
(p.code == "leader_through_feature"
|| (native && p.code == "extension_through_feature"))
&& p.subjects
.first()
.is_some_and(|id| plan.0.iter().any(|d| &d.id == id && new(d)))
})
};
let a = score(&first.0);
if a.0 == 0 && a.1 == 0 && !new_crossing(&first.0) {
return Ok(first);
}
let alternative = plan_inventory_ordered(
analysis,
spec,
sheet,
views,
obstacles,
held,
cache,
excluded,
1,
profile_first,
)?;
let mut best = if score(&alternative.0) < a {
alternative
} else {
first
};
if score(&best.0).0 > 0 || new_crossing(&best.0) {
let mut joint_held = held.to_vec();
joint_held.extend(
best.0
.0
.iter()
.filter(|d| {
d.kind == "callout"
&& new(d)
&& !best.0.2.iter().any(|p| {
p.subjects.contains(&d.id)
&& matches!(
p.code.as_str(),
"annotation_ink_overlap"
| "annotation_overlap"
| "annotation_over_view"
| "annotation_off_sheet"
| "leader_through_feature"
)
})
})
.cloned(),
);
let joint = plan_inventory_ordered(
analysis,
spec,
sheet,
views,
obstacles,
&joint_held,
cache,
excluded,
2,
profile_first,
)?;
if score(&joint.0) < score(&best.0) {
best = joint;
}
}
if native
&& !excluded.is_empty()
&& score(&best.0).0 == 0
&& best.0.2.iter().any(|p| {
p.code == "extension_through_feature"
&& p.subjects
.first()
.is_some_and(|id| best.0.0.iter().any(|d| &d.id == id && new(d)))
})
{
let preserved = plan_inventory_ordered(
analysis,
spec,
sheet,
views,
obstacles,
held,
cache,
excluded,
3,
profile_first,
)?;
if score(&preserved.0) < score(&best.0) {
best = preserved;
}
}
Ok(best)
}
#[allow(clippy::too_many_arguments)]
#[inline(never)]
fn plan_inventory_ordered(
analysis: &Analysis,
spec: &Spec,
sheet: &PlannedSheet,
views: &[PlannedView],
obstacles: &[PlannedDimension],
held: &[PlannedDimension],
cache: &footprints::Cache,
excluded: &BTreeSet<String>,
stage: u8,
profile_first: bool,
) -> Result<(Planned, Vec<PlannedDimension>), Invalid> {
let fmt = |v| crate::dims::fmt_in(v, spec.units);
let (mut wants, mut problems, notes) = pmi_policy::wants(analysis, spec);
if !spec.views.sections.is_empty()
&& views
.iter()
.any(|v| v.cut.is_some() && v.from_spec.iter().any(|f| f == "views/sections/0"))
{
sections::apply(analysis, spec, &mut wants);
}
callouts::apply(analysis, spec, &mut wants);
let (scheme_covered, scheme_problems) = schemes::apply(analysis, spec, &mut wants)?;
problems.extend(scheme_problems);
problems.extend(authored::append(analysis, spec, &mut wants)?);
profile_angles::apply(analysis, spec, &mut wants)?;
let retired: Vec<_> = if scheme_covered.is_empty() {
Vec::new()
} else {
let retired = wants
.iter()
.filter(|w| scheme_covered.contains_key(&w.id))
.cloned()
.collect();
wants.retain(|w| !scheme_covered.contains_key(&w.id));
retired
};
let frames: BTreeMap<&str, Frame> = views
.iter()
.filter_map(|v| {
let name = v.id.strip_prefix("v:")?;
(NAMES.contains(&name)
|| crate::detail::NAMES.contains(&name)
|| (!spec.views.sections.is_empty() && v.cut.is_some()))
.then(|| (name, Frame::new(analysis, v)))
})
.collect();
detail_routes(analysis, &mut wants, views);
let authored_ownership = authored::ownership(analysis, spec, &wants);
problems.extend(chosen_views(&mut wants, spec, &frames)?);
plates::prefer_readable(analysis, &mut wants, &frames, Some(spec));
let (mut chosen, mut end_on) = route(&mut wants, &frames, analysis);
let display_owners = if spec.precision.is_empty() {
Vec::new()
} else {
placement::owners(analysis, spec, &wants, &chosen)
};
precision::apply(spec, &mut wants)?;
for (i, owner) in display_owners.iter().enumerate() {
if let Some(j) = owner
&& (wants[i].value - wants[*j].value).abs() <= 1e-6
&& (precision::leading(spec, &wants[i]).is_some()
|| precision::leading(spec, &wants[*j]).is_some())
&& precision::leading(spec, &wants[i]).map_or_else(
|| fmt(wants[i].value),
|p| precision::format(wants[i].value, p),
) != precision::leading(spec, &wants[*j]).map_or_else(
|| fmt(wants[*j].value),
|p| precision::format(wants[*j].value, p),
)
{
return Err(Invalid {
path: None,
also: Vec::new(),
message: format!(
"conflicting precision for coincident dimensions {} and {}; shared measurements require matching display precision",
wants[i].id, wants[*j].id
),
choices: vec![],
});
}
}
let ids: BTreeSet<&str> = wants
.iter()
.chain(&retired)
.map(|w| w.id.as_str())
.collect();
if ids.len() != wants.len() + retired.len() {
return Err(Invalid {
path: None,
also: Vec::new(),
message: "Dimension IDs are not unique; the input has conflicting measurement owners."
.into(),
choices: vec![],
});
}
problems.extend(precision::unapplied(spec, &wants));
if let Err(e) = unknown_dimensions(spec, &ids) {
for path in e.path.iter().chain(&e.also) {
problems.push(Problem {
id: format!("p:dimension_edit_unapplied:{path}"),
subjects: vec![path.clone()],
code: "dimension_edit_unapplied".into(),
severity: "warning".into(),
message: "A dimension named by the saved Spec has no current measurement; its edit is retained but cannot be applied.".into(),
..Default::default()
});
}
}
let incremental = crate::incremental::dimensions();
let keep: Vec<_> = wants
.iter()
.map(|w| {
!excluded.contains(&w.id) && incremental.as_ref().is_none_or(|p| p.ids.contains(&w.id))
})
.collect();
let mut index = 0;
wants.retain(|_| {
let k = keep[index];
index += 1;
k
});
let mut index = 0;
chosen.retain(|_| {
let k = keep[index];
index += 1;
k
});
let mut index = 0;
end_on.retain(|_| {
let k = keep[index];
index += 1;
k
});
let all = wants.clone();
let mut owner = placement::owners(analysis, spec, &all, &chosen);
let mut omitted: Vec<_> = retired
.into_iter()
.map(|w| Omitted {
state: State::Covered {
by: scheme_covered[&w.id].clone(),
},
basis: format!(
"conveyed by {}: the Spec station network determines this interval",
scheme_covered[&w.id]
),
id: w.id,
label: crate::callouts::label(&w.terms),
})
.collect();
let mut kept = Vec::new();
let default_plan = (spec.dimension_source == DimensionSource::Authored
|| spec.dimensions.values().any(|d| d.omit))
.then(|| {
let mut defaults: Vec<_> = all
.iter()
.cloned()
.zip(chosen.iter().copied())
.map(|(w, view)| (w, true, view, Vec::new()))
.collect();
let mut unused = Vec::new();
plates::consolidate(analysis, &mut defaults, &mut unused);
bosses::consolidate(analysis, &mut defaults, &mut unused);
let mut nominal = vec![Vec::new(); defaults.len()];
holes::consolidate(analysis, &mut defaults, &mut nominal, &mut unused);
recesses::consolidate(analysis, &mut defaults, &mut nominal, &mut unused);
plates::share(analysis, &mut defaults, &mut nominal, &mut unused);
share_statements(analysis, &mut defaults, &mut nominal, &mut unused, true);
share_pmi(
analysis,
spec.sheet.references == Some(true),
&mut defaults,
&mut nominal,
&mut unused,
spec.units,
);
redundancy::consolidate(analysis, spec, &mut defaults, &mut nominal, &mut unused);
(defaults, unused)
});
let conveyed: BTreeSet<&str> = default_plan
.as_ref()
.map(|(_, unused)| {
unused
.iter()
.filter(|u| {
matches!(&u.state, State::Covered { by }
if correlated_set(analysis, by) != correlated_set(analysis, &u.id))
})
.map(|u| u.id.as_str())
.collect()
})
.unwrap_or_default();
let (with_set, set_problems) = correlated_sets(analysis, spec, &wants, &conveyed);
problems.extend(set_problems);
for ((w, view), along) in wants.into_iter().zip(chosen.iter().copied()).zip(end_on) {
let entry = spec.dimensions.get(&w.id);
if pmi_policy::strict(analysis, spec)
&& spec.sheet.references != Some(true)
&& !pmi_policy::authored(&w)
&& !(spec.dimension_source == DimensionSource::Authored && entry.is_some())
{
omitted.push(Omitted {
id: w.id,
label: crate::callouts::label(&w.terms),
basis: "authored: the strict PMI set withholds generated dimensions".into(),
state: State::NotAuthored,
});
continue;
}
if let Some(basis) = with_set.get(&w.id) {
match spec.dimension_source {
DimensionSource::Auto => omitted.push(Omitted {
label: crate::callouts::label(&w.terms),
id: w.id,
basis: basis.clone(),
state: State::OmittedByUser,
}),
DimensionSource::Authored => kept.push((w, true, view, along)),
}
continue;
}
match spec.dimension_source {
_ if entry.is_some_and(|e| e.omit) => omitted.push(Omitted {
label: crate::callouts::label(&w.terms),
id: w.id,
basis: "authored: the spec omits it".into(),
state: State::OmittedByUser,
}),
DimensionSource::Authored if entry.is_none() && !pmi_policy::authored(&w) => {
if let Some(by) = authored_ownership.covered.get(&w.id) {
omitted.push(Omitted {
label: crate::callouts::label(&w.terms), id: w.id,
basis: format!("spec-authored: this quantity is stated by {by}; the unlisted automatic candidate is withheld (one owner)"),
state: State::Covered { by: by.clone() },
});
continue;
}
problems.push(Problem {
id: format!("p:dimension_not_authored:{}", w.id),
code: "dimension_not_authored".into(),
severity: "warning".into(),
message: format!(
"{} is not drawn: the authored dimension set does not list it. Add it to the set to draw it.",
crate::wording::capital(&wording_of(&w))
),
subjects: vec![w.id.clone()],
fix: vec![Op::set(format!("dimensions/{}", crate::items::key(&w.id)), serde_json::json!({}))],
..Default::default()
});
omitted.push(Omitted {
label: crate::callouts::label(&w.terms),
id: w.id,
basis: "authored: not in the spec's complete set (dimension_source authored)"
.into(),
state: State::NotAuthored,
});
}
_ => kept.push((w, entry.is_none(), view, along)),
}
}
plates::consolidate(analysis, &mut kept, &mut omitted);
bosses::consolidate(analysis, &mut kept, &mut omitted);
pmi_dimensions::yield_to_spec(spec, ¬es, &mut kept, &mut problems);
pmi_dimensions::hide_fit_limits(spec, ¬es, &mut kept);
precision::labels(spec, kept.iter_mut().map(|(w, ..)| w))?;
let mut given = tolerance::apply(analysis, spec, &mut kept, &omitted, &mut problems)?;
holes::consolidate(analysis, &mut kept, &mut given, &mut omitted);
recesses::consolidate(analysis, &mut kept, &mut given, &mut omitted);
plates::share(analysis, &mut kept, &mut given, &mut omitted);
share_statements(analysis, &mut kept, &mut given, &mut omitted, true);
let before_pmi = omitted.len();
share_pmi(
analysis,
spec.sheet.references == Some(true),
&mut kept,
&mut given,
&mut omitted,
spec.units,
);
let index: BTreeMap<&str, usize> = all
.iter()
.enumerate()
.map(|(i, w)| (w.id.as_str(), i))
.collect();
for o in &omitted[before_pmi..] {
if let State::Covered { by } = &o.state
&& let (Some(&i), Some(&j)) = (index.get(o.id.as_str()), index.get(by.as_str()))
&& owner[i].is_none()
{
owner[i] = Some(owner[j].unwrap_or(j));
}
}
let references = redundancy::consolidate(analysis, spec, &mut kept, &mut given, &mut omitted);
if pmi_policy::carries(analysis) && spec.sheet.references == Some(true) {
for ((w, ..), authored) in kept.iter_mut().zip(&given) {
if authored.is_empty()
&& spec.dimension_source == DimensionSource::Auto
&& !pmi_policy::source_dimension(analysis, w)
&& !w.terms.join(" ").starts_with('(')
{
w.terms = vec![format!("({})", crate::callouts::label(&w.terms))];
w.basis += "; reference: an engine dimension not stated by the applied PMI";
}
}
}
problems.extend(pmi_dimensions::omitted(analysis, &all, &kept, &omitted));
let mut reference_terms = BTreeMap::new();
if let Some((defaults, unused)) = default_plan {
if spec.dimension_source == DimensionSource::Authored {
redundancy::authored_parity(&all, &defaults, &unused, &mut kept, &mut omitted);
}
reference_terms.extend(defaults.into_iter().filter_map(|(w, ..)| {
w.terms
.first()
.is_some_and(|t| t.starts_with('('))
.then_some((w.id, w.terms))
}));
}
wording::apply(spec, &mut kept, &mut given)?;
if let Some(policy) = &incremental {
for (w, ..) in &mut kept {
if let Some(old) = policy.held.get(&w.id) {
if w.measurements != old.measurements {
problems.extend(crate::incremental::basis_offers(
analysis,
&w.id,
&w.measurements,
));
}
w.value = old.value;
w.terms.clone_from(&old.terms);
w.measurements.clone_from(&old.measurements);
}
}
}
problems.extend(wording::check_covered(spec, &kept, &omitted));
let labels: BTreeMap<&str, &[String]> = kept
.iter()
.map(|(w, ..)| (w.id.as_str(), w.terms.as_slice()))
.collect();
let all: Vec<Want> = all
.into_iter()
.map(|mut w| {
if let Some((statement, ..)) = kept.iter().find(|(statement, ..)| statement.id == w.id)
{
w.measurements = statement.measurements.clone();
if w.measurements
.first()
.is_some_and(|m| m.parameter == "seat_diameter.diameter")
{
w.shape = statement.shape.clone();
}
}
if spec
.dimensions
.get(&w.id)
.is_none_or(|d| d.tolerance.is_none() && d.text.is_none() && d.style.is_none())
&& let Some(terms) = reference_terms.get(&w.id)
{
w.terms = terms.clone();
} else if let Some(terms) = labels.get(w.id.as_str()) {
w.terms = terms.to_vec();
}
w
})
.collect();
let (mut sides, mut lane) = placement::lanes(
analysis,
&frames,
all.iter().zip(chosen.iter().copied()),
&owner,
);
for (i, w) in all.iter().enumerate() {
if matches!(w.shape, Shape::Angular { .. })
&& spec
.dimensions
.get(&w.id)
.and_then(|e| e.place.as_ref())
.is_some_and(|p| p.side.is_some())
{
problems.push(Problem {id:format!("p:placement_side_not_applicable:{}",w.id),code:"placement_side_not_applicable".into(),severity:"warning".into(),message:format!("{} keeps its measured angular sector; place it with lane and along, without a side.",crate::wording::capital(&wording_of(w))),subjects:vec![w.id.clone()],fix:vec![Op::remove(format!("dimensions/{}/place/side",crate::items::key(&w.id)))],..Default::default()});
}
if spec
.dimensions
.get(&w.id)
.and_then(|e| e.place.as_ref())
.is_some_and(|p| p.offset.is_some())
&& !matches!(
w.shape,
Shape::Callout { .. } | Shape::Linear { .. } | Shape::Aligned { .. }
)
{
problems.push(warning(
"placement_unapplied",
&w.id,
format!(
"The placement offset of {} applies only to callouts and linear dimensions.",
wording_of(w)
),
vec![w.id.clone()],
));
}
if spec
.dimensions
.get(&w.id)
.and_then(|e| e.place.as_ref())
.is_some_and(|p| p.offset.is_some())
&& matches!(w.shape, Shape::Linear { .. } | Shape::Aligned { .. })
&& !held.iter().any(|d| {
d.id == w.id
&& d.placed.is_some()
&& chosen[i].is_some_and(|v| d.view == format!("v:{v}"))
})
{
problems.push(warning(
"placement_unapplied",
&w.id,
format!(
"{} has no automatic linear placement in the requested view",
wording_of(w)
),
vec![w.id.clone()],
));
}
if let Some(p) = spec.dimensions.get(&w.id).and_then(|e| e.place.as_ref())
&& let (Shape::Linear { axis, .. }, Some(side), Some(v)) = (&w.shape, p.side, chosen[i])
&& placement::across(&frames[v], *axis).is_some_and(|h| {
h != matches!(
side,
crate::placement_intent::Side::Above | crate::placement_intent::Side::Below
)
})
{
problems.push(warning(
"placement_unapplied",
&w.id,
format!(
"The side of {} must be parallel to its dimension line in {v}.",
wording_of(w)
),
vec![w.id.clone()],
));
}
}
let mut intended: Vec<Option<Placed>> = all
.iter()
.enumerate()
.map(|(i, w)| {
let p = spec.dimensions.get(&w.id)?.place.as_ref()?;
let v = chosen[i]?;
if let Some(offset) = p.offset {
let held = held
.iter()
.find(|d| d.id == w.id && d.view == format!("v:{v}"))
.and_then(|d| Some((d.placed.as_ref()?, d.terms.as_slice())));
return if matches!(w.shape, Shape::Callout { .. }) {
leaders::offset_intent(&frames[v], w, offset, held)
} else {
placement::offset_intent(&frames[v], w, offset, held)
};
}
if crate::placement_intent::automatic_pin(p)
&& let Some(d) = held
.iter()
.find(|d| d.id == w.id && d.view == format!("v:{v}"))
{
return d
.placed
.as_ref()
.map(|held| placement::relabel(held, &d.terms, w));
}
let callout_width = held
.iter()
.filter(|d| d.kind == "callout" && d.view == format!("v:{v}"))
.filter_map(|d| d.placed.as_ref())
.map(|p| p.label_box[2] - p.label_box[0])
.fold(0.0, f64::max);
let side = sides[i].or_else(|| {
matches!(w.shape, Shape::Angular { .. }).then_some(placement::Side::Below)
})?;
placement::intent(&frames[v], w, p, side, lane[i], callout_width)
})
.collect();
let places: Vec<_> = all
.iter()
.map(|w| spec.dimensions.get(&w.id).and_then(|e| e.place.as_ref()))
.collect();
let mut occupied: BTreeMap<&'static str, Vec<Vec<[P2; 2]>>> = BTreeMap::new();
for (i, w) in all.iter().enumerate() {
if intended[i].is_none()
&& owner[i].is_none()
&& matches!(w.shape, Shape::Linear { .. })
&& let (Some(v), Some(side)) = (chosen[i], sides[i])
&& let Some(p) = placement::place(&frames[v], w, side, lane[i])
{
occupied.entry(v).or_default().push(p.lines);
}
}
let page = placement::Page::new(analysis, sheet, views);
placement::respace(
&frames,
&all,
&chosen,
&places,
&sides,
&lane,
&mut occupied,
&page,
&mut intended,
);
let prescribed = intended.clone();
let mut locked: Vec<_> = intended.iter().map(Option::is_some).collect();
let pin_ink: Vec<_> = all
.iter()
.zip(&prescribed)
.filter_map(|(w, p)| {
spec.dimensions
.get(&w.id)?
.place
.as_ref()?
.pinned
.then_some((w.id.as_str(), p.as_ref()?))
})
.chain(
obstacles
.iter()
.filter_map(|d| Some((d.id.as_str(), d.placed.as_ref()?))),
)
.collect();
for (i, w) in all.iter().enumerate() {
if let (Some(side), Some(v), Some(placed)) = (
spec.dimensions
.get(&w.id)
.and_then(|e| e.place.as_ref())
.and_then(|p| p.side),
chosen[i],
intended[i].as_mut(),
) {
let view = views
.iter()
.find(|view| view.id == format!("v:{v}"))
.unwrap();
leaders::route_intent(
&cache.get(analysis, view),
&frames[v],
w,
placed,
side,
&pin_ink,
spec.dimensions[&w.id].place.as_ref().unwrap().pinned,
);
}
}
placement::fall_through(
&frames, &all, &chosen, &owner, &mut sides, &mut lane, analysis, sheet, views,
);
let drawn: Vec<bool> = all
.iter()
.map(|w| labels.contains_key(w.id.as_str()))
.collect();
let alone = placement::separate(&owner, &drawn, &chosen, &sides, &mut lane);
let has_baseline = all.iter().any(|w| w.id.ends_with("/step_position.length"));
let local_tolerances: std::collections::BTreeSet<usize> = all
.iter()
.enumerate()
.filter(|(i, w)| {
has_baseline
&& owner[*i].is_some()
&& drawn[*i]
&& w.id.ends_with("/step.length")
&& spec
.dimensions
.get(&w.id)
.is_some_and(|d| d.tolerance.is_some() && d.place.is_none())
})
.map(|(i, _)| i)
.collect();
let mut locals: Vec<_> = local_tolerances.iter().copied().collect();
locals.sort_by(|&i, &j| all[i].value.total_cmp(&all[j].value).then(i.cmp(&j)));
let mut next = BTreeMap::new();
for i in locals {
let Some((view, side)) = chosen[i].zip(sides[i]) else {
continue;
};
let opposite = match side {
placement::Side::Right => placement::Side::Left,
placement::Side::Left => placement::Side::Right,
placement::Side::Above => placement::Side::Below,
placement::Side::Below => placement::Side::Above,
};
let n = next.entry((view, opposite)).or_insert_with(|| {
(0..all.len())
.filter(|j| {
chosen[*j] == Some(view)
&& sides[*j] == Some(opposite)
&& matches!(all[*j].shape, Shape::Linear { .. })
&& owner[*j].is_none()
})
.map(|j| lane[j] + 1)
.max()
.unwrap_or(0)
});
sides[i] = Some(opposite);
lane[i] = *n;
*n += 1;
}
let mut placed: Vec<Option<Placed>> = all
.iter()
.zip(&chosen)
.zip(sides.iter().zip(&lane))
.zip(&owner)
.enumerate()
.map(|(i, (((w, view), (side, lane)), owner))| {
view.zip(*side)
.filter(|_| owner.is_none() || local_tolerances.contains(&i))
.and_then(|(v, side)| placement::place(&frames[v], w, side, *lane))
})
.collect();
for i in 0..all.len() {
if alone[i]
&& let (Some(view), Some(side)) = (chosen[i], sides[i])
{
placed[i] = placement::place(&frames[view], &all[i], side, lane[i]);
}
}
for (i, p) in intended.iter().enumerate() {
if p.is_some() {
placed[i] = p.clone();
} else if owner[i].is_none()
&& matches!(all[i].shape, Shape::Linear { .. })
&& let Some(d) = held.iter().find(|d| {
d.id == all[i].id && chosen[i].is_some_and(|v| d.view == format!("v:{v}"))
})
&& let Some(p) = &d.placed
{
placed[i] = Some(placement::relabel(p, &d.terms, &all[i]));
}
}
let fixed: Vec<bool> = all
.iter()
.map(|w| {
spec.dimensions
.get(&w.id)
.and_then(|e| e.place.as_ref())
.is_some_and(|p| p.pinned)
})
.collect();
placement::avoid_fixed(
analysis,
&frames,
&all,
&chosen,
&mut sides,
&mut lane,
&fixed,
&places,
&drawn,
&mut placed,
obstacles,
&page,
);
placement::settle_fixed(&mut placed, &fixed, obstacles, analysis, sheet, views);
let recess_context = all.iter().any(|w| {
w.measurements
.iter()
.any(|m| m.feature.starts_with("f:section_recess:"))
});
for (i, w) in all.iter().enumerate() {
if placement::automatic_slot_layout(analysis)
&& intended[i].is_none()
&& let (Some(view), Some(p)) = (chosen[i], placed[i].as_mut())
{
placement::nearest_pattern_witnesses(analysis, &frames[view], &w.measurements, p);
}
}
let radius_incumbents = held.iter().any(|d| {
d.placed.is_some()
&& d.measurements.iter().any(|m| {
matches!(
m.parameter.as_str(),
"fillet.radius" | "blend.radius" | "cylindrical_profile.radius"
)
})
});
let mut incumbents = vec![false; all.len()];
let nominal_context = leaders::nominal_context(analysis, spec, &all);
let nominal_seeded = if matches!(stage, 0 | 3)
&& (recess_context || nominal_context)
&& intended.iter().all(Option::is_none)
{
leaders::seed_established(analysis, spec, views, &all, &chosen, &mut placed, &page)
} else {
false
};
for (i, w) in all.iter().enumerate() {
if radius_incumbents
&& !crate::nominal::comparison()
&& intended[i].is_none()
&& matches!(w.shape, Shape::Callout { .. })
&& spec.dimensions.get(&w.id).is_none_or(|d| {
d.place.is_none() && d.tolerance.is_none() && d.text.is_none() && d.style.is_none()
})
&& let Some(d) = held.iter().find(|d| {
d.id == w.id
&& d.terms == w.terms
&& d.measurements == w.measurements
&& chosen[i].is_some_and(|v| d.view == format!("v:{v}"))
})
&& d.placed.is_some()
{
placed[i] = d.placed.clone();
locked[i] = true;
incumbents[i] = true;
}
}
let repair_context = recess_context
|| nominal_seeded
|| spec
.dimensions
.values()
.any(|d| d.tolerance.is_some() || d.text.is_some() || d.style.is_some())
|| spec.features.values().any(|f| f.fit.is_some());
let crossed: Vec<_> = all
.iter()
.enumerate()
.map(|(i, w)| {
if !placement::physical_witness_retry(analysis)
|| !matches!(w.shape, Shape::Linear { .. })
{
return false;
}
let (Some(name), Some(p)) = (chosen[i], placed[i].as_ref()) else {
return false;
};
let Some(view) = views.iter().find(|v| v.id.strip_prefix("v:") == Some(name)) else {
return false;
};
let footprints = cache.get(analysis, view);
let counter = footprints.extension_counter(&w.measurements);
p.lines.iter().take(2).any(|line| counter.count(*line) > 0)
})
.collect();
if (repair_context || crossed.iter().any(|c| *c)) && intended.iter().all(Option::is_none) {
let repair_locked: Vec<_> = locked
.iter()
.zip(&crossed)
.map(|(locked, crossed)| *locked || (!repair_context && !crossed))
.collect();
placement::clear_linear(
analysis,
cache,
views,
&frames,
&all,
&chosen,
&mut sides,
&mut lane,
&mut placed,
obstacles,
&repair_locked,
&page,
stage != 3,
);
}
let early_chosen: Vec<_> = all
.iter()
.enumerate()
.map(|(i, w)| {
if !matches!(w.shape, Shape::Linear { .. }) {
return None;
}
if interior::through_notch(w) {
return chosen[i];
}
if !placement::automatic_slot_layout(analysis) || recess_context {
return None;
}
let (Some(name), Some(p)) = (chosen[i], placed[i].as_ref()) else {
return None;
};
let v = views
.iter()
.find(|v| v.id.strip_prefix("v:") == Some(name))?;
let f = cache.get(analysis, v);
p.lines
.iter()
.take(2)
.any(|l| !f.crossed_extension(*l, &w.measurements).is_empty())
.then_some(name)
})
.collect();
let early_interior = if early_chosen.iter().any(Option::is_some) {
interior::place(
analysis,
spec,
views,
&frames,
&all,
&early_chosen,
&sides,
&mut placed,
&page,
obstacles,
cache,
true,
false,
None,
)
} else {
BTreeSet::new()
};
for (i, w) in all.iter().enumerate() {
if !early_interior.is_empty()
&& matches!(w.shape, Shape::Callout { .. })
&& placed[i].is_none()
&& intended[i].is_none()
{
locked[i] = false;
incumbents[i] = false;
}
}
let shared_owner: Vec<_> = owner
.iter()
.zip(&alone)
.map(|(owner, alone)| if *alone { None } else { *owner })
.collect();
placement::share_lanes(&shared_owner, &mut sides, &mut lane);
let body_fixed: Vec<bool> = all
.iter()
.enumerate()
.map(|(i, w)| {
fixed[i]
|| spec
.dimensions
.get(&w.id)
.is_some_and(|entry| entry.place.is_some())
})
.collect();
problems.extend(placement::body_chains(
analysis,
&frames,
&all,
&chosen,
&body_fixed,
&mut placed,
&page,
));
let interior_proofs = interior::proofs(analysis, views);
let opening_proofs = all
.iter()
.any(interior::through_notch)
.then(|| interior::callout_proofs(analysis, views));
let statement_proof = |opening: bool, id: &str| {
if opening {
opening_proofs.as_ref().and_then(|proofs| proofs.get(id))
} else {
interior_proofs.get(id)
}
};
let notch_void = |view: &str, p: &Placed| {
let Some(frame) = frames.get(view) else {
return false;
};
let b = frame.outline();
if !page.fits_interior(p, view)
|| !placement::ink_points(p)
.all(|q| q[0] >= b[0] && q[0] <= b[2] && q[1] >= b[1] && q[1] <= b[3])
{
return false;
}
views
.iter()
.find(|v| v.id == format!("v:{view}"))
.is_some_and(|v| {
interior_proofs
.get(&v.id)
.is_some_and(|proof| proof.exterior_annotation_clear(v, p))
})
};
if profile_first {
angular::place_profile(
&frames,
&all,
&chosen,
obstacles,
&mut placed,
&page,
views,
&interior_proofs,
);
}
angular::place(
&frames,
&all,
&chosen,
&sides,
&fixed,
obstacles,
&mut placed,
&page,
¬ch_void,
);
let mut callout_views = chosen.clone();
for (i, w) in all.iter().enumerate() {
if owner[i].is_some()
&& !drawn[i]
&& matches!(w.shape, Shape::Callout { .. })
&& w.measurements
.iter()
.any(|m| m.feature.starts_with("f:section_recess:"))
{
callout_views[i] = None;
}
}
let mut prior = callout_views.clone();
for (i, w) in all.iter().enumerate() {
if incumbents[i]
|| interior::through_notch(w)
|| w.measurements.iter().any(|m| m.parameter == "blend.radius")
|| matches!(w.shape, Shape::Callout { .. })
&& w.measurements.iter().any(|m| {
m.feature.starts_with("f:section_recess:")
|| (m.feature.starts_with("f:modelled_thread:")
&& matches!(&w.shape, Shape::Callout { sites, .. }
if sites.iter().any(|(_, radius)| *radius > 0.)))
})
{
prior[i] = None;
}
}
let recess_linear: Vec<_> = if recess_context {
all.iter()
.enumerate()
.filter(|(i, w)| {
!locked[*i]
&& matches!(w.shape, Shape::Linear { .. })
&& !w.measurements.is_empty()
&& w.measurements
.iter()
.all(|m| m.feature.starts_with("f:section_recess:"))
})
.map(|(i, _)| (i, placed[i].take()))
.collect()
} else {
Vec::new()
};
let mut leader_problems = if obstacles.is_empty() {
leaders::place(
analysis,
spec,
cache,
views,
&frames,
&all,
&prior,
&sides,
&mut placed,
&page,
)
} else {
leaders::place_with_obstacles(
analysis,
spec,
cache,
views,
&frames,
&all,
&prior,
&sides,
&mut placed,
&page,
obstacles,
)
};
if !recess_linear.is_empty() {
let mut established = chosen.clone();
for (i, w) in all.iter().enumerate() {
if w.measurements
.iter()
.any(|m| m.feature.starts_with("f:section_recess:"))
{
established[i] = None;
}
}
leader_problems.extend(leaders::place_with_obstacles(
analysis,
spec,
cache,
views,
&frames,
&all,
&established,
&sides,
&mut placed,
&page,
obstacles,
));
}
let mut interior = if !recess_linear.is_empty() {
let mut existing = chosen.clone();
for (i, w) in all.iter().enumerate() {
if !matches!(w.shape, Shape::Callout { .. })
|| w.measurements
.iter()
.any(|m| m.feature.starts_with("f:section_recess:"))
{
existing[i] = None;
}
}
interior::place(
analysis,
spec,
views,
&frames,
&all,
&existing,
&sides,
&mut placed,
&page,
obstacles,
cache,
false,
false,
None,
)
} else {
BTreeSet::new()
};
interior.extend(early_interior);
let restore = |placed: &mut [Option<Placed>],
sides: &mut [Option<placement::Side>],
lane: &mut [usize]| {
if recess_linear.is_empty() {
return;
}
for (i, p) in &recess_linear {
placed[*i] = p.clone();
}
placement::clear_linear(
analysis,
cache,
views,
&frames,
&all,
&chosen,
sides,
lane,
placed,
obstacles,
&locked,
&page,
stage != 3,
);
placement::share_lanes(&shared_owner, sides, lane);
};
if matches!(stage, 1 | 3) {
restore(&mut placed, &mut sides, &mut lane);
}
if stage == 2 {
for (i, w) in all.iter().enumerate() {
if matches!(w.shape, Shape::Callout { .. })
&& w.measurements
.iter()
.any(|m| m.feature.starts_with("f:section_recess:"))
&& let Some(d) = held.iter().find(|d| {
d.id == w.id
&& d.terms == w.terms
&& chosen[i].is_some_and(|v| d.view == format!("v:{v}"))
})
{
placed[i] = d.placed.clone();
}
}
}
let witnesses: Vec<_> = if stage == 2 {
placed
.iter_mut()
.enumerate()
.filter_map(|(i, p)| {
if !matches!(all[i].shape, Shape::Linear { .. }) {
return None;
}
Some((i, p.take()?))
})
.collect()
} else {
Vec::new()
};
if prior != callout_views {
leader_problems.extend(leaders::place_with_obstacles(
analysis,
spec,
cache,
views,
&frames,
&all,
&callout_views,
&sides,
&mut placed,
&page,
obstacles,
));
}
for (i, p) in witnesses {
placed[i] = Some(p);
}
if !matches!(stage, 1 | 3) {
restore(&mut placed, &mut sides, &mut lane);
}
placement::place_merged(
&frames,
&all,
&chosen,
&owner,
&alone,
&drawn,
&sides,
&lane,
&mut placed,
&page,
placement::automatic_slot_layout(analysis),
);
if !local_tolerances.is_empty() {
placement::settle_fixed(&mut placed, &fixed, obstacles, analysis, sheet, views);
}
placement::clear_aligned(
&frames,
&all,
&chosen,
&sides,
&fixed,
&mut placed,
obstacles,
&page,
);
for (i, w) in all.iter().enumerate() {
if !fixed[i]
&& matches!(w.shape, Shape::Angular { .. })
&& spec
.dimensions
.get(&w.id)
.is_some_and(|d| d.between.is_some())
{
placed[i] = None;
}
}
angular::place(
&frames,
&all,
&chosen,
&sides,
&fixed,
obstacles,
&mut placed,
&page,
¬ch_void,
);
interior.extend(interior::place(
analysis,
spec,
views,
&frames,
&all,
&chosen,
&sides,
&mut placed,
&page,
obstacles,
cache,
false,
false,
None,
));
let refined = leaders::refine(
analysis,
spec,
cache,
sheet,
views,
&frames,
&all,
&chosen,
&mut placed,
&page,
obstacles,
&interior_proofs,
);
let mut leader_problems: Vec<_> = leader_problems
.into_iter()
.filter(|p| {
!p.subjects.iter().any(|s| {
interior.contains(s)
|| (matches!(
p.code.as_str(),
"annotation_line_over_view" | "annotation_leader_long"
) && refined.contains_key(s))
})
})
.collect();
leader_problems.extend(refined.into_values().flatten());
for (i, w) in all.iter().enumerate().filter(|(i, _)| incumbents[*i]) {
if let (Some(name), Some(p)) = (chosen[i], &placed[i])
&& let Some(v) = views.iter().find(|v| v.id.trim_start_matches("v:") == name)
&& let Some(shaft) = p.lines.first()
{
leader_problems.retain(|q| {
!matches!(
q.code.as_str(),
"annotation_line_over_view" | "annotation_leader_long"
) || q.subjects.first() != Some(&w.id)
});
leader_problems.extend(leaders::route_reports(
w,
name,
cache.get(analysis, v).solids(),
*shaft,
));
}
}
let retry_angles = if profile_first || all.iter().any(interior::through_notch) {
angular::withdraw_profile_contacts(&all, &mut placed, spec)
} else {
Vec::new()
};
angular::place_profile(
&frames,
&all,
&chosen,
obstacles,
&mut placed,
&page,
views,
&interior_proofs,
);
for (i, old) in retry_angles {
if placed[i].is_none() {
placed[i] = Some(old);
}
}
let inventory = all
.iter()
.enumerate()
.filter_map(|(i, w)| {
if owner[i].is_some()
&& !alone[i]
&& !(drawn[i] && matches!(w.shape, Shape::Callout { .. }))
{
return None;
}
let view = format!("v:{}", chosen[i]?);
let region = placed[i]
.as_ref()
.filter(|p| {
views.iter().find(|v| v.id == view).is_some_and(|v| {
statement_proof(interior::through_notch(w), &v.id).is_some_and(|proof| {
(if interior::through_notch(w)
&& matches!(w.shape, Shape::Linear { .. })
{
proof.opening_linear_clear(v, p)
} else {
proof.annotation_clear(
v,
p,
matches!(w.shape, Shape::Callout { .. }),
)
}) || (w.measurements.iter().any(|m| m.parameter == "notch.angle")
&& notch_void(v.id.trim_start_matches("v:"), p))
})
})
})
.map(|_| CandidateRegion::Interior);
Some(PlannedDimension {
callout_offset: None,
reference: false,
determined_by: Vec::new(),
region,
id: w.id.clone(),
kind: match w.shape {
Shape::Linear { .. } | Shape::Aligned { .. } => "linear",
Shape::Callout { .. } => "callout",
Shape::Angular { .. } => "angular",
}
.into(),
view,
value: w.value,
label: crate::callouts::label(&w.terms),
terms: w.terms.clone(),
measurements: w.measurements.clone(),
default: true,
from_spec: vec![],
basis: w.basis.clone(),
state: State::Shown,
placed: placed[i].clone(),
})
})
.collect();
let geometry_terms: BTreeMap<_, _> = all
.iter()
.map(|w| (w.id.clone(), w.terms.clone()))
.collect();
let witnesses: BTreeMap<String, Shape> = all
.iter()
.filter(|w| omitted.iter().any(|o| o.id == w.id))
.map(|w| (w.id.clone(), w.shape.clone()))
.collect();
let mut positions: BTreeMap<String, Option<Placed>> =
all.into_iter().map(|w| w.id).zip(placed).collect();
type Undrawn = (Vec<&'static str>, Vec<&'static str>);
let mut missing: BTreeMap<Undrawn, Vec<String>> = BTreeMap::new();
let mut out = Vec::new();
for ((mut w, default, chosen, along), mut from_spec) in kept.into_iter().zip(given) {
if !spec.views.sections.is_empty() && w.basis.contains("applied views/sections/0") {
from_spec.push("views/sections/0".into());
}
if !spec.dimension_schemes.is_empty()
&& let Some(path) = schemes::from_spec(&w)
{
from_spec.push(path);
}
if spec
.dimensions
.get(&w.id)
.is_some_and(|d| d.between.is_some())
{
from_spec.push("between".into());
if spec.dimensions[&w.id].kind.is_some() {
from_spec.push("kind".into());
}
if spec.dimensions[&w.id].along.is_some() {
from_spec.push("along".into());
}
}
if from_spec.is_empty()
&& spec
.dimensions
.get(&w.id)
.and_then(|d| d.place.as_ref())
.is_some_and(|p| p.pinned)
&& let Some(previous) = held.iter().find(|d| d.id == w.id)
&& previous.label == format!("({})", w.terms.join(" "))
{
w.terms = previous.terms.clone();
w.basis += "; reference retained by its pin after the controlling chain was edited";
problems.push(Problem {
id: format!("p:pinned_reference_chain_changed:{}", w.id),
code: "pinned_reference_chain_changed".into(),
severity: "warning".into(),
message: format!("{} stays reference to preserve its pinned ink, but its controlling chain was edited. Release the pin to make it controlling, or restore the chain.", crate::wording::capital(&wording_of(&w))),
subjects: vec![w.id.clone()],
fix: vec![Op::remove(format!("dimensions/{}/place", crate::items::key(&w.id)))],
..Default::default()
});
}
let placed = positions
.remove(&w.id)
.flatten()
.map(|p| placement::relabel(&p, &geometry_terms[&w.id], &w))
.map(|p| {
if spec.dimension_schemes.is_empty() {
p
} else {
schemes::ink(&w, p)
}
})
.map(|p| match chosen.and_then(|v| frames.get(v)) {
Some(frame) => slots::nearest_witness(
&w,
p,
omitted
.iter()
.filter(|o| {
matches!(&o.state, State::Covered { by } if *by == w.id)
&& o.basis.contains("locates the same span")
})
.filter_map(|o| witnesses.get(&o.id)),
frame,
),
None => p,
});
if let Some(v) = spec.dimensions.get(&w.id).and_then(|e| e.view.as_deref())
&& chosen == Some(v)
{
from_spec.push("view".into());
}
if spec
.dimensions
.get(&w.id)
.is_some_and(|e| e.place.is_some())
{
from_spec.push("place".into());
}
let view = chosen.unwrap_or(w.views[0]);
let dropped_angle =
placed.is_none() && chosen.is_some() && profile_angles::is_profile_angle(&w);
if dropped_angle {
problems.push(Problem {
id: format!("p:angular_dimension_dropped:{}", w.id),
code: "angular_dimension_dropped".into(),
severity: "info".into(),
message: format!(
"{} is not drawn: no clear place was found for it in the view that shows it true. Its corner stays undefined; dimension it linearly (faces/<face>/profile_corners: linear) or make room.",
crate::wording::capital(&wording_of(&w))
),
subjects: vec![w.id.clone()],
..Default::default()
});
} else if placed.is_none() {
missing
.entry((w.views.clone(), along))
.or_default()
.push(w.id.clone());
}
let opening = interior::through_notch(&w);
let mut basis = if default {
w.basis
} else {
format!("listed in the spec; {}", w.basis)
};
let region = placed
.as_ref()
.filter(|p| {
views
.iter()
.find(|v| v.id == format!("v:{view}"))
.is_some_and(|v| {
statement_proof(opening, &v.id).is_some_and(|proof| {
(if opening && matches!(w.shape, Shape::Linear { .. }) {
proof.opening_linear_clear(v, p)
} else {
proof.annotation_clear(
v,
p,
matches!(w.shape, Shape::Callout { .. }),
)
}) || (w.measurements.iter().any(|m| m.parameter == "notch.angle")
&& notch_void(v.id.trim_start_matches("v:"), p))
})
})
})
.map(|_| CandidateRegion::Interior);
if region.is_some() {
basis += interior::BASIS;
}
let reference = matches!(&w.terms[..], [t] if t.starts_with('(') && t.ends_with(')'));
let determined_by = if reference {
references.get(&w.id).cloned().unwrap_or_default()
} else {
Vec::new()
};
if let Some(claims) = authored_ownership.claims.get(&w.id) {
w.measurements.extend(claims.iter().cloned());
if !claims.is_empty() {
basis += "; retains the measured roles of unlisted automatic candidates";
}
}
out.push(PlannedDimension {
callout_offset: None,
reference,
determined_by,
region,
id: w.id,
kind: match w.shape {
Shape::Linear { .. } | Shape::Aligned { .. } => "linear",
Shape::Callout { .. } => "callout",
Shape::Angular { .. } => "angular",
}
.into(),
view: format!("v:{view}"),
value: w.value,
label: crate::callouts::label(&w.terms),
terms: w.terms,
measurements: w.measurements,
default: default && from_spec.is_empty(),
from_spec,
basis,
state: State::placed_or(
placed.is_some(),
if dropped_angle {
"angular_dimension_dropped"
} else {
"view_plan_incomplete"
},
),
placed,
});
}
for ((candidates, along), ids) in missing {
let n = ids.len();
let s = if n == 1 { "" } else { "s" };
let named = |v: &[&str]| v.join(" or ");
let (omitted, dropped): (Vec<&str>, Vec<&str>) = candidates
.iter()
.copied()
.filter(|v| !along.contains(v))
.partition(|v| spec.views.get(*v).is_some_and(|s| s.omit));
let mut why = Vec::new();
if !omitted.is_empty() {
why.push(format!(
"the {} view{} omitted",
named(&omitted),
if omitted.len() == 1 { " is" } else { "s are" }
));
}
if !dropped.is_empty() {
why.push(if dropped.iter().all(|v| idle_dropped() == Some(*v)) {
format!(
"the {} view is left out as adding only its outline (set views/{} to {{}} to keep it)",
named(&dropped),
dropped[0]
)
} else {
format!(
"the {} view is left out as the turned part's repeated profile",
named(&dropped)
)
});
}
if !along.is_empty() {
why.push(format!(
"the {} view looks along {}",
named(&along),
if n == 1 { "it" } else { "them" }
));
}
let (how, fix) = if let Some(v) = omitted.first() {
(
format!("Restore the view (remove views/{v}/omit) or omit them."),
vec![Op::remove(format!("views/{v}/omit"))],
)
} else if let Some(v) = dropped.first() {
(
format!("Keep the view (set views/{v} to {{}}) or omit them."),
Vec::new(),
)
} else {
(
"Omit them, or show a view across them.".to_string(),
Vec::new(),
)
};
problems.push(Problem {
id: format!("p:view_plan_incomplete:v:{}", candidates.join("+v:")),
code: "view_plan_incomplete".into(),
severity: "error".into(),
message: format!(
"{n} dimension{s} can only be drawn in the {} view, but {}; they stay in the plan undrawn. {how}",
named(&candidates),
why.join(" and ")
),
subjects: ids,
fix,
..Default::default()
});
}
let drawn: BTreeSet<&str> = out.iter().map(|d| d.id.as_str()).collect();
for mut p in leader_problems {
p.subjects
.retain(|s| s.starts_with("v:") || drawn.contains(s.as_str()));
if p.subjects.iter().any(|s| !s.starts_with("v:")) {
p.id = format!("p:{}:{}", p.code, p.subjects.join("|"));
problems.push(p);
}
}
pmi_dimensions::finish(&mut out);
problems.extend(placement::collisions_cached(
analysis, &out, sheet, views, cache,
));
Ok(((out, omitted, problems), inventory))
}
const SHARED: [(&str, bool); 6] = [
("notch_depth.length", true),
("pitch.length", false),
("slot_width.length", true),
("slot_length.length", true),
("slot_centres.length", true),
("boss_height.length", true),
];
fn share(
analysis: &Analysis,
kept: &mut Vec<(Want, bool, Option<&'static str>, Vec<&'static str>)>,
given: &mut Vec<Vec<String>>,
omitted: &mut Vec<Omitted>,
) {
share_statements(analysis, kept, given, omitted, false);
}
fn correlated_set(analysis: &Analysis, id: &str) -> Option<&'static str> {
const HEIGHTS: &str = "d:step_level/step_height.length";
if id.starts_with(HEIGHTS) {
return Some(HEIGHTS);
}
let parameter = id.strip_prefix("d:step_level/")?;
match analysis.ladder_axis(parameter)? {
0 => Some("d:step_level/step_position.length.x"),
1 => Some("d:step_level/step_position.length.y"),
2 => Some("d:step_level/step_position.length.z"),
_ => None,
}
}
fn correlated_sets(
analysis: &Analysis,
spec: &Spec,
wants: &[Want],
conveyed: &BTreeSet<&str>,
) -> (BTreeMap<String, String>, Vec<Problem>) {
let mut sets: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
let auto = spec.dimension_source == DimensionSource::Auto;
for w in wants {
if !auto && conveyed.contains(w.id.as_str()) {
continue;
}
if let Some(set) = correlated_set(analysis, &w.id) {
sets.entry(set).or_default().push(&w.id);
}
}
let mut spread = BTreeMap::new();
let mut problems = Vec::new();
for (set, members) in sets {
let decided: Vec<&str> = members
.iter()
.copied()
.filter(|id| spec.dimensions.get(*id).is_some_and(|e| !auto || e.omit))
.collect();
let rest: Vec<&str> = members
.iter()
.copied()
.filter(|id| !decided.contains(id))
.collect();
if decided.is_empty() || rest.is_empty() {
continue;
}
let what = if set.contains("step_height") {
"the height ladder".to_string()
} else {
format!("the ladder's shoulders along {}", &set[set.len() - 1..])
};
let named = decided.join(", ");
let restore: Vec<Op> = decided
.iter()
.map(|id| Op::remove(format!("dimensions/{}/omit", crate::items::key(id))))
.collect();
let own: Vec<&str> = if auto {
rest.iter()
.copied()
.filter(|id| spec.dimensions.contains_key(*id))
.collect()
} else {
Vec::new()
};
if !own.is_empty() {
let kept = own.join(", ");
problems.push(Problem {
id: format!("p:correlated_set_conflict:{set}"),
code: "correlated_set_conflict".into(),
severity: "warning".into(),
message: format!(
"The spec omits {named} of {what} but states {kept} itself, so half the set is drawn: a correlated set is approved or omitted whole. Remove the omission to draw the set, or omit every member."
),
subjects: decided.iter().chain(&own).map(|s| s.to_string()).collect(),
fix: restore,
..Default::default()
});
continue;
}
let (basis, message, fix) = if auto {
(
format!(
"authored: omitted with {what}, which the spec omits ({named}): a correlated set is omitted whole"
),
format!(
"Omitting {named} omits all of {what}: a correlated set is approved or omitted whole, never half a staircase. Remove the omission to restore the set."
),
restore,
)
} else {
(
String::new(),
format!(
"Listing {named} approves all of {what}: a correlated set is approved or omitted whole, never half a staircase."
),
Vec::new(),
)
};
for id in &rest {
spread.insert(id.to_string(), basis.clone());
}
problems.push(Problem {
id: format!("p:correlated_set_whole:{set}"),
code: "correlated_set_whole".into(),
severity: "info".into(),
message,
subjects: rest.iter().map(|s| s.to_string()).collect(),
fix,
..Default::default()
});
}
(spread, problems)
}
fn presentation_id(a: &Analysis, w: &Want) -> String {
w.measurements.first().map_or_else(
|| w.id.clone(),
|m| {
w.id.replacen(
&m.feature,
&crate::geometry_ids::presentation_key(a, &m.feature),
1,
)
},
)
}
fn share_statements(
analysis: &Analysis,
kept: &mut Vec<(Want, bool, Option<&'static str>, Vec<&'static str>)>,
given: &mut Vec<Vec<String>>,
omitted: &mut Vec<Omitted>,
cross_family: bool,
) {
const SAME_SPAN: f64 = 1e-6;
const SPAN_ENDS: f64 = crate::lint::redundancy::TOL;
type Span = (String, &'static str, usize, f64, f64, String, f64, bool);
let coordinate = |p: &str| {
p.starts_with("location")
|| p.starts_with("seat_location.")
|| p.starts_with("step_height.length.")
|| p.starts_with("step_position.length.")
};
let diameter = |p: &str| matches!(p, "od.diameter" | "boss.diameter");
let level = |p: &str| p.starts_with("step_height.length.");
let span = |(w, _, view, _): &(Want, bool, Option<&'static str>, Vec<&'static str>)| {
let parameter = &w.measurements.first()?.parameter;
if w.measurements
.iter()
.any(|m| m.parameter.starts_with("seat_location."))
{
return None;
}
if !(SHARED.iter().any(|(p, _)| p == parameter)
|| cross_family && (coordinate(parameter) || diameter(parameter)))
{
return None;
}
match w.shape {
Shape::Linear { a, b, axis, .. } => Some((
parameter.clone(),
view.unwrap_or(w.views[0]),
axis,
a[axis].min(b[axis]),
a[axis].max(b[axis]),
w.terms.join(" "),
w.value,
pmi_dimensions::basic(&w.measurements),
)),
Shape::Callout { .. } | Shape::Angular { .. } | Shape::Aligned { .. } => None,
}
};
let same = |s: &Span, t: &Span| {
(s.0 == t.0 && !diameter(&s.0)
|| cross_family
&& (coordinate(&s.0) && coordinate(&t.0)
|| s.0 == "boss_height.length" && level(&t.0)
|| t.0 == "boss_height.length" && level(&s.0)
|| (s.0 == "od.diameter" || t.0 == "od.diameter")
&& diameter(&s.0)
&& diameter(&t.0)))
&& s.1 == t.1
&& s.2 == t.2
&& (s.3 - t.3).abs() <= SPAN_ENDS
&& (s.4 - t.4).abs() <= SPAN_ENDS
&& s.5 == t.5
&& (s.6 - t.6).abs() <= SAME_SPAN
&& s.7 == t.7
};
let mut groups: Vec<(Span, Vec<usize>)> = Vec::new();
for (i, k) in kept.iter().enumerate() {
let Some(s) = span(k) else { continue };
match groups.iter_mut().find(|(t, _)| same(&s, t)) {
Some((_, members)) => members.push(i),
None => groups.push((s, vec![i])),
}
}
let mut gone = BTreeSet::new();
for (s, mut members) in groups.into_iter().filter(|(_, m)| m.len() > 1) {
members.sort_by_cached_key(|&x| (kept[x].1, presentation_id(analysis, &kept[x].0)));
let (winner, losers) = members.split_first().expect("a group of two or more");
let owner = kept[*winner].0.id.clone();
let mut claims = Vec::new();
for &i in losers {
let w = &kept[i].0;
omitted.push(Omitted {
id: w.id.clone(),
label: crate::callouts::label(&w.terms),
basis: format!(
"conveyed by {owner}: it states the same {} ({}) across the same span in {}, in the same view (one span, one dimension: ARCH0009-6)",
s.0, s.5, AXES[s.2]
),
state: State::Covered { by: owner.clone() },
});
claims.extend(w.measurements.iter().cloned());
gone.insert(i);
}
let counted_parameter = members.iter().find_map(|&i| {
let p = &kept[i].0.measurements[0].parameter;
SHARED
.iter()
.any(|(shared, count)| shared == p && *count)
.then(|| p.clone())
});
let w = &mut kept[*winner].0;
w.measurements.extend(claims);
let features: BTreeSet<&str> = w
.measurements
.iter()
.filter(|m| counted_parameter.as_ref().is_none_or(|p| &m.parameter == p))
.map(|m| m.feature.as_str())
.collect();
let n = features.len();
let counted = counted_parameter.is_some() && n > 1;
w.basis += &format!(
"; one span, one dimension: it states the same size of all {n} features it spans (ARCH0009-6){}",
if counted { ", counted" } else { "" }
);
if counted {
w.terms.insert(0, format!("{n}×"));
}
}
let mut i = 0;
kept.retain(|_| {
i += 1;
!gone.contains(&(i - 1))
});
let mut i = 0;
given.retain(|_| {
i += 1;
!gone.contains(&(i - 1))
});
}
fn share_pmi(
analysis: &Analysis,
references: bool,
kept: &mut Vec<(Want, bool, Option<&'static str>, Vec<&'static str>)>,
given: &mut Vec<Vec<String>>,
omitted: &mut Vec<Omitted>,
units: crate::units::Units,
) {
let fmt = |v| crate::dims::fmt_in(v, units);
notches::share_pmi(analysis, kept, given, omitted);
pmi_policy::cover(analysis, kept, given, omitted);
const SAME_SPAN: f64 = 1e-6;
let pmi = |w: &Want| {
w.measurements
.first()
.is_some_and(|m| m.feature.starts_with("pmi:") && m.parameter.starts_with("linear"))
};
let span =
|(w, _, view, _): &(Want, bool, Option<&'static str>, Vec<&'static str>)| match w.shape {
Shape::Linear { a, b, axis, .. } => Some((
view.unwrap_or(w.views[0]),
axis,
a[axis].min(b[axis]),
a[axis].max(b[axis]),
)),
Shape::Callout { .. } | Shape::Angular { .. } | Shape::Aligned { .. } => None,
};
let mut gone = BTreeSet::new();
let order: Vec<usize> = {
let mut o: Vec<usize> = (0..kept.len()).filter(|&i| pmi(&kept[i].0)).collect();
o.sort_by(|&x, &y| kept[x].0.id.cmp(&kept[y].0.id));
o
};
for &p in &order {
if gone.contains(&p) {
continue;
}
let Some(sp) = span(&kept[p]) else { continue };
let owner = kept[p].0.id.clone();
let mut claims = Vec::new();
for i in 0..kept.len() {
if i == p || gone.contains(&i) {
continue;
}
let Some(si) = span(&kept[i]) else { continue };
let engine_same = references
&& !pmi(&kept[i].0)
&& kept[i].1
&& given[i].is_empty()
&& pmi_policy::same_quantity(analysis, &kept[p].0, &kept[i].0);
let same = (if pmi(&kept[i].0) {
sp.0 == si.0
} else {
engine_same
}) && sp.1 == si.1
&& (sp.2 - si.2).abs() <= SAME_SPAN
&& (sp.3 - si.3).abs() <= SAME_SPAN;
if !same {
continue;
}
let (w, ..) = &kept[i];
let how = if pmi(w) {
if w.terms != kept[p].0.terms
|| pmi_dimensions::basic(&w.measurements)
!= pmi_dimensions::basic(&kept[p].0.measurements)
{
continue;
}
"the part's PMI states the same span again"
} else if engine_same {
"the part's PMI states this same physical quantity, independently of its carrying view"
} else {
continue;
};
omitted.push(Omitted {
id: w.id.clone(),
label: crate::callouts::label(&w.terms),
basis: format!(
"conveyed by {owner}: it states the same {} across the same span in {}, in the same view; {how} (one span, one dimension: ARCH0009-6)",
fmt(w.value),
AXES[sp.1]
),
state: State::Covered { by: owner.clone() },
});
claims.extend(w.measurements.iter().cloned());
gone.insert(i);
}
if !claims.is_empty() {
let w = &mut kept[p].0;
w.measurements.extend(claims);
w.basis += "; one span, one dimension: it also states what the others across the same span stated (ARCH0009-6)";
}
}
let mut i = 0;
kept.retain(|_| {
i += 1;
!gone.contains(&(i - 1))
});
let mut i = 0;
given.retain(|_| {
i += 1;
!gone.contains(&(i - 1))
});
notches::share_bevel_depths(analysis, kept, given, omitted);
}
pub fn render(
dims: &[PlannedDimension],
items: &mut Vec<Item>,
text: &mut Vec<TextRun>,
) -> Vec<Annotation> {
let font = font();
let mut annotations = Vec::new();
for d in dims {
let Some(p) = &d.placed else { continue };
let first = text.len();
for l in &p.lines {
items.push(Item::Edge(Curve::Line(l[0], l[1])));
}
for [tip, tail] in &p.arrows {
let n = [tail[1] - tip[1], tip[0] - tail[0]];
let len = n[0].hypot(n[1]).max(1e-12);
let k = ARROW_HALF / len;
let b1 = [tail[0] + k * n[0], tail[1] + k * n[1]];
let b2 = [tail[0] - k * n[0], tail[1] - k * n[1]];
items.push(Item::Face(vec![vec![
Curve::Line(*tip, b1),
Curve::Line(b1, b2),
Curve::Line(b2, *tip),
]]));
}
let ink = label_ink(&d.terms, font);
let f = to_page(p.label_at, p.rotation);
let rows = crate::callouts::lines(&d.terms).len();
let run_at = |y: f64| {
first
+ ((-y / crate::callouts::line_spacing()).round().max(0.) as usize)
.min(rows.saturating_sub(1))
};
for s in &ink.strokes {
items.push(Item::TextStroke(
Curve::Line(f(s[0]), f(s[1])),
run_at((s[0][1] + s[1][1]) / 2.),
));
}
for contours in &ink.glyphs {
let wires: Vec<Vec<Curve>> = contours
.iter()
.filter(|c| !c.is_empty())
.map(|c| {
c.iter()
.map(|s| match moved(s, &f) {
Segment::Line(a, b) => Curve::Line(a, b),
Segment::Quad(a, b, c) => Curve::Beziers(vec![vec![a, b, c]]),
Segment::Cubic(a, b, c, e) => Curve::Beziers(vec![vec![a, b, c, e]]),
})
.collect()
})
.collect();
if !wires.is_empty() {
let run = if rows <= 1 {
first
} else {
let (lo, hi) = contours
.iter()
.flatten()
.map(|s| s.at(0.)[1])
.fold((f64::INFINITY, f64::NEG_INFINITY), |(lo, hi), y| {
(lo.min(y), hi.max(y))
});
run_at((lo + hi) / 2.)
};
items.push(Item::Glyph(wires, run));
}
}
if crate::label_text::basic(d) {
let [x0, y0, x1, y1] = p.label_box;
for (a, b) in [
([x0, y0], [x1, y0]),
([x1, y0], [x1, y1]),
([x1, y1], [x0, y1]),
([x0, y1], [x0, y0]),
] {
items.push(Item::Edge(Curve::Line(a, b)));
}
}
for (i, terms) in crate::callouts::lines(&d.terms).iter().enumerate() {
let line = label_ink(terms, font);
text.push(TextRun {
text: terms.join(" "),
at: f([
0.5 * (line.min[0] + line.max[0]),
-(i as f64) * crate::callouts::line_spacing(),
]),
size: TEXT,
rotation: p.rotation,
font: FONT.into(),
h_align: HAlign::Center,
v_align: VAlign::Baseline,
});
}
annotations.push(Annotation {
approved_claims: Vec::new(),
approved_text: None,
coordinate_origin: None,
satisfies: Vec::new(),
name: d.id.clone(),
view: Some(d.view.clone()),
runs: (first..text.len()).collect(),
claims: d
.measurements
.iter()
.map(|m| Claim {
feature: m.feature.clone(),
parameter: m.parameter.clone(),
})
.collect(),
});
}
annotations
}
pub fn feature_origin(analysis: &Analysis, id: &str) -> Option<P3> {
let frame = QFrame(analysis.recognition.as_ref()?.frame);
if let Some(f) = analysis.features.iter().find(|f| f.id == id) {
let at = f
.parameters
.get("location")
.or(f.parameters.get("at"))
.or(f.parameters.get("floor_center"));
return point(at).map(|l| frame.point(l));
}
let p = analysis
.recognition
.as_ref()?
.patterns
.iter()
.find(|p| p.id == id)?;
if let Some(c) = point(p.parameters.get("center")) {
return Some(frame.point(c));
}
let members: Vec<P3> = p
.members
.iter()
.filter_map(|m| feature_origin(analysis, m))
.collect();
(!members.is_empty()).then(|| {
let n = members.len() as f64;
members
.iter()
.fold([0.0; 3], |s, m| add(s, scale3(*m, 1.0 / n)))
})
}
pub(crate) fn leader_reentry(
analysis: &Analysis,
views: &[PlannedView],
d: &PlannedDimension,
) -> f64 {
let (Some(v), Some(shaft)) = (
views.iter().find(|v| {
let name = v.id.strip_prefix("v:").unwrap_or(&v.id);
v.id == d.view
&& (NAMES.contains(&name)
|| crate::detail::NAMES.contains(&name)
|| v.cut.is_some())
}),
d.placed.as_ref().and_then(|p| p.lines.first()),
) else {
return 0.0;
};
crate::material::reentry(
footprints::Cache::default().get(analysis, v).solids(),
*shaft,
)
}
pub(crate) fn callout_members(
analysis: &Analysis,
spec: &Spec,
plan: &crate::model::Plan,
id: &str,
view: &str,
) -> Vec<crate::session::CalloutMember> {
let corrected = crate::recognition_corrections::effective(analysis, spec).ok();
let analysis = corrected.as_deref().unwrap_or(analysis);
let (mut wants, ..) = pmi_policy::wants(analysis, spec);
callouts::apply(analysis, spec, &mut wants);
if schemes::apply(analysis, spec, &mut wants).is_err()
|| authored::append(analysis, spec, &mut wants).is_err()
|| profile_angles::apply(analysis, spec, &mut wants).is_err()
{
return Vec::new();
}
detail_routes(analysis, &mut wants, &plan.views);
let Some(v) = plan.views.iter().find(|v| v.id == view) else {
return Vec::new();
};
let Some(w) = wants.iter_mut().find(|w| w.id == id) else {
return Vec::new();
};
if let Some(name) = v.id.strip_prefix("v:").and_then(|name| {
NAMES
.iter()
.chain(crate::detail::NAMES.iter())
.copied()
.find(|n| *n == name)
}) {
route_sites(analysis, w, name);
}
let Shape::Callout { sites, arcs } = &w.shape else {
return Vec::new();
};
leaders::page_sites(&Frame::new(analysis, v), sites, arcs)
.into_iter()
.map(|(centre, radius, span)| crate::session::CalloutMember {
centre,
radius,
span: span.map(|s| crate::session::CalloutSpan {
start: s.start,
sweep: s.sweep,
}),
})
.collect()
}
pub fn placement_options(analysis: &Analysis, views: &[PlannedView]) -> BTreeMap<String, Value> {
placement_options_with_spec(analysis, views, &Spec::default())
}
pub(crate) fn placement_options_with_spec(
analysis: &Analysis,
views: &[PlannedView],
spec: &Spec,
) -> BTreeMap<String, Value> {
let resolved = crate::units::effective(analysis, spec);
let spec = resolved.as_ref();
let (mut wants, ..) = pmi_policy::wants(analysis, spec);
callouts::apply(analysis, spec, &mut wants);
if schemes::apply(analysis, spec, &mut wants).is_err()
|| authored::append(analysis, spec, &mut wants).is_err()
|| profile_angles::apply(analysis, spec, &mut wants).is_err()
{
return BTreeMap::new();
}
detail_routes(analysis, &mut wants, views);
let original = wants.clone();
let frames: BTreeMap<&str, Frame> = views
.iter()
.filter_map(|v| {
let name = v.id.strip_prefix("v:")?;
(NAMES.contains(&name)
|| crate::detail::NAMES.contains(&name)
|| (!spec.views.sections.is_empty() && v.cut.is_some()))
.then(|| (name, Frame::new(analysis, v)))
})
.collect();
route(&mut wants, &frames, analysis);
wants
.iter()
.zip(original)
.map(|(w, original)| {
let options = original
.views
.iter()
.filter_map(|&name| {
let frame = frames.get(name)?;
let mut probe = original.clone();
probe.views = vec![name];
let (chosen, _) = route(std::slice::from_mut(&mut probe), &frames, analysis);
if chosen[0] != Some(name) {
return None;
}
let sides = match probe.shape {
Shape::Linear { axis, .. } => {
if across(frame, axis)? {
vec!["above", "below"]
} else {
vec!["left", "right"]
}
}
Shape::Callout { .. } | Shape::Aligned { .. } => {
vec!["above", "below", "left", "right"]
}
Shape::Angular { .. }
if probe.id.starts_with("d:authored:")
|| probe
.measurements
.iter()
.any(|m| m.parameter == "notch.angle") =>
{
Vec::new()
}
Shape::Angular { .. } => return None,
};
Some(serde_json::json!({"view": name, "sides": sides}))
})
.collect::<Vec<_>>();
(
w.id.clone(),
serde_json::json!({"label": w.terms.join(" "), "views": options}),
)
})
.collect()
}
type LocalCandidateChecks = BTreeMap<(usize, Vec<u8>), Vec<Problem>>;
#[derive(Default)]
pub(crate) struct LocalFixTrials {
cache: footprints::Cache,
before: std::cell::OnceCell<Vec<Problem>>,
candidates: std::cell::RefCell<LocalCandidateChecks>,
}
pub(crate) fn local_placement_fix(
analysis: &Analysis,
plan: &crate::model::Plan,
problem: &Problem,
options: &BTreeMap<String, Value>,
trials: &LocalFixTrials,
) -> Option<crate::fixes::Fix> {
local_placement_candidate(analysis, plan, problem, options, trials, false).map(|(fix, _)| fix)
}
fn local_placement_candidate(
analysis: &Analysis,
plan: &crate::model::Plan,
problem: &Problem,
options: &BTreeMap<String, Value>,
trials: &LocalFixTrials,
pattern_only: bool,
) -> Option<(crate::fixes::Fix, PlannedDimension)> {
use crate::placement_intent::{Along, Place, Slot};
let spec = plan.resolved_spec();
let inch = spec.units == crate::units::Units::In;
let mut raw = if inch {
pmi_policy::wants(analysis, &spec).0
} else {
wants(analysis).0
};
callouts::apply(analysis, &spec, &mut raw);
schemes::apply(analysis, &spec, &mut raw).ok()?;
authored::append(analysis, &spec, &mut raw).ok()?;
profile_angles::apply(analysis, &spec, &mut raw).ok()?;
detail_routes(analysis, &mut raw, &plan.views);
let mut all = plan.dimensions.clone();
all.extend(plan.gdt.iter().filter_map(gdt::proxy));
let cache = &trials.cache;
let same = |a: &Problem, b: &Problem| a.code == b.code && a.subjects == b.subjects;
let before = trials
.before
.get_or_init(|| recheck_cached(analysis, plan, cache));
if !before.iter().any(|p| same(p, problem)) {
return None;
}
let mut fallback = None;
let mut traded = false;
let mut remaining = 3usize;
for id in &problem.subjects {
let Some(index) = plan.dimensions.iter().position(|d| &d.id == id) else {
continue;
};
let d = &plan.dimensions[index];
if pattern_only
&& (d.kind != "linear"
|| !d
.measurements
.iter()
.any(|m| m.parameter.starts_with("grid_pitch.")))
{
continue;
}
let repeated = plan
.problems
.iter()
.filter(|p| p.subjects.contains(id))
.nth(1)
.is_some();
if d.state != crate::items::State::Shown || d.placed.is_none() {
continue;
}
if plan
.spec
.dimensions
.get(id)
.and_then(|d| d.place.as_ref())
.is_some_and(|p| p.pinned)
{
continue;
}
let Some(original) = raw.iter().find(|w| w.id == *id) else {
continue;
};
let Some(views) = options.get(id).and_then(|o| o["views"].as_array()) else {
continue;
};
let mut views: Vec<_> = views.iter().collect();
views.sort_by_key(|v| v["view"].as_str() != d.view.strip_prefix("v:"));
for option in views {
let Some(name) = option["view"].as_str() else {
continue;
};
if pattern_only && d.view.strip_prefix("v:") != Some(name) {
continue;
}
let Some(view) = plan
.views
.iter()
.find(|v| v.id.strip_prefix("v:") == Some(name))
else {
continue;
};
let frames = BTreeMap::from([(name, Frame::new(analysis, view))]);
let frame = &frames[name];
let mut want = original.clone();
want.terms = d.terms.clone();
want.measurements = d.measurements.clone();
want.views.retain(|v| *v == name);
if want.views.is_empty()
|| route(std::slice::from_mut(&mut want), &frames, analysis).0[0] != Some(name)
{
continue;
}
let sheet = std::iter::once(&plan.sheet)
.chain(&plan.sheets)
.find(|s| s.number == view.sheet)?;
let mut offer = |placed: Placed, place: &Place| {
if !inch && traded {
if remaining == 0 {
return fallback.take();
}
remaining -= 1;
}
all[index] = d.clone();
all[index].placed = Some(placed);
all[index].view = view.id.clone();
if d.kind == "callout"
&& leader_reentry(analysis, &plan.views, &all[index])
> leader_reentry(analysis, &plan.views, d) + 1e-6
{
return None;
}
let key = repeated.then(|| {
(
index,
serde_json::to_vec(place).expect("placement serializes"),
)
});
let saved = key
.as_ref()
.and_then(|key| trials.candidates.borrow().get(key).cloned());
let after = if let Some(saved) = saved {
saved
} else {
let after =
placement::collisions_for(analysis, &all, sheet, &plan.views, cache, id);
if let Some(key) = key {
let mut saved = trials.candidates.borrow_mut();
if saved.len() == 128 {
saved.clear();
}
saved.insert(key, after.clone());
}
after
};
if after.iter().any(|p| same(p, problem))
|| after
.iter()
.any(|p| p.severity == "error" && !before.iter().any(|old| same(p, old)))
{
return None;
}
let ops = vec![Op::set(
format!("dimensions/{}/place", crate::items::key(id)),
serde_json::to_value(place).unwrap(),
)];
crate::model::edit(&plan.spec, &serde_json::to_value(&ops).unwrap()).ok()?;
let mut description = crate::fixes::placement_description(
&format!(
"the {} {}",
d.label,
if d.kind == "callout" {
"callout"
} else {
"dimension"
}
),
place,
);
let added: Vec<_> = after
.iter()
.filter(|p| {
!plan
.problems
.iter()
.any(|old| same(p, old) && p.id == old.id)
})
.collect();
if added.is_empty() {
return Some((crate::fixes::Fix::new(description, ops), all[index].clone()));
}
traded = true;
let removed = before
.iter()
.filter(|p| {
p.subjects.contains(id)
&& plan.problems.iter().any(|old| same(p, old))
&& !after.iter().any(|a| a.id == p.id)
})
.count();
if crate::fixes::keeps_layout(plan, &ops) && added.len() + 2 > removed {
return None;
}
if fallback.is_none() {
description.push_str(crate::fixes::TRADE_OFF);
description.push_str(
&added
.iter()
.map(|p| format!("{} [{}]", p.message, p.code))
.collect::<Vec<_>>()
.join(" "),
);
description.push('.');
fallback = Some((crate::fixes::Fix::new(description, ops), all[index].clone()));
}
None
};
for side in option["sides"].as_array().into_iter().flatten() {
let side: crate::placement_intent::Side =
serde_json::from_value(side.clone()).ok()?;
let strip = match side {
crate::placement_intent::Side::Above => placement::Side::Above,
crate::placement_intent::Side::Below => placement::Side::Below,
crate::placement_intent::Side::Left => placement::Side::Left,
crate::placement_intent::Side::Right => placement::Side::Right,
};
for lane in 0..=1 {
for along in [Slot::Middle, Slot::Start, Slot::End, Slot::Outside] {
let place = Place {
view: Some(name.into()),
side: Some(side),
lane: Some(lane),
along: Some(Along::Slot(along)),
offset: None,
pinned: false,
};
let Some(placed) = placement::intent(
frame,
&want,
&place,
strip,
lane,
plan.canonical_callout_width(&view.id).unwrap_or(0.0),
) else {
continue;
};
if let Some(fix) = offer(placed, &place) {
return Some(fix);
}
}
}
}
if view.id == d.view
&& plan
.spec
.dimensions
.get(id)
.is_none_or(|d| d.place.is_none())
&& let Some(held) = d.placed.as_ref()
&& matches!(d.kind.as_str(), "linear" | "callout")
{
let base = if d.kind == "callout" {
let [tip, bend] = *held.lines.first()?;
[bend[0] - tip[0], bend[1] - tip[1]]
} else {
[0.0, 0.0]
};
let grid = [1., 2., 3., 4.].into_iter().flat_map(|step| {
[
[0., 1.],
[0., -1.],
[1., 0.],
[-1., 0.],
[1., 1.],
[-1., 1.],
[1., -1.],
[-1., -1.],
]
.map(|[dx, dy]| {
[
base[0] + step * lane_pitch() * dx,
base[1] + step * lane_pitch() * dy,
]
})
});
let mut witnesses = Vec::new();
if inch && d.kind == "linear" && held.lines.len() >= 3 {
let normal = usize::from(held.rotation == 0.0);
for station in &held.lines[..2] {
for step in [-4., -3., -2., -1., 1., 2., 3., 4.] {
let mut offset = [0., 0.];
offset[normal] =
station[0][normal] + step * lane_pitch() - held.lines[2][0][normal];
witnesses.push(offset);
}
}
}
for offset in grid.chain(witnesses) {
let held = Some((held, d.terms.as_slice()));
let placed = if d.kind == "callout" {
leaders::offset_intent(frame, &want, offset, held)
} else {
placement::offset_intent(frame, &want, offset, held)
};
let Some(placed) = placed else {
continue;
};
let place = Place {
view: Some(name.into()),
side: None,
lane: None,
along: None,
offset: Some(offset),
pinned: true,
};
if let Some(fix) = offer(placed, &place) {
return Some(fix);
}
}
}
}
all[index] = d.clone();
}
fallback
}
pub(crate) fn settle_source_pattern_columns(analysis: &Analysis, plan: &mut crate::model::Plan) {
if !crate::nominal::authored_pmi(analysis) {
return;
}
for _ in 0..4 {
let before = recheck(analysis, plan);
let problems: Vec<_> = before
.iter()
.filter(|p| {
matches!(
p.code.as_str(),
"annotation_leader_crossing" | "annotation_ink_overlap"
) && p.subjects.iter().any(|id| {
plan.dimensions.iter().any(|d| {
d.id == *id
&& d.kind == "linear"
&& d.measurements
.iter()
.any(|m| m.parameter.starts_with("grid_pitch."))
})
})
})
.take(8)
.cloned()
.collect();
if problems.is_empty() {
return;
}
let options = placement_options_with_spec(analysis, &plan.views, &plan.resolved_spec());
let trials = LocalFixTrials::default();
let mut improved = None;
for problem in problems {
let Some((_, mut d)) =
local_placement_candidate(analysis, plan, &problem, &options, &trials, true)
else {
continue;
};
let mut trial = plan.clone();
d.default = true;
d.basis += "; automatic source-pattern column repair against the finished callouts";
let moved_id = d.id.clone();
*trial
.dimensions
.iter_mut()
.find(|q| q.id == moved_id)
.expect("the trial retains its dimension") = d;
let after = recheck(analysis, &trial);
let codes: BTreeSet<_> = before
.iter()
.chain(&after)
.map(|p| p.code.as_str())
.collect();
trial.problems.retain(|p| !codes.contains(p.code.as_str()));
trial.problems.extend(after);
contacts::report(analysis, &mut trial);
if callout_routes::no_regression(&plan.problems, &trial.problems)
&& trial.problems.len() < plan.problems.len()
&& crate::model::finished_sheets_fit(analysis, &trial)
{
trial.record_dimension_labels();
trial.record_callout_offsets();
improved = Some(trial);
break;
}
}
let Some(trial) = improved else {
return;
};
*plan = trial;
}
}
pub(crate) fn symbolic_bore(r: &crate::pmi::Record) -> bool {
pmi_dimensions::symbolic_bore(r)
}
pub(crate) fn refine_inventory(
analysis: &Analysis,
spec: &Spec,
sheet: &PlannedSheet,
views: &[PlannedView],
inventory: &mut [PlannedDimension],
obstacles: &[PlannedDimension],
cache: &footprints::Cache,
) -> BTreeMap<String, Vec<Problem>> {
let recesses = inventory.iter().any(|d| {
d.measurements
.iter()
.any(|m| m.feature.starts_with("f:section_recess:"))
});
let overlapping_text = !recesses
&& leaders::edited_labels(spec)
&& inventory.iter().enumerate().any(|(i, d)| {
d.kind == "callout"
&& d.placed.as_ref().is_some_and(|p| {
inventory.iter().enumerate().any(|(j, q)| {
i != j
&& q.placed
.as_ref()
.is_some_and(|q| placement::boxes_overlap(p.label_box, q.label_box))
})
})
});
let thread_callouts = inventory.iter().any(|d| {
d.measurements
.iter()
.any(|m| m.feature.starts_with("f:modelled_thread:"))
});
if !recesses && !overlapping_text && !thread_callouts {
return BTreeMap::new();
}
let (raw, ..) = wants(analysis);
let frames: BTreeMap<_, _> = views
.iter()
.filter_map(|v| {
let name = v.id.strip_prefix("v:")?;
NAMES
.contains(&name)
.then(|| (name, Frame::new(analysis, v)))
})
.collect();
let mut items = Vec::new();
let mut indices = Vec::new();
let mut chosen = Vec::new();
let mut placed = Vec::new();
for (i, d) in inventory.iter().enumerate() {
let Some(w) = raw.iter().find(|w| w.id == d.id) else {
continue;
};
let mut w = w.clone();
w.terms = d.terms.clone();
w.measurements = d.measurements.clone();
if w.measurements
.first()
.is_some_and(|m| m.parameter == "seat_diameter.diameter")
&& let Shape::Callout { sites, .. } = &mut w.shape
{
for peer in raw.iter().filter(|peer| {
peer.id != w.id && peer.measurements.iter().any(|m| w.measurements.contains(m))
}) {
if let Shape::Callout { sites: more, .. } = &peer.shape {
sites.extend(more.iter().copied());
}
}
}
let name = d.view.trim_start_matches("v:");
route_sites(analysis, &mut w, name);
let view = NAMES
.iter()
.copied()
.find(|v| *v == name && frames.contains_key(v));
items.push(w);
indices.push(i);
chosen.push(view);
placed.push(d.placed.clone());
}
let page = placement::Page::new(analysis, sheet, views);
let mut sides = Vec::new();
let mut lanes = Vec::new();
for ((i, view), p) in indices.iter().zip(&chosen).zip(&placed) {
let side = view.and_then(|v| {
let p = p.as_ref()?;
if inventory[*i].region == Some(CandidateRegion::Interior)
|| p.arrows.len() != 2
|| spec
.dimensions
.get(&inventory[*i].id)
.is_some_and(|d| d.place.is_some())
{
return None;
}
let o = frames[v].outline();
Some(if p.rotation == 0.0 {
if p.arrows[0][0][1] < o[1] {
placement::Side::Below
} else {
placement::Side::Above
}
} else if p.arrows[0][0][0] < o[0] {
placement::Side::Left
} else {
placement::Side::Right
})
});
sides.push(side);
lanes.push(0);
}
if obstacles.iter().any(|d| d.kind == "callout") {
for (i, w) in items.iter().enumerate() {
if w.measurements.is_empty()
|| !w
.measurements
.iter()
.all(|m| m.feature.starts_with("f:section_recess:"))
&& !(crate::nominal::geometry_eligible(analysis)
&& !crate::nominal::comparison()
&& matches!(items[i].shape, Shape::Linear { order: 0, .. }))
{
sides[i] = None;
}
}
}
if recesses {
placement::clear_linear(
analysis,
cache,
views,
&frames,
&items,
&chosen,
&mut sides,
&mut lanes,
&mut placed,
obstacles,
&[],
&page,
true,
);
}
let proofs = interior::proofs(analysis, views);
let reports = leaders::refine(
analysis,
spec,
cache,
sheet,
views,
&frames,
&items,
&chosen,
&mut placed,
&page,
obstacles,
&proofs,
);
for (i, p) in indices.into_iter().zip(placed) {
inventory[i].placed = p;
}
if recesses {
stations_outward(&frames, &page, inventory);
}
reports
}
fn stations_outward(
frames: &BTreeMap<&str, Frame>,
page: &placement::Page,
inventory: &mut [PlannedDimension],
) {
for s in 0..inventory.len() {
if !inventory[s].id.starts_with("d:envelope/profile_location_") {
continue;
}
let view = inventory[s].view.trim_start_matches("v:");
let (Some(p), Some(frame)) = (
inventory[s].placed.clone().filter(|p| p.arrows.len() == 2),
frames.get(view),
) else {
continue;
};
let others: Vec<&Placed> = inventory
.iter()
.enumerate()
.filter(|(j, _)| *j != s)
.filter_map(|(_, d)| d.placed.as_ref())
.collect();
let shared = |c: &Placed| {
others.iter().any(|q| {
q.arrows.len() == 2 && crate::output::shared_dimension_line(&c.lines, &q.lines)
})
};
if !shared(&p) {
continue;
}
let k = usize::from(p.rotation == 0.0);
let line = p.arrows[0][0][k];
let out = if line < frame.outline()[k] { -1.0 } else { 1.0 };
let clear = |c: &Placed| {
!shared(c)
&& page.fits(c, view)
&& others.iter().all(|q| {
!placement::boxes_overlap(c.label_box, q.label_box)
&& !c.lines.iter().any(|l| placement::crosses(*l, q.label_box))
&& !q.lines.iter().any(|l| placement::crosses(*l, c.label_box))
})
};
if let Some(c) = (1..=4)
.map(|n| in_lane(&p, line + out * f64::from(n) * lane_pitch()))
.find(|c| clear(c))
{
inventory[s].placed = Some(c);
}
}
}
pub(crate) fn recheck(analysis: &Analysis, plan: &crate::model::Plan) -> Vec<Problem> {
recheck_cached(analysis, plan, &footprints::Cache::default())
}
pub(crate) fn recheck_cached(
analysis: &Analysis,
plan: &crate::model::Plan,
cache: &footprints::Cache,
) -> Vec<Problem> {
let mut all = plan.dimensions.clone();
all.extend(plan.gdt.iter().filter_map(gdt::proxy));
let mut checked: Vec<_> = std::iter::once(&plan.sheet)
.chain(&plan.sheets)
.flat_map(|sheet| placement::collisions_cached(analysis, &all, sheet, &plan.views, cache))
.collect();
for contact in contacts::scope_overlaps(plan) {
if !checked.iter().any(|p| p.id == contact.id) {
checked.push(contact);
}
}
checked
}
pub(crate) fn chamfer_stated(
analysis: &Analysis,
spec: &Spec,
size: f64,
band: &crate::pmi_lowering::Depiction,
) -> bool {
let dimensions = wants(analysis).0;
band.span.iter().all(|end| {
dimensions.iter().any(|w| {
w.measurements.iter().any(|m| {
m.parameter == "chamfer.length"
&& (m.value - size).abs() < 1e-6
&& feature_origin(analysis, &m.feature).is_some_and(|at| {
(at[band.axis] - end).abs() <= size + 0.01
&& (0..3)
.filter(|k| *k != band.axis)
.map(|k| (at[k] - band.on[k]).powi(2))
.sum::<f64>()
.sqrt()
<= band.diameter / 2.0 + size + 0.01
})
}) && !spec.dimensions.get(&w.id).is_some_and(|d| d.omit)
&& (spec.dimension_source == DimensionSource::Auto
|| spec.dimensions.contains_key(&w.id))
&& w.views
.iter()
.any(|v| !spec.views.get(*v).is_some_and(|v| v.omit))
})
})
}
pub(crate) fn measurement_ids(analysis: &Analysis) -> BTreeSet<String> {
wants(analysis)
.0
.iter()
.flat_map(|w| &w.measurements)
.map(|m| want_id(&m.feature, &m.parameter))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn feature_notes_preserve_on_origin_wants() {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/steerability/guide-bar.step.gz");
let (_, a) =
crate::model::analyse_native_bytes("guide-bar.step.gz", &std::fs::read(path).unwrap())
.unwrap();
let w = size(&a.features[0].id, "location.length.x", 0., 0., &["0"]).0;
assert!(pmi_policy::on_origin(&w));
let mut spec = Spec::default();
let mut plain = vec![w.clone()];
pmi_policy::drop_on_origin(&a, &spec, &mut plain);
spec.features
.entry(a.features[0].id.clone())
.or_default()
.note = Some("X".into());
let noted = crate::spec_gdt::effective(&a, &spec).unwrap();
assert!(pmi_policy::carries(¬ed));
let mut with_note = vec![w.clone()];
pmi_policy::drop_on_origin(¬ed, &spec, &mut with_note);
assert_eq!(plain.len(), 1);
assert_eq!(with_note.len(), 1);
assert_eq!(with_note[0].id, plain[0].id);
spec.origin.insert("x".into(), "min".into());
pmi_policy::drop_on_origin(¬ed, &spec, &mut with_note);
assert!(with_note.is_empty());
}
#[test]
fn feature_note_preserves_candidate_repair_offers() {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/steerability/guide-bar.step.gz");
let (_, a) =
crate::model::analyse_native_bytes("guide-bar.step.gz", &std::fs::read(path).unwrap())
.unwrap();
let mut spec: Spec = serde_json::from_value(serde_json::json!({"sheet":{"size":"A4","scale":"1:1"},"dimensions":{"d:envelope/width.length":{"place":{"view":"plan","side":"above","lane":40}}}})).unwrap();
let before = crate::model::plan_automatic(&a, &spec).unwrap();
spec.features
.entry(
a.features
.iter()
.find(|f| f.kind == "hole")
.unwrap()
.id
.clone(),
)
.or_default()
.note = Some("X".into());
let effective = crate::spec_gdt::effective(&a, &spec).unwrap();
let (items, _) = crate::pmi_lowering::lower(&effective, &|_| true);
let note = items
.iter()
.find(|g| g.source.starts_with("spec:features/"))
.unwrap();
spec.annotations.entry(note.id.clone()).or_default().omit = true;
let after = crate::model::plan_automatic(&a, &spec).unwrap();
let mut witnessed = 0;
for p in before
.problems
.iter()
.filter(|p| !p.offered_fixes().is_empty())
{
if let Some(q) = after
.problems
.iter()
.find(|q| q.code == p.code && q.subjects == p.subjects)
{
witnessed += 1;
assert_eq!(
q.offered_fixes(),
p.offered_fixes(),
"{} {:?}",
q.code,
q.subjects
);
}
}
assert!(
witnessed > 0,
"must exercise a surviving repairable finding"
);
}
#[test]
fn compact_lanes_clear_labels_and_overshoot() {
assert_eq!((first_line(), lane_pitch()), (FIRST_NORMAL, LANE_NORMAL));
let normal = crate::layout::lane_band(4);
let compact = with_compact(true, || {
assert_eq!((first_line(), lane_pitch()), (FIRST_COMPACT, LANE_COMPACT));
crate::layout::lane_band(4)
});
assert!((compact - normal * LANE_COMPACT / LANE_NORMAL).abs() < 1e-9);
assert_eq!(
lane_pitch(),
LANE_NORMAL,
"the scope restores normal spacing"
);
}
#[test]
fn rejected_head_routes_leave_parent_witnesses_untouched() {
let (_, mut a) = crate::model::analyse_native(
&std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/parts/turned.step"),
)
.unwrap();
let p = crate::model::plan(&a, &Spec::default()).unwrap();
let mut v = p.views.iter().find(|v| v.id == "v:front").unwrap().clone();
a.recognition = None;
let f = &mut a.features[0];
f.kind = "turned_step".into();
f.parameters = serde_json::json!({"axis":"x","lo":0.,"hi":10.,"diameter":8.,"profile":{"axis_origin":[0.,0.,0.]}}).as_object().unwrap().clone();
let feature = f.id.clone();
v.id = "v:detail_a".into();
v.detail = Some(serde_json::from_value(serde_json::json!({
"label":"A","axis":0,"source_view":"v:front","kind":"turned-head",
"bounds":[[0.,-5.,-5.],[5.,5.,5.]],"marked_bounds":[[0.,-5.,-5.],[5.,5.,5.]],
"dimensions":[want_id(&feature,"step.length")],"scale":2.,"authored":false,"partial_profile":false
})).unwrap());
let mut w = size(&feature, "step.length", 0., 10., &["10"]).0;
if let Shape::Linear { a, b, .. } = &mut w.shape {
a[1] = 0.;
b[1] = 0.;
}
let original = w.clone();
detail_routes(&a, std::slice::from_mut(&mut w), std::slice::from_ref(&v));
let witnesses = |w: &Want| match w.shape {
Shape::Linear { a, b, .. } => [a, b],
_ => unreachable!(),
};
assert_eq!(w.views, original.views);
assert_eq!(w.basis, original.basis);
assert_eq!(
witnesses(&w),
witnesses(&original),
"a rejected crop must not change the parent's shoulders"
);
v.detail.as_mut().unwrap().bounds = [[0., -5., 3.], [10., 5., 5.]];
detail_routes(&a, std::slice::from_mut(&mut w), &[v]);
assert_eq!(w.views[0], "detail_a");
assert_eq!(witnesses(&w), [[0., 0., 4.], [10., 0., 4.]]);
}
#[test]
fn side_only_callout_corridors_grow_with_authored_precision() {
let (_, analysis) = crate::model::analyse_native(
&std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/parts/plate.step"),
)
.unwrap();
let id = "d:f:hole_pattern:f6/bore.diameter";
for side in ["left", "right"] {
let mut spec: Spec = serde_json::from_value(serde_json::json!({
"dimensions": {id: {"place": {"view": "plan", "side": side}}}
}))
.unwrap();
let bare = callout_bands(&analysis, &spec).unwrap();
assert_eq!(bare.len(), 1);
spec.precision.insert(id.into(), 15);
let precise = callout_bands(&analysis, &spec).unwrap();
assert_eq!(precise.len(), 1);
assert_eq!(precise[0].side, side);
let plan = crate::model::plan(&analysis, &spec).unwrap();
let d = plan.dimensions.iter().find(|d| d.id == id).unwrap();
assert!(d.label.contains("6.000000000000000"));
assert!(d.label.lines().count() > 1);
let ink = label_ink(&d.terms, font());
let width = ink.max[0] - ink.min[0];
assert!(width <= crate::callouts::WIDTH);
assert_eq!(precise[0].depth, first_line() + (width + SHOULDER) + 2.0);
let unwrapped: Vec<_> = d
.terms
.iter()
.filter(|s| !crate::callouts::is_break(s))
.cloned()
.collect();
let ink = label_ink(&unwrapped, font());
assert!(first_line() + ink.max[0] - ink.min[0] + SHOULDER + 2.0 > bare[0].depth + 20.0);
}
}
type Kept = (Want, bool, Option<&'static str>, Vec<&'static str>);
fn size(feature: &str, parameter: &str, lo: f64, hi: f64, label: &[&str]) -> Kept {
let want = Want {
units: Default::default(),
id: want_id(feature, parameter),
views: vec!["plan"],
value: hi - lo,
terms: label.iter().map(|t| t.to_string()).collect(),
measurements: vec![Measurement {
feature: feature.into(),
parameter: parameter.into(),
value: hi - lo,
}],
basis: String::new(),
shape: Shape::Linear {
a: [lo, 5.0, 0.0],
b: [hi, 5.0, 0.0],
axis: 0,
a_edge: false,
b_edge: false,
order: 0,
},
turning: None,
};
(want, true, Some("plan"), vec![])
}
type Shared = (Vec<(String, String, usize)>, Vec<(String, String)>);
fn shared(mut kept: Vec<Kept>) -> Shared {
let mut given = vec![Vec::new(); kept.len()];
let mut omitted = Vec::new();
share(&block(&[]), &mut kept, &mut given, &mut omitted);
assert_eq!(given.len(), kept.len());
(
kept.iter()
.map(|(w, ..)| (w.id.clone(), w.terms.join(" "), w.measurements.len()))
.collect(),
omitted
.iter()
.map(|o| {
let owner = o.basis["conveyed by ".len()..o.basis.find(": ").unwrap()].into();
(o.id.clone(), owner)
})
.collect(),
)
}
#[test]
fn one_span_one_size_counted_and_claiming_every_feature() {
let (kept, omitted) = shared(vec![
size("f:slot:2", "slot_width.length", 10.0, 16.0, &["6"]),
size("f:slot:1", "slot_width.length", 10.0, 16.0, &["6"]),
size("f:slot:3", "slot_width.length", 20.0, 26.0, &["6"]),
]);
assert_eq!(
kept,
[
("d:f:slot:1/slot_width.length".into(), "2× 6".into(), 2),
("d:f:slot:3/slot_width.length".into(), "6".into(), 1),
]
);
assert_eq!(
omitted,
[(
"d:f:slot:2/slot_width.length".into(),
"d:f:slot:1/slot_width.length".into()
)]
);
}
#[test]
fn a_pitch_keeps_its_label_and_other_bands_or_parameters_never_merge() {
let (kept, _) = shared(vec![
size("f:hole_pattern:1", "pitch.length", 0.0, 75.0, &["2× 75"]),
size("f:hole_pattern:2", "pitch.length", 0.0, 75.0, &["2× 75"]),
]);
assert_eq!(
kept,
[("d:f:hole_pattern:1/pitch.length".into(), "2× 75".into(), 2)]
);
let (kept, omitted) = shared(vec![
size("f:boss:1", "boss_height.length", 0.0, 5.0, &["5"]),
size("f:boss:2", "boss_height.length", 0.0, 5.0, &["5", "±0.1"]),
size("f:slot:1", "slot_length.length", 0.0, 5.0, &["5"]),
size("f:step:1", "step.length", 0.0, 5.0, &["5"]),
size("f:step:2", "step.length", 0.0, 5.0, &["5"]),
]);
assert_eq!(kept.len(), 5, "{kept:?}");
assert!(omitted.is_empty());
}
#[test]
fn the_spec_listed_one_survives() {
let mut listed = size("f:boss:2", "boss_height.length", 0.0, 5.0, &["5"]);
listed.1 = false;
let (kept, omitted) = shared(vec![
size("f:boss:1", "boss_height.length", 0.0, 5.0, &["5"]),
listed,
]);
assert_eq!(
kept,
[("d:f:boss:2/boss_height.length".into(), "2× 5".into(), 2)]
);
assert_eq!(omitted[0].0, "d:f:boss:1/boss_height.length");
}
fn block(features: &[(&str, &[usize], serde_json::Value)]) -> Analysis {
let mut n = BTreeMap::<&str, usize>::new();
let features: Vec<_> = features
.iter()
.map(|(kind, faces, record)| {
let i = n.entry(kind).or_default();
*i += 1;
serde_json::json!({
"id": format!("f:{kind}:{i}"), "kind": kind, "faces": faces,
"parameters": record,
"fingerprint": { "version": "test", "family": "test", "subtype": "test",
"sizes": {}, "position": [0.0, 0.0, 0.0], "neighbourhood": [] },
})
})
.collect();
serde_json::from_value(serde_json::json!({
"schema": "draftwright.analysis/2",
"binding": { "source_sha256": "", "part": "test", "face_count": 20, "loader": "test" },
"envelope": [[0.0, 0.0, 0.0], [100.0, 60.0, 20.0]],
"features": features,
"iso_bounds": [0.0, 0.0, 0.0, 0.0],
"recognition": { "recogniser": "test",
"frame": [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]] },
}))
.unwrap()
}
fn own(wants: &[Want], id: &str) -> Vec<(String, Vec<&'static str>, String, Vec<String>)> {
wants
.iter()
.filter(|w| w.measurements.iter().any(|m| m.feature == id))
.map(|w| {
(
w.id.rsplit_once('/').unwrap().1.to_string(),
w.views.clone(),
w.terms.join(" "),
w.measurements.iter().map(|m| m.parameter.clone()).collect(),
)
})
.collect()
}
#[test]
fn cylindrical_profiles_and_concave_fillets_keep_their_radii() {
let a = block(&[
(
"cylindrical_profile",
&[1],
serde_json::json!({"axis":"z", "axis_point":[20.0,20.0,0.0], "radius":203.2}),
),
(
"cylindrical_profile",
&[2],
serde_json::json!({"axis":"z", "axis_point":[40.0,20.0,0.0], "radius":203.2}),
),
(
"fillet",
&[3],
serde_json::json!({"axis":"z", "at":[10.0,10.0,0.0], "radius":3.0, "turned":false, "side":"concave"}),
),
(
"fillet",
&[4],
serde_json::json!({"axis":"z", "at":[90.0,10.0,0.0], "radius":3.0, "turned":false}),
),
]);
let (ws, ps, _) = wants_uncached(&a, &Spec::default());
assert!(ps.is_empty(), "{ps:?}");
let profiles = own(&ws, "f:cylindrical_profile:1");
assert_eq!(profiles.len(), 1);
assert_eq!(profiles[0].2, "2× R203.2");
assert_eq!(profiles[0].3, ["cylindrical_profile.radius"; 2]);
let fillets = own(&ws, "f:fillet:1");
assert_eq!(fillets.len(), 1);
assert_eq!(fillets[0].2, "2× R3");
}
#[test]
fn a_turned_body_states_its_od_and_face_recess_without_a_boss() {
let mut a = block(&[
(
"turned_body",
&[0, 1, 2, 3],
serde_json::json!({"axis":"z", "axis_point":[0.0,0.0,3.5], "od":22.0, "length":7.0}),
),
(
"face_groove",
&[1, 2, 3],
serde_json::json!({"axis":[0.0,0.0,1.0], "axis_point":[0.0,0.0,7.0], "outer_diameter":18.5, "inner_diameter":13.0, "depth":0.25, "outer_wall_angle":45.0, "inner_wall_angle":45.0}),
),
]);
a.envelope = [[-11.0, -11.0, 0.0], [11.0, 11.0, 7.0]];
let (ws, ps, _) = wants_uncached(&a, &Spec::default());
assert!(ps.is_empty(), "{ps:?}");
assert_eq!(ws.iter().filter(|w| w.terms == ["ø22"]).count(), 1);
let recess = own(&ws, "f:face_groove:1");
assert_eq!(recess.len(), 1, "concentric recess shares the turning axis");
assert_eq!(recess[0].2, "⌀18.5 × ⌀13 ↧ 0.25 OUTER 45° INNER 45°");
assert_eq!(recess[0].3.len(), 5);
a.features[1]
.parameters
.insert("inner_wall_angle".into(), serde_json::json!(90.0));
let (ws, _, _) = wants_uncached(&a, &Spec::default());
assert_eq!(
own(&ws, "f:face_groove:1")[0].2,
"⌀18.5 × ⌀13 ↧ 0.25 OUTER 45°"
);
}
#[test]
fn an_annular_groove_is_called_out_unless_its_walls_state_it() {
let groove = serde_json::json!({
"axis": [0.0, 0.0, 1.0], "floor_center": [30.0, 20.0, 18.0],
"inner_radius": 1.0, "outer_radius": 1.5, "depth": 2.0,
});
let a = block(&[("annular_groove", &[3, 4, 5], groove.clone())]);
let (wants, problems, _) = wants_uncached(&a, &Spec::default());
let mut got = own(&wants, "f:annular_groove:1");
got.sort();
assert_eq!(
got,
[
(
"annular_groove_outer.diameter".to_string(),
vec!["plan"],
"⌀3 × ⌀2 ↧ 2".to_string(),
vec![
"annular_groove_outer.diameter".to_string(),
"annular_groove_inner.diameter".into(),
"annular_groove_depth.length".into()
]
),
(
"location.length.x".into(),
vec!["plan"],
"30".into(),
vec!["location.length.x".into()]
),
(
"location.length.y".into(),
vec!["plan"],
"20".into(),
vec!["location.length.y".into()]
),
]
);
assert!(
problems
.iter()
.all(|p| !p.subjects.contains(&"f:annular_groove:1".to_string()))
);
assert!(stated_by(&a, &a.features[0]).is_empty());
let walled = block(&[
("annular_groove", &[3, 4, 5], groove),
(
"boss",
&[5],
serde_json::json!({"axis": [0.0, 0.0, 1.0], "diameter": 2.0, "height": 2.0, "location": [30.0, 20.0, 18.0]}),
),
(
"hole",
&[3],
serde_json::json!({"axis": [0.0, 0.0, 1.0], "diameter": 3.0, "depth": 2.0, "location": [30.0, 20.0, 20.0]}),
),
]);
let (wants, ..) = wants_uncached(&walled, &Spec::default());
assert!(own(&wants, "f:annular_groove:1").is_empty());
let by = stated_by(&walled, &walled.features[0]);
assert_eq!(
by,
[
("f:hole:1".to_string(), "bore.diameter"),
("f:hole:1".into(), "bore.depth"),
("f:boss:1".into(), "boss.diameter")
]
);
let id = wants
.iter()
.find(|w| {
w.measurements
.iter()
.any(|m| m.feature == "f:boss:1" && m.parameter == "boss.diameter")
})
.unwrap()
.id
.clone();
let spec: Spec =
serde_json::from_value(serde_json::json!({"dimensions": {id: {"omit": true}}}))
.unwrap();
let (steered, ..) = pmi_policy::wants(&walled, &spec);
assert_eq!(own(&steered, "f:annular_groove:1").len(), 3);
let (default, ..) = pmi_policy::wants(&walled, &Spec::default());
assert!(own(&default, "f:annular_groove:1").is_empty());
}
#[test]
fn a_v_channel_states_its_angle_depth_and_trough() {
let channel = |axis: [f64; 3]| {
serde_json::json!({
"axis": axis, "opening": [0.0, 0.0, 1.0], "at": [40.0, 30.0, 15.0],
"length": 60.0, "width": 5.773503, "depth": 5.0, "included_angle_degrees": 60.0,
})
};
let a = block(&[("v_channel", &[7, 8], channel([0.0, 1.0, 0.0]))]);
let (wants, ..) = wants_uncached(&a, &Spec::default());
assert_eq!(
own(&wants, "f:v_channel:1"),
[
(
"v_channel.angle".to_string(),
vec!["front"],
"60° V × 5 DEEP".to_string(),
vec![
"v_channel.angle".to_string(),
"v_channel_depth.length".into()
]
),
(
"location.length.x".into(),
vec!["front"],
"40".into(),
vec!["location.length.x".into()]
),
]
);
let oblique = block(&[("v_channel", &[7, 8], channel([0.6, 0.8, 0.0]))]);
let (wants, problems, _) = wants_uncached(&oblique, &Spec::default());
assert!(own(&wants, "f:v_channel:1").is_empty());
assert!(
problems
.iter()
.any(|p| p.code == "feature_not_dimensioned" && p.subjects == ["f:v_channel:1"])
);
}
#[test]
fn a_semicircular_blind_slot_is_a_round_bottom_open_slot() {
let slot = serde_json::json!({
"axis": [-1.0, 0.0, 0.0], "opening": [0.0, 0.0, 1.0], "at": [95.0, 25.0, 17.0],
"length": 10.0, "radius": 2.0, "straight_height": 4.0,
});
let a = block(&[("semicircular_blind_slot", &[9, 10, 11, 12], slot)]);
let (wants, ..) = wants_uncached(&a, &Spec::default());
assert_eq!(
own(&wants, "f:semicircular_blind_slot:1"),
[
(
"semicircular_blind_slot_width.length".to_string(),
vec!["plan"],
"ROUND-BOTTOM OPEN SLOT 4 × 10 × 6 DEEP".to_string(),
vec![
"semicircular_blind_slot_width.length".to_string(),
"semicircular_blind_slot_length.length".into(),
"semicircular_blind_slot_depth.length".into()
]
),
(
"location_slot_across.length".into(),
vec!["plan"],
"25".into(),
vec!["location_slot_across.length".into()]
),
]
);
}
}