Browse Source

Added possibility to move arrow extremity of balloons

pull/1/head
Youen 2 years ago
parent
commit
4588a3716a
  1. 49
      ahb_cmd_view_annotate.py
  2. 119
      ahb_techdraw_extensions.py

49
ahb_cmd_view_annotate.py

@ -2,8 +2,6 @@ import FreeCADGui as Gui
import FreeCAD as App import FreeCAD as App
class AHB_View_Annotate: class AHB_View_Annotate:
updating_balloon = False
def GetResources(self): def GetResources(self):
return {"MenuText": "View/Annotate", return {"MenuText": "View/Annotate",
"ToolTip": "Annotates a TechDraw view with object names", "ToolTip": "Annotates a TechDraw view with object names",
@ -15,9 +13,6 @@ class AHB_View_Annotate:
def Activated(self): def Activated(self):
workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench
workbench.docObserver.onObjectTypeChanged('view_annotate_balloon_changed', 'TechDraw::DrawViewBalloon', lambda obj, prop: self.onBalloonChanged(obj, prop))
doc = App.activeDocument()
if len(Gui.Selection.getSelection()) != 1: if len(Gui.Selection.getSelection()) != 1:
raise Exception("Please select exactly one TechDraw view") raise Exception("Please select exactly one TechDraw view")
@ -26,6 +21,8 @@ class AHB_View_Annotate:
if view.TypeId != 'TechDraw::DrawViewPart': if view.TypeId != 'TechDraw::DrawViewPart':
raise Exception("Selected object is not a TechDraw view") raise Exception("Selected object is not a TechDraw view")
doc = view.Document
page = workbench.techDrawExtensions.getViewPage(view) page = workbench.techDrawExtensions.getViewPage(view)
if page is None: if page is None:
raise Exception("Can't find page in which the selected view is located") raise Exception("Can't find page in which the selected view is located")
@ -50,49 +47,25 @@ class AHB_View_Annotate:
# Create a new balloon if needed # Create a new balloon if needed
if balloon is None: if balloon is None:
balloon = App.ActiveDocument.addObject("TechDraw::DrawViewBalloon", "Balloon") partName = partLink.Name
balloon = doc.addObject("TechDraw::DrawViewBalloon", "Balloon" + partName)
balloon.SourceView = view balloon.SourceView = view
balloon.addProperty("App::PropertyString", "AssemblyHandbook_PartName", "AssemblyHandbook") balloon.addProperty("App::PropertyString", "AssemblyHandbook_PartName", "AssemblyHandbook")
balloon.AssemblyHandbook_PartName = partLink.Name balloon.AssemblyHandbook_PartName = partName
balloon.addProperty("App::PropertyFloat", "AssemblyHandbook_OriginOffsetX", "AssemblyHandbook")
balloon.addProperty("App::PropertyFloat", "AssemblyHandbook_OriginOffsetY", "AssemblyHandbook")
page.addView(balloon) page.addView(balloon)
self.updateBalloon(balloon) workbench.techDrawExtensions.updateBalloon(balloon)
balloon.X = int(balloon.OriginX) + 20 balloon.X = int(balloon.OriginX) + 20
balloon.Y = int(balloon.OriginY) + 20 balloon.Y = int(balloon.OriginY) + 20
else: else:
self.updateBalloon(balloon) workbench.techDrawExtensions.updateBalloon(balloon)
def onBalloonChanged(self, obj, prop):
# Avoid reentry
if self.updating_balloon:
return
#print('Balloon changed: ' + obj.Name + '.' + prop)
if prop == 'Y' and "AssemblyHandbook_PartName" in obj.PropertiesList:
self.updating_balloon = True
self.updateBalloon(obj)
self.updating_balloon = False
def updateBalloon(self, balloon):
workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench
doc = App.activeDocument()
view = balloon.SourceView
obj = doc.getObject(balloon.AssemblyHandbook_PartName)
objectCenterView = workbench.techDrawExtensions.computePartCenter(view, obj)
balloon.OriginX = objectCenterView.x
balloon.OriginY = objectCenterView.y
balloon.Text = obj.LinkedObject.Document.Name if obj.TypeId == 'App::Link' else obj.Name
balloon.ViewObject.Font = 'DejaVu Sans'
balloon.ViewObject.Fontsize = 4
balloon.BubbleShape = 'Inspection'
balloon.EndTypeScale = 4
from ahb_command import AHB_CommandWrapper from ahb_command import AHB_CommandWrapper
AHB_CommandWrapper.addGuiCommand('AHB_view_annotate', AHB_View_Annotate()) AHB_CommandWrapper.addGuiCommand('AHB_view_annotate', AHB_View_Annotate())

119
ahb_techdraw_extensions.py

@ -2,14 +2,69 @@ import FreeCAD as App
import FreeCADGui as Gui import FreeCADGui as Gui
from PySide.QtCore import QTimer from PySide.QtCore import QTimer
from pickle import TRUE
import TechDraw, TechDrawGui
from PySide import QtGui, QtCore
TDG = TechDrawGui
class CursorItem(QtGui.QGraphicsItem):
def __init__(self, parent = None, view = None):
super().__init__(parent)
self.Type = QtGui.QGraphicsItem.UserType + 501
self.setZValue(500)
self.margin = 10.0
self.size = 100.0
self.view = view
def onViewPosChange(self, callback):
self.viewPosChangeCallback = callback
def boundingRect(self):
return QtCore.QRectF(-self.size/2 - self.margin, -self.size/2 - self.margin,
self.size + 2.0*self.margin, self.size + 2.0*self.margin)
def paint(self, painter, option, widget):
#print("paint")
painter.setBrush(QtCore.Qt.darkRed)
#painter.drawRoundedRect(-100, -100, 200, 200, 50, 50)
h = self.size/2.0
painter.drawLine(-h, 0, h, 0)
painter.drawLine(0, -h, 0, h)
def mousePressEvent(self, event):
#print('mouse press', event)
self.startMovePos = event.pos()
def mouseMoveEvent(self, event):
#print('mouse move', event)
offset = event.pos() - self.startMovePos
self.moveBy(offset.x(), offset.y())
def mouseReleaseEvent(self, event):
#print('mouse release', event)
#print('new pos', self.x(), self.y())
if self.viewPosChangeCallback is not None:
self.viewPosChangeCallback(self.getViewPos())
def getViewPos(self):
scale = self.view.Scale * 10.0
return App.Vector(self.x() / scale, -self.y() / scale)
def setViewPos(self, p):
scale = self.view.Scale * 10.0
self.setPos(p.x * scale, -p.y * scale)
class TechDrawExtensions: class TechDrawExtensions:
views_to_repaint = {} views_to_repaint = {}
view_cursors = {}
updating_balloon = False
def __init__(self): def __init__(self):
workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench
workbench.docObserver.onObjectTypeSelected('view_annotate_balloon_selected', 'TechDraw::DrawViewBalloon', lambda operation, obj, sub, point: self.onBalloonSelected(operation, obj, sub, point)) 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))
def repaint(self, view): def repaint(self, view):
self.views_to_repaint[view] = True self.views_to_repaint[view] = True
@ -24,7 +79,7 @@ class TechDrawExtensions:
self.views_to_repaint = {} self.views_to_repaint = {}
for view in to_repaint: for view in to_repaint:
print("Repainting " + view.Name) #print("Repainting " + view.Name)
selected_balloons = [] selected_balloons = []
for obj in Gui.Selection.getSelection(): for obj in Gui.Selection.getSelection():
@ -82,6 +137,35 @@ class TechDrawExtensions:
view.requestPaint() view.requestPaint()
cursor = self.view_cursors.get(view, None)
if cursor is None:
cursor = CursorItem(view = view)
TDG.addQGIToView(view, cursor);
cursor.onViewPosChange(lambda new_pos: self.onCursorMoved(view, new_pos))
self.view_cursors[view] = cursor
if selected_balloons[0] is None:
cursor.setVisible(False)
else:
cursor.setViewPos(App.Vector(selected_balloons[0].OriginX, selected_balloons[0].OriginY))
cursor.setVisible(True)
def onCursorMoved(self, view, new_pos):
if len(Gui.Selection.getSelection()) == 0: return
balloon = Gui.Selection.getSelection()[0]
if balloon.TypeId != 'TechDraw::DrawViewBalloon' or not 'AssemblyHandbook_PartName' in balloon.PropertiesList: return
if balloon.SourceView != view: return
balloon.OriginX = new_pos.x
balloon.OriginY = new_pos.y
obj = balloon.Document.getObject(balloon.AssemblyHandbook_PartName)
view = balloon.SourceView
center = self.computePartCenter(view, obj)
balloon.AssemblyHandbook_OriginOffsetX = new_pos.x - center.x
balloon.AssemblyHandbook_OriginOffsetY = new_pos.y - center.y
def onBalloonSelected(self, operation, balloon, sub, point): def onBalloonSelected(self, operation, balloon, sub, point):
import time import time
@ -91,6 +175,35 @@ class TechDrawExtensions:
view = balloon.SourceView view = balloon.SourceView
self.repaint(view) self.repaint(view)
def onBalloonChanged(self, obj, prop):
# Avoid reentry
if self.updating_balloon:
return
#print('Balloon changed: ' + obj.Name + '.' + prop)
if prop == 'Y' and "AssemblyHandbook_PartName" in obj.PropertiesList:
self.updating_balloon = True
self.updateBalloon(obj)
self.updating_balloon = False
def updateBalloon(self, balloon):
workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench
view = balloon.SourceView
doc = view.Document
obj = doc.getObject(balloon.AssemblyHandbook_PartName)
objectCenterView = workbench.techDrawExtensions.computePartCenter(view, obj)
balloon.OriginX = objectCenterView.x + balloon.AssemblyHandbook_OriginOffsetX
balloon.OriginY = objectCenterView.y + balloon.AssemblyHandbook_OriginOffsetY
balloon.Text = obj.LinkedObject.Document.Name if obj.TypeId == 'App::Link' else obj.Name
balloon.ViewObject.Font = 'DejaVu Sans'
balloon.ViewObject.Fontsize = 4
balloon.BubbleShape = 'Inspection'
balloon.EndTypeScale = 4
def getViewPage(self, view): def getViewPage(self, view):
for obj in view.InList: for obj in view.InList:
if obj.TypeId == 'TechDraw::DrawPage': if obj.TypeId == 'TechDraw::DrawPage':

Loading…
Cancel
Save