# Pass RhinoDoc to Grasshopper in a C# Plugin hosted by Rhino.Compute

**URL:** <https://discourse.mcneel.com/t/pass-rhinodoc-to-grasshopper-in-a-c-plugin-hosted-by-rhino-compute/159297>\
**Category:** Rhino Developer\
**Tags:** windows, rhinocompute\
**Created:** [May 3, 2023, 1:57pm UTC](https://discourse.mcneel.com/t/pass-rhinodoc-to-grasshopper-in-a-c-plugin-hosted-by-rhino-compute/159297 "2023-05-03T13:57:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tai](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/tai/32/623033_2.png) [@Tai](https://discourse.mcneel.com/u/Tai)\
**Post date:** [May 3, 2023, 1:57pm UTC](https://discourse.mcneel.com/t/pass-rhinodoc-to-grasshopper-in-a-c-plugin-hosted-by-rhino-compute/159297/1 "2023-05-03T13:57:59Z")

</div>

I’d like to be able to pass a .3dm file to Rhino.Compute, apply a Grasshopper definition to it which modifies the document, and then stream the modified document back to the caller.

- I have a C# class which inherits Rhino.Plugins.Plugin, which is installed on a Rhino.Compute instance and exposes a custom endpoint named “Modify” using `HostUtils.RegisterComputeEndpoint` during OnLoad, which is all working fine.

- The “Modify” method takes as input the .3dm file as a base64 string, and the string path to a GH definition file. The 3dm file is written to disk as a temp file and then loaded into memory via  
`var rhinoDoc = RhinoDoc.CreateHeadless(tempFilePath)`.

- My first optimistic attempt was to just call  
`RhinoApp.RunScript($"_-GrasshopperPlayer \"{pathToGHDef}\"", false)`  
but my .GH file uses eleFront components for document IO, which threw `NullReferenceException`. From this post on the forums ([Hops and Elefront](https://discourse.mcneel.com/t/hops-and-elefront/139496)), I’m guessing my issue is that my plugin code does not provide the Grasshopper engine with a reference to the “current” RhinoDoc. I verified this by making a GH definition with a single C# script component which outputs a bool if `this.RhinoDocument==null`.

- I then saw that `RhinoApp.RunScript` has an overload which takes a `uint documentSerialNumber`, so I tried passing it my loaded `rhinoDoc.RuntimeSerialNumber`, which caused the same exception again.

Is there a way to invoke the grasshopper solver where you supply the RhinoDoc reference for it to operate on?

---

<div class="post-metadata">

**Author:** ![emilpoulsen](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/emilpoulsen/32/672256_2.png) [@emilpoulsen](https://discourse.mcneel.com/u/emilpoulsen)\
**Post date:** [May 3, 2023, 5:32pm UTC](https://discourse.mcneel.com/t/pass-rhinodoc-to-grasshopper-in-a-c-plugin-hosted-by-rhino-compute/159297/2 "2023-05-03T17:32:11Z")

</div>

Could another option be to import the 3dm file to Grasshopper directly using [Resthopper](https://github.com/mcneel/compute.rhino3d/blob/7.x/src/compute.geometry/ResthopperEndpoints.cs)?

I would do something like this from a GH component executed in a rhino compute context (inspired from @fraguada’s earlier [post](https://discourse.mcneel.com/t/exports-and-imports-using-rhino-compute/72436/20)):

```auto
string tmpPath = "<path to file>"
using( var doc = Rhino.RhinoDoc.CreateHeadless(null)){
  doc.Import(tmpPath);
  var ros = doc.Objects.FindByObjectType(Rhino.DocObjects.ObjectType.Mesh);
  var meshes = new List<Rhino.Geometry.Mesh>();
  foreach( Rhino.DocObjects.RhinoObject obj in ros )
  {
    meshes.Add(obj.DuplicateGeometry() as Rhino.Geometry.Mesh);
  }
}
//output the meshes or whatever from the component

```

Then you pass these meshes out to the GH defintion where it’s processed further. Finally you create a new modified version of the 3dm file using the [File3dm class](https://developer.rhino3d.com/api/rhinocommon/rhino.fileio.file3dm#methods) which then is streamed back to the client.

---

<div class="post-metadata">

**Author:** ![Tai](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/tai/32/623033_2.png) [@Tai](https://discourse.mcneel.com/u/Tai)\
**Post date:** [May 4, 2023, 3:55pm UTC](https://discourse.mcneel.com/t/pass-rhinodoc-to-grasshopper-in-a-c-plugin-hosted-by-rhino-compute/159297/4 "2023-05-04T15:55:19Z")

</div>

I realized it’s way simpler than I was making it. From inside the context of C# code running as a plugin inside Rhino.Compute, I can just load the definition into a GH\_Document, populate the GH\_Structures for inputs, run definition.NewSolution(…) and just collect the result parameters.
