use std::collections::BTreeSet;
use cadkit_core::{
Block, Document, Format, Layer, LengthUnit, Model, ModelKind, Props, ReadOptions, Result,
SourceInfo, Units, Value,
};
use crate::budget::Budget;
use crate::map::{Item, Mapper, Xf, tree_v7, unit_from_label};
use crate::native::element::ElementData;
use crate::native::v7::{self, V7View, description_end_bytes};
use crate::text::{self, TextDecoder};
pub(crate) fn read(bytes: &[u8], options: &ReadOptions) -> Result<Document> {
let mut budget = Budget::new(options.limits);
let enc = text::encoding_for(options.fallback_codepage.as_deref());
let file = v7::scan(bytes, &mut budget, enc)?;
Ok(map_file(&file, options, enc))
}
fn is_control(item: &Item<'_>) -> bool {
let h = &item.el.header;
!h.has_display_header
|| matches!(h.type_code, 5 | 8 | 66)
|| matches!(
item.el.data,
ElementData::Tcb(_) | ElementData::ColorTable { .. } | ElementData::TagSet(_)
)
}
fn map_file(file: &V7View<'_>, options: &ReadOptions, enc: TextDecoder) -> Document {
let mut m = Mapper::new(options.keep_raw, options.limits.max_depth);
let mut warnings = file.warnings.clone();
let all: Vec<(Item<'_>, Option<u64>)> = file
.records
.iter()
.map(|r| {
let item = Item {
el: v7::decode_bytes(r.bytes, file.is_3d, enc, &options.limits),
raw: r.bytes,
offset: r.offset as u64,
};
(
item,
description_end_bytes(r.offset, r.bytes).map(|e| e as u64),
)
})
.collect();
let deleted = all.iter().filter(|(i, _)| i.el.header.deleted).count();
for (it, _) in &all {
match &it.el.data {
ElementData::ColorTable { colors } if !it.el.header.deleted => {
for (dst, src) in m.palette.iter_mut().zip(colors.iter()) {
*dst = *src;
}
}
ElementData::TagSet(ts) => {
if let Some(n) = ts.number {
m.tag_sets.insert(u64::from(n), ts.clone());
}
}
_ => {}
}
}
let tcb = file.tcb.as_ref();
let upm = tcb
.map(|t| f64::from(t.subunits_per_master) * f64::from(t.uor_per_subunit))
.filter(|v| v.is_finite() && *v > 0.0);
if upm.is_none() {
warnings.push(cadkit_core::Warning::new(
"dgn.v7_units",
"design header has no usable units; coordinates are in UOR",
));
}
let xf = Xf {
origin: tcb.map_or([0.0; 3], |t| t.origin),
scale: upm.map_or(1.0, |u| 1.0 / u),
};
let units = match tcb.and_then(|t| unit_from_label(&t.master_label)) {
Some((unit, meters)) => Units::new(unit).with_meters_per_unit(meters),
None => Units::new(LengthUnit::Unitless),
};
let (items, ends): (Vec<Item<'_>>, Vec<Option<u64>>) = all
.into_iter()
.filter(|(i, _)| !i.el.header.deleted && !is_control(i))
.unzip();
let tree = tree_v7(&items, &ends, options.limits.max_depth);
let mut blocks = Vec::new();
let mut entities = Vec::new();
for node in &tree {
let Some(it) = items.get(node.idx) else {
continue;
};
if let ElementData::SharedCellDefinition(c) = &it.el.data {
let rel = xf.relative();
let name = c
.name
.clone()
.unwrap_or_else(|| format!("DGN_SHARED_CELL_{}", it.offset));
let entities = node
.children
.iter()
.filter_map(|ch| m.entity(&items, ch, &rel, 1))
.collect();
let mut props = Props::new();
props.insert("dgn.type".into(), Value::Int(34));
blocks.push(
Block::new(name)
.with_base_point(rel.pt(c.origin))
.with_entities(entities)
.with_props(props),
);
continue;
}
if let Some(e) = m.entity(&items, node, &xf, 0) {
entities.push(e);
}
}
let levels: BTreeSet<u32> = m.used_levels.clone();
let layers = levels
.into_iter()
.map(|l| Layer::new(m.level_name(l)).with_id(u64::from(l)))
.collect();
let mut model_props = Props::new();
if let Some(u) = upm {
model_props.insert("dgn.uor_per_master".into(), Value::Float(u));
}
if let Some(t) = tcb {
model_props.insert(
"dgn.master_unit".into(),
Value::Text(t.master_label.clone()),
);
model_props.insert("dgn.sub_unit".into(), Value::Text(t.sub_label.clone()));
model_props.insert(
"dgn.sub_units_per_master".into(),
Value::Int(i64::from(t.subunits_per_master)),
);
model_props.insert(
"dgn.global_origin".into(),
Value::List(t.origin.iter().map(|v| Value::Float(*v)).collect()),
);
}
model_props.insert("dgn.element_count".into(), Value::Int(items.len() as i64));
let mut props = Props::new();
props.insert(
"dgn.record_count".into(),
Value::Int(file.records.len() as i64),
);
props.insert("dgn.deleted_count".into(), Value::Int(deleted as i64));
props.insert("dgn.end_offset".into(), Value::Int(file.end_offset as i64));
if let Some(sets) = m.tag_sets_value() {
props.insert("dgn.tag_sets".into(), sets);
}
let text_styles = m.text_styles();
let linetypes = m.linetypes();
warnings.extend(m.finish_warnings());
{
let mut doc = Document::default();
doc.source =
SourceInfo::new(Format::DgnV7, "V7").with_codepage(enc.name().to_ascii_lowercase());
doc.units = units;
doc.layers = layers;
doc.linetypes = linetypes;
doc.text_styles = text_styles;
doc.blocks = blocks;
doc.models = vec![
Model::new("Default", ModelKind::Model)
.with_id(0)
.with_is_3d(file.is_3d)
.with_entities(entities)
.with_props(model_props),
];
doc.props = props;
doc.warnings = warnings;
doc
}
}
#[cfg(test)]
mod tests {
use cadkit_core::{EntityKind, GroupKind, ReadOptions};
use crate::native::v7::tests::{disp, mid, record};
fn tcb() -> Vec<u8> {
let mut t = record(9, 8, &[0u8; 1532]);
t[1112..1116].copy_from_slice(&mid(10));
t[1116..1120].copy_from_slice(&mid(100));
t[1120..1124].copy_from_slice(b"m mm");
t
}
fn line(x0: i32, y0: i32, x1: i32, y1: i32, complex: bool) -> Vec<u8> {
let mut body = disp(2, 1, 0);
for v in [x0, y0, x1, y1] {
body.extend(mid(v));
}
let mut r = record(3, 7, &body);
if complex {
r[0] |= 0x80;
}
r
}
#[test]
fn complex_chain_groups_its_components() {
let a = line(0, 0, 1000, 0, true);
let b = line(1000, 0, 1000, 1000, true);
let mut head = disp(0, 0, 0);
head.extend(&0u16.to_le_bytes());
head.extend(&2u16.to_le_bytes());
head.extend([0u8; 8]);
let mut chain = record(12, 7, &head);
let words = ((chain.len() - 38 + a.len() + b.len()) / 2) as u16;
chain[36..38].copy_from_slice(&words.to_le_bytes());
let mut file = tcb();
file.extend(chain);
file.extend(a);
file.extend(b);
file.extend(line(0, 0, 0, 500, false));
file.extend([0xff, 0xff]);
let doc = crate::read(&file, &ReadOptions::default()).unwrap();
assert_eq!(doc.units.unit, cadkit_core::LengthUnit::Meter);
let m = &doc.models[0];
assert_eq!(m.entities.len(), 2, "{:?}", m.entities);
let EntityKind::Group {
group_kind,
children,
..
} = &m.entities[0].kind
else {
panic!()
};
assert_eq!(*group_kind, GroupKind::ComplexChain);
assert_eq!(children.len(), 2);
let EntityKind::Line { end, .. } = children[1].kind else {
panic!()
};
assert_eq!((end.x, end.y), (1.0, 1.0));
assert_eq!(m.entities[0].layer.as_deref(), Some("Level 7"));
}
}