Browse Source

Added button for faster rendering and fixed problem with overlay not recomputing

dev/better-annotations
Youen 2 years ago
parent
commit
3c7bdc2a4c
  1. 7
      InitGui.py
  2. 4
      ahb_cmd_view_annotate_detail.py
  3. 4
      ahb_cmd_view_refresh.py
  4. 22
      ahb_cmd_view_refresh_fast.py
  5. 22
      ahb_raster_view.py
  6. 38
      ahb_techdraw_extensions.py

7
InitGui.py

@ -78,8 +78,8 @@ class AssemblyHandbookWorkbench(Gui.Workbench):
#self.importModule('ahb_cmd_export_csv') #self.importModule('ahb_cmd_export_csv')
#toolbox.append("AHB_exportCsv") #toolbox.append("AHB_exportCsv")
self.importModule('ahb_cmd_render') #self.importModule('ahb_cmd_render')
toolbox.append("AHB_render") #toolbox.append("AHB_render")
self.importModule('ahb_cmd_new_step') self.importModule('ahb_cmd_new_step')
toolbox.append("AHB_new_step") toolbox.append("AHB_new_step")
@ -98,6 +98,9 @@ class AssemblyHandbookWorkbench(Gui.Workbench):
toolbox.append("AHB_view_add_source_parts") toolbox.append("AHB_view_add_source_parts")
toolbox.append("AHB_view_remove_source_parts") toolbox.append("AHB_view_remove_source_parts")
self.importModule('ahb_cmd_view_refresh_fast')
toolbox.append("AHB_view_refresh_fast")
self.importModule('ahb_cmd_view_refresh') self.importModule('ahb_cmd_view_refresh')
toolbox.append("AHB_view_refresh") toolbox.append("AHB_view_refresh")

4
ahb_cmd_view_annotate_detail.py

@ -3,8 +3,8 @@ import FreeCAD as App
class AHB_View_Annotate_Detail: class AHB_View_Annotate_Detail:
def GetResources(self): def GetResources(self):
return {"MenuText": "Add annotation details", return {"MenuText": "Annotate sub-assembly",
"ToolTip": "Annotates each part of a sub-assembly", "ToolTip": "Annotates each part of selected sub-assembly balloons",
"Pixmap": "" "Pixmap": ""
} }

4
ahb_cmd_view_refresh.py

@ -3,7 +3,7 @@ import FreeCAD as App
class AHB_RefreshView: class AHB_RefreshView:
def GetResources(self): def GetResources(self):
return {"MenuText": "Refresh page", return {"MenuText": "Refresh page (final quality)",
"ToolTip": "Redraws the current page", "ToolTip": "Redraws the current page",
"Pixmap": "" "Pixmap": ""
} }
@ -16,7 +16,7 @@ class AHB_RefreshView:
workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench
page = workbench.techDrawExtensions.getActivePage() page = workbench.techDrawExtensions.getActivePage()
if page is not None: if page is not None:
workbench.techDrawExtensions.forceRedrawPage(page) workbench.techDrawExtensions.forceRedrawPage(page, fast_render = False)
from ahb_command import AHB_CommandWrapper from ahb_command import AHB_CommandWrapper
AHB_CommandWrapper.addGuiCommand('AHB_view_refresh', AHB_RefreshView()) AHB_CommandWrapper.addGuiCommand('AHB_view_refresh', AHB_RefreshView())

22
ahb_cmd_view_refresh_fast.py

@ -0,0 +1,22 @@
import FreeCADGui as Gui
import FreeCAD as App
class AHB_RefreshViewFast:
def GetResources(self):
return {"MenuText": "Refresh page (fast)",
"ToolTip": "Redraws the current page",
"Pixmap": ""
}
def IsActive(self):
workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench
return workbench.techDrawExtensions.getActivePage() is not None
def Activated(self):
workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench
page = workbench.techDrawExtensions.getActivePage()
if page is not None:
workbench.techDrawExtensions.forceRedrawPage(page, fast_render = True)
from ahb_command import AHB_CommandWrapper
AHB_CommandWrapper.addGuiCommand('AHB_view_refresh_fast', AHB_RefreshViewFast())

22
ahb_raster_view.py

@ -99,7 +99,7 @@ class RasterView:
def _should_render(self, obj): def _should_render(self, obj):
return obj.TypeId in ['Part::Feature', 'Part::FeaturePython', 'PartDesign::Body', 'Part::Mirroring', 'Part::Cut', 'Part::Part2DObjectPython'] return obj.TypeId in ['Part::Feature', 'Part::FeaturePython', 'PartDesign::Body', 'Part::Mirroring', 'Part::Cut', 'Part::Part2DObjectPython']
def render(self): def render(self, fast_render = True):
from pivy import coin from pivy import coin
import os import os
from PIL import Image, ImageDraw, ImageChops from PIL import Image, ImageDraw, ImageChops
@ -141,6 +141,7 @@ class RasterView:
is_new_part = workbench.techDrawExtensions.isNewPartInView(view, part) is_new_part = workbench.techDrawExtensions.isNewPartInView(view, part)
if not fast_render:
is_conflicting = False is_conflicting = False
if link.LinkedObject in duplicated_parts.keys(): if link.LinkedObject in duplicated_parts.keys():
link.LinkedObject = duplicated_parts[link.LinkedObject] link.LinkedObject = duplicated_parts[link.LinkedObject]
@ -173,6 +174,7 @@ class RasterView:
continue continue
if self._should_render(obj): if self._should_render(obj):
if not fast_render:
objects_to_reset[obj] = ( objects_to_reset[obj] = (
obj.ViewObject.Visibility, obj.ViewObject.Visibility,
obj.ViewObject.LineColor, obj.ViewObject.LineColor,
@ -218,16 +220,18 @@ class RasterView:
resolution[0] = int(resolution[0] * max_res / resolution[1]) resolution[0] = int(resolution[0] * max_res / resolution[1])
resolution[1] = int(max_res) resolution[1] = int(max_res)
if fast_render:
composite_img = self._render_lines(tmp_doc, resolution, prev_parts + new_parts, (0.0, 0.0, 0.0), [])
else:
# render old parts in gray lines # render old parts in gray lines
prev_parts_img = self._render_lines(tmp_doc, resolution, prev_parts, (0.6, 0.6, 0.6), []) prev_parts_img = self._render_lines(tmp_doc, resolution, prev_parts, (0.6, 0.6, 0.6), [], fast_render)
# render new parts in black lines (old parts can mask them) # render new parts in black lines (old parts can mask them)
new_parts_img = self._render_lines(tmp_doc, resolution, new_parts, (0.0, 0.0, 0.0), prev_parts) new_parts_img = self._render_lines(tmp_doc, resolution, new_parts, (0.0, 0.0, 0.0), prev_parts, fast_render)
# create the composite image # create the composite image
composite_img = prev_parts_img.copy() composite_img = prev_parts_img.copy()
composite_img.paste(new_parts_img, None, new_parts_img) composite_img.paste(new_parts_img, None, new_parts_img)
#composite_img = new_parts_img.copy()
finally: finally:
# restore properties on objects we have modified # restore properties on objects we have modified
@ -289,7 +293,7 @@ class RasterView:
image.ImageFile = self.image_file_name # TODO: see if it's possible to set a relative path image.ImageFile = self.image_file_name # TODO: see if it's possible to set a relative path
image.recompute() image.recompute()
def _render_lines(self, doc, resolution, parts, line_color, masking_parts): def _render_lines(self, doc, resolution, parts, line_color, masking_parts, fast_render = True):
import tempfile import tempfile
from PIL import Image, ImageDraw, ImageFilter from PIL import Image, ImageDraw, ImageFilter
@ -303,7 +307,7 @@ class RasterView:
# in current version of freecad, link override material does not allow to override all material properties, for example emissive color, so we have to change material of the linked object # in current version of freecad, link override material does not allow to override all material properties, for example emissive color, so we have to change material of the linked object
for obj in self._flatten_objects_tree([link]): for obj in self._flatten_objects_tree([link]):
if self._should_render(obj): 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, 0.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)
@ -325,6 +329,8 @@ class RasterView:
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)
generate_outlines = not fast_render
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
# The technique could be improved by using the depth buffer instead, in order to detect boundaries within the same object # The technique could be improved by using the depth buffer instead, in order to detect boundaries within the same object
@ -381,6 +387,8 @@ class RasterView:
alpha_fullres = alpha_img.resize(outlines_img.size, Image.NEAREST) alpha_fullres = alpha_img.resize(outlines_img.size, Image.NEAREST)
alpha_fullres.paste(outlines_img.point(lambda p: 255), None, outlines_img.point(lambda p: 255 if p == 0 else 0)) alpha_fullres.paste(outlines_img.point(lambda p: 255), None, outlines_img.point(lambda p: 255 if p == 0 else 0))
alpha_img = alpha_fullres.resize(all_lines.size, Image.BILINEAR) alpha_img = alpha_fullres.resize(all_lines.size, Image.BILINEAR)
else:
all_lines = lines_img
# colorize final image # colorize final image
fill_color = (1.0, 1.0, 1.0) fill_color = (1.0, 1.0, 1.0)
@ -388,7 +396,7 @@ class RasterView:
all_lines.point(lambda p: int(fill_color[0] * p + line_color[0] * (255.0 - p))), all_lines.point(lambda p: int(fill_color[0] * p + line_color[0] * (255.0 - p))),
all_lines.point(lambda p: int(fill_color[1] * p + line_color[1] * (255.0 - p))), all_lines.point(lambda p: int(fill_color[1] * p + line_color[1] * (255.0 - p))),
all_lines.point(lambda p: int(fill_color[2] * p + line_color[2] * (255.0 - p))), all_lines.point(lambda p: int(fill_color[2] * p + line_color[2] * (255.0 - p))),
alpha_img alpha_img.point(lambda p: 0 if p == 0 else 255)
]) ])
# crop 1px borders # crop 1px borders

38
ahb_techdraw_extensions.py

@ -82,8 +82,8 @@ class TechDrawExtensions:
workbench.docObserver.onObjectTypeChanged('balloon_changed', 'TechDraw::DrawViewBalloon', lambda obj, prop: self.onBalloonChanged(obj, prop)) workbench.docObserver.onObjectTypeChanged('balloon_changed', 'TechDraw::DrawViewBalloon', lambda obj, prop: self.onBalloonChanged(obj, prop))
workbench.docObserver.onObjectTypeSelected('balloon_selected', 'TechDraw::DrawViewBalloon', lambda operation, obj, sub, point: self.onBalloonSelected(operation, obj, sub, point)) workbench.docObserver.onObjectTypeSelected('balloon_selected', 'TechDraw::DrawViewBalloon', lambda operation, obj, sub, point: self.onBalloonSelected(operation, obj, sub, point))
def repaint(self, view): def repaint(self, view, fast_render = True):
self.views_to_repaint[view] = True self.views_to_repaint[view] = fast_render
QTimer.singleShot(10, self._do_repaint) QTimer.singleShot(10, self._do_repaint)
def _do_repaint(self): def _do_repaint(self):
@ -93,10 +93,10 @@ class TechDrawExtensions:
selection = Gui.Selection.getSelection() selection = Gui.Selection.getSelection()
to_repaint = self.views_to_repaint.keys() to_repaint = self.views_to_repaint.copy()
self.views_to_repaint = {} self.views_to_repaint = {}
for view in to_repaint: for view, fast_render in to_repaint.items():
if '_overlay' in view.Label: if '_overlay' in view.Label:
continue continue
#print("Repainting " + view.Name) #print("Repainting " + view.Name)
@ -116,7 +116,7 @@ class TechDrawExtensions:
print("Rasterizing view " + view.Label + "...") print("Rasterizing view " + view.Label + "...")
raster_view = RasterView(view) raster_view = RasterView(view)
raster_view.render() raster_view.render(fast_render)
view.Visibility = False view.Visibility = False
overlayName = view.Label + "_overlay" overlayName = view.Label + "_overlay"
@ -583,16 +583,17 @@ class TechDrawExtensions:
return obj return obj
return None return None
def forceRedrawPage(self, page, callback = None): def forceRedrawPage(self, page, callback = None, fast_render = True):
needPageUpdate = False
for view in page.Views: for view in page.Views:
if view.TypeId == 'TechDraw::DrawViewPart' and 'Assembly_handbook_PreviousStepView' in view.PropertiesList: if view.TypeId == 'TechDraw::DrawViewPart' and 'Assembly_handbook_PreviousStepView' in view.PropertiesList:
if not 'Assembly_handbook_RasterView' in view.PropertiesList: if not 'Assembly_handbook_RasterView' in view.PropertiesList:
view.addProperty("App::PropertyBool", "Assembly_handbook_RasterView", "Assembly_handbook") view.addProperty("App::PropertyBool", "Assembly_handbook_RasterView", "Assembly_handbook")
view.Assembly_handbook_RasterView = True view.Assembly_handbook_RasterView = True
if 'Assembly_handbook_RasterView' not in view.PropertiesList or not view.Assembly_handbook_RasterView: if 'Assembly_handbook_RasterView' in view.PropertiesList and view.Assembly_handbook_RasterView:
needPageUpdate = True view.purgeTouched() # make sure we don't trigger rendering of source views (this is awfully slow and doesn't even work for a lot of models)
else:
view.touch()
self.refreshView(view) self.refreshView(view)
elif view.TypeId == 'TechDraw::DrawViewBalloon': elif view.TypeId == 'TechDraw::DrawViewBalloon':
if view.ViewObject.Visibility: if view.ViewObject.Visibility:
@ -608,23 +609,34 @@ class TechDrawExtensions:
view.ViewObject.Visibility = True view.ViewObject.Visibility = True
view.ViewObject.Visibility = False view.ViewObject.Visibility = False
if page.KeepUpdated or not needPageUpdate: if page.KeepUpdated:
for view in page.Views: for view in page.Views:
if view.TypeId != 'TechDraw::DrawViewPart': if view.TypeId != 'TechDraw::DrawViewPart':
view.recompute() view.recompute()
for view in page.Views: for view in page.Views:
if view.TypeId == 'TechDraw::DrawViewPart': if view.TypeId == 'TechDraw::DrawViewPart':
view.recompute() view.recompute()
self.repaint(view) self.repaint(view, fast_render)
if callback is not None: if callback is not None:
callback() callback()
else: else:
page.KeepUpdated = True page.KeepUpdated = True
def restoreKeepUpdated(): def restoreKeepUpdated():
page.KeepUpdated = False
for view in page.Views: for view in page.Views:
if view.TypeId == 'TechDraw::DrawViewPart': if view.TypeId == 'TechDraw::DrawViewPart':
self.repaint(view) if view.Name.endswith('_overlay'):
view.touch()
view.recompute()
for sub_view in page.Views:
try:
if sub_view.SourceView == view:
sub_view.recompute()
except:
pass
else:
view.recompute()
self.repaint(view, fast_render)
page.KeepUpdated = False
if callback is not None: if callback is not None:
callback() callback()
QTimer.singleShot(10, restoreKeepUpdated) QTimer.singleShot(10, restoreKeepUpdated)

Loading…
Cancel
Save