FreeCAD
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
26import os27import FreeCAD28from draftguitools import gui_base29from draftutils import params30from draftutils.translate import QT_TRANSLATE_NOOP, translate31
32
33class Draft_Hatch(gui_base.GuiCommandSimplest):34
35
36def GetResources(self):37
38return {'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
43def Activated(self):44
45import FreeCADGui46
47if FreeCADGui.Selection.getSelection():48FreeCADGui.Control.showDialog(Draft_Hatch_TaskPanel(FreeCADGui.Selection.getSelection()[0]))49else:50FreeCAD.Console.PrintError(translate("Draft","You must choose a base object before using this command")+"\n")51
52
53class Draft_Hatch_TaskPanel:54
55
56def __init__(self,baseobj):57
58import FreeCADGui59from PySide import QtCore,QtGui60import Draft_rc61
62self.baseobj = baseobj63self.form = FreeCADGui.PySideUic.loadUi(":/ui/dialogHatch.ui")64self.form.setWindowIcon(QtGui.QIcon(":/icons/Draft_Hatch.svg"))65self.form.File.fileNameChanged.connect(self.onFileChanged)66self.form.File.setFileName(params.get_param("FilePattern", path="Mod/TechDraw/PAT"))67pat = params.get_param("NamePattern", path="Mod/TechDraw/PAT")68if pat in [self.form.Pattern.itemText(i) for i in range(self.form.Pattern.count())]:69self.form.Pattern.setCurrentText(pat)70self.form.Scale.setValue(params.get_param("HatchPatternScale"))71self.form.Rotation.setValue(params.get_param("HatchPatternRotation"))72
73def accept(self):74
75import FreeCADGui76
77params.set_param("FilePattern", self.form.File.property("fileName"), path="Mod/TechDraw/PAT")78params.set_param("NamePattern", self.form.Pattern.currentText(), path="Mod/TechDraw/PAT")79params.set_param("HatchPatternScale", self.form.Scale.value())80params.set_param("HatchPatternRotation", self.form.Rotation.value())81if hasattr(self.baseobj,"File") and hasattr(self.baseobj,"Pattern"):82# modify existing hatch object83o = "FreeCAD.ActiveDocument.getObject(\""+self.baseobj.Name+"\")"84FreeCADGui.doCommand(o+".File=\""+self.form.File.property("fileName")+"\"")85FreeCADGui.doCommand(o+".Pattern=\""+self.form.Pattern.currentText()+"\"")86FreeCADGui.doCommand(o+".Scale="+str(self.form.Scale.value()))87FreeCADGui.doCommand(o+".Rotation="+str(self.form.Rotation.value()))88else:89# create new hatch object90FreeCAD.ActiveDocument.openTransaction("Create Hatch")91FreeCADGui.addModule("Draft")92cmd = "Draft.make_hatch("93cmd += "baseobject=FreeCAD.ActiveDocument.getObject(\""+self.baseobj.Name94cmd += "\"),filename=\""+self.form.File.property("fileName")95cmd += "\",pattern=\""+self.form.Pattern.currentText()96cmd += "\",scale="+str(self.form.Scale.value())97cmd += ",rotation="+str(self.form.Rotation.value())+")"98FreeCADGui.doCommand(cmd)99FreeCAD.ActiveDocument.commitTransaction()100FreeCADGui.doCommand("FreeCAD.ActiveDocument.recompute()")101self.reject()102
103def reject(self):104
105import FreeCADGui106
107FreeCADGui.Control.closeDialog()108FreeCADGui.ActiveDocument.resetEdit()109FreeCAD.ActiveDocument.recompute()110
111def onFileChanged(self,filename):112
113pat = self.form.Pattern.currentText()114self.form.Pattern.clear()115patterns = self.getPatterns(filename)116self.form.Pattern.addItems(patterns)117if pat in patterns:118self.form.Pattern.setCurrentText(pat)119
120def getPatterns(self,filename):121
122"""returns a list of pattern names found in a PAT file"""123patterns = []124if os.path.exists(filename):125with open(filename) as patfile:126for line in patfile:127if line.startswith("*"):128patterns.append(line.split(",")[0][1:])129return patterns130
131if FreeCAD.GuiUp:132import FreeCADGui133FreeCADGui.addCommand("Draft_Hatch",Draft_Hatch())134