Skip to content

Commit

Permalink
Add color selection for unfold sketches (issue #41)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaise committed Sep 15, 2018
1 parent 33a8fb7 commit 02e5d0e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ http://theseger.com/projects/2015/06/sheet-metal-addon-for-freecad/
Starting from FreeCAD 0.17 it can be installed via the [Addon Manager](https://github.com/FreeCAD/FreeCAD-addons) (from Tools menu)

#### Release notes:
* V0.2.02 15 Sep 2018: Add color selection for unfold sketches (issue #41)
* V0.2.01 15 Sep 2018: Fix bug when not generating sketch (issue #42). Support separate color for bend lines (issue #41)
* V0.2.00 04 Sep 2018: Make SheetMetal compatible with Python 3 and QT 5
* V0.1.40 20 Aug 2018: Merge Ulrich's V20 unfolder script - supports many more sheet metal cases and more robust
Expand Down
80 changes: 68 additions & 12 deletions SheetMetalUnfolder.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ def main():
import Drawing
from SheetMetalCmd import iconPath

genSketchChecked = True
sepSketchChecked = False
genSketchChecked = True
genSketchColor = '#000080'
bendSketchChecked = False
bendSketchColor = '#c00000'
genObjTransparency = 70
manKFactor = -100.0

Expand Down Expand Up @@ -2110,6 +2112,40 @@ def _translate(context, text, disambig):
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)


class QColorButton(QtGui.QPushButton):
'''
Custom Qt Widget to show a chosen color.
'''

def __init__(self, *args, **kwargs):
super(QColorButton, self).__init__(*args, **kwargs)
self._color = None
self.setMaximumWidth(32)
self.pressed.connect(self.onColorPicker)

def setColor(self, color):
if color != self._color:
self._color = color
FreeCAD.Console.PrintLog("Set color " + self._color + "\n")
if self._color is not None:
self.setStyleSheet("background-color: %s;" % self._color)
else:
self.setStyleSheet("")

def color(self):
return self._color

def colorF(self):
return QtGui.QColor(self._color).getRgbF()

def onColorPicker(self):
dlg = QtGui.QColorDialog()
if self._color:
dlg.setCurrentColor(QtGui.QColor('self._color'))
if dlg.exec_():
self.setColor(dlg.currentColor().name())

class SMUnfoldTaskPanel:
'''A TaskPanel for the facebinder'''
def __init__(self):
Expand All @@ -2122,13 +2158,28 @@ def __init__(self):
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.horizontalLayout_1 = QtGui.QHBoxLayout()
self.checkSketch = QtGui.QCheckBox(self.form)
self.checkSketch.setObjectName(_fromUtf8("checkSketch"))
self.verticalLayout.addWidget(self.checkSketch)
self.checkSketch.stateChanged.connect(self.checkSketchChange)
self.horizontalLayout_1.addWidget(self.checkSketch)
self.genColor = QColorButton()
self.genColor.setMaximumHeight(self.checkSketch.height() * 2 / 3)
self.genColor.setColor(genSketchColor)
self.horizontalLayout_1.addWidget(self.genColor)
self.verticalLayout.addLayout(self.horizontalLayout_1)

self.horizontalLayout_3 = QtGui.QHBoxLayout()
self.checkSeparate = QtGui.QCheckBox(self.form)
self.checkSeparate.setObjectName(_fromUtf8("checkSeparate"))
self.verticalLayout.addWidget(self.checkSeparate)
self.checkSeparate.stateChanged.connect(self.checkSketchChange)
self.horizontalLayout_3.addWidget(self.checkSeparate)
self.bendColor = QColorButton()
self.bendColor.setMaximumHeight(self.checkSketch.height() * 2 / 3)
self.bendColor.setColor(bendSketchColor)
self.horizontalLayout_3.addWidget(self.bendColor)
self.verticalLayout.addLayout(self.horizontalLayout_3)

self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setSizeConstraint(QtGui.QLayout.SetDefaultConstraint)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
Expand Down Expand Up @@ -2179,7 +2230,7 @@ def __init__(self):

if genSketchChecked:
self.checkSketch.setCheckState(QtCore.Qt.CheckState.Checked)
if sepSketchChecked:
if bendSketchChecked:
self.checkSeparate.setCheckState(QtCore.Qt.CheckState.Checked)
if manKFactor < -9.0:
self.kFactSpin.setEnabled(False)
Expand Down Expand Up @@ -2218,9 +2269,12 @@ def getStandardButtons(self):
return int(QtGui.QDialogButtonBox.Ok)

def accept(self):
global genSketchChecked, sepSketchChecked, genObjTransparency, manKFactor
global genSketchChecked, bendSketchChecked, genObjTransparency, manKFactor
global genSketchColor, bendSketchColor
genSketchChecked = self.checkSketch.isChecked()
sepSketchChecked = self.checkSeparate.isChecked()
genSketchColor = self.genColor.color()
bendSketchChecked = self.checkSeparate.isChecked()
bendSketchColor = self.bendColor.color()
if self.checkKfact.isChecked():
manKFactor = self.kFactSpin.value()
else:
Expand All @@ -2247,13 +2301,13 @@ def accept(self):
if len(foldLines) > 0:
co = Part.makeCompound(foldLines)
grp2 = Drawing.projectEx(co, norm)
if not sepSketchChecked:
if not bendSketchChecked:
edges.append(grp2[0])
self.generateSketch(edges, "Unfold_Sketch", (0.0,0.0,0.5))
if len(foldLines) > 0 and sepSketchChecked:
self.generateSketch(edges, "Unfold_Sketch", self.genColor.colorF())
if len(foldLines) > 0 and bendSketchChecked:
foldEdges = []
foldEdges.append(grp2[0])
self.generateSketch(foldEdges, "Unfold_Sketch_bends", (0.5,0.0,0.0))
self.generateSketch(foldEdges, "Unfold_Sketch_bends", self.bendColor.colorF())
doc.commitTransaction()
docG = FreeCADGui.ActiveDocument
docG.getObject(a.Name).Transparency = genObjTransparency
Expand Down Expand Up @@ -2283,11 +2337,13 @@ def checkKfactChange(self):

def checkSketchChange(self):
self.checkSeparate.setEnabled(self.checkSketch.isChecked())
#self.genColor.setEnabled(self.checkSketch.isChecked())
#self.bendColor.setEnabled(self.checkSketch.isChecked() and self.checkSeparate.isChecked())

def retranslateUi(self):
self.form.setWindowTitle(_translate("SheetMetal", "Unfold sheet metal onject", None))
self.checkSketch.setText(_translate("SheetMetal", "Generate projection sketch", None))
self.checkSeparate.setText(_translate("SheetMetal", "Separate projection layers", None))
self.checkSeparate.setText(_translate("SheetMetal", "Separate bend lines sketch", None))
self.checkKfact.setText(_translate("SheetMetal", "Manual K-factor", None))
self.label.setText(_translate("SheetMetal", "Unfold object transparency", None))
self.transSpin.setSuffix(_translate("SheetMetal", "%", None))
Expand Down

0 comments on commit 02e5d0e

Please sign in to comment.