public static void DrawInstanceObject(this DisplayPipeline display, InstanceObject instanceObject, Transform xForm, bool highlight) { instanceObject.Explode(false, out var pieces, out _, out var pieceTransforms); var geo = instanceObject.Geometry; for (int i = 0; i < pieces.Length; i++) { if (highlight) { display.DrawObjectHighlighted(pieces[i], xForm * pieceTransforms[i]); } else { display.DrawObject(pieces[i], xForm * pieceTransforms[i]); } } }
protected override Result RunCommand(RhinoDoc doc, RunMode mode) { const Rhino.DocObjects.ObjectType geometryFilter = Rhino.DocObjects.ObjectType.Surface | Rhino.DocObjects.ObjectType.PolysrfFilter; Rhino.DocObjects.ObjRef objref; Result rc = Rhino.Input.RhinoGet.GetOneObject("Select first/main polysurface or surface to bool", false, geometryFilter, out objref); if (rc != Rhino.Commands.Result.Success) { return(rc); } if (objref == null) { return(Rhino.Commands.Result.Failure); } Rhino.Geometry.Brep brep = objref.Brep(); bool resIssolid = brep.IsSolid; if (!resIssolid) { Dialogs.ShowMessage("Your polysurface or surface is not solid! Result might not be valid!", "Warning!"); } Guid firstBrep = objref.ObjectId; doc.Objects.UnselectAll(true); //Select rest of polysurfaces or surfaces to bool Rhino.Input.Custom.GetObject go = new Rhino.Input.Custom.GetObject(); go.SetCommandPrompt("Select rest of polysurfaces or surfaces to bool"); go.GeometryFilter = geometryFilter | Rhino.DocObjects.ObjectType.InstanceReference; go.GroupSelect = true; go.GetMultiple(1, 0); bool isSolid = true; //Add set to breps list List <Rhino.Geometry.Brep> breps = new List <Rhino.Geometry.Brep>(); for (int i = 0; i < go.ObjectCount; i++) { //Explode if instance object and add to breps list if (go.Object(i).Object() is InstanceObject) { InstanceObject instObj = go.Object(i).Object() as InstanceObject; RhinoObject[] explodedObjects; ObjectAttributes[] attributesOfExplodedObjects; Transform[] transformOfExplodedObjects; instObj.Explode(false, out explodedObjects, out attributesOfExplodedObjects, out transformOfExplodedObjects); Guid addedObjectID = doc.Objects.Add(explodedObjects[0].Geometry, explodedObjects[0].Attributes); ObjRef objrefs = new Rhino.DocObjects.ObjRef(addedObjectID); Rhino.Geometry.Brep brepd = objrefs.Brep(); brepd.Transform(transformOfExplodedObjects[0]); resIssolid = brepd.IsSolid; if (!resIssolid) { isSolid = false; } if (brepd != null && firstBrep != addedObjectID) { breps.Add(brepd); } doc.Objects.Delete(addedObjectID, true); } else { Rhino.DocObjects.ObjRef objrefs = go.Object(i); Rhino.Geometry.Brep brepd = objrefs.Brep(); resIssolid = brepd.IsSolid; if (!resIssolid) { isSolid = false; } if (brepd != null && firstBrep != objrefs.ObjectId) { breps.Add(brepd); } } } if (!isSolid) { Dialogs.ShowMessage("At least on polysurface or surface to subtract is not solid! Result might not be valid!", "Warning!"); } doc.Objects.UnselectAll(true); //Create layers for failed and successfull booleans if not already existing var fail_layer_index = doc.Layers.FindName("FJ Boolean Fails"); if (fail_layer_index == null) { string name_fail_layer = "FJ Boolean Fails"; Rhino.DocObjects.Layer boolFail = new Rhino.DocObjects.Layer(); boolFail.Name = name_fail_layer; boolFail.Color = System.Drawing.Color.Red; doc.Layers.Add(boolFail); fail_layer_index = doc.Layers.FindName(name_fail_layer); } var done_layer_index = doc.Layers.FindName("FJ Boolean Done"); if (done_layer_index == null) { string name_done_layer = "FJ Boolean Done"; Rhino.DocObjects.Layer boolDone = new Rhino.DocObjects.Layer(); boolDone.Name = name_done_layer; boolDone.Color = System.Drawing.Color.BlueViolet; doc.Layers.Add(boolDone); done_layer_index = doc.Layers.FindName(name_done_layer); } //Compute boolean union double tolerance = doc.ModelAbsoluteTolerance; int a = 0; for (int i = 0; i < breps.Count; i++) { RhinoApp.WriteLine("computing number: " + i + " of: " + breps.Count + " operations..."); List <Brep> brepBool = new List <Brep>(); brepBool.Add(brep); brepBool.Add(breps[i]); Rhino.Geometry.Brep[] brepBoolNew = Rhino.Geometry.Brep.CreateBooleanUnion(brepBool, tolerance, true); if (brepBoolNew == null || brepBoolNew.Length > 1) { a++; doc.Objects.Delete(go.Object(i).Object()); var boolresultfail = doc.Objects.AddBrep(breps[i]); ObjRef objFailref = new ObjRef(boolresultfail); Rhino.DocObjects.RhinoObject objFail = objFailref.Object(); objFail.Attributes.LayerIndex = fail_layer_index.Index; objFail.CommitChanges(); } else { brep = brepBoolNew[0]; doc.Objects.Delete(go.Object(i).Object()); var boolresult = doc.Objects.AddBrep(breps[i]); ObjRef obj20ref = new ObjRef(boolresult); Rhino.DocObjects.RhinoObject obj2Org = obj20ref.Object(); obj2Org.Attributes.LayerIndex = done_layer_index.Index; obj2Org.CommitChanges(); } doc.Views.Redraw(); } RhinoApp.WriteLine(a + " of " + breps.Count + " operations failed!"); Rhino.DocObjects.RhinoObject obj1Org = objref.Object(); doc.Objects.Delete(obj1Org); doc.Objects.AddBrep(brep); doc.Views.Redraw(); return(Rhino.Commands.Result.Success); }