Browse Source

Added code to handle clipping view

master
Youen 2 years ago
parent
commit
c06af9db24
  1. 44
      ahb_raster_view.py

44
ahb_raster_view.py

@ -129,13 +129,25 @@ class RasterView:
dir = os.path.dirname(self.image_file_name) dir = os.path.dirname(self.image_file_name)
if not os.path.exists(dir): if not os.path.exists(dir):
os.makedirs(dir) os.makedirs(dir)
tmp_doc = App.newDocument('tmp_raster', hidden=False, temp=False) if 'Assembly_handbook_RasterSavedView' in view.PropertiesList and view.Assembly_handbook_RasterSavedView is not None:
tmp_doc = view.Assembly_handbook_RasterSavedView.Document
close_tmp_doc = False
else:
tmp_doc = App.newDocument('tmp_raster', hidden=False, temp=False)
close_tmp_doc = True
objects_to_reset = {} objects_to_reset = {}
duplicated_parts = {} duplicated_parts = {}
try: try:
print_verbose("Preparing scene...") print_verbose("Preparing scene...")
# Clean existing scene (if any)
sceneGroup = tmp_doc.getObject('Scene')
if sceneGroup is not None:
sceneGroup.removeObjectsFromDocument()
tmp_doc.removeObject(sceneGroup.Name)
# construct new scene with links to the parts we want # construct new scene with links to the parts we want
sceneGroup = tmp_doc.addObject('App::DocumentObjectGroup', 'Scene') sceneGroup = tmp_doc.addObject('App::DocumentObjectGroup', 'Scene')
prev_parts = [] prev_parts = []
@ -315,7 +327,8 @@ class RasterView:
obj.ViewObject.DisplayMode = props[7] obj.ViewObject.DisplayMode = props[7]
# remove the temporary document # remove the temporary document
App.closeDocument(tmp_doc.Name) if close_tmp_doc:
App.closeDocument(tmp_doc.Name)
print_verbose("Finalizing view...") print_verbose("Finalizing view...")
@ -375,8 +388,9 @@ class RasterView:
doc_view = Gui.getDocument(doc.Name).mdiViewsOfType('Gui::View3DInventor')[0] doc_view = Gui.getDocument(doc.Name).mdiViewsOfType('Gui::View3DInventor')[0]
# render lines in black, background in red, fill shapes in green # render lines in blue, background in red, fill shapes in green
# the green band contains the lines images, the red band contains the inverted alpha layer # the green band contains the lines image, the red band contains the inverted alpha layer
# if there is a clipping plane set with "Fill clip plane", the blue band contains the intersection with the clip plane
configured = [] configured = []
print_verbose('Preparing objects for line rendering...') print_verbose('Preparing objects for line rendering...')
for link in doc.findObjects(): for link in doc.findObjects():
@ -389,11 +403,11 @@ class RasterView:
configured.append(obj) configured.append(obj)
if self._should_render(obj) and not fast_render: if self._should_render(obj) and not fast_render:
obj.ViewObject.LineColor = (0.0, 0.0, 0.0, 0.0) if link in parts else (1.0, 0.0, 1.0) obj.ViewObject.LineColor = (0.0, 0.0, 1.0, 0.0) if link in parts else (1.0, 0.0, 1.0)
obj.ViewObject.ShapeMaterial.AmbientColor = (0.0, 0.0, 0.0, 0.0) 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.DiffuseColor = (0.0, 0.0, 0.0, 0.0)
obj.ViewObject.ShapeMaterial.SpecularColor = (0.0, 0.0, 0.0, 0.0) obj.ViewObject.ShapeMaterial.SpecularColor = (0.0, 0.0, 0.0, 0.0)
obj.ViewObject.ShapeMaterial.EmissiveColor = (0.0, 1.0, 0.0, 0.0) if link in parts else (1.0, 0.0, 1.0) obj.ViewObject.ShapeMaterial.EmissiveColor = (0.0, 1.0, 1.0, 0.0) if link in parts else (1.0, 0.0, 1.0)
# We need to set two different values otherwise freecad does not always update LineWidth of sub-elements # We need to set two different values otherwise freecad does not always update LineWidth of sub-elements
obj.ViewObject.LineWidth = 1.0 obj.ViewObject.LineWidth = 1.0
@ -402,15 +416,18 @@ class RasterView:
link.ViewObject.Visibility = False link.ViewObject.Visibility = False
print_verbose('Rendering lines...') print_verbose('Rendering lines...')
temp_file_name = tempfile.gettempdir() + "/ahb_temp_image.png" #temp_file_name = tempfile.gettempdir() + "/ahb_temp_image.png"
doc_view.saveImage(temp_file_name, resolution[0]+2, resolution[1]+2, "#ff0000") # we add 1 pixel border that we will need to crop later temp_file_name = "/home/youen/tmp/ahb_temp_image.png"
doc_view.saveImage(temp_file_name, resolution[0]+2, resolution[1]+2, "#ff00ff") # we add 1 pixel border that we will need to crop later
lines_bands_img = self._read_image(temp_file_name) lines_bands_img = self._read_image(temp_file_name)
lines_bands = lines_bands_img.split() lines_bands = lines_bands_img.split()
lines_img = lines_bands[1] lines_img = lines_bands[1]
alpha_img = lines_bands[0].point(lambda p: 255 - p) alpha_img = lines_bands[0].point(lambda p: 255 - p)
clip_img = lines_bands[2]
generate_outlines = not fast_render generate_outlines = not fast_render
#generate_outlines = False
if generate_outlines: if generate_outlines:
# Render all shapes with different colors, in order to extract outlines (where color changes) # Render all shapes with different colors, in order to extract outlines (where color changes)
# This is needed because FreeCAD does not render lines on the boundary of curve shapes, such as spheres or cylinders # This is needed because FreeCAD does not render lines on the boundary of curve shapes, such as spheres or cylinders
@ -491,6 +508,15 @@ class RasterView:
alpha_img alpha_img
]) ])
# set clip color
clip_color = (0.5, 0.5, 0.5)
colorized_clip_img = Image.merge("RGB", [
clip_img.point(lambda p: int(clip_color[0] * (255.0 - p))),
clip_img.point(lambda p: int(clip_color[1] * (255.0 - p))),
clip_img.point(lambda p: int(clip_color[2] * (255.0 - p)))
])
result.paste(colorized_clip_img, clip_img.point(lambda p: 255 - p))
# crop 1px borders # crop 1px borders
result = result.crop((1, 1, result.size[0] - 1, result.size[1] - 1)) result = result.crop((1, 1, result.size[0] - 1, result.size[1] - 1))

Loading…
Cancel
Save