/
CyPhyMetaLinkInterpreter.cs
791 lines (708 loc) · 32.6 KB
/
CyPhyMetaLinkInterpreter.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using GME.CSharp;
using GME;
using GME.MGA;
using GME.MGA.Core;
using GmeCommon = ISIS.GME.Common;
using CyPhyML = ISIS.GME.Dsml.CyPhyML.Interfaces;
using CyPhyMLClasses = ISIS.GME.Dsml.CyPhyML.Classes;
using META;
using MetaLinkBridge = CyPhyMetaLinkBridgeClient;
using MetaLinkProtobuf = edu.vanderbilt.isis.meta;
using System.Diagnostics;
using System.Threading;
using System.Security;
using System.Drawing;
using Microsoft.Win32;
using System.Threading.Tasks;
using System.ComponentModel;
using CyPhyGUIs;
namespace CyPhyMetaLink
{
/// <summary>
/// This class implements the necessary COM interfaces for a GME interpreter component.
/// </summary>
[Guid(ComponentConfig_Intf.guid),
ProgId(ComponentConfig_Intf.progID),
ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class CyPhyMetalinkInterpreter : IMgaComponentEx, IGMEVersionInfo
{
/// <summary>
/// Contains information about the GUI event that initiated the invocation.
/// </summary>
public enum ComponentStartMode
{
GME_MAIN_START = 0, // Not used by GME
GME_BROWSER_START = 1, // Right click in the GME Tree Browser window
GME_CONTEXT_START = 2, // Using the context menu by right clicking a model element in the GME modeling window
GME_EMBEDDED_START = 3, // Not used by GME
GME_MENU_START = 16, // Clicking on the toolbar icon, or using the main menu
GME_BGCONTEXT_START = 18, // Using the context menu by right clicking the background of the GME modeling window
GME_ICON_START = 32, // Not used by GME
GME_SILENT_MODE = 128 // Not used by GME, available to testers not using GME
}
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
/// <summary>
/// This function is called for each interpreter invocation before Main.
/// Don't perform MGA operations here unless you open a tansaction.
/// </summary>
/// <param name="project">The handle of the project opened in GME, for which the interpreter was called.</param>
public void Initialize(MgaProject project)
{
}
/// <summary>
/// The main entry point of the interpreter. A transaction is already open,
/// GMEConsole is available. A general try-catch block catches all the exceptions
/// coming from this function, you don't need to add it. For more information, see InvokeEx.
/// </summary>
/// <param name="project">The handle of the project opened in GME, for which the interpreter was called.</param>
/// <param name="currentobj">The model open in the active tab in GME. Its value is null if no model is open (no GME modeling windows open). </param>
/// <param name="selectedobjs">
/// A collection for the selected model elements. It is never null.
/// If the interpreter is invoked by the context menu of the GME Tree Browser, then the selected items in the tree browser. Folders
/// are never passed (they are not FCOs).
/// If the interpreter is invoked by clicking on the toolbar icon or the context menu of the modeling window, then the selected items
/// in the active GME modeling window. If nothing is selected, the collection is empty (contains zero elements).
/// </param>
/// <param name="startMode">Contains information about the GUI event that initiated the invocation.</param>
[ComVisible(false)]
public void Main(MgaProject project, MgaFCO currentobj, MgaFCOs selectedobjs, ComponentStartMode startMode)
{
GMEConsole.Error.WriteLine("This interpreter doesn't run in console mode.");
}
private CyPhyML.ComponentAssembly FindTopLevelAssembly(GmeCommon.Interfaces.FCO componentasm)
{
CyPhyML.ComponentAssembly topassembly = null;
if (componentasm.ParentContainer.Kind == "ComponentAssemblies") // Top Level Assembly
{
topassembly = (componentasm as CyPhyML.ComponentAssembly);
}
else if (componentasm.ParentContainer.Kind == "ComponentAssembly")
{
topassembly = FindTopLevelAssembly(componentasm.ParentContainer as CyPhyML.ComponentAssembly);
}
return topassembly;
}
#region IMgaComponentEx Members
public MgaGateway MgaGateway { get; set; }
public GMEConsole GMEConsole { get; set; }
private CyPhyMetaLink.CyPhyMetaLinkAddon metalinkAddon;
Task<bool> ConnectToMetaLinkBridgeTask;
public void InvokeEx(MgaProject project, MgaFCO currentobj, MgaFCOs selectedobjs, int param)
{
if (!enabled)
{
return;
}
try
{
GMEConsole = GMEConsole.CreateFromProject(project);
MgaGateway = new MgaGateway(project);
ProjectDirectory = Path.GetDirectoryName(project.ProjectConnStr.Substring("MGA=".Length));
metalinkAddon = GetMetaLinkAddon(project);
if (metalinkAddon == null)
{
GMEConsole.Error.WriteLine("MetaLink: Unable to find CyPhyMetaLinkAddon. Was it disabled under Tools>Register Components?");
return;
}
ConnectToMetaLinkBridge(project, param);
string currentobjKind = null;
string currentobjName = null;
MgaFCO selectedCADModel = null;
string componentVersion = null;
Guid currentobjGuid = Guid.Empty;
if (currentobj != null)
{
MgaGateway.PerformInTransaction(delegate
{
currentobjKind = currentobj.Meta.Name;
currentobjName = currentobj.Name;
currentobjGuid = CyPhyMLClasses.DesignElement.Cast(currentobj).Guid;
if (currentobjKind == "CADModel")
{
selectedCADModel = currentobj;
currentobj = (MgaFCO)currentobj.ParentModel;
currentobjKind = currentobj.Meta.Name;
currentobjName = currentobj.Name;
currentobjGuid = CyPhyMLClasses.DesignElement.Cast(currentobj).Guid;
}
else
{
Func<MgaFCO, bool> isCADCreoModel = fco => fco.Meta.Name == "CADModel"
&& CyPhyMLClasses.CADModel.Cast(fco).Attributes.FileFormat == CyPhyMLClasses.CADModel.AttributesClass.FileFormat_enum.Creo;
selectedCADModel = (selectedobjs ?? (System.Collections.IEnumerable)(new List<MgaFCO>())).Cast<MgaFCO>()
.FirstOrDefault(isCADCreoModel);
if (selectedCADModel == null && currentobj.ObjType == GME.MGA.Meta.objtype_enum.OBJTYPE_MODEL)
{
// TODO: show dialog instead of picking one at random
selectedCADModel = ((MgaModel)currentobj).ChildFCOs.Cast<MgaFCO>().FirstOrDefault(isCADCreoModel);
}
}
if (currentobj != null && currentobj.Meta.Name == typeof(CyPhyMLClasses.Component).Name)
{
componentVersion = CyPhyMLClasses.Component.Cast(currentobj).Attributes.Version;
}
}, transactiontype_enum.TRANSACTION_NON_NESTED, abort: false);
}
if (currentobjKind != null && currentobjKind != "Component" && currentobjKind != "CADModel" && currentobjKind != "ComponentAssembly")
{
System.Windows.Forms.MessageBox.Show("Please open a Component or a Component Assembly");
return;
}
if (currentobjKind == "Component")
{
CyPhyML.Component component = CyPhyMLClasses.Component.Cast(currentobj);
if (selectedCADModel == null)
{
GMEConsole.Error.WriteLine("This component has no CADModels to edit.");
return;
}
bool connected = true;
ConnectToMetaLinkBridgeTask.Wait();
connected = ConnectToMetaLinkBridgeTask.Result;
if (connected)
{
LinkComponent(component, selectedCADModel);
}
return;
}
if (currentobjKind == null)
{
bool connected = true;
ConnectToMetaLinkBridgeTask.Wait();
connected = ConnectToMetaLinkBridgeTask.Result;
if (connected)
{
metalinkAddon.StartCreoEmpyMode();
}
return;
}
//GMEConsole.Out.WriteLine("Running CyPhySync interpreter...");
if (metalinkAddon.AssemblyID != null)
{
if (currentobjGuid == Guid.Parse(metalinkAddon.AssemblyID))
{
metalinkAddon.assemblySyncPaused = !metalinkAddon.assemblySyncPaused;
if (metalinkAddon.assemblySyncPaused)
{
GMEConsole.Info.WriteLine(String.Format("Pausing MetaLink for {0}", SecurityElement.Escape(currentobjName)));
}
else
{
GMEConsole.Info.WriteLine(String.Format("Resuming MetaLink for {0}", SecurityElement.Escape(currentobjName)));
if (metalinkAddon.assembliesToRestart.Count > 0)
{
MgaGateway.PerformInTransaction(delegate
{
// n.b. end of current transaction will trigger MetaLinkAddon RestartAssemblySync
});
}
}
}
else
{
GMEConsole.Warning.WriteLine("A different ComponentAssembly is already synced");
}
}
else
{
StartAssemblySync(project, currentobj, param);
}
//GMEConsole.Out.WriteLine("End of CyPhySync interpreter...");
}
catch (AggregateException e)
{
foreach (var exception in e.InnerExceptions)
{
GMEConsole.Error.WriteLine(exception.Message);
}
throw new ApplicationException(e.InnerException.Message, e.InnerException);
}
finally
{
MgaGateway = null;
project = null;
currentobj = null;
selectedobjs = null;
GMEConsole = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
private void LinkComponent(CyPhyML.Component component, MgaFCO selectedCADModel)
{
try
{
bool createNewComponent = false;
CyPhyMetaLink.CyPhyMetaLinkAddon.CreoOpenMode creoMode = CyPhyMetaLink.CyPhyMetaLinkAddon.CreoOpenMode.OPEN_EMPTY;
var createOrEdit = new CreateOrEditComponent();
createOrEdit.btnCreateNewComponent.Click += (o, e) =>
{
createNewComponent = true;
creoMode = CyPhyMetaLink.CyPhyMetaLinkAddon.CreoOpenMode.OPEN_COMPONENT;
createOrEdit.DialogResult = DialogResult.OK;
createOrEdit.Close();
};
createOrEdit.btnEditExistingComponent.Click += (o, e) =>
{
createNewComponent = false;
creoMode = CyPhyMetaLink.CyPhyMetaLinkAddon.CreoOpenMode.OPEN_COMPONENT;
createOrEdit.DialogResult = DialogResult.OK;
createOrEdit.Close();
};
createOrEdit.btnEmptyCreoOpen.Click += (o, e) =>
{
createNewComponent = false;
creoMode = CyPhyMetaLink.CyPhyMetaLinkAddon.CreoOpenMode.OPEN_EMPTY;
createOrEdit.DialogResult = DialogResult.OK;
createOrEdit.Close();
};
DialogResult result = createOrEdit.ShowDialog(new NativeWindowWrapper(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle));
if (result != DialogResult.Cancel)
{
if (creoMode == CyPhyMetaLink.CyPhyMetaLinkAddon.CreoOpenMode.OPEN_COMPONENT)
{
metalinkAddon.StartEditingComponent(component, selectedCADModel, createNewComponent);
}
else
{
metalinkAddon.StartCreoEmpyMode();
}
}
}
catch (IOException e) // e.g. manifest.project.json not found
{
GMEConsole.Error.WriteLine(e.ToString());
}
return;
}
public static string GetJavaInstallationPath()
{
foreach (var javaKey in new[] {
new { key = "SOFTWARE\\JavaSoft\\JRE", view = RegistryView.Registry64 },
new { key = "SOFTWARE\\JavaSoft\\JRE", view = RegistryView.Registry32 },
new { key = "SOFTWARE\\JavaSoft\\Java Runtime Environment", view = RegistryView.Registry64 },
new { key = "SOFTWARE\\JavaSoft\\Java Runtime Environment", view = RegistryView.Registry32 },
new { key = "SOFTWARE\\JavaSoft\\Java Development Kit", view = RegistryView.Registry64 },
new { key = "SOFTWARE\\JavaSoft\\Java Development Kit", view = RegistryView.Registry32 },
new { key = "SOFTWARE\\JavaSoft\\JDK", view = RegistryView.Registry64 },
new { key = "SOFTWARE\\JavaSoft\\JDK", view = RegistryView.Registry32 },
})
{
try
{
using (var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, javaKey.view).OpenSubKey(javaKey.key))
{
if (baseKey == null)
{
continue;
}
object currentVersion = baseKey.GetValue("CurrentVersion");
if (currentVersion == null)
{
continue;
}
using (var homeKey = baseKey.OpenSubKey(currentVersion.ToString()))
return homeKey.GetValue("JavaHome").ToString();
}
}
catch (Exception)
{
// no 64-bit Java was found. Will try 64-bit
}
}
return null;
}
public Task<bool> ConnectToMetaLinkBridge(MgaProject project, int param)
{
ConnectToMetaLinkBridgeTask = Task.Run(() => _ConnectToMetaLinkBridge(project, param));
return ConnectToMetaLinkBridgeTask;
}
private async Task<bool> _ConnectToMetaLinkBridge(MgaProject project, int param)
{
bool connected = false;
if (metalinkAddon == null)
{
metalinkAddon = GetMetaLinkAddon(project);
}
bool firstTimeConnect = metalinkAddon.bridgeClient.ConnectionWasRequested == false;
if (await metalinkAddon.EstablishConnection())
{
connected = true;
}
else
{
//GMEConsole.Error.WriteLine("Failed to establish connection to MetaLink Bridge.");
if (param != 128)
{
string metaLinkPath = Path.Combine(META.VersionInfo.MetaPath, @"src\MetaLink\meta-bridge\java-server\target\metalink-java-server-1.1.0.jar"); // dev machine
if (!File.Exists(metaLinkPath))
{
metaLinkPath = Path.Combine(META.VersionInfo.MetaPath, @"bin\metalink-java-server-1.1.0.jar"); // installed machine
}
if (File.Exists(metaLinkPath))
{
string java_path = GetJavaInstallationPath();
if (java_path == null)
{
throw new ApplicationException("Could not find java.exe. Is Java installed?");
}
string java_exe = Path.Combine(java_path, "bin\\java.exe");
//DialogResult res = MessageBox.Show("Could not connect to MetaLink.\n\nDo you want to start MetaLink?", "MetaLink", MessageBoxButtons.YesNo);
//if (res == DialogResult.Yes)
{
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = java_exe,
// Arguments = String.Format("-jar \"{0}\" -r \"{1}\"", metaLinkPath, Path.GetTempFileName()),
Arguments = String.Format("-jar \"{0}\"", metaLinkPath),
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
};
#if DEBUG
// startInfo.Arguments += String.Format(" -r \"{0}\"", Path.Combine(Path.GetTempPath, "metalink_recordedmessages.mlp");
#endif
Process metalink = new Process();
metalink.StartInfo = startInfo;
string logPath = Path.Combine(ProjectDirectory, "log", "MetaLinkBridge_" + Process.GetCurrentProcess().Id.ToString() + ".log");
Directory.CreateDirectory(Path.GetDirectoryName(logPath));
var log = new StreamWriter(logPath);
int streamsClosed = 0;
TaskCompletionSource<bool> bridgeListening = new TaskCompletionSource<bool>();
DataReceivedEventHandler handler = (sender, e) =>
{
lock (log)
{
if (e.Data == null)
{
streamsClosed += 1;
if (streamsClosed == 2)
{
log.Close();
log.Dispose();
}
return;
}
if (bridgeListening.Task.IsCompleted == false && e.Data.Contains("AssemblyDesignBridgeServer started and listening"))
{
bridgeListening.SetResult(true);
}
log.WriteLine(e.Data);
// log.Flush();
}
};
metalink.OutputDataReceived += handler;
metalink.ErrorDataReceived += handler;
metalink.Start();
metalink.BeginErrorReadLine();
metalink.BeginOutputReadLine();
if (Task.WaitAny(new Task[] { bridgeListening.Task }, 20 * 1000) == -1)
{
// throw new TimeoutException();
}
// n.b. ensure that even if GME is killed or crashes, MetaLinkBridge will exit
// this is important because we are reading its output, and it may block when we are not around to read it
IntPtr job = JobObjectPinvoke.AssignProcessToKillOnCloseJob(metalink);
for (int i = 0; i < 100; i++)
{
if (await metalinkAddon.EstablishConnection())
{
connected = true;
break;
}
await Task.Delay(100);
}
if (!connected)
{
if (GMEConsole != null)
{
GMEConsole.Error.WriteLine("Failed to start MetaLink Bridge.");
}
if (metalink.HasExited)
{
metalink.Kill();
}
metalink.Dispose();
JobObjectPinvoke.CloseHandle(job);
}
else
{
metalinkAddon._metalinkBridge = metalink;
metalinkAddon._metalinkBridgeJob = job;
}
}
}
else
{
if (GMEConsole != null)
{
GMEConsole.Error.WriteLine("Meta-Link Bridge executable is missing. Please check that Meta-Link Bridge is installed.");
}
}
}
}
if (connected && firstTimeConnect)
{
if (GMEConsole != null)
{
GMEConsole.Info.WriteLine("Established connection to MetaLink Bridge.");
}
}
return connected;
}
public void StartAssemblySync(MgaProject project, MgaFCO currentobj, int param)
{
// FIXME: possible race between sending MetaLink Bridge INTEREST and it registering it, and Creo sending its INTEREST
// n.b. the tests call StartAssemblySync directly
metalinkAddon.assemblySyncPaused = false;
ProjectDirectory = Path.GetDirectoryName(project.ProjectConnStr.Substring("MGA=".Length));
if (ConnectToMetaLinkBridgeTask == null)
{
ConnectToMetaLinkBridge(project, param);
}
metalinkAddon.Enable(true);
CyPhyML.ComponentAssembly topasm = null;
MgaGateway.PerformInTransaction(delegate
{
topasm = Run(project, currentobj, param);
if (topasm != null)
{
metalinkAddon.AssemblyID = topasm.Guid.ToString();
}
});
ConnectToMetaLinkBridgeTask.Wait();
if (ConnectToMetaLinkBridgeTask.Result == false)
{
return;
}
if (topasm == null)
{
metalinkAddon.AssemblyID = null;
GMEConsole.Error.WriteLine("No top level assembly can be found. Open an assembly and try again.");
return;
}
try
{
metalinkAddon.CallCyPhy2CADWithTransaction(project,
(topasm.Impl as MgaFCO),
param);
}
catch (Exception e)
{
metalinkAddon.syncedComponents.Remove(metalinkAddon.AssemblyID);
// FIXME: propagateAddon.LastStartedInstance.Remove
metalinkAddon.AssemblyID = null;
GMEConsole.Error.Write(e.Message);
}
}
private static CyPhyMetaLink.CyPhyMetaLinkAddon GetMetaLinkAddon(MgaProject project)
{
CyPhyMetaLink.CyPhyMetaLinkAddon cyPhyMLSyncComponent = null;
MgaComponents mgaComponents = project.AddOnComponents;
foreach (IMgaComponent iMgaComponent in mgaComponents)
{
if (iMgaComponent.ComponentName == "CyPhyMetaLinkAddon")
{
cyPhyMLSyncComponent = (CyPhyMetaLink.CyPhyMetaLinkAddon)iMgaComponent;
break;
}
}
return cyPhyMLSyncComponent;
}
private ComponentStartMode Convert(int param)
{
switch (param)
{
case (int)ComponentStartMode.GME_BGCONTEXT_START:
return ComponentStartMode.GME_BGCONTEXT_START;
case (int)ComponentStartMode.GME_BROWSER_START:
return ComponentStartMode.GME_BROWSER_START;
case (int)ComponentStartMode.GME_CONTEXT_START:
return ComponentStartMode.GME_CONTEXT_START;
case (int)ComponentStartMode.GME_EMBEDDED_START:
return ComponentStartMode.GME_EMBEDDED_START;
case (int)ComponentStartMode.GME_ICON_START:
return ComponentStartMode.GME_ICON_START;
case (int)ComponentStartMode.GME_MAIN_START:
return ComponentStartMode.GME_MAIN_START;
case (int)ComponentStartMode.GME_MENU_START:
return ComponentStartMode.GME_MENU_START;
case (int)ComponentStartMode.GME_SILENT_MODE:
return ComponentStartMode.GME_SILENT_MODE;
}
return ComponentStartMode.GME_SILENT_MODE;
}
#region Component Information
public string ComponentName
{
get { return GetType().Name; }
}
public string ComponentProgID
{
get
{
return ComponentConfig_Intf.progID;
}
}
public componenttype_enum ComponentType
{
get { return ComponentConfig_Intf.componentType; }
}
public string Paradigm
{
get { return ComponentConfig_Intf.paradigmName; }
}
#endregion
#region Enabling
bool enabled = true;
public void Enable(bool newval)
{
enabled = newval;
}
#endregion
#region Interactive Mode
protected bool interactiveMode = true;
public bool InteractiveMode
{
get
{
return interactiveMode;
}
set
{
interactiveMode = value;
}
}
#endregion
#region Custom Parameters
SortedDictionary<string, object> componentParameters = null;
public object get_ComponentParameter(string Name)
{
if (Name == "type")
{
return "csharp";
}
if (Name == "path")
{
return GetType().Assembly.Location;
}
if (Name == "fullname")
{
return GetType().FullName;
}
object value;
if (componentParameters != null && componentParameters.TryGetValue(Name, out value))
{
return value;
}
return null;
}
public void set_ComponentParameter(string Name, object pVal)
{
if (componentParameters == null)
{
componentParameters = new SortedDictionary<string, object>();
}
componentParameters[Name] = pVal;
}
#endregion
#region Unused Methods
// Old interface, it is never called for MgaComponentEx interfaces
public void Invoke(MgaProject Project, MgaFCOs selectedobjs, int param)
{
throw new NotImplementedException();
}
// Not used by GME
public void ObjectsInvokeEx(MgaProject Project, MgaObject currentobj, MgaObjects selectedobjs, int param)
{
throw new NotImplementedException();
}
#endregion
#endregion
#region IMgaVersionInfo Members
public GMEInterfaceVersion_enum version
{
get { return GMEInterfaceVersion_enum.GMEInterfaceVersion_Current; }
}
#endregion
#region Registration Helpers
[ComRegisterFunctionAttribute]
public static void GMERegister(Type t)
{
Registrar.RegisterInterpreter();
}
[ComUnregisterFunctionAttribute]
public static void GMEUnRegister(Type t)
{
Registrar.UnregisterInterpreter();
}
#endregion
private CyPhyML.ComponentAssembly Run(MgaProject project,
MgaFCO currentobj,
int param)
{
CyPhyML.ComponentAssembly topasm = null;
if (currentobj == null || currentobj.MetaBase.Name != "ComponentAssembly")
{
MessageBox.Show("Please open an assembly!");
return topasm;
}
// [1] Find top-level assembly
// [2] Create folder name
// [3] MainFrame
// [4] CyPhy2CAD_CSharp
// [1]
topasm = FindTopLevelAssembly(CyPhyMLClasses.ComponentAssembly.Cast(currentobj));
if (topasm == null)
{
return topasm;
}
#if DEBUG
if (topasm != null)
{
GMEConsole.Out.WriteLine("TopAsm:" + topasm.Name);
}
#endif
string workingDir = "MetaLink_" + DateTime.Now.ToString("MMddHHmmss");
CyPhyMetaLink.SyncedComponentData cdata = new CyPhyMetaLink.SyncedComponentData()
{
Type = SyncedComponentData.EditType.Design,
WorkingDir = Path.Combine(ProjectDirectory, "metalink", workingDir),
Id = topasm.Guid.ToString(),
InstanceId = (CyPhyMetaLinkAddon.IdCounter++).ToString()
};
if (!Directory.Exists(cdata.WorkingDir))
{
Directory.CreateDirectory(cdata.WorkingDir);
}
// [3]
DialogResult ok = DialogResult.Cancel;
if (param != 128)
{
using (MainForm mf = new MainForm(cdata.WorkingDir))
{
ok = mf.ShowDialog();
cdata.AuxDir = mf.AuxiliaryDir;
if (ok != DialogResult.OK)
{
return null;
}
}
}
metalinkAddon.syncedComponents.Add(cdata.Id, cdata);
return topasm;
}
string ProjectDirectory;
}
}