From 7597a8b66d4562ef1bb8c8f648b692202ebffc30 Mon Sep 17 00:00:00 2001
From: Alan Odom <clockmender@icloud.com>
Date: Tue, 28 Jan 2020 11:05:27 +0000
Subject: [PATCH] Add System Input Rounding

Applies system rounding to inputs as defined in Add-on Preferences.

Default is 5 places of decimal, values taken from UI, or by calculation are
rounded before command is submitted.
---
 pdt_command_functions.py               |  30 +++--
 precision_drawing_tools/__init__.py    |  12 +-
 precision_drawing_tools/pdt_command.py |  32 +++--
 precision_drawing_tools/pdt_design.py  | 180 ++++++++++++++++++-------
 4 files changed, 178 insertions(+), 76 deletions(-)

diff --git a/pdt_command_functions.py b/pdt_command_functions.py
index baf84c919..772f3fc0e 100644
--- a/pdt_command_functions.py
+++ b/pdt_command_functions.py
@@ -620,18 +620,25 @@ def set_angle_distance_two(context):
         v0 = np.array([vector_a[a1] + 1, vector_a[a2]]) - np.array([vector_a[a1], vector_a[a2]])
         v1 = np.array([vector_b[a1], vector_b[a2]]) - np.array([vector_a[a1], vector_a[a2]])
     ang = np.rad2deg(np.arctan2(np.linalg.det([v0, v1]), np.dot(v0, v1)))
+    val_round = context.preferences.addons[__package__].preferences.pdt_input_round
     if flip_a:
         if ang > 0:
-            pg.angle = ang - 180
+            pg.angle = round(ang - 180, val_round)
         else:
-            pg.angle = ang + 180
+            pg.angle = round(ang - 180, val_round)
     else:
-        pg.angle = ang
+        pg.angle = round(ang, val_round)
     if plane == "LO":
-        pg.distance = sqrt((vector_a.x - vector_b.x) ** 2 + (vector_a.y - vector_b.y) ** 2)
+        pg.distance = round(sqrt(
+            (vector_a.x - vector_b.x) ** 2 +
+            (vector_a.y - vector_b.y) ** 2), val_round
+        )
     else:
-        pg.distance = sqrt((vector_a[a1] - vector_b[a1]) ** 2 + (vector_a[a2] - vector_b[a2]) ** 2)
-    pg.cartesian_coords = vector_b - vector_a
+        pg.distance = round(sqrt(
+            (vector_a[a1] - vector_b[a1]) ** 2 +
+            (vector_a[a2] - vector_b[a2]) ** 2), val_round
+        )
+    pg.cartesian_coords = Vector(([round(i, val_round) for i in (vector_b - vector_a)]))
     return
 
 
@@ -692,15 +699,16 @@ def set_angle_distance_three(context):
     )
     angle_cosine = np.dot(ba, bc) / (np.linalg.norm(ba) * np.linalg.norm(bc))
     ang = np.degrees(np.arccos(angle_cosine))
+    val_round = context.preferences.addons[__package__].preferences.pdt_input_round
     if flip_a:
         if ang > 0:
-            pg.angle = ang - 180
+            pg.angle = round(ang - 180, val_round)
         else:
-            pg.angle = ang + 180
+            pg.angle = round(ang - 180, val_round)
     else:
-        pg.angle = ang
-    pg.distance = (vector_a - vector_b).length
-    pg.cartesian_coords = vector_b - vector_a
+        pg.angle = round(ang, val_round)
+    pg.distance = round((vector_a - vector_b).length, val_round)
+    pg.cartesian_coords = Vector(([round(i, val_round) for i in (vector_b - vector_a)]))
     return
 
 
diff --git a/precision_drawing_tools/__init__.py b/precision_drawing_tools/__init__.py
index 1d2bb3137..81ef97e44 100644
--- a/precision_drawing_tools/__init__.py
+++ b/precision_drawing_tools/__init__.py
@@ -448,15 +448,23 @@ class PDTPreferences(AddonPreferences):
         description="Cutoff width for shrinking items per line in menus"
     )
 
+    pdt_input_round : IntProperty(
+        name='Input Rounding',
+        default=5,
+        description='Rounding Factor for Inputs'
+    )
+
     def draw(self, context):
         layout = self.layout
 
         box = layout.box()
         row1 = box.row()
         row2 = box.row()
+        row3 = box.row()
         row1.prop(self, "debug")
-        row1.prop(self, "pdt_ui_width")
-        row2.prop(self, "pdt_library_path")
+        row2.prop(self, "pdt_ui_width")
+        row2.prop(self, "pdt_input_round")
+        row3.prop(self, "pdt_library_path")
 
 
 # List of All Classes in the Add-on to register
diff --git a/precision_drawing_tools/pdt_command.py b/precision_drawing_tools/pdt_command.py
index ee1b36914..d941dc2ae 100644
--- a/precision_drawing_tools/pdt_command.py
+++ b/precision_drawing_tools/pdt_command.py
@@ -376,21 +376,23 @@ def command_maths(context, mode, pg, expression, output_target):
         pg.error = PDT_ERR_BADMATHS
         context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
         raise PDT_MathsError
+
+    val_round = context.preferences.addons[__package__].preferences.pdt_input_round
     if output_target == "x":
-        pg.cartesian_coords.x = maths_result
+        pg.cartesian_coords.x = round(maths_result, val_round)
     elif output_target == "y":
-        pg.cartesian_coords.y = maths_result
+        pg.cartesian_coords.y = round(maths_result, val_round)
     elif output_target == "z":
-        pg.cartesian_coords.z = maths_result
+        pg.cartesian_coords.z = round(maths_result, val_round)
     elif output_target == "d":
-        pg.distance = maths_result
+        pg.distance = round(maths_result, val_round)
     elif output_target == "a":
-        pg.angle = maths_result
+        pg.angle = round(maths_result, val_round)
     elif output_target == "p":
-        pg.percent = maths_result
+        pg.percent = round(maths_result, val_round)
     else:
         # Mst be "o"
-        pg.maths_output = maths_result
+        pg.maths_output = round(maths_result, val_round)
     return
 
 
@@ -404,13 +406,17 @@ def command_parse(context):
     mode_s = pg.select
     obj = context.view_layer.objects.active
     ind = 0
-    for r in values:
+    for v in values:
         try:
-            _ = float(r)
+            _ = float(v)
             good = True
         except ValueError:
-            values[ind] = "0"
+            values[ind] = "0.0"
         ind = ind + 1
+    # Apply System Rounding
+    val_round = context.preferences.addons[__package__].preferences.pdt_input_round
+    values_out = [str(round(float(v), val_round)) for v in values]
+
     bm, good = obj_check(context, obj, scene, operation)
     if good:
         obj_loc = obj.matrix_world.decompose()[0]
@@ -432,10 +438,10 @@ def command_parse(context):
     else:
         verts = []
 
-    debug(f"command: {command}")
+    debug(f"command: {operation}{mode}{values_out}")
     debug(f"obj: {obj}, bm: {bm}, obj_loc: {obj_loc}")
 
-    return pg, values, obj, obj_loc, bm, verts
+    return pg, values_out, obj, obj_loc, bm, verts
 
 
 def move_cursor_pivot(context, pg, operation, mode, obj, verts, values):
@@ -845,7 +851,7 @@ def fillet_geometry(context, pg, mode, obj, bm, verts, values):
     # Note that passing an empty parameter results in that parameter being seen as "0"
     # _offset <= 0 is ignored since a bevel/fillet radius must be > 0 to make sense
     _offset = float(values[0])
-    _segments = int(values[1])
+    _segments = float(values[1])
     if _segments < 1:
         _segments = 1   # This is a single, flat segment (ignores profile)
     _profile = float(values[2])
diff --git a/precision_drawing_tools/pdt_design.py b/precision_drawing_tools/pdt_design.py
index 51f60a930..20cb6ac35 100644
--- a/precision_drawing_tools/pdt_design.py
+++ b/precision_drawing_tools/pdt_design.py
@@ -56,7 +56,7 @@ from .pdt_msg_strings import (
     PDT_LAB_DIR,
     PDT_LAB_INTERSECT,
     PDT_LAB_PERCENT,
-    PDT_LAB_PLANE
+    PDT_LAB_PLANE,
 )
 
 
@@ -90,31 +90,50 @@ class PDT_OT_PlacementAbs(Operator):
 
         pg = context.scene.pdt_pg
         operation = pg.operation
+        val_round = context.preferences.addons[__package__].preferences.pdt_input_round
 
         if operation == "CU":
             # Cursor
-            pg.command = (f"ca{pg.cartesian_coords.x},{pg.cartesian_coords.y},"\
-                f"{pg.cartesian_coords.z}")
+            pg.command = (
+                f"ca{str(round(pg.cartesian_coords.x, val_round))}"
+                f",{str(round(pg.cartesian_coords.y, val_round))}"
+                f",{str(round(pg.cartesian_coords.z, val_round))}"
+            )
         elif operation == "PP":
             # Pivot Point
-            pg.command = (f"pa{pg.cartesian_coords.x},{pg.cartesian_coords.y},"\
-                f"{pg.cartesian_coords.z}")
+            pg.command = (
+                f"pa{str(round(pg.cartesian_coords.x, val_round))}"
+                f",{str(round(pg.cartesian_coords.y, val_round))}"
+                f",{str(round(pg.cartesian_coords.z, val_round))}"
+            )
         elif operation == "MV":
             # Move Entities
-            pg.command = (f"ga{pg.cartesian_coords.x},{pg.cartesian_coords.y},"\
-                f"{pg.cartesian_coords.z}")
+            pg.command = (
+                f"ga{str(round(pg.cartesian_coords.x, val_round))}"
+                f",{str(round(pg.cartesian_coords.y, val_round))}"
+                f",{str(round(pg.cartesian_coords.z, val_round))}"
+            )
         elif operation == "SE":
             # Split Edges
-            pg.command = (f"sa{pg.cartesian_coords.x},{pg.cartesian_coords.y},"\
-                f"{pg.cartesian_coords.z}")
+            pg.command = (
+                f"sa{str(round(pg.cartesian_coords.x, val_round))}"
+                f",{str(round(pg.cartesian_coords.y, val_round))}"
+                f",{str(round(pg.cartesian_coords.z, val_round))}"
+            )
         elif operation == "NV":
             # New Vertex
-            pg.command = (f"na{pg.cartesian_coords.x},{pg.cartesian_coords.y},"\
-                f"{pg.cartesian_coords.z}")
+            pg.command = (
+                f"na{str(round(pg.cartesian_coords.x, val_round))}"
+                f",{str(round(pg.cartesian_coords.y, val_round))}"
+                f",{str(round(pg.cartesian_coords.z, val_round))}"
+            )
         elif operation == "EV":
             # Extrude Vertices
-            pg.command = (f"va{pg.cartesian_coords.x},{pg.cartesian_coords.y},"\
-                f"{pg.cartesian_coords.z}")
+            pg.command = (
+                f"va{str(round(pg.cartesian_coords.x, val_round))}"
+                f",{str(round(pg.cartesian_coords.y, val_round))}"
+                f",{str(round(pg.cartesian_coords.z, val_round))}"
+            )
         else:
             errmsg = f"{operation} {PDT_ERR_NON_VALID} {PDT_LAB_ABS}"
             self.report({"ERROR"}, errmsg)
@@ -153,39 +172,64 @@ class PDT_OT_PlacementDelta(Operator):
 
         pg = context.scene.pdt_pg
         operation = pg.operation
+        val_round = context.preferences.addons[__package__].preferences.pdt_input_round
 
         if operation == "CU":
             # Cursor
-            pg.command = (f"cd{pg.cartesian_coords.x},{pg.cartesian_coords.y},"\
-                f"{pg.cartesian_coords.z}")
+            pg.command = (
+                f"cd{str(round(pg.cartesian_coords.x, val_round))}"
+                f",{str(round(pg.cartesian_coords.y, val_round))}"
+                f",{str(round(pg.cartesian_coords.z, val_round))}"
+            )
         elif operation == "PP":
             # Pivot Point
-            pg.command = (f"pd{pg.cartesian_coords.x},{pg.cartesian_coords.y},"\
-                f"{pg.cartesian_coords.z}")
+            pg.command = (
+                f"pd{str(round(pg.cartesian_coords.x, val_round))}"
+                f",{str(round(pg.cartesian_coords.y, val_round))}"
+                f",{str(round(pg.cartesian_coords.z, val_round))}"
+            )
         elif operation == "MV":
             # Move Entities
-            pg.command = (f"gd{pg.cartesian_coords.x},{pg.cartesian_coords.y},"\
-                f"{pg.cartesian_coords.z}")
+            pg.command = (
+                f"gd{str(round(pg.cartesian_coords.x, val_round))}"
+                f",{str(round(pg.cartesian_coords.y, val_round))}"
+                f",{str(round(pg.cartesian_coords.z, val_round))}"
+            )
         elif operation == "SE":
             # Split Edges
-            pg.command = (f"sd{pg.cartesian_coords.x},{pg.cartesian_coords.y},"\
-                f"{pg.cartesian_coords.z}")
+            pg.command = (
+                f"sd{str(round(pg.cartesian_coords.x, val_round))}"
+                f",{str(round(pg.cartesian_coords.y, val_round))}"
+                f",{str(round(pg.cartesian_coords.z, val_round))}"
+            )
         elif operation == "NV":
             # New Vertex
-            pg.command = (f"nd{pg.cartesian_coords.x},{pg.cartesian_coords.y},"\
-                f"{pg.cartesian_coords.z}")
+            pg.command = (
+                f"nd{str(round(pg.cartesian_coords.x, val_round))}"
+                f",{str(round(pg.cartesian_coords.y, val_round))}"
+                f",{str(round(pg.cartesian_coords.z, val_round))}"
+            )
         elif operation == "EV":
             # Extrue Vertices
-            pg.command = (f"vd{pg.cartesian_coords.x},{pg.cartesian_coords.y},"\
-                f"{pg.cartesian_coords.z}")
+            pg.command = (
+                f"vd{str(round(pg.cartesian_coords.x, val_round))}"
+                f",{str(round(pg.cartesian_coords.y, val_round))}"
+                f",{str(round(pg.cartesian_coords.z, val_round))}"
+            )
         elif operation == "DG":
             # Duplicate Entities
-            pg.command = (f"dd{pg.cartesian_coords.x},{pg.cartesian_coords.y},"\
-                f"{pg.cartesian_coords.z}")
+            pg.command = (
+                f"dd{str(round(pg.cartesian_coords.x, val_round))}"
+                f",{str(round(pg.cartesian_coords.y, val_round))}"
+                f",{str(round(pg.cartesian_coords.z, val_round))}"
+            )
         elif operation == "EG":
             # Extrue Geometry
-            pg.command = (f"ed{pg.cartesian_coords.x},{pg.cartesian_coords.y},"\
-                f"{pg.cartesian_coords.z}")
+            pg.command = (
+                f"ed{str(round(pg.cartesian_coords.x, val_round))}"
+                f",{str(round(pg.cartesian_coords.y, val_round))}"
+                f",{str(round(pg.cartesian_coords.z, val_round))}"
+            )
         else:
             errmsg = f"{operation} {PDT_ERR_NON_VALID} {PDT_LAB_DEL}"
             self.report({"ERROR"}, errmsg)
@@ -224,30 +268,56 @@ class PDT_OT_PlacementDis(Operator):
 
         pg = context.scene.pdt_pg
         operation = pg.operation
+        val_round = context.preferences.addons[__package__].preferences.pdt_input_round
+
         if operation == "CU":
             # Cursor
-            pg.command = f"ci{pg.distance},{pg.angle}"
+            pg.command = (
+                f"ci{str(round(pg.distance, val_round))}"
+                f",{str(round(pg.angle, val_round))}"
+            )
         elif operation == "PP":
             # Pivot Point
-            pg.command = f"pi{pg.distance},{pg.angle}"
+            pg.command = (
+                f"pi{str(round(pg.distance, val_round))}"
+                f",{str(round(pg.angle, val_round))}"
+            )
         elif operation == "MV":
             # Move Entities
-            pg.command = f"gi{pg.distance},{pg.angle}"
+            pg.command = (
+                f"gi{str(round(pg.distance, val_round))}"
+                f",{str(round(pg.angle, val_round))}"
+            )
         elif operation == "SE":
             # Split Edges
-            pg.command = f"si{pg.distance},{pg.angle}"
+            pg.command = (
+                f"si{str(round(pg.distance, val_round))}"
+                f",{str(round(pg.angle, val_round))}"
+                )
         elif operation == "NV":
             # New Vertex
-            pg.command = f"ni{pg.distance},{pg.angle}"
+            pg.command = (
+                f"ni{str(round(pg.distance, val_round))}"
+                f",{str(round(pg.angle, val_round))}"
+            )
         elif operation == "EV":
             # Extrude Vertices
-            pg.command = f"vi{pg.distance},{pg.angle}"
+            pg.command = (
+                f"vi{str(round(pg.distance, val_round))}"
+                f",{str(round(pg.angle, val_round))}"
+            )
         elif operation == "DG":
             # Duplicate Geometry
-            pg.command = f"di{pg.distance},{pg.angle}"
+            pg.command = (
+                f"di{str(round(pg.distance, val_round))}"
+                f",{str(round(pg.angle, val_round))}"
+            )
         elif operation == "EG":
             # Extrude Geometry
-            pg.command = f"ei{pg.distance},{pg.angle}"
+            pg.command = (
+                f"ei{str(round(pg.distance, val_round))}"
+                f",{str(round(pg.angle, val_round))}"
+            )
         else:
             errmsg = f"{operation} {PDT_ERR_NON_VALID} {PDT_LAB_DIR}"
             self.report({"ERROR"}, errmsg)
@@ -261,7 +331,6 @@ class PDT_OT_PlacementPer(Operator):
     bl_label = "Percentage Mode"
     bl_options = {"REGISTER", "UNDO"}
 
-
     def execute(self, context):
         """Manipulates Geometry, or Objects by Percentage between 2 points.
 
@@ -285,25 +354,26 @@ class PDT_OT_PlacementPer(Operator):
 
         pg = context.scene.pdt_pg
         operation = pg.operation
+        val_round = context.preferences.addons[__package__].preferences.pdt_input_round
 
         if operation == "CU":
             # Cursor
-            pg.command = f"cp{pg.percent}"
+            pg.command = f"cp{str(round(pg.percent, val_round))}"
         elif operation == "PP":
             # Pivot Point
-            pg.command = f"pp{pg.percent}"
+            pg.command = f"pp{str(round(pg.percent, val_round))}"
         elif operation == "MV":
             # Move Entities
-            pg.command = f"gp{pg.percent}"
+            pg.command = f"gp{str(round(pg.percent, val_round))}"
         elif operation == "SE":
             # Split Edges
-            pg.command = f"sp{pg.percent}"
+            pg.command = f"sp{str(round(pg.percent, val_round))}"
         elif operation == "NV":
             # New Vertex
-            pg.command = f"np{pg.percent}"
+            pg.command = f"np{str(round(pg.percent, val_round))}"
         elif operation == "EV":
             # Extrude Vertices
-            pg.command = f"vp{pg.percent}"
+            pg.command = f"vp{str(round(pg.percent, val_round))}"
         else:
             errmsg = f"{operation} {PDT_ERR_NON_VALID} {PDT_LAB_PERCENT}"
             self.report({"ERROR"}, errmsg)
@@ -317,7 +387,6 @@ class PDT_OT_PlacementNormal(Operator):
     bl_label = "Normal Mode"
     bl_options = {"REGISTER", "UNDO"}
 
-
     def execute(self, context):
         """Manipulates Geometry, or Objects by Normal Intersection between 3 points.
 
@@ -515,12 +584,25 @@ class PDT_OT_Fillet(Operator):
         """
 
         pg = context.scene.pdt_pg
+        val_round = context.preferences.addons[__package__].preferences.pdt_input_round
         if pg.fillet_intersect:
-            pg.command = f"fi{pg.fillet_radius},{pg.fillet_segments},{pg.fillet_profile}"
+            pg.command = (
+            f"fi{str(round(pg.fillet_radius, val_round))}"
+            f",{str(round(pg.fillet_segments, val_round))}"
+            f",{str(round(pg.fillet_profile, val_round))}"
+            )
         elif pg.fillet_vertices_only:
-            pg.command = f"fv{pg.fillet_radius},{pg.fillet_segments},{pg.fillet_profile}"
+            pg.command = (
+            f"fv{str(round(pg.fillet_radius, val_round))}"
+            f",{str(round(pg.fillet_segments, val_round))}"
+            f",{str(round(pg.fillet_profile, val_round))}"
+            )
         else:
-            pg.command = f"fe{pg.fillet_radius},{pg.fillet_segments},{pg.fillet_profile}"
+            pg.command = (
+            f"fe{str(round(pg.fillet_radius, val_round))}"
+            f",{str(round(pg.fillet_segments, val_round))}"
+            f",{str(round(pg.fillet_profile, val_round))}"
+            )
         return {"FINISHED"}
 
 
@@ -609,7 +691,6 @@ class PDT_OT_Taper(Operator):
     bl_label = "Taper"
     bl_options = {"REGISTER", "UNDO"}
 
-
     @classmethod
     def poll(cls, context):
         ob = context.object
@@ -617,7 +698,6 @@ class PDT_OT_Taper(Operator):
             return False
         return all([bool(ob), ob.type == "MESH", ob.mode == "EDIT"])
 
-
     def execute(self, context):
         """Taper Geometry along World Axes.
 
-- 
GitLab