From e90fa33277061dc9eee40e43754674ade8486325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9=20Rios?= Date: Mon, 6 Nov 2023 12:17:12 +0100 Subject: [PATCH] More formatting + fixed JSON creation --- dxf_viewer/app.py | 76 +++++++++++++++++++++++++++++++++++++- dxf_viewer/divers/tools.py | 67 +++++++++++++++++++-------------- 2 files changed, 114 insertions(+), 29 deletions(-) diff --git a/dxf_viewer/app.py b/dxf_viewer/app.py index f32d557..48266d4 100644 --- a/dxf_viewer/app.py +++ b/dxf_viewer/app.py @@ -1,7 +1,10 @@ # Imports +from logging import warning +from random import choice import tkinter as tk from tkinter import filedialog import os +from cv2 import ChiHistogramCostExtractor import ezdxf import sys import numpy as np @@ -14,6 +17,9 @@ from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk from matplotlib.patches import Polygon from matplotlib.patches import Rectangle +from tkinter import simpledialog + +from sympy import true matplotlib.use("TkAgg") from tkinter.filedialog import asksaveasfile @@ -36,7 +42,7 @@ def __init__(self): self.x_ratio = self.initial_width / 1280 self.y_ratio = self.initial_height / 720 self.cur_file = "" - self.protocol("WM_DELETE_WINDOW", lambda: self.on_closing_window()) + self.protocol("WM_DELETE_WINDOW", self.on_closing_window) self.grid_rowconfigure(0, weight=1) self.grid_columnconfigure(0, weight=1) @@ -478,9 +484,75 @@ def export_to_json(self): return extensions = [("Json file", "*.json")] default_name = get_file_name(self.cur_file) # vire le path et l'extension + + ROOT = tk.Toplevel() + # ROOT.wait_visibility() + ROOT.withdraw() + ROOT.grab_set_global() + + def mychoicebox(choicelist, title, prompt): + global result + + def buttonfn(): + global result + result = var.get() + choicewin.destroy() + choicewin.quit() + + def wm_delete_window(): + print("test") + ROOT.destroy() + choicewin.destroy() + choicewin.quit() + + choicewin = tk.Toplevel(master=ROOT) + choicewin.protocol("WM_DELETE_WINDOW", wm_delete_window) + choicewin.title(title) + tk.Label(choicewin, text=prompt).grid(row=0, column=0, sticky="W") + + var = tk.StringVar(choicewin) + var.set(choicelist[0]) # default option + popupMenu = tk.OptionMenu(choicewin, var, *choicelist) + popupMenu.grid(sticky=tk.N + tk.S + tk.E + tk.W, row=1, column=0) + + tk.Button(choicewin, text="Done", command=buttonfn).grid(row=2, column=0) + choicewin.mainloop() + return result + + category = mychoicebox(["room", "building"], "Category", "Select a category :") + name = simpledialog.askstring(title="Name", prompt=f"Input the {category}'s name :", parent=ROOT) + if name is None: + return + while len(name) == 0: + self.warning("The name can not be empty") + ROOT.deiconify() + ROOT.wait_visibility() + ROOT.withdraw() + name = simpledialog.askstring(title="Name", prompt=f"Input the {category}'s name :", parent=ROOT) + if name is None: + return + + ROOT.deiconify() + ROOT.wait_visibility() + ROOT.tk.eval(f"tk::PlaceWindow {ROOT._w} center") + ROOT.withdraw() + height = simpledialog.askfloat(title="Height", prompt=f"Input the height of the {category} (m)", parent=ROOT, initialvalue=3, minvalue=0) + + if height is None: + return + + preJSON = {"slug": name, "category": category} + ROOT.grab_set_global() + if category == "room": + preJSON["axisOrientation"] = mychoicebox( + ["+x+y", "+x-y", "-x+y", "-x-y"], "Axis orientation", "Select an orientation for the axis of the room" + ) + preJSON["floorUnit"] = mychoicebox(["m", "f", "t"], "Floor unit", "Select the measurement unit for the floor of the room") + + ROOT.destroy() file = asksaveasfile(filetypes=extensions, defaultextension=extensions, initialfile=f"{default_name}") if file: - file.write(generate_json(self.selection)) + file.write(generate_json(preJSON, self.selection, height)) file.close() # Bouton séléction de points diff --git a/dxf_viewer/divers/tools.py b/dxf_viewer/divers/tools.py index e76214e..762826d 100644 --- a/dxf_viewer/divers/tools.py +++ b/dxf_viewer/divers/tools.py @@ -2,12 +2,15 @@ import time import tkinter as tk import json +from shapely.geometry import Polygon + def transform(vertices, new_center, axis1, axis2): mat = np.linalg.inv(np.array([axis1, axis2])) @ np.array([[1, 0], [0, 1]]) transformed_vertices = (vertices - new_center) @ mat return transformed_vertices + def transform_single_axis(vertices, new_center, axis1, direct): if direct: axis2 = np.array([-axis1[1], axis1[0]]) @@ -34,9 +37,9 @@ def zoom(event): cur_ylim = ax.get_ylim() xdata = event.xdata ydata = event.ydata - if event.button == 'up': + if event.button == "up": scale_factor = 1 / base_scale - elif event.button == 'down': + elif event.button == "down": scale_factor = base_scale else: scale_factor = 1 @@ -48,7 +51,7 @@ def zoom(event): ax.set_xlim([xdata - new_width * (1 - relx), xdata + new_width * relx]) ax.set_ylim([ydata - new_height * (1 - rely), ydata + new_height * rely]) ax.figure.canvas.draw() - except : + except: pass def pan(event): @@ -65,16 +68,15 @@ def pan(event): def release(event): if event.button == 2: self.press = None - + fig = ax.get_figure() - fig.canvas.mpl_connect('scroll_event', zoom) - fig.canvas.mpl_connect('button_press_event', pan) - fig.canvas.mpl_connect('button_release_event', release) + fig.canvas.mpl_connect("scroll_event", zoom) + fig.canvas.mpl_connect("button_press_event", pan) + fig.canvas.mpl_connect("button_release_event", release) return zoom, pan, release - def cap_frequency(max_calls_per_second): min_interval = 1.0 / max_calls_per_second last_call_time = 0.0 @@ -88,12 +90,13 @@ def wrapper(*args, **kwargs): result = func(*args, **kwargs) last_call_time = current_time return result + return wrapper + return decorator class ToolTip(object): - def __init__(self, widget): self.widget = widget self.tipwindow = None @@ -111,9 +114,7 @@ def showtip(self, text): self.tipwindow = tw = tk.Toplevel(self.widget) tw.wm_overrideredirect(1) tw.wm_geometry("+%d+%d" % (x, y)) - label = tk.Label(tw, text=self.text, justify=tk.LEFT, - background="#ffffe0", relief=tk.SOLID, borderwidth=1, - font=("tahoma", "8", "normal")) + label = tk.Label(tw, text=self.text, justify=tk.LEFT, background="#ffffe0", relief=tk.SOLID, borderwidth=1, font=("tahoma", "8", "normal")) label.pack(ipadx=1) def hidetip(self): @@ -122,33 +123,45 @@ def hidetip(self): if tw: tw.destroy() + def CreateToolTip(widget, text): toolTip = ToolTip(widget) + def enter(event): toolTip.showtip(text) + def leave(event): toolTip.hidetip() - widget.bind('', enter) - widget.bind('', leave) -def generate_json(points): - json_response = {"points":[]} + widget.bind("", enter) + widget.bind("", leave) + + +def generate_json(preJSON, points, height): + json_response = preJSON + json_response["vertices"] = [] for i in points: - json_response["points"].append(list(i)) - return json.dumps(json_response) + json_response["vertices"].append([round(x,2) for x in i]) + poly = Polygon(points) + json_response["center"] = [round(poly.centroid.x, 2), round(poly.centroid.y, 2)] + json_response["sizeWDHm"] = [round(poly.bounds[2] - poly.bounds[0],2), round(poly.bounds[3] - poly.bounds[1],2), height] + return json.dumps(json_response, indent=4) + def get_file_name(file_path): - file_path_components = file_path.split('/') - file_name_and_extension = file_path_components[-1].rsplit('.', 1) + file_path_components = file_path.split("/") + file_name_and_extension = file_path_components[-1].rsplit(".", 1) return file_name_and_extension[0] + class DragDropListbox(tk.Listbox): - """ A Tkinter listbox with drag'n'drop reordering of entries. """ + """A Tkinter listbox with drag'n'drop reordering of entries.""" + def __init__(self, master, **kw): - kw['selectmode'] = tk.SINGLE + kw["selectmode"] = tk.SINGLE tk.Listbox.__init__(self, master, kw) - self.bind('', self.setCurrent) - self.bind('', self.shiftSelection) + self.bind("", self.setCurrent) + self.bind("", self.shiftSelection) self.curIndex = None def setCurrent(self, event): @@ -159,10 +172,10 @@ def shiftSelection(self, event): if i < self.curIndex: x = self.get(i) self.delete(i) - self.insert(i+1, x) + self.insert(i + 1, x) self.curIndex = i elif i > self.curIndex: x = self.get(i) self.delete(i) - self.insert(i-1, x) - self.curIndex = i \ No newline at end of file + self.insert(i - 1, x) + self.curIndex = i