Exemplo n.º 1
0
        // support for reporting non-fatal errors in large operations.  Not implemented yet

        /// <summary>Reports an error which may be non-fatal in some cases</summary>
        /// <param name="text">Translatable text</param>
        /// <param name="throwOutsideOperation">If true this throws an exception if not in a context where a group of non-fatal errors can be reported</param>
        public static void NonFatalOperationalError(string text, bool throwOutsideOperation = true)
        {
            text = Strings.Translate(text);
            Root.Log.WriteLine("Op error: " + text);
            if (CurrentOperation != null)
            {
                if (OperationErrors.ContainsKey(text))
                {
                    OperationErrors[text] += 1;
                }
                else
                {
                    OperationErrors.Add(text, 1);
                }
            }
            else if (throwOutsideOperation)
            {
                throw new UserException(text);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Add Operation Error.
 /// </summary>
 /// <param name="errorCode">Error code</param>
 /// <param name="errorMessage">Error message</param>
 /// <param name="members">Members</param>
 public void AddOperationError(string errorCode, string errorMessage, List <string> members)
 {
     OperationErrors.Add(new ZOperationError(errorCode, errorMessage, null, null, members));
 }
Exemplo n.º 3
0
        ///// <summary>
        ///// Add Operation Error.
        ///// </summary>
        ///// <param name="errorCode">Error code</param>
        ///// <param name="errorMessage">Error message</param>
        //public void AddOperationError(string errorCode, string errorMessage)
        //{
        //    OperationErrors.Add(new ZOperationError(errorCode, errorMessage));
        //}

        /// <summary>
        /// Add Operation Error.
        /// </summary>
        /// <param name="errorCode">Error code</param>
        /// <param name="errorMessage">Error message</param>
        /// <param name="errorStackTrace">Error stack trace</param>
        /// <param name="errorException">Exception</param>
        /// <param name="members">Members</param>
        public void AddOperationError(string errorCode, string errorMessage, string errorStackTrace = null, Exception errorException = null, List <string> members = null)
        {
            OperationErrors.Add(new ZOperationError(errorCode, errorMessage, errorStackTrace, errorException, members));
        }
        /// <summary>
        /// Receives a stream and bakes into the existing revit file.
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public override async Task <StreamState> ReceiveStream(StreamState state)
        {
            ConversionErrors.Clear();
            OperationErrors.Clear();

            var kit       = KitManager.GetDefaultKit();
            var converter = kit.LoadConverter(ConnectorRevitUtils.RevitAppName);

            converter.SetContextDocument(CurrentDoc.Document);
            var previouslyReceiveObjects = state.ReceivedObjects;

            var transport = new ServerTransport(state.Client.Account, state.Stream.id);

            string referencedObject = state.Commit.referencedObject;

            if (state.CancellationTokenSource.Token.IsCancellationRequested)
            {
                return(null);
            }

            //if "latest", always make sure we get the latest commit when the user clicks "receive"
            if (state.Commit.id == "latest")
            {
                var res = await state.Client.BranchGet(state.CancellationTokenSource.Token, state.Stream.id, state.Branch.name, 1);

                referencedObject = res.commits.items.FirstOrDefault().referencedObject;
            }

            var commit = state.Commit;



            var commitObject = await Operations.Receive(
                referencedObject,
                state.CancellationTokenSource.Token,
                transport,
                onProgressAction : dict => UpdateProgress(dict, state.Progress),
                onErrorAction : (s, e) =>
            {
                OperationErrors.Add(e);
                state.Errors.Add(e);
                state.CancellationTokenSource.Cancel();
            },
                onTotalChildrenCountKnown : count => Execute.PostToUIThread(() => state.Progress.Maximum = count)
                );

            if (OperationErrors.Count != 0)
            {
                Globals.Notify("Failed to get commit.");
                return(state);
            }

            if (state.CancellationTokenSource.Token.IsCancellationRequested)
            {
                return(null);
            }



            // Bake the new ones.
            Queue.Add(() =>
            {
                using (var t = new Transaction(CurrentDoc.Document, $"Baking stream {state.Stream.name}"))
                {
                    var failOpts = t.GetFailureHandlingOptions();
                    failOpts.SetFailuresPreprocessor(new ErrorEater(converter));
                    failOpts.SetClearAfterRollback(true);
                    t.SetFailureHandlingOptions(failOpts);

                    t.Start();
                    var flattenedObjects = FlattenCommitObject(commitObject, converter);
                    // needs to be set for editing to work
                    converter.SetPreviousContextObjects(previouslyReceiveObjects);
                    // needs to be set for openings in floors and roofs to work
                    converter.SetContextObjects(flattenedObjects.Select(x => new ApplicationPlaceholderObject {
                        applicationId = x.applicationId, NativeObject = x
                    }).ToList());
                    var newPlaceholderObjects = ConvertReceivedObjects(flattenedObjects, converter, state);
                    // receive was cancelled by user
                    if (newPlaceholderObjects == null)
                    {
                        converter.ConversionErrors.Add(new Exception("fatal error: receive cancelled by user"));
                        t.RollBack();
                        return;
                    }

                    DeleteObjects(previouslyReceiveObjects, newPlaceholderObjects);

                    state.ReceivedObjects = newPlaceholderObjects;

                    t.Commit();

                    state.Errors.AddRange(converter.ConversionErrors);
                }
            });

            Executor.Raise();

            while (Queue.Count > 0)
            {
                //wait to let queue finish
            }

            if (converter.ConversionErrors.Any(x => x.Message.Contains("fatal error")))
            {
                // the commit is being rolled back
                return(null);
            }

            try
            {
                await state.RefreshStream();

                WriteStateToFile();
            }
            catch (Exception e)
            {
                WriteStateToFile();
                state.Errors.Add(e);
                Globals.Notify($"Receiving done, but failed to update stream from server.\n{e.Message}");
            }

            return(state);
        }
        /// <summary>
        /// Converts the Revit elements that have been added to the stream by the user, sends them to
        /// the Server and the local DB, and creates a commit with the objects.
        /// </summary>
        /// <param name="state">StreamState passed by the UI</param>
        public override async Task <StreamState> SendStream(StreamState state)
        {
            ConversionErrors.Clear();
            OperationErrors.Clear();

            var kit       = KitManager.GetDefaultKit();
            var converter = kit.LoadConverter(ConnectorRevitUtils.RevitAppName);

            converter.SetContextDocument(CurrentDoc.Document);

            var streamId = state.Stream.id;
            var client   = state.Client;

            var selectedObjects = new List <Element>();

            if (state.Filter != null)
            {
                selectedObjects         = GetSelectionFilterObjects(state.Filter, converter);
                state.SelectedObjectIds = selectedObjects.Select(x => x.UniqueId).ToList();
            }
            else //selection was by cursor
            {
                // TODO: update state by removing any deleted or null object ids
                selectedObjects = state.SelectedObjectIds.Select(x => CurrentDoc.Document.GetElement(x)).Where(x => x != null).ToList();
            }


            if (!selectedObjects.Any())
            {
                state.Errors.Add(new Exception("There are zero objects to send. Please use a filter, or set some via selection."));
                return(state);
            }

            converter.SetContextObjects(selectedObjects.Select(x => new ApplicationPlaceholderObject {
                applicationId = x.UniqueId
            }).ToList());

            var commitObject = new Base();

            var conversionProgressDict = new ConcurrentDictionary <string, int>();

            conversionProgressDict["Conversion"] = 0;
            Execute.PostToUIThread(() => state.Progress.Maximum = selectedObjects.Count());
            var convertedCount = 0;

            var placeholders = new List <Base>();

            foreach (var revitElement in selectedObjects)
            {
                try
                {
                    if (revitElement == null)
                    {
                        continue;
                    }

                    if (!converter.CanConvertToSpeckle(revitElement))
                    {
                        state.Errors.Add(new Exception($"Skipping not supported type: {revitElement.GetType()}, name {revitElement.Name}"));
                        continue;
                    }

                    var conversionResult = converter.ConvertToSpeckle(revitElement);

                    conversionProgressDict["Conversion"]++;
                    UpdateProgress(conversionProgressDict, state.Progress);

                    placeholders.Add(new ApplicationPlaceholderObject {
                        applicationId = revitElement.UniqueId, ApplicationGeneratedId = revitElement.UniqueId
                    });

                    convertedCount++;

                    //hosted elements will be returned as `null` by the ConvertToSpeckle method
                    //since they are handled when converting their parents
                    if (conversionResult != null)
                    {
                        var category = $"@{revitElement.Category.Name}";
                        if (commitObject[category] == null)
                        {
                            commitObject[category] = new List <Base>();
                        }
                        ((List <Base>)commitObject[category]).Add(conversionResult);
                    }
                }
                catch (Exception e)
                {
                    state.Errors.Add(e);
                }
            }

            if (converter.Report.ConversionErrorsCount != 0)
            {
                // TODO: Get rid of the custom Error class. It's not needed.
                ConversionErrors.AddRange(converter.Report.ConversionErrors);
                state.Errors.AddRange(converter.Report.ConversionErrors);
            }

            if (convertedCount == 0)
            {
                Globals.Notify("Zero objects converted successfully. Send stopped.");
                return(state);
            }

            Execute.PostToUIThread(() => state.Progress.Maximum = (int)commitObject.GetTotalChildrenCount());

            if (state.CancellationTokenSource.Token.IsCancellationRequested)
            {
                return(state);
            }

            var transports = new List <ITransport>()
            {
                new ServerTransport(client.Account, streamId)
            };

            var objectId = await Operations.Send(
                @object : commitObject,
                cancellationToken : state.CancellationTokenSource.Token,
                transports : transports,
                onProgressAction : dict => UpdateProgress(dict, state.Progress),
                onErrorAction : (s, e) =>
            {
                OperationErrors.Add(e); // TODO!
                state.Errors.Add(e);
                state.CancellationTokenSource.Cancel();
            },
                disposeTransports : true
                );

            if (OperationErrors.Count != 0)
            {
                Globals.Notify("Failed to send.");
                state.Errors.AddRange(OperationErrors);
                return(state);
            }

            if (state.CancellationTokenSource.Token.IsCancellationRequested)
            {
                return(null);
            }

            var actualCommit = new CommitCreateInput()
            {
                streamId          = streamId,
                objectId          = objectId,
                branchName        = state.Branch.name,
                message           = state.CommitMessage != null ? state.CommitMessage : $"Sent {convertedCount} objects from {ConnectorRevitUtils.RevitAppName}.",
                sourceApplication = ConnectorRevitUtils.RevitAppName,
            };

            if (state.PreviousCommitId != null)
            {
                actualCommit.parents = new List <string>()
                {
                    state.PreviousCommitId
                };
            }

            try
            {
                var commitId = await client.CommitCreate(actualCommit);

                await state.RefreshStream();

                state.PreviousCommitId = commitId;

                WriteStateToFile();
                RaiseNotification($"{convertedCount} objects sent to Speckle 🚀");
            }
            catch (Exception e)
            {
                state.Errors.Add(e);
                Globals.Notify($"Failed to create commit.\n{e.Message}");
            }

            return(state);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Add Operation Error.
 /// </summary>
 /// <param name="errorCode">Error code</param>
 /// <param name="errorMessage">Error message</param>
 /// <param name="members">Members</param>
 public void AddOperationError(string errorCode, string errorMessage, IEnumerable <string> members)
 {
     OperationErrors.Add(new ZOperationError(errorCode, errorMessage, members));
 }
Exemplo n.º 7
0
 /// <summary>
 /// Add Operation Error.
 /// </summary>
 /// <param name="errorCode">Error code</param>
 /// <param name="errorMessage">Error message</param>
 /// <param name="member">Member</param>
 public void AddOperationError(string errorCode, string errorMessage, string member)
 {
     OperationErrors.Add(new ZOperationError(errorCode, errorMessage, new List <string> {
         member
     }));
 }
Exemplo n.º 8
0
 /// <summary>
 /// Add Operation Error.
 /// </summary>
 /// <param name="errorCode">Error code</param>
 /// <param name="errorMessage">Error message</param>
 public void AddOperationError(string errorCode, string errorMessage)
 {
     OperationErrors.Add(new ZOperationError(errorCode, errorMessage));
 }
Exemplo n.º 9
0
 /// <summary>
 /// Add Operation Error.
 /// </summary>
 /// <param name="errorCode">Error code</param>
 /// <param name="errorMessage">Error message</param>
 /// <param name="errorStackTrace">Error stack trace</param>
 public void AddOperationError(string errorCode, string errorMessage, string errorStackTrace = null)
 {
     OperationErrors.Add(new ZOperationError(errorCode, errorMessage, errorStackTrace));
 }