Grasshopper

algorithmic modeling for Rhino

Model formatter.Deserialize - problem with casting karamba Model to GH_Model

Hi there,

Could someone help me with the following issue that came along? I'm creating components in visual studio, C#.

I successfully 'externalised' a Karamba Model by saving it as a text-file. Class of kModel is Karamba.Models.Model.

System.Runtime.Serialization.IFormatter formatter = Karamba.Utilities.ModelSerializer.GetInstance().Formatter;

Stream stream = new FileStream(folderPath + @"\" + fileName + ".txt", FileMode.Create, FileAccess.Write, FileShare.None);

formatter.Serialize(stream, kModel);
stream.Close();

However of course, I want to read it back in and also this seems to be working.

System.Runtime.Serialization.IFormatter formatter = Karamba.Utilities.ModelSerializer.GetInstance().Formatter;

Stream streamRead = new FileStream(folderPath + @"\" + fileName + ".txt", FileMode.Open, FileAccess.Read, FileShare.Read);

Model model = new Model();
try { model = (Model)formatter.Deserialize(streamRead); } catch { }

Now I try to cast this Karamba.Models.Model into Karamba.Models.GH_Model and set it as an output with following setup:

pManager.Register_GenericParam("model", "model", "model", GH_ParamAccess.item);
-----------------------------
GH_Model gH_Model = new GH_Model(model);
DA.SetData(1, gH_Model);

But with this set-up I get following pop-up in Grasshopper:

Data formatting threw an exception

System.NullReferenceException: Object reference not set to an instance of an object.

This is strange to me since I am able to set in example the model.elems list as an output, but not the casted GH_Model.

Someone who sees what I'm doing wrong?

Regards,

Lennert

Views: 269

Replies to This Discussion

Dear Lennert,

after reading the C#-model one has to call 'model.buildFEModel()' so that the C++ model is created (see attached Rhino6 file).

Best,

Clemens

Attachments:

Many thanks for the brief and helpful support! :-)

Lennert

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service