Exemplo n.º 1
0
        protected override void TrySolveInstance(IGH_DataAccess DA)
        {
            var elementList = new List <Types.IGH_Element>();

            if (!DA.GetDataList("Elements", elementList))
            {
                return;
            }

            try
            {
                var elementGroups = elementList.
                                    GroupBy(x => x.Document).
                                    ToArray();

                if (elementGroups.Length > 0)
                {
                    var transactionGroups = new Queue <DB.TransactionGroup>();

                    try
                    {
                        var _Deleted_  = Params.IndexOfOutputParam("Deleted");
                        var _Modified_ = Params.IndexOfOutputParam("Modified");

                        var Succeeded = true;

                        var Deleted = _Deleted_ < 0 ? default : new List <Types.Element>();

                                      var Modified = _Modified_ < 0 ? default : new List <Types.Element>();

                                                     foreach (var elementGroup in elementGroups)
                                                     {
                                                         var doc = elementGroup.Key;

                                                         // Start a transaction Group to be able to rollback in case changes in different documents fails.
                                                         {
                                                             var group = new DB.TransactionGroup(doc, Name);
                                                             if (group.Start() != DB.TransactionStatus.Started)
                                                             {
                                                                 group.Dispose();
                                                                 continue;
                                                             }
                                                             transactionGroups.Enqueue(group);
                                                         }

                                                         var elements = new HashSet <DB.ElementId>(elementGroup.Select(x => x.Id));

                                                         var result = Delete(doc, elements, Deleted, Modified);
                                                         if (result < 0)
                                                         {
                                                             Succeeded = false;
                                                         }
                                                         else
                                                         {
                                                             deletedElements += result;
                                                         }
                                                     }

                                                     if (Succeeded && !Simulated)
                                                     {
                                                         // Transactions in all documents succeded, it's fine to assimilate all changes as one.
                                                         while (transactionGroups.Count > 0)
                                                         {
                                                             using (var group = transactionGroups.Dequeue())
                                                             {
                                                                 group.Assimilate();
                                                             }
                                                         }
                                                     }
                                                     else
                                                     {
                                                         deletedElements = 0;
                                                     }

                                                     DA.SetData("Succeeded", Succeeded);

                                                     if (_Deleted_ >= 0)
                                                     {
                                                         DA.SetDataList(_Deleted_, Deleted);
                                                     }

                                                     if (_Modified_ >= 0)
                                                     {
                                                         DA.SetDataList(_Modified_, Modified);
                                                     }
                    }
                    catch (Autodesk.Revit.Exceptions.ArgumentException)
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "One or many of the elements cannot be deleted or are invalid.");
                        DA.SetData("Succeeded", false);
                    }
                    finally
                    {
                        // In case we still have transaction groups here something bad happened
                        foreach (var group in transactionGroups.Cast <DB.TransactionGroup>().Reverse())
                        {
                            using (group)
                            {
                                group.RollBack();
                            }
                        }
                        transactionGroups.Clear();
                    }
                }
            }
            catch (NullReferenceException)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "One or many of the elements are Null.");
            }
        }
Exemplo n.º 2
0
        protected override void TrySolveInstance(IGH_DataAccess DA)
        {
            var elementList = new List <Types.IGH_Element>();

            if (!DA.GetDataList("Elements", elementList))
            {
                return;
            }

            try
            {
                var elementGroups = elementList.
                                    GroupBy(x => x.Document).
                                    ToArray();

                if (elementGroups.Length > 0)
                {
                    var transactionGroups = new Queue <DB.TransactionGroup>();

                    try
                    {
                        var Succeeded = true;
                        var Deleted   = new List <Types.Element>();
                        var Modified  = new List <Types.Element>();

                        foreach (var elementGroup in elementGroups)
                        {
                            var doc = elementGroup.Key;

                            // Start a transaction Group to be able to rollback in case changes in different documents fails.
                            {
                                var group = new DB.TransactionGroup(doc, Name);
                                if (group.Start() != DB.TransactionStatus.Started)
                                {
                                    group.Dispose();
                                    continue;
                                }
                                transactionGroups.Enqueue(group);
                            }

                            ClassifyElementIds(elementGroup, out var elements, out var types);

                            // Step 1. Purge non ElementTypes
                            Succeeded &= Purge(doc, elements, Deleted, Modified);

                            // Step 2. Check if ElementTypes to be purged are still in use in the model by any Instance
                            if (types.Count > 0)
                            {
                                var typesInUse = new List <DB.ElementId>(types.Count);

                                // Remove purgable ElementTypes from types
                                if (doc.GetPurgableElementTypes(out var purgableTypes))
                                {
                                    foreach (var type in types)
                                    {
                                        if (!purgableTypes.Contains(type))
                                        {
                                            typesInUse.Add(type);
                                        }
                                    }
                                }

                                // Post a FailureMessage in case we have non purgable ElementTypes on types
                                if (typesInUse.Count > 0)
                                {
                                    using (var transaction = NewTransaction(doc))
                                    {
                                        transaction.Start();

                                        using (var message = new DB.FailureMessage(DBX.ExternalFailures.ElementFailures.FailedToPurgeElement))
                                        {
                                            message.SetFailingElements(typesInUse);
                                            doc.PostFailure(message);
                                        }

                                        CommitTransaction(doc, transaction);
                                    }

                                    Succeeded &= false;
                                }
                            }

                            // Step 3. Purge ElementTypes
                            // Types include typesInUse in order to get information about Deleted and Modified elements.
                            Succeeded &= Purge(doc, types, Deleted, Modified);
                        }

                        if (Succeeded && !Simulated)
                        {
                            deletedElements += Deleted.Count;

                            // Transactions in all documents succeded, it's fine to assimilate all changes as one.
                            while (transactionGroups.Count > 0)
                            {
                                using (var group = transactionGroups.Dequeue())
                                {
                                    group.Assimilate();
                                }
                            }
                        }

                        DA.SetData("Succeeded", Succeeded);
                        DA.SetDataList("Deleted", Deleted);
                        DA.SetDataList("Modified", Modified);
                    }
                    catch (Autodesk.Revit.Exceptions.ArgumentException)
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "One or many of the elements cannot be deleted or are invalid.");
                        DA.SetData("Succeeded", false);
                    }
                    finally
                    {
                        // In case we still have transaction groups here something bad happened
                        foreach (var group in transactionGroups.Cast <DB.TransactionGroup>().Reverse())
                        {
                            using (group)
                            {
                                group.RollBack();
                            }
                        }
                    }
                }
            }
            catch (NullReferenceException)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "One or many of the elements are Null.");
            }
        }
Exemplo n.º 3
0
        public static Result Execute
        (
            UIApplication app,
            DB.View view,
            IDictionary <string, string> JournalData,
            string filePath,
            ref string message
        )
        {
            var result = Result.Failed;

            if ((result = ReadFromFile(filePath, out var definition)) == Result.Succeeded)
            {
                using (definition)
                {
                    bool enableSolutions = GH_Document.EnableSolutions;
                    var  currentCulture  = Thread.CurrentThread.CurrentCulture;
                    try
                    {
                        using (var transGroup = new DB.TransactionGroup(app.ActiveUIDocument.Document))
                        {
                            transGroup.Start(Path.GetFileNameWithoutExtension(definition.Properties.ProjectFileName));

                            GH_Document.EnableSolutions = true;
                            definition.Enabled          = true;
                            definition.ExpireSolution();

                            var inputs = GetInputParams(definition);
                            result = PromptForInputs(app.ActiveUIDocument, inputs, out var values);
                            if (result != Result.Succeeded)
                            {
                                return(result);
                            }

                            // Update input volatile data values
                            foreach (var value in values)
                            {
                                value.Key.AddVolatileDataList(new Grasshopper.Kernel.Data.GH_Path(0), value.Value);
                            }

                            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                            using (var modal = new Rhinoceros.ModalScope())
                            {
                                definition.NewSolution(false, GH_SolutionMode.Silent);

                                do
                                {
                                    if (modal.Run(false, false) == Result.Failed)
                                    {
                                        return(Result.Failed);
                                    }
                                } while (definition.ScheduleDelay >= GH_Document.ScheduleRecursive);
                            }
                            Thread.CurrentThread.CurrentCulture = currentCulture;

                            if (definition.SolutionState == GH_ProcessStep.Aborted)
                            {
                                message = $"Solution aborted by user after ~{ definition.SolutionSpan.TotalSeconds} seconds";
                                return(Result.Cancelled);
                            }

                            transGroup.Assimilate();
                        }
                    }
                    catch (Exception e)
                    {
                        message = e.Message;
                        return(Result.Failed);
                    }
                    finally
                    {
                        Thread.CurrentThread.CurrentCulture = currentCulture;
                        GH_Document.EnableSolutions         = enableSolutions;
                    }
                }
            }

            return(result);
        }