Esempio n. 1
0
            protected override Result RunCommand(RhinoDoc doc, RunMode mode)
            {
                string family = "";

                // Get family from user
                var getFamily = new GetString();

                getFamily.SetCommandPrompt("Enter Family name");
                getFamily.Get();
                family = getFamily.StringResult();
                if (string.IsNullOrEmpty(family.Trim()))
                {
                    return(Result.Nothing);
                }

                // get point selection in and create blocks from each group
                var getObj = new GetObject();

                getObj.SetCommandPrompt("Select points in order");
                getObj.SubObjectSelect    = false;
                getObj.GeometryFilter     = ObjectType.Point;
                getObj.OneByOnePostSelect = true;
                List <RhinoObject> selectedObjects = new List <RhinoObject>();

                for (; ;)
                {
                    GetResult res = getObj.GetMultiple(3, 0);
                    if (res == GetResult.Object)
                    {
                        //make block from points and assign adaptive component schema
                        var blockName = $"AdaptiveComponent-{Guid.NewGuid()}";

                        // Gather all of the selected objects
                        var geometry   = new List <Rhino.Geometry.GeometryBase>();
                        var attributes = new List <ObjectAttributes>();
                        for (int i = 0; i < getObj.ObjectCount; i++)
                        {
                            var rhinoObject = getObj.Object(i).Object();
                            if (rhinoObject != null)
                            {
                                geometry.Add(rhinoObject.Geometry);
                                attributes.Add(rhinoObject.Attributes);
                            }
                        }
                        var basePoint = geometry.First() as Rhino.Geometry.Point; // Use first selected point as basepoint

                        // Gather all of the selected objects and make definition
                        int definitionIndex = doc.InstanceDefinitions.Add(blockName, string.Empty, basePoint.Location, geometry, attributes);
                        if (definitionIndex < 0)
                        {
                            RhinoApp.WriteLine("Unable to create block definition ", blockName);
                            return(Result.Failure);
                        }

                        // add the objects to a block instance
                        var  transform  = Rhino.Geometry.Transform.Translation(basePoint.Location - doc.ModelBasepoint);
                        Guid instanceId = doc.Objects.AddInstanceObject(definitionIndex, transform);
                        if (instanceId == Guid.Empty)
                        {
                            RhinoApp.WriteLine("Unable to create block instance ", blockName);
                            return(Result.Failure);
                        }
                        var instance = doc.Objects.FindId(instanceId) as InstanceObject;

                        // attach user string to block instance
                        ApplyAdaptiveComponent(instance, family, family, doc);

                        // clear everything for next selection
                        selectedObjects.Clear();
                        getObj.ClearObjects();
                        continue;
                    }
                    else if (res == GetResult.Cancel)
                    {
                        break;
                    }
                    break;
                }

                return(Result.Success);
            }