File size: 6,929 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | # SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# * *
# * Copyright (c) 2021 Yorik van Havre <yorik@uncreated.net> *
# * *
# * 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 *
# * *
# ***************************************************************************
"""This module contains FreeCAD commands for the Draft workbench"""
import os
import FreeCAD
from draftguitools import gui_base
from draftutils import params
from draftutils.todo import todo
from draftutils.translate import QT_TRANSLATE_NOOP, translate
class Draft_Hatch(gui_base.GuiCommandNeedsSelection):
def GetResources(self):
return {
"Pixmap": "Draft_Hatch",
"MenuText": QT_TRANSLATE_NOOP("Draft_Hatch", "Hatch"),
"Accel": "H, A",
"ToolTip": QT_TRANSLATE_NOOP(
"Draft_Hatch", "Creates hatches on the faces of a selected object"
),
}
def Activated(self):
import FreeCADGui
if FreeCADGui.Selection.getSelection():
task = FreeCADGui.Control.showDialog(
Draft_Hatch_TaskPanel(FreeCADGui.Selection.getSelection()[0])
)
task.setDocumentName(FreeCADGui.ActiveDocument.Document.Name)
task.setAutoCloseOnDeletedDocument(True)
else:
FreeCAD.Console.PrintError(
translate("Draft", "Choose a base object before using this command") + "\n"
)
class Draft_Hatch_TaskPanel:
def __init__(self, baseobj):
import FreeCADGui
from PySide import QtCore, QtGui
import Draft_rc
self.baseobj = baseobj
self.form = FreeCADGui.PySideUic.loadUi(":/ui/dialogHatch.ui")
self.form.setWindowIcon(QtGui.QIcon(":/icons/Draft_Hatch.svg"))
self.form.File.fileNameChanged.connect(self.onFileChanged)
self.form.File.setFileName(params.get_param("HatchPatternFile"))
pat = params.get_param("HatchPatternName")
if pat in [self.form.Pattern.itemText(i) for i in range(self.form.Pattern.count())]:
self.form.Pattern.setCurrentText(pat)
self.form.Scale.setValue(params.get_param("HatchPatternScale"))
self.form.Rotation.setValue(params.get_param("HatchPatternRotation"))
self.form.Translate.setChecked(params.get_param("HatchPatternTranslate"))
todo.delay(self.form.setFocus, None) # Make sure using Esc works.
def accept(self):
import FreeCADGui
params.set_param("HatchPatternFile", self.form.File.property("fileName"))
params.set_param("HatchPatternName", self.form.Pattern.currentText())
params.set_param("HatchPatternScale", self.form.Scale.value())
params.set_param("HatchPatternRotation", self.form.Rotation.value())
params.set_param("HatchPatternTranslate", self.form.Translate.isChecked())
if hasattr(self.baseobj, "File") and hasattr(self.baseobj, "Pattern"):
# modify existing hatch object
o = 'FreeCAD.ActiveDocument.getObject("' + self.baseobj.Name + '")'
FreeCADGui.doCommand(o + '.File="' + self.form.File.property("fileName") + '"')
FreeCADGui.doCommand(o + '.Pattern="' + self.form.Pattern.currentText() + '"')
FreeCADGui.doCommand(o + ".Scale=" + str(self.form.Scale.value()))
FreeCADGui.doCommand(o + ".Rotation=" + str(self.form.Rotation.value()))
FreeCADGui.doCommand(o + ".Translate=" + str(self.form.Translate.isChecked()))
else:
# create new hatch object
FreeCAD.ActiveDocument.openTransaction("Create Hatch")
FreeCADGui.addModule("Draft")
cmd = "Draft.make_hatch("
cmd += 'baseobject=FreeCAD.ActiveDocument.getObject("' + self.baseobj.Name
cmd += '"),filename="' + self.form.File.property("fileName")
cmd += '",pattern="' + self.form.Pattern.currentText()
cmd += '",scale=' + str(self.form.Scale.value())
cmd += ",rotation=" + str(self.form.Rotation.value())
cmd += ",translate=" + str(self.form.Translate.isChecked()) + ")"
FreeCADGui.doCommand(cmd)
FreeCAD.ActiveDocument.commitTransaction()
FreeCADGui.doCommand("FreeCAD.ActiveDocument.recompute()")
self.reject()
def reject(self):
import FreeCADGui
FreeCADGui.Control.closeDialog()
FreeCADGui.ActiveDocument.resetEdit()
FreeCAD.ActiveDocument.recompute()
def onFileChanged(self, filename):
if filename[0] == "." and self.baseobj:
# File path relative to the FreeCAD file directory.
filename = os.path.join(os.path.dirname(self.baseobj.Document.FileName), filename)
filename = os.path.abspath(filename)
pat = self.form.Pattern.currentText()
self.form.Pattern.clear()
patterns = self.getPatterns(filename)
self.form.Pattern.addItems(patterns)
if pat in patterns:
self.form.Pattern.setCurrentText(pat)
def getPatterns(self, filename):
"""returns a list of pattern names found in a PAT file"""
patterns = []
if os.path.exists(filename):
with open(filename) as patfile:
for line in patfile:
if line.startswith("*"):
patterns.append(line.split(",")[0][1:])
return patterns
if FreeCAD.GuiUp:
import FreeCADGui
FreeCADGui.addCommand("Draft_Hatch", Draft_Hatch())
|