add scene graph by iv-string

Need help, or want to share a macro? Post here!
User avatar
microelly2
Posts: 4329
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

add scene graph by iv-string

Postby microelly2 » Tue Jan 12, 2016 9:12 am

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
Posts: 6840
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: add scene graph by iv-string

Postby DeepSOIC » Tue Jan 12, 2016 10:44 am

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
Posts: 4329
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: add scene graph by iv-string

Postby microelly2 » Tue Jan 12, 2016 12:55 pm

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
Site Admin
Posts: 14586
Joined: Thu Feb 19, 2009 10:32 am

Re: add scene graph by iv-string

Postby wmayer » Tue Jan 12, 2016 6:37 pm

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.
looo
Posts: 2734
Joined: Mon Nov 11, 2013 5:29 pm

Re: add scene graph by iv-string

Postby looo » Tue Jan 12, 2016 7:45 pm

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
Posts: 4329
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: add scene graph by iv-string

Postby microelly2 » Tue Jan 12, 2016 8:20 pm

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.
looo
Posts: 2734
Joined: Mon Nov 11, 2013 5:29 pm

Re: add scene graph by iv-string

Postby looo » Tue Jan 12, 2016 9:07 pm

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

Re: add scene graph by iv-string

Postby microelly2 » Tue Jan 12, 2016 9:36 pm

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
Posts: 4329
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: add scene graph by iv-string

Postby microelly2 » Mon Jan 18, 2016 6:37 pm

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 1539 times
see: http://freecadbuch.de/doku.php?id=blog: ... g_erzeugen
looo
Posts: 2734
Joined: Mon Nov 11, 2013 5:29 pm

Re: add scene graph by iv-string

Postby looo » Wed Jan 27, 2016 5:44 pm

"SoInput.setBuffer" should work with this commit.