Browse Source

Add delete object

Kevin Lee 1 year ago
parent
commit
8bbe3bc990
3 changed files with 68 additions and 18 deletions
  1. 35 15
      README.md
  2. 3 1
      src/config/mod.rs
  3. 30 2
      src/config/object.rs

+ 35 - 15
README.md

@@ -23,9 +23,7 @@ Operations defined in the configuration file are applied in order of definition.
 
 ### Patching Pen
 
-Three operations are supported for patching pen settings:
-
-1. Setting values of specific fields for a given pen:
+Setting values of specific fields for a given pen:
 
 ``` yaml
 Ops: 
@@ -39,7 +37,7 @@ Ops:
     Frequency: 10000        # Optional - target pen frequency
 ```
 
-2. Cloning one pen to another (and optionally override settings):
+Cloning one pen to another (and optionally override settings):
 
 ``` yaml
 Ops: 
@@ -54,7 +52,7 @@ Ops:
     # Frequency: 10000      # Optional - target pen frequency
 ```
 
-3. Incrementing a specific field by some value across a sequence of pens:
+Incrementing a specific field by some value across a sequence of pens:
 
 ``` yaml
 Ops: 
@@ -67,11 +65,9 @@ Ops:
     # Field: !Frequency 1000  # Choose one
 ```
 
-### Import/Export
-
-Individual pens and objects can be exported via operations:
+### Importing and Exporting Pens and Objects
 
-1. Exporting a pen to a file:
+Exporting a pen to a file:
 
 ``` yaml
 Ops:
@@ -80,7 +76,7 @@ Ops:
     Path: "exported_pen.bin"
 ```
 
-2. Importing a pen from a file:
+Importing a pen from a file:
 
 ``` yaml
 Ops:
@@ -89,7 +85,7 @@ Ops:
     Path: "exported_pen.bin"
 ```
 
-3. Exporting an object to a file:
+Exporting an object to a file:
 
 ``` yaml
 Ops:
@@ -99,16 +95,40 @@ Ops:
     Path: "exported_object.bin"
 ```
 
-3. Importing an object from a file:
+Importing and append an object from a file:
 
 ``` yaml
 Ops:
   - !ImportObject
     Layer: 0
-    Object: 0 # Optional, will append to object list if not provided
     Path: "exported_object.bin"
 ```
 
-### Generating Objects
+Importing and replace an existing object from a file:
+
+``` yaml
+Ops:
+  - !ImportObject
+    Layer: 0
+    Object: 3
+    Path: "exported_object.bin"
+```
+
+### Deleting and Generating Objects
+
+Deleting all objects from a layer:
 
-TODO..
+``` yaml
+Ops:
+  - !DeleteObjects
+    Layer: 0
+```
+
+Deleting a specific object from a layer:
+
+``` yaml
+Ops:
+  - !DeleteObjects
+    Layer: 0
+    Object: 3
+```

+ 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, RectangleArray},
+    object::{DeleteObjects, ExportObject, ImportObject, RectangleArray},
     pen::{ClonePen, ImportExportPen, PatchPen, PatternPen},
 };
 
@@ -18,6 +18,7 @@ pub enum Operation {
     ImportPen(ImportExportPen),
     ExportObject(ExportObject),
     ImportObject(ImportObject),
+    DeleteObjects(DeleteObjects),
     RectangleArray(RectangleArray),
 }
 
@@ -36,6 +37,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::DeleteObjects(x) => x.delete(layers),
                 Operation::RectangleArray(x) => x.generate(layers),
             }
         }

+ 30 - 2
src/config/object.rs

@@ -83,6 +83,36 @@ impl ImportObject {
     }
 }
 
+#[derive(Debug, Serialize, Deserialize)]
+#[serde(rename_all = "PascalCase")]
+pub struct DeleteObjects {
+    layer: usize,
+    object: Option<usize>,
+}
+
+impl DeleteObjects {
+    pub fn delete(&self, layers: &mut ArrayOf<Layer>) {
+        debug!(
+            "Deleting {} from  layer #{}",
+            match &self.object {
+                Some(object) => format!("object #{}", object),
+                None => format!("all objects"),
+            },
+            self.layer,
+        );
+
+        let layer: &mut Layer = layers.get_mut(self.layer).expect("Invalid layer index");
+
+        match self.object {
+            Some(index) => {
+                assert!(index < layer.objects.len(), "Invalid object index");
+                layer.objects.remove(index);
+            }
+            None => layer.objects.clear(),
+        }
+    }
+}
+
 #[derive(Debug, Serialize, Deserialize)]
 #[serde(rename_all = "PascalCase")]
 pub struct RectangleArray {
@@ -100,7 +130,5 @@ impl RectangleArray {
             "Generating {} x {} array of ({} x {}) with spacing of {} starting from pen {}",
             self.rows, self.columns, self.width, self.height, self.spacing, self.pen
         );
-
-        
     }
 }