public void SetCurrentLayer(string layer)
        {
            Rhino.DocObjects.Tables.LayerTable layers = RhinoDoc.ActiveDoc.Layers;
            int layerIndex = layers.Find(layer, true);

            layers.SetCurrentLayerIndex(layerIndex, true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates layers in the current Rhino Document, by using the layer data contained within each
        /// Parasite Object
        /// </summary>
        /// <param name="parasiteObjects"></param>
        /// <param name="doc"></param>
        /// <returns></returns>
        public static List <ObjectAttributes> CreateLayers(List <ParasiteObject> parasiteObjects, Rhino.RhinoDoc doc)
        {
            List <ObjectAttributes> objectAttributes = new List <ObjectAttributes>();

            for (int i = 0; i < parasiteObjects.Count; i++)
            {
                //Make new attribute to set name
                ObjectAttributes att = new ObjectAttributes();

                string parentLayerName = null;
                string childLayerName  = null;

                if (parasiteObjects[i].GetParameter(ParameterType.Layer.ToString(), out Parameter value))
                {
                    parentLayerName = value.GetValue();
                    AssertLayerNameValidity(parentLayerName);
                }


                if (parasiteObjects[i].GetParameter(ParameterType.ChildLayer.ToString(), out Parameter val))
                {
                    childLayerName = val.GetValue();
                    AssertLayerNameValidity(childLayerName);
                }

                if (childLayerName == null)
                {
                    throw new ParasiteArgumentException($"Could not retrieve Parameter of type {ParameterType.Layer} ");
                }
                if (parentLayerName == null)
                {
                    throw new ParasiteArgumentException($"Could not retrieve Parameter of type {ParameterType.Layer} ");
                }


                //Set layer
                if (!string.IsNullOrEmpty(parentLayerName) && Rhino.DocObjects.Layer.IsValidName(parentLayerName))
                {
                    //Get the current layer index
                    Rhino.DocObjects.Tables.LayerTable layerTable = doc.Layers;
                    int parentLayerIndex = layerTable.Find(parentLayerName, true);

                    if (parentLayerIndex < 0) //This layer does not exist, we add it
                    {
                        Layer onlayer = new Layer
                        {
                            Name  = parentLayerName,
                            Color = System.Drawing.Color.FromArgb(ran.Next(0, 255), ran.Next(0, 255), ran.Next(0, 255))
                        };                                          //Make a new layer

                        parentLayerIndex = layerTable.Add(onlayer); //Add the layer to the layer table

                        if (parentLayerIndex > -1)                  //We manged to add layer!
                        {
                            att.LayerIndex = parentLayerIndex;
                            att.LayerIndex = LayerFactory.AddChildLayer(parentLayerName, parentLayerIndex, childLayerName, doc);
                        }

                        else
                        {
                            throw new ParasiteArgumentException(string.Format("Unable to add {0} layer.", parentLayerName));
                        }
                    }

                    // Layer already exists
                    else
                    {
                        att.LayerIndex = parentLayerIndex;
                        att.LayerIndex = AddChildLayer(parentLayerName, parentLayerIndex, childLayerName, doc);
                    }
                }


                Layer childLayer = doc.Layers[att.LayerIndex];

                if (childLayer.Name == "Default")
                {
                    throw new ParasiteArgumentException("Child Layer was set to default!");
                }


                objectAttributes.Add(att);
            }

            return(objectAttributes);
        }