Code:
class InsertWindowMacro
{
[Start]
public void Do()
{
string pathWindowMacro = "PfadFenstermakro"
var Macro = GetMacro(pathWindowMacro);
InsertMacro(Macro); }
public object GetMacro(string filePath)
{
var _MasterDataAssembly = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(a => a.FullName.StartsWith("Eplan.EplApi.DataModel"));
var typeWindowMacro = _MasterDataAssembly.ExportedTypes.FirstOrDefault(t => t.Name == "WindowMacro");
var lWindowMacro = Activator.CreateInstance(typeWindowMacro);
var firstSelProject = GetSelectedProject();
//string[] Pages = GetPages();
//MessageBox.Show(RepresentationType.ToString());
var args = new object[] { filePath, firstSelProject, lWindowMacro.GetProp1("RepresentationType"),0 };
lWindowMacro.DoMethod1("Open", args);
return lWindowMacro;
}
public object GetSelectedProject()
{
var _HEServicesAssembly = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(a => a.FullName.StartsWith("Eplan.EplApi.HEServices"));
var typeSelectionSet = _HEServicesAssembly.ExportedTypes.FirstOrDefault(t => t.Name == "SelectionSet");
object selectionSet = Activator.CreateInstance(typeSelectionSet);
//selectionSet.SetProp("LockProjectByDefault", false);
selectionSet.SetProp1("LockSelectionByDefault", false);
IEnumerable<object> selections = (IEnumerable<object>)selectionSet.GetProp1("SelectedProjects");
//foreach (var item in selection)
//{
// MessageBox.Show(item.GetType().ToString());
//}
if (selections.Count() > 0)
{
return selections.First();
}
else
{
return null;
}
}
public void InsertMacro(object WindowMacro)
{
var _HEServicesAssembly = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(a => a.FullName.StartsWith("Eplan.EplApi.HEServices"));
var typeInsert = _HEServicesAssembly.ExportedTypes.FirstOrDefault(t => t.Name == "Insert");
object insert = Activator.CreateInstance(typeInsert);
var _MasterDataAssembly = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(a => a.FullName.StartsWith("Eplan.EplApi.DataModel"));
var typeProject = _MasterDataAssembly.ExportedTypes.FirstOrDefault(t => t.Name == "Project");
var Project = Activator.CreateInstance(typeProject);
var firstSelProject = GetSelectedProject();
Project = firstSelProject;
var args = new object[] { WindowMacro, Project.Pages[1], new PointD(0.0, 0.0), insert.GetProp1("MoveKind") };
insert.DoMethod1("WindowMacro", args);
}
}
public static class Utils
{
public static object GetProp1(this object obj, string name)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(obj);
PropertyDescriptor prop = properties.Find(name, false);
return prop.GetValue(obj);
}
public static object GetProp1(this object obj, string name, int index)
{
Type type = obj.GetType();
var info = type.GetProperties().FirstOrDefault(p => p.ToString().Contains(name + " [Int32]"));
if (info == null) { return null; }
return info.GetValue(obj, new object[] { index });
}
public static void SetProp1(this object obj, string name, object value)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(obj);
PropertyDescriptor prop = properties.Find(name, false);
prop.SetValue(obj, value);
}
public static void SetProp1(this object obj, string name, object value, int index)
{
Type type = obj.GetType();
var info = type.GetProperties().FirstOrDefault(p => p.ToString().Contains(name + " [Int32]"));
info.SetValue(obj, value, new object[] { index });
}
public static int ToInteger(this string txt)
{
int value = 0;
if (int.TryParse(txt, out value))
{
return value;
}
else
{
return 1;
}
}
public static object DoMethod1(this object obj, string name, object[] args)
{
var method = obj.GetType().GetMethods().FirstOrDefault(m => m.Name.Contains(name));
return method.Invoke(obj, args);
}
}