Browse Source

Added code to change colors of parts rendered in rasterized views (white with black lines)

dev/better-annotations
Youen 2 years ago
parent
commit
25c5149e9f
  1. 128
      ahb_raster_view.py

128
ahb_raster_view.py

@ -79,12 +79,33 @@ class RasterView:
offset = self.image_view.Assembly_handbook_ViewVolumeOffset offset = self.image_view.Assembly_handbook_ViewVolumeOffset
p = point2d - offset p = point2d - offset
return self.image_x_dir_inv * p.x + self.image_y_dir_inv * p.y + self.image_z_dir_inv * p.z return self.image_x_dir_inv * p.x + self.image_y_dir_inv * p.y + self.image_z_dir_inv * p.z
def _flatten_objects_tree(self, obj_list):
result = []
for obj in obj_list:
if obj.TypeId == 'Part::FeaturePython' and hasattr(obj, 'LinkedObject'): # variant link
result.extend(self._flatten_objects_tree(obj.Group))
elif obj.TypeId in ['App::Link']:
result.extend(self._flatten_objects_tree([obj.LinkedObject]))
elif obj.TypeId in ['App::Part', 'App::DocumentObjectGroup']:
result.extend(self._flatten_objects_tree(obj.Group))
elif obj.TypeId in ['Part::Feature', 'Part::FeaturePython', 'PartDesign::Body', 'PartDesign::CoordinateSystem', 'PartDesign::Line', 'Part::Mirroring', 'Part::Cut']:
result.append(obj)
if hasattr(obj, 'Group'):
result.extend(self._flatten_objects_tree(obj.Group))
return result
def _should_render(self, obj):
return obj.TypeId in ['Part::Feature', 'Part::FeaturePython', 'PartDesign::Body', 'Part::Mirroring', 'Part::Cut']
def render(self): def render(self):
from pivy import coin from pivy import coin
import os import os
from PIL import Image, ImageDraw, ImageChops from PIL import Image, ImageDraw, ImageChops
workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench
view = self.source_view view = self.source_view
self.init_image() self.init_image()
@ -94,36 +115,87 @@ 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)
doc = App.newDocument('tmp_raster', hidden=False, temp=False) doc = App.newDocument('tmp_raster', hidden=False, temp=False)
for part in view.XSource:
link = doc.addObject('App::Link', part.Name) objects_to_reset = {}
link.Label = part.Label try:
if part.TypeId == 'App::Link': for part in view.XSource:
link.LinkedObject = part.LinkedObject link = doc.addObject('App::Link', part.Name)
link.Placement = part.Placement link.Label = part.Label
elif part.TypeId == 'Part::FeaturePython' and hasattr(part, 'LinkedObject'): # variant link if part.TypeId == 'App::Link':
link.LinkedObject = part.LinkedObject link.LinkedObject = part.LinkedObject
link.Placement = part.Placement link.Placement = part.Placement
else: elif part.TypeId == 'Part::FeaturePython' and hasattr(part, 'LinkedObject'): # variant link
link.LinkedObject = part link.LinkedObject = part.LinkedObject
link.Placement = part.Placement
else:
link.LinkedObject = part
new_part = workbench.techDrawExtensions.isNewPartInView(view, part)
docView = Gui.getDocument(doc.Name).mdiViewsOfType('Gui::View3DInventor')[0] link.ViewObject.OverrideMaterial = True
link.ViewObject.ShapeMaterial.DiffuseColor = (0.0, 0.0, 0.0, 0.0) if new_part else (0.5, 0.5, 0.5, 0.0) # this actually changes the line color
cam = docView.getCameraNode()
# 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
rot = coin.SbRotation(coin.SbVec3f(1,0,0), coin.SbVec3f(view.XDirection.x,view.XDirection.y,view.XDirection.z)) for obj in self._flatten_objects_tree([link]):
rot *= coin.SbRotation(coin.SbVec3f(0,0,1), coin.SbVec3f(view.Direction.x,view.Direction.y,view.Direction.z)) if obj in objects_to_reset.keys():
cam.orientation.setValue(rot) continue
docView.fitAll() if self._should_render(obj):
objects_to_reset[obj] = (
viewVolume = cam.getViewVolume(0.0) obj.ViewObject.LineColor,
self.image_view.Assembly_handbook_ViewVolumeWidth = viewVolume.getWidth() obj.ViewObject.ShapeMaterial.AmbientColor,
self.image_view.Assembly_handbook_ViewVolumeHeight = viewVolume.getHeight() obj.ViewObject.ShapeMaterial.DiffuseColor,
self.image_view.Assembly_handbook_ViewVolumeDepth = viewVolume.getDepth() obj.ViewObject.ShapeMaterial.SpecularColor,
obj.ViewObject.ShapeMaterial.EmissiveColor,
docView.saveImage(self.image_file_name, 4096, 4096, "#ffffff") obj.ViewObject.LineWidth
)
obj.ViewObject.LineColor = (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.SpecularColor = (0.0, 0.0, 0.0, 0.0)
obj.ViewObject.ShapeMaterial.EmissiveColor = (1.0, 1.0, 1.0, 0.0)
# 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 = 2.0
else:
objects_to_reset[obj] = (
obj.ViewObject.Visibility,
)
obj.ViewObject.Visibility = False
docView = Gui.getDocument(doc.Name).mdiViewsOfType('Gui::View3DInventor')[0]
cam = docView.getCameraNode()
rot = coin.SbRotation(coin.SbVec3f(1,0,0), coin.SbVec3f(view.XDirection.x,view.XDirection.y,view.XDirection.z))
rot *= coin.SbRotation(coin.SbVec3f(0,0,1), coin.SbVec3f(view.Direction.x,view.Direction.y,view.Direction.z))
cam.orientation.setValue(rot)
docView.fitAll()
viewVolume = cam.getViewVolume(0.0)
self.image_view.Assembly_handbook_ViewVolumeWidth = viewVolume.getWidth()
self.image_view.Assembly_handbook_ViewVolumeHeight = viewVolume.getHeight()
self.image_view.Assembly_handbook_ViewVolumeDepth = viewVolume.getDepth()
docView.saveImage(self.image_file_name, 4096, 4096, "#ffffff")
finally:
for obj, props in objects_to_reset.items():
if self._should_render(obj):
obj.ViewObject.LineColor = props[0]
obj.ViewObject.ShapeMaterial.AmbientColor = props[1]
obj.ViewObject.ShapeMaterial.DiffuseColor = props[2]
obj.ViewObject.ShapeMaterial.SpecularColor = props[3]
obj.ViewObject.ShapeMaterial.EmissiveColor = props[4]
obj.ViewObject.LineWidth = props[5]
else:
obj.ViewObject.Visibility = props[0]
with Image.open(self.image_file_name) as img: with Image.open(self.image_file_name) as img:
original_size = img.size original_size = img.size

Loading…
Cancel
Save