From 7719650a85b70193fca97a31c8e3ba12e71071b7 Mon Sep 17 00:00:00 2001 From: Youen Toupin Date: Sun, 19 Dec 2021 23:53:22 +0100 Subject: [PATCH] Added code to render the 3D view to an image, with shape outlines --- InitGui.py | 3 ++ ahb_cmd_render.py | 89 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 ahb_cmd_render.py diff --git a/InitGui.py b/InitGui.py index e762958..c7dd394 100644 --- a/InitGui.py +++ b/InitGui.py @@ -62,6 +62,9 @@ class AssemblyHandbookWorkbench(Gui.Workbench): self.importModule('ahb_cmd_switch_visibility_mode') toolbox.append("AHB_switchVisibilityMode") + self.importModule('ahb_cmd_render') + toolbox.append("AHB_render") + self.importModule('ahb_cmd_animate') toolbox.append("AHB_animate") diff --git a/ahb_cmd_render.py b/ahb_cmd_render.py new file mode 100644 index 0000000..081a498 --- /dev/null +++ b/ahb_cmd_render.py @@ -0,0 +1,89 @@ +import os + +import FreeCADGui as Gui +import FreeCAD as App + +class AHB_Render: + def GetResources(self): + return {"MenuText": "Render", + "ToolTip": "Render to images", + "Pixmap": "" + } + + def IsActive(self): + return True + + def set_render_lines(self, line_color = (0.0,0.0,0.0,0.0), background_color = (1.0,1.0,1.0,0.0)): + doc = App.activeDocument() + for obj in doc.Objects: + if obj.TypeId == 'Part::Feature': + if 'AssemblyHandbook_Stage' in obj.PropertiesList: + obj.ViewObject.DisplayMode = 'Flat Lines' + obj.ViewObject.ShapeMaterial.AmbientColor = (0.0, 0.0, 0.0, 0.0) + obj.ViewObject.ShapeMaterial.EmissiveColor = background_color + obj.ViewObject.LineColor = line_color + + def set_render_outlines(self): + doc = App.activeDocument() + step = 8 + r = step + g = step + b = step + for obj in doc.Objects: + if obj.TypeId == 'Part::Feature': + if 'AssemblyHandbook_Stage' in obj.PropertiesList: + obj.ViewObject.DisplayMode = 'Shaded' + obj.ViewObject.ShapeMaterial.AmbientColor = (0.0, 0.0, 0.0, 0.0) + obj.ViewObject.ShapeMaterial.DiffuseColor = (0.0, 0.0, 0.0, 0.0) + obj.ViewObject.ShapeMaterial.SpecularColor = (0.0, 0.0, 0.0, 0.0) + obj.ViewObject.ShapeMaterial.EmissiveColor = (r/255.0, g/255.0, b/255.0, 0.0) + + r = r + step + if r >= 256 - step: + r = step + g = g + step + if g >= 256 - step: + g = step + b = b + step + if b >= 256 - step: + b = step + + def Activated(self): + resolution = (512, 512) + + self.set_render_lines() + Gui.ActiveDocument.ActiveView.saveImage("/home/youen/dev_linux/vhelio-render/vhelio.png", resolution[0], resolution[1], "#ffffff") + + self.set_render_outlines() + Gui.ActiveDocument.ActiveView.saveImage("/home/youen/dev_linux/vhelio-render/vhelio-shapes.png", resolution[0]*2, resolution[1]*2, "#ffffff") + + from PIL import Image + import time + lines = Image.open("/home/youen/dev_linux/vhelio-render/vhelio.png") + lines = lines.convert("L") + shapes = Image.open("/home/youen/dev_linux/vhelio-render/vhelio-shapes.png") + startTime = time.perf_counter() + for xb in range(1, lines.size[0]-1): + for yb in range(1, lines.size[1]-1): + outline_color = 1.0 + for x in range(xb*2, xb*2+2): + for y in range(yb*2, yb*2+2): + c = shapes.getpixel((x, y)) + border = False + if c != shapes.getpixel((x - 1, y - 1)): border = True + if c != shapes.getpixel((x, y - 1)): border = True + if c != shapes.getpixel((x + 1, y - 1)): border = True + if c != shapes.getpixel((x - 1, y)): border = True + if c != shapes.getpixel((x, y)): border = True + if c != shapes.getpixel((x + 1, y)): border = True + if c != shapes.getpixel((x - 1, y + 1)): border = True + if c != shapes.getpixel((x, y + 1)): border = True + if c != shapes.getpixel((x + 1, y + 1)): border = True + if border: outline_color = outline_color - 0.25 + lines.putpixel((xb, yb), min(lines.getpixel((xb, yb)), int(outline_color * 255.0))) + endTime = time.perf_counter() + print("Outline detection: " + str(round((endTime - startTime)*1000)/1000) + "s") + lines.save("/home/youen/dev_linux/vhelio-render/vhelio-final.png") + +from ahb_command import AHB_CommandWrapper +AHB_CommandWrapper.addGuiCommand('AHB_render', AHB_Render())