Advertisement
danfalck

facebinder_taskpanel.py

Feb 6th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.78 KB | None | 0 0
  1. import FreeCAD,FreeCADGui
  2. import FreeCADGui as Gui
  3.  
  4. from PySide import QtCore,QtGui,QtSvg
  5.  
  6. import Draft
  7. from Draft import _ViewProviderDraft, _DraftObject
  8.  
  9. class FacebinderTaskPanel:
  10.     '''A TaskPanel for the facebinder'''
  11.     def __init__(self):
  12.        
  13.         self.obj = None
  14.         self.form = QtGui.QWidget()
  15.         self.form.setObjectName("FacebinderTaskPanel")
  16.         self.grid = QtGui.QGridLayout(self.form)
  17.         self.grid.setObjectName("grid")
  18.         self.title = QtGui.QLabel(self.form)
  19.         self.grid.addWidget(self.title, 0, 0, 1, 2)
  20.  
  21.         # tree
  22.         self.tree = QtGui.QTreeWidget(self.form)
  23.         self.grid.addWidget(self.tree, 1, 0, 1, 2)
  24.         self.tree.setColumnCount(2)
  25.         self.tree.setHeaderLabels(["Name","Subelement"])
  26.  
  27.         # buttons
  28.         self.addButton = QtGui.QPushButton(self.form)
  29.         self.addButton.setObjectName("addButton")
  30.         self.addButton.setIcon(QtGui.QIcon(":/icons/Arch_Add.svg"))
  31.         self.grid.addWidget(self.addButton, 3, 0, 1, 1)
  32.  
  33.         self.delButton = QtGui.QPushButton(self.form)
  34.         self.delButton.setObjectName("delButton")
  35.         self.delButton.setIcon(QtGui.QIcon(":/icons/Arch_Remove.svg"))
  36.         self.grid.addWidget(self.delButton, 3, 1, 1, 1)
  37.  
  38.         QtCore.QObject.connect(self.addButton, QtCore.SIGNAL("clicked()"), self.addElement)
  39.         QtCore.QObject.connect(self.delButton, QtCore.SIGNAL("clicked()"), self.removeElement)
  40.         self.update()
  41.  
  42.     def isAllowedAlterSelection(self):
  43.         return True
  44.  
  45.     def isAllowedAlterView(self):
  46.         return True
  47.  
  48.     def getStandardButtons(self):
  49.         return int(QtGui.QDialogButtonBox.Ok)
  50.  
  51.     def update(self):
  52.         'fills the treewidget'
  53.         self.tree.clear()
  54.         if self.obj:
  55.             for f in self.obj.Faces:
  56.                 item = QtGui.QTreeWidgetItem(self.tree)
  57.                 item.setText(0,f[0].Name)
  58.                 item.setIcon(0,QtGui.QIcon(":/icons/Tree_Part.svg"))
  59.                 item.setText(1,f[1])
  60.         self.retranslateUi(self.form)
  61.  
  62.     def addElement(self):
  63.         if self.obj:
  64.             for sel in FreeCADGui.Selection.getSelectionEx():
  65.                 if sel.HasSubObjects:
  66.                     obj = sel.Object
  67.                     for elt in sel.SubElementNames:
  68.                         if "Face" in elt:
  69.                             flist = self.obj.Faces
  70.                             found = False
  71.                             for face in flist:
  72.                                 if (face[0] == obj.Name) and (face[1] == elt):
  73.                                     found = True
  74.                             if not found:
  75.                                 flist.append((obj,elt))
  76.                                 self.obj.Faces = flist
  77.                                 FreeCAD.ActiveDocument.recompute()
  78.             self.update()
  79.  
  80.     def removeElement(self):
  81.         if self.obj:
  82.             it = self.tree.currentItem()
  83.             if it:
  84.                 obj = FreeCAD.ActiveDocument.getObject(str(it.text(0)))
  85.                 elt = str(it.text(1))
  86.                 flist = []
  87.                 for face in self.obj.Faces:
  88.                     if (face[0].Name != obj.Name) or (face[1] != elt):
  89.                         flist.append(face)
  90.                 self.obj.Faces = flist
  91.                 FreeCAD.ActiveDocument.recompute()
  92.             self.update()
  93.  
  94.     def accept(self):
  95.         FreeCAD.ActiveDocument.recompute()
  96.         FreeCADGui.ActiveDocument.resetEdit()
  97.         return True
  98.  
  99.     def retranslateUi(self, TaskPanel):
  100.         TaskPanel.setWindowTitle(QtGui.QApplication.translate("draft", "Faces", None, QtGui.QApplication.UnicodeUTF8))
  101.         self.delButton.setText(QtGui.QApplication.translate("draft", "Remove", None, QtGui.QApplication.UnicodeUTF8))
  102.         self.addButton.setText(QtGui.QApplication.translate("draft", "Add", None, QtGui.QApplication.UnicodeUTF8))
  103.         self.title.setText(QtGui.QApplication.translate("draft", "Facebinder elements", None, QtGui.QApplication.UnicodeUTF8))
  104.  
  105.  
  106.  
  107. def makeFacebinder(selectionset,name="Facebinder"):
  108.     """makeFacebinder(selectionset,[name]): creates a Facebinder object from a selection set.
  109.    Only faces will be added."""
  110.     if not isinstance(selectionset,list):
  111.         selectionset = [selectionset]
  112.     fb = FreeCAD.ActiveDocument.addObject("Part::FeaturePython",name)
  113.     _Facebinder(fb)
  114.     if gui:
  115.         _ViewProviderFacebinder(fb.ViewObject)
  116.     faces = []
  117.     fb.Proxy.addSubobjects(fb,selectionset)
  118.     return fb
  119.  
  120. class _Facebinder(_DraftObject):
  121.     "The Draft Facebinder object"
  122.     def __init__(self,obj):
  123.         _DraftObject.__init__(self,obj,"Facebinder")
  124.         obj.addProperty("App::PropertyLinkSubList","Faces","Draft","Linked faces")
  125.  
  126.     def execute(self,obj):
  127.         pl = obj.Placement
  128.         if not obj.Faces:
  129.             return
  130.         faces = []
  131.         for f in obj.Faces:
  132.             if "Face" in f[1]:
  133.                 try:
  134.                     fnum = int(f[1][4:])-1
  135.                     faces.append(f[0].Shape.Faces[fnum])
  136.                 except(IndexError,Part.OCCError):
  137.                     print("Draft: wrong face index")
  138.                     return
  139.         if not faces:
  140.             return
  141.         import Part
  142.         try:
  143.             if len(faces) > 1:
  144.                 sh = faces.pop()
  145.                 sh = sh.multiFuse(faces)
  146.                 sh = sh.removeSplitter()
  147.             else:
  148.                 sh = faces[0]
  149.                 sh.transformShape(sh.Matrix, True)
  150.         except Part.OCCError:
  151.             print("Draft: error building facebinder")
  152.             return
  153.         obj.Shape = sh
  154.         obj.Placement = pl
  155.        
  156.     def addSubobjects(self,obj,facelinks):
  157.         "adds facelinks to this facebinder"
  158.         objs = obj.Faces
  159.         for o in facelinks:
  160.             if isinstance(o,tuple) or isinstance(o,list):
  161.                 if o[0].Name != obj.Name:
  162.                     objs.append(tuple(o))
  163.             else:
  164.                 for el in o.SubElementNames:
  165.                     if "Face" in el:
  166.                         if o.Object.Name != obj.Name:
  167.                             objs.append((o.Object,el))
  168.         obj.Faces = objs
  169.         self.execute(obj)
  170.        
  171.        
  172. class _ViewProviderFacebinder(_ViewProviderDraft):
  173.     def __init__(self,vobj):
  174.         _ViewProviderDraft.__init__(self,vobj)
  175.        
  176.     def setEdit(self,vobj,mode):
  177.         import DraftGui
  178.         taskd = DraftGui.FacebinderTaskPanel()
  179.         taskd.obj = vobj.Object
  180.         taskd.update()
  181.         FreeCADGui.Control.showDialog(taskd)
  182.         return True
  183.  
  184.     def unsetEdit(self,vobj,mode):
  185.         FreeCADGui.Control.closeDialog()
  186.         return False
  187.  
  188. '''
  189. selectionset=Gui.Selection.getSelectionEx()
  190. gui=True
  191. makeFacebinder(selectionset,name="Facebinder")
  192. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement