FreeCAD workbench to create assembly handbooks
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
4.7 KiB

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:
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 = (1024, 1024)
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")
cacheWidth = 8
cacheHalfWidth = int(cacheWidth/2)
cache = []
for y in range(0, 4):
cacheLine = []
for x in range(0, cacheWidth + 2):
cacheLine.append((0.0,0.0,0.0))
cache.append(cacheLine)
startTime = time.perf_counter()
for xb in range(1, lines.size[0]-cacheHalfWidth, cacheHalfWidth):
for yb in range(1, lines.size[1]-1):
for y in range(0, 4):
cacheLine = cache[y]
for x in range(0, cacheWidth + 2):
cacheLine[x] = shapes.getpixel((xb*2+x-1, yb*2+y-1))
for xa in range(0, cacheHalfWidth):
outline_color = 1.0
for xaa in range(0, 2):
x = xa*2 + xaa
for y in range(0, 2):
c = cache[y+1][x+1]
border = False
if c != cache[y][x]: border = True
if c != cache[y][x+1]: border = True
if c != cache[y][x+2]: border = True
if c != cache[y+1][x]: border = True
if c != cache[y+1][x+2]: border = True
if c != cache[y+2][x]: border = True
if c != cache[y+2][x+1]: border = True
if c != cache[y+2][x+2]: border = True
if border: outline_color = outline_color - 0.25
lines.putpixel((xb+xa, yb), min(lines.getpixel((xb+xa, 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())