Hört sich einfach an, ist aber bisl umständlich zu implementieren. Ich hab das mal ausprogrammiert… bin mir aber nicht sicher ob es alle Eigenschaften richtig setzt, bzw. ob es recht performant ist…
Verwendung
var project = ProjectUtility.GetCurrentProject(); Search search = new Search(); search[Search.Settings.AllProperties] = true; search[Search.Settings.Placeholders] = true; search[Search.Settings.DeviceTag] = true; search[Search.Settings.GraphicPages] = true; search[Search.Settings.InstallationSpaces] = true; search[Search.Settings.LogicPages] = true; search[Search.Settings.NotPlaced] = true; search[Search.Settings.EvalutionPages] = false; search[Search.Settings.PageData] = true; search[Search.Settings.ProjectData] = true; search[Search.Settings.Texts] = true; search[Search.Settings.WholeTexts] = true; search.ClearSearchDB(project); // important const string SEARCH_TEXT = "MyStringToSearch"; const string REPLACE_TEXT = "MyNewString"; SearchUtility.SearchAndReplaceText(search, SEARCH_TEXT, REPLACE_TEXT, project);
Methode
/// <summary> /// Find and replace text /// </summary> /// <param name="search">Searchobject with the given properties</param> /// <param name="searchText">Text to search</param> /// <param name="replaceText">Replacement text</param> /// <param name="project">EPLAN Project</param> public static void SearchAndReplaceText(Search search, string searchText, string replaceText, Project project) { // Init search search.ClearSearchDB(project); search.Project(project, searchText); // Get objects StorableObject[] foundObjects = search.GetAllSearchDBEntries(project); foreach (var foundObject in foundObjects) { // Filter only text objects // todo: EPLAN fix (2.6) T1085938 var existingValues = foundObject.Properties.ExistingValues .Where(p => !p.Definition.IsInternal && (p.Definition.Type == PropertyDefinition.PropertyType.MultilangString || p.Definition.Type == PropertyDefinition.PropertyType.String)).ToList(); List<PropertyValue> existingValuesWithoutEmpty = new List<PropertyValue>(); foreach (var propertyValue in existingValues) { if (propertyValue.Definition.IsIndexed) { foreach (int index in propertyValue.Indexes) { if (!propertyValue[index].IsEmpty) { existingValuesWithoutEmpty.Add(propertyValue[index]); } } } else { if (!propertyValue.IsEmpty) { existingValuesWithoutEmpty.Add(propertyValue); } } } existingValues = existingValuesWithoutEmpty; // Replace foreach (PropertyValue propertyValue in existingValues) { switch (propertyValue.Definition.Type) { // MultiLanguageString case PropertyDefinition.PropertyType.MultilangString: MultiLangString multiLangString = propertyValue; var valueMultiLangString = multiLangString.GetAsString(); if (valueMultiLangString.Contains(searchText)) { string newValue = valueMultiLangString.Replace(searchText, replaceText); // All languages multiLangString.SetAsString(newValue); propertyValue.Set(newValue); } break; // String case PropertyDefinition.PropertyType.String: var value = propertyValue.ToString(); if (value.Contains(searchText)) { string newValue = value.Replace(searchText, replaceText); propertyValue.Set(newValue); } break; } } } }
Hinterlasse einen Kommentar