Repository navigation
DXF: write the DIMSTYLE jog angle (DIMJOGANG) in degrees, as AutoCAD reads it #1038
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 8 commits
debe636
e113a45
ef5a34d
6232c67
5618d60
0472429
e238f9b
8a4f112
303a6ec
477dd70
e1bcc5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,11 +1,12 @@ | ||||||||||||||||||||||||||||||||
| //! The dimension variables added for fit, suppression, fixed extension lines, tolerance | ||||||||||||||||||||||||||||||||
| //! justification, angular zeros, arc symbols, leader arrows, linetypes, lineweights and the jog | ||||||||||||||||||||||||||||||||
| //! angle survive DXF: in DIMSTYLE records (group codes of the DXF Reference) and as per-dimension | ||||||||||||||||||||||||||||||||
| //! `ACAD` DSTYLE overrides. Linetypes are LTYPE handles; DIMJOGANG is in radians in the file. | ||||||||||||||||||||||||||||||||
| //! `ACAD` DSTYLE overrides. Linetypes are LTYPE handles. DIMJOGANG is in degrees in DIMSTYLE | ||||||||||||||||||||||||||||||||
| //! group 50, as AutoCAD reads it, and in radians in xdata and `$DIMJOGANG`; either unit reads. | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| use cadcraft_doc::{Common, DimKind, DimStyle, Dimension, Drawing, EntityKind, Linetype, Space}; | ||||||||||||||||||||||||||||||||
| use cadcraft_doc::{Common, DimKind, DimStyle, Dimension, Drawing, EntityKind, HVal, Linetype, Space}; | ||||||||||||||||||||||||||||||||
| use cadcraft_geom::Vec3; | ||||||||||||||||||||||||||||||||
| use cadcraft_io::{read_dxf, write_dxf}; | ||||||||||||||||||||||||||||||||
| use cadcraft_io::{SaveOptions, Version, read_dxf, write_dxf}; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| fn styled() -> DimStyle { | ||||||||||||||||||||||||||||||||
| DimStyle { | ||||||||||||||||||||||||||||||||
|
|
@@ -131,3 +132,128 @@ fn header_jog_angle_is_read_in_degrees_and_hostile_values_are_dropped() { | |||||||||||||||||||||||||||||||
| assert_eq!((st.dim_linetype.as_str(), st.jog_angle, st.fit), ("", 45.0, 3), "{st:?}"); | ||||||||||||||||||||||||||||||||
| assert_eq!(st.ext_linetype1, "CENTER"); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| fn dxf(d: &Drawing, v: Version) -> String { | ||||||||||||||||||||||||||||||||
| String::from_utf8(cadcraft_io::write_with(d, "x.dxf", &SaveOptions { version: Some(v), binary: false }).unwrap()).unwrap() | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /// `d` saved (2018) and opened again. | ||||||||||||||||||||||||||||||||
| fn reopened(d: &Drawing) -> Drawing { | ||||||||||||||||||||||||||||||||
| read_dxf(dxf(d, Version::R2018).as_bytes()).unwrap() | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /// The line holding the value of group `code` in the DIMSTYLE record `name`. | ||||||||||||||||||||||||||||||||
| fn style_group(lines: &[&str], name: &str, code: &str) -> Option<usize> { | ||||||||||||||||||||||||||||||||
| let pair = |i: usize, c: &str, v: &str| lines.get(i).is_some_and(|l| l.trim() == c) && lines.get(i + 1).is_some_and(|l| l.trim() == v); | ||||||||||||||||||||||||||||||||
| let table = (0..lines.len()).step_by(2).find(|&i| pair(i, "2", "DIMSTYLE"))?; | ||||||||||||||||||||||||||||||||
| let rec = (table..lines.len()).step_by(2).find(|&i| pair(i, "2", name))?; | ||||||||||||||||||||||||||||||||
| (rec..lines.len()).step_by(2).take_while(|&i| lines[i].trim() != "0").find(|&i| lines[i].trim() == code).map(|i| i + 1) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /// `text` with the value of group `code` in the DIMSTYLE record `name` replaced by `value`. | ||||||||||||||||||||||||||||||||
| fn with_style_group(text: &str, name: &str, code: &str, value: &str) -> String { | ||||||||||||||||||||||||||||||||
| let mut lines: Vec<&str> = text.lines().collect(); | ||||||||||||||||||||||||||||||||
| let at = style_group(&lines, name, code).unwrap(); | ||||||||||||||||||||||||||||||||
| lines[at] = value; | ||||||||||||||||||||||||||||||||
| lines.join("\r\n") | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||
| fn jog_angle_is_degrees_in_dimstyle_groups() { | ||||||||||||||||||||||||||||||||
| // AutoCAD reads group 50 as degrees: given 0.7853981633974483 (45° in radians), it took the | ||||||||||||||||||||||||||||||||
| // jog angle as 0.785°. | ||||||||||||||||||||||||||||||||
| let d = drawing(); | ||||||||||||||||||||||||||||||||
| for v in [Version::R2004, Version::R2018] { | ||||||||||||||||||||||||||||||||
| let text = dxf(&d, v); | ||||||||||||||||||||||||||||||||
| let lines: Vec<&str> = text.lines().collect(); | ||||||||||||||||||||||||||||||||
| for (name, want) in [("Standard", "45.0"), ("Detail", "30.0")] { | ||||||||||||||||||||||||||||||||
| let at = style_group(&lines, name, "50").unwrap(); | ||||||||||||||||||||||||||||||||
| assert_eq!(lines[at].trim(), want, "{v:?} {name}"); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| let back = read_dxf(text.as_bytes()).unwrap(); | ||||||||||||||||||||||||||||||||
| assert_eq!(back.dim_style("Standard").unwrap(), &DimStyle::default(), "{v:?}"); | ||||||||||||||||||||||||||||||||
| assert_eq!(back.dim_style("Detail").unwrap(), &styled(), "{v:?}"); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||
| fn jog_angle_in_radians_in_a_group_still_reads() { | ||||||||||||||||||||||||||||||||
| // CADCraft 0.7.0 and earlier wrote group 50 in radians. 5° to 90° in radians are below the 5° | ||||||||||||||||||||||||||||||||
| // a style takes, so they can't be degrees and read as radians. | ||||||||||||||||||||||||||||||||
| let text = dxf(&drawing(), Version::R2018); | ||||||||||||||||||||||||||||||||
| for (file, want) in [("0.7853981633974483", 45.0), ("1.0471975511965976", 60.0), ("0.08726646259971647", 5.0), ("1.5707963267948966", 90.0)] { | ||||||||||||||||||||||||||||||||
| let back = read_dxf(with_style_group(&text, "Detail", "50", file).as_bytes()).unwrap(); | ||||||||||||||||||||||||||||||||
| assert_eq!(back.dim_style("Detail").unwrap().jog_angle, want, "{file}"); | ||||||||||||||||||||||||||||||||
| // Saved again, it's degrees: the same style comes back, its linetypes too. | ||||||||||||||||||||||||||||||||
| assert_eq!(reopened(&back).dim_style("Detail").unwrap(), &DimStyle { jog_angle: want, ..styled() }, "saved again: {file}"); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||
| fn jog_angle_no_style_takes_keeps_the_default() { | ||||||||||||||||||||||||||||||||
| let text = dxf(&drawing(), Version::R2018); | ||||||||||||||||||||||||||||||||
| for file in ["0.0", "0.05", "2.0", "4.99", "90.5", "-30.0", "1e300", "-1e300", "NaN", "inf"] { | ||||||||||||||||||||||||||||||||
| let back = read_dxf(with_style_group(&text, "Detail", "50", file).as_bytes()).unwrap(); | ||||||||||||||||||||||||||||||||
| let st = back.dim_style("Detail").unwrap(); | ||||||||||||||||||||||||||||||||
| assert_eq!(st.jog_angle, 45.0, "{file}"); | ||||||||||||||||||||||||||||||||
| assert_eq!(DimStyle { jog_angle: 30.0, ..st.clone() }, styled(), "the rest of the style reads: {file}"); | ||||||||||||||||||||||||||||||||
|
Comment on lines
+196
to
+199
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: sed -n '1,255p' crates/io/tests/dimstyle_vars_roundtrip.rs
rg -n 'when a variant reads successfully|hostile raw-DXF|safe write/reopen|reference integrity|with_style_group|linetype' crates/io/tests README.md AGENTS.mdRepository: storytold/cadcraft Length of output: 17873 🏁 Script executed: set +e
printf '%s\n' '--- relevant test references ---'
rg -n -F --glob '*.rs' \
-e 'with_style_group' \
-e 'jog_angle_no_style_takes_keeps_the_default' \
-e 'write_dxf(&back)' \
-e 'read_dxf(write_dxf' \
-e 'jog_angle' \
crates/io/tests
status=$?
if [ "$status" -gt 1 ]; then exit "$status"; fi
printf '%s\n' '--- target block ---'
nl -ba crates/io/tests/dimstyle_vars_roundtrip.rs | sed -n '168,199p'Repository: storytold/cadcraft Length of output: 7707 Write and reopen accepted hostile DXF variants. For each accepted group-50 variant, write Suggested test extension let st = back.dim_style("Detail").unwrap();
assert_eq!(st.jog_angle, 45.0, "{file}");
assert_eq!(DimStyle { jog_angle: 30.0, ..st.clone() }, styled(), "the rest of the style reads: {file}");
+ let reopened = read_dxf(write_dxf(&back).as_bytes()).unwrap();
+ let st = reopened.dim_style("Detail").unwrap();
+ assert_eq!(st.jog_angle, 45.0, "after reopen: {file}");
+ assert_eq!(DimStyle { jog_angle: 30.0, ..st.clone() }, styled(), "the rest of the style reads after reopen: {file}");
+ for name in [st.dim_linetype.as_str(), st.ext_linetype1.as_str(), st.ext_linetype2.as_str()] {
+ assert!(reopened.linetype(name).is_some(), "missing linetype {name}: {file}");
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
| assert_eq!(reopened(&back).dim_style("Detail").unwrap(), &DimStyle { jog_angle: 45.0, ..styled() }, "saved again: {file}"); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||
| fn jog_angle_overrides_stay_radians_in_xdata() { | ||||||||||||||||||||||||||||||||
| // Xdata keeps radians, as CADCraft's DSTYLE overrides and R2000 DIMVARS always have (AutoCAD's | ||||||||||||||||||||||||||||||||
| // own R2000 save keeps the style's jog angle in xdata in radians too). Degrees read as well. | ||||||||||||||||||||||||||||||||
| let mut d = drawing(); | ||||||||||||||||||||||||||||||||
| d.add(&Space::Model, Common::default(), dimension(serde_json::json!({ "DIMJOGANG": 60 }))).unwrap(); | ||||||||||||||||||||||||||||||||
| let text = dxf(&d, Version::R2018); | ||||||||||||||||||||||||||||||||
| let rad = 60f64.to_radians().to_string(); | ||||||||||||||||||||||||||||||||
| let lines: Vec<&str> = text.lines().map(str::trim).collect(); | ||||||||||||||||||||||||||||||||
| assert!(lines.windows(4).any(|w| w == ["1070", "50", "1040", rad.as_str()]), "DSTYLE: 1070 50, 1040 {rad}"); | ||||||||||||||||||||||||||||||||
| let jog = |text: &str| { | ||||||||||||||||||||||||||||||||
| read_dxf(text.as_bytes()).unwrap().model.iter().find_map(|e| match &e.kind { | ||||||||||||||||||||||||||||||||
| EntityKind::Dimension(dm) => Some(DimStyle::default().with_overrides(&dm.overrides).jog_angle), | ||||||||||||||||||||||||||||||||
| _ => None, | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
| assert_eq!(jog(&text), Some(60.0)); | ||||||||||||||||||||||||||||||||
| let degrees = text.replacen(&format!("1040\r\n{rad}\r\n"), "1040\r\n60.0\r\n", 1); | ||||||||||||||||||||||||||||||||
| assert_ne!(degrees, text); | ||||||||||||||||||||||||||||||||
| assert_eq!(jog(°rees), Some(60.0)); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||
| fn header_jog_angle_takes_degrees_too_and_drops_unusable_values() { | ||||||||||||||||||||||||||||||||
| let d = drawing(); | ||||||||||||||||||||||||||||||||
| // The header carries the current style's jog angle, in radians. | ||||||||||||||||||||||||||||||||
| let text = write_dxf(&d); | ||||||||||||||||||||||||||||||||
| let lines: Vec<&str> = text.lines().collect(); | ||||||||||||||||||||||||||||||||
| let at = lines.iter().position(|l| l.trim() == "$DIMJOGANG").expect("$DIMJOGANG written") + 2; | ||||||||||||||||||||||||||||||||
| assert!((lines[at].trim().parse::<f64>().unwrap() - 45f64.to_radians()).abs() < 1e-9, "{}", lines[at]); | ||||||||||||||||||||||||||||||||
| let jog = |v: &str| { | ||||||||||||||||||||||||||||||||
| let mut lines = lines.clone(); | ||||||||||||||||||||||||||||||||
| lines[at] = v; | ||||||||||||||||||||||||||||||||
| read_dxf(lines.join("\r\n").as_bytes()).unwrap().header.get("DIMJOGANG").and_then(HVal::as_f64) | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
| assert_eq!(jog("0.5235987755982988"), Some(30.0)); | ||||||||||||||||||||||||||||||||
| assert_eq!(jog("30.0"), Some(30.0)); | ||||||||||||||||||||||||||||||||
| for v in ["0.0", "120.0", "1e300", "NaN"] { | ||||||||||||||||||||||||||||||||
| assert_eq!(jog(v), None, "{v}"); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /// The DWG bridge (acadrust) reads and writes DIMSTYLE group 50 in degrees too, so a DWG whose | ||||||||||||||||||||||||||||||||
| /// style jogs at 30° (made here by the bridge from our 2018 DXF) opens at 30°. Read as radians, | ||||||||||||||||||||||||||||||||
| /// it was 1719° and the style fell back to 45°. | ||||||||||||||||||||||||||||||||
| #[cfg(not(target_arch = "wasm32"))] | ||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||
| fn dwg_jog_angle_reads_in_degrees() { | ||||||||||||||||||||||||||||||||
| let dwg = cadcraft_dwg::dxf_to_dwg_as(dxf(&drawing(), Version::R2018).as_bytes(), "AC1032").unwrap(); | ||||||||||||||||||||||||||||||||
| let text = String::from_utf8(cadcraft_dwg::dwg_to_dxf(&dwg).unwrap()).unwrap(); | ||||||||||||||||||||||||||||||||
| let lines: Vec<&str> = text.lines().collect(); | ||||||||||||||||||||||||||||||||
| let jog: f64 = lines[style_group(&lines, "Detail", "50").unwrap()].trim().parse().unwrap(); | ||||||||||||||||||||||||||||||||
| assert!((jog - 30.0).abs() < 1e-9, "the bridge's DXF has degrees: {jog}"); | ||||||||||||||||||||||||||||||||
| let back = cadcraft_io::read(&dwg, "x.dwg").unwrap(); | ||||||||||||||||||||||||||||||||
| assert_eq!(back.dim_style("Detail").unwrap().jog_angle, 30.0); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve valid jog-angle precision.
For a valid group-50 value of
37.123456789123degrees,round(x)returns37.123456789. A DXF save-and-reopen cycle therefore changes the stored style value. Preserve degree inputs without quantization. If radian conversion needs roundoff correction, apply it only to conversion artifacts. (raw.githubusercontent.com)