Example #1
0
        /// <summary>
        /// Run the toggle switches process, it can be cancel
        /// </summary>
        /// <param name="cps">Cancelable Progressor Source to show the progression</param>
        /// <param name="mapExtent">Map Extent to zomm to the precedent extent</param>
        /// <returns>An error comment if needed, empty of no error</returns>
        internal static async Task <string> RunCancelableToggleSwwitches(CancelableProgressorSource cps, Envelope mapExtent)
        {
            string status = "";
            await QueuedTask.Run(() =>
            {
                cps.Progressor.Max = 6;

                cps.Progressor.Value  += 1;
                cps.Progressor.Status  = (cps.Progressor.Value * 100 / cps.Progressor.Max) + @" % Completed";
                cps.Progressor.Message = "Step 1 – Toggle switch status";

                // change selected switches attributes
                status = ToggleSwitchesExecute();
            }, cps.Progressor);

            await QueuedTask.Run(() =>
            {
                if (String.IsNullOrEmpty(status) && !cps.Progressor.CancellationToken.IsCancellationRequested)
                {
                    cps.Progressor.Value  += 1;
                    cps.Progressor.Status  = (cps.Progressor.Value * 100 / cps.Progressor.Max) + @" % Completed";
                    cps.Progressor.Message = "Step 2 –  Validate and save edits";

                    // ValidateNetworkTopology need an Edit Operation if there are some editions not saved
                    Project.Current.SaveEditsAsync();
                    GlobalUtilityNetwork.ValidateNetworkTopology();
                    // ValidateNetworkTopology create an edit operation which must be saved
                    Project.Current.SaveEditsAsync();

                    cps.Progressor.Value  += 1;
                    cps.Progressor.Status  = (cps.Progressor.Value * 100 / cps.Progressor.Max) + @" % Completed";
                    cps.Progressor.Message = "Step 3 – Update the dirty subnetwork(s)";

                    UtilityNetworkDefinition unDef = GlobalUtilityNetwork.GetDefinition();
                    Tier tier = unDef.GetDomainNetwork(cDomainNetworkName).GetTier(cTierName);

                    SubnetworkManager subManager = GlobalUtilityNetwork.GetSubnetworkManager();

                    // Update subnetwork
                    try
                    { subManager.UpdateAllSubnetworks(tier, true); }
                    catch (Exception ex)
                    { status = ExceptionFormat(ex); }
                }
            }, cps.Progressor);

            await QueuedTask.Run(() =>
            {
                if (String.IsNullOrEmpty(status) && !cps.Progressor.CancellationToken.IsCancellationRequested)
                {
                    cps.Progressor.Value  += 1;
                    cps.Progressor.Status  = (cps.Progressor.Value * 100 / cps.Progressor.Max) + @" % Completed";
                    cps.Progressor.Message = "Step 4 – Color the diagram edges per subnetwork";
                    ColorEdges.ExecuteReductionEdgeColorBySubnetwork(GetDiagramLayerFromMap(MapView.Active.Map));

                    cps.Progressor.Value  += 1;
                    cps.Progressor.Status  = (cps.Progressor.Value * 100 / cps.Progressor.Max) + @" % Completed";
                    cps.Progressor.Message = "Step 5 – Redraw the Network Diagram";

                    MapView.Active.Redraw(true);
                    MapView.Active.ZoomTo(mapExtent);

                    // re set the selection
                    if (GlobalMapSelection != null)
                    {
                        MapView.Active.Map.ClearSelection();
                        MapView.Active.Map.SetSelection(GlobalMapSelection);
                    }
                }
            }, cps.Progressor);

            await QueuedTask.Run(() =>
            {
                CleanModule();
            });

            return(status);
        }
Example #2
0
        /// <summary>
        /// Set Global fields
        /// </summary>
        /// <param name="map">Diagram Layer</param>
        internal static void GetParameters(Map map)
        {
            if (map == null)
            {
                CleanModule();
                return;
            }

            if (GlobalActiveMapName != map.Name || GlobalDiagram == null || GlobalDiagramLayer == null)
            {
                GlobalActiveMap     = map;
                GlobalActiveMapName = GlobalActiveMap.Name;
                GlobalDiagramLayer  = GetDiagramLayerFromMap(map);
                if (GlobalDiagramLayer == null)
                {
                    CleanModule();
                }
                else
                {
                    QueuedTask.Run(() =>
                    {
                        try
                        {
                            GlobalDiagram = GlobalDiagramLayer.GetNetworkDiagram();
                        }
                        catch
                        {
                            GlobalDiagram = null;
                        }

                        try
                        {
                            if (GlobalDiagram != null)
                            {
                                GlobalDiagramManager = GlobalDiagram.DiagramManager;

                                GlobalUtilityNetwork = GlobalDiagramManager.GetNetwork <UtilityNetwork>();

                                GlobalGeodatabase = GlobalUtilityNetwork.GetDatastore() as Geodatabase;

                                NetworkDiagramInfo networkDiagramInfo = GlobalDiagram.GetDiagramInfo();
                                if (networkDiagramInfo == null)
                                {
                                    GlobalIsSystem        = false;
                                    GlobalEnvelope        = null;
                                    GlobalIsStored        = false;
                                    GlobalContainerMargin = 0.5;
                                }
                                else
                                {
                                    GlobalIsSystem        = networkDiagramInfo.IsSystem;
                                    GlobalEnvelope        = networkDiagramInfo.DiagramExtent;
                                    GlobalIsStored        = networkDiagramInfo.IsStored;
                                    GlobalContainerMargin = networkDiagramInfo.ContainerMargin;
                                }

                                GlobalTypeGeo = GlobalGeodatabase.GetGeodatabaseType();

                                if (GlobalTypeGeo == GeodatabaseType.Service)
                                {
                                    VersionManager vm = GlobalGeodatabase.GetVersionManager();
                                    if (vm != null)
                                    {
                                        GlobalIsVersioned = (vm.GetCurrentVersion() != null);
                                    }
                                    else
                                    {
                                        GlobalIsVersioned = false;
                                    }
                                }
                                else
                                {
                                    GlobalIsVersioned = false;
                                }
                            }
                            else
                            {
                                GlobalDiagramLayer = null;
                            }

                            CleanSelections();
                        }
                        catch (Exception ex)
                        {
                            ShowException(exception: ex);

                            CleanModule();
                        }
                    });
                }
            }
        }
Example #3
0
        /// <summary>
        /// Internal execution fonction to toggle switches
        /// </summary>
        /// <returns>An error comment if needed, empty of no error</returns>
        private static string ToggleSwitchesExecute()
        {
            bool canUpdate = false;

            // Load all Diagram Element and selection
            if (!IsDiagramUsable(GetAllElements: false, GetSelection: true))
            {
                return("");
            }

            if (GlobalSelectedJunctionIDs.Count == 0)
            {
                return("There are no junction selected");
            }

            Dictionary <long, List <Guid> > guidBySourceId = new Dictionary <long, List <Guid> >();

            // retrieve the junctions GlobalId
            foreach (long oid in GlobalSelectedJunctionIDs)
            {
                DiagramJunctionElement junctionElement = GlobalDiagramJunctionElements.FirstOrDefault(a => a.ObjectID == oid);
                if (junctionElement != null)
                {
                    if (!guidBySourceId.TryGetValue(junctionElement.AssociatedSourceID, out List <Guid> guidList))
                    {
                        guidList = new List <Guid>();
                        guidBySourceId.Add(junctionElement.AssociatedSourceID, guidList);
                    }

                    if (!guidList.Contains(junctionElement.AssociatedGlobalID))
                    {
                        guidList.Add(junctionElement.AssociatedGlobalID);
                    }
                }
            }

            IReadOnlyList <NetworkSource> theSources = GlobalUtilityNetwork.GetDefinition().GetNetworkSources();

            List <string> searchFields = new List <string> {
                cDeviceStatusFieldName, "ObjectId", "AssetGroup"
            };

            foreach (NetworkSource source in theSources)
            {
                if (guidBySourceId.TryGetValue(source.ID, out List <Guid> guidList))
                {
                    // Get a cursor of guid list, get the qualified fields name
                    using (RowCursor sel = GetRowCursorFromSourceNameAndGuidList(SourceName: source.Name.Replace(" ", ""), SearchGuid: guidList, ListSearchFields: searchFields, WhereField: "GlobalId", FieldsName: out List <Tuple <string, string> > FieldsName))
                    {
                        int deviceStatusIndex = -1;
                        int assetGroupIndex   = -1;
                        // retrieved the fields indexes
                        foreach (Tuple <string, string> findTuple in FieldsName)
                        {
                            if (findTuple.Item1 == cDeviceStatusFieldName)
                            {
                                deviceStatusIndex = sel.FindField(findTuple.Item2);
                            }
                            else if (findTuple.Item1 == "AssetGroup")
                            {
                                assetGroupIndex = sel.FindField(findTuple.Item2);
                            }
                        }

                        if (deviceStatusIndex >= 0)                         // run only if there is a device status
                        {
                            var modifyStringsOperation = new EditOperation
                            {
                                Name = String.Format("Modify string field '{0}' in source {1}.", cDeviceStatusFieldName, source.Name)
                            };

                            ICollection <long> oidSet = new List <long>();
                            while (sel.MoveNext())
                            {
                                string AssetGroupValue = sel.Current[assetGroupIndex].ToString();
                                // verify if the Asset Group is correct
                                if (!String.IsNullOrEmpty(AssetGroupValue) && AssetGroupValue == cAssetGroupFieldValue)
                                {
                                    string deviceStatus  = sel.Current[deviceStatusIndex]?.ToString();
                                    Guid   globalIdValue = sel.Current.GetGlobalID();
                                    long   oid           = sel.Current.GetObjectID();

                                    // set up a new dictionary with fields to modify
                                    var modifiedAttributes = new Dictionary <string, object>
                                    {
                                        // add the name of the string field and the new attribute value to the dictionary
                                        { cDeviceStatusFieldName, deviceStatus == "2" ? 1 : 2 }
                                    };

                                    // put the modify operation on the editor stack
                                    modifyStringsOperation.Modify(sel.Current, modifiedAttributes);
                                    oidSet.Add(oid);
                                }
                            }

                            if (oidSet.Count > 0)
                            {                              // execute the modify operation to apply the changes
                                modifyStringsOperation.Execute();
                                canUpdate = true;
                            }
                            else
                            {
                                modifyStringsOperation.Abort();
                            }
                        }
                    }
                }
            }

            return(canUpdate ? "" : "This command only applies to medium voltage switches. Please make sure there is at least one selected medium voltage switch in the active diagram before its execution.");
        }