EPLAN API 入门系列- 实战篇(How to Change Properties in EPlan?)

How to Change Properties in EPlan?

In general,I think there are tow ways:

1、Offline programe in Console Application

 1         public void Execute()
 2         {
 3             Console.WriteLine("Update Article Properties Demo");
 4 
 5             //Start P8
 6             EplApplication eplApp = new EplApplication();
 7             eplApp.Init("", true, true);  
 8 
 9             Project prj = null;
10             try
11             {
12                 using (LockingStep ls = new LockingStep())
13                 {
14                     // 1. Open a project
15                     Console.WriteLine("Opening project...");
16                     eplApp.OpenProjectDlg();
17 
18                     ProjectManager prjMngr = new ProjectManager();
19                     if (prjMngr.CurrentProject == null)
20                     {
21                         Console.WriteLine("No project is open. Finished.");
22                         return;
23                     }
24                     prj = prjMngr.CurrentProject;
25                     Console.WriteLine("Project '{0}' is open.", prj.ProjectFullName);
26 
27                     // 2. Get all terminal strips in the project
28                     DMObjectsFinder fndr = new DMObjectsFinder(prj);
29                     TerminalStrip[] arrTermStrips = fndr.GetTerminalStrips(null);
30 
31                     using (UndoStep us = (new UndoManager()).CreateUndoStep())
32                     {
33                         // 3. Change a property
34                         Console.WriteLine("Modifying terminal strips...");
35                         foreach (TerminalStrip ts in arrTermStrips)
36                         {
37                             if (ts.ManualPlacementType != DocumentTypeManager.DocumentType.Circuit
38                                 && ts.ManualPlacementType != DocumentTypeManager.DocumentType.CircuitSingleLine) continue;
39  
40                             string sNewValue = string.Format("{0} - Change at {1}", (new Random()).Next(), DateTime.Now.ToShortTimeString());
41 
42                             MessageBox.Show(sNewValue.ToString());
43 
44                             MultiLangString mls = new MultiLangString();
45                             mls.AddString((new Languages()).GuiLanguage.GetNumber(), sNewValue);
46                             ts.Properties.FUNC_TEXT = mls;
47                         }
48 
49                         // revert changes
50                         Console.WriteLine("Reverting changes in project...");
51                         us.DoUndo(); 
52                     }
53 
54                     Console.WriteLine("Done.");
55                 }
56             }
57             catch (System.Exception ex)
58             {
59                 Console.WriteLine("ERROR. " + ex.Message);
60             }
61             finally
62             {
63                 if (prj != null) prj.Close();
64 
65                 GC.Collect();
66                 GC.WaitForPendingFinalizers();
67 
68                 Console.WriteLine("Press any key.");
69                 Console.ReadKey();
70 
71                 eplApp.Exit();  
72             }
73         }

2、Online programe via EventListerner in Eplan.Action.Addin

EventListerner class:

 1     public class SystemEventListerner
 2     {
 3         [DeclareEventHandler("onActionStart.String.*")]
 4         public long MyEventHandlerFunction(String strActionName)
 5         {
 6             try
 7             {
 8                 System.Windows.Forms.MessageBox.Show("Action " + strActionName + " was started!",
 9                                         "MyEventHandler");
10             }
11             catch (System.InvalidCastException exc)
12             {
13                 String strExc = exc.Message;
14                 System.Windows.Forms.MessageBox.Show("Parameter error: " + strExc, "MyEventHandler");
15             }
16 
17             return 0;
18 
19         }
20     }

Addin Class:

 1         #region IEplAddIn Members
 2 
 3         public bool OnExit()
 4         {
 5             return true;
 6         }
 7 
 8         SystemEventListerner _lstnr = null; 
 9 
10         public bool OnInit()
11         {
12             _lstnr = new SystemEventListerner();
13             _lstnr.MyEventHandlerFunction(Action.NAME);
14 
15             return true;
16         }
17 
18         public bool OnInitGui()
19         {
20             Menu m = new Menu();
21 
22             uint id = m.GetPersistentMenuId("System messages");
23 
24             //menu item
25             id = m.AddMainMenu("Example", Menu.MainMenuName.eMainMenuUtilities, "Example #1", Action.NAME, Action.DESCR, 1);
26              
27             return true;
28         }
29 
30         public bool OnRegister(ref bool bLoadOnStart)
31         {
32             //bLoadOnStart = false; // this prevents addin intialization (OnInit, OnInitGui) when P8 starts; not usefull
33             return true;
34         }
35 
36         public bool OnUnregister()
37         {
38             _lstnr = null;
39             return true;
40         }
41 
42         #endregion

Action class:

 1         public bool Execute(ActionCallingContext oActionCallingContext)
 2         {
 3             try
 4             {
 5                 using (LockingStep ls = new LockingStep())
 6                 {  
 7                     // 1. Get all terminal strips in the project
 8                     SelectionSet oSelSet = new SelectionSet();
 9                     Project oProj = oSelSet.GetCurrentProject(true);
10 
11                     DMObjectsFinder fndr = new DMObjectsFinder(oProj);
12                     TerminalStrip[] arrTermStrips = fndr.GetTerminalStrips(null);
13 
14                     using (UndoStep us = (new UndoManager()).CreateUndoStep())
15                     {
16                         // 2. Change a property
17                         Console.WriteLine("Modifying terminal strips...");
18                         foreach (TerminalStrip ts in arrTermStrips)
19                         {
20                             if (ts.ManualPlacementType != DocumentTypeManager.DocumentType.Circuit
21                                 && ts.ManualPlacementType != DocumentTypeManager.DocumentType.CircuitSingleLine) continue;
22 
23                             string sNewValue = string.Format("{0} - Change at {1}", (new Random()).Next(), DateTime.Now.ToShortTimeString());
24 
25                             MessageBox.Show(sNewValue.ToString());
26 
27                             MultiLangString mls = new MultiLangString();
28                             mls.AddString((new Languages()).GuiLanguage.GetNumber(), sNewValue);
29                             ts.Properties.FUNC_TEXT = mls;
30                         }
31 
32                         // revert changes
33                         //Console.WriteLine("Reverting changes in project...");
34                         us.DoUndo();
35                     }
36 
37                     Console.WriteLine("Done.");
38                 }
39             }
40             catch (System.Exception ex)
41             {
42                 //Console.WriteLine("ERROR. " + ex.Message);
43             }
44             finally
45             {
46                 //if (prj != null) prj.Close();
47 
48                 GC.Collect();
49                 GC.WaitForPendingFinalizers();
50 
51                 //Console.WriteLine("Press any key.");
52                 //Console.ReadKey();
53 
54                 //eplApp.Exit();
55             }
56 
57             return true;
58         }

if any question,welcome you to comment...

posted @ 2013-04-11 15:40  AriLee  Views(4012)  Comments(0Edit  收藏  举报