public void Import(IEnumerable <string> lbrFiles) { project.Create("MGA=" + Path.GetFullPath(Path.Combine(".", "ComponentCreator", "Components")) + ".mga", "CyPhyML"); IMgaComponent signalBlocksAddon = (IMgaComponent)Activator.CreateInstance(Type.GetTypeFromProgID("MGA.Addon.CyPhySignalBlocksAddOn")); signalBlocksAddon.Initialize(project); project.Notify(globalevent_enum.GLOBALEVENT_OPEN_PROJECT_FINISHED); System.Windows.Forms.Application.DoEvents(); // let CyPhySignalBlocksAddOn create libs project.BeginTransactionInNewTerr(transactiontype_enum.TRANSACTION_NON_NESTED); try { MgaMetaFolder componentsMeta = (MgaMetaFolder)project.RootMeta.RootFolder.DefinedFolderByName["Components", false]; var components = project.RootFolder.CreateFolder(componentsMeta); components.Name = componentsMeta.Name; } finally { project.CommitTransaction(); } foreach (string eagleFilePath in lbrFiles) { //Console.WriteLine(eagleFilePath); //Console.WriteLine(string.Join(" ", CyPhyComponentAuthoring.Modules.EDAModelImport.GetDevicesInEagleModel(eagleFilePath).ToArray())); foreach (string deviceName in CyPhyComponentAuthoring.Modules.EDAModelImport.GetDevicesInEagleModel(eagleFilePath)) { try { CreateNewComponentMga(eagleFilePath, deviceName); Console.WriteLine("Imported {0} {1}", eagleFilePath, deviceName); } catch (Exception e) { MessageBox.Show("Import failed: " + e.ToString()); } } } project.Save("", true); project.Close(true); }
private void AttachLibrary(LibraryInfo libraryInfo) { string mgaPath = metaPath + "\\" + libraryInfo.MgaName + ".mga"; if ((project.ProjectStatus & PROJECT_STATUS_OPEN) == PROJECT_STATUS_OPEN) { if (!File.Exists(mgaPath)) { GMEConsole.Error.WriteLine("Path '" + mgaPath + "' does not exist. Cannot attach " + libraryInfo.MgaName); return; } project.Notify(globalevent_enum.APPEVENT_LIB_ATTACH_BEGIN); try { project.BeginTransaction(project.ActiveTerritory); IMgaFolder oldLibFolder = project.RootFolder.ChildFolders.Cast <IMgaFolder>() .Where(x => string.IsNullOrWhiteSpace(x.LibraryName) == false && (x.Name.Contains(libraryInfo.DisplayName) || x.Name.Contains(libraryInfo.DisplayName))).FirstOrDefault(); bool needAttach; if (oldLibFolder == null) { needAttach = true; if (libraryPaths.Contains(Path.GetFullPath(project.ProjectConnStr.Substring("MGA=".Length)))) { // Don't attach libraries to themselves needAttach = false; } } else { DateTime oldModTime; long loldModTime; if (long.TryParse(oldLibFolder.RegistryValue["modtime"], out loldModTime)) { oldModTime = DateTime.FromFileTimeUtc(loldModTime); } else { oldModTime = DateTime.MinValue; } needAttach = File.GetLastWriteTimeUtc(mgaPath).CompareTo(oldModTime) > 0; if (!needAttach) { GMEConsole.Info.WriteLine("Library is up-to-date: embedded library modified " + oldModTime.ToString() + ", " + libraryInfo.MgaName + " modified " + File.GetLastWriteTimeUtc(mgaPath).ToString()); } } if (needAttach) { MgaProject proj = (MgaProject)Activator.CreateInstance(Type.GetTypeFromProgID("Mga.MgaProject")); int mgaVersion; string paradigmName; string paradigmVersion; object paradigmGuid; bool readOnly; proj.QueryProjectInfo("MGA=" + mgaPath, out mgaVersion, out paradigmName, out paradigmVersion, out paradigmGuid, out readOnly); Guid guidP1 = ConvertToGUID(paradigmGuid); Guid guidP2 = ConvertToGUID(project.RootMeta.GUID); bool guidsEqual = guidP1.Equals(guidP2); if (paradigmName != project.MetaName || !guidsEqual) { GMEConsole.Info.WriteLine("Skipping refresh of " + libraryInfo.DisplayName + " because it uses a different metamodel version than the current project."); project.AbortTransaction(); // not true, but don't try again libraryInfo.attachedLibrary = true; return; } // GMEConsole.Info.WriteLine("Attaching library " + mgaPath); RootFolder newLibFolder = Common.Classes.RootFolder.GetRootFolder(project).AttachLibrary("MGA=" + mgaPath); DateTime modtime = File.GetLastWriteTimeUtc(mgaPath); ((newLibFolder as ISIS.GME.Common.Classes.RootFolder).Impl as GME.MGA.IMgaFolder).RegistryValue["modtime"] = modtime.ToFileTimeUtc().ToString(); if (oldLibFolder != null) { ReferenceSwitcher.Switcher sw = new ReferenceSwitcher.Switcher(oldLibFolder, newLibFolder.Impl, null); sw.UpdateSublibrary(); oldLibFolder.DestroyObject(); } ((newLibFolder as ISIS.GME.Common.Classes.RootFolder).Impl as GME.MGA.IMgaFolder).LibraryName = libraryInfo.DisplayName; GMEConsole.Info.WriteLine((oldLibFolder == null ? "Attached " : "Refreshed ") + libraryInfo.MgaName + ".mga library."); } project.CommitTransaction(); } catch (Exception e) { GMEConsole.Error.WriteLine("Error refreshing library: " + SecurityElement.Escape(e.Message)); project.AbortTransaction(); } finally { project.Notify(globalevent_enum.APPEVENT_LIB_ATTACH_END); } libraryInfo.attachedLibrary = true; } }