import FreeCADGui as Gui import FreeCAD as App class AHB_View_Annotate: def GetResources(self): return {"MenuText": "Annotate view", "ToolTip": "Annotates a TechDraw view with object names", "Pixmap": "" } def IsActive(self): return True def Activated(self): workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench if len(Gui.Selection.getSelection()) == 0: page = workbench.techDrawExtensions.getActivePage() workbench.techDrawExtensions.refreshOverlays(page) return if len(Gui.Selection.getSelection()) != 1: raise Exception("Please select exactly one TechDraw view") view = Gui.Selection.getSelection()[0] if view.TypeId == 'TechDraw::DrawPage': workbench.techDrawExtensions.refreshOverlays(view) return elif view.TypeId != 'TechDraw::DrawViewPart': raise Exception("Selected object is not a TechDraw view") overlay_view = workbench.techDrawExtensions.getOverlayView(view) doc = view.Document page = workbench.techDrawExtensions.getViewPage(view) if page is None: raise Exception("Can't find page in which the selected view is located") def list_sub_parts(parts): result = [] for part in parts: if part.TypeId == 'App::Link': result.append(part) elif part.TypeId == 'Part::FeaturePython' and hasattr(part, 'LinkedObject'): # variant link result.append(part) if hasattr(part, 'Group'): result.extend(list_sub_parts(part.Group)) return result all_parts = list_sub_parts(view.XSource) # Remove balloons referencing missing objects for balloon in page.Views: if balloon.TypeId == 'TechDraw::DrawViewBalloon' and "Assembly_handbook_Source" in balloon.PropertiesList: if balloon.SourceView != view and balloon.SourceView != overlay_view: continue partLink = workbench.techDrawExtensions.getBalloonSourcePart(balloon) if partLink is None or partLink not in all_parts: ref_name = "" try: ref_name = balloon.Assembly_handbook_Source[1] except: pass print(balloon.Name + " references missing object " + ref_name + ", removing balloon") doc.removeObject(balloon.Name) for partLink in view.XSource: workbench.techDrawExtensions.add_or_update_balloon(view, partLink, '') workbench.techDrawExtensions.refreshOverlays(page) from ahb_command import AHB_CommandWrapper AHB_CommandWrapper.addGuiCommand('AHB_view_annotate', AHB_View_Annotate())