/// <summary> /// Add in our target types /// </summary> /// <param name="ElemType"></param> /// <param name="TargetType"></param> private void AddType(MM_Element_Type ElemType, Type TargetType) { ToolStripMenuItem OpMenu = (ToolStripMenuItem)byOperatorToolStripMenuItem.DropDownItems.Add(ElemType.Name); ToolStripMenuItem CtyMenu = (ToolStripMenuItem)byCountyToolStripMenuItem.DropDownItems.Add(ElemType.Name); OpMenu.DropDownItemClicked += btnTrendHistory_DropDownItemClicked; CtyMenu.DropDownItemClicked += btnTrendHistory_DropDownItemClicked; OpMenu.Tag = ElemType; CtyMenu.Tag = ElemType; SortedDictionary <String, MemberInfo> Members = new SortedDictionary <string, MemberInfo>(); foreach (MemberInfo mI in TargetType.GetMembers(BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.Public)) { if ((mI is FieldInfo && ((FieldInfo)mI).FieldType == typeof(float)) || (mI is PropertyInfo && ((PropertyInfo)mI).PropertyType == typeof(float))) { Members.Add(mI.Name, mI); } } foreach (KeyValuePair <String, MemberInfo> kvp in Members) { OpMenu.DropDownItems.Add(kvp.Key).Tag = kvp.Value; CtyMenu.DropDownItems.Add(kvp.Key).Tag = kvp.Value; } }
/// <summary> /// Locate or create an element type /// </summary> /// <param name="TypeName"></param> /// <returns></returns> public static MM_Element_Type FindElementType(String TypeName) { MM_Element_Type OutType; if (!MM_Repository.ElemTypes.TryGetValue(TypeName, out OutType)) { MM_Repository.ElemTypes.Add(TypeName, OutType = new MM_Element_Type(TypeName, TypeName.Substring(0, 3), "")); } return(OutType); }
/// <summary> /// Generate a standalone tracking form /// </summary> /// <param name="Title"></param> /// <param name="ElemType"></param> /// <param name="MemberToCheck"></param> /// <param name="Elements"></param> public static MM_Form GenerateTrackingForm(String Title, MM_Element_Type ElemType, MemberInfo MemberToCheck, MM_Element[] Elements) { MM_Form NewForm = new MM_Form(); MM_LoadGen_Tracking_Operator Tracking = new MM_LoadGen_Tracking_Operator(Title, ElemType, MemberToCheck); NewForm.Icon = Properties.Resources.CompanyIcon; Tracking.Elements.AddRange(Elements); Tracking.Dock = DockStyle.Fill; NewForm.Controls.Add(Tracking); return(NewForm); }
/// <summary> /// Handle our drop-down item /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnTrendHistory_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) { if (e.ClickedItem.Tag == null || e.ClickedItem.Tag is MemberInfo == false) { return; } MM_Element_Type ElemType = (MM_Element_Type)(((ToolStripItem)sender).Tag); MemberInfo mI = (MemberInfo)e.ClickedItem.Tag; btnTrendHistory.Text = ElemType.Name + " " + mI.Name + " " + e.ClickedItem.OwnerItem.OwnerItem.Text; flwMain.Controls.Clear(); MM_LoadGen_Tracking_Operator FoundOperator; SortedDictionary <String, MM_LoadGen_Tracking_Operator> Trackers = new SortedDictionary <string, MM_LoadGen_Tracking_Operator>(); foreach (MM_Element Elem in MM_Repository.TEIDs.Values) { if (Elem.ElemType == ElemType) { //Determine our key String Key = ""; if (e.ClickedItem.OwnerItem.OwnerItem == byOperatorToolStripMenuItem) { Key = Elem.Operator.Alias; } else if (e.ClickedItem.OwnerItem.OwnerItem == byCountyToolStripMenuItem) { if (Elem.Substation == null || Elem.Substation.County == null) { Key = "Unknown"; } else { Key = Elem.Substation.County.Name; } } if (!Trackers.TryGetValue(Key, out FoundOperator)) { Trackers.Add(Key, FoundOperator = new MM_LoadGen_Tracking_Operator(Key, ElemType, mI)); } FoundOperator.Elements.Add(Elem); } } foreach (MM_LoadGen_Tracking_Operator Oper in Trackers.Values) { Oper.BeginTracking(); } flwMain.Controls.AddRange(Trackers.Values.ToArray()); }
/// <summary> /// Check to see whether an element meets the inclusion criteria /// </summary> /// <param name="Elem">The element to be checked </param> /// <param name="KVLevel">The KV Level to be used</param> /// <param name="ElemType">The Element type to be used</param> /// <param name="Words">The words to search for</param> /// <param name="ElementWords">The collection of words for an element, for testing</param> /// <returns>True if the element passes all criteria</returns> /// private bool CheckElement(MM_Element Elem, MM_KVLevel KVLevel, MM_Element_Type ElemType, List <String> Words, Dictionary <String, bool> ElementWords) { if ((KVLevel != null) && (Elem.KVLevel != KVLevel)) { return(false); } else if (ElemType != null) { if (ElemType.Name == "SeriesCompensator" && Elem is MM_Line && (Elem as MM_Line).IsSeriesCompensator) { } else if (Elem.ElemType != ElemType) { return(false); } } //Now build up our collection of words for this element BuildWords(Elem, ElementWords); //Now if we have any words, let's test them against this element. If all pass, then we're ok. //If a substation, check both name and long name foreach (String Word in Words) { if (Word.IndexOfAny(new char[] { '?', '*' }) == -1) { if (!ElementWords.ContainsKey(Word)) { return(false); } } else { Regex TestReg = new Regex("^" + Regex.Escape(Word).Replace("\\*", ".*").Replace("\\?", ".") + "$", RegexOptions.IgnoreCase); bool FoundMatch = false; foreach (String str in ElementWords.Keys) { if (!FoundMatch && TestReg.IsMatch(str)) { FoundMatch = true; } } if (!FoundMatch) { return(false); } } } return(true); }
/// <summary> /// Initialize our tracking form /// </summary> /// <param name="Title"></param> /// <param name="ElemType"></param> /// <param name="MemberToCheck"></param> public void InitializeTracker(String Title, MM_Element_Type ElemType, MemberInfo MemberToCheck) { StringBuilder TitleIter = new StringBuilder(); for (int a = 0; a < Title.Length; a++) { if (a > 0 && char.IsUpper(Title[a]) && Char.IsLower(Title[a - 1])) { TitleIter.Append(' '); } TitleIter.Append(Title[a]); } this.Title = TitleIter.ToString().Replace('_', ' '); this.ElemType = ElemType; lblOperator.Text = this.Title; if (MemberToCheck is FieldInfo) { this.FieldToCheck = (FieldInfo)MemberToCheck; } else { this.PropertyToCheck = (PropertyInfo)MemberToCheck; } if (MemberToCheck.Name.Contains("Frequency") || MemberToCheck.Name.Contains("PerUnit")) { nudMax.DecimalPlaces = nudMin.DecimalPlaces = 2; NumberFormat = "#,##0.00"; } else if (MemberToCheck.Name.Contains("Voltage")) { nudMax.DecimalPlaces = nudMin.DecimalPlaces = 1; NumberFormat = "#,##0.0"; } else { nudMax.DecimalPlaces = nudMin.DecimalPlaces = 0; NumberFormat = "#,##0"; } }
/// <param name="Title"></param> /// <param name="ElemType"></param> /// <param name="MemberToCheck"></param> public MM_LoadGen_Tracking_Operator(String Title, MM_Element_Type ElemType, MemberInfo MemberToCheck) { InitializeComponent(); InitializeTracker(Title, ElemType, MemberToCheck); }
/// <summary> /// Locate an element, based on its substation name, element name, element type and KV level. /// </summary> /// <param name="SubstationName">The name of the substation</param> /// <param name="ElementName">The name of the element</param> /// <param name="ElementType">The type of the element</param> /// <param name="ElementKVLevel">The KV level of the element</param> /// <returns>The appropriate element</returns> public static MM_Element LocateElement(String SubstationName, String ElementName, String ElementType, String ElementKVLevel) { //First, if we have a line, let's try and find that line if ((ElementType == "LN" || ElementType == "Line") && (Lines.ContainsKey(ElementName))) { return(Lines[ElementName]); } //First, find the substation in question. MM_Substation BaseStation = Substations[SubstationName]; //Now, based on the element type, see if we already have the element MM_Element_Type OutElemType = null; if (ElementType == "DSC") { ElementType = "SW"; } else if (ElementType == "SHUNT") { ElementType = "CP"; } else if (ElementType == "TRANSF") { ElementType = "XF"; } foreach (MM_Element_Type ElemType in ElemTypes.Values) { if (String.Equals(ElemType.Name, ElementType, StringComparison.CurrentCultureIgnoreCase) || String.Equals(ElemType.Acronym, ElementType, StringComparison.CurrentCultureIgnoreCase)) { OutElemType = ElemType; break; } } //Now, if we have a unit, load or transformer, run a quick check to see if it's already there if (OutElemType.Name == "Unit") { foreach (MM_Unit Unit in BaseStation.Units) { if (String.Equals(Unit.Name, ElementName, StringComparison.CurrentCultureIgnoreCase)) { return(Unit); } else if (OutElemType.Name == "Load") { foreach (MM_Load Load in BaseStation.Loads) { if (String.Equals(Load.Name, ElementName, StringComparison.CurrentCultureIgnoreCase)) { return(Load); } else if (OutElemType.Name == "Transformer") { foreach (MM_Transformer Transformer in BaseStation.Transformers) { if (String.Equals(Transformer.Name, ElementName, StringComparison.CurrentCultureIgnoreCase)) { return(Transformer); } } } } } } } //If not, send out our request to the CIM server MM_Element FoundElement = null; //TODO: Handle loading of a new element /*if (MM_Server_Interface.Client != null) * FoundElement = MM_Server_Interface.Client.LoadElement(SubstationName, ElementName, OutElemType); */ //If we can't find it, let's create a new one. if (FoundElement == null) { FoundElement = new MM_Element(); FoundElement.ElemType = OutElemType; FoundElement.Name = ElementName; FoundElement.Substation = BaseStation; FoundElement.KVLevel = GetVoltageLevel(ElementKVLevel); } return(FoundElement); }
/// <summary> /// Perform the actual searching /// </summary> /// <param name="ControlPressed">Whether to return just substations with one item</param> /// <param name="AltPressed">Whether alt is pressed</param> /// <returns>True if a succesful match has been found</returns> public bool PerformSearch(bool ControlPressed, bool AltPressed) { picAbort.Visible = true; BaseData.Data.Tables.Clear(); this.lvSearch.Items.Clear(); this.Cursor = Cursors.WaitCursor; //First, see if the string is a TEID, and if so, just popup the property page. Int32 TEIDTest = 0; MM_Element FoundElem; if (ItemSelectionChanged == null && Int32.TryParse(SearchText.Trim(), out TEIDTest)) { if (MM_Repository.TEIDs.TryGetValue(TEIDTest, out FoundElem)) { if (ControlPressed) { if (FoundElem is MM_Substation) { nMap.Coordinates.Center = ((FoundElem as MM_Substation).LngLat); } else if (FoundElem is MM_Line) { nMap.Coordinates.Center = ((FoundElem as MM_Line).Midpoint); } else if (FoundElem.Substation != null) { nMap.Coordinates.Center = (FoundElem.Substation.LngLat); } else { MM_Form_Builder.PropertyPage(MM_Repository.TEIDs[TEIDTest], nMap); return(false); } nMap.Coordinates.UpdateZoom(MM_Repository.OverallDisplay.StationZoomLevel); } else { MM_Form_Builder.PropertyPage(MM_Repository.TEIDs[TEIDTest], nMap); } return(false); } else { return(FailError("Unable to find TEID #" + TEIDTest.ToString() + "!")); } } //Split apart the string List <String> Words = new List <string>(); //Define the parameters we might be looking for. MM_KVLevel KVLevel = null; MM_Element_Type ElemType = null; if (ItemSelectionChanged != null && int.TryParse(SearchText, out TEIDTest) && MM_Repository.TEIDs.TryGetValue(TEIDTest, out FoundElem)) { AddElement(FoundElem); } //Go through each word, and test it. foreach (String Word in SearchText.Split(new char[] { ' ', '\t', '_' }, StringSplitOptions.RemoveEmptyEntries)) { Object FoundWord = FindWord(Word); if (FoundWord == null) { Words.Add(Word); } else if (FoundWord is MM_KVLevel) { if (KVLevel != null) { return(FailError("Please only specify one KV Level in the search query.\nFound " + KVLevel.Name + " and " + (FoundWord as MM_KVLevel).Name)); } else { KVLevel = FoundWord as MM_KVLevel; } } else if (FoundWord is MM_Element_Type) { if (ElemType != null) { return(FailError("Please only specify one element type in the search query.\nFound " + ElemType.Name + " and " + (FoundWord as MM_Element_Type).Name)); } else { ElemType = FoundWord as MM_Element_Type; } } } //If we're doing a control, add 'substation' if (ElemType == null && (ControlPressed || AltPressed)) { ElemType = MM_Repository.FindElementType("Substation"); } //Now go through all elements, searching for results Dictionary <String, bool> ElementWords = new Dictionary <string, bool>(StringComparer.CurrentCultureIgnoreCase); //int tempref = 0; /////nataros this is for a catch test, to remove later foreach (MM_Element Elem in new List <MM_Element>(MM_Repository.TEIDs.Values)) { if (Elem.Name != null && Elem.TEID > 0) { if (Elem is MM_Blackstart_Corridor_Element == false) { if (Elem.Permitted && CheckElement(Elem, KVLevel, ElemType, Words, ElementWords) && Elem.ElemType != null) { AddElement(Elem); } if (AbortSearch) { AbortSearch = false; return(lvSearch.Items.Count > 0); } } else { //catch test, to remove this else case later, needed break pt and counter for test - nataros //tempref = tempref++; } } } this.lvSearch.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); this.Cursor = Cursors.Default; if (viewSummary != null) { viewSummary.SetControls(BaseData); } picAbort.Visible = false; if (lvSearch.Items.Count == 0) { return(FailError("No results found for: " + this.SearchText)); } else if (ControlPressed) { MM_Element SelectedElement = null; bool OnlyOneElement = lvSearch.Items.Count == 1; if (lvSearch.Items.Count == 1) { SelectedElement = lvSearch.Items[0].Tag as MM_Element; } else { foreach (ListViewItem lvi in lvSearch.Items) { if ((lvi.Tag as MM_Element).Name.Equals(SearchText.Trim(), StringComparison.CurrentCultureIgnoreCase)) { if (SelectedElement != null) { OnlyOneElement = false; } else if (SelectedElement == null) { SelectedElement = lvi.Tag as MM_Element; OnlyOneElement = true; } } } } if (OnlyOneElement) { if (SelectedElement is MM_Substation) { nMap.Coordinates.Center = ((SelectedElement as MM_Substation).LngLat); nMap.Coordinates.UpdateZoom(MM_Repository.OverallDisplay.StationZoomLevel); nMap.Coordinates.Center = ((SelectedElement as MM_Substation).LngLat); } else if (SelectedElement is MM_Line) { nMap.Coordinates.Center = (((SelectedElement as MM_Line).Midpoint)); nMap.Coordinates.UpdateZoom(MM_Repository.OverallDisplay.StationZoomLevel); nMap.Coordinates.Center = (((SelectedElement as MM_Line).Midpoint)); } else if (SelectedElement.Substation != null) { nMap.Coordinates.Center = (SelectedElement.Substation.LngLat); nMap.Coordinates.UpdateZoom(MM_Repository.OverallDisplay.StationZoomLevel); nMap.Coordinates.Center = (SelectedElement.Substation.LngLat); } return(false); } } else if (AltPressed) { MM_Element SelectedElement = null; bool OnlyOneElement = lvSearch.Items.Count == 1; if (lvSearch.Items.Count == 1) { SelectedElement = lvSearch.Items[0].Tag as MM_Element; } else { foreach (ListViewItem lvi in lvSearch.Items) { if ((lvi.Tag as MM_Element).Name.Equals(SearchText.Trim(), StringComparison.CurrentCultureIgnoreCase)) { if (SelectedElement != null) { OnlyOneElement = false; } else if (SelectedElement == null) { SelectedElement = lvi.Tag as MM_Element; OnlyOneElement = true; } } } } if (Data_Integration.Permissions.ShowOneLines == true && OnlyOneElement) { if (SelectedElement is MM_Substation) { MM_Form_Builder.OneLine_Display(SelectedElement, nMap); } else if (SelectedElement is MM_Line) { MM_Form_Builder.OneLine_Display(SelectedElement, nMap); } else if (SelectedElement.Substation != null) { MM_Form_Builder.OneLine_Display(SelectedElement.Substation, nMap); } return(false); } } //If we have results, select them if (ItemSelectionChanged != null && lvSearch.Items.Count > 0) { lvSearch.Items[0].Selected = true; } return(true); }
/// <summary> /// Prepare our level for questions. /// </summary> private void PrepareLevel() { MM_KVLevel KVLevel = null; MM_Boundary County = null; MM_Element_Type ElemType = null; if (CurrentLevel.KVLevel != null) { MM_Repository.KVLevels.TryGetValue(CurrentLevel.KVLevel, out KVLevel); } if (CurrentLevel.County != null) { MM_Repository.Counties.TryGetValue(CurrentLevel.County, out County); } if (CurrentLevel.ElemType != null) { MM_Repository.ElemTypes.TryGetValue(CurrentLevel.ElemType, out ElemType); } List <MM_Element> Elements = new List <MM_Element>(); foreach (MM_Element Elem in MM_Repository.TEIDs.Values) { bool Include = true; if (ElemType != null && Elem.ElemType != null && !Elem.ElemType.Equals(ElemType)) { Include = false; } if (Include && CurrentLevel.KVLevel != null) { if (Elem is MM_Substation) { if (((MM_Substation)Elem).KVLevels.IndexOf(KVLevel) == -1) { Include = false; } } else if (Elem.KVLevel != KVLevel) { Include = false; } } if (Include && CurrentLevel.County != null) { if (Elem is MM_Substation && (Elem as MM_Substation).County != County) { Include = false; } else if (Elem is MM_Line && (Elem as MM_Line).Substation1.County != County && (Elem as MM_Line).Substation2.County != County) { Include = false; } } if (Include && !string.IsNullOrEmpty(CurrentLevel.BooleanToCheck)) { MemberInfo[] mI = Elem.GetType().GetMember(CurrentLevel.BooleanToCheck); if (mI.Length != 1) { Include = false;//throw new InvalidOperationException("Unable to locate member " + CurrentLevel.BooleanToCheck); } else if (mI[0] is PropertyInfo) { if (!(bool)((PropertyInfo)mI[0]).GetValue(Elem, null)) { Include = false; } } else if (mI[0] is FieldInfo) { if (!(bool)((FieldInfo)mI[0]).GetValue(Elem)) { Include = false; } } } if (Include) { Elements.Add(Elem); } } QuestionsWrong = 0; QuestionsRight = 0; QuestionsAnswered = 0; PriorLevel = CurrentLevel; AvailableElements = Elements.ToArray(); StartLevel(); }