Exemplo n.º 1
0
        public static IDictionary MergeElementTypes(DynaElem FromType, DynaElem ToType)
        {
            //  Name of Transaction
            string transactionName = "Merge Element Type";

            // Get the Revit elements from the Dynamo Elements
            RevitDB.ElementType rFromType = (RevitDB.ElementType)FromType.InternalElement;
            RevitDB.ElementType rToType   = (RevitDB.ElementType)ToType.InternalElement;

            RevitDoc document = rToType.Document;

            // Collect all instances of FromType
            RevitDB.FilteredElementCollector collector        = new RevitDB.FilteredElementCollector(document);
            RevitDB.BuiltInParameter         parameterId      = RevitDB.BuiltInParameter.ELEM_TYPE_PARAM;
            RevitDB.FilterNumericEquals      filterNumberRule = new RevitDB.FilterNumericEquals();
            RevitDB.ParameterValueProvider   provider         = new RevitDB.ParameterValueProvider(new RevitDB.ElementId(parameterId));
            RevitDB.FilterRule    filterRule      = new RevitDB.FilterElementIdRule(provider, filterNumberRule, rFromType.Id);
            RevitDB.ElementFilter filterParameter = new RevitDB.ElementParameterFilter(filterRule, false);

            Type instanceType = Select.InstanceClassFromTypeClass(rFromType.GetType());

            if (instanceType != null)
            {
                collector.OfClass(instanceType);
            }

            IEnumerable <RevitDB.Element> instances = collector
                                                      .WhereElementIsNotElementType()
                                                      .WherePasses(filterParameter)
                                                      .ToElements();

            // Intialize list for elements that are successfully merged and failed to merge.
            List <DynaElem> elements       = new List <DynaElem>();
            List <DynaElem> elementsFailed = new List <DynaElem>();

            // Define Function to change instances types.
            Action <IEnumerable <RevitDB.Element> > _SetType = (isntances) =>
            {
                foreach (RevitDB.Element elem in instances)
                {
                    // If Element is in a group, put the element in the failed list
                    int groupId = elem.GroupId.IntegerValue;
                    if (groupId == -1)
                    {
                        //elem.TextNoteType = rToType;
                        RevitDB.Parameter param = elem.get_Parameter(RevitDB.BuiltInParameter.ELEM_TYPE_PARAM);
                        param.Set(rToType.Id);
                        DynaElem dElem = elem.ToDSType(true);
                        elements.Add(dElem);
                    }
                    else
                    {
                        DynaElem dElem = elem.ToDSType(true);
                        elementsFailed.Add(dElem);
                    }
                }

                // Check if there are any instances of FromType left
                int count = collector.Count();
                if (count == 0)
                {
                    document.Delete(rFromType.Id);
                }
            };

            if (document.IsModifiable)
            {
                TransactionManager.Instance.EnsureInTransaction(document);
                _SetType(instances);
                TransactionManager.Instance.TransactionTaskDone();
            }
            else
            {
                using (Autodesk.Revit.DB.Transaction trans = new Autodesk.Revit.DB.Transaction(document))
                {
                    trans.Start(transactionName);
                    _SetType(instances);
                    trans.Commit();
                }
            }

            return(new Dictionary <string, object>
            {
                { "Merged", elements },
                { "Failed", elementsFailed }
            });
        }