Browse Source

alternative rendering method (rasterization) ; wip

dev/better-annotations
Youen 2 years ago
parent
commit
be9ff003e3
  1. 1
      ahb_cmd_new_step.py
  2. 253
      ahb_techdraw_extensions.py

1
ahb_cmd_new_step.py

@ -64,6 +64,7 @@ class AHB_New_Step:
view = doc.addObject('TechDraw::DrawViewPart', 'View') view = doc.addObject('TechDraw::DrawViewPart', 'View')
view.Perspective = False view.Perspective = False
view.addProperty("App::PropertyString", "Assembly_handbook_PreviousStepView", "Assembly_handbook") view.addProperty("App::PropertyString", "Assembly_handbook_PreviousStepView", "Assembly_handbook")
view.addProperty("App::PropertyBool", "Assembly_handbook_RasterView", "Assembly_handbook")
if prev_view is None: if prev_view is None:
try: try:
workbench.techDrawExtensions.setCurrentViewDirection(view) workbench.techDrawExtensions.setCurrentViewDirection(view)

253
ahb_techdraw_extensions.py

@ -94,113 +94,173 @@ class TechDrawExtensions:
for view in to_repaint: for view in to_repaint:
#print("Repainting " + view.Name) #print("Repainting " + view.Name)
page = self.getViewPage(view)
view_cache = self.getViewCache(view) view_cache = self.getViewCache(view)
view_cache.reset() view_cache.reset()
doc = view.Document doc = view.Document
fast_rendering = False if not 'Assembly_handbook_RasterView' in view.PropertiesList:
#try: view.addProperty("App::PropertyBool", "Assembly_handbook_RasterView", "Assembly_handbook")
# fast_rendering = view.Assembly_handbook_FastRendering
#except:
# pass
if view.CoarseView:
fast_rendering = True
selected_balloons = []
for obj in Gui.Selection.getSelection():
if obj.TypeId == 'TechDraw::DrawViewBalloon' and obj.SourceView == view and 'Assembly_handbook_Source' in obj.PropertiesList:
selected_balloons.append(obj)
#view.clearGeomFormats() # for an unknown reason, this will crash freecad
if not fast_rendering:
is_first_part = True
parts_to_paint = []
# repaint parts that are highlighted by selection if view.Assembly_handbook_RasterView:
if self.enable_selected_part_highlight: print("Rasterizing view " + view.Label + "...")
for balloon in selected_balloons: imageName = view.Label + "_raster"
part = self.getBalloonSourcePart(balloon) image = doc.getObject(imageName)
if part in view.XSource: if image is None:
image = doc.addObject('TechDraw::DrawViewImage', view.Label + "_raster")
if not image in page.Views:
page.addView(image)
new_views_list = page.Views
new_views_list.remove(image)
view_idx = new_views_list.index(view)
new_views_list.insert(view_idx, image)
page.Views = new_views_list
image_file_name = doc.FileName.replace('.FCStd', '') + '_raster/' + view.Name + '.png'
self.rasterizeView(view, image_file_name)
image.ImageFile = image_file_name # TODO: see if it's possible to set a relative path
image.recompute()
else:
fast_rendering = False
#try:
# fast_rendering = view.Assembly_handbook_FastRendering
#except:
# pass
if view.CoarseView:
fast_rendering = True
selected_balloons = []
for obj in Gui.Selection.getSelection():
if obj.TypeId == 'TechDraw::DrawViewBalloon' and obj.SourceView == view and 'Assembly_handbook_Source' in obj.PropertiesList:
selected_balloons.append(obj)
#view.clearGeomFormats() # for an unknown reason, this will crash freecad
if not fast_rendering:
is_first_part = True
parts_to_paint = []
# repaint parts that are highlighted by selection
if self.enable_selected_part_highlight:
for balloon in selected_balloons:
part = self.getBalloonSourcePart(balloon)
if part in view.XSource:
parts_to_paint.append(part)
# repaint parts that are new in this step (thick line)
prev_view = None
if 'Assembly_handbook_PreviousStepView' in view.PropertiesList:
prev_view = doc.getObject(view.Assembly_handbook_PreviousStepView)
for part in view.XSource:
if (prev_view is None or part not in prev_view.XSource) and part not in parts_to_paint:
parts_to_paint.append(part) parts_to_paint.append(part)
# repaint parts that are new in this step (thick line)
prev_view = None
if 'Assembly_handbook_PreviousStepView' in view.PropertiesList:
prev_view = doc.getObject(view.Assembly_handbook_PreviousStepView)
for part in view.XSource:
if (prev_view is None or part not in prev_view.XSource) and part not in parts_to_paint:
parts_to_paint.append(part)
# make sure the list is not empty, so that we reset all lines
if len(parts_to_paint) == 0:
parts_to_paint.append(None)
for part in parts_to_paint: # make sure the list is not empty, so that we reset all lines
default_line_thickness = 0.05 if len(parts_to_paint) == 0:
line_thickness = default_line_thickness parts_to_paint.append(None)
default_color = (0.0, 0.0, 0.0) if fast_rendering else (0.5, 0.5, 0.5)
color = default_color
if part is not None: for part in parts_to_paint:
part_view = workbench.partsCache.getPart2DView(view, part) default_line_thickness = 0.05
line_thickness = default_line_thickness
center = self.computePartCenter(view, part) default_color = (0.0, 0.0, 0.0) if fast_rendering else (0.5, 0.5, 0.5)
color = default_color
if self.isNewPartInView(view, part):
line_thickness = 0.2
color = (0, 0, 0)
if self.enable_selected_part_highlight: if part is not None:
for balloon in selected_balloons: part_view = workbench.partsCache.getPart2DView(view, part)
if part == self.getBalloonSourcePart(balloon):
color = (0.0, 0.85, 0.0) # selection highlighting center = self.computePartCenter(view, part)
# iterate edges of actual view and highlight matching edges if self.isNewPartInView(view, part):
for edgeIdx in range(10000): line_thickness = 0.2
hasEdge = False color = (0, 0, 0)
try:
edge = view.getEdgeByIndex(edgeIdx) if self.enable_selected_part_highlight:
hasEdge = True for balloon in selected_balloons:
except: if part == self.getBalloonSourcePart(balloon):
pass color = (0.0, 0.85, 0.0) # selection highlighting
if not hasEdge:
break
is_edge_of_part = False # iterate edges of actual view and highlight matching edges
if part is not None and (not hasattr(edge.Curve, 'Degree') or edge.Curve.Degree == 1) and len(edge.Vertexes) == 2: for edgeIdx in range(10000):
edgeData = [ hasEdge = False
edge.Vertexes[0].X - center.x, try:
edge.Vertexes[0].Y - center.y, edge = view.getEdgeByIndex(edgeIdx)
edge.Vertexes[1].X - center.x, hasEdge = True
edge.Vertexes[1].Y - center.y except:
] pass
v0 = App.Vector(edgeData[0], edgeData[1]) if not hasEdge:
v1 = App.Vector(edgeData[2], edgeData[3]) break
for line in part_view.cached_lines: is_edge_of_part = False
l0 = App.Vector(line[0], line[1]) if part is not None and (not hasattr(edge.Curve, 'Degree') or edge.Curve.Degree == 1) and len(edge.Vertexes) == 2:
l1 = App.Vector(line[2], line[3]) edgeData = [
#d = abs(edgeData[0] - line[0]) + abs(edgeData[1] - line[1]) + abs(edgeData[2] - line[2]) + abs(edgeData[3] - line[3]) edge.Vertexes[0].X - center.x,
d = v0.distanceToLineSegment(l0, l1).Length + v1.distanceToLineSegment(l0, l1).Length edge.Vertexes[0].Y - center.y,
if d < 0.01: edge.Vertexes[1].X - center.x,
is_edge_of_part = True edge.Vertexes[1].Y - center.y
break ]
v0 = App.Vector(edgeData[0], edgeData[1])
v1 = App.Vector(edgeData[2], edgeData[3])
if is_edge_of_part: for line in part_view.cached_lines:
view.formatGeometricEdge(edgeIdx,1,line_thickness,color,True) l0 = App.Vector(line[0], line[1])
elif is_first_part: l1 = App.Vector(line[2], line[3])
# reset edge format #d = abs(edgeData[0] - line[0]) + abs(edgeData[1] - line[1]) + abs(edgeData[2] - line[2]) + abs(edgeData[3] - line[3])
view.formatGeometricEdge(edgeIdx,1,default_line_thickness,default_color,True) d = v0.distanceToLineSegment(l0, l1).Length + v1.distanceToLineSegment(l0, l1).Length
if d < 0.01:
is_first_part = False is_edge_of_part = True
break
view.requestPaint()
if is_edge_of_part:
view.formatGeometricEdge(edgeIdx,1,line_thickness,color,True)
elif is_first_part:
# reset edge format
view.formatGeometricEdge(edgeIdx,1,default_line_thickness,default_color,True)
is_first_part = False
view.requestPaint()
def rasterizeView(self, view, image_file_name):
from pivy import coin
import os
print('Rasterizing ' + view.Label + " to " + image_file_name + "...")
dir = os.path.dirname(image_file_name)
if not os.path.exists(dir):
os.makedirs(dir)
doc = App.newDocument('tmp_raster', hidden=False, temp=False)
for part in view.XSource:
link = doc.addObject('App::Link', part.Name)
link.Label = part.Label
if part.TypeId == 'App::Link':
link.LinkedObject = part.LinkedObject
link.Placement = part.Placement
else:
link.LinkedObject = part
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()
docView.saveImage(image_file_name, 4096, 4096, "#ffffff")
App.closeDocument(doc.Name)
def updateBalloonCursor(self, view): def updateBalloonCursor(self, view):
selected_balloons = [] selected_balloons = []
for obj in Gui.Selection.getSelection(): for obj in Gui.Selection.getSelection():
@ -227,7 +287,7 @@ class TechDrawExtensions:
if doc != Gui.ActiveDocument.Document: if doc != Gui.ActiveDocument.Document:
raise Exception("Current view is not for the same document as TechDraw view " + view.Name) raise Exception("Current view is not for the same document as TechDraw view " + view.Name)
activeView = Gui.ActiveDocument.ActiveView activeView = Gui.ActiveDocument.ActiveView
if str(type(activeView)) != "<class 'View3DInventorPy'>": if str(type(activeView)) not in ["<class 'View3DInventorPy'>", "<class 'Gui.View3DInventor'>"]:
raise Exception("Current view is not a 3D view") raise Exception("Current view is not a 3D view")
cam = activeView.getCameraNode() cam = activeView.getCameraNode()
@ -408,8 +468,11 @@ class TechDrawExtensions:
return None return None
def forceRedrawPage(self, page, callback = None): def forceRedrawPage(self, page, callback = None):
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 'Assembly_handbook_RasterView' not in view.PropertiesList or not view.Assembly_handbook_RasterView:
needPageUpdate = True
self.refreshView(view) self.refreshView(view)
elif view.TypeId == 'TechDraw::DrawViewBalloon': elif view.TypeId == 'TechDraw::DrawViewBalloon':
if view.ViewObject.Visibility: if view.ViewObject.Visibility:
@ -425,7 +488,7 @@ class TechDrawExtensions:
view.ViewObject.Visibility = True view.ViewObject.Visibility = True
view.ViewObject.Visibility = False view.ViewObject.Visibility = False
if page.KeepUpdated: if page.KeepUpdated or not needPageUpdate:
for view in page.Views: for view in page.Views:
if view.TypeId != 'TechDraw::DrawViewPart': if view.TypeId != 'TechDraw::DrawViewPart':
view.recompute() view.recompute()

Loading…
Cancel
Save