From 2709d2a59532e25be10fcb203993e381ffc04f5e Mon Sep 17 00:00:00 2001 From: wandererfan Date: Mon, 20 Oct 2025 18:42:28 -0400 Subject: [PATCH] [TD]defend against deleted object error - App.ActiveDocument.Objects was returning a deleted object Most likely due to shenanigans with DrawViewPart/DrawProjGroup item in TaskProjGroup. --- .../CommandFillTemplateFields.py | 62 ++++++++++--------- .../TechDrawTools/TaskFillTemplateFields.py | 16 +++-- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/Mod/TechDraw/TechDrawTools/CommandFillTemplateFields.py b/src/Mod/TechDraw/TechDrawTools/CommandFillTemplateFields.py index 3ec1ab5759..00c7afca3e 100644 --- a/src/Mod/TechDraw/TechDrawTools/CommandFillTemplateFields.py +++ b/src/Mod/TechDraw/TechDrawTools/CommandFillTemplateFields.py @@ -62,37 +62,39 @@ class CommandFillTemplateFields: """Return True when the command should be active or False when it should be disabled (greyed).""" if App.ActiveDocument: - objs = App.ActiveDocument.Objects + objs = App.ActiveDocument.findObjects(Type="TechDraw::DrawPage") + if not objs: + return false + for obj in objs: - if obj.TypeId == "TechDraw::DrawPage": - file_path = ( - App.getResourceDir() - + "Mod/TechDraw/CSVdata/FillTemplateFields.csv" - ) - if os.path.exists(file_path): - listofkeys = [ - "CreatedByChkLst", - "ScaleChkLst", - "LabelChkLst", - "CommentChkLst", - "CompanyChkLst", - "LicenseChkLst", - "CreatedDateChkLst", - "LastModifiedDateChkLst", - ] - with codecs.open(file_path, encoding="utf-8") as fp: - reader = csv.DictReader(fp) - page = obj - texts = page.Template.EditableTexts - if ( - texts - and os.path.exists(file_path) - and listofkeys == reader.fieldnames - and obj.Views != [] - ): - return True - else: - return False + file_path = ( + App.getResourceDir() + + "Mod/TechDraw/CSVdata/FillTemplateFields.csv" + ) + if os.path.exists(file_path): + listofkeys = [ + "CreatedByChkLst", + "ScaleChkLst", + "LabelChkLst", + "CommentChkLst", + "CompanyChkLst", + "LicenseChkLst", + "CreatedDateChkLst", + "LastModifiedDateChkLst", + ] + with codecs.open(file_path, encoding="utf-8") as fp: + reader = csv.DictReader(fp) + page = obj + texts = page.Template.EditableTexts + if ( + texts + and os.path.exists(file_path) + and listofkeys == reader.fieldnames + and obj.Views != [] + ): + return True + return false + # diff --git a/src/Mod/TechDraw/TechDrawTools/TaskFillTemplateFields.py b/src/Mod/TechDraw/TechDrawTools/TaskFillTemplateFields.py index d39de1b0ca..1660932085 100644 --- a/src/Mod/TechDraw/TechDrawTools/TaskFillTemplateFields.py +++ b/src/Mod/TechDraw/TechDrawTools/TaskFillTemplateFields.py @@ -91,11 +91,11 @@ keyLst = [] class TaskFillTemplateFields: def __init__(self): - objs = App.ActiveDocument.Objects + objs = App.ActiveDocument.findObjects(Type="TechDraw::DrawPage") + for obj in objs: if ( - obj.TypeId == "TechDraw::DrawPage" - and os.path.exists(file_path) + os.path.exists(file_path) and listofkeys == reader.fieldnames ): self.page = obj @@ -117,10 +117,14 @@ class TaskFillTemplateFields: projgrp_view = None for pageObj in obj.Views: - if pageObj.isDerivedFrom("TechDraw::DrawViewPart"): - projgrp_view = self.page.Views[0] - elif pageObj.isDerivedFrom("TechDraw::DrawProjGroup"): + if ( + pageObj.isDerivedFrom("TechDraw::DrawViewPart") + or pageObj.isDerivedFrom("TechDraw::DrawProjGroup") + ): + # should this not be pageObj? this is looking for any DVP or DPG on the page? + # Views[0] could be an annotation or symbol or ??? - WF projgrp_view = self.page.Views[0] + break self.texts = self.page.Template.EditableTexts