Browse Source

Object query WIP

Kevin Lee 4 months ago
parent
commit
8af494f133

+ 7 - 1
src/ezcad/objects/circle.rs

@@ -1,4 +1,4 @@
-use std::fmt::Debug;
+use std::fmt::{Debug, Display};
 
 use binrw::{BinRead, BinWrite};
 use diff::Diff;
@@ -24,6 +24,12 @@ pub struct Circle {
     pub modifier: ObjectModifier,
 }
 
+impl Display for Circle {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "{}, Radius: {}", self.core, self.radius)
+    }
+}
+
 impl Default for Circle {
     fn default() -> Self {
         Self {

+ 65 - 59
src/ezcad/objects/ellipse.rs

@@ -1,59 +1,65 @@
-use std::fmt::Debug;
-
-use binrw::{BinRead, BinWrite};
-use diff::Diff;
-
-use crate::{
-    field_of::FieldOf,
-    types::{Coordinate, ObjectType, F64, U32},
-};
-
-use super::{ObjectCore, ObjectModifier, Translate};
-
-#[derive(BinRead, BinWrite, Clone, Debug, Diff, PartialEq)]
-#[diff(attr(
-    #[derive(Debug, PartialEq)]
-))]
-pub struct Ellipse {
-    pub core: ObjectCore,
-    #[brw(magic(8u32))] // Number of following fields in struct
-    pub clockwise: U32,
-    pub drawn_corner_a: FieldOf<Coordinate>,
-    pub drawn_corner_b: FieldOf<Coordinate>,
-    pub start_angle: F64, // Radians
-    pub end_angle: F64,   // Radians
-    pub modifier: ObjectModifier,
-    pub open_curve: U32,
-}
-
-impl Default for Ellipse {
-    fn default() -> Self {
-        Self {
-            core: ObjectCore::default(ObjectType::Ellipse),
-            clockwise: 0.into(),
-            drawn_corner_a: Coordinate { x: 0.0, y: 0.0 }.into(),
-            drawn_corner_b: Coordinate { x: 0.0, y: 0.0 }.into(),
-            start_angle: 0.0.into(),
-            end_angle: 0.0.into(),
-            modifier: ObjectModifier::default(),
-            open_curve: 0.into(),
-        }
-    }
-}
-
-impl Translate for Ellipse {
-    fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>) {
-        origin.map(|origin| {
-            let delta: Coordinate = origin - *self.core.origin;
-            self.modifier.move_relative(delta);
-        });
-        self.core.move_absolute(origin, z);
-    }
-
-    fn move_relative(&mut self, delta: Option<Coordinate>, z: Option<f64>) {
-        delta.map(|delta| {
-            self.modifier.move_relative(delta);
-        });
-        self.core.move_relative(delta, z);
-    }
-}
+use std::fmt::{Debug, Display};
+
+use binrw::{BinRead, BinWrite};
+use diff::Diff;
+
+use crate::{
+    field_of::FieldOf,
+    types::{Coordinate, ObjectType, F64, U32},
+};
+
+use super::{ObjectCore, ObjectModifier, Translate};
+
+#[derive(BinRead, BinWrite, Clone, Debug, Diff, PartialEq)]
+#[diff(attr(
+    #[derive(Debug, PartialEq)]
+))]
+pub struct Ellipse {
+    pub core: ObjectCore,
+    #[brw(magic(8u32))] // Number of following fields in struct
+    pub clockwise: U32,
+    pub drawn_corner_a: FieldOf<Coordinate>,
+    pub drawn_corner_b: FieldOf<Coordinate>,
+    pub start_angle: F64, // Radians
+    pub end_angle: F64,   // Radians
+    pub modifier: ObjectModifier,
+    pub open_curve: U32,
+}
+
+impl Display for Ellipse {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "{}", self.core)
+    }
+}
+
+impl Default for Ellipse {
+    fn default() -> Self {
+        Self {
+            core: ObjectCore::default(ObjectType::Ellipse),
+            clockwise: 0.into(),
+            drawn_corner_a: Coordinate { x: 0.0, y: 0.0 }.into(),
+            drawn_corner_b: Coordinate { x: 0.0, y: 0.0 }.into(),
+            start_angle: 0.0.into(),
+            end_angle: 0.0.into(),
+            modifier: ObjectModifier::default(),
+            open_curve: 0.into(),
+        }
+    }
+}
+
+impl Translate for Ellipse {
+    fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>) {
+        origin.map(|origin| {
+            let delta: Coordinate = origin - *self.core.origin;
+            self.modifier.move_relative(delta);
+        });
+        self.core.move_absolute(origin, z);
+    }
+
+    fn move_relative(&mut self, delta: Option<Coordinate>, z: Option<f64>) {
+        delta.map(|delta| {
+            self.modifier.move_relative(delta);
+        });
+        self.core.move_relative(delta, z);
+    }
+}

+ 7 - 1
src/ezcad/objects/hatch.rs

@@ -1,4 +1,4 @@
-use std::fmt::Debug;
+use std::fmt::{Debug, Display};
 
 use crate::{
     array_of::ArrayOf,
@@ -370,6 +370,12 @@ pub struct Hatch {
     pub hatches: Option<Hatches>,
 }
 
+impl Display for Hatch {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "{}", self.core)
+    }
+}
+
 impl Translate for Hatch {
     fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>) {
         self.outline

+ 7 - 1
src/ezcad/objects/line.rs

@@ -1,4 +1,4 @@
-use std::fmt::Debug;
+use std::fmt::{Debug, Display};
 
 use binrw::{binrw, BinRead, BinWrite};
 use diff::Diff;
@@ -75,6 +75,12 @@ pub struct Lines {
     pub lines: Vec<LineType>,
 }
 
+impl Display for Lines {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "{}", self.core)
+    }
+}
+
 impl Translate for Lines {
     fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>) {
         origin.map(|origin| {

+ 32 - 2
src/ezcad/objects/mod.rs

@@ -1,5 +1,5 @@
 use std::{
-    fmt::Debug,
+    fmt::{Debug, Display},
     fs::File,
     io::{Cursor, Read, Write},
     path::PathBuf,
@@ -73,6 +73,21 @@ pub struct ObjectCore {
     pub index: U32, // Increments for each hatch fill line (types 1/2) or 0xFFFF_FFFF (types 3/4/5/7/8)
 }
 
+impl Display for ObjectCore {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(
+            f,
+            "Enabled: {}, Type: {}, Pen: {}, Count: {}, Origin: {}, Z: {}",
+            (self.flags.disabled() == 0),
+            self.obj_type,
+            self.pen,
+            self.count,
+            self.origin,
+            self.z
+        )
+    }
+}
+
 // Custom Debug implementation to only print known fields
 #[cfg(not(feature = "default-debug"))]
 impl Debug for ObjectCore {
@@ -135,7 +150,7 @@ impl ObjectCore {
     }
 }
 
-#[derive(BinRead, BinWrite, Clone, Debug, Diff, PartialEq, strum::Display)]
+#[derive(BinRead, BinWrite, Clone, Debug, Diff, PartialEq)]
 #[diff(attr(
     #[derive(Debug, PartialEq)]
 ))]
@@ -156,6 +171,21 @@ pub enum Object {
     Hatch(Hatch),
 }
 
+impl Display for Object {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        // write!(f, "{}", self.core)
+        match self {
+            Object::Curve(x) => write!(f, "{x}"),
+            Object::Point(x) => write!(f, "{x}"),
+            Object::Rectangle(x) => write!(f, "{x}"),
+            Object::Circle(x) => write!(f, "{x}"),
+            Object::Ellipse(x) => write!(f, "{x}"),
+            Object::Polygon(x) => write!(f, "{x}"),
+            Object::Hatch(x) => write!(f, "{x}"),
+        }
+    }
+}
+
 impl Translate for Object {
     fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>) {
         match self {

+ 69 - 63
src/ezcad/objects/polygon.rs

@@ -1,63 +1,69 @@
-use std::fmt::Debug;
-
-use binrw::{BinRead, BinWrite};
-use diff::Diff;
-
-use crate::{
-    field_of::FieldOf,
-    types::{Coordinate, ObjectType, F64, U32},
-};
-
-use super::{ObjectCore, ObjectModifier, Translate};
-
-#[derive(BinRead, BinWrite, Clone, Debug, Diff, PartialEq)]
-#[diff(attr(
-    #[derive(Debug, PartialEq)]
-))]
-pub struct Polygon {
-    pub core: ObjectCore,
-    #[brw(magic(10u32))] // Number of following fields in struct
-    pub invert_shape: U32,
-    pub drawn_corner_a: FieldOf<Coordinate>,
-    pub drawn_corner_b: FieldOf<Coordinate>,
-    pub offset_cx: F64,
-    pub offset_cy: F64,
-    pub offset_dx: F64,
-    pub offset_dy: F64,
-    pub edges: U32,
-    pub modifier: ObjectModifier,
-}
-
-impl Default for Polygon {
-    fn default() -> Self {
-        Self {
-            core: ObjectCore::default(ObjectType::Polygon),
-            invert_shape: 1.into(),
-            drawn_corner_a: Coordinate { x: 0.0, y: 0.0 }.into(),
-            drawn_corner_b: Coordinate { x: 0.0, y: 0.0 }.into(),
-            offset_cx: 0.0.into(),
-            offset_cy: 0.0.into(),
-            offset_dx: 0.0.into(),
-            offset_dy: 0.0.into(),
-            edges: 6.into(),
-            modifier: ObjectModifier::default(),
-        }
-    }
-}
-
-impl Translate for Polygon {
-    fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>) {
-        origin.map(|origin| {
-            let delta: Coordinate = origin - *self.core.origin;
-            self.modifier.move_relative(delta);
-        });
-        self.core.move_absolute(origin, z);
-    }
-
-    fn move_relative(&mut self, delta: Option<Coordinate>, z: Option<f64>) {
-        delta.map(|delta| {
-            self.modifier.move_relative(delta);
-        });
-        self.core.move_relative(delta, z);
-    }
-}
+use std::fmt::{Debug, Display};
+
+use binrw::{BinRead, BinWrite};
+use diff::Diff;
+
+use crate::{
+    field_of::FieldOf,
+    types::{Coordinate, ObjectType, F64, U32},
+};
+
+use super::{ObjectCore, ObjectModifier, Translate};
+
+#[derive(BinRead, BinWrite, Clone, Debug, Diff, PartialEq)]
+#[diff(attr(
+    #[derive(Debug, PartialEq)]
+))]
+pub struct Polygon {
+    pub core: ObjectCore,
+    #[brw(magic(10u32))] // Number of following fields in struct
+    pub invert_shape: U32,
+    pub drawn_corner_a: FieldOf<Coordinate>,
+    pub drawn_corner_b: FieldOf<Coordinate>,
+    pub offset_cx: F64,
+    pub offset_cy: F64,
+    pub offset_dx: F64,
+    pub offset_dy: F64,
+    pub edges: U32,
+    pub modifier: ObjectModifier,
+}
+
+impl Display for Polygon {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "{}", self.core)
+    }
+}
+
+impl Default for Polygon {
+    fn default() -> Self {
+        Self {
+            core: ObjectCore::default(ObjectType::Polygon),
+            invert_shape: 1.into(),
+            drawn_corner_a: Coordinate { x: 0.0, y: 0.0 }.into(),
+            drawn_corner_b: Coordinate { x: 0.0, y: 0.0 }.into(),
+            offset_cx: 0.0.into(),
+            offset_cy: 0.0.into(),
+            offset_dx: 0.0.into(),
+            offset_dy: 0.0.into(),
+            edges: 6.into(),
+            modifier: ObjectModifier::default(),
+        }
+    }
+}
+
+impl Translate for Polygon {
+    fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>) {
+        origin.map(|origin| {
+            let delta: Coordinate = origin - *self.core.origin;
+            self.modifier.move_relative(delta);
+        });
+        self.core.move_absolute(origin, z);
+    }
+
+    fn move_relative(&mut self, delta: Option<Coordinate>, z: Option<f64>) {
+        delta.map(|delta| {
+            self.modifier.move_relative(delta);
+        });
+        self.core.move_relative(delta, z);
+    }
+}

+ 67 - 61
src/ezcad/objects/rectangle.rs

@@ -1,61 +1,67 @@
-use std::fmt::Debug;
-
-use binrw::{BinRead, BinWrite};
-use diff::Diff;
-
-use crate::{
-    field_of::FieldOf,
-    types::{Coordinate, ObjectType, F64},
-};
-
-use super::{ObjectCore, ObjectModifier, Translate};
-
-#[derive(BinRead, BinWrite, Clone, Debug, Diff, PartialEq)]
-#[diff(attr(
-    #[derive(Debug, PartialEq)]
-))]
-pub struct Rectangle {
-    pub core: ObjectCore,
-    #[brw(magic(8u32))] // Number of following fields in struct
-    pub drawn_corner_a: FieldOf<Coordinate>,
-    pub drawn_corner_b: FieldOf<Coordinate>,
-    pub round_bottom_left: F64,
-    pub round_bottom_right: F64,
-    pub round_top_right: F64,
-    pub round_top_left: F64,
-    pub modifier: ObjectModifier,
-}
-
-impl Default for Rectangle {
-    fn default() -> Self {
-        Self {
-            core: ObjectCore::default(ObjectType::Rectangle),
-            drawn_corner_a: Coordinate { x: 0.0, y: 0.0 }.into(),
-            drawn_corner_b: Coordinate { x: 0.0, y: 0.0 }.into(),
-            round_bottom_left: 0.0.into(),
-            round_bottom_right: 0.0.into(),
-            round_top_right: 0.0.into(),
-            round_top_left: 0.0.into(),
-            modifier: ObjectModifier::default(),
-        }
-    }
-}
-
-// origin_x = x_corretion + (drawn_a.x + drawn_b.x) / 2 * x_scale
-// x_correction = origin_x - (drawn_a.x + drawn_b.x) / 2 * x_scale
-impl Translate for Rectangle {
-    fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>) {
-        origin.map(|origin| {
-            let delta: Coordinate = origin - *self.core.origin;
-            self.modifier.move_relative(delta);
-        });
-        self.core.move_absolute(origin, z);
-    }
-
-    fn move_relative(&mut self, delta: Option<Coordinate>, z: Option<f64>) {
-        delta.map(|delta| {
-            self.modifier.move_relative(delta);
-        });
-        self.core.move_relative(delta, z);
-    }
-}
+use std::fmt::{Debug, Display};
+
+use binrw::{BinRead, BinWrite};
+use diff::Diff;
+
+use crate::{
+    field_of::FieldOf,
+    types::{Coordinate, ObjectType, F64},
+};
+
+use super::{ObjectCore, ObjectModifier, Translate};
+
+#[derive(BinRead, BinWrite, Clone, Debug, Diff, PartialEq)]
+#[diff(attr(
+    #[derive(Debug, PartialEq)]
+))]
+pub struct Rectangle {
+    pub core: ObjectCore,
+    #[brw(magic(8u32))] // Number of following fields in struct
+    pub drawn_corner_a: FieldOf<Coordinate>,
+    pub drawn_corner_b: FieldOf<Coordinate>,
+    pub round_bottom_left: F64,
+    pub round_bottom_right: F64,
+    pub round_top_right: F64,
+    pub round_top_left: F64,
+    pub modifier: ObjectModifier,
+}
+
+impl Display for Rectangle {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "{}", self.core)
+    }
+}
+
+impl Default for Rectangle {
+    fn default() -> Self {
+        Self {
+            core: ObjectCore::default(ObjectType::Rectangle),
+            drawn_corner_a: Coordinate { x: 0.0, y: 0.0 }.into(),
+            drawn_corner_b: Coordinate { x: 0.0, y: 0.0 }.into(),
+            round_bottom_left: 0.0.into(),
+            round_bottom_right: 0.0.into(),
+            round_top_right: 0.0.into(),
+            round_top_left: 0.0.into(),
+            modifier: ObjectModifier::default(),
+        }
+    }
+}
+
+// origin_x = x_corretion + (drawn_a.x + drawn_b.x) / 2 * x_scale
+// x_correction = origin_x - (drawn_a.x + drawn_b.x) / 2 * x_scale
+impl Translate for Rectangle {
+    fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>) {
+        origin.map(|origin| {
+            let delta: Coordinate = origin - *self.core.origin;
+            self.modifier.move_relative(delta);
+        });
+        self.core.move_absolute(origin, z);
+    }
+
+    fn move_relative(&mut self, delta: Option<Coordinate>, z: Option<f64>) {
+        delta.map(|delta| {
+            self.modifier.move_relative(delta);
+        });
+        self.core.move_relative(delta, z);
+    }
+}

+ 5 - 4
src/main.rs

@@ -227,10 +227,12 @@ fn main() {
                     .get(layer_index)
                     .expect("Invalid layer index");
                 let object: &Object = layer.objects.get(obj_index).expect("Invalid object index");
-                info!("Layer {}, Object {}: {:?}", layer_index, obj_index, object);
                 let pen_index: u32 = *object.core().pen;
-                trace!(
-                    "Pen #{}: {}",
+                info!(
+                    "Layer {}, Object {}:\n{}\nPen: #{}: {}",
+                    layer_index,
+                    obj_index,
+                    object,
                     pen_index,
                     file.pens_offset
                         .data
@@ -238,7 +240,6 @@ fn main() {
                         .get(pen_index as usize)
                         .expect("Invalid pen index in object")
                 );
-                // TODO: object info
             });
         }
         SubCommands::Apply(args) => {