Hey, I’m choosing tech stack for ongoing project in my company. This is the reason why I started experimenting with rhino.compute. I am able to solve simple grasshopper solutions on server and get geometric/textual data back. However, I have no understanding in regards to handling blocks’ instances and definitions with rhino.compute.
For the sake of testing lets assume following scenario:
I have simple GH Solution, that creates sphere, encloses it inside block definition and than instantiate the definition 10 times in different positions each time. In that case, sphere’s radius and block’s definition name would make input params and 10 block’s instances - output parameter. I’d like to get input from web app, pass it to rhino.compute, solve the above definition, create new rhino .3dm file and insert those 10 block’s instances inside the newly created .3dm file.
Let’s consider only the simplest case, where both rhino compute and the web app are running locally, so the rhino file is being saved on the same desktop environment.
How to tackle this problem?
*I’m targeting rhino.compute based on rhino 8
Nvm, solved it. So block definitions and block instances are objects, that cannot exist without rhino document. Because rhino.compute is headless version of rhino it doesn’t have active document (if I understand) set by default. So what you need to do is create headless document, insert block definition into it, and then insert block instance based on the definition (order of insertion is important here, because instances store reference to definitions) .
Below code that you can paste into C# grasshopper component. It takes block definition made with Model Block Definition component as input and outputs .3dm file with block instance baked at the origin point.
private void RunScript(object x, ref object a)
{
Console.WriteLine(x.GetType());
Guid defId = Guid.NewGuid();
//Cast type
var def = (Grasshopper.Rhinoceros.Model.ModelInstanceDefinition) x;
var doc = Rhino.RhinoDoc.CreateHeadless(null);
var attr = doc.CreateDefaultAttributes();
//Bake block definition to headless RhinoDoc
bool success = def.BakeGeometry(doc, attr, ref defId );
Console.WriteLine(success);
//Place an instance of definition into headless document at origin point
Rhino.DocObjects.InstanceDefinition rhinoDef = doc.InstanceDefinitions.Find(def.Name);
Transform transform = Transform.Identity;
Console.WriteLine(defId);
doc.Objects.AddInstanceObject(rhinoDef.Index, transform, attr);
a = doc.Export(@"C:\Users\<your-desired-path-here>\plsInsertDef.3dm");
}
1 Like