Browse Source

Refactor power density as Hatch fn

Kevin Lee 1 month ago
parent
commit
86119b86b7
3 changed files with 32 additions and 24 deletions
  1. 0 1
      src/ezcad/objects/circle.rs
  2. 26 1
      src/ezcad/objects/hatch.rs
  3. 6 22
      src/main.rs

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

@@ -2,7 +2,6 @@ use std::fmt::{Debug, Display};
 
 use binrw::{BinRead, BinWrite};
 use diff::Diff;
-use float_cmp::approx_eq;
 use log::warn;
 
 use crate::{

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

@@ -3,7 +3,8 @@ use std::fmt::{Debug, Display};
 use crate::{
     array_of::ArrayOf,
     field_of::FieldOf,
-    types::{Field, Point, F64, U32},
+    pen::Pen,
+    types::{Field, Point, PulseWidth, F64, U32},
     FP,
 };
 use binrw::{binrw, BinRead, BinWrite};
@@ -12,6 +13,7 @@ use modular_bitfield::{
     bitfield,
     specifiers::{B1, B18},
 };
+use num_enum::TryFromPrimitive;
 use serde::{Deserialize, Serialize};
 
 use super::{line::Lines, Object, ObjectCore, Translate};
@@ -483,3 +485,26 @@ impl Translate for Hatch {
         self.core.move_relative(delta, z);
     }
 }
+
+impl Hatch {
+    /// Estimate the power density from the hatch's line spacing and pen speed/frequency/power/pulse
+    pub fn calc_power_density(&self, pens: &Vec<Pen>) -> u64 {
+        let pen = pens
+            .get(*self.core.pen as usize)
+            .expect("Invalid pen index");
+        let line_spacing: f64 = *self
+            .hatch_settings
+            .iter()
+            .find(|h| h.enabled.into())
+            .expect("Hatch object does not have enabled settings")
+            .line_spacing;
+        let power: f64 = *pen.power;
+        let speed: f64 = *pen.speed;
+        let frequency: f64 = f64::from(*pen.frequency);
+        let pulse: f64 = PulseWidth::try_from_primitive(*pen.pulse_width)
+            .expect("Invalid pen pulse width")
+            .power_ratio();
+
+        ((1.0 / line_spacing) * power * (1.0 / speed) * frequency * pulse) as u64
+    }
+}

+ 6 - 22
src/main.rs

@@ -9,14 +9,12 @@ use std::{
 use binrw::{BinRead, BinWrite, BinWriterExt};
 use clap::{error::ErrorKind, Args, Error, Parser, Subcommand};
 use clap_verbosity_flag::{InfoLevel, Verbosity};
-use config::{hatch, object};
 use dialoguer::Confirm;
 use diff::Diff;
 use env_logger::Target;
-use ezcad::{file::EzCadHeader, layer::Layer, objects::Object, pen::Pen, types::PulseWidth};
+use ezcad::{file::EzCadHeader, layer::Layer, objects::Object, pen::Pen};
 use itertools::Itertools;
 use log::{info, trace, warn};
-use num_enum::TryFromPrimitive;
 use num_format::{Locale, ToFormattedString};
 use regex::Regex;
 
@@ -257,25 +255,11 @@ fn main() {
 
                 // Calculate estimated power density if object is hatched
                 let hatch_power: Option<String> = match object {
-                    Object::Hatch(hatch) => {
-                        let line_spacing: f64 = *hatch
-                            .hatch_settings
-                            .iter()
-                            .find(|h| h.enabled.into())
-                            .expect("Hatch object does not have enabled settings")
-                            .line_spacing;
-                        let power: f64 = *pen.power;
-                        let speed: f64 = *pen.speed;
-                        let frequency: f64 = f64::from(*pen.frequency);
-                        let pulse: f64 = PulseWidth::try_from_primitive(*pen.pulse_width)
-                            .expect("Invalid pen pulse width")
-                            .power_ratio();
-
-                        let hatch_power: u64 =
-                            ((1.0 / line_spacing) * power * (1.0 / speed) * frequency * pulse)
-                                as u64;
-                        Some(hatch_power.to_formatted_string(&Locale::en))
-                    }
+                    Object::Hatch(hatch) => Some(
+                        hatch
+                            .calc_power_density(pens)
+                            .to_formatted_string(&Locale::en),
+                    ),
                     _ => None,
                 };