Grasshopper

algorithmic modeling for Rhino

Custom Data Types as output/input from/to a custom component

Hi,

I am looking for a way to use custom Data Type as output from a custom component to another custom component.

but I get an Error: Invalid cast: ObjectWrapper myCustomDataType

I saw a few topics and also the Simple Data Types Help in the SDK..I tried to implement IGH_Goo without any success ..Does someone has some other samples to share ?

thanks

Views: 1943

Replies to This Discussion

I've used the following technique with some success:

For the export of custom data of type CustomDataType:

DA.SetData(0, new GH_ObjectWrapper(CustomDataTypeObject));

and to read it back in, read it in with a generic_param, and 

foreach (GH_ObjectWrapper objWrap in Object_WrapperInputList)
{
CustomDataTypeList.Add(objWrap.Value as CustomDataType);
}

To be smart about it one should probably define a real custom param type, but this seems to do the trick with a minimum of fuss. 

That works great ! Thanks really much Andrew

Hi,

just go back to the discussion because I have some issues with the GH_ObjectWrapper.

In my custom class I have some list of geometries like Point3d, Line, Curve and Brep. When I try to read it back I can only read Curve and Brep objects type. Point3d and Line are lost..

Does someone already had such issue ? Does some object Type are not working with the GH_ObjectWrapper?


thanks.

GH_ObjectWrapper is not supposed to work with serialization at all. It's a last ditch effort to allow data of unknown type to participate in a Grasshopper network.

If you want your data to serialize, then you really need to implement IGH_Goo (or derive from GH_Goo<T>). If (de)serialization is not what you meant by "read back in", then you'll need to post some source code so I can have a look at it.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Hi David, ok it's time to try to implement  IGH_Goo...thanks.

raf

So... I have implemented IGH_Goo (probably incorrectly) on a custom class, and I've reached the point where I need to cast from IGH_Goo to my custom type.  My class is as follows:

public class Acorn : GH_Goo<Dictionary<string, IGH_Goo>>
{

public List<string> types = new List<string>();
public Dictionary<string, IGH_Goo> dictionary = new Dictionary<string, IGH_Goo>();

//public bool IsValid => true;

//public string IsValidWhyNot => throw new NotImplementedException();

//public string TypeName => "Squirrel Acorn";

//public string TypeDescription => "A dictionary of Grasshopper data created by Squirrel";

public override bool IsValid
{
get
{
if (Value == null)
{
return false;
} else if (this.dictionary.Count == 0)
{
return false;
} else
{
return true;
}
}
}

public override string TypeName => "Squirrel Acorn";

public override string TypeDescription => "A dictionary of Grasshopper data created by Squirrel";

public Acorn()
{
this.types = new List<string>();
this.dictionary = new Dictionary<string, IGH_Goo>();
}

public Acorn(Dictionary<string, IGH_Goo> dictionary)
{
Dictionary<string, IGH_Goo> newDictionary = new Dictionary<string, IGH_Goo>(dictionary);
this.dictionary = newDictionary;
foreach (string key in this.dictionary.Keys)
{
this.types.Add(this.dictionary[key].TypeName);
}
}


public void AddKVPair(string str, IGH_Goo goo)
{
if (goo != null)
{
this.dictionary.Add(str, goo);
this.types.Add(goo.TypeName);

} else
{

this.dictionary.Add(str, new GH_String());
this.types.Add("Null");
}
}

public void RemoveKVPair(string str)
{
this.dictionary.Remove(str);
}

private string GetKeyValueType(string key)
{
IGH_Goo goo = this.dictionary[key];
return goo.TypeName;
}

public IGH_Goo ToGoo()
{
IGH_Goo newGoo = GH_Convert.ToGoo(this);
return newGoo;


}


public override string ToString()
{
Dictionary<string, string> displayText = new Dictionary<string, string>();

List<string> values = new List<string>();

foreach (string key in this.dictionary.Keys)
{
displayText.Add(key + " (" + GetKeyValueType(key) + ")", this.dictionary[key].ToString());
}

return JsonConvert.SerializeObject(displayText);
}

public override IGH_Goo Duplicate()
{
return new Acorn(Value);
}

//public IGH_Goo Duplicate()
//{
// throw new NotImplementedException();
//}

//public IGH_GooProxy EmitProxy()
//{
// throw new NotImplementedException();
//}

//public bool CastFrom(object source)
//{
// return false;
//}

//public bool CastTo<T>(out T target)
//{
// IGH_Goo val = GH_Convert.ToGoo(this);
// if (val != null)
// {
// return true;
// } else
// {
// return false;
// }
//}

//public bool CastTo<T>(out T target)
//{

// object ptr = null;
// //First, see if Q is similar to the Integer primitive.
// if (typeof(T).IsAssignableFrom(typeof(GH_Goo<T>)))
// {
// ptr = this.dictionary;
// target = (T)ptr;
// return true;
// }

// target = (T)ptr;
// //We could choose to also handle casts to Boolean, GH_Boolean,
// //Double and GH_Number, but this is left as an exercise for the reader.
// return false;
//}

//public object ScriptVariable()
//{
// throw new NotImplementedException();
//}

//public bool Write(GH_IWriter writer)
//{
// throw new NotImplementedException();
//}

//public bool Read(GH_IReader reader)
//{
// throw new NotImplementedException();
//}

//public override IGH_Goo Duplicate()
//{
// throw new NotImplementedException();
//}
}

How do I allow IGH_Goo to cast to this Acorn type?  

Thanks,

Marc

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service