Grasshopper

algorithmic modeling for Rhino

Hi,

What would be a method to retrieve object from rhino (preferable as guid) and then cast guid to instance object (block)?

Thanks

Views: 1402

Replies to This Discussion

You cannot cast a Guid to a Instance Definition or Instance Reference, casting only works if the target data is already of the desired type.

My grasp on how blocks work in Rhino is a bit tenuous, you're probably better off asking pure RhinoCommon questions on the discourse forum.

if you have the guid, and you want to get the instance object, try RhinoDocument.Objects.Find(x) where x is the Guid. This will give you a Rhino.DocObjects.InstanceObject.

(If you're working in visual studio developing a GHA, use Rhino.RhinoDoc.ActiveDoc in place of the RhinoDocument shortcut present in c# scripting components.)

Thanks it works:

//Find rhino object by guid
Rhino.DocObjects.RhinoObject o = Rhino.RhinoDoc.ActiveDoc.Objects.Find(G);

//Find block with similar name and delete them all
String blockName = "ObjectName ";
List<int> deleteId = new List<int>();

foreach(Rhino.DocObjects.InstanceDefinition idef in Rhino.RhinoDoc.ActiveDoc.InstanceDefinitions) {
int len = blockName.Length;
if(idef.Name.Length > len)
if(idef.Name.Substring(0, len) == blockName)
deleteId.Add(idef.Index);
}

foreach(int i in deleteId)
Rhino.RhinoDoc.ActiveDoc.InstanceDefinitions.Delete(i, true, true);

//Create block
BoundingBox bbox = RhinoDoc.ActiveDoc.Objects.Find(G).Geometry.GetBoundingBox(true);
Point3d origin = new Point3d(bbox.Center.X, bbox.Center.Y, bbox.Min.Z);
int idx = Rhino.RhinoDoc.ActiveDoc.InstanceDefinitions.Add(blockName + System.DateTime.Now.ToString(), "DESCRIPTION", origin, o.Geometry, o.Attributes);

//Copy paste stuff
Random rand = new Random(0);

if(idx != -1){
for(int i = 0; i < 10000; i++) {
Vector3d v = new Vector3d(rand.NextDouble() * x, rand.NextDouble() * y, rand.NextDouble() * z);
Transform t = Transform.Translation(v);
Rhino.RhinoDoc.ActiveDoc.Objects.AddInstanceObject(idx, t);
}
}

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service