From 5ec252777dc8cb6906d3500358b7d79c34b6dc81 Mon Sep 17 00:00:00 2001 From: Youen Toupin Date: Tue, 21 Dec 2021 11:04:48 +0100 Subject: [PATCH] improved outline rendering --- ahb_cmd_render.py | 63 ++++++++++++++++++++++++++++++++++------------- ahb_context.py | 6 +++-- 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/ahb_cmd_render.py b/ahb_cmd_render.py index 9f9001f..ba07a1f 100644 --- a/ahb_cmd_render.py +++ b/ahb_cmd_render.py @@ -52,41 +52,70 @@ class AHB_Render: if b >= 256 - step: b = step + def reset_display(self): + doc = App.activeDocument() + for obj in doc.Objects: + if obj.TypeId == 'Part::Feature': + if 'AssemblyHandbook_Stage' in obj.PropertiesList: + obj.ViewObject.LineColor = (0.0, 0.0, 0.0, 0.0) + obj.ViewObject.DisplayMode = 'Flat Lines' + obj.ViewObject.ShapeMaterial.AmbientColor = (0.3, 0.3, 0.3, 0.0) + obj.ViewObject.ShapeMaterial.DiffuseColor = (1.0, 1.0, 1.0, 0.0) + obj.ViewObject.ShapeMaterial.SpecularColor = (0.5, 0.5, 0.5, 0.0) + obj.ViewObject.ShapeMaterial.EmissiveColor = (0.0, 0.0, 0.0, 0.0) + + workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") + workbench.context.onPartStageChanged(None) + def Activated(self): - resolution = (2048, 2048) + resolution = (2000, 2000) filename = "/home/youen/dev_linux/vhelio-render/vhelio.png" temp_lines_file_name = tempfile.gettempdir() + "/ahb_temp_lines.png" temp_shapes_file_name = tempfile.gettempdir() + "/ahb_temp_shapes.png" + self.set_render_outlines() + Gui.ActiveDocument.ActiveView.saveImage(temp_shapes_file_name, resolution[0] * 2, resolution[1] * 2, "#ffffff") + self.set_render_lines() Gui.ActiveDocument.ActiveView.saveImage(temp_lines_file_name, resolution[0], resolution[1], "#ffffff") - self.set_render_outlines() - Gui.ActiveDocument.ActiveView.saveImage(temp_shapes_file_name, resolution[0]*2, resolution[1]*2, "#ffffff") + self.reset_display() from PIL import Image, ImageFilter - #import time + import time lines = Image.open(temp_lines_file_name) lines = lines.convert("L") shapes = Image.open(temp_shapes_file_name) - #startTime = time.perf_counter() + startTime = time.perf_counter() + + outlines = None + for x in range(0, 3): + for y in range(0, 3): + if x == 1 and y == 1: continue + kernel = [0, 0, 0, 0, 1, 0, 0, 0, 0] + kernel[y*3 + x] = -1 + partial_outlines = shapes.filter(ImageFilter.Kernel((3, 3), kernel, 1, 127)) + partial_outlines = partial_outlines.point(lambda p: 255 if p == 127 else 0) + partial_outlines = partial_outlines.convert("L") + partial_outlines = partial_outlines.point(lambda p: 255 if p == 255 else 0) + if outlines is None: + outlines = partial_outlines + else: + outlines.paste(partial_outlines, None, partial_outlines.point(lambda p: 255 - p)) - outlines = shapes.filter(ImageFilter.Kernel((3, 3), (-1, -1, -1, -1, 8, -1, -1, -1, -1), 1, 0)) - outlines2 = shapes.filter(ImageFilter.Kernel((3, 3), (1, 1, 1, 1, -8, 1, 1, 1, 1), 1, 0)) - outlines = outlines.convert("L") - outlines2 = outlines2.convert("L") - outlines = outlines.point(lambda p: 0 if p > 0 else 255) - outlines2 = outlines2.point(lambda p: 0 if p > 0 else 255) - outlines.paste(outlines2, None, outlines2.point(lambda p: 255 - p)) #outlines.save("/home/youen/dev_linux/vhelio-render/vhelio-outlines.png") - lines = lines.resize(outlines.size, Image.NEAREST) - lines.paste(outlines, None, outlines.point(lambda p: 255 - p)) - lines = lines.resize(lines.size, Image.BILINEAR) - #endTime = time.perf_counter() - #print("Outline detection: " + str(round((endTime - startTime)*1000)/1000) + "s") + #outlines = outlines.resize(lines.size, Image.BILINEAR) + #lines.paste(outlines, None, outlines.point(lambda p: 255 - p)) + + lines_fullres = lines.resize(outlines.size, Image.NEAREST) + lines_fullres.paste(outlines, None, outlines.point(lambda p: 255 - p)) + lines = lines_fullres.resize(lines.size, Image.BILINEAR) + + endTime = time.perf_counter() + print("Outline detection: " + str(round((endTime - startTime)*1000)/1000) + "s") lines.save(filename) from ahb_command import AHB_CommandWrapper diff --git a/ahb_context.py b/ahb_context.py index db30415..70d3493 100644 --- a/ahb_context.py +++ b/ahb_context.py @@ -1,3 +1,5 @@ +from typing import Optional + import FreeCADGui as Gui import FreeCAD as App from PySide.QtCore import QTimer @@ -45,12 +47,12 @@ class AHB_Context: allStages.sort() return allStages - def onPartStageChanged(self, part: App.DocumentObject): + def onPartStageChanged(self, part: Optional[App.DocumentObject]): allStages = self.getAllStages() doc = App.ActiveDocument - if not 'AssemblyHandbook_Stage' in part.PropertiesList: + if part is not None and not 'AssemblyHandbook_Stage' in part.PropertiesList: part.ViewObject.ShapeColor = (0.8,0.8,0.8,0.0) for obj in doc.Objects: