-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ProjectUtility.cs
256 lines (234 loc) · 8.73 KB
/
ProjectUtility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using Eplan.EplApi.DataModel;
using Eplan.EplApi.DataModel.MasterData;
using Eplan.EplApi.HEServices;
using Suplanus.Sepla.Objects;
namespace Suplanus.Sepla.Helper
{
/// <summary>
/// Project helper class
/// </summary>
public class ProjectUtility
{
/// <summary>
/// Returns the active project
/// </summary>
/// <returns>Active Project</returns>
public static Project GetCurrentProject()
{
SelectionSet selectionSet = new SelectionSet();
selectionSet.LockProjectByDefault = false;
selectionSet.LockSelectionByDefault = false;
return selectionSet.GetCurrentProject(false);
}
/// <summary>
/// Creates project or get the existing project, or overwrite the existing
/// </summary>
/// <param name="projectLinkFilePath">EPLAN project file (*.elk)</param>
/// <param name="projectTemplateFilePath">EPLAN template project (*.zw9)</param>
/// <param name="overwrite">Overwrites the existing project, False: Open the project</param>
/// <returns>EPLAN Project</returns>
public static Project Create(string projectLinkFilePath, string projectTemplateFilePath, bool overwrite)
{
ProjectManager projectManager = new ProjectManager();
Project project = null;
using (new LockingStep()) // needed
{
// Exists
if (projectManager.ExistsProject(projectLinkFilePath) && overwrite == false)
{
project = OpenProject(projectLinkFilePath);
}
// New
if (!projectManager.ExistsProject(projectLinkFilePath) || overwrite == true)
{
// Close if project exists and should be overwritten
var existingProject = projectManager.OpenProjects.FirstOrDefault(p => p.ProjectLinkFilePath.Equals(projectLinkFilePath));
if (existingProject != null)
{
existingProject.Close();
}
// Remove project if exists: Removing on file layer is much faster than via EPLAN API
DeleteProject(projectLinkFilePath, projectManager);
project = projectManager.CreateProject(projectLinkFilePath, projectTemplateFilePath);
}
}
return project;
}
private static void DeleteProject(string projectLinkFilePath, ProjectManager projectManager)
{
try
{
if (projectManager.ExistsProject(projectLinkFilePath))
{
// Project folder
var suffix = ".elk";
string projectFolder = projectLinkFilePath.Substring(0, projectLinkFilePath.Length - suffix.Length);
projectFolder = projectFolder + ".edb";
Directory.Delete(projectFolder, true);
// Link file
File.Delete(projectLinkFilePath);
}
}
catch (Exception exception)
{
ExceptionHelper.HandleException(exception, "MacroTool");
throw new Exception("Project could not be removed:" + Environment.NewLine + projectLinkFilePath, exception);
}
}
/// <summary>
/// Copy the project or get the existing project, or overwrite the existing
/// </summary>
/// <param name="projectSource">Source project</param>
/// <param name="projectLinkFilePath">EPLAN project file (*.elk)</param>
/// <param name="overwrite">Overwrites the existing project, False: Open the project</param>
/// <param name="copyMode"></param>
/// <returns>EPLAN Project</returns>
public static Project Copy(Project projectSource, string projectLinkFilePath, bool overwrite, ProjectManager.CopyMode copyMode)
{
using (new LockingStep()) // needed
{
ProjectManager projectManager = new ProjectManager();
// New
if (!projectManager.ExistsProject(projectLinkFilePath) || overwrite == true)
{
projectManager.CopyProject(projectSource.ProjectLinkFilePath, projectLinkFilePath,
copyMode);
}
return OpenProject(projectLinkFilePath);
}
}
/// <summary>
/// Copy the project or get the existing project, or overwrite the existing
/// </summary>
/// <param name="projectSource">Source project</param>
/// <param name="projectLinkFilePath">EPLAN project file (*.elk)</param>
/// <param name="overwrite">Overwrites the existing project, False: Open the project</param>
/// <param name="copyMode"></param>
/// <returns>EPLAN Project</returns>
public static Project Copy(string projectSource, string projectLinkFilePath, bool overwrite, ProjectManager.CopyMode copyMode)
{
using (new LockingStep()) // needed
{
ProjectManager projectManager = new ProjectManager();
// New
if (!projectManager.ExistsProject(projectLinkFilePath) || overwrite == true)
{
projectManager.CopyProject(projectSource, projectLinkFilePath,
copyMode);
}
return OpenProject(projectLinkFilePath);
}
}
/// <summary>
/// Opens project and checks if its open
/// </summary>
/// <param name="projectLinkFilePath">EPLAN project file (*.elk)</param>
/// <param name="openMode">Set how the project should be opened</param>
/// <param name="upgradeIfNeeded">Set if the project should be upgraded from an older version</param>
/// <returns>EPLAN Project</returns>
public static Project OpenProject(string projectLinkFilePath, ProjectManager.OpenMode openMode = ProjectManager.OpenMode.Standard, bool upgradeIfNeeded = true)
{
if (!File.Exists(projectLinkFilePath))
{
throw new FileNotFoundException("EPLAN project link file not found", projectLinkFilePath);
}
using (new LockingStep())
{
ProjectManager projectManager = new ProjectManager();
// Use filename because the path could be other (UNC / Pathvariable)
var filenameWithoutExtendsion = Path.GetFileNameWithoutExtension(projectLinkFilePath);
var project = projectManager.OpenProjects.FirstOrDefault(p => p.ProjectName.Equals(filenameWithoutExtendsion));
// Check if openMode is OK
if (project != null)
{
bool reOpen = false;
switch (openMode)
{
case ProjectManager.OpenMode.Standard:
if (project.IsExclusive || project.IsReadOnly)
{
reOpen = true;
}
break;
case ProjectManager.OpenMode.ReadOnly:
if (!project.IsReadOnly)
{
reOpen = true;
}
break;
case ProjectManager.OpenMode.Exclusive:
if (!project.IsExclusive)
{
reOpen = true;
}
break;
default: throw new ArgumentOutOfRangeException(nameof(openMode), openMode, null);
}
if (reOpen)
{
project.Close();
project = OpenProject(projectLinkFilePath, openMode);
}
return project;
}
return projectManager.OpenProject(projectLinkFilePath, openMode, upgradeIfNeeded);
}
}
/// <summary>
/// Returns true if there is a multi user conflict in project
/// </summary>
/// <param name="project">EPLAN project</param>
/// <param name="showDialog">Shows dialog if there is a conflict (optional)</param>
/// <returns></returns>
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;
}
public static bool IsCircuitProject(Project project)
{
var projectType = project.Properties.PROJ_NUMERICTYPE.ToInt();
if (projectType == 1) // 1=CircutitProject 2=MacroProject
{
return true;
}
return false;
}
}
}