Browse Source

Initial rect generation

Kevin Lee 1 year ago
parent
commit
e0faade7d4

+ 4 - 0
Cargo.toml

@@ -25,3 +25,7 @@ num = "0.4.1"
 serde = { version = "1.0.193", features = ["derive"] }
 serde_yaml = "0.9.29"
 strum = { version = "0.25.0", features = ["derive"] }
+
+[features]
+default = []
+default-debug = []

+ 4 - 0
README.md

@@ -108,3 +108,7 @@ Ops:
     Object: 0 # Optional, will append to object list if not provided
     Path: "exported_object.bin"
 ```
+
+### Generating Objects
+
+TODO..

+ 22 - 14
config.yml

@@ -18,20 +18,28 @@ Ops:
   #   To: 5
   #   Field: !Speed 10
 
-  - !ExportPen
-    Index: 0
-    Path: export_pen.bin
+  # - !ExportPen
+  #   Index: 0
+  #   Path: export_pen.bin
 
-  - !ImportPen
-    Index: 1
-    Path: export_pen.bin
+  # - !ImportPen
+  #   Index: 1
+  #   Path: export_pen.bin
 
-  - !ExportObject
-    Layer: 0
-    Object: 0
-    Path: export_object.bin
+  # - !ExportObject
+  #   Layer: 0
+  #   Object: 0
+  #   Path: export_object.bin
 
-  - !ImportObject
-    Layer: 0
-    Object: 0
-    Path: export_object.bin
+  # - !ImportObject
+  #   Layer: 0
+  #   Object: 0
+  #   Path: export_object.bin
+
+  - !RectangleArray
+    Width: 3.0
+    Height: 3.0
+    Rows: 10
+    Columns: 4
+    Spacing: 0.5
+    Pen: 0

+ 3 - 1
src/config/mod.rs

@@ -2,7 +2,7 @@ use ezcad::{array_of::ArrayOf, layer::Layer, pen::Pen};
 use serde::{Deserialize, Serialize};
 
 use self::{
-    object::{ExportObject, ImportObject},
+    object::{ExportObject, ImportObject, RectangleArray},
     pen::{ClonePen, ImportExportPen, PatchPen, PatternPen},
 };
 
@@ -18,6 +18,7 @@ pub enum Operation {
     ImportPen(ImportExportPen),
     ExportObject(ExportObject),
     ImportObject(ImportObject),
+    RectangleArray(RectangleArray),
 }
 
 pub trait Operations {
@@ -35,6 +36,7 @@ impl Operations for Vec<Operation> {
                 Operation::ExportPen(x) => x.export(pens),
                 Operation::ExportObject(x) => x.export(layers),
                 Operation::ImportObject(x) => x.import(layers),
+                Operation::RectangleArray(x) => x.generate(layers),
             }
         }
     }

+ 22 - 0
src/config/object.rs

@@ -82,3 +82,25 @@ impl ImportObject {
         }
     }
 }
+
+#[derive(Debug, Serialize, Deserialize)]
+#[serde(rename_all = "PascalCase")]
+pub struct RectangleArray {
+    width: f64,
+    height: f64,
+    rows: usize,
+    columns: usize,
+    spacing: f64,
+    pen: usize,
+}
+
+impl RectangleArray {
+    pub fn generate(&self, layers: &mut ArrayOf<Layer>) {
+        debug!(
+            "Generating {} x {} array of ({} x {}) with spacing of {} starting from pen {}",
+            self.rows, self.columns, self.width, self.height, self.spacing, self.pen
+        );
+
+        
+    }
+}

+ 12 - 14
src/config/pen.rs

@@ -181,22 +181,20 @@ impl PatternPen {
             }
         };
 
-        for dst in (self.from..=self.to).skip(1) {
-            // Clone to target pen
-            let pen: &mut Pen = pens.get_mut(dst).expect("Invalid pen index");
-            *pen = src.clone();
+        for idx in (self.from..=self.to).skip(1) {
+            let dst: &mut Pen = pens.get_mut(idx).expect("Invalid pen index");
 
             // Calculate new setting
             setting = match (setting, &self.field) {
                 (PatternField::Loops(prev), PatternField::Loops(incr)) => {
                     let value: i32 = prev + incr;
-                    debug!("Patching loop count for pen #{}: {}", dst, value);
+                    debug!("Patching loop count for pen #{}: {}", idx, value);
                     assert!(value > 0, "Pen loop count must be greater than zero");
                     PatternField::Loops(value)
                 }
                 (PatternField::Speed(prev), PatternField::Speed(incr)) => {
                     let value: f64 = prev + incr;
-                    debug!("Patching speed for pen #{}: {}", dst, value);
+                    debug!("Patching speed for pen #{}: {}", idx, value);
                     assert!(
                         value > SPEED_MIN && value <= SPEED_MAX,
                         "Pen speed must be between {} and {}",
@@ -207,7 +205,7 @@ impl PatternPen {
                 }
                 (PatternField::Power(prev), PatternField::Power(incr)) => {
                     let value: f64 = prev + incr;
-                    debug!("Patching power for pen #{}: {}", dst, value);
+                    debug!("Patching power for pen #{}: {}", idx, value);
                     assert!(
                         value > POWER_MIN && value <= POWER_MAX,
                         "Pen power must be between {} and {}",
@@ -218,7 +216,7 @@ impl PatternPen {
                 }
                 (PatternField::Frequency(prev), PatternField::Frequency(incr)) => {
                     let value: i32 = prev + incr;
-                    debug!("Patching frequency for pen #{}: {}", dst, value);
+                    debug!("Patching frequency for pen #{}: {}", idx, value);
                     assert!(
                         value >= FREQUENCY_MIN.try_into().unwrap()
                             && value <= FREQUENCY_MAX.try_into().unwrap(),
@@ -233,17 +231,17 @@ impl PatternPen {
 
             // Patch updated value
             match setting {
-                PatternField::Loops(x) => *pen.loop_count = x.try_into().unwrap(),
-                PatternField::Speed(x) => *pen.speed = x,
-                PatternField::Power(x) => *pen.power = x,
+                PatternField::Loops(x) => *dst.loop_count = x.try_into().unwrap(),
+                PatternField::Speed(x) => *dst.speed = x,
+                PatternField::Power(x) => *dst.power = x,
                 PatternField::Frequency(x) => {
-                    *pen.frequency = x.try_into().unwrap();
-                    *pen.frequency_2 = x.try_into().unwrap();
+                    *dst.frequency = x.try_into().unwrap();
+                    *dst.frequency_2 = x.try_into().unwrap();
                 }
             }
 
             // Always enable custom settings for pen
-            *pen.use_default = 0;
+            *dst.use_default = 0;
         }
     }
 }

+ 17 - 0
src/ezcad/array_of.rs

@@ -73,6 +73,23 @@ where
     }
 }
 
+impl<T, S> From<Vec<T>> for ArrayOfPrimitive<T, S>
+where
+    T: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
+    T: Num + 'static,
+    S: for<'a> BinRead<Args<'a> = ()> + for<'a> BinWrite<Args<'a> = ()>,
+    S: TryFrom<usize> + Copy + Clone + Debug + 'static,
+    <S as TryFrom<usize>>::Error: Display + Debug + Send + Sync,
+    usize: TryFrom<S>,
+{
+    fn from(value: Vec<T>) -> Self {
+        Self {
+            value,
+            _marker: PhantomData,
+        }
+    }
+}
+
 /// Wrapper to prefix array of single non-primitive type
 #[binrw]
 #[derive(Clone, PartialEq)]

+ 1 - 1
src/ezcad/field_of.rs

@@ -8,7 +8,7 @@ use std::{
 
 /// Wrapper to prefix arbitrary types with size of type as u32
 #[binrw]
-#[derive(Clone, Default, PartialEq)]
+#[derive(Clone, Copy, Default, PartialEq)]
 pub struct FieldOf<T>
 where
     T: for<'a> BinRead<Args<'a> = ()>,

+ 2 - 0
src/ezcad/objects/circle.rs

@@ -10,6 +10,7 @@ use crate::{
 
 use super::ObjectCore;
 
+#[cfg_attr(feature = "default-debug", derive(Debug))]
 #[derive(BinRead, BinWrite, Diff, PartialEq)]
 #[diff(attr(
     #[derive(Debug, PartialEq)]
@@ -25,6 +26,7 @@ pub struct Circle {
 }
 
 // Custom Debug implementation to only print known fields
+#[cfg(not(feature = "default-debug"))]
 impl Debug for Circle {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         f.debug_struct("Circle")

+ 2 - 0
src/ezcad/objects/ellipse.rs

@@ -10,6 +10,7 @@ use crate::{
 
 use super::ObjectCore;
 
+#[cfg_attr(feature = "default-debug", derive(Debug))]
 #[derive(BinRead, BinWrite, Diff, PartialEq)]
 #[diff(attr(
     #[derive(Debug, PartialEq)]
@@ -27,6 +28,7 @@ pub struct Ellipse {
 }
 
 // Custom Debug implementation to only print known fields
+#[cfg(not(feature = "default-debug"))]
 impl Debug for Ellipse {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         f.debug_struct("Ellipse")

+ 6 - 0
src/ezcad/objects/hatch.rs

@@ -45,6 +45,7 @@ pub struct HatchLine {
     lines: ArrayOf<Lines>,
 }
 
+#[cfg_attr(feature = "default-debug", derive(Debug))]
 #[derive(BinRead, BinWrite, Diff, PartialEq)]
 #[diff(attr(
     #[derive(Debug, PartialEq)]
@@ -75,6 +76,7 @@ pub struct HatchSettings1 {
 }
 
 // Custom Debug implementation to only print known fields
+#[cfg(not(feature = "default-debug"))]
 impl Debug for HatchSettings1 {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         f.debug_struct("HatchSettings1")
@@ -96,6 +98,7 @@ impl Debug for HatchSettings1 {
     }
 }
 
+#[cfg_attr(feature = "default-debug", derive(Debug))]
 #[derive(BinRead, BinWrite, Diff, PartialEq)]
 #[diff(attr(
     #[derive(Debug, PartialEq)]
@@ -119,6 +122,7 @@ pub struct HatchSettings2 {
 }
 
 // Custom Debug implementation to only print known fields
+#[cfg(not(feature = "default-debug"))]
 impl Debug for HatchSettings2 {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         f.debug_struct("HatchSettings2")
@@ -138,6 +142,7 @@ impl Debug for HatchSettings2 {
     }
 }
 
+#[cfg_attr(feature = "default-debug", derive(Debug))]
 #[derive(BinRead, BinWrite, Diff, PartialEq)]
 #[diff(attr(
     #[derive(Debug, PartialEq)]
@@ -159,6 +164,7 @@ pub struct Hatch {
 }
 
 // Custom Debug implementation to only print known fields
+#[cfg(not(feature = "default-debug"))]
 impl Debug for Hatch {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         f.debug_struct("Hatch")

+ 33 - 0
src/ezcad/objects/mod.rs

@@ -8,6 +8,7 @@ use modular_bitfield::{
 };
 
 use crate::{
+    array_of::ArrayOfPrimitive,
     field_of::FieldOf,
     types::{Field, ObjectType, Point, WString, F64, U16, U32},
 };
@@ -39,6 +40,7 @@ pub struct ObjectFlags {
 }
 
 /// Core properties defined for all object types
+#[cfg_attr(feature = "default-debug", derive(Debug))]
 #[derive(BinRead, BinWrite, Diff, PartialEq)]
 #[diff(attr(
     #[derive(Debug, PartialEq)]
@@ -61,6 +63,7 @@ pub struct ObjectCore {
 }
 
 // Custom Debug implementation to only print known fields
+#[cfg(not(feature = "default-debug"))]
 impl Debug for ObjectCore {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         f.debug_struct("ObjectCore")
@@ -78,6 +81,36 @@ impl Debug for ObjectCore {
     }
 }
 
+impl Default for ObjectCore {
+    fn default() -> Self {
+        Self {
+            pen: 0.into(),
+            obj_type: ObjectType::Unknown.into(),
+            flags: ObjectFlags::new()
+                .with_disabled(0)
+                .with_aspect_ratio_unlocked(1)
+                .into(),
+            name: String::new().into(),
+            count: 0.into(),
+            _unknown_1: vec![0, 0].into(), // 0_u16
+            io_control_enable_mask: Default::default(),
+            io_control_disable_mask: Default::default(),
+            _unknown_2: [
+                vec![0, 0].into(),                     // 0_u16
+                vec![1, 0, 0, 0].into(),               // 1_u32
+                vec![1, 0, 0, 0].into(),               // 1_u32
+                vec![0, 0, 0, 0, 0, 0, 36, 64].into(), // 10.0_f64
+                vec![0, 0, 0, 0, 0, 0, 36, 64].into(), // 10.0_f64
+            ]
+            .into(),
+            origin: Point { x: 0.0, y: 0.0 }.into(),
+            z: 0.0.into(),
+            a: 0.0.into(),
+            _unknown_3: vec![0, 0, 0, 0].into(), // 0_u32
+        }
+    }
+}
+
 #[derive(BinRead, BinWrite, Debug, Diff, PartialEq)]
 #[diff(attr(
     #[derive(Debug, PartialEq)]

+ 2 - 0
src/ezcad/objects/polygon.rs

@@ -10,6 +10,7 @@ use crate::{
 
 use super::ObjectCore;
 
+#[cfg_attr(feature = "default-debug", derive(Debug))]
 #[derive(BinRead, BinWrite, Diff, PartialEq)]
 #[diff(attr(
     #[derive(Debug, PartialEq)]
@@ -29,6 +30,7 @@ pub struct Polygon {
 }
 
 // Custom Debug implementation to only print known fields
+#[cfg(not(feature = "default-debug"))]
 impl Debug for Polygon {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         f.debug_struct("Polygon")

+ 26 - 0
src/ezcad/objects/rectangle.rs

@@ -10,6 +10,7 @@ use crate::{
 
 use super::ObjectCore;
 
+#[cfg_attr(feature = "default-debug", derive(Debug))]
 #[derive(BinRead, BinWrite, Diff, PartialEq)]
 #[diff(attr(
     #[derive(Debug, PartialEq)]
@@ -27,6 +28,7 @@ pub struct Rectangle {
 }
 
 // Custom Debug implementation to only print known fields
+#[cfg(not(feature = "default-debug"))]
 impl Debug for Rectangle {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         f.debug_struct("Rectangle")
@@ -40,3 +42,27 @@ impl Debug for Rectangle {
             .finish()
     }
 }
+
+impl Default for Rectangle {
+    fn default() -> Self {
+        Self {
+            core: Default::default(),
+            corner_a: Point { x: 0.0, y: 0.0 }.into(),
+            corner_b: Point { 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(),
+            _unknown_1: [
+                vec![0, 0, 0, 0].into(), // 0_u32
+                vec![
+                    0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 63,
+                ] // ???
+                .into(),
+            ]
+            .into(),
+        }
+    }
+}

+ 2 - 0
src/ezcad/pen.rs

@@ -51,6 +51,7 @@ pub struct Pens {
     pub pens: Vec<Pen>,
 }
 
+#[cfg_attr(feature = "default-debug", derive(Debug))]
 #[derive(BinRead, BinWrite, Clone, Diff, PartialEq)]
 #[diff(attr(
     #[derive(Debug, PartialEq)]
@@ -92,6 +93,7 @@ pub struct Pen {
 }
 
 // Custom Debug implementation to only print known fields
+#[cfg(not(feature = "default-debug"))]
 impl Debug for Pen {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         f.debug_struct("Pen")