static void RhinoUpdateObjectGroups(ref RhinoObject obj, ref Dictionary<int, int> group_map)
    {
        if (obj == null) return;

        int attrib_group_count = obj.Attributes.GroupCount;
        if (attrib_group_count == 0) return;

        var doc = obj.Document;
        if (doc == null) return;

        var groups = doc.Groups;

        int group_count = groups.Count;
        if (group_count == 0) return;

        if (group_map.Count == 0)
          for (int i = 0; i < group_count; i++)
        group_map.Add(i, -1);

        var attributes = obj.Attributes;
        var group_list = attributes.GetGroupList();
        if (group_list == null) return;
        attrib_group_count = group_list.Length;

        for (int i = 0; i < attrib_group_count; i++)
        {
          int old_group_index = group_list[i];
          int new_group_index = group_map[old_group_index];
          if (new_group_index == -1)
          {
        new_group_index = doc.Groups.Add();
        group_map[old_group_index] = new_group_index;
          }
          group_list[i] = new_group_index;
        }

        attributes.RemoveFromAllGroups();
        for (int i = 0; i < attrib_group_count; i++)
          attributes.AddToGroup(group_list[i]);

        obj.CommitChanges();
    }
    public static Rhino.Commands.Result AddTexture(Rhino.RhinoDoc doc)
    {
        // Select object to add texture
        const ObjectType filter = Rhino.DocObjects.ObjectType.Surface |
                                  Rhino.DocObjects.ObjectType.PolysrfFilter |
                                  Rhino.DocObjects.ObjectType.Mesh;

        Rhino.DocObjects.ObjRef objref;
        Rhino.Commands.Result   rc = Rhino.Input.RhinoGet.GetOneObject("Select object to add texture", false, filter, out objref);
        if (rc != Rhino.Commands.Result.Success)
        {
            return(rc);
        }

        Rhino.DocObjects.RhinoObject rhino_object = objref.Object();
        if (rhino_object == null)
        {
            return(Rhino.Commands.Result.Failure);
        }

        // Select texture
        Rhino.UI.OpenFileDialog fd = new Rhino.UI.OpenFileDialog();
        fd.Filter = "Image Files (*.bmp;*.png;*.jpg)|*.bmp;*.png;*.jpg";
        if (fd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
        {
            return(Rhino.Commands.Result.Cancel);
        }

        // Verify texture
        string bitmap_filename = fd.FileName;

        if (string.IsNullOrEmpty(bitmap_filename) || !System.IO.File.Exists(bitmap_filename))
        {
            return(Rhino.Commands.Result.Nothing);
        }

        // Make sure the object has it's material source set to "material_from_object"
        rhino_object.Attributes.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject;

        // Make sure the object has a material assigned
        int material_index = rhino_object.Attributes.MaterialIndex;

        if (material_index < 0)
        {
            // Create a new material based on Rhino's default material
            material_index = doc.Materials.Add();
            // Assign the new material (index) to the object.
            rhino_object.Attributes.MaterialIndex = material_index;
        }

        if (material_index >= 0)
        {
            Rhino.DocObjects.Material mat = doc.Materials[material_index];
            mat.SetBumpTexture(bitmap_filename);
            mat.CommitChanges();

            //Don't forget to update the object, if necessary
            rhino_object.CommitChanges();

            doc.Views.Redraw();
            return(Rhino.Commands.Result.Success);
        }

        return(Rhino.Commands.Result.Failure);
    }
Exemple #3
0
        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);
        }