Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.82 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. # ***************************************************************************
  4. # *                                                                         *
  5. # *   Copyright (c) 2017 sliptonic <shopinthewoods@gmail.com>               *
  6. # *                                                                         *
  7. # *   This program is free software; you can redistribute it and/or modify  *
  8. # *   it under the terms of the GNU Lesser General Public License (LGPL)    *
  9. # *   as published by the Free Software Foundation; either version 2 of     *
  10. # *   the License, or (at your option) any later version.                   *
  11. # *   for detail see the LICENCE text file.                                 *
  12. # *                                                                         *
  13. # *   This program is distributed in the hope that it will be useful,       *
  14. # *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  15. # *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  16. # *   GNU Library General Public License for more details.                  *
  17. # *                                                                         *
  18. # *   You should have received a copy of the GNU Library General Public     *
  19. # *   License along with this program; if not, write to the Free Software   *
  20. # *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
  21. # *   USA                                                                   *
  22. # *                                                                         *
  23. # ***************************************************************************
  24. import FreeCAD
  25. import FreeCADGui
  26. import PathScripts.PathLog as PathLog
  27. import PathScripts.PathUtils as PathUtils
  28.  
  29. from PathScripts.PathPreferences import PathPreferences
  30. from PySide import QtCore
  31. from pivy import coin
  32.  
  33. PathLog.setLevel(PathLog.Level.DEBUG, PathLog.thisModule())
  34. PathLog.trackModule()
  35.  
  36. # Qt tanslation handling
  37. def translate(context, text, disambig=None):
  38.     return QtCore.QCoreApplication.translate(context, text, disambig)
  39.  
  40. class HoldingTagMarker:
  41.     def __init__(self, point, colors):
  42.         self.point = point
  43.         self.color = colors
  44.         self.sep = coin.SoSeparator()
  45.         self.pos = coin.SoTranslation()
  46.         self.pos.translation = (point.x, point.y, point.z)
  47.         self.sphere = coin.SoSphere()
  48.         self.scale = coin.SoType.fromName('SoShapeScale').createInstance()
  49.         self.scale.setPart('shape', self.sphere)
  50.         self.scale.scaleFactor.setValue(7)
  51.         self.material = coin.SoMaterial()
  52.         self.sep.addChild(self.pos)
  53.         self.sep.addChild(self.material)
  54.         self.sep.addChild(self.scale)
  55.         self.enabled = True
  56.         self.selected = False
  57.  
  58.     def setSelected(self, select):
  59.         self.selected = select
  60.         self.sphere.radius = 1.5 if select else 1.0
  61.         self.setEnabled(self.enabled)
  62.  
  63.     def setEnabled(self, enabled):
  64.         self.enabled = enabled
  65.         if enabled:
  66.             self.material.diffuseColor = self.color[0] if not self.selected else self.color[2]
  67.             self.material.transparency = 0.0
  68.         else:
  69.             self.material.diffuseColor = self.color[1] if not self.selected else self.color[2]
  70.             self.material.transparency = 0.6
  71.  
  72. class PathDressupTagViewProvider:
  73.  
  74.     def __init__(self, vobj):
  75.         vobj.Proxy = self
  76.         self.vobj = vobj
  77.         self.panel = None
  78.  
  79.     def setupColors(self):
  80.         def colorForColorValue(val):
  81.             v = [((val >> n) & 0xff) / 255. for n in [24, 16, 8, 0]]
  82.             return coin.SbColor(v[0], v[1], v[2])
  83.  
  84.         pref = PathPreferences.preferences()
  85.         #                                                      R         G          B          A
  86.         npc = pref.GetUnsigned('DefaultPathMarkerColor',    (( 85*256 + 255)*256 +   0)*256 + 255)
  87.         hpc = pref.GetUnsigned('DefaultHighlightPathColor', ((255*256 + 125)*256 +   0)*256 + 255)
  88.         dpc = pref.GetUnsigned('DefaultDisabledPathColor',  ((205*256 + 205)*256 + 205)*256 + 154)
  89.         self.colors = [colorForColorValue(npc), colorForColorValue(dpc), colorForColorValue(hpc)]
  90.  
  91.     def attach(self, vobj):
  92.         self.setupColors()
  93.         self.obj = vobj.Object
  94.         self.tags = []
  95.         self.switch = coin.SoSwitch()
  96.         vobj.RootNode.addChild(self.switch)
  97.         self.turnMarkerDisplayOn(False)
  98.  
  99.     def turnMarkerDisplayOn(self, display):
  100.         sw = coin.SO_SWITCH_ALL if display else coin.SO_SWITCH_NONE
  101.         self.switch.whichChild = sw
  102.  
  103.     def claimChildren(self):
  104.         if self.obj and self.obj.Base:
  105.             for i in self.obj.Base.InList:
  106.                 if hasattr(i, 'Group') and self.obj.Base.Name in [o.Name for o in i.Group]:
  107.                     i.Group = [o for o in i.Group if o.Name != self.obj.Base.Name]
  108.             if self.obj.Base.ViewObject:
  109.                 PathLog.info("Setting visibility for %s" % (self.obj.Base.Name))
  110.                 self.obj.Base.ViewObject.Visibility = False
  111.             else:
  112.                 PathLog.info("Ignoring visibility")
  113.             if self.obj.Debug.ViewObject:
  114.                 self.obj.Debug.ViewObject.Visibility = False
  115.             return [self.obj.Base, self.obj.Debug]
  116.         return []
  117.  
  118.     def onDelete(self, arg1=None, arg2=None):
  119.         '''this makes sure that the base operation is added back to the project and visible'''
  120.         obj = FreeCADGui.ActiveDocument.getObject(arg1.Object.Base.Name)
  121.         if obj:
  122.             obj.Visibility = True
  123.         PathUtils.addToJob(arg1.Object.Base)
  124.         arg1.Object.Base = None
  125.         return True
  126.  
  127.     def updateData(self, obj, propName):
  128.         if 'Disabled' == propName:
  129.             for tag in self.tags:
  130.                 self.switch.removeChild(tag.sep)
  131.             tags = []
  132.             for i, p in enumerate(obj.Positions):
  133.                 tag = HoldingTagMarker(p, self.colors)
  134.                 tag.setEnabled(not i in obj.Disabled)
  135.                 tags.append(tag)
  136.                 self.switch.addChild(tag.sep)
  137.             self.tags = tags
  138.  
  139.     def selectTag(self, index):
  140.         PathLog.track(index)
  141.         for i, tag in enumerate(self.tags):
  142.             tag.setSelected(i == index)
  143.  
  144.     def tagAtPoint(self, point):
  145.         p = FreeCAD.Vector(point[0], point[1], point[2])
  146.         for i, tag in enumerate(self.tags):
  147.             if PathGeom.pointsCoincide(p, tag.point, tag.sphere.radius.getValue() * 1.1):
  148.                 return i
  149.         return -1
  150.  
  151.     # SelectionObserver interface
  152.     def allow(self, doc, obj, sub):
  153.         if obj == self.obj:
  154.             return True
  155.         return False
  156.  
  157.     def addSelection(self, doc, obj, sub, point):
  158.         i = self.tagAtPoint(point)
  159.         if self.panel:
  160.             self.panel.selectTagWithId(i)
  161.         FreeCADGui.updateGui()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement