Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import FreeCAD,FreeCADGui
- import FreeCADGui as Gui
- from PySide import QtCore,QtGui,QtSvg
- import Draft
- from Draft import _ViewProviderDraft, _DraftObject
- class FacebinderTaskPanel:
- '''A TaskPanel for the facebinder'''
- def __init__(self):
- self.obj = None
- self.form = QtGui.QWidget()
- self.form.setObjectName("FacebinderTaskPanel")
- self.grid = QtGui.QGridLayout(self.form)
- self.grid.setObjectName("grid")
- self.title = QtGui.QLabel(self.form)
- self.grid.addWidget(self.title, 0, 0, 1, 2)
- # tree
- self.tree = QtGui.QTreeWidget(self.form)
- self.grid.addWidget(self.tree, 1, 0, 1, 2)
- self.tree.setColumnCount(2)
- self.tree.setHeaderLabels(["Name","Subelement"])
- # buttons
- self.addButton = QtGui.QPushButton(self.form)
- self.addButton.setObjectName("addButton")
- self.addButton.setIcon(QtGui.QIcon(":/icons/Arch_Add.svg"))
- self.grid.addWidget(self.addButton, 3, 0, 1, 1)
- self.delButton = QtGui.QPushButton(self.form)
- self.delButton.setObjectName("delButton")
- self.delButton.setIcon(QtGui.QIcon(":/icons/Arch_Remove.svg"))
- self.grid.addWidget(self.delButton, 3, 1, 1, 1)
- QtCore.QObject.connect(self.addButton, QtCore.SIGNAL("clicked()"), self.addElement)
- QtCore.QObject.connect(self.delButton, QtCore.SIGNAL("clicked()"), self.removeElement)
- self.update()
- def isAllowedAlterSelection(self):
- return True
- def isAllowedAlterView(self):
- return True
- def getStandardButtons(self):
- return int(QtGui.QDialogButtonBox.Ok)
- def update(self):
- 'fills the treewidget'
- self.tree.clear()
- if self.obj:
- for f in self.obj.Faces:
- item = QtGui.QTreeWidgetItem(self.tree)
- item.setText(0,f[0].Name)
- item.setIcon(0,QtGui.QIcon(":/icons/Tree_Part.svg"))
- item.setText(1,f[1])
- self.retranslateUi(self.form)
- def addElement(self):
- if self.obj:
- for sel in FreeCADGui.Selection.getSelectionEx():
- if sel.HasSubObjects:
- obj = sel.Object
- for elt in sel.SubElementNames:
- if "Face" in elt:
- flist = self.obj.Faces
- found = False
- for face in flist:
- if (face[0] == obj.Name) and (face[1] == elt):
- found = True
- if not found:
- flist.append((obj,elt))
- self.obj.Faces = flist
- FreeCAD.ActiveDocument.recompute()
- self.update()
- def removeElement(self):
- if self.obj:
- it = self.tree.currentItem()
- if it:
- obj = FreeCAD.ActiveDocument.getObject(str(it.text(0)))
- elt = str(it.text(1))
- flist = []
- for face in self.obj.Faces:
- if (face[0].Name != obj.Name) or (face[1] != elt):
- flist.append(face)
- self.obj.Faces = flist
- FreeCAD.ActiveDocument.recompute()
- self.update()
- def accept(self):
- FreeCAD.ActiveDocument.recompute()
- FreeCADGui.ActiveDocument.resetEdit()
- return True
- def retranslateUi(self, TaskPanel):
- TaskPanel.setWindowTitle(QtGui.QApplication.translate("draft", "Faces", None, QtGui.QApplication.UnicodeUTF8))
- self.delButton.setText(QtGui.QApplication.translate("draft", "Remove", None, QtGui.QApplication.UnicodeUTF8))
- self.addButton.setText(QtGui.QApplication.translate("draft", "Add", None, QtGui.QApplication.UnicodeUTF8))
- self.title.setText(QtGui.QApplication.translate("draft", "Facebinder elements", None, QtGui.QApplication.UnicodeUTF8))
- def makeFacebinder(selectionset,name="Facebinder"):
- """makeFacebinder(selectionset,[name]): creates a Facebinder object from a selection set.
- Only faces will be added."""
- if not isinstance(selectionset,list):
- selectionset = [selectionset]
- fb = FreeCAD.ActiveDocument.addObject("Part::FeaturePython",name)
- _Facebinder(fb)
- if gui:
- _ViewProviderFacebinder(fb.ViewObject)
- faces = []
- fb.Proxy.addSubobjects(fb,selectionset)
- return fb
- class _Facebinder(_DraftObject):
- "The Draft Facebinder object"
- def __init__(self,obj):
- _DraftObject.__init__(self,obj,"Facebinder")
- obj.addProperty("App::PropertyLinkSubList","Faces","Draft","Linked faces")
- def execute(self,obj):
- pl = obj.Placement
- if not obj.Faces:
- return
- faces = []
- for f in obj.Faces:
- if "Face" in f[1]:
- try:
- fnum = int(f[1][4:])-1
- faces.append(f[0].Shape.Faces[fnum])
- except(IndexError,Part.OCCError):
- print("Draft: wrong face index")
- return
- if not faces:
- return
- import Part
- try:
- if len(faces) > 1:
- sh = faces.pop()
- sh = sh.multiFuse(faces)
- sh = sh.removeSplitter()
- else:
- sh = faces[0]
- sh.transformShape(sh.Matrix, True)
- except Part.OCCError:
- print("Draft: error building facebinder")
- return
- obj.Shape = sh
- obj.Placement = pl
- def addSubobjects(self,obj,facelinks):
- "adds facelinks to this facebinder"
- objs = obj.Faces
- for o in facelinks:
- if isinstance(o,tuple) or isinstance(o,list):
- if o[0].Name != obj.Name:
- objs.append(tuple(o))
- else:
- for el in o.SubElementNames:
- if "Face" in el:
- if o.Object.Name != obj.Name:
- objs.append((o.Object,el))
- obj.Faces = objs
- self.execute(obj)
- class _ViewProviderFacebinder(_ViewProviderDraft):
- def __init__(self,vobj):
- _ViewProviderDraft.__init__(self,vobj)
- def setEdit(self,vobj,mode):
- import DraftGui
- taskd = DraftGui.FacebinderTaskPanel()
- taskd.obj = vobj.Object
- taskd.update()
- FreeCADGui.Control.showDialog(taskd)
- return True
- def unsetEdit(self,vobj,mode):
- FreeCADGui.Control.closeDialog()
- return False
- '''
- selectionset=Gui.Selection.getSelectionEx()
- gui=True
- makeFacebinder(selectionset,name="Facebinder")
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement