EPLAN-API

Alles rund um die API in EPLAN

PartsManagementExtension

Ihr wollte eine eigene Registerkarte in der EPLAN Artikeldatenbank? Kein Problem, zumindest per API.
Die Funktionen hier sind von EPLAN nicht offiziell Freigegeben und werden nicht supported. Dennoch gibt es eine kleine Beschreibung in der API-Hilfe.

Die Implementierung ist nicht ganz so einfach, da hier mit manuellen Events gearbeitet wird und diese muss man dann erst in der Action senden und im UserControl registrieren.

Addin:

using System.IO;
using System.Reflection;
using Eplan.EplApi.ApplicationFramework;
using Eplan.EplApi.MasterData;
using Eplan.EplSDK.WPF;
using Eplan.EplSDK.WPF.DB;

namespace Suplanus.Example.EplAddIn.PartsManagementExtensionExample
{
    class Addin : IEplAddIn
    {
        public string TabsheetName => nameof(PartsManagementExtensionContent);
        public string ItemType => "eplan.part";

        public string AddinName
        {
            get
            {
                string addinName = typeof(Addin).Assembly.CodeBase;
                addinName = Path.GetFileNameWithoutExtension(addinName);
                return addinName;
            }
        }        

        public bool OnRegister(ref bool isLoadingOnStart)
        {
            isLoadingOnStart = true;
            return true;
        }

        public bool OnUnregister()
        {
            var partsManagement = new MDPartsManagement();
            partsManagement.UnregisterAddin(AddinName);
            partsManagement.UnregisterItem(ItemType);
            partsManagement.UnregisterTabsheet(TabsheetName);

            return true;
        }

        public bool OnInit()
        {
            return true;
        }

        public bool OnInitGui()
        {
            var partsManagement = new MDPartsManagement();
            string actionName = nameof(PartsManagementExtensionExampleAction);

            partsManagement.RegisterAddin(AddinName, actionName);            
            partsManagement.RegisterItem(AddinName, ItemType);
            partsManagement.RegisterTabsheet(AddinName, ItemType, TabsheetName);
            DialogFactoryDB dialogBarFactory = new DialogFactoryDB(TabsheetName,
                typeof(PartsManagementExtensionContent));
            PartsManagementExtensionExampleAction.TabsheetName = TabsheetName;

            return true;
        }

        public bool OnExit()
        {
            return true;
        }
    }
}

 

Action:

using Eplan.EplApi.ApplicationFramework;
using Eplan.EplSDK.WPF.EEvent;

namespace Suplanus.Example.EplAddIn.PartsManagementExtensionExample
{
    class PartsManagementExtensionExampleAction : IEplAction
    {
        public static string TabsheetName = null;

        public void GetActionProperties(ref ActionProperties actionProperties) { }

        public bool OnRegister(ref string name, ref int ordinal)
        {
            name = nameof(PartsManagementExtensionExampleAction);
            return true;
        }

        public bool Execute(ActionCallingContext actionCallingContext)
        {
            // Sent events to WPF control from base action
            string itemType = string.Empty;
            string action = string.Empty;
            string key = string.Empty;
            actionCallingContext.GetParameter("itemtype", ref itemType);
            actionCallingContext.GetParameter("action", ref action);
            actionCallingContext.GetParameter("key", ref key);

            WPFDialogEventManager wpfDialogEventManager = new WPFDialogEventManager();

            switch (action)
            {
                case "SelectItem":
                case "SaveItem":
                case "PreShowTab":
                case "OpenDatabase":
                case "CreateDatabase":
                    wpfDialogEventManager.send("XPartsManagementDialog", action, key);
                    break;
            }

            return true;
        }
    }
}

 

XAML: CodeBehind

using Eplan.EplApi.MasterData;
using Eplan.EplSDK.WPF.EEvent;
using Eplan.EplSDK.WPF.Interfaces.DialogServices;

namespace Suplanus.Example.EplAddIn.PartsManagementExtensionExample
{
    public partial class PartsManagementExtensionContent : IDialog
    {
        public string Caption => nameof(PartsManagementExtensionContent);
        public bool IsTabsheet => true;
        public ViewModel ViewModel { get; set; } 

        private readonly MDPartsManagement _partsManagement = new MDPartsManagement();

        public PartsManagementExtensionContent()
        {
            InitializeComponent();

            ViewModel = new ViewModel();
            DataContext = ViewModel;

            ViewModel.IsReadOnly = _partsManagement.SelectedPartsDatabase.IsReadOnly;

            // Events, called from Action of this Tab
            WPFDialogEventManager dialogEventManager = new WPFDialogEventManager();
            dialogEventManager.getOnWPFNotifyEvent("XPartsManagementDialog", "SelectItem").Notify += SelectItem;
            dialogEventManager.getOnWPFNotifyEvent("XPartsManagementDialog", "SaveItem").Notify += SaveItem;
            dialogEventManager.getOnWPFNotifyEvent("XPartsManagementDialog", "PreShowTab").Notify += PreShowTab;
            dialogEventManager.getOnWPFNotifyEvent("XPartsManagementDialog", "OpenDatabase").Notify += OpenDatabase;
            dialogEventManager.getOnWPFNotifyEvent("XPartsManagementDialog", "CreateDatabase").Notify += CreateDatabase;
        }

        private void CreateDatabase(string data)
        {
            
        }

        private void OpenDatabase(string data)
        {
            
        }

        private void PreShowTab(string data)
        {
            
        }

        private void SelectItem(string data)
        {
            
        }

        private void SaveItem(string data)
        {
            
        }

    }
}

 

XAML: UserControl

<UserControl
    x:Class="Suplanus.Example.EplAddIn.PartsManagementExtensionExample.PartsManagementExtensionContent"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    d:DesignHeight="300"
    d:DesignWidth="300"
    DataContext="{Binding RelativeSource={RelativeSource Self}, Path=ViewModel}"
    mc:Ignorable="d">
    <Grid Margin="10">
        <Viewbox>
            <TextBox
                IsEnabled="{Binding IsEnabled}"
                Text="Hello PartsManagementExtension!"
                TextWrapping="Wrap" />
        </Viewbox>
    </Grid>
</UserControl>

 

ViewModel:

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Suplanus.Example.EplAddIn.PartsManagementExtensionExample
{
    public class ViewModel : INotifyPropertyChanged
    {
        private bool _isReadonly;
        public bool IsReadOnly
        {
            get { return _isReadonly; }
            set
            {
                if (value == _isReadonly) return;
                _isReadonly = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

 

Natürlich findet Ihr das wie gewohnt auf GitHub!

Von |2019-03-14T08:42:17+01:002017-10-18|EPLAN, EPLAN-API|

Navigator in API

Ewig steht das schon auf meiner Todo-Liste: Einen eigenen Navi per API implementieren.
Die Möglichkeit welche ich hier beschreibe ist nicht teil der offiziellen EPLAN API. Somit bekommt man auch kein Support oder Ähnliches.

Dennoch finde ich die Möglichkeit super und denke viele von Euch haben dafür auch einen Anwendungsfall.

Eigentlich braucht man nur eine DialogBarFactory und ein WPF-UserControl dazu:

using System.Reflection;
using Eplan.EplApi.ApplicationFramework;
using Eplan.EplSDK.WPF;

namespace Suplanus.EplAddIn.NavigatorExample
{
    class NavigatorExample : IEplAddIn
    {
        public bool OnRegister(ref bool isLoadingOnStart)
        {
            isLoadingOnStart = true;
            return true;
        }

        public bool OnUnregister()
        {
            return true;
        }

        public bool OnInit()
        {
            return true;
        }

        public bool OnInitGui()
        {
            // Get name from class
            // ReSharper disable once PossibleNullReferenceException
            var className = MethodBase.GetCurrentMethod().DeclaringType.Name;
            DialogBarFactory dialogBarFactory = new DialogBarFactory(className, typeof(NavigatorContent), DialogDockingOptions.Any, 0);

            return true;
        }

        public bool OnExit()
        {
            return true;
        }
    }
}

Im UserControl müssen aber viele Interfaces implementiert sein :^)

using System.Reflection;
using Eplan.EplSDK.WPF.Interfaces;
using Eplan.EplSDK.WPF.Interfaces.DialogServices;

namespace Suplanus.EplAddIn.NavigatorExample
{
    public partial class NavigatorContent : IDialog, IDialogBar, IDialogComponentAccess, ICallingContext, IDialogState, IDialogAction, IDialogClose, IElementStateAccess
    {
        public string Caption { get; set; }
        public bool IsTabsheet { get; set; }
        public int UniqueBarID { get; set; }
        public IDialogComponent Component { get; set; }
        public object Context { get; set; }
        public IDialogStateManager DialogStateManager { get; set; }
        public IElementStateCollection ElementStateCollection { get; set; }

        public NavigatorContent()
        {
            // Iniit
            ElementStateCollection = new Eplan.EplSDK.WPF.Controls.Persistency.ElementStateCollection();
            ElementStateCollection.Load(nameof(NavigatorContent));

            InitializeComponent();

            Caption = "NavigatorExample";
            IsTabsheet = false;

            // Use Class name for uniqueid
            // ReSharper disable once PossibleNullReferenceException
            var className = MethodBase.GetCurrentMethod().DeclaringType.Name;
            UniqueBarID = (int)(uint)className.GetHashCode();           
            
        }
       
        public void init()
        {
            
        }

        public bool isValid()
        {
            return true;
        }

        public void reload()
        {
            
        }

        public void save()
        {
            
        }

        public void close()
        {
            
        }

    }
}

Anbei ein Beispiel UserControl in WPF:

<UserControl
    x:Class="Suplanus.EplAddIn.NavigatorExample.NavigatorContent"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    d:DesignHeight="300"
    d:DesignWidth="300"
    mc:Ignorable="d">
    <Grid Margin="10">
        <Viewbox>
            <TextBlock Text="Hello Navigator!" TextWrapping="Wrap" />
        </Viewbox>
    </Grid>
</UserControl>

Eine lauffähiges Beispiel findet Ihr auf GitHub

Von |2022-04-13T11:20:49+02:002017-09-28|EPLAN, EPLAN-API|

Multiuserkonflikt erkennen

Vorab: Leider geht das nur per API, nicht im Scripting.

Die Ermittlung der User ist schnell gemacht, hab aber noch einen Dialog dazu gepackt, welcher Infos über die User anzeigt:

public static bool IsMultiUserConflict(Project project, bool showDialog = false)
{
   var currentUsers = project.CurrentUsers.ToList();

   // No conflict
   if (currentUsers.Count <= 1)
   {
      return false;
   }

   // Conflict
   if (showDialog)
   {
      StringBuilder sb = new StringBuilder();
      foreach (var user in currentUsers)
      {
         if (!string.IsNullOrEmpty(user.Name) && !string.IsNullOrEmpty(user.Identification))
         {
            sb.AppendLine(user.ComputerName + " / " + user.Name + " / " + user.Identification);
         }
         else if (!string.IsNullOrEmpty(user.Name))
         {
            sb.AppendLine(user.ComputerName + " / " + user.Name);
         }
         else if (!string.IsNullOrEmpty(user.Identification))
         {
            sb.AppendLine(user.ComputerName + " / " + user.Identification);
         }
         else
         {
            sb.AppendLine(user.ComputerName);
         }
      }
      MessageBox.Show(sb.ToString(), "Multi user conflict", MessageBoxButton.OK, MessageBoxImage.Warning);
   }
   return true;
}
Von |2017-05-03T13:34:52+02:002017-05-04|EPLAN, EPLAN-API|

EPLAN-API: Suchen & Ersetzen

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;
         }

      }
   }
}
Von |2016-12-13T07:55:09+01:002016-12-13|EPLAN, EPLAN-API|
Nach oben