Beispiel #1
0
    public static Rhino.Commands.Result AddMaterial(Rhino.RhinoDoc doc)
    {
        // materials are stored in the document's material table
        int index = doc.Materials.Add();

        Rhino.DocObjects.Material mat = doc.Materials[index];
        mat.DiffuseColor  = System.Drawing.Color.Chocolate;
        mat.SpecularColor = System.Drawing.Color.CadetBlue;
        mat.CommitChanges();

        // set up object attributes to say they use a specific material
        Rhino.Geometry.Sphere             sp   = new Rhino.Geometry.Sphere(Rhino.Geometry.Plane.WorldXY, 5);
        Rhino.DocObjects.ObjectAttributes attr = new Rhino.DocObjects.ObjectAttributes();
        attr.MaterialIndex  = index;
        attr.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject;
        doc.Objects.AddSphere(sp, attr);

        // add a sphere without the material attributes set
        sp.Center = new Rhino.Geometry.Point3d(10, 10, 0);
        doc.Objects.AddSphere(sp);

        doc.Views.Redraw();
        return(Rhino.Commands.Result.Success);
    }
Beispiel #2
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            List <IGH_GeometricGoo> objs = new List <IGH_GeometricGoo>();

            DA.GetDataList(0, objs);

            string layerName = "Default";
            object material  = null;
            bool   clearL    = false;

            DA.GetData(1, ref layerName);
            DA.GetData(2, ref material);

            /*Rhino.DocObjects.ObjectAttributes att = new Rhino.DocObjects.ObjectAttributes();
             * att.LayerIndex = doc.Layers.Find(layerName, true);
             * att.MaterialIndex = doc.Materials.Find(matName, true);*/

            Rhino.DocObjects.ObjectAttributes att = new Rhino.DocObjects.ObjectAttributes();


            //Set material
            if (material != null)
            {
                string          matName       = "";
                DisplayMaterial mat           = new DisplayMaterial();
                Color           col           = new Color();
                int             isName        = -1;
                int             materialIndex = -1;
                try { matName = (material as GH_String).Value; isName = 1; }
                catch
                {
                    try
                    {
                        mat    = (material as GH_Material).Value;
                        isName = 0;
                    }
                    catch
                    {
                        try { col = (material as GH_Colour).Value; mat = new DisplayMaterial(col); isName = 0; }
                        catch { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Can't identify material object. Please supply render material name, GH material or color."); }
                    }
                }


                if (isName == 1)
                {
                    materialIndex      = doc.Materials.Find(matName, true);
                    att.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject;
                    if (materialIndex < 0)
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Couldn't find material by name. If you're sure the material exists in the Rhino Material list, try adding it to one object manually. After that it should work.");
                    }
                    att.MaterialIndex = materialIndex;
                }

                else
                {
                    materialIndex = doc.Materials.Add();
                    if (materialIndex > -1)
                    {
                        Rhino.DocObjects.Material m = doc.Materials[materialIndex];
                        m.Name          = matName;
                        m.AmbientColor  = mat.Ambient;
                        m.DiffuseColor  = mat.Diffuse;
                        m.EmissionColor = mat.Emission;
                        //m.ReflectionColor = no equivalent
                        m.SpecularColor = mat.Specular;
                        m.Shine         = mat.Shine;
                        m.Transparency  = mat.Transparency;
                        //m.TransparentColor = no equivalent
                        m.CommitChanges();

                        att.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject;
                        att.MaterialIndex  = materialIndex;
                    }
                    else
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Couldn't add material. Try cleaning up your materials."); //This never happened to me.
                    }
                }
            }


            DA.GetData(3, ref att);
            DA.GetData(4, ref clearL);

            //Delete objects by GUID
            doc.Objects.Delete(ids, true);


            for (int i = 0; i < objs.Count; i++)
            {
                GeometryBase gb;
                Point3d      pt;
                Brep         b;
                if (ids.Count <= i)
                {
                    ids.Add(Guid.NewGuid());
                }

                att.ObjectId = ids[i];
                if (objs[i].CastTo <Point3d>(out pt))
                {
                    doc.Objects.AddPoint(pt, att);
                }
                else if (objs[i].CastTo <Brep>(out b))
                {
                    doc.Objects.AddBrep(b, att);
                }
                else if (objs[i].CastTo <GeometryBase>(out gb))
                {
                    doc.Objects.Add(gb, att);
                }

                else
                {
                    throw new Exception("Object nr. " + i + " is not bakeable:\n" + objs[i].ToString() + ".\nIf it's a box or a curve, try turning it into a Brep by wiring it through a Brep container component before feeding it to Instant Bake.");
                }
            }

            if (clearL)
            {
                foreach (Rhino.DocObjects.RhinoObject o in doc.Objects.FindByLayer(layerName))
                {
                    doc.Objects.Delete(o, true);
                }
            }

            if (counter >= 10)
            {
                Rhino.RhinoDoc.ActiveDoc.ClearUndoRecords(true);
                counter = 0;
            }


            DA.SetDataList(0, ids);
            counter++;
        }
Beispiel #3
0
    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())
        {
            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);
    }