import os import tempfile 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: if 'AssemblyHandbook_RenderLines' in obj.PropertiesList and not obj.AssemblyHandbook_RenderLines: obj.ViewObject.LineColor = background_color else: obj.ViewObject.LineColor = line_color obj.ViewObject.DisplayMode = 'Flat Lines' obj.ViewObject.ShapeMaterial.AmbientColor = (0.0, 0.0, 0.0, 0.0) obj.ViewObject.ShapeMaterial.EmissiveColor = background_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 = (2048, 2048) 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_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") from PIL import Image, ImageFilter #import time lines = Image.open(temp_lines_file_name) lines = lines.convert("L") shapes = Image.open(temp_shapes_file_name) #startTime = time.perf_counter() 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") lines.save(filename) from ahb_command import AHB_CommandWrapper AHB_CommandWrapper.addGuiCommand('AHB_render', AHB_Render())