Пример #1
0
 def testPivy(self):
     '''Test Pivy'''
     FreeCAD.Console.PrintLog('Checking Pivy...\n')
     from pivy import coin
     c = coin.SoCube()
     FreeCADGui.ActiveDocument.ActiveView.getSceneGraph().addChild(c)
     self.failUnless(c, "Pivy is not working properly")
Пример #2
0
def shape2shape(name, geom):
    '''
    Convert from gegede shape to a SoShape
    '''
    obj = geom.get_shape(name)
    if type(obj).__name__ == 'Box':
        box = coin.SoCube()
        box.width = 2.0 * obj.dx.to(spatial_units).magnitude
        box.height = 2.0 * obj.dy.to(spatial_units).magnitude
        box.depth = 2.0 * obj.dz.to(spatial_units).magnitude
        return box

    if type(obj).__name__ == 'Tubs':
        tub = coin.SoCylinder()
        tub.radius = obj.rmax.to(spatial_units).magnitude
        tub.height = 0.5 * obj.dz.to(spatial_units).magnitude
        # fixme: no built-in support of rmin, nor start/stop angles - need to make boolean
        return tub

    if type(obj).__name__ == 'Sphere':
        sph = coin.SoSphere()
        sph.radius = obj.rmax.to(spatial_units).magnitude
        # fixme: no built-in support of rmin, nor start/stop angles - need to make boolean
        return sph

    if type(obj).__name__ == 'Boolean':
        print(
            'WARNING: Boolean shapes are not supported, simply using the first shape for %s'
            % obj.name)
        return shape2shape(obj.first, geom)

    raise ValueError('Unsupported shape for scenegraph: "%s"' %
                     type(obj).__name__)
Пример #3
0
def render(outputfile,scene=None,camera=None,zoom=False,width=400,height=300,background=(1.0,1.0,1.0)):

    """render(outputfile,scene=None,camera=None,zoom=False,width=400,height=300,background=(1.0,1.0,1.0)):
    Renders a PNG image of given width and height and background color from the given coin scene, using
    the given coin camera (ortho or perspective). If zoom is True the camera will be resized to fit all
    objects. The outputfile must be a file path to save a png image."""

    # On Linux, the X server must have indirect rendering enabled in order to be able to do offline
    # PNG rendering. Unfortunately, this is turned off by default on most recent distros. The easiest
    # way I found is to edit (or create if inexistant) /etc/X11/xorg.conf and add this:
    #
    # Section "ServerFlags"
    #    Option "AllowIndirectGLX" "on"
    #    Option "IndirectGLX" "on"
    # EndSection
    #
    # But there are other ways, google of GLX indirect rendering

    if isinstance(camera,str):
        camera = getCoinCamera(camera)

    print("Starting offline renderer")
    # build an offline scene root separator
    root = coin.SoSeparator()
    # add one light (mandatory)
    light = coin.SoDirectionalLight()
    root.addChild(light)
    if not camera:
        # create a default camera if none was given
        camera = coin.SoPerspectiveCamera()
        cameraRotation = coin.SbRotation.identity()
        cameraRotation *= coin.SbRotation(coin.SbVec3f(1,0,0),-0.4)
        cameraRotation *= coin.SbRotation(coin.SbVec3f(0,1,0), 0.4)
        camera.orientation = cameraRotation
        # make sure all objects get in the view later
        zoom = True
    root.addChild(camera)
    if scene:
        root.addChild(scene)
    else:
        # no scene was given, add a simple cube
        cube = coin.SoCube()
        root.addChild(cube)
    vpRegion = coin.SbViewportRegion(width,height)
    if zoom:
        camera.viewAll(root,vpRegion)
    print("Creating viewport")
    offscreenRenderer = coin.SoOffscreenRenderer(vpRegion)
    offscreenRenderer.setBackgroundColor(coin.SbColor(background[0],background[1],background[2]))
    print("Ready to render")
    # ref ensures that the node will not be garbage-collected during rendering
    root.ref()
    ok = offscreenRenderer.render(root)
    root.unref()
    if ok:
        offscreenRenderer.writeToFile(outputfile,"PNG")
        print("Rendering",outputfile,"done")
    else:
        print("Error rendering image")
Пример #4
0
 def test_pivy_draw(self):
     """Use Coin (pivy.coin) to draw a cube on the active view."""
     import pivy.coin as coin
     cube = coin.SoCube()
     _msg("  Draw cube")
     Gui.ActiveDocument.ActiveView.getSceneGraph().addChild(cube)
     _msg("  Adding cube to the active view scene")
     self.assertTrue(cube, "Pivy is not working properly.")
Пример #5
0
 def update(self, qte=1, line=None, start=False, end=False):
     if qte == 1:
         # mettre la barre au milieu des 2 points
         pass
     if qte > 1:
         for i in range(qte):
             cube = coin.SoCube()
             self.array.addChild(cube)
Пример #6
0
 def __init__(self):
     self.trans = coin.SoTransform()
     w = coin.SoDrawStyle()
     w.style = coin.SoDrawStyle.LINES
     cube = coin.SoCube()
     self.array = coin.SoArray()
     self.array.addChild(cube)
     Tracker.__init__(
         self, children=[self.trans, w, self.array], name="arrayBoxTracker"
     )
Пример #7
0
    def createWidget(self, parent):
        widget = quarter.QuarterWidget(parent, self._firstwidget)

        if not self._firstwidget:
            self._firstwidget = widget
            self.connect(widget, QtCore.SIGNAL("destroyed(QObject*)"), self,
                         QtCore.SLOT("widgetDestroyed(QObject*)"))

        widget.setSceneGraph(coin.SoCube())
        return widget
Пример #8
0
def make_mother_daughter():
    '''
    Make a cube with two cylinders inside.
    '''

    tube_shape = coin.SoCylinder()
    tube_shape.radius = 0.25
    tube_shape.height = 1.5

    tube_mat = coin.SoMaterial()
    tube_mat.ambientColor = (.33, .22, .27)
    tube_mat.diffuseColor = (.78, .57, .11)
    tube_mat.specularColor = (.99, .94, .81)
    tube_mat.shininess = .28
    tube_mat.transparency = 0.5

    tube_lv = coin.SoSeparator()
    tube_lv.addChild(tube_mat)
    tube_lv.addChild(tube_shape)

    tube_tr1 = coin.SoTransform()
    tube_tr1.translation = (+0.75, 0, 0)
    tube_tr2 = coin.SoTransform()
    tube_tr2.translation = (-0.75, 0, 0)

    tube_pv1 = coin.SoSeparator()
    tube_pv1.addChild(tube_tr1)
    tube_pv1.addChild(tube_lv)

    tube_pv2 = coin.SoSeparator()
    tube_pv2.addChild(tube_tr2)
    tube_pv2.addChild(tube_lv)

    cube_lv = coin.SoSeparator()
    cube_lv.addChild(tube_pv1)
    cube_lv.addChild(tube_pv2)

    cube_mat = coin.SoMaterial()
    cube_mat.ambientColor = (.2, .2, .2)
    cube_mat.diffuseColor = (.6, .6, .6)
    cube_mat.specularColor = (.5, .5, .5)
    cube_mat.shininess = .5

    cube_shape = coin.SoCube()
    cube_shape.width = 3.0      # x
    cube_shape.height = 2.0     # y
    cube_shape.depth = 1.0      # z, out of screen

    cube_lv.addChild(cube_mat)
    cube_lv.addChild(cube_shape)

    return cube_lv
Пример #9
0
 def __init__(self, line=None, width=0.1, height=1):
     self.trans = coin.SoTransform()
     m = coin.SoMaterial()
     m.transparency.setValue(0.8)
     m.diffuseColor.setValue([0.4, 0.4, 0.6])
     self.cube = coin.SoCube()
     self.cube.height.setValue(width)
     self.cube.depth.setValue(height)
     self.baseline = None
     if line:
         self.baseline = line
         self.update()
     Tracker.__init__(self, children=[self.trans, m, self.cube])
Пример #10
0
 def __init__(self,line=None,width=0.1,height=1,shaded=False):
     self.trans = coin.SoTransform()
     m = coin.SoMaterial()
     m.transparency.setValue(0.8)
     m.diffuseColor.setValue([0.4,0.4,0.6])
     w = coin.SoDrawStyle()
     w.style = coin.SoDrawStyle.LINES
     self.cube = coin.SoCube()
     self.cube.height.setValue(width)
     self.cube.depth.setValue(height)
     self.baseline = None
     if line:
         self.baseline = line
         self.update()
     if shaded:
         Tracker.__init__(self,children=[self.trans,m,self.cube],name="boxTracker")
     else:
         Tracker.__init__(self,children=[self.trans,w,self.cube],name="boxTracker")
Пример #11
0
    def attach(self, obj):
        "'''Setup the scene sub-graph of the view provider, this method is mandatory'''"
        self.wireframe = coin.SoGroup()
        self.scale = coin.SoScale()
        #		self.color = coin.SoBaseColor()
        self.trans = coin.SoTranslation()

        self.data = coin.SoCube()
        # Switch Z to proper default value
        self.trans.translation.setValue([0, 0, 100])
        self.wireframe.addChild(self.trans)
        style = coin.SoDrawStyle()
        style.style = coin.SoDrawStyle.LINES
        self.wireframe.addChild(style)
        self.wireframe.addChild(self.scale)
        #		self.wireframe.addChild(self.color)
        self.wireframe.addChild(self.data)
        obj.addDisplayMode(self.wireframe, "Wireframe")
Пример #12
0
def main():
    app = QtWidgets.QApplication(sys.argv)

    root = coin.SoSeparator()
    mat = coin.SoMaterial()
    mat.diffuseColor.setValue(coin.SbColor(0.8, 1, 0.8))
    mat.specularColor.setValue(coin.SbColor(1, 1, 1))
    mat.shininess.setValue(1.0)
    mat.transparency.setValue(0.9)
    root.addChild(mat)
    root.addChild(coin.SoSphere())
    root.addChild(coin.SoCube())

    viewer = quarter.QuarterWidget()
    viewer.setSceneGraph(root)

    viewer.setWindowTitle("minimal")
    viewer.show()
    sys.exit(app.exec_())
Пример #13
0
 def attach(self, obj):
     '''Setup the scene sub-graph of the view provider, this method is mandatory'''
     self.shaded = coin.SoGroup()
     self.wireframe = coin.SoGroup()
     self.scale = coin.SoScale()
     self.color = coin.SoBaseColor()
    
     data=coin.SoCube()
     self.shaded.addChild(self.scale)
     self.shaded.addChild(self.color)
     self.shaded.addChild(data)
     obj.addDisplayMode(self.shaded,"Shaded");
     style=coin.SoDrawStyle()
     style.style = coin.SoDrawStyle.LINES
     self.wireframe.addChild(style)
     self.wireframe.addChild(self.scale)
     self.wireframe.addChild(self.color)
     self.wireframe.addChild(data)
     obj.addDisplayMode(self.wireframe,"Wireframe");
     self.onChanged(obj,"Color")
Пример #14
0
def main():
    app = QApplication(sys.argv)

    root = coin.SoSeparator()
    

    vert = coin.SoVertexShader()
    vert.sourceProgram = "vertex.glsl"
    
    frag = coin.SoFragmentShader()
    frag.sourceProgram = "frag.glsl"
    
    shaders = [vert,frag]
    pro = coin.SoShaderProgram()
    pro.shaderObject.setValues(0,len(shaders),shaders)
    
    
    mat = coin.SoMaterial()
    mat.diffuseColor.setValue(coin.SbColor(0.8, 0.8, 0.8))
    mat.specularColor.setValue(coin.SbColor(1, 1, 1))
    mat.shininess.setValue(1.0)
    mat.transparency.setValue(0.5)
    
    
    
    
    sphere = coin.SoSphere()
    sphere.radius = 1.2
    
    
    root.addChild(pro)
    root.addChild(sphere)
    root.addChild(mat)
    root.addChild(coin.SoCube())

    viewer = quarter.QuarterWidget()
    viewer.setSceneGraph(root)

    viewer.setWindowTitle("minimal")
    viewer.show()
    sys.exit(app.exec_())
Пример #15
0
    def attach(self, obj):
        self.shaded = coin.SoGroup()
        self.wireframe = coin.SoGroup()
        self.color = coin.SoBaseColor()
        self.trans = coin.SoTranslation()
        self.box = coin.SoCube()

        self.shaded.addChild(self.color)
        self.shaded.addChild(self.trans)
        self.shaded.addChild(self.box)
        obj.addDisplayMode(self.shaded, "Shaded")

        style = coin.SoDrawStyle()
        style.style = coin.SoDrawStyle.LINES
        self.wireframe.addChild(style)
        self.wireframe.addChild(self.color)
        self.wireframe.addChild(self.trans)
        self.wireframe.addChild(self.box)
        obj.addDisplayMode(self.wireframe, "Wireframe")
        self.onChanged(obj, "Color")
        return
Пример #16
0
    def attach(self, obj):
        "'''Setup the scene sub-graph of the view provider, this method is mandatory'''"
        self.shaded = coin.SoGroup()
        self.wireframe = coin.SoGroup()
        self.scale = coin.SoScale()
        self.color = coin.SoBaseColor()
        self.trans = coin.SoTranslation()

        self.data = coin.SoCube()
        # Top of print bed is X-Y plane
        self.trans.translation.setValue([0, 0, -0.5])
        self.shaded.addChild(self.trans)
        self.shaded.addChild(self.scale)
        self.shaded.addChild(self.color)
        self.shaded.addChild(self.data)
        obj.addDisplayMode(self.shaded, "Shaded")
        style = coin.SoDrawStyle()
        style.style = coin.SoDrawStyle.LINES
        self.wireframe.addChild(style)
        self.wireframe.addChild(self.scale)
        self.wireframe.addChild(self.color)
        self.wireframe.addChild(self.data)
        obj.addDisplayMode(self.wireframe, "Wireframe")
        self.onChanged(obj, "Color")
Пример #17
0
        self.whichChild.connectFrom(self.calc.od)

    def disconnect(self):
        self.whichChild = 0

    def setTolerance(self, tol):
        self.calc.expression.set1Value(0, "ta=%f" % tol)


trans = coin.SoTranslation()
trans.translation = (2.0, 0, 0)

color = coin.SoBaseColor()
color.rgb = (0, 0, 0)

cube = coin.SoCube()

# One text node in each child of the Switch

no = coin.SoText2()
no.string = "View is not orthogonal to any plane"

xy = coin.SoText2()
xy.string = "View is orthogonal to a plane"

# --------------

orthoSwitch = orthoToggleSwitch("Grid")

orthoSwitch.view_0.addChild(no)
orthoSwitch.view_1.addChild(xy)
Пример #18
0
        diag.setWindowFlags(PyQt4.QtCore.Qt.WindowStaysOnTopHint) # PyQt4 # this function sets the window before
    except Exception:    
        diag.setWindowFlags(PySide.QtCore.Qt.WindowStaysOnTopHint)# PySide # this function sets the window before
#    diag.setWindowModality(QtCore.Qt.ApplicationModal)       # function has been disabled to promote "WindowStaysOnTopHint"
    diag.exec_()

self.doubleSpinBox.setProperty("value", 10.0)  # PyQt4

self.doubleSpinBox.setValue(10.0)  # PySide

self.doubleSpinBox.setToolTip(_translate("MainWindow", "Coordinate placement Axis Y", None))  # PyQt4

self.doubleSpinBox.setToolTip(_fromUtf8("Coordinate placement Axis Y"))  # PySide

self.doubleSpinBox.setToolTip(u"Coordinate placement Axis Y.")# PySide

import pivy

from pivy import coin
App.newDocument() # Open a document and a view 
view = Gui.ActiveDocument.ActiveView 
FCSceneGraph = view.getSceneGraph() # returns a pivy Python object that holds a SoSeparator, the main "container" of the Coin scenegraph
FCSceneGraph.addChild(coin.SoCube()) # add a box to scene

from lazy_loader.lazy_loader import LazyLoader
Part = LazyLoader('Part', globals(), 'Part')

P = LazyLoader('Part', globals(), 'Part')

utils = LazyLoader('PathScripts', globals(), 'PathScripts.PathUtils')
Пример #19
0
# https://wiki.freecadweb.org/Scripting_examples
# https://wiki.freecadweb.org/Pivy

from pivy import coin

sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
print(sg)

for node in sg.getChildren():
    print(node)

col = coin.SoBaseColor()
col.rgb = (1, 0, 0)
cub = coin.SoCube()
myCustomNode = coin.SoSeparator()
myCustomNode.addChild(col)
myCustomNode.addChild(cub)
sg.addChild(myCustomNode)

col.rgb = (1, 1, 0)

col = coin.SoBaseColor()
col.rgb = (1, 0, 0)
trans = coin.SoTranslation()
trans.translation.setValue([0, 0, 0])
cub = coin.SoCube()
myCustomNode = coin.SoSeparator()
myCustomNode.addChild(col)
myCustomNode.addChild(trans)
myCustomNode.addChild(cub)
sg.addChild(myCustomNode)