FreeCAD

Форк
0
133 строки · 5.8 Кб
1
#***************************************************************************
2
#*                                                                         *
3
#*   Copyright (c) 2021 Yorik van Havre <yorik@uncreated.net>              *
4
#*                                                                         *
5
#*   This program is free software; you can redistribute it and/or modify  *
6
#*   it under the terms of the GNU Lesser General Public License (LGPL)    *
7
#*   as published by the Free Software Foundation; either version 2 of     *
8
#*   the License, or (at your option) any later version.                   *
9
#*   for detail see the LICENCE text file.                                 *
10
#*                                                                         *
11
#*   This program is distributed in the hope that it will be useful,       *
12
#*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
#*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
#*   GNU Library General Public License for more details.                  *
15
#*                                                                         *
16
#*   You should have received a copy of the GNU Library General Public     *
17
#*   License along with this program; if not, write to the Free Software   *
18
#*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
19
#*   USA                                                                   *
20
#*                                                                         *
21
#***************************************************************************
22

23

24
"""This module contains FreeCAD commands for the Draft workbench"""
25

26
import os
27
import FreeCAD
28
from draftguitools import gui_base
29
from draftutils import params
30
from draftutils.translate import QT_TRANSLATE_NOOP, translate
31

32

33
class Draft_Hatch(gui_base.GuiCommandSimplest):
34

35

36
    def GetResources(self):
37

38
        return {'Pixmap'  : "Draft_Hatch",
39
                'MenuText': QT_TRANSLATE_NOOP("Draft_Hatch", "Hatch"),
40
                'Accel': "H, A",
41
                'ToolTip' : QT_TRANSLATE_NOOP("Draft_Hatch", "Creates hatches on the faces of a selected object")}
42

43
    def Activated(self):
44

45
        import FreeCADGui
46

47
        if FreeCADGui.Selection.getSelection():
48
            FreeCADGui.Control.showDialog(Draft_Hatch_TaskPanel(FreeCADGui.Selection.getSelection()[0]))
49
        else:
50
            FreeCAD.Console.PrintError(translate("Draft","You must choose a base object before using this command")+"\n")
51

52

53
class Draft_Hatch_TaskPanel:
54

55

56
    def __init__(self,baseobj):
57

58
        import FreeCADGui
59
        from PySide import QtCore,QtGui
60
        import Draft_rc
61

62
        self.baseobj = baseobj
63
        self.form = FreeCADGui.PySideUic.loadUi(":/ui/dialogHatch.ui")
64
        self.form.setWindowIcon(QtGui.QIcon(":/icons/Draft_Hatch.svg"))
65
        self.form.File.fileNameChanged.connect(self.onFileChanged)
66
        self.form.File.setFileName(params.get_param("FilePattern", path="Mod/TechDraw/PAT"))
67
        pat = params.get_param("NamePattern", path="Mod/TechDraw/PAT")
68
        if pat in [self.form.Pattern.itemText(i) for i in range(self.form.Pattern.count())]:
69
            self.form.Pattern.setCurrentText(pat)
70
        self.form.Scale.setValue(params.get_param("HatchPatternScale"))
71
        self.form.Rotation.setValue(params.get_param("HatchPatternRotation"))
72

73
    def accept(self):
74

75
        import FreeCADGui
76

77
        params.set_param("FilePattern", self.form.File.property("fileName"), path="Mod/TechDraw/PAT")
78
        params.set_param("NamePattern", self.form.Pattern.currentText(), path="Mod/TechDraw/PAT")
79
        params.set_param("HatchPatternScale", self.form.Scale.value())
80
        params.set_param("HatchPatternRotation", self.form.Rotation.value())
81
        if hasattr(self.baseobj,"File") and hasattr(self.baseobj,"Pattern"):
82
            # modify existing hatch object
83
            o = "FreeCAD.ActiveDocument.getObject(\""+self.baseobj.Name+"\")"
84
            FreeCADGui.doCommand(o+".File=\""+self.form.File.property("fileName")+"\"")
85
            FreeCADGui.doCommand(o+".Pattern=\""+self.form.Pattern.currentText()+"\"")
86
            FreeCADGui.doCommand(o+".Scale="+str(self.form.Scale.value()))
87
            FreeCADGui.doCommand(o+".Rotation="+str(self.form.Rotation.value()))
88
        else:
89
            # create new hatch object
90
            FreeCAD.ActiveDocument.openTransaction("Create Hatch")
91
            FreeCADGui.addModule("Draft")
92
            cmd = "Draft.make_hatch("
93
            cmd += "baseobject=FreeCAD.ActiveDocument.getObject(\""+self.baseobj.Name
94
            cmd += "\"),filename=\""+self.form.File.property("fileName")
95
            cmd += "\",pattern=\""+self.form.Pattern.currentText()
96
            cmd += "\",scale="+str(self.form.Scale.value())
97
            cmd += ",rotation="+str(self.form.Rotation.value())+")"
98
            FreeCADGui.doCommand(cmd)
99
            FreeCAD.ActiveDocument.commitTransaction()
100
        FreeCADGui.doCommand("FreeCAD.ActiveDocument.recompute()")
101
        self.reject()
102

103
    def reject(self):
104

105
        import FreeCADGui
106

107
        FreeCADGui.Control.closeDialog()
108
        FreeCADGui.ActiveDocument.resetEdit()
109
        FreeCAD.ActiveDocument.recompute()
110

111
    def onFileChanged(self,filename):
112

113
        pat = self.form.Pattern.currentText()
114
        self.form.Pattern.clear()
115
        patterns = self.getPatterns(filename)
116
        self.form.Pattern.addItems(patterns)
117
        if pat in patterns:
118
            self.form.Pattern.setCurrentText(pat)
119

120
    def getPatterns(self,filename):
121

122
        """returns a list of pattern names found in a PAT file"""
123
        patterns = []
124
        if os.path.exists(filename):
125
            with open(filename) as patfile:
126
                for line in patfile:
127
                    if line.startswith("*"):
128
                        patterns.append(line.split(",")[0][1:])
129
        return patterns
130

131
if FreeCAD.GuiUp:
132
    import FreeCADGui
133
    FreeCADGui.addCommand("Draft_Hatch",Draft_Hatch())
134

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.