Advertisement
danfalck

PointClass.py

Dec 16th, 2011
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.22 KB | None | 0 0
  1. import FreeCAD, Part
  2. from pivy.coin import *
  3. from pivy import coin
  4.  
  5. class _Point:
  6.     def __init__(self, obj,x,y,z):
  7.         obj.addProperty("App::PropertyFloat","x_pt","Point","Location").x_pt = x
  8.         obj.addProperty("App::PropertyFloat","y_pt","Point","Location").y_pt = y
  9.         obj.addProperty("App::PropertyFloat","z_pt","Point","Location").z_pt = z
  10.         obj.Proxy = self
  11.  
  12.     def execute(self, obj):
  13.         if obj.ViewObject:
  14.             obj.ViewObject.update()
  15.  
  16. class _ViewProviderPoint:
  17.  
  18.     def __init__(self, obj):
  19.         obj.addProperty("App::PropertyColor","PointColor","Base","Point color")
  20.         obj.addProperty("App::PropertyInteger", "PointSize", "Base", "Point size")
  21.         obj.Proxy = self
  22.  
  23.     def __del__(self):
  24.         FreeCADGui.ActiveDocument.ActiveView.getSceneGraph().removeChild(self.selnode)
  25.  
  26.     def attach(self, obj):
  27.         self.material = coin.SoMaterial()
  28.         self.color = coin.SoBaseColor()
  29.         self.style=coin.SoDrawStyle()
  30.  
  31.         self.myPoint = coin.SoPointSet()
  32.         self.selnode=coin.SoType.fromName("SoFCSelection").createInstance()
  33.         self.selnode.documentName.setValue(FreeCAD.ActiveDocument.Name)
  34.         self.selnode.objectName.setValue(obj.Object.Name)
  35.         self.selnode.subElementName.setValue("Point")
  36.         self.node=coin.SoSeparator()
  37.         self.coords = coin.SoCoordinate3()
  38.         self.node.addChild(self.coords)
  39.         self.node.addChild(self.material)
  40.         self.node.addChild(self.color)
  41.         self.node.addChild(self.style)
  42.         self.node.addChild(self.myPoint)
  43.         self.selnode.addChild(self.node)
  44.         FreeCADGui.ActiveDocument.ActiveView.getSceneGraph().addChild(self.selnode)
  45.  
  46.     def updateData(self, fp, prop):
  47.         self.coords.point.setValue(fp.x_pt,fp.y_pt,fp.z_pt)
  48.  
  49.     def onChanged(self, vp, prop):
  50.         c = vp.getPropertyByName("PointColor")
  51.         self.color.rgb.setValue(c[0],c[1],c[2])
  52.         self.style.pointSize =vp.getPropertyByName("PointSize")
  53.         if vp.getPropertyByName("Visibility")== False:
  54.             self.material.transparency.setValue(1.0)
  55.         else:
  56.             self.material.transparency.setValue(.1)
  57.  
  58.     def getIcon(self):
  59.         return """
  60.            /* XPM */
  61.            static char * test_xpm[] = {
  62.            "16 16 2 1",
  63.             "   c #FFFFFF",
  64.             ".  c #000000",
  65.            "                ",
  66.            "                ",
  67.            "                ",
  68.            "                ",
  69.            "                ",
  70.            "      ..        ",
  71.            "     ....       ",
  72.            "    ......      ",
  73.            "     ....       ",
  74.            "      ..        ",
  75.            "                ",
  76.            "                ",
  77.            "                ",
  78.            "                ",
  79.            "                ",
  80.            "                "};
  81.            """
  82.  
  83.     def __getstate__(self):
  84.  
  85.         return None
  86.  
  87.  
  88.  
  89.     def __setstate__(self,state):
  90.  
  91.         return None
  92.  
  93. def makePoint(x_pt=0, y_pt=0, z_pt=0,c=(0,1,0),point_size= 5):
  94.     ''' make a point (at coordinates x,y,z ,color(r,g,b),point_size)
  95.        example usage:
  96.        p1 = makePoint()
  97.        p1.ViewObject.Visibility= False # make it invisible
  98.        p1.ViewObject.Visibility= True  # make it visible
  99.        p1 = makePoint(-1,0,0) #make a point at -1,0,0
  100.        p1 = makePoint(1,0,0,(1,0,0)) # color = red
  101.        p1.x_pt = 1 #move it in x
  102.        p1.ViewObject.PointColor =(0.0,0.0,1.0) #change the color-make sure values are floats
  103.    '''
  104.     obj=FreeCAD.ActiveDocument.addObject("App::FeaturePython","Point")
  105.     _Point(obj,x_pt,y_pt,z_pt)
  106.     _ViewProviderPoint(obj.ViewObject)
  107.     obj.ViewObject.PointColor = (float(c[0]), float(c[1]), float(c[2]))
  108.     obj.ViewObject.PointSize = point_size
  109.     obj.ViewObject.Visibility = True
  110.     FreeCAD.ActiveDocument.recompute()
  111.     return obj
  112.  
  113. ''' example usage:
  114. p1 = makePoint()
  115. p1.ViewObject.Visibility= False # make it invisible
  116. p1.ViewObject.Visibility= True  # make it visible
  117.  
  118. p2 = makePoint(-1,0,0)
  119. p3 = makePoint(1,0,0,(1,0,0)) # color = red
  120. p4 = makePoint()
  121. p4.x_pt = 1 #move it in x
  122. p5 = makePoint()
  123. p5.ViewObject.PointColor =(0.0,0.0,1.0) #change the color
  124. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement