Example #1
0
        public async void Execute(object parameter)
        {
            //Run Revit API directly here
            var savePath = await RevitTask.RunAsync(async app =>
            {
                try
                {
                    //Support async task
                    //Raise global external event handler
                    Family randomFamily = await RevitTask
                                          .RaiseGlobal <GetRandomFamilyExternalEventHandler, bool, Family>(parameter as bool? ?? false);

                    //Raise scoped external event handler
                    return(await ScopedRevitTask.Raise <SaveFamilyToDesktopExternalEventHandler, Family, string>(randomFamily));
                }
                catch (Exception)
                {
                    return(null);
                }
            });

            var saveResult = !string.IsNullOrWhiteSpace(savePath);

            MessageBox.Show($"Family {(saveResult ? "" : "not ")}saved:\n{savePath}");
            if (saveResult && Path.GetDirectoryName(savePath) is string dir)
            {
                Process.Start(dir);
            }
        }
Example #2
0
        public void Execute(object parameter)
        {
            for (var i = 0; i < 5; i++)
            {
                RevitTask.RunAsync(async app =>
                {
                    //Revit API can be executed directly here

                    try
                    {
                        //Support async task
                        //Raise global external event handler
                        var randomFamily = await RevitTask.RaiseGlobalNew <GetRandomFamilyExternalEventHandler, bool, Family>(parameter as bool? ?? false);

                        //Raise scoped external event handler
                        return(await ScopedRevitTask.RaiseNew <SaveFamilyToDesktopExternalEventHandler, Family, string>(randomFamily));
                    }
                    catch (Exception)
                    {
                        return(null);
                    }
                }).ContinueWith(savePathTask =>
                {
                    var savePath   = savePathTask.Result;
                    var saveResult = !string.IsNullOrWhiteSpace(savePath);
                    if (saveResult && Path.GetDirectoryName(savePath) is string dir)
                    {
                        Process.Start(dir);
                    }
                });
            }
        }
Example #3
0
 public override async void WriteStreamsToFile(List <StreamState> streams)
 {
     await RevitTask.RunAsync(
         app =>
     {
         using (Transaction t = new Transaction(CurrentDoc.Document, "Speckle Write State"))
         {
             t.Start();
             StreamStateManager2.WriteStreamStateList(CurrentDoc.Document, streams);
             t.Commit();
         }
     });
 }
Example #4
0
        /// <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, ProgressViewModel progress)
        {
            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.StreamId);

            var stream = await state.Client.StreamGet(state.StreamId);

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

            Commit myCommit = null;

            //if "latest", always make sure we get the latest commit when the user clicks "receive"
            if (state.CommitId == "latest")
            {
                var res = await state.Client.BranchGet(progress.CancellationTokenSource.Token, state.StreamId, state.BranchName, 1);

                myCommit = res.commits.items.FirstOrDefault();
            }
            else
            {
                myCommit = await state.Client.CommitGet(progress.CancellationTokenSource.Token, state.StreamId, state.CommitId);
            }
            string referencedObject = myCommit.referencedObject;

            var commitObject = await Operations.Receive(
                referencedObject,
                progress.CancellationTokenSource.Token,
                transport,
                onProgressAction : dict => progress.Update(dict),
                onErrorAction : (s, e) =>
            {
                progress.Report.LogOperationError(e);
                progress.CancellationTokenSource.Cancel();
            },
                onTotalChildrenCountKnown : count => { progress.Max = count; },
                disposeTransports : true
                );

            try
            {
                await state.Client.CommitReceived(new CommitReceivedInput
                {
                    streamId          = stream?.id,
                    commitId          = myCommit?.id,
                    message           = myCommit?.message,
                    sourceApplication = ConnectorRevitUtils.RevitAppName
                });
            }
            catch
            {
                // Do nothing!
            }

            if (progress.Report.OperationErrorsCount != 0)
            {
                return(state);
            }

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



            await RevitTask.RunAsync(app =>
            {
                using (var t = new Transaction(CurrentDoc.Document, $"Baking stream {state.StreamId}"))
                {
                    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, progress);
                    // receive was cancelled by user
                    if (newPlaceholderObjects == null)
                    {
                        progress.Report.LogConversionError(new Exception("fatal error: receive cancelled by user"));
                        t.RollBack();
                        return;
                    }

                    DeleteObjects(previouslyReceiveObjects, newPlaceholderObjects);

                    state.ReceivedObjects = newPlaceholderObjects;

                    t.Commit();
                    progress.Report.Merge(converter.Report);
                }
            });



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


            return(state);
        }