add scene graph by iv-string
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
- microelly2
- Veteran
- Posts: 4688
- Joined: Tue Nov 12, 2013 4:06 pm
- Contact:
add scene graph by iv-string
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?
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?
- 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
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...
- microelly2
- Veteran
- Posts: 4688
- Joined: Tue Nov 12, 2013 4:06 pm
- Contact:
Re: add scene graph by iv-string
I found this c++
But no idea how to transform it to python
I get these errors
Code: Select all
# c++ example
'''
// Read from the string.
SoInput in;
in.setBuffer(dodecahedron, strlen(dodecahedron));
SoNode *result;
SoDB::read(&in, result);
'''
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)
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 *'
No idea what char const *[] and void const * means in pythonFile "/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 *[]'
Re: add scene graph by iv-string
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.
As alternative you must write the string to a file on disk and then use SoInput.openFile.
Re: add scene graph by iv-string
https://github.com/looooo/pivy/blob/mas ... dString.py this one works for me.
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.
OT:There are a couple of functions that are broken in pivy
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.
- microelly2
- Veteran
- Posts: 4688
- Joined: Tue Nov 12, 2013 4:06 pm
- Contact:
Re: add scene graph by iv-string
Thank you, nice collection of examples - I was not aware that the inventor mentor examples exist in python too.looo wrote:https://github.com/looooo/pivy/blob/mas ... dString.py this one works for me.
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.
Re: add scene graph by iv-string
I am sorry I have run the wrong file. This example doesn't work for me either.https://github.com/looooo/pivy/blob/mas ... dString.py this one works for me.
- microelly2
- Veteran
- Posts: 4688
- Joined: Tue Nov 12, 2013 4:06 pm
- Contact:
Re: add scene graph by iv-string
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)
- microelly2
- Veteran
- Posts: 4688
- Joined: Tue Nov 12, 2013 4:06 pm
- Contact:
Re: add scene graph by iv-string
I have now the prototype for a parser for inventor data.microelly2 wrote: I will look for or write my own parser for inventor file syntax.
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.
see: http://freecadbuch.de/doku.php?id=blog: ... g_erzeugen
Re: add scene graph by iv-string
"SoInput.setBuffer" should work with this commit.