add scene graph by iv-string

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

add scene graph by iv-string

Post by microelly2 »

I want to add an object inside the given scene graph
but instead of using hundreds of coin addChild I want to read in a string in inventor syntax (iv file).
There is a way to get the scene out

obj = FreeCAD.ActiveDocument.ActiveObject
viewprovider = obj.ViewObject
print viewprovider.toString()

But I look for the opposite direction: What is the function to extend the scenegraph by a string?
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: add scene graph by iv-string

Post by DeepSOIC »

I think I managed it once with pivy.SoDB.read(). Was quite some time ago, and it was just some random playaround - I didn't save anything...
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: add scene graph by iv-string

Post by microelly2 »

I found this c++

Code: Select all

# c++ example
'''
// Read from the string.
SoInput in;
in.setBuffer(dodecahedron, strlen(dodecahedron));

SoNode *result;
SoDB::read(&in, result);

'''
But no idea how to transform it to python

Code: Select all

from pivy import coin

inp=coin.SoInput()
FreeCAD.activeDocument().addObject("Part::Box","myBox")
s=FreeCADGui.activeDocument().getObject("myBox").toString()

inp.setBuffer(s,1000)
myNode=coin.SoDB.readAll(inp)

myNode

result=coin.SoSeparator()

myNode=coin.SoDB.read(inp,result)

myNode
result

s=['a','b']
inp.setStringArray(s)
I get these errors
File "/usr/lib/python2.7/dist-packages/pivy/coin.py", line 4692, in setBuffer
return _coin.SoInput_setBuffer(self, *args)
TypeError: in method 'SoInput_setBuffer', argument 2 of type 'void const *'
File "/usr/lib/python2.7/dist-packages/pivy/coin.py", line 4696, in setStringArray
return _coin.SoInput_setStringArray(self, *args)
TypeError: in method 'SoInput_setStringArray', argument 2 of type 'char const *[]'
No idea what char const *[] and void const * means in python :roll:
wmayer
Founder
Posts: 20245
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: add scene graph by iv-string

Post by wmayer »

There are a couple of functions that are broken in pivy -- or I don't know how to do it. One of them is to set the buffer of the SoInput class.
As alternative you must write the string to a file on disk and then use SoInput.openFile.
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: add scene graph by iv-string

Post by looo »

https://github.com/looooo/pivy/blob/mas ... dString.py this one works for me.
There are a couple of functions that are broken in pivy
OT:
What about going with PyInventor with the upcoming python3 port? It seems to be more active than pivy and it has nicer syntax, but I don't know how complete it is.
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: add scene graph by iv-string

Post by microelly2 »

Thank you, nice collection of examples - I was not aware that the inventor mentor examples exist in python too.

But it gives the same error inside freecad,
so I have to go the hard way
I will look for or write my own parser for inventor file syntax.

thanks to +wmayer. For the first I can go through a file.
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: add scene graph by iv-string

Post by looo »

I am sorry I have run the wrong file. This example doesn't work for me either.
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: add scene graph by iv-string

Post by microelly2 »

Problem solved - workaround example

Code: Select all

import os
from pivy.coin import SoInput,SoDB

dm='''#Inventor V2.0 ascii
 
Separator {
    RotationXYZ {      
       axis Z
       angle 0
    }
    Transform {
       translation 0 0 0.5
    }
    Separator {
       Material {
          diffuseColor 0.05 0.05 0.05
       }
       Transform {
          rotation 1 0 0 1.5708
          scaleFactor 0.2 0.5 0.2
       }
       Cylinder {
       }
    }
}
'''


tmpname="/tmp/freecad_%s.iv" % os.getpid()
f=open(tmpname,"w")
f.write(dm)
f.close()
mySceneInput = SoInput()
mySceneInput.openFile(tmpname)
myGraph = SoDB.readAll(mySceneInput)
mySceneInput.closeFile()
sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
sg.addChild(myGraph)
os.remove(tmpname)

User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: add scene graph by iv-string

Post by microelly2 »

microelly2 wrote: I will look for or write my own parser for inventor file syntax.
I have now the prototype for a parser for inventor data.
Its not the usual syntax but I can use the syntax of my parser to process Qt and FreeCAD Models too.

I think its more productive to describe different structures the same syntactic way.
After some tests I will improve miki to handle more.
bn_823.png
bn_823.png (98.34 KiB) Viewed 2839 times
see: http://freecadbuch.de/doku.php?id=blog: ... g_erzeugen
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: add scene graph by iv-string

Post by looo »

"SoInput.setBuffer" should work with this commit.
Post Reply