示例#1
0
 /// <summary>
 /// Clear.
 /// </summary>
 public void Clear()
 {
     StatusCode    = "";
     StatusMessage = "";
     ErrorCode     = "";
     ErrorMessage  = "";
     OperationErrors.Clear();
 }
示例#2
0
 public Operation(string name)
 {
     Name = Strings.Translate(name);
     if (CurrentOperation != null)
     {
         throw new Exception("Overlapping operations not permitted");
     }
     CurrentOperation = this;
     OperationErrors.Clear();
 }
示例#3
0
 /// <summary>
 /// Clear.
 /// </summary>
 public void Clear()
 {
     Data               = "";
     ErrorCode          = "";
     ErrorMessage       = "";
     ErrorException     = null;
     InformationCode    = "";
     InformationMessage = "";
     WarningCode        = "";
     WarningMessage     = "";
     OperationErrors.Clear();
     OperationInformations.Clear();
     OperationWarnings.Clear();
 }
        /// <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);
        }