Grasshopper

algorithmic modeling for Rhino

Hi, I hope someone can help me ..!

I ve got a problem implementing code which saves a state - without the dialogue box. This is how it looks:

GH_State tmpState = new GH_State();

IEnumerator<IGH_DocumentObject> enumObj;
try
{
enumObj = doc.Objects.GetEnumerator();
while (enumObj.MoveNext())
{
IGH_DocumentObject obj = enumObj.Current;
if (obj is IGH_StateAwareObject)
{
tmpState.AddStateObject((Grasshopper.Kernel.IGH_StateAwareObject)obj, obj.ComponentGuid);
}
}
}
finally
{

}

tmpState.Name = "OctoSolution " + DateTime.Now;

doc.StateServer.Add(tmpState);

The state is created without any errors, also when debugging I see the correct stateobjects (slider data etc.) being stored in the dictionary as Guid+string.

However, when I try to restore the state using Grasshopper's UI, nothing happens.

I am adding a custom name to it, however I am not able to set the date right, since this is a get-only property..

I would apreciate help a lot,

many thanks

Robert

Views: 659

Replies to This Discussion

Don't do this:

enumObj = doc.Objects.GetEnumerator();
while (enumObj.MoveNext())
{
IGH_DocumentObject obj = enumObj.Current;

Use a foreach loop, that's what the enumerator is for.

--

David Rutten

david@mcneel.com

that's what .Net reflector gave me from grasshopper.dll ;) but i guess it made a mistake then, as it often does ..!

No, foreach is compiled to that complicated MSIL code, but if you're writing C# rather than MSIL you may as well use the easy syntax.

--

David Rutten

david@mcneel.com

The problem is with AddStateObject. You're putting in the ComponentGuid, but you need to supply the InstanceGuid.

private void RunScript(bool Record, ref object A)
{
if (!Record)
return;

GH_State state = new GH_State();

foreach (IGH_DocumentObject obj in GrasshopperDocument.Objects)
{
IGH_StateAwareObject stateObject = obj as IGH_StateAwareObject;
if (stateObject == null)
continue;

state.AddStateObject(stateObject, obj.InstanceGuid);
}

if (state.StateCount == 0)
return;

state.Name = "OctoSolution " + DateTime.Now;
GrasshopperDocument.StateServer.Add(state);
}

--

David Rutten

david@mcneel.com

I see, thanks a lot!

Best,

Robert

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service