Kevin Lee 1 year ago
parent
commit
ddf9049083
5 changed files with 83 additions and 84 deletions
  1. 4 11
      README.md
  2. 39 40
      config.yml
  3. 1 3
      src/config/mod.rs
  4. 4 7
      src/config/object.rs
  5. 35 23
      src/config/pen.rs

+ 4 - 11
README.md

@@ -37,13 +37,14 @@ Ops:
     Frequency: 10000        # Optional - target pen frequency
 ```
 
-Cloning one pen to another (and optionally override settings):
+Cloning pen(s) (and optionally override settings):
 
 ``` yaml
 Ops: 
   - !ClonePen
     From: 0                 # Source pen
     To: 1                   # Target pen
+    # Inclusive: true       # Optional - will clone to all pens in range
     # Color: [64, 64, 64]   # Optional - target pen RGB color
     # Enabled: true         # Optional - target pen enable flag
     # LoopCount: 3          # Optional - target pen loop count
@@ -52,15 +53,6 @@ Ops:
     # Frequency: 10000      # Optional - target pen frequency
 ```
 
-Cloning one pen to another pen (and all pens in between)
-
-``` yaml
-Ops:
-  - !ClonePenRange
-    From: 0
-    To: 3
-```
-
 Incrementing a specific field by some value across a sequence of pens:
 
 ``` yaml
@@ -130,10 +122,11 @@ Generating a grid of rectangles with incrementing pens:
 ``` yaml
 Ops:
   - !RectangleArray
+    Layer: 0
     Width: 10.0
     Height: 10.0
-    Rows: 5
     Columns: 3
+    Rows: 5
     Spacing: 0.5
     Z: 0.0
     StartingPen: 0

+ 39 - 40
config.yml

@@ -1,44 +1,43 @@
 Ops: 
-  # - !PatchPen
-  #   Pen: 1
-  #   Color: [127, 127, 127]
-  #   Enabled: true
-  #   LoopCount: 3
-  #   Speed: 20.0
-  #   Power: 55.0
-  #   Frequency: 10000
-
-  # - !ClonePen
-  #   From: 0
-  #   To: 1
-  #   LoopCount: 2
-
-  # - !ClonePenRange
-  #   From: 0
-  #   To: 4
-
-  # - !PatternPen
-  #   From: 1
-  #   To: 5
-  #   Field: !Speed 10
-
-  # - !ExportPen
-  #   Index: 0
-  #   Path: export_pen.bin
-
-  # - !ImportPen
-  #   Index: 1
-  #   Path: export_pen.bin
-
-  # - !ExportObject
-  #   Layer: 0
-  #   Object: 0
-  #   Path: export_object.bin
-
-  # - !ImportObject
-  #   Layer: 0
-  #   Object: 0
-  #   Path: export_object.bin
+  - !PatchPen
+    Pen: 0
+    Color: [127, 127, 127]
+    Enabled: true
+    LoopCount: 3
+    Speed: 20.0
+    Power: 55.0
+    Frequency: 10000
+
+  - !ClonePen
+    From: 0
+    To: 5
+    Inclusive: True
+
+  - !PatternPen
+    From: 0
+    To: 5
+    Field: !Speed 10
+
+  - !ExportPen
+    Index: 0
+    Path: export_pen.bin
+
+  - !ImportPen
+    Index: 1
+    Path: export_pen.bin
+
+  - !ExportObject
+    Layer: 0
+    Object: 0
+    Path: export_object.bin
+
+  - !ImportObject
+    Layer: 0
+    Object: 0
+    Path: export_object.bin
+
+  - !DeleteObjects
+    Layer: 0
 
   - !RectangleArray
     Layer: 0

+ 1 - 3
src/config/mod.rs

@@ -13,7 +13,6 @@ pub mod pen;
 pub enum Operation {
     PatchPen(PatchPen),
     ClonePen(ClonePen),
-    ClonePenRange(ClonePen),
     PatternPen(PatternPen),
     ExportPen(ImportExportPen),
     ImportPen(ImportExportPen),
@@ -32,8 +31,7 @@ impl Operations for Vec<Operation> {
         for op in self {
             match op {
                 Operation::PatchPen(x) => x.patch(pens),
-                Operation::ClonePen(x) => x.clone_single(pens),
-                Operation::ClonePenRange(x) => x.clone_range(pens),
+                Operation::ClonePen(x) => x.clone(pens),
                 Operation::PatternPen(x) => x.pattern(pens),
                 Operation::ImportPen(x) => x.import(pens),
                 Operation::ExportPen(x) => x.export(pens),

+ 4 - 7
src/config/object.rs

@@ -12,7 +12,7 @@ use ezcad::{
     pen::Pen,
     types::Point,
 };
-use log::{debug, trace};
+use log::debug;
 use rand::{seq::SliceRandom, thread_rng};
 use serde::{Deserialize, Serialize};
 
@@ -100,7 +100,7 @@ pub struct DeleteObjects {
 impl DeleteObjects {
     pub fn delete(&self, layers: &mut ArrayOf<Layer>) {
         debug!(
-            "Deleting {} from  layer #{}",
+            "Deleting {} from layer #{}",
             match &self.object {
                 Some(object) => format!("object #{}", object),
                 None => format!("all objects"),
@@ -191,12 +191,9 @@ impl RectangleArray {
             *rect.core.pen = (self.starting_pen + pen_incr).try_into().unwrap();
             *rect.core.z = self.z;
 
-            trace!(
+            debug!(
                 "Adding rectangle with pen #{} at {} (from {} to {})",
-                *rect.core.pen,
-                *rect.core.origin,
-                *rect.corner_a,
-                *rect.corner_b
+                *rect.core.pen, *rect.core.origin, *rect.corner_a, *rect.corner_b
             );
 
             layer.objects.push(Object::Rectangle(rect));

+ 35 - 23
src/config/pen.rs

@@ -105,41 +105,53 @@ impl PatchPen {
 pub struct ClonePen {
     from: usize,
     to: usize,
+    inclusive: Option<bool>,
     #[serde(flatten)]
     patch: Option<Patch>,
 }
 
 impl ClonePen {
-    pub fn clone_single(&self, pens: &mut Vec<Pen>) {
-        debug!("Cloning pen #{} to #{}", self.from, self.to);
+    pub fn clone(&self, pens: &mut Vec<Pen>) {
+        debug!(
+            "Cloning pen #{} to #{}{}",
+            self.from,
+            self.to,
+            match self.inclusive {
+                Some(true) => format!("(inclusive)"),
+                _ => format!(""),
+            }
+        );
 
         // Clone pen
         let src: Pen = pens.get(self.from).expect("Invalid pen index").clone();
-        let dst: &mut Pen = pens.get_mut(self.to).expect("Invalid pen index");
-        *dst = src;
 
-        // Patch pen if needed
-        if let Some(patch) = &self.patch {
-            patch.patch(self.to, pens);
-        }
-    }
+        match self.inclusive {
+            Some(true) => {
+                assert!(
+                    self.to > self.from,
+                    "Target pen(s) must be greater than source pen"
+                );
 
-    pub fn clone_range(&self, pens: &mut Vec<Pen>) {
-        debug!(
-            "Cloning pen #{} to #{} (and all in between)",
-            self.from, self.to
-        );
-        assert!(
-            self.to > self.from,
-            "Target pen(s) must be greater than source pen"
-        );
+                // Clone pen
+                for idx in (self.from..=self.to).skip(1) {
+                    let dst: &mut Pen = pens.get_mut(idx).expect("Invalid pen index");
+                    *dst = src.clone();
 
-        // Clone pen
-        let src: Pen = pens.get(self.from).expect("Invalid pen index").clone();
+                    // Patch pen if needed
+                    if let Some(patch) = &self.patch {
+                        patch.patch(idx, pens);
+                    }
+                }
+            }
+            _ => {
+                let dst: &mut Pen = pens.get_mut(self.to).expect("Invalid pen index");
+                *dst = src;
 
-        for idx in (self.from..=self.to).skip(1) {
-            let dst: &mut Pen = pens.get_mut(idx).expect("Invalid pen index");
-            *dst = src.clone();
+                // Patch pen if needed
+                if let Some(patch) = &self.patch {
+                    patch.patch(self.to, pens);
+                }
+            }
         }
     }
 }