File size: 4,356 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# ***************************************************************************
# *   Copyright (c) 2023 Syres                                              *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   This program is distributed in the hope that it will be useful,       *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************
"""Provides the TechDraw FillTemplateFields GuiCommand."""

__title__ = "TechDrawTools.CommandFillTemplateFields"
__author__ = "Syres"
__url__ = "https://www.freecad.org"
__version__ = "00.02"
__date__ = "2023/12/07"

from PySide.QtCore import QT_TRANSLATE_NOOP

import FreeCAD as App
import FreeCADGui as Gui
import codecs
import csv
import os.path

import TechDrawTools


class CommandFillTemplateFields:
    """Use document info to populate the template fields."""

    def GetResources(self):
        """Return a dictionary with data that will be used by the button or menu item."""
        return {
            "Pixmap": "actions/TechDraw_FillTemplateFields.svg",
            "Accel": "",
            "MenuText": QT_TRANSLATE_NOOP(
                "TechDraw_FillTemplateFields", "Update Template Fields"
            ),
            "ToolTip": QT_TRANSLATE_NOOP(
                "TechDraw_FillTemplateFields",
                "Uses document info to populate the template fields",
            ),
        }

    def Activated(self):
        """Run the following code when the command is activated (button press)."""
        TechDrawTools.TaskFillTemplateFields()

    def IsActive(self):
        """Return True when the command should be active
        or False when it should be disabled (greyed)."""
        if App.ActiveDocument:
            objs = App.ActiveDocument.findObjects(Type="TechDraw::DrawPage")
            if not objs:
                return false

            for obj in objs:
                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



#
# The command must be "registered" with a unique name by calling its class.
Gui.addCommand("TechDraw_FillTemplateFields", CommandFillTemplateFields())