algorithmic modeling for Rhino
Hi,
i seaching for a method to store persistent data of a GH_component in an object witch can be serialized and deserialized as an XML-string. I do not want to use the input or outputs to store the persistant data , as the inputs and outputs of the component should be build by the data stored in the object.
thanxs in advance
Michael
here is the code of the object....
public class Proxy
{
public List<string> _name_in;
public List<string> _name_out;
public List<SerializableType> _type_in;
public List<SerializableType> _type_out;
public List<GH_ParamAccess> _access_in;
public string _path;
public string _script;
public bool _internalized;
public bool _working;
public Proxy(List<string> name_in, List<string> name_out, List<SerializableType> type_in, List<SerializableType> type_out, List<GH_ParamAccess> access_in, string path, string script, bool internalized)
{
_name_in = name_in;
_name_out = name_out;
_type_in = type_in;
_type_out = type_out;
_access_in = access_in;
_path = path;
_script = script;
_internalized = internalized;
_working = true;
}
public Proxy()
{
_name_in = new List<string>();
_name_out = new List<string>();
_type_in = new List<SerializableType>();
_type_out = new List<SerializableType>();
_access_in = new List<GH_ParamAccess>();
_path = get_path_of_plugin();
_script = "";
_internalized = false;
_working = false;
}
public static string get_path_of_plugin()
{
string temp_cut;
string string_path = System.Reflection.Assembly.GetExecutingAssembly().Location;
string string_name = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
int temp_name_int = string_name.Length + 5;
int temp_path_int = string_path.Length;
temp_cut = string_path.Remove(temp_path_int - temp_name_int);
return temp_cut;
}
public static T ObjectDeserializer<T>(string XmlInput)
{
System.Xml.XmlDocument XmlDoc = new System.Xml.XmlDocument();
XmlDoc.Load(new System.IO.StringReader(XmlInput));
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
T out_ob = (T)ser.Deserialize(new System.IO.StringReader(XmlInput));
return out_ob;
}
public static string ObjectSerializer<T>(T SerializedObject)
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
System.Text.StringBuilder builder = new System.Text.StringBuilder();
XmlWriter xmllol = XmlWriter.Create(builder);
ser.Serialize(xmllol, SerializedObject);
return builder.ToString();
}
}
public class SerializableType
{
private Type type;
// when serializing, store as a string
// [DataMember]
public string TypeString
{
get
{
if (type == null)
return null;
return type.FullName;
}
set
{
if (value == null)
type = null;
else
{
type = Type.GetType(value);
}
}
}
public Type return_Type() { return type; }
// constructors
public SerializableType()
{
type = null;
}
public SerializableType(Type t)
{
type = t;
}
// allow SerializableType to implicitly be converted to and from System.Type
static public implicit operator Type(SerializableType stype)
{
return stype.type;
}
static public implicit operator SerializableType(Type t)
{
return new SerializableType(t);
}
// overload the == and != operators
public static bool operator ==(SerializableType a, SerializableType b)
{
// If both are null, or both are same instance, return true.
if (System.Object.ReferenceEquals(a, b))
{
return true;
}
// If one is null, but not both, return false.
if (((object)a == null) || ((object)b == null))
{
return false;
}
// Return true if the fields match:
return a.type == b.type;
}
public static bool operator !=(SerializableType a, SerializableType b)
{
return !(a == b);
}
// we don't need to overload operators between SerializableType and System.Type because we already enabled them to implicitly convert
public override int GetHashCode()
{
return type.GetHashCode();
}
// overload the .Equals method
public override bool Equals(System.Object obj)
{
// If parameter is null return false.
if (obj == null)
{
return false;
}
// If parameter cannot be cast to SerializableType return false.
SerializableType p = obj as SerializableType;
if ((System.Object)p == null)
{
return false;
}
// Return true if the fields match:
return (type == p.type);
}
public bool Equals(SerializableType p)
{
// If parameter is null return false:
if ((object)p == null)
{
return false;
}
// Return true if the fields match:
return (type == p.type);
}
}
public class GH_Proxy : Grasshopper.Kernel.Types.GH_Goo<Proxy>
{
public override Grasshopper.Kernel.Types.IGH_Goo Duplicate()
{
return this;
}
public override bool IsValid
{
get { return true; }
}
public override string ToString()
{
return Proxy.ObjectSerializer<Proxy>(this.Value);
}
public override string TypeDescription
{
get { return "his is a proxy"; }
}
public override string TypeName
{
get { return "his is a proxy"; }
}
}
Tags:
Ok ... i got the solution....
public class cssscripter : Grasshopper.Kernel.GH_Component
{
internal Grasshopper.Kernel.Types.GH_String gh_string;
internal Proxy proxy;
public cssscripter(): base("css_plugin", "css_plugin", "this is a plugin for C# scripting", "Extra", "MikeTools")
{
proxy = new Proxy();
Random rand = new Random();
proxy._script = Convert.ToString(rand.Next(100));
gh_string = new Grasshopper.Kernel.Types.GH_String(Proxy.ObjectSerializer<Proxy>(proxy));
}
public override bool Write(GH_IO.Serialization.GH_IWriter writer)
{
gh_string.Write(writer);
return base.Write(writer);
}
public override bool Read(GH_IO.Serialization.GH_IReader reader)
{
gh_string.Read(reader);
proxy = Proxy.ObjectDeserializer<Proxy>(gh_string.Value);
return base.Read(reader);
}
......................................
Hi Michael,
I don't exactly follow. Where does this data come from? Where is it supposed to be (de)serialized from/to? Is it shared amongst multiple components? Is it just for your own components or will other components also have to deal with it? At what point should this data affect the inputs/outputs of a component?
--
David Rutten
david@mcneel.com
Poprad, Slovakia
It will come from a menübutton form in the future and a external file or database... I now just checked if the data is stored in the component...
it should affect the inputs and outputs when the object inside of the componenet is changed.
I want to have a component which reads a file [with the script in c# , input / output definitions] and when build the inputs and the processing logic...
Ok. Then why are you using GH_Goo(Of T), that's only useful for data that is going to be stored inside parameters. Still, seeing as how you found a solution, I suppose we may as wel end this :)
--
David Rutten
david@mcneel.com
Poprad, Slovakia
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