您的位置:首页 > 运维架构

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

2013-04-11 15:40 1131 查看
How to Change Properties in EPlan?

In general,I think there are tow ways:

1、Offline programe in Console Application

public void Execute()
{
Console.WriteLine("Update Article Properties Demo");

//Start P8
EplApplication eplApp = new EplApplication();
eplApp.Init("", true, true);

Project prj = null;
try
{
using (LockingStep ls = new LockingStep())
{
// 1. Open a project
Console.WriteLine("Opening project...");
eplApp.OpenProjectDlg();

ProjectManager prjMngr = new ProjectManager();
if (prjMngr.CurrentProject == null)
{
Console.WriteLine("No project is open. Finished.");
return;
}
prj = prjMngr.CurrentProject;
Console.WriteLine("Project '{0}' is open.", prj.ProjectFullName);

// 2. Get all terminal strips in the project
DMObjectsFinder fndr = new DMObjectsFinder(prj);
TerminalStrip[] arrTermStrips = fndr.GetTerminalStrips(null);

using (UndoStep us = (new UndoManager()).CreateUndoStep())
{
// 3. Change a property
Console.WriteLine("Modifying terminal strips...");
foreach (TerminalStrip ts in arrTermStrips)
{
if (ts.ManualPlacementType != DocumentTypeManager.DocumentType.Circuit
&& ts.ManualPlacementType != DocumentTypeManager.DocumentType.CircuitSingleLine) continue;

string sNewValue = string.Format("{0} - Change at {1}", (new Random()).Next(), DateTime.Now.ToShortTimeString());

MessageBox.Show(sNewValue.ToString());

MultiLangString mls = new MultiLangString();
mls.AddString((new Languages()).GuiLanguage.GetNumber(), sNewValue);
ts.Properties.FUNC_TEXT = mls;
}

// revert changes
Console.WriteLine("Reverting changes in project...");
us.DoUndo();
}

Console.WriteLine("Done.");
}
}
catch (System.Exception ex)
{
Console.WriteLine("ERROR. " + ex.Message);
}
finally
{
if (prj != null) prj.Close();

GC.Collect();
GC.WaitForPendingFinalizers();

Console.WriteLine("Press any key.");
Console.ReadKey();

eplApp.Exit();
}
}


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

EventListerner class:

public class SystemEventListerner
{
[DeclareEventHandler("onActionStart.String.*")]
public long MyEventHandlerFunction(String strActionName)
{
try
{
System.Windows.Forms.MessageBox.Show("Action " + strActionName + " was started!",
"MyEventHandler");
}
catch (System.InvalidCastException exc)
{
String strExc = exc.Message;
System.Windows.Forms.MessageBox.Show("Parameter error: " + strExc, "MyEventHandler");
}

return 0;

}
}


Addin Class:

#region IEplAddIn Members

public bool OnExit()
{
return true;
}

SystemEventListerner _lstnr = null;

public bool OnInit()
{
_lstnr = new SystemEventListerner();
_lstnr.MyEventHandlerFunction(Action.NAME);

return true;
}

public bool OnInitGui()
{
Menu m = new Menu();

uint id = m.GetPersistentMenuId("System messages");

//menu item
id = m.AddMainMenu("Example", Menu.MainMenuName.eMainMenuUtilities, "Example #1", Action.NAME, Action.DESCR, 1);

return true;
}

public bool OnRegister(ref bool bLoadOnStart)
{
//bLoadOnStart = false; // this prevents addin intialization (OnInit, OnInitGui) when P8 starts; not usefull
return true;
}

public bool OnUnregister()
{
_lstnr = null;
return true;
}

#endregion


Action class:

public bool Execute(ActionCallingContext oActionCallingContext)
{
try
{
using (LockingStep ls = new LockingStep())
{
// 1. Get all terminal strips in the project
SelectionSet oSelSet = new SelectionSet();
Project oProj = oSelSet.GetCurrentProject(true);

DMObjectsFinder fndr = new DMObjectsFinder(oProj);
TerminalStrip[] arrTermStrips = fndr.GetTerminalStrips(null);

using (UndoStep us = (new UndoManager()).CreateUndoStep())
{
// 2. Change a property
Console.WriteLine("Modifying terminal strips...");
foreach (TerminalStrip ts in arrTermStrips)
{
if (ts.ManualPlacementType != DocumentTypeManager.DocumentType.Circuit
&& ts.ManualPlacementType != DocumentTypeManager.DocumentType.CircuitSingleLine) continue;

string sNewValue = string.Format("{0} - Change at {1}", (new Random()).Next(), DateTime.Now.ToShortTimeString());

MessageBox.Show(sNewValue.ToString());

MultiLangString mls = new MultiLangString();
mls.AddString((new Languages()).GuiLanguage.GetNumber(), sNewValue);
ts.Properties.FUNC_TEXT = mls;
}

// revert changes
//Console.WriteLine("Reverting changes in project...");
us.DoUndo();
}

Console.WriteLine("Done.");
}
}
catch (System.Exception ex)
{
//Console.WriteLine("ERROR. " + ex.Message);
}
finally
{
//if (prj != null) prj.Close();

GC.Collect();
GC.WaitForPendingFinalizers();

//Console.WriteLine("Press any key.");
//Console.ReadKey();

//eplApp.Exit();
}

return true;
}


if any question,welcome you to comment...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: