@ -7,8 +7,6 @@ import TechDraw, TechDrawGui
from PySide import QtGui , QtCore
from PySide import QtGui , QtCore
TDG = TechDrawGui
TDG = TechDrawGui
from ahb_material import Material
class CursorItem ( QtGui . QGraphicsItem ) :
class CursorItem ( QtGui . QGraphicsItem ) :
def __init__ ( self , parent = None , view = None ) :
def __init__ ( self , parent = None , view = None ) :
super ( ) . __init__ ( parent )
super ( ) . __init__ ( parent )
@ -18,9 +16,6 @@ class CursorItem(QtGui.QGraphicsItem):
self . size = 100.0
self . size = 100.0
self . view = view
self . view = view
def removeSceneEventFilter ( self , a , b ) :
print ( ' removeSceneEventFilter ' , a , b )
def onViewPosChange ( self , callback ) :
def onViewPosChange ( self , callback ) :
self . viewPosChangeCallback = callback
self . viewPosChangeCallback = callback
@ -79,230 +74,133 @@ class TechDrawExtensions:
enable_selected_part_highlight = False # disable for now, for performance reasons
enable_selected_part_highlight = False # disable for now, for performance reasons
initialized_documents = [ ]
def __init__ ( self ) :
def __init__ ( self ) :
workbench = Gui . getWorkbench ( " AssemblyHandbookWorkbench " ) #: :type workbench: AssemblyHandbookWorkbench
workbench = Gui . getWorkbench ( " AssemblyHandbookWorkbench " ) #: :type workbench: AssemblyHandbookWorkbench
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 ) )
workbench . docObserver . onDocumentEvent ( ' techdrawext_doc_event ' , lambda doc , event : self . onDocumentEvent ( doc , event ) )
if App . ActiveDocument is not None :
self . onDocumentEvent ( App . ActiveDocument , ' activate ' )
def repaint ( self , view , fast_render = True ) :
def repaint ( self , view ) :
self . views_to_repaint [ view ] = fast_render
self . views_to_repaint [ view ] = True
QTimer . singleShot ( 10 , self . _do_repaint )
QTimer . singleShot ( 10 , self . _do_repaint )
def _do_repaint ( self ) :
def _do_repaint ( self ) :
from ahb_raster_view import RasterView
import Draft
workbench = Gui . getWorkbench ( " AssemblyHandbookWorkbench " ) #: :type workbench: AssemblyHandbookWorkbench
workbench = Gui . getWorkbench ( " AssemblyHandbookWorkbench " ) #: :type workbench: AssemblyHandbookWorkbench
selection = Gui . Selection . getSelection ( )
selection = Gui . Selection . getSelection ( )
to_repaint = self . views_to_repaint . copy ( )
to_repaint = self . views_to_repaint . keys ( )
self . views_to_repaint = { }
self . views_to_repaint = { }
for view , fast_render in to_repaint . items ( ) :
for view in to_repaint :
if ' _overlay ' in view . Label :
continue
#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
if not ' Assembly_handbook_RasterView ' in view . PropertiesList :
fast_rendering = False
view . addProperty ( " App::PropertyBool " , " Assembly_handbook_RasterView " , " Assembly_handbook " )
#try:
view . Assembly_handbook_RasterView = True
# fast_rendering = view.Assembly_handbook_FastRendering
#except:
# pass
if view . Assembly_handbook_RasterView :
if view . CoarseView :
print ( " Rasterizing view " + view . Label + " ... " )
fast_rendering = True
raster_view = RasterView ( view )
selected_balloons = [ ]
raster_view . render ( fast_render )
for obj in Gui . Selection . getSelection ( ) :
if obj . TypeId == ' TechDraw::DrawViewBalloon ' and obj . SourceView == view and ' Assembly_handbook_Source ' in obj . PropertiesList :
view . Visibility = False
selected_balloons . append ( obj )
overlayName = view . Label + " _overlay "
overlay = doc . getObject ( overlayName )
#view.clearGeomFormats() # for an unknown reason, this will crash freecad
if overlay is None :
overlay = doc . addObject ( ' TechDraw::DrawViewPart ' , overlayName )
if not fast_rendering :
page . addView ( overlay )
is_first_part = True
overlay_frame_name = view . Label + " _frame "
parts_to_paint = [ ]
overlay_frame = doc . getObject ( overlay_frame_name )
if overlay_frame is not None :
# repaint parts that are highlighted by selection
doc . removeObject ( overlay_frame . Name )
if self . enable_selected_part_highlight :
#overlay_frame = Draft.makeWire(points, closed=False, face=False, support=None)
for balloon in selected_balloons :
overlay_frame = doc . addObject ( " Part::Part2DObjectPython " , overlay_frame_name )
part = self . getBalloonSourcePart ( balloon )
Draft . Wire ( overlay_frame )
if part in view . XSource :
pos = raster_view . projectImageViewPointTo3D ( App . Vector ( 0 , 0 , 0 ) )
pos2 = raster_view . projectImageViewPointTo3D ( App . Vector ( 0.001 , 0.001 , 1 ) )
overlay_frame . Points = [ pos , pos2 ]
Draft . ViewProviderWire ( overlay_frame . ViewObject )
overlay_frame . recompute ( )
overlay_frame2_name = view . Label + " _frame2 "
overlay_frame2 = doc . getObject ( overlay_frame2_name )
if overlay_frame2 is not None :
doc . removeObject ( overlay_frame2 . Name )
overlay_frame2 = doc . addObject ( " Part::Part2DObjectPython " , overlay_frame2_name )
Draft . Wire ( overlay_frame2 )
pos = raster_view . projectImageViewPointTo3D ( App . Vector ( 1 , 1 , 0 ) )
pos2 = raster_view . projectImageViewPointTo3D ( App . Vector ( 1.001 , 1.001 , 1 ) )
overlay_frame2 . Points = [ pos , pos2 ]
Draft . ViewProviderWire ( overlay_frame2 . ViewObject )
overlay_frame2 . recompute ( )
overlay . Source = [ overlay_frame , overlay_frame2 ]
overlay . X = view . X
overlay . Y = view . Y
overlay . Direction = view . Direction
overlay . XDirection = view . XDirection
overlay . ScaleType = view . ScaleType
overlay . Scale = view . Scale
overlay . ViewObject . LineWidth = 0.01
# migrate balloons from source view to overlay
for balloon in page . Views :
if balloon . TypeId == ' TechDraw::DrawViewBalloon ' and " Assembly_handbook_Source " in balloon . PropertiesList and balloon . SourceView == view :
if balloon . SourceView == view :
old_source = balloon . Assembly_handbook_Source
old_OriginOffsetX = balloon . Assembly_handbook_OriginOffsetX
old_OriginOffsetY = balloon . Assembly_handbook_OriginOffsetY
old_X = balloon . X
old_Y = balloon . Y
old_Visibility = balloon . ViewObject . Visibility
balloonName = balloon . Name
doc . removeObject ( balloon . Name )
balloon = doc . addObject ( " TechDraw::DrawViewBalloon " , balloonName )
balloon . SourceView = overlay
balloon . addProperty ( " App::PropertyXLink " , " Assembly_handbook_Source " , " Assembly_handbook " )
balloon . Assembly_handbook_Source = old_source
balloon . addProperty ( " App::PropertyFloat " , " Assembly_handbook_OriginOffsetX " , " Assembly_handbook " )
balloon . addProperty ( " App::PropertyFloat " , " Assembly_handbook_OriginOffsetY " , " Assembly_handbook " )
balloon . Assembly_handbook_OriginOffsetX = old_OriginOffsetX
balloon . Assembly_handbook_OriginOffsetY = old_OriginOffsetY
page . addView ( balloon )
self . updateBalloon ( balloon )
balloon . X = old_X
balloon . Y = old_Y
balloon . ViewObject . Visibility = old_Visibility
balloon . recompute ( )
overlay . recompute ( )
page . 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 )
# make sure the list is not empty, so that we reset all lines
for part in parts_to_paint :
if len ( parts_to_paint ) == 0 :
default_line_thickness = 0.05
parts_to_paint . append ( None )
line_thickness = default_line_thickness
for part in parts_to_paint :
default_color = ( 0.0 , 0.0 , 0.0 ) if fast_rendering else ( 0.5 , 0.5 , 0.5 )
default_line_thickness = 0.05
color = default_color
line_thickness = default_line_thickness
if part is not None :
default_color = ( 0.0 , 0.0 , 0.0 ) if fast_rendering else ( 0.5 , 0.5 , 0.5 )
part_view = workbench . partsCache . getPart2DView ( view , part )
color = default_color
if part is not None :
center = self . computePartCenter ( view , part )
part_view = workbench . partsCache . getPart2DView ( view , part )
if self . isNewPartInView ( view , part ) :
center = self . computePartCenter ( view , part )
line_thickness = 0.2
color = ( 0 , 0 , 0 )
if self . isNewPartInView ( view , part ) :
if self . enable_selected_part_highlight :
line_thickness = 0.2
for balloon in selected_balloons :
color = ( 0 , 0 , 0 )
if part == self . getBalloonSourcePart ( balloon ) :
color = ( 0.0 , 0.85 , 0.0 ) # selection highlighting
if self . enable_selected_part_highlight :
for balloon in selected_balloons :
# iterate edges of actual view and highlight matching edges
if part == self . getBalloonSourcePart ( balloon ) :
for edgeIdx in range ( 10000 ) :
color = ( 0.0 , 0.85 , 0.0 ) # selection highlighting
hasEdge = False
try :
edge = view . getEdgeByIndex ( edgeIdx )
hasEdge = True
except :
pass
if not hasEdge :
break
# iterate edges of actual view and highlight matching edges
is_edge_of_part = False
for edgeIdx in range ( 10000 ) :
if part is not None and ( not hasattr ( edge . Curve , ' Degree ' ) or edge . Curve . Degree == 1 ) and len ( edge . Vertexes ) == 2 :
hasEdge = False
edgeData = [
try :
edge . Vertexes [ 0 ] . X - center . x ,
edge = view . getEdgeByIndex ( edgeIdx )
edge . Vertexes [ 0 ] . Y - center . y ,
hasEdge = True
edge . Vertexes [ 1 ] . X - center . x ,
except :
edge . Vertexes [ 1 ] . Y - center . y
pass
]
if not hasEdge :
v0 = App . Vector ( edgeData [ 0 ] , edgeData [ 1 ] )
break
v1 = App . Vector ( edgeData [ 2 ] , edgeData [ 3 ] )
is_edge_of_part = False
for line in part_view . cached_lines :
if part is not None and ( not hasattr ( edge . Curve , ' Degree ' ) or edge . Curve . Degree == 1 ) and len ( edge . Vertexes ) == 2 :
l0 = App . Vector ( line [ 0 ] , line [ 1 ] )
edgeData = [
l1 = App . Vector ( line [ 2 ] , line [ 3 ] )
edge . Vertexes [ 0 ] . X - center . x ,
#d = abs(edgeData[0] - line[0]) + abs(edgeData[1] - line[1]) + abs(edgeData[2] - line[2]) + abs(edgeData[3] - line[3])
edge . Vertexes [ 0 ] . Y - center . y ,
d = v0 . distanceToLineSegment ( l0 , l1 ) . Length + v1 . distanceToLineSegment ( l0 , l1 ) . Length
edge . Vertexes [ 1 ] . X - center . x ,
if d < 0.01 :
edge . Vertexes [ 1 ] . Y - center . y
is_edge_of_part = True
]
break
v0 = App . Vector ( edgeData [ 0 ] , edgeData [ 1 ] )
v1 = App . Vector ( edgeData [ 2 ] , edgeData [ 3 ] )
for line in part_view . cached_lines :
if is_edge_of_part :
l0 = App . Vector ( line [ 0 ] , line [ 1 ] )
view . formatGeometricEdge ( edgeIdx , 1 , line_thickness , color , True )
l1 = App . Vector ( line [ 2 ] , line [ 3 ] )
elif is_first_part :
#d = abs(edgeData[0] - line[0]) + abs(edgeData[1] - line[1]) + abs(edgeData[2] - line[2]) + abs(edgeData[3] - line[3])
# reset edge format
d = v0 . distanceToLineSegment ( l0 , l1 ) . Length + v1 . distanceToLineSegment ( l0 , l1 ) . Length
view . formatGeometricEdge ( edgeIdx , 1 , default_line_thickness , default_color , True )
if d < 0.01 :
is_edge_of_part = True
is_first_part = False
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 updateBalloonCursor ( self , view ) :
def updateBalloonCursor ( self , view ) :
selected_balloons = [ ]
selected_balloons = [ ]
for obj in Gui . Selection . getSelection ( ) :
for obj in Gui . Selection . getSelection ( ) :
@ -310,13 +208,6 @@ class TechDrawExtensions:
selected_balloons . append ( obj )
selected_balloons . append ( obj )
cursor = self . view_cursors . get ( view , None )
cursor = self . view_cursors . get ( view , None )
if cursor is not None :
try :
cursor . x ( ) # this can throw an exception if the Qt item has been deleted (for example when closing the page)
except Exception as ex :
print ( " Re-generating cursor... " )
cursor = None
if cursor is None :
if cursor is None :
cursor = CursorItem ( view = view )
cursor = CursorItem ( view = view )
TDG . addQGIToView ( view , cursor ) ;
TDG . addQGIToView ( view , cursor ) ;
@ -336,7 +227,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 ) ) not in [ " <class ' View3DInventorPy ' > " , " <class ' Gui.View3DInventor ' > " ] :
if str ( type ( activeView ) ) != " <class ' View3DInventorPy ' > " :
raise Exception ( " Current view is not a 3D view " )
raise Exception ( " Current view is not a 3D view " )
cam = activeView . getCameraNode ( )
cam = activeView . getCameraNode ( )
@ -424,7 +315,7 @@ class TechDrawExtensions:
obj = self . getBalloonSourcePart ( balloon )
obj = self . getBalloonSourcePart ( balloon )
view = balloon . SourceView
view = balloon . SourceView
center = self . computePartCenter ( view , obj , self . getBalloonSourcePartPath ( balloon ) )
center = self . computePartCenter ( view , obj )
balloon . Assembly_handbook_OriginOffsetX = new_pos . x - center . x
balloon . Assembly_handbook_OriginOffsetX = new_pos . x - center . x
balloon . Assembly_handbook_OriginOffsetY = new_pos . y - center . y
balloon . Assembly_handbook_OriginOffsetY = new_pos . y - center . y
@ -436,7 +327,7 @@ class TechDrawExtensions:
view = balloon . SourceView
view = balloon . SourceView
self . updateBalloonCursor ( view )
self . updateBalloonCursor ( view )
if self . enable_selected_part_highlight :
if self . enable_selected_part_highlight :
self . repaint ( view )
self . repaint ( view ) # disabled for now, for performance reasons
def onBalloonChanged ( self , obj , prop ) :
def onBalloonChanged ( self , obj , prop ) :
# Avoid reentry
# Avoid reentry
@ -448,94 +339,31 @@ class TechDrawExtensions:
self . updating_balloon = True
self . updating_balloon = True
self . updateBalloon ( obj )
self . updateBalloon ( obj )
self . updating_balloon = False
self . updating_balloon = False
def add_or_update_balloon ( self , view , part , parent_path ) :
balloonsCreated = [ ]
page = self . getViewPage ( view )
overlay_view = self . getOverlayView ( view )
doc = page . Document
path = parent_path
if path == ' ' :
path = part . Document . Name + ' # ' + part . Name
else :
path + = ' . '
path + = part . Name
# Search an existing balloon to update
balloon = None
for obj in page . Views :
if obj . TypeId == ' TechDraw::DrawViewBalloon ' and self . getBalloonSourcePart ( obj ) == part and self . getBalloonSourcePartPath ( obj ) == path :
if obj . SourceView != overlay_view : continue
balloon = obj
# Create a new balloon if needed
if balloon is None :
if self . isNewPartInView ( view , part ) :
partName = part . Name
balloonName = partName + " _Balloon "
balloon = doc . addObject ( " TechDraw::DrawViewBalloon " , balloonName )
balloon . SourceView = overlay_view
balloon . addProperty ( " App::PropertyXLink " , " Assembly_handbook_Source " , " Assembly_handbook " )
balloon . Assembly_handbook_Source = ( part , part . Name )
balloon . addProperty ( " App::PropertyString " , " Assembly_handbook_SourcePath " , " Assembly_handbook " )
balloon . Assembly_handbook_SourcePath = path
balloon . addProperty ( " App::PropertyFloat " , " Assembly_handbook_OriginOffsetX " , " Assembly_handbook " )
balloon . addProperty ( " App::PropertyFloat " , " Assembly_handbook_OriginOffsetY " , " Assembly_handbook " )
page . addView ( balloon )
self . updateBalloon ( balloon )
if not self . isNewPartInView ( view , part ) :
balloon . ViewObject . Visibility = False
else :
balloonsCreated . append ( balloon )
else :
self . updateBalloon ( balloon )
return balloonsCreated
def updateBalloon ( self , balloon ) :
def updateBalloon ( self , balloon ) :
workbench = Gui . getWorkbench ( " AssemblyHandbookWorkbench " ) #: :type workbench: AssemblyHandbookWorkbench
workbench = Gui . getWorkbench ( " AssemblyHandbookWorkbench " ) #: :type workbench: AssemblyHandbookWorkbench
view = balloon . SourceView
view = balloon . SourceView
obj = self . getBalloonSourcePart ( balloon )
obj = self . getBalloonSourcePart ( balloon )
path = self . getBalloonSourcePartPath ( balloon )
if obj is not None :
partDisplayName = self . getPartDisplayName ( obj )
objectCenterView = workbench . techDrawExtensions . computePartCenter ( view , obj , path )
balloon . OriginX = objectCenterView . x + balloon . Assembly_handbook_OriginOffsetX
objectCenterView = workbench . techDrawExtensions . computePartCenter ( view , obj )
balloon . OriginY = objectCenterView . y + balloon . Assembly_handbook_OriginOffsetY
partDisplayName = ' Inconnu ' if obj is None else self . getPartDisplayName ( obj )
balloon . Text = partDisplayName
balloon . OriginX = objectCenterView . x + balloon . Assembly_handbook_OriginOffsetX
balloon . OriginY = objectCenterView . y + balloon . Assembly_handbook_OriginOffsetY
balloon . Text = partDisplayName
balloon . ViewObject . Font = ' DejaVu Sans '
balloon . ViewObject . Font = ' DejaVu Sans '
balloon . ViewObject . Fontsize = 4
balloon . ViewObject . Fontsize = 4
balloon . BubbleShape = ' Inspection '
balloon . BubbleShape = ' Inspection '
balloon . EndTypeScale = 0.5
balloon . EndTypeScale = 1
def getBalloonSourcePart ( self , balloon ) :
def getBalloonSourcePart ( self , balloon ) :
try :
try :
return balloon . Assembly_handbook_Source [ 0 ]
return balloon . Assembly_handbook_Source [ 0 ]
except :
except :
return None
return None
def getBalloonSourcePartPath ( self , balloon ) :
try :
return balloon . Assembly_handbook_SourcePath
except :
part = self . getBalloonSourcePart ( balloon )
if part is None :
return ' '
return part . Document . Name + ' # ' + part . Name
def isPartLink ( self , obj ) :
def isPartLink ( self , obj ) :
if obj is None :
if obj is None :
@ -581,17 +409,9 @@ class TechDrawExtensions:
return obj
return obj
return None
return None
def forceRedrawPage ( self , page , callback = None , fast_render = True ) :
def forceRedrawPage ( self , page , callback = None ) :
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 :
view . addProperty ( " App::PropertyBool " , " Assembly_handbook_RasterView " , " Assembly_handbook " )
view . Assembly_handbook_RasterView = True
if ' Assembly_handbook_RasterView ' in view . PropertiesList and view . Assembly_handbook_RasterView :
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 :
@ -614,133 +434,29 @@ class TechDrawExtensions:
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 , fast_render )
self . repaint ( view )
if callback is not None :
if callback is not None :
callback ( )
callback ( )
else :
else :
page . KeepUpdated = True
page . KeepUpdated = True
def restoreKeepUpdated ( ) :
def restoreKeepUpdated ( ) :
for view in page . Views :
if view . TypeId == ' TechDraw::DrawViewPart ' :
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
page . KeepUpdated = False
if callback is not None :
callback ( )
QTimer . singleShot ( 10 , restoreKeepUpdated )
def refreshOverlays ( self , page , callback = None ) :
import os
for view in page . Views :
if view . TypeId == ' TechDraw::DrawViewPart ' and ' Assembly_handbook_RasterView ' in view . PropertiesList and view . Assembly_handbook_RasterView :
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)
doc = page . Document
for image in page . Views :
if image . TypeId == ' TechDraw::DrawViewImage ' :
folder_name = ' / ' + os . path . basename ( doc . FileName ) . replace ( ' .FCStd ' , ' ' ) + ' _raster/ '
if folder_name in image . ImageFile :
full_path = doc . FileName . replace ( ' .FCStd ' , ' ' ) + ' _raster/ ' + image . ImageFile . split ( folder_name ) [ 1 ]
if image . ImageFile != full_path :
image . ImageFile = full_path
if page . KeepUpdated :
if callback :
callback ( )
else :
page . KeepUpdated = True
def restoreKeepUpdated ( ) :
for view in page . Views :
for view in page . Views :
if view . TypeId == ' TechDraw::DrawViewPart ' :
if view . TypeId == ' TechDraw::DrawViewPart ' :
if view . Name . endswith ( ' _overlay ' ) :
self . repaint ( view )
view . touch ( )
view . recompute ( )
for sub_view in page . Views :
try :
if sub_view . SourceView == view :
sub_view . recompute ( )
except :
pass
page . KeepUpdated = False
for view in page . Views :
if view . TypeId == ' TechDraw::DrawViewBalloon ' :
if view . ViewObject . Visibility :
# workaround for a TechDraw bug: sometimes the balloon should be visible but doesn't appear, showing it again fixes the issue
view . ViewObject . Visibility = False
def makeRedrawCallback ( view ) :
def redrawBalloon ( ) :
view . ViewObject . Visibility = True
return redrawBalloon
QTimer . singleShot ( 0 , makeRedrawCallback ( view ) )
else :
# workaround for a TechDraw bug: sometimes the balloon text is visible even if the balloon is hidden, hiding it again fixes the issue
view . ViewObject . Visibility = True
view . ViewObject . Visibility = False
if callback is not None :
if callback is not None :
callback ( )
callback ( )
QTimer . singleShot ( 10 , restoreKeepUpdated )
QTimer . singleShot ( 10 , restoreKeepUpdated )
def getSourceView ( self , view ) :
def computePartCenter ( self , view , obj ) :
if view . Name . endswith ( ' _overlay ' ) :
view = view . Document . getObject ( view . Name [ 0 : - 8 ] )
if view is None :
raise Exception ( " Can ' t find source view of " + view . Name )
return view
def getOverlayView ( self , view ) :
if view . Name . endswith ( ' _overlay ' ) :
return view
overlay = view . Document . getObject ( view . Name + ' _overlay ' )
return overlay if overlay is not None else view
def computePartCenter ( self , view , obj , path = None ) :
view = self . getSourceView ( view )
mat = App . Matrix ( )
if path is not None :
path_parts = path . split ( ' . ' )
path_parts . pop ( )
parent = None
for part in path_parts :
if parent is None :
doc_obj = part . split ( ' # ' )
doc = App . getDocument ( doc_obj [ 0 ] )
link = doc . getObject ( doc_obj [ 1 ] )
else :
link = parent . Document . getObject ( part )
mat = link . LinkPlacement * mat
parent = link . LinkedObject
if obj . TypeId == ' App::Link ' :
if obj . TypeId == ' App::Link ' :
partLink = obj
partLink = obj
mat = mat . multiply ( partLink . LinkPlacement . Matrix )
objectCenterWorld = partLink . LinkPlacement . Matrix . multiply ( partLink . LinkedObject . Shape . CenterOfGravity )
objectCenterWorld = partLink . LinkedObject . Shape . CenterOfGravity
elif obj . TypeId == ' Part::FeaturePython ' and hasattr ( obj , ' LinkedObject ' ) : # variant link
elif obj . TypeId == ' Part::FeaturePython ' and hasattr ( obj , ' LinkedObject ' ) : # variant link
partLink = obj
partLink = obj
mat = mat . multiply ( partLink . Placement . Matrix )
objectCenterWorld = partLink . Placement . Matrix . multiply ( partLink . LinkedObject . Shape . CenterOfGravity )
objectCenterWorld = partLink . LinkedObject . Shape . CenterOfGravity
else :
else :
objectCenterWorld = obj . Shape . CenterOfGravity
objectCenterWorld = obj . Shape . CenterOfGravity
objectCenterWorld = mat . multiply ( objectCenterWorld )
''' view_cache = self.getViewCache(view)
''' view_cache = self.getViewCache(view)
@ -759,12 +475,6 @@ class TechDrawExtensions:
return self . projectPoint ( view , objectCenterWorld )
return self . projectPoint ( view , objectCenterWorld )
def projectPoint ( self , view , point3d ) :
def projectPoint ( self , view , point3d ) :
if ' Assembly_handbook_RasterView ' in view . PropertiesList and view . Assembly_handbook_RasterView :
from ahb_raster_view import RasterView
raster_view = RasterView ( view )
if raster_view . init_image_projection ( ) :
return raster_view . project3DPointToSourceView ( point3d )
# DrawViewPart::projectPoint should be exposed to python in freecad 0.21, but for 0.20 we have to use a workaround
# DrawViewPart::projectPoint should be exposed to python in freecad 0.21, but for 0.20 we have to use a workaround
view_cache = self . getViewCache ( view )
view_cache = self . getViewCache ( view )
if view_cache . projected_origin is None :
if view_cache . projected_origin is None :
@ -782,48 +492,3 @@ class TechDrawExtensions:
cache = ViewCache ( )
cache = ViewCache ( )
self . view_cache [ view ] = cache
self . view_cache [ view ] = cache
return cache
return cache
def onDocumentEvent ( self , doc , event ) :
if event == ' activate ' :
if doc not in self . initialized_documents :
self . initialized_documents . append ( doc )
self . initializeDocument ( doc )
elif event == ' deleted ' :
if doc in self . initialized_documents :
self . initialized_documents . remove ( doc )
def initializeDocument ( self , doc ) :
def doInit ( ) :
main_part = None
try :
for obj in doc . Objects :
if obj . TypeId == ' TechDraw::DrawPage ' :
self . onPageLoaded ( obj )
main_parts = doc . getObjectsByLabel ( doc . Name )
if len ( main_parts ) == 1 :
main_part = main_parts [ 0 ]
except :
pass
if main_part is not None :
self . initPartMetadata ( main_part )
QTimer . singleShot ( 0 , doInit )
def initPartMetadata ( self , part ) :
current_material = ' Unknown '
if ' Assembly_handbook_Material ' in part . PropertiesList :
current_material = part . Assembly_handbook_Material
else :
part . addProperty ( " App::PropertyEnumeration " , " Assembly_handbook_Material " , " Assembly_handbook " )
material_list = [ ' Unknown ' ] + Material . GetMaterialIDs ( )
part . Assembly_handbook_Material = material_list
part . Assembly_handbook_Material = material_list . index ( current_material ) if current_material in material_list else 0
if ' Assembly_handbook_Weight ' not in part . PropertiesList :
part . addProperty ( " App::PropertyFloat " , " Assembly_handbook_Weight " , " Assembly_handbook " , ' Part weight in grams. Set a negative number if weight is unknown. ' )
part . Assembly_handbook_Weight = - 1
def onPageLoaded ( self , page ) :
self . refreshOverlays ( page )