forked from youen/assembly_handbook
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
4.8 KiB
112 lines
4.8 KiB
import FreeCADGui as Gui |
|
import FreeCAD as App |
|
|
|
class AHB_View_Annotate: |
|
updating_balloon = False |
|
|
|
def GetResources(self): |
|
return {"MenuText": "View/Annotate", |
|
"ToolTip": "Annotates a TechDraw view with object names", |
|
"Pixmap": "" |
|
} |
|
|
|
def IsActive(self): |
|
return True |
|
|
|
def Activated(self): |
|
workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") |
|
workbench.docObserver.onObjectTypeChanged('view_annotate_balloon_changed', 'TechDraw::DrawViewBalloon', lambda obj, prop: self.onBalloonChanged(obj, prop)) |
|
workbench.docObserver.onObjectTypeSelected('view_annotate_balloon_selected', 'TechDraw::DrawViewBalloon', lambda operation, obj, sub, point: self.onBalloonSelected(operation, obj, sub, point)) |
|
|
|
doc = App.activeDocument() |
|
|
|
if len(Gui.Selection.getSelection()) != 1: |
|
raise Exception("Please select exactly one TechDraw view") |
|
|
|
view = Gui.Selection.getSelection()[0] |
|
if view.TypeId != 'TechDraw::DrawViewPart': |
|
raise Exception("Selected object is not a TechDraw view") |
|
|
|
page = None |
|
for obj in doc.Objects: |
|
if obj.TypeId == 'TechDraw::DrawPage': |
|
if view in obj.Views: |
|
page = obj |
|
if page is None: |
|
raise Exception("Can't find page in which the selected view is located") |
|
|
|
# Remove balloons referencing missing objects |
|
for balloon in page.Views: |
|
if balloon.TypeId == 'TechDraw::DrawViewBalloon' and "AssemblyHandbook_PartName" in balloon.PropertiesList: |
|
partLink = doc.getObject(balloon.AssemblyHandbook_PartName) |
|
if partLink is None or partLink not in view.XSource: |
|
print(balloon.Name + " references missing object " + balloon.AssemblyHandbook_PartName + ", removing balloon") |
|
doc.removeObject(balloon.Name) |
|
|
|
for partLink in view.XSource: |
|
balloon = None |
|
|
|
# Search an existing balloon to update |
|
for obj in page.Views: |
|
if obj.TypeId == 'TechDraw::DrawViewBalloon' and "AssemblyHandbook_PartName" in obj.PropertiesList and obj.AssemblyHandbook_PartName == partLink.Name: |
|
balloon = obj |
|
|
|
# Create a new balloon if needed |
|
if balloon is None: |
|
balloon = App.ActiveDocument.addObject("TechDraw::DrawViewBalloon", "Balloon") |
|
balloon.SourceView = view |
|
|
|
balloon.addProperty("App::PropertyString", "AssemblyHandbook_PartName", "AssemblyHandbook") |
|
balloon.AssemblyHandbook_PartName = partLink.Name |
|
|
|
page.addView(balloon) |
|
|
|
self.updateBalloon(balloon) |
|
|
|
balloon.X = int(balloon.OriginX) + 100 |
|
balloon.Y = int(balloon.OriginY) + 100 |
|
else: |
|
self.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 onBalloonSelected(self, operation, obj, sub, point): |
|
#print(operation, obj.Name, sub, point) |
|
if "AssemblyHandbook_PartName" in obj.PropertiesList: |
|
if operation == 'added': |
|
print("Selected balloon of " + obj.AssemblyHandbook_PartName) |
|
elif operation == 'removed': |
|
print("Deselected balloon of " + obj.AssemblyHandbook_PartName) |
|
|
|
def updateBalloon(self, balloon): |
|
doc = App.activeDocument() |
|
view = balloon.SourceView |
|
partLink = doc.getObject(balloon.AssemblyHandbook_PartName) |
|
|
|
# Get object center in view space |
|
objectCenterWorld = partLink.LinkPlacement.Matrix.multiply(partLink.LinkedObject.Shape.CenterOfGravity) |
|
vertId = view.makeCosmeticVertex3d(objectCenterWorld) |
|
vert = view.getCosmeticVertex(vertId) |
|
objectCenterView = vert.Point |
|
view.removeCosmeticVertex(vertId) |
|
|
|
balloon.OriginX = objectCenterView.x |
|
balloon.OriginY = objectCenterView.y |
|
|
|
balloon.Text = partLink.LinkedObject.Document.Name |
|
balloon.ViewObject.Font = 'DejaVu Sans' |
|
balloon.ViewObject.Fontsize = 4 |
|
balloon.BubbleShape = 'Inspection' |
|
balloon.EndTypeScale = 4 |
|
|
|
from ahb_command import AHB_CommandWrapper |
|
AHB_CommandWrapper.addGuiCommand('AHB_view_annotate', AHB_View_Annotate())
|
|
|