public static ITFLoadableProject GetCurrentProject()
        {
            ITFApplication appInst = new TFApplicationList();

            if (0 == appInst.GetProject(0, out ITFLoadableProjectList projList) && projList != null)
            {
                ITFLoadableProject proj = projList.AsTFLoadableProject;
                return(proj);
            }
            return(null);
        }
예제 #2
0
        private Base ConvertGridLines(ISpeckleConverter converter, ProgressViewModel progress)
        {
            Base converted = null;

            ITFApplication appInst = new TFApplicationList();

            if (0 == appInst.GetProject(0, out ITFLoadableProjectList projList) && projList != null)
            {
                ITFLoadableProject proj = projList.AsTFLoadableProject;
                if (null == proj)
                {
                    progress.Report.ConversionErrors.Add(new Exception("Could not retrieve project for exporting gridlines"));
                    return(converted);
                }

                ITFDrawingGrid drawingGrid = null;
                if (Control.InvokeRequired)
                {
                    Control.Invoke((Action)(() => { proj.GetDrawingGrid(false, 0, out drawingGrid); }));
                }
                else
                {
                    proj.GetDrawingGrid(false, 0, out drawingGrid);
                }

                if (null == drawingGrid)
                {
                    progress.Report.ConversionErrors.Add(new Exception("Could not retrieve drawing grid for exporting gridlines"));
                    return(converted);
                }

                if (Control.InvokeRequired)
                {
                    converted = (Base)Control.Invoke(new SpeckleConversionDelegate(converter.ConvertToSpeckle), new object[] { drawingGrid });
                }
                else
                {
                    converted = converter.ConvertToSpeckle(drawingGrid);
                }
            }
            return(converted);
        }
예제 #3
0
        public override async Task <StreamState> SendStream(StreamState state)
        {
            ConversionErrors.Clear();
            OperationErrors.Clear();

            var kit       = KitManager.GetDefaultKit();
            var converter = kit.LoadConverter(Utils.BentleyAppName);

            if (Control.InvokeRequired)
            {
                Control.Invoke(new SetContextDelegate(converter.SetContextDocument), new object[] { Session.Instance });
            }
            else
            {
                converter.SetContextDocument(Session.Instance);
            }

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

            var selectedObjects = new List <Object>();

            if (state.Filter != null)
            {
                if (Control.InvokeRequired)
                {
                    state.SelectedObjectIds = (List <string>)Control.Invoke(new GetObjectsFromFilterDelegate(GetObjectsFromFilter), new object[] { state.Filter, converter });
                }
                else
                {
                    state.SelectedObjectIds = GetObjectsFromFilter(state.Filter, converter);
                }
            }

            if (state.SelectedObjectIds.Count == 0 && !ExportGridLines)
            {
                RaiseNotification("Zero objects selected; send stopped. Please select some objects, or check that your filter can actually select something.");
                return(state);
            }

            var commitObj = new Base();

            var units = Units.GetUnitsFromString(ModelUnits).ToLower();

            commitObj["units"] = units;

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

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

            // grab elements from active model
            var objs = new List <Element>();

#if (OPENROADS || OPENRAIL)
            bool convertCivilObject = false;
            var  civObjs            = new List <NamedModelEntity>();

            if (civilElementKeys.Count(x => state.SelectedObjectIds.Contains(x)) > 0)
            {
                if (Control.InvokeRequired)
                {
                    civObjs = (List <NamedModelEntity>)Control.Invoke(new GetCivilObjectsDelegate(GetCivilObjects), new object[] { state });
                }
                else
                {
                    civObjs = GetCivilObjects(state);
                }

                objs = civObjs.Select(x => x.Element).ToList();
                convertCivilObject = true;
            }
            else
            {
                objs = state.SelectedObjectIds.Select(x => Model.FindElementById((ElementId)Convert.ToUInt64(x))).ToList();
            }
#else
            objs = state.SelectedObjectIds.Select(x => Model.FindElementById((ElementId)Convert.ToUInt64(x))).ToList();
#endif

#if (OPENBUILDINGS)
            if (ExportGridLines)
            {
                // grab grid lines
                ITFApplication appInst = new TFApplicationList();

                if (0 == appInst.GetProject(0, out ITFLoadableProjectList projList) && projList != null)
                {
                    ITFLoadableProject proj = projList.AsTFLoadableProject;
                    if (null == proj)
                    {
                        return(null);
                    }

                    ITFDrawingGrid drawingGrid = null;
                    if (Control.InvokeRequired)
                    {
                        Control.Invoke((Action)(() => { proj.GetDrawingGrid(false, 0, out drawingGrid); }));
                    }
                    else
                    {
                        proj.GetDrawingGrid(false, 0, out drawingGrid);
                    }

                    if (null == drawingGrid)
                    {
                        return(null);
                    }

                    Base converted;
                    if (Control.InvokeRequired)
                    {
                        converted = (Base)Control.Invoke(new SpeckleConversionDelegate(converter.ConvertToSpeckle), new object[] { drawingGrid });
                    }
                    else
                    {
                        converted = converter.ConvertToSpeckle(drawingGrid);
                    }

                    if (converted != null)
                    {
                        var containerName = "Grid Systems";

                        if (commitObj[$"@{containerName}"] == null)
                        {
                            commitObj[$"@{containerName}"] = new List <Base>();
                        }
                        ((List <Base>)commitObj[$"@{containerName}"]).Add(converted);

                        // not sure this makes much sense here
                        conversionProgressDict["Conversion"]++;
                        UpdateProgress(conversionProgressDict, state.Progress);

                        //gridLine.applicationId = ??;

                        convertedCount++;
                    }
                }
            }
#endif

            foreach (var obj in objs)
            {
                if (state.CancellationTokenSource.Token.IsCancellationRequested)
                {
                    return(null);
                }

                if (obj == null)
                {
                    state.Errors.Add(new Exception($"Failed to find local object."));
                    continue;
                }

                var objId   = obj.ElementId.ToString();
                var objType = obj.ElementType;

                if (!converter.CanConvertToSpeckle(obj))
                {
                    state.Errors.Add(new Exception($"Objects of type ${objType} are not supported"));
                    continue;
                }

                // convert obj
                // try catch to prevent memory access violation crash in case a conversion goes wrong
                Base   converted     = null;
                string containerName = string.Empty;
                try
                {
                    var levelCache = Model.GetFileLevelCache();
                    var objLevel   = levelCache.GetLevel(obj.LevelId);
                    var layerName  = "Unknown";
                    if (objLevel != null)
                    {
                        layerName = objLevel.Name;
                    }

#if (OPENROADS || OPENRAIL)
                    if (convertCivilObject)
                    {
                        var civilObj = civObjs[objs.IndexOf(obj)];
                        if (Control.InvokeRequired)
                        {
                            converted = (Base)Control.Invoke(new SpeckleConversionDelegate(converter.ConvertToSpeckle), new object[] { civilObj });
                            Control.Invoke((Action)(() => { containerName = civilObj.Name == "" ? "Unnamed" : civilObj.Name; }));
                        }
                        else
                        {
                            converted     = converter.ConvertToSpeckle(civilObj);
                            containerName = civilObj.Name == "" ? "Unnamed" : civilObj.Name;
                        }
                    }
                    else
                    {
                        if (Control.InvokeRequired)
                        {
                            converted = (Base)Control.Invoke(new SpeckleConversionDelegate(converter.ConvertToSpeckle), new object[] { obj });
                        }
                        else
                        {
                            converted = converter.ConvertToSpeckle(obj);
                        }
                        containerName = layerName;
                    }
#else
                    if (Control.InvokeRequired)
                    {
                        converted = (Base)Control.Invoke(new SpeckleConversionDelegate(converter.ConvertToSpeckle), new object[] { obj });
                    }
                    else
                    {
                        converted = converter.ConvertToSpeckle(obj);
                    }

                    containerName = layerName;
#endif
                    if (converted == null)
                    {
                        state.Errors.Add(new Exception($"Failed to convert object ${objId} of type ${objType}."));
                        continue;
                    }
                }
                catch
                {
                    state.Errors.Add(new Exception($"Failed to convert object {objId} of type {objType}."));
                    continue;
                }

                /* TODO: adding the feature data and properties per object
                 * foreach (var key in obj.ExtensionDictionary)
                 * {
                 * converted[key] = obj.ExtensionDictionary.GetUserString(key);
                 * }
                 */

                if (commitObj[$"@{containerName}"] == null)
                {
                    commitObj[$"@{containerName}"] = new List <Base>();
                }
                ((List <Base>)commitObj[$"@{containerName}"]).Add(converted);

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

                converted.applicationId = objId;

                convertedCount++;
            }

            Execute.PostToUIThread(() => state.Progress.Maximum = convertedCount);

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

            var commitObjId = await Operations.Send(
                commitObj,
                state.CancellationTokenSource.Token,
                transports,
                onProgressAction : dict => UpdateProgress(dict, state.Progress),
                onErrorAction : (err, exception) => { Exceptions.Add(exception); }
                );

            if (Exceptions.Count != 0)
            {
                RaiseNotification($"Failed to send: \n {Exceptions.Last().Message}");
                return(null);
            }

            if (convertedCount > 0)
            {
                var actualCommit = new CommitCreateInput
                {
                    streamId          = streamId,
                    objectId          = commitObjId,
                    branchName        = state.Branch.name,
                    message           = state.CommitMessage != null ? state.CommitMessage : $"Pushed {convertedCount} elements from {Utils.AppName}.",
                    sourceApplication = Utils.BentleyAppName
                };

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

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

                    await state.RefreshStream();

                    state.PreviousCommitId = commitId;

                    try
                    {
                        PersistAndUpdateStreamInFile(state);
                    }
                    catch (Exception e)
                    {
                    }
                    RaiseNotification($"{convertedCount} objects sent to {state.Stream.name}.");
                }
                catch (Exception e)
                {
                    Globals.Notify($"Failed to create commit.\n{e.Message}");
                    state.Errors.Add(e);
                }
            }
            else
            {
                Globals.Notify($"Did not create commit: no objects could be converted.");
            }

            return(state);
        }