algorithmic modeling for Rhino
as the post says, how does one serialize a curve object to a an xml string that can be deserialized back to a curve object? Im lookign at the grasshopper gh_Curve type and there appears to be XML writer methods here but Im not an expert in this,
cheers
Tags:
Hello Steve,
Besides any in-built ways to do so, you can just go the "geometric" way and extract all the control points + the degree of the curve. Then use System.XML to write your own XML document!
Johannes
Thanks johannes, i want to avoid having to reinevent that.
There is no native way to do this. Curves are (de)serialized using OpenNurbs methods and they do not support xml flavours. When you serialize a curve using GH_IO method you end up with a section of xml that is just a text representation of the byte array that OpenNurbs writes out.
There are two methods in the Grasshopper.Kernel.GH_Convert class for converting RhinoCommon objects such as curves into byte arrays and back again: CommonObjectToByteArray() and ByteArrayToCommonObject().
If you really want xml, then you'll need to use GH_IO methods, see attached.
--
David Rutten
david@mcneel.com
Poprad, Slovakia
Super, exactly what i was looking for, thanks.
Hey David,
I have been messing around with CommonObjectToByteArray() and ByteArrayToCommonObject(). It seems I can get this to work fine with curves, meshes, and surfaces, but when I try to convert a BRep to a Byte array, the code fails and gives me an error message "Value cannot be null. Parameter name: source (line: 0)
Additionally, does the GH_IO method used in your GH file only work for curves? I have tried to repeat that process for surfaces and other geometry types and it gives me the same error as above.
-Nathan
Hi Nathan,
this is happening on Rhino4 yes? Not Rhino5? There is a bug in the Rhino4 serializer and there's no way on Earth I can try and debug that code. Unfortunately it's very low priority as practically all development in Seattle in focused on Rhino5, which seems to work fine in terms of (de)serialization.
If it's happening on Rhino5, then there's some other bug and we need to fix it soon.
--
David Rutten
david@mcneel.com
Poprad, Slovakia
Great, looks like serialization works as expected in Rhino 5.
Can you give me any tips on Deserializing the XML back to an Object?
For example, I have created a component that serializes different object types as XML strings. Now I want to make a component that Deserializes the XML back into objects.
I tried using Deserialize_Xml(xml) but this tells me that the expression does not produce a value.
I assumed the process would be as such (probably too easy):
Private Sub RunScript(ByVal xml As String, ByRef A As Object)
Dim xmlOBJchunk As New GH_IO.Serialization.GH_LooseChunk("serialized XML")
A = xmlOBJchunk.Deserialize_Xml(xml)
Any help is appreciated,
-Nathan
Up and running... this is what I did to deserialize an XML string back into a BRep. It works, but maybe there is a better way?
'create a new chunk
Dim chunk As New GH_IO.Serialization.GH_LooseChunk("xml")
'deserialize the xml string into the chunk
chunk.Deserialize_Xml(xml)
'get the deserialized chunk items
Dim chunkItems As List(Of GH_IO.Types.GH_Item) = chunk.Items
'last item in list is the byte array
Dim chunkItem As GH_IO.Types.GH_Item = chunkItems(chunkItems.Count-1)
'convert the chunk item to a byte array
Dim myBytes() As Byte = chunkItem._bytearray
'convert the byte array to a common object
Dim myObject As Object = Kernel.GH_Convert.ByteArrayToCommonObject(Of Brep)(myBytes)
obj = myObject
-Nathan
Nice Nathan! What kind of performance do you get by serializing/deserializing?
Luis,
I haven't tried it with a large collection of objects so I have not yet been able fully judged performance with this process.
With the code above I have tested it on the following data types with success: Curves, Surfaces, Breps, and Meshes
It seems that certain data types such as points and planes aren't recognized as CommonObjects so they fail when I pass them to ByteArrayToCommonObject.
I am going to do some further testing this weekend and if I am comfortable with the process, I will release some components as part of the next version of Slingshot!
Hi Nathan/Luis.
Just seen this.
I had some success with deserializing a curve a while back. I used ghhowl components to send a curve object over UDP from one session of rhino to another. Il post something this week end. It opens up some interesting oppurtunities.............
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by