Пример #1
0
        public static void ConfirmLayerColor(Color color, string layerName, string layerParentName)
        {
            EnsureLayer(layerName, layerParentName);

            RhinoDoc doc = RhinoDoc.ActiveDoc;

            //Get a fresh copy of the LayerTable
            Rhino.DocObjects.Tables.LayerTable layerTable = doc.Layers;
            //Build a search string to see if the layer exists already
            string layerNameSearchString = "";

            if (layerParentName != null && layerParentName != "")
            {
                layerNameSearchString += layerParentName + "::";
            }
            layerNameSearchString += layerName;

            RhinoApp.WriteLine("Changing color of '" + layerNameSearchString + ".");


            //Run the actual check to see if the Layer exists
            RhinoApp.WriteLine("Checking '" + layerNameSearchString + "' to see if it exists.");
            int layerIndex = layerTable.FindByFullPath(layerNameSearchString, -1);

            if (layerIndex >= 0)
            {
                Layer layerToChange = layerTable.FindIndex(layerIndex);
                layerToChange.Color = color;
            }
        }
Пример #2
0
        public static string EnsureLayer(string layerName, string parentLayer)
        {
            RhinoDoc doc = RhinoDoc.ActiveDoc;

            //Get a fresh copy of the LayerTable
            Rhino.DocObjects.Tables.LayerTable layerTable = doc.Layers;
            //Build a search string to see if the layer exists already
            string layerNameSearchString = "";

            if (parentLayer != null && parentLayer != "")
            {
                layerNameSearchString += parentLayer + "::";
            }
            layerNameSearchString += layerName;
            //Run the actual check to see if the Layer exists
            RhinoApp.WriteLine("Checking '" + layerNameSearchString + "' to see if it exists.");
            int layerIndex = layerTable.FindByFullPath(layerNameSearchString, -1);

            //The layer was found, return it
            if (layerIndex >= 0)
            {
                RhinoApp.WriteLine("Layer '" + layerNameSearchString + "' was found.");
                RhinoApp.WriteLine("Moving on.");
                return(layerNameSearchString);
            }
            //This layer was not found/ does not exist... yet
            else
            {
                RhinoApp.WriteLine("Layer '" + layerNameSearchString + "' was not found.");
                //Create the layer
                RhinoApp.WriteLine("Creating layer '" + layerName + "'.");
                Rhino.DocObjects.Layer layer = new Rhino.DocObjects.Layer();
                layer.Name = layerName;
                //Make the new layer a child of the parent layer (if there was one)
                if (parentLayer != null && parentLayer != "")
                {
                    RhinoApp.WriteLine("Setting layer '" + layerName + "' as a child of layer '" + parentLayer + "'.");
                    int parentLayerIndex = layerTable.FindByFullPath(parentLayer, -1);
                    Rhino.DocObjects.Layer parentLayerObject = layerTable.FindIndex(parentLayerIndex);
                    layer.ParentLayerId = parentLayerObject.Id;
                }
                //Add the layer to the document
                layerTable.Add(layer);
                //Return the newly created layer
                return(layerNameSearchString);
            }
        }
Пример #3
0
        public static Rhino.DocObjects.Layer EnsureFullPath(string fullpath)
        {
            RhinoDoc doc = RhinoDoc.ActiveDoc;

            //Get a fresh copy of the LayerTable
            Rhino.DocObjects.Tables.LayerTable layerTable = doc.Layers;
            //Run the actual check to see if the Layer exists
            RhinoApp.WriteLine("Checking " + fullpath + " to see if it exists.");
            int layerIndex = layerTable.FindByFullPath(fullpath, -1);

            //The layer was found, return it
            if (layerIndex >= 0)
            {
                RhinoApp.WriteLine("That layer does exist.");
                return(layerTable.FindIndex(layerIndex));
            }
            //This layer was not found/ does not exist... yet
            else
            {
                RhinoApp.WriteLine("That layer does not exist.");
                //Ensure the layer's parent exists (recursive)
                int    splitPoint = fullpath.LastIndexOf("::");
                string parentPath = fullpath.Substring(0, splitPoint);
                string childLayer = fullpath.Substring(1, splitPoint);
                Rhino.DocObjects.Layer parentLayer = null;
                if (parentPath.Length > 0)
                {
                    RhinoApp.WriteLine("Checking to see if parent layer exists...");
                    parentLayer = EnsureFullPath(parentPath);
                }
                //Create this new layer (on the parent path)
                RhinoApp.WriteLine("Creating layer " + childLayer + "...");
                Rhino.DocObjects.Layer layer = new Rhino.DocObjects.Layer();
                layer.Name = childLayer;
                //Make the new layer a child of the parent layer (if there was one)
                if (parentLayer != null)
                {
                    RhinoApp.WriteLine("Assigning layer " + childLayer + " as of child of " + parentPath); //That layer does not exist.");
                    layer.ParentLayerId = parentLayer.Id;
                }
                //Add the layer to the document
                layerTable.Add(layer);
                //Return the newly created layer
                return(layer);
            }
        }
Пример #4
0
        public static void BakeObjectToLayer(GeometryBase obj, string layerName, string rootLayerPath)
        {
            string   parentOfNestedLayer = null;
            RhinoDoc doc = RhinoDoc.ActiveDoc;


            //Check geometry object is valid
            if (obj == null)
            {
                RhinoApp.WriteLine("Object is null...");
                //this.Component.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Geometry object is null");
                return;
            }
            if (layerName == null)
            {
                RhinoApp.WriteLine("Target layer is null...");
                //this.Component.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Target Layer is null");
                return;
            }

            if (!String.IsNullOrEmpty(rootLayerPath))
            {
                parentOfNestedLayer = CheckLayerStructure(rootLayerPath);
            }

            //Check layer name is valid
            if (string.IsNullOrEmpty(layerName) || !Rhino.DocObjects.Layer.IsValidName(layerName))
            {
                RhinoApp.WriteLine("Layer name " + layerName + " is invalid");
                return;
                //this.Component.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Layer name " + layerName + " is invalid");
            }
            //Ensure the target layer exists (at the end of the optional root path)
            RhinoApp.WriteLine("Ensuring child layer...");
            string ensuredLayer = EnsureLayer(layerName, parentOfNestedLayer);

            RhinoApp.WriteLine("-------------------------");
            //Make new attribute to set name
            Rhino.DocObjects.ObjectAttributes  att        = new Rhino.DocObjects.ObjectAttributes();
            Rhino.DocObjects.Tables.LayerTable layerTable = doc.Layers;
            int layerIndex = layerTable.FindByFullPath(ensuredLayer, -1);

            att.LayerIndex = layerIndex;
            Bake(obj, att);
        }
Пример #5
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 <Curve>  C      = new List <Curve>();
            List <string> Layer  = new List <string>();
            List <Color>  LColor = new List <Color>();

            if (!DA.GetDataList(0, C))
            {
                return;
            }
            if (C == null || C.Count == 0)
            {
                return;
            }

            if (!DA.GetDataList(1, Layer))
            {
                for (int i = 0; i < C.Count; i++)
                {
                    Layer.Add("Default");
                }
            }
            if (Layer == null || Layer.Count != C.Count)
            {
                return;
            }

            if (!DA.GetDataList(2, LColor))
            {
                for (int i = 0; i < C.Count; i++)
                {
                    LColor.Add(Color.Black);
                }
            }
            if (LColor == null || LColor.Count != C.Count)
            {
                return;
            }

            bool PreserveUnits = true, ViewBound = true, ColStyle = true, SolidHatch = true, OrderLayers = true;

            DA.GetData(3, ref PreserveUnits);
            DA.GetData(4, ref ViewBound);
            DA.GetData(5, ref ColStyle);
            DA.GetData(6, ref SolidHatch);
            DA.GetData(7, ref OrderLayers);

            string path = "", name = "";

            if (!DA.GetData(8, ref path))
            {
                return;
            }
            if (!DA.GetData(9, ref name))
            {
                return;
            }

            bool export = false;

            DA.GetData(10, ref export);

            // NOTE
            // normally, a button would freeze GH canvas after a RunScript operation, while a Toggle would not;
            // this fix (use a pending variable) was suggested by Florian Frank here:
            // https://www.grasshopper3d.com/forum/topics/boolean-button-runscript
            if (!export && !pending)
            {
                return;
            }

            // as suggested by Rutten here: https://discourse.mcneel.com/t/rhino-command-export-in-c/63610/4
            if (Rhino.RhinoDoc.ActiveDoc == null)
            {
                return;
            }

            if (!pending)
            {
                pending = true;
                return;
            }

            pending = false;
            // export options
            string presUn   = PreserveUnits ? "Yes" : "No";
            string vBound   = ViewBound ? "Yes" : "No";
            string cs       = ColStyle ? "RGB" : "CMYK";
            string solHatch = SolidHatch ? "Yes" : "No";
            string ordLay   = OrderLayers ? "Yes" : "No";

            //                                         Yes/No                          Yes/No            RGB/CMYK                          Yes/No                      Yes/No
            string scriptOptions = " PreserveUnits=" + presUn + " ViewportBoundary=" + vBound + " Color=" + cs + " HatchesAsSolidFills=" + solHatch + " OrderLayers=" + ordLay;
            string scriptString  = "-_Export " + '"' + path + name + ".ai" + '"' + scriptOptions + " _Enter";

            Guid[]   id  = new Guid[C.Count];
            string[] lay = Layer.ToArray();
            Color[]  col = LColor.ToArray();

            // lists of effectively added layers
            List <int> addedLayers = new List <int>();

            //Make new attribute to set name
            Rhino.DocObjects.ObjectAttributes att = new Rhino.DocObjects.ObjectAttributes();

            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }

            Rhino.RhinoApp.RunScript("_SelNone", false);
            IEnumerator it   = C.GetEnumerator();
            int         k    = 0;
            int         ind  = 0;
            int         cInd = 0;

            foreach (Curve curve in C)
            {
                //Set layer
                if (!string.IsNullOrEmpty(lay[ind]) && Rhino.DocObjects.Layer.IsValidName(lay[ind]))
                {
                    //Get the current layer index
                    Rhino.DocObjects.Tables.LayerTable layerTable = Rhino.RhinoDoc.ActiveDoc.Layers;
                    //int layerIndex = layerTable.Find(lay[ind], true);
                    int layerIndex = layerTable.FindByFullPath(lay[ind], -1);

                    if (layerIndex < 0)                                                //This layer does not exist, we add it
                    {
                        Rhino.DocObjects.Layer onlayer = new Rhino.DocObjects.Layer(); //Make a new layer
                        onlayer.Name = lay[ind];

                        onlayer.Color = col[ind];

                        layerIndex = layerTable.Add(onlayer); //Add the layer to the layer table
                        if (layerIndex > -1)                  //We managed to add layer!
                        {
                            att.LayerIndex = layerIndex;
                            addedLayers.Add(layerIndex); // add index to the newly added layers list
                                                         //Print("Added new layer to the document at position " + layerIndex + " named " + Layer + ". ");
                        } //else
                          //Print("Layer did not add. Try cleaning up your layers."); //This never happened to me.
                    }
                    else
                    {
                        att.LayerIndex = layerIndex; //We simply add to the existing layer
                    }
                }

                // bake and select the object
                id[k] = Rhino.RhinoDoc.ActiveDoc.Objects.AddCurve(curve, att);
                Rhino.RhinoDoc.ActiveDoc.Objects.Select(id[k], true);

                // increment indexes
                k++;
                if (ind < lay.Length - 1)
                {
                    ind++;
                }
                if (cInd < col.Length - 1)
                {
                    cInd++;
                }
            }

            Rhino.RhinoApp.RunScript(scriptString, false);
            Rhino.RhinoApp.RunScript("_delete", false);


            // delete only the added layers
            Rhino.DocObjects.Tables.LayerTable layerTableNew = Rhino.RhinoDoc.ActiveDoc.Layers;

            foreach (int lInd in addedLayers)
            {
                layerTableNew.Delete(lInd, true);
            }
        }
Пример #6
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)
        {
            /*
             * Original Version Written by Giulio Piacentino - 2010 11 21 - for Grasshopper 0.8.002
             * Enhanced by Co-de-iT (Alessio) - now bakes on chosen layer(s) and in groups
             */
            GeometryBase obj = null;

            if (!DA.GetData(0, ref obj))
            {
                return;
            }

            string name = "";

            DA.GetData(1, ref name);
            string layer = "";

            DA.GetData(2, ref layer);

            Color color = new Color();

            DA.GetData(3, ref color);
            Object material = new Object();

            DA.GetData(4, ref material);
            int group_n = 0;

            DA.GetData(5, ref group_n);

            bool group = false;

            DA.GetData(6, ref group);
            bool bake_iT = false;

            DA.GetData(7, ref bake_iT);

            if (!bake_iT)
            {
                return;
            }

            //Make new attribute to set name
            Rhino.DocObjects.ObjectAttributes att = new Rhino.DocObjects.ObjectAttributes();

            //Set object name
            if (!string.IsNullOrEmpty(name))
            {
                att.Name = name;
            }

            //Set color
            if (!color.IsEmpty)
            {
                att.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject; //Make the color type "by object"
                att.ObjectColor = color;

                att.PlotColorSource = Rhino.DocObjects.ObjectPlotColorSource.PlotColorFromObject; //Make the plot color type "by object"
                att.PlotColor       = color;
            }

            // Set group

            if (group)
            {
                Rhino.RhinoDoc.ActiveDoc.Groups.Add(Convert.ToString(group_n));
                att.AddToGroup(group_n);
            }

            //Set layer
            if (!string.IsNullOrEmpty(layer) && Rhino.DocObjects.Layer.IsValidName(layer))
            {
                //Get the current layer index
                Rhino.DocObjects.Tables.LayerTable layerTable = Rhino.RhinoDoc.ActiveDoc.Layers;
                //int layerIndex = layerTable.Find(layer, true);
                int layerIndex = layerTable.FindByFullPath(layer, -1);

                if (layerIndex < 0)                                                //This layer does not exist, we add it
                {
                    Rhino.DocObjects.Layer onlayer = new Rhino.DocObjects.Layer(); //Make a new layer
                    onlayer.Name  = layer;
                    onlayer.Color = Color.Gainsboro;                               // sets new layer color - future dev: choose new layers color

                    layerIndex = layerTable.Add(onlayer);                          //Add the layer to the layer table
                    if (layerIndex > -1)                                           //We managed to add the layer!
                    {
                        att.LayerIndex = layerIndex;
                        //Print("Added new layer to the document at position " + layerIndex + " named " + layer + ". ");
                    }
                    //else
                    //Print("Layer did not add. Try cleaning up your layers."); //This never happened to me.
                }
                else
                {
                    att.LayerIndex = layerIndex; //We simply add to the existing layer
                }
            }


            //Set plotweight
            //if (pWidth > 0)
            //{
            //    att.PlotWeightSource = Rhino.DocObjects.ObjectPlotWeightSource.PlotWeightFromObject;
            //    att.PlotWeight = pWidth;
            //}


            //Set material

            bool materialByName = !string.IsNullOrEmpty(material as string);

            Rhino.Display.DisplayMaterial inMaterial;
            if (material is GH_Material)
            {
                GH_Material gMat = material as GH_Material;
                inMaterial = gMat.Value as Rhino.Display.DisplayMaterial;
            }
            else
            {
                inMaterial = material as Rhino.Display.DisplayMaterial;
            }
            if (material is Color)
            {
                inMaterial = new Rhino.Display.DisplayMaterial((Color)material);
            }
            if (material != null && inMaterial == null && !materialByName)
            {
                if (!(material is string))
                {
                    try //We also resort to try with IConvertible
                    {
                        inMaterial = (Rhino.Display.DisplayMaterial)Convert.ChangeType(material, typeof(Rhino.Display.DisplayMaterial));
                    }
                    catch (InvalidCastException)
                    {
                    }
                }
            }
            if (inMaterial != null || materialByName)
            {
                string matName;

                if (!materialByName)
                {
                    matName = string.Format("D:{0}-E:{1}-S:{2},{3}-T:{4}",
                                            Format(inMaterial.Diffuse),
                                            Format(inMaterial.Emission),
                                            Format(inMaterial.Specular),
                                            inMaterial.Shine.ToString(),
                                            inMaterial.Transparency.ToString()
                                            );
                }
                else
                {
                    matName = (string)material;
                }

                int materialIndex = Rhino.RhinoDoc.ActiveDoc.Materials.Find(matName, true);
                if (materialIndex < 0 && !materialByName)                     //Material does not exist and we have its specs
                {
                    materialIndex = Rhino.RhinoDoc.ActiveDoc.Materials.Add(); //Let's add it
                    if (materialIndex > -1)
                    {
                        //Print("Added new material at position " + materialIndex + " named \"" + matName + "\". ");
                        Rhino.DocObjects.Material m = Rhino.RhinoDoc.ActiveDoc.Materials[materialIndex];
                        m.Name          = matName;
                        m.DiffuseColor  = inMaterial.Diffuse;
                        m.EmissionColor = inMaterial.Emission;
                        //m.ReflectionColor = inMaterial.Specular;
                        m.SpecularColor = inMaterial.Specular;
                        m.Shine         = inMaterial.Shine;
                        m.Transparency  = inMaterial.Transparency;
                        //m.TransparentColor = no equivalent

                        m.CommitChanges();

                        att.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject;
                        att.MaterialIndex  = materialIndex;
                    }
                    else
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Material did not add. Try cleaning up your materials."); //This never happened to me.
                    }
                }
                else if (materialIndex < 0 && materialByName) //Material does not exist and we do not have its specs. We do nothing
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Warning: material name not found. I cannot set the source to this material name. Add a material with name: " + matName);
                }
                else
                {
                    //If this material exists, we do not replace it!
                    att.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject;
                    att.MaterialIndex  = materialIndex;
                }
            }

            //Set wire density
            //if (wires == -1 || wires > 0)
            //{
            //    att.WireDensity = wires;
            //}


            //Bake to the right type of object
            switch (obj.ObjectType)
            {
            case Rhino.DocObjects.ObjectType.Brep:
                Rhino.RhinoDoc.ActiveDoc.Objects.AddBrep(obj as Brep, att);
                break;

            case Rhino.DocObjects.ObjectType.Curve:
                Rhino.RhinoDoc.ActiveDoc.Objects.AddCurve(obj as Curve, att);
                break;

            case Rhino.DocObjects.ObjectType.Point:
                Rhino.RhinoDoc.ActiveDoc.Objects.AddPoint((obj as Rhino.Geometry.Point).Location, att);
                break;

            case Rhino.DocObjects.ObjectType.Surface:
                Rhino.RhinoDoc.ActiveDoc.Objects.AddSurface(obj as Surface, att);
                break;

            case Rhino.DocObjects.ObjectType.Mesh:
                Rhino.RhinoDoc.ActiveDoc.Objects.AddMesh(obj as Mesh, att);
                break;

            case Rhino.DocObjects.ObjectType.Extrusion:
                typeof(Rhino.DocObjects.Tables.ObjectTable).InvokeMember("AddExtrusion", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.InvokeMethod, null, Rhino.RhinoDoc.ActiveDoc.Objects, new object[] { obj, att });
                break;

            case Rhino.DocObjects.ObjectType.PointSet:
                Rhino.RhinoDoc.ActiveDoc.Objects.AddPointCloud(obj as Rhino.Geometry.PointCloud, att);     //This is a speculative entry
                break;

            default:
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "The script does not know how to handle this type of geometry: " + obj.GetType().FullName);
                break;
            }
        }