forked from youen/assembly_handbook
alternative rendering method (rasterization) ; wip
This commit is contained in:
parent
05e7da16a5
commit
be9ff003e3
@ -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)
|
||||||
|
@ -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:
|
if view.Assembly_handbook_RasterView:
|
||||||
fast_rendering = True
|
print("Rasterizing view " + view.Label + "...")
|
||||||
|
imageName = view.Label + "_raster"
|
||||||
selected_balloons = []
|
image = doc.getObject(imageName)
|
||||||
for obj in Gui.Selection.getSelection():
|
if image is None:
|
||||||
if obj.TypeId == 'TechDraw::DrawViewBalloon' and obj.SourceView == view and 'Assembly_handbook_Source' in obj.PropertiesList:
|
image = doc.addObject('TechDraw::DrawViewImage', view.Label + "_raster")
|
||||||
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)
|
|
||||||
|
|
||||||
# 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:
|
if not image in page.Views:
|
||||||
default_line_thickness = 0.05
|
page.addView(image)
|
||||||
line_thickness = default_line_thickness
|
|
||||||
|
new_views_list = page.Views
|
||||||
default_color = (0.0, 0.0, 0.0) if fast_rendering else (0.5, 0.5, 0.5)
|
new_views_list.remove(image)
|
||||||
color = default_color
|
view_idx = new_views_list.index(view)
|
||||||
|
new_views_list.insert(view_idx, image)
|
||||||
if part is not None:
|
page.Views = new_views_list
|
||||||
part_view = workbench.partsCache.getPart2DView(view, part)
|
|
||||||
|
image_file_name = doc.FileName.replace('.FCStd', '') + '_raster/' + view.Name + '.png'
|
||||||
center = self.computePartCenter(view, part)
|
self.rasterizeView(view, image_file_name)
|
||||||
|
image.ImageFile = image_file_name # TODO: see if it's possible to set a relative path
|
||||||
if self.isNewPartInView(view, part):
|
image.recompute()
|
||||||
line_thickness = 0.2
|
else:
|
||||||
color = (0, 0, 0)
|
fast_rendering = False
|
||||||
|
#try:
|
||||||
if self.enable_selected_part_highlight:
|
# fast_rendering = view.Assembly_handbook_FastRendering
|
||||||
for balloon in selected_balloons:
|
#except:
|
||||||
if part == self.getBalloonSourcePart(balloon):
|
# pass
|
||||||
color = (0.0, 0.85, 0.0) # selection highlighting
|
|
||||||
|
if view.CoarseView:
|
||||||
# iterate edges of actual view and highlight matching edges
|
fast_rendering = True
|
||||||
for edgeIdx in range(10000):
|
|
||||||
hasEdge = False
|
selected_balloons = []
|
||||||
try:
|
for obj in Gui.Selection.getSelection():
|
||||||
edge = view.getEdgeByIndex(edgeIdx)
|
if obj.TypeId == 'TechDraw::DrawViewBalloon' and obj.SourceView == view and 'Assembly_handbook_Source' in obj.PropertiesList:
|
||||||
hasEdge = True
|
selected_balloons.append(obj)
|
||||||
except:
|
|
||||||
pass
|
#view.clearGeomFormats() # for an unknown reason, this will crash freecad
|
||||||
if not hasEdge:
|
|
||||||
break
|
if not fast_rendering:
|
||||||
|
is_first_part = True
|
||||||
is_edge_of_part = False
|
|
||||||
if part is not None and (not hasattr(edge.Curve, 'Degree') or edge.Curve.Degree == 1) and len(edge.Vertexes) == 2:
|
parts_to_paint = []
|
||||||
edgeData = [
|
|
||||||
edge.Vertexes[0].X - center.x,
|
# repaint parts that are highlighted by selection
|
||||||
edge.Vertexes[0].Y - center.y,
|
if self.enable_selected_part_highlight:
|
||||||
edge.Vertexes[1].X - center.x,
|
for balloon in selected_balloons:
|
||||||
edge.Vertexes[1].Y - center.y
|
part = self.getBalloonSourcePart(balloon)
|
||||||
]
|
if part in view.XSource:
|
||||||
v0 = App.Vector(edgeData[0], edgeData[1])
|
parts_to_paint.append(part)
|
||||||
v1 = App.Vector(edgeData[2], edgeData[3])
|
|
||||||
|
|
||||||
for line in part_view.cached_lines:
|
# repaint parts that are new in this step (thick line)
|
||||||
l0 = App.Vector(line[0], line[1])
|
prev_view = None
|
||||||
l1 = App.Vector(line[2], line[3])
|
if 'Assembly_handbook_PreviousStepView' in view.PropertiesList:
|
||||||
#d = abs(edgeData[0] - line[0]) + abs(edgeData[1] - line[1]) + abs(edgeData[2] - line[2]) + abs(edgeData[3] - line[3])
|
prev_view = doc.getObject(view.Assembly_handbook_PreviousStepView)
|
||||||
d = v0.distanceToLineSegment(l0, l1).Length + v1.distanceToLineSegment(l0, l1).Length
|
for part in view.XSource:
|
||||||
if d < 0.01:
|
if (prev_view is None or part not in prev_view.XSource) and part not in parts_to_paint:
|
||||||
is_edge_of_part = True
|
parts_to_paint.append(part)
|
||||||
break
|
|
||||||
|
# make sure the list is not empty, so that we reset all lines
|
||||||
if is_edge_of_part:
|
if len(parts_to_paint) == 0:
|
||||||
view.formatGeometricEdge(edgeIdx,1,line_thickness,color,True)
|
parts_to_paint.append(None)
|
||||||
elif is_first_part:
|
|
||||||
# reset edge format
|
|
||||||
view.formatGeometricEdge(edgeIdx,1,default_line_thickness,default_color,True)
|
|
||||||
|
|
||||||
is_first_part = False
|
for part in parts_to_paint:
|
||||||
|
default_line_thickness = 0.05
|
||||||
view.requestPaint()
|
line_thickness = default_line_thickness
|
||||||
|
|
||||||
|
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:
|
||||||
|
part_view = workbench.partsCache.getPart2DView(view, part)
|
||||||
|
|
||||||
|
center = self.computePartCenter(view, part)
|
||||||
|
|
||||||
|
if self.isNewPartInView(view, part):
|
||||||
|
line_thickness = 0.2
|
||||||
|
color = (0, 0, 0)
|
||||||
|
|
||||||
|
if self.enable_selected_part_highlight:
|
||||||
|
for balloon in selected_balloons:
|
||||||
|
if part == self.getBalloonSourcePart(balloon):
|
||||||
|
color = (0.0, 0.85, 0.0) # selection highlighting
|
||||||
|
|
||||||
|
# iterate edges of actual view and highlight matching edges
|
||||||
|
for edgeIdx in range(10000):
|
||||||
|
hasEdge = False
|
||||||
|
try:
|
||||||
|
edge = view.getEdgeByIndex(edgeIdx)
|
||||||
|
hasEdge = True
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
if not hasEdge:
|
||||||
|
break
|
||||||
|
|
||||||
|
is_edge_of_part = False
|
||||||
|
if part is not None and (not hasattr(edge.Curve, 'Degree') or edge.Curve.Degree == 1) and len(edge.Vertexes) == 2:
|
||||||
|
edgeData = [
|
||||||
|
edge.Vertexes[0].X - center.x,
|
||||||
|
edge.Vertexes[0].Y - center.y,
|
||||||
|
edge.Vertexes[1].X - center.x,
|
||||||
|
edge.Vertexes[1].Y - center.y
|
||||||
|
]
|
||||||
|
v0 = App.Vector(edgeData[0], edgeData[1])
|
||||||
|
v1 = App.Vector(edgeData[2], edgeData[3])
|
||||||
|
|
||||||
|
for line in part_view.cached_lines:
|
||||||
|
l0 = App.Vector(line[0], line[1])
|
||||||
|
l1 = App.Vector(line[2], line[3])
|
||||||
|
#d = abs(edgeData[0] - line[0]) + abs(edgeData[1] - line[1]) + abs(edgeData[2] - line[2]) + abs(edgeData[3] - line[3])
|
||||||
|
d = v0.distanceToLineSegment(l0, l1).Length + v1.distanceToLineSegment(l0, l1).Length
|
||||||
|
if d < 0.01:
|
||||||
|
is_edge_of_part = True
|
||||||
|
break
|
||||||
|
|
||||||
|
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…
Reference in New Issue
Block a user