public CyPhy.EDAModel BuildCyPhyEDAModel(avm.schematic.eda.EDAModel edaModel, CyPhy.Component comp) { var rf = CyPhyClasses.RootFolder.GetRootFolder(CurrentProj); var builder = new AVM2CyPhyML.CyPhyMLComponentBuilder(rf); CyPhy.EDAModel cyPhyEDAModel = builder.process(edaModel, comp); #region layout // find the largest current Y value so our new elements are added below the existing design elements greatest_current_y = 0; foreach (var child in GetCurrentDesignElement().AllChildren) { foreach (MgaPart item in (child.Impl as MgaFCO).Parts) { int read_x, read_y; string read_str; item.GetGmeAttrs(out read_str, out read_x, out read_y); greatest_current_y = (read_y > greatest_current_y) ? read_y : greatest_current_y; } } // layout CAD model to the "south" of existing elements foreach (MgaPart item in (cyPhyEDAModel.Impl as MgaFCO).Parts) { item.SetGmeAttrs(null, MODEL_START_X, greatest_current_y + MODEL_START_Y); } #endregion ExtendDevicePorts(cyPhyEDAModel); return(cyPhyEDAModel); }
public void ImportCyberModel(string Cyberpath = null) { string CyberFilename = ""; this.Logger = new CyPhyGUIs.GMELogger(CurrentProj, this.GetType().Name); // - Display a dialog box to let the user choose their Cyber model file bool Cyber_file_chosen = false; //bool test_copy_and_path_only = false; if (string.IsNullOrWhiteSpace(Cyberpath)) { Cyber_file_chosen = get_Cyber_file(out CyberFilename); } else { //test_copy_and_path_only = true; CyberFilename = Cyberpath; if (File.Exists(CyberFilename)) { Cyber_file_chosen = true; } else { this.Logger.WriteError("Invalid Cyber file path passed in: " + Cyberpath); } } // - Run the extractor on the Creo model file #region Run the Extractor List<string> componentList = new List<string>(); IMgaProject project = (IMgaProject)Activator.CreateInstance(Type.GetTypeFromProgID("Mga.MgaProject")); string compName; project.OpenEx("MGA=" + CyberFilename, "CyberComposition", null); try { project.BeginTransactionInNewTerr(); IMgaFCO currentObj; try { IMgaFolder currentFolder = (IMgaFolder)project.RootFolder; IMgaFolders cFolders = currentFolder.ChildFolders; foreach (IMgaFolder f in cFolders) { if (f.MetaFolder.Name == "Components") { IMgaFCOs objects = f.ChildFCOs; foreach (IMgaFCO o in objects) { componentList.Add("/" + f.Name + "/" + o.Name); } } } } finally { project.CommitTransaction(); } // Adapted from ModelicaImporter.cs string result = ""; using (CyberComponentPicker cyberpicker = new CyberComponentPicker(componentList)) { var dialogResult = cyberpicker.ShowDialog(); if (dialogResult != System.Windows.Forms.DialogResult.OK) { this.Logger.WriteInfo("Modelica import was cancelled by the user."); return; } result = cyberpicker.compResult; } var halves = result.Split('/'); compName = halves[1]; project.BeginTransactionInNewTerr(); try { currentObj = (IMgaFCO)project.RootFolder.ObjectByPath[result]; } finally { project.CommitTransaction(); } IMgaComponentEx comp = (IMgaComponentEx)Activator.CreateInstance(Type.GetTypeFromProgID("MGA.Interpreter.CyberComponentExporter")); comp.Initialize((MgaProject)project); comp.InvokeEx((MgaProject)project, (MgaFCO)currentObj, null, 128); project.Save(); } finally { project.Close(true); } #endregion // - Use a function from CyPhy2ComponentModel to convert the extractor's XML format into a CyPhy model fragment #region Convert_to_XML // used in creating the resource object below CyPhy.CyberModel ProcessedCyberModel = null; if (true) { this.Logger.WriteDebug("About to call DeserializeAvmComponentXml..."); StreamReader streamReader = new StreamReader(compName + ".component.acm"); avm.Component ac_import = CyPhyComponentImporter.CyPhyComponentImporterInterpreter.DeserializeAvmComponentXml(streamReader); streamReader.Close(); this.Logger.WriteDebug("... finished DeserializeAvmComponentXml call."); foreach (avm.cyber.CyberModel Cybermodel in ac_import.DomainModel.Where(dm => dm is avm.cyber.CyberModel) .Cast<avm.cyber.CyberModel>()) { var rf = CyPhyClasses.RootFolder.GetRootFolder(CurrentProj); Dictionary<string, CyPhy.Component> avmidComponentMap = new Dictionary<string, CyPhy.Component>(); AVM2CyPhyML.CyPhyMLComponentBuilder newComponent = new AVM2CyPhyML.CyPhyMLComponentBuilder(rf); ProcessedCyberModel = newComponent.process(Cybermodel, GetCurrentComp()); ProcessedCyberModel.Name = Path.GetFileNameWithoutExtension(CyberFilename); } // find the largest current Y value so our new elements are added below the existing design elements foreach (var child in GetCurrentComp().AllChildren) { foreach (MgaPart item in (child.Impl as MgaFCO).Parts) { int read_x, read_y; string read_str; item.GetGmeAttrs(out read_str, out read_x, out read_y); greatest_current_y = (read_y > greatest_current_y) ? read_y : greatest_current_y; } } // layout Cyber model to the "south" of existing elements foreach (MgaPart item in (ProcessedCyberModel.Impl as MgaFCO).Parts) { item.SetGmeAttrs(null, Cyber_MODEL_START_X, greatest_current_y + Cyber_MODEL_START_Y); } // Extend it's properties out to the component level. this.CyberModuleImportExtension(ProcessedCyberModel); } #endregion // - Copy the Cyber Model files into the component's backend folder // - Note: The solution includes a function that can find this folder using the project.manifest.json file. // - For nice organization, create them in a subfolder called "Cyber" // create avmproj #region Copy files to backend folder // used in creating the resource object below string PathforComp = null; var importedCyberFiles = new List<String>(); if (true) { try { // create the destination path PathforComp = GetCurrentComp().GetDirectoryPath(ComponentLibraryManager.PathConvention.ABSOLUTE); string finalPathName = Path.Combine(PathforComp, "Cyber"); Directory.CreateDirectory(finalPathName); // determine if one part file or all part and assembly files need to be copied string cpsrcfile = System.IO.Path.GetFileName(CyberFilename); // copy the selected file string CyberFileCopyPath = System.IO.Path.Combine(finalPathName, cpsrcfile); System.IO.File.Copy(CyberFilename, CyberFileCopyPath, true); // Set "primary" file as the first in the list. importedCyberFiles.Add(Path.Combine("Cyber", Path.GetFileName(CyberFileCopyPath))); if (true) { // get a string of the XML contents this.Logger.WriteDebug("About to read contents of XML file using class XmlDocument: " + compName + ".component.acm"); XmlDocument doc = new XmlDocument(); doc.Load(compName + ".component.acm"); string xmlcontents = doc.InnerXml; // mine down to the Resource dependencies using (XmlReader reader = XmlReader.Create(new StringReader(xmlcontents))) { // iterate through each file listed in the resourcedependency section while (reader.ReadToFollowing("ResourceDependency") == true) { string res_name = reader.GetAttribute("Name"); string res_path = reader.GetAttribute("Path"); this.Logger.WriteDebug("Copying this file: " + res_path + "\\" + res_name); // Cyber files end in .1 .2 etc. Pick the latest ones var allFiles = Directory.EnumerateFiles(Path.GetDirectoryName(res_path), "*mga." /*n.b. literal dot*/ + "*") .Select(Path.GetFileName) .Select(filename => new { basename = filename.Substring(0, filename.LastIndexOf('.')), version = filename.Substring(filename.LastIndexOf('.') + 1) }) .Where(p => { int val; return Int32.TryParse(p.version, out val); }) .OrderByDescending(p => Int32.Parse(p.version)) .ToArray(); foreach (var basename in allFiles.Select(p => p.basename).Distinct()) { var latest = allFiles.Where(p => p.basename == basename).FirstOrDefault(); if (latest != null) { string latestFilename = latest.basename + "." + latest.version; // Need to limit this down to just the filename in question // The XML file changes the name to all caps, so compare apples to apples if (latestFilename.ToUpper().StartsWith(res_name.ToUpper())) { string destpathandname = Path.Combine(finalPathName, latestFilename); if (!importedCyberFiles.Contains(Path.Combine("Cyber", Path.GetFileName(destpathandname)))) { importedCyberFiles.Add(Path.Combine("Cyber", Path.GetFileName(destpathandname))); } File.Copy(Path.Combine(Path.GetDirectoryName(res_path), latestFilename), destpathandname, true); break; } } } } } } } catch (Exception err_create_proj) { this.Logger.WriteError("Error creating AVM project: " + err_create_proj.Message, " - Extraction Failed. Possible ComponentManagement issue."); //cleanup(tempXMLfile, true); return; } } #endregion // - Create Resource objects in the CyPhy Component model that point to these Cyber Model files // - Note: The "paths" of these should be the relative path from that component's root folder if (true) { foreach (var CyberFile in importedCyberFiles) { CyPhy.Resource ResourceObj = CyPhyClasses.Resource.Create(GetCurrentComp()); ResourceObj.Attributes.ID = Guid.NewGuid().ToString("B"); ResourceObj.Attributes.Path = CyberFile; ResourceObj.Attributes.Notes = "Cyber Model Import tool added this resource object for the imported Cyber file"; ResourceObj.Name = Path.GetFileName(CyberFile); // layout Resource just to the side of the Cyber model foreach (MgaPart item in (ResourceObj.Impl as MgaFCO).Parts) { item.SetGmeAttrs(null, RESOURCE_START_X, greatest_current_y + RESOURCE_START_Y); } // The "primary" Cyber model is the first one -- associate it with the CyPhy CyberModel object if (importedCyberFiles.IndexOf(CyberFile) == 0) { // - Create a UsesResource association between the CyPhy CyberModel object and the Resource object that represents the top-level Creo Model file. CyPhyClasses.UsesResource.Connect(ResourceObj, ProcessedCyberModel, null, null, GetCurrentComp()); } } } // throw in an ACM file for the current state of the component. if (true) { var exporter = new CyPhyComponentExporter.CyPhyComponentExporterInterpreter(); String acmPath = Path.Combine(PathforComp,GetCurrentComp().Name + ".component.acm"); CyPhyComponentExporterInterpreter.ExportToFile(GetCurrentComp(), Path.GetDirectoryName(acmPath)); } // Clean up //cleanup(tempXMLfile, true); }
private void ProcessAVMComponentUpdate(string componentId, string component_xml) { try { File.WriteAllText(Path.Combine(Path.GetTempPath(), "CyPhyMLPropagate.log"), component_xml); } catch (IOException) { } addon.Project.BeginTransactionInNewTerr(); try { CyPhyML.Component cyPhyComponent = CyphyMetaLinkUtils.GetComponentByAvmId(addon.Project, componentId); if (!cyPhyComponent.Children.CADModelCollection.Any()) throw new Exception(String.Format("No CADModel found inside component under update: {1}" + cyPhyComponent.ToHyperLink())); string absModelPath = Path.Combine(GetProjectDir(), Path.GetDirectoryName(GetCadModelPath(cyPhyComponent))); XmlDocument doc = new XmlDocument(); doc.LoadXml(component_xml); XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable); manager.AddNamespace("avm", "avm"); manager.AddNamespace("cad", "cad"); manager.AddNamespace("modelica", "modelica"); manager.AddNamespace("cyber", "cyber"); manager.AddNamespace("manufacturing", "manufacturing"); List<string> notcopied = new List<string>(); string cadmodelpath = Path.GetDirectoryName(GetCadModelPath(cyPhyComponent)); CopyResources(cadmodelpath, doc, manager, notcopied, true); avm.Component component = CyPhyComponentImporter.CyPhyComponentImporterInterpreter.DeserializeAvmComponentXml(new StringReader(doc.OuterXml)); avm.Component oldComponent = CyPhy2ComponentModel.Convert.CyPhyML2AVMComponent(cyPhyComponent); // find replace corresponding DomainModels in oldComponent, and find the corresponding CyPhy CADModels Dictionary<avm.DomainModel, CyPhyML.CADModel> avmDomainModelToCyPhyCADModel = new Dictionary<avm.DomainModel, CyPhyML.CADModel>(); var cadmodel = cyPhyComponent.Children.CADModelCollection.Where(cadModel => cadModel.Attributes.FileFormat == CyPhyMLClasses.CADModel.AttributesClass.FileFormat_enum.Creo).First(); // FIXME possible out-of-bounds access avmDomainModelToCyPhyCADModel.Add(component.DomainModel[0], cadmodel); /*foreach (var cadModel in ) { string cadModelRelativePath; if (cadModel.TryGetResourcePath(out cadModelRelativePath)) { foreach (var domainModel in component.DomainModel) { var dependency = oldComponent.ResourceDependency.Where(rd => rd.ID == domainModel.UsesResource).FirstOrDefault(); if (dependency != null) { if (CyphyMetaLinkUtils.CleanPath(dependency.Path) == CyphyMetaLinkUtils.CleanPath(cadModelRelativePath)) { avmDomainModelToCyPhyCADModel[domainModel] = cadModel; } foreach (var oldCadModel in oldComponent.DomainModel.Where(dm => dm.UsesResource == domainModel.UsesResource).ToList()) { oldComponent.DomainModel.Remove(oldCadModel); oldComponent.DomainModel.Add(domainModel); // TODO: need to hook anything up? } } else { GMEConsole.Warning.WriteLine(string.Format("Could not find ResourceDependency '{0}' for DomainModel", domainModel.UsesResource)); } } } }*/ foreach (var models in avmDomainModelToCyPhyCADModel) { var builder = new AVM2CyPhyML.CyPhyMLComponentBuilder(CyPhyMLClasses.RootFolder.GetRootFolder(models.Value.Impl.Project)); var newCADModel = builder.process((avm.cad.CADModel)models.Key, cyPhyComponent); newCADModel.Attributes.FileType = models.Value.Attributes.FileType; // META-1680 this is not set by the Component Importer. Assume it hasn't changed // wire new Datums to CyPhy model, delete old CADModel Func<MgaFCO, string> mapping = x => { if (datumKinds.Contains(x.MetaBase.Name)) { return CyPhyMLClasses.CADDatum.Cast(x).Attributes.DatumName.ToUpper() + "__ __" + x.Meta.Name; } return x.Name.ToUpper() + "__ __" + x.Meta.Name; }; Dictionary<string, MgaFCO> nameMap = ((MgaModel)newCADModel.Impl).ChildFCOs.Cast<MgaFCO>().ToDictionary(mapping, fco => fco); foreach (var connPoint in models.Value.Impl.ChildObjects.Cast<MgaFCO>() .SelectMany(child => child.PartOfConns.Cast<MgaConnPoint>().Where(cp => cp.Owner.ParentModel.ID == cyPhyComponent.ID))) { MgaFCO mappedFCO; if (nameMap.TryGetValue(mapping.Invoke(connPoint.Target), out mappedFCO)) { if (connPoint.ConnRole == "dst") { ((MgaSimpleConnection)connPoint.Owner).SetDst(connPoint.References, mappedFCO); } else { ((MgaSimpleConnection)connPoint.Owner).SetSrc(connPoint.References, mappedFCO); } } else { GMEConsole.Warning.WriteLine("Could not find connection target " + connPoint.Target.Meta.Name + " '" + connPoint.Target.Name + "'. Existing connection will be removed from model."); connPoint.Owner.DestroyObject(); } } foreach (MgaConnPoint connPoint in ((MgaFCO)models.Value.Impl).PartOfConns) { if (connPoint.ConnRole == "dst") { ((MgaSimpleConnection)connPoint.Owner).SetDst(connPoint.References, (MgaFCO)newCADModel.Impl); } else { ((MgaSimpleConnection)connPoint.Owner).SetSrc(connPoint.References, (MgaFCO)newCADModel.Impl); } } foreach (MgaPart part in ((MgaFCO)newCADModel.Impl).Parts) { string icon; int x, y; ((MgaFCO)models.Value.Impl).PartByMetaPart[part.Meta].GetGmeAttrs(out icon, out x, out y); part.SetGmeAttrs(icon, x, y); } List<string> existing = new List<string>(); // Refresh resources List<CyPhyML.Resource> removeList = new List<CyPhyML.Resource>(); foreach (CyPhyML.Resource res in cyPhyComponent.Children.ResourceCollection) { if (CyphyMetaLinkUtils.IsCADResource(res)) { if (!notcopied.Where(s => res.Attributes.Path.ToLower().EndsWith(s.ToLower())).Any()) { removeList.Add(res); } else { existing.Add(res.Attributes.Path); } } } foreach (CyPhyML.Resource res in removeList) { res.Delete(); } foreach (var res in component.ResourceDependency) { if (!existing.Where(s => s.ToLower().EndsWith(res.Name.ToLower())).Any()) { CyPhyML.Resource newRes = CyPhyMLClasses.Resource.Create(cyPhyComponent); newRes.Attributes.ID = res.ID; newRes.Name = res.Name; string cmpath = cadmodelpath.Substring(cyPhyComponent.GetDirectoryPath(ComponentLibraryManager.PathConvention.ABSOLUTE).Length); cmpath = cmpath.Trim(new char[] { '\\', '/' }); newRes.Attributes.Path = Path.Combine(cmpath, res.Name); } } CyPhyML.Resource usesRes = null; try { usesRes = cyPhyComponent.Children.ResourceCollection.Where(res => res.Attributes.ID.Equals(models.Key.UsesResource)).First(); if (!usesRes.SrcConnections.UsesResourceCollection.Any()) { CyPhyMLClasses.UsesResource.Connect(newCADModel, usesRes, null, null, cyPhyComponent); } } catch (Exception) { GMEConsole.Warning.WriteLine(String.Format("Unable to set main resource for CADModel: {0}", newCADModel.ToHyperLink())); } try { string mainResourcePath = component.ResourceDependency.Where(resdep => resdep.ID == component.DomainModel[0].UsesResource).First().Path; // Handle manufacturing files string manufacturingPath = Path.Combine(Path.GetDirectoryName(mainResourcePath), "..", "Manufacturing"); RefreshManufacturingResources(cyPhyComponent, manufacturingPath); } catch (Exception) { GMEConsole.Warning.WriteLine(String.Format("Error during adding manufacturing resources to Component: {0}", cyPhyComponent.ToHyperLink())); } GMEConsole.Out.WriteLine(String.Format("Updated CADModel definition for <a href=\"mga:{0}\">{1}</a>", newCADModel.ID, SecurityElement.Escape(newCADModel.ParentContainer.Name + "/" + newCADModel.Name))); models.Value.Delete(); } CyPhy2ComponentModel.CyPhyComponentAutoLayout.LayoutComponent(cyPhyComponent); string acmPath; if (ComponentLibraryManager.TryGetOriginalACMFilePath(cyPhyComponent, out acmPath, ComponentLibraryManager.PathConvention.ABSOLUTE)) { avm.Component newComponent = CyPhy2ComponentModel.Convert.CyPhyML2AVMComponent(cyPhyComponent); CyPhyComponentExporter.CyPhyComponentExporterInterpreter.SerializeAvmComponent(oldComponent, acmPath); } } finally { addon.Project.CommitTransaction(); } }
public void ImportCyberModel(string Cyberpath = null) { string CyberFilename = ""; this.Logger = new CyPhyGUIs.GMELogger(CurrentProj, this.GetType().Name); // - Display a dialog box to let the user choose their Cyber model file bool Cyber_file_chosen = false; //bool test_copy_and_path_only = false; if (string.IsNullOrWhiteSpace(Cyberpath)) { Cyber_file_chosen = get_Cyber_file(out CyberFilename); } else { //test_copy_and_path_only = true; CyberFilename = Cyberpath; if (File.Exists(CyberFilename)) { Cyber_file_chosen = true; } else { this.Logger.WriteError("Invalid Cyber file path passed in: " + Cyberpath); } } // - Run the extractor on the Creo model file #region Run the Extractor List <string> componentList = new List <string>(); IMgaProject project = (IMgaProject)Activator.CreateInstance(Type.GetTypeFromProgID("Mga.MgaProject")); string compName; project.OpenEx("MGA=" + CyberFilename, "CyberComposition", null); try { project.BeginTransactionInNewTerr(); IMgaFCO currentObj; try { IMgaFolder currentFolder = (IMgaFolder)project.RootFolder; IMgaFolders cFolders = currentFolder.ChildFolders; foreach (IMgaFolder f in cFolders) { if (f.MetaFolder.Name == "Components") { IMgaFCOs objects = f.ChildFCOs; foreach (IMgaFCO o in objects) { componentList.Add("/" + f.Name + "/" + o.Name); } } } } finally { project.CommitTransaction(); } // Adapted from ModelicaImporter.cs string result = ""; using (CyberComponentPicker cyberpicker = new CyberComponentPicker(componentList)) { var dialogResult = cyberpicker.ShowDialog(); if (dialogResult != System.Windows.Forms.DialogResult.OK) { this.Logger.WriteInfo("Modelica import was cancelled by the user."); return; } result = cyberpicker.compResult; } var halves = result.Split('/'); compName = halves[1]; project.BeginTransactionInNewTerr(); try { currentObj = (IMgaFCO)project.RootFolder.ObjectByPath[result]; } finally { project.CommitTransaction(); } IMgaComponentEx comp = (IMgaComponentEx)Activator.CreateInstance(Type.GetTypeFromProgID("MGA.Interpreter.CyberComponentExporter")); comp.Initialize((MgaProject)project); comp.InvokeEx((MgaProject)project, (MgaFCO)currentObj, null, 128); project.Save(); } finally { project.Close(true); } #endregion // - Use a function from CyPhy2ComponentModel to convert the extractor's XML format into a CyPhy model fragment #region Convert_to_XML // used in creating the resource object below CyPhy.CyberModel ProcessedCyberModel = null; if (true) { this.Logger.WriteDebug("About to call DeserializeAvmComponentXml..."); StreamReader streamReader = new StreamReader(compName + ".component.acm"); avm.Component ac_import = CyPhyComponentImporter.CyPhyComponentImporterInterpreter.DeserializeAvmComponentXml(streamReader); streamReader.Close(); this.Logger.WriteDebug("... finished DeserializeAvmComponentXml call."); foreach (avm.cyber.CyberModel Cybermodel in ac_import.DomainModel.Where(dm => dm is avm.cyber.CyberModel) .Cast <avm.cyber.CyberModel>()) { var rf = CyPhyClasses.RootFolder.GetRootFolder(CurrentProj); Dictionary <string, CyPhy.Component> avmidComponentMap = new Dictionary <string, CyPhy.Component>(); AVM2CyPhyML.CyPhyMLComponentBuilder newComponent = new AVM2CyPhyML.CyPhyMLComponentBuilder(rf); ProcessedCyberModel = newComponent.process(Cybermodel, GetCurrentComp()); ProcessedCyberModel.Name = Path.GetFileNameWithoutExtension(CyberFilename); } // find the largest current Y value so our new elements are added below the existing design elements foreach (var child in GetCurrentComp().AllChildren) { foreach (MgaPart item in (child.Impl as MgaFCO).Parts) { int read_x, read_y; string read_str; item.GetGmeAttrs(out read_str, out read_x, out read_y); greatest_current_y = (read_y > greatest_current_y) ? read_y : greatest_current_y; } } // layout Cyber model to the "south" of existing elements foreach (MgaPart item in (ProcessedCyberModel.Impl as MgaFCO).Parts) { item.SetGmeAttrs(null, Cyber_MODEL_START_X, greatest_current_y + Cyber_MODEL_START_Y); } // Extend it's properties out to the component level. this.CyberModuleImportExtension(ProcessedCyberModel); } #endregion // - Copy the Cyber Model files into the component's backend folder // - Note: The solution includes a function that can find this folder using the project.manifest.json file. // - For nice organization, create them in a subfolder called "Cyber" // create avmproj #region Copy files to backend folder // used in creating the resource object below string PathforComp = null; var importedCyberFiles = new List <String>(); if (true) { try { // create the destination path PathforComp = GetCurrentComp().GetDirectoryPath(ComponentLibraryManager.PathConvention.ABSOLUTE); string finalPathName = Path.Combine(PathforComp, "Cyber"); Directory.CreateDirectory(finalPathName); // determine if one part file or all part and assembly files need to be copied string cpsrcfile = System.IO.Path.GetFileName(CyberFilename); // copy the selected file string CyberFileCopyPath = System.IO.Path.Combine(finalPathName, cpsrcfile); System.IO.File.Copy(CyberFilename, CyberFileCopyPath, true); // Set "primary" file as the first in the list. importedCyberFiles.Add(Path.Combine("Cyber", Path.GetFileName(CyberFileCopyPath))); if (true) { // get a string of the XML contents this.Logger.WriteDebug("About to read contents of XML file using class XmlDocument: " + compName + ".component.acm"); XmlDocument doc = new XmlDocument(); doc.Load(compName + ".component.acm"); string xmlcontents = doc.InnerXml; // mine down to the Resource dependencies using (XmlReader reader = XmlReader.Create(new StringReader(xmlcontents))) { // iterate through each file listed in the resourcedependency section while (reader.ReadToFollowing("ResourceDependency") == true) { string res_name = reader.GetAttribute("Name"); string res_path = reader.GetAttribute("Path"); this.Logger.WriteDebug("Copying this file: " + res_path + "\\" + res_name); // Cyber files end in .1 .2 etc. Pick the latest ones var allFiles = Directory.EnumerateFiles(Path.GetDirectoryName(res_path), "*mga." /*n.b. literal dot*/ + "*") .Select(Path.GetFileName) .Select(filename => new { basename = filename.Substring(0, filename.LastIndexOf('.')), version = filename.Substring(filename.LastIndexOf('.') + 1) }) .Where(p => { int val; return(Int32.TryParse(p.version, out val)); }) .OrderByDescending(p => Int32.Parse(p.version)) .ToArray(); foreach (var basename in allFiles.Select(p => p.basename).Distinct()) { var latest = allFiles.Where(p => p.basename == basename).FirstOrDefault(); if (latest != null) { string latestFilename = latest.basename + "." + latest.version; // Need to limit this down to just the filename in question // The XML file changes the name to all caps, so compare apples to apples if (latestFilename.ToUpper().StartsWith(res_name.ToUpper())) { string destpathandname = Path.Combine(finalPathName, latestFilename); if (!importedCyberFiles.Contains(Path.Combine("Cyber", Path.GetFileName(destpathandname)))) { importedCyberFiles.Add(Path.Combine("Cyber", Path.GetFileName(destpathandname))); } File.Copy(Path.Combine(Path.GetDirectoryName(res_path), latestFilename), destpathandname, true); break; } } } } } } } catch (Exception err_create_proj) { this.Logger.WriteError("Error creating AVM project: " + err_create_proj.Message, " - Extraction Failed. Possible ComponentManagement issue."); //cleanup(tempXMLfile, true); return; } } #endregion // - Create Resource objects in the CyPhy Component model that point to these Cyber Model files // - Note: The "paths" of these should be the relative path from that component's root folder if (true) { foreach (var CyberFile in importedCyberFiles) { CyPhy.Resource ResourceObj = CyPhyClasses.Resource.Create(GetCurrentComp()); ResourceObj.Attributes.ID = Guid.NewGuid().ToString("B"); ResourceObj.Attributes.Path = CyberFile; ResourceObj.Attributes.Notes = "Cyber Model Import tool added this resource object for the imported Cyber file"; ResourceObj.Name = Path.GetFileName(CyberFile); // layout Resource just to the side of the Cyber model foreach (MgaPart item in (ResourceObj.Impl as MgaFCO).Parts) { item.SetGmeAttrs(null, RESOURCE_START_X, greatest_current_y + RESOURCE_START_Y); } // The "primary" Cyber model is the first one -- associate it with the CyPhy CyberModel object if (importedCyberFiles.IndexOf(CyberFile) == 0) { // - Create a UsesResource association between the CyPhy CyberModel object and the Resource object that represents the top-level Creo Model file. CyPhyClasses.UsesResource.Connect(ResourceObj, ProcessedCyberModel, null, null, GetCurrentComp()); } } } // throw in an ACM file for the current state of the component. if (true) { var exporter = new CyPhyComponentExporter.CyPhyComponentExporterInterpreter(); String acmPath = Path.Combine(PathforComp, GetCurrentComp().Name + ".component.acm"); CyPhyComponentExporterInterpreter.ExportToFile(GetCurrentComp(), Path.GetDirectoryName(acmPath)); } // Clean up //cleanup(tempXMLfile, true); }
public void ImportCADModel(string CADpath = null) { string cadFilename = ""; this.Logger = new CyPhyGUIs.GMELogger(CurrentProj, this.GetType().Name); //The CAT module will perform these steps, in this order: // - Check that the user has Creo (so that the extraction will be successful) // - Implementation Suggestion: The extraction utility may have special flag to have it indicates if all the dependencies are met bool creo_installed = this.CREO_present(); // - Display a dialog box to let the user choose their Creo model file bool cad_file_chosen = false; bool test_copy_and_path_only = false; if (string.IsNullOrWhiteSpace(CADpath)) { cad_file_chosen = get_cad_file(out cadFilename); } else { test_copy_and_path_only = true; cadFilename = CADpath; if (File.Exists(cadFilename)) { cad_file_chosen = true; } else { this.Logger.WriteError("Invalid CAD file path passed in: " + CADpath); } } // - Run the extractor on the Creo model file #region Run the Extractor bool extractor_ran_success = test_copy_and_path_only; string tempXMLfile = Path.GetTempFileName(); this.Logger.WriteDebug("Temporary XML file created: " + tempXMLfile); if (creo_installed && cad_file_chosen && !test_copy_and_path_only) { try { // assemble the arg string // first the input CAD file // include quotation marks to handle paths with white spaces string argstring = "-c \""; argstring += cadFilename; argstring += "\""; // add the XML output file name argstring += " -x "; argstring += tempXMLfile; // Debug only: pause before exit, graphics mode. //argstring += " -p -g"; Process firstProc = new Process(); // NOTE: Process class does not expand environment variables, do it manually string temp = Environment.GetEnvironmentVariable("PROE_ISIS_EXTENSIONS"); if (temp == null) { this.Logger.WriteError("Please set the PROE_ISIS_EXTENSIONS environment variable"); cleanup(tempXMLfile, true); return; } string path = Path.Combine(temp, "bin\\ExtractACM-XMLfromCreoModels.exe"); if (!File.Exists(path)) { this.Logger.WriteError("Cannot find ExtractACM-XMLfromCreoModels.exe - Check if CAD Assembler was built and files installed to PROE_ISIS_EXTENSIONS"); throw new Exception("ExtractACM-XMLfromCreoModels.exe not found."); } firstProc.StartInfo.FileName = path; firstProc.StartInfo.Arguments = argstring; this.Logger.WriteDebug("Calling ExtractACM-XMLfromCreoModels.exe with argument string: " + argstring); firstProc.EnableRaisingEvents = true; firstProc.Start(); firstProc.WaitForExit(); this.Logger.WriteDebug("ExtractACM-XMLfromCreoModels.exe has completed."); if (firstProc.ExitCode == 0) { extractor_ran_success = true; } else { this.Logger.WriteDebug("ExtractACM-XMLfromCreoModels.exe returned error code " + firstProc.ExitCode.ToString()); throw new Exception("Extract executable returned error code " + firstProc.ExitCode.ToString()); } } catch (Exception ex) { this.Logger.WriteError("An error occurred running Creo parametric: " + ex.Message + " - Extraction Failed. Insure you can access the Creo license server"); cleanup(tempXMLfile, true); return; } } #endregion // - Use a function from CyPhy2ComponentModel to convert the extractor's XML format into a CyPhy model fragment #region Convert_to_XML // used in creating the resource object below CyPhy.CADModel ProcessedCADModel = null; if (extractor_ran_success && !test_copy_and_path_only) { this.Logger.WriteDebug("About to call DeserializeAvmComponentXml..."); StreamReader streamReader = new StreamReader(tempXMLfile); avm.Component ac_import = CyPhyComponentImporter.CyPhyComponentImporterInterpreter.DeserializeAvmComponentXml(streamReader); streamReader.Close(); this.Logger.WriteDebug("... finished DeserializeAvmComponentXml call."); foreach (var cadmodel in ac_import.DomainModel.Where(dm => dm is avm.cad.CADModel) .Cast<avm.cad.CADModel>()) { var rf = CyPhyClasses.RootFolder.GetRootFolder(CurrentProj); Dictionary<string, CyPhy.Component> avmidComponentMap = new Dictionary<string, CyPhy.Component>(); AVM2CyPhyML.CyPhyMLComponentBuilder newComponent = new AVM2CyPhyML.CyPhyMLComponentBuilder(rf); ProcessedCADModel = newComponent.process(cadmodel, GetCurrentComp()); ProcessedCADModel.Name = Path.GetFileNameWithoutExtension(cadFilename); } // find the largest current Y value so our new elements are added below the existing design elements foreach (var child in GetCurrentComp().AllChildren) { foreach (MgaPart item in (child.Impl as MgaFCO).Parts) { int read_x, read_y; string read_str; item.GetGmeAttrs(out read_str, out read_x, out read_y); greatest_current_y = (read_y > greatest_current_y) ? read_y : greatest_current_y; } } // layout CAD model to the "south" of existing elements foreach (MgaPart item in (ProcessedCADModel.Impl as MgaFCO).Parts) { item.SetGmeAttrs(null, CAD_MODEL_START_X, greatest_current_y + CAD_MODEL_START_Y); } // Extend it's properties out to the component level. this.CADModuleImportExtension(ProcessedCADModel); } else if (test_copy_and_path_only) { ProcessedCADModel = CyPhyClasses.CADModel.Create(GetCurrentComp()); ProcessedCADModel.Name = Path.GetFileNameWithoutExtension(CADpath); } #endregion // - Copy the Creo Model files into the component's backend folder // - Note: The solution includes a function that can find this folder using the project.manifest.json file. // - For nice organization, create them in a subfolder called "CAD" // create avmproj #region Copy files to backend folder // used in creating the resource object below string PathforComp = null; var importedCADFiles = new List<String>(); if (extractor_ran_success) { try { // create the destination path PathforComp = META.ComponentLibraryManager.EnsureComponentFolder(GetCurrentComp()); PathforComp = GetCurrentComp().GetDirectoryPath(ComponentLibraryManager.PathConvention.ABSOLUTE); string finalPathName = Path.Combine(PathforComp, "CAD"); Directory.CreateDirectory(finalPathName); // determine if one part file or all part and assembly files need to be copied string cpsrcfile = System.IO.Path.GetFileName(cadFilename); // copy the selected file string cadFileCopyPath = System.IO.Path.Combine(finalPathName, cpsrcfile); System.IO.File.Copy(cadFilename, cadFileCopyPath, true); // Set "primary" file as the first in the list. importedCADFiles.Add(Path.Combine("CAD", Path.GetFileName(cadFileCopyPath))); if (fileISasm(cpsrcfile) && !test_copy_and_path_only) { // get a string of the XML contents this.Logger.WriteDebug("About to read contents of XML file using class XmlDocument: " + tempXMLfile); XmlDocument doc = new XmlDocument(); doc.Load(tempXMLfile); string xmlcontents = doc.InnerXml; // mine down to the Resource dependencies using (XmlReader reader = XmlReader.Create(new StringReader(xmlcontents))) { // iterate through each file listed in the resourcedependency section while (reader.ReadToFollowing("ResourceDependency") == true) { string res_name = reader.GetAttribute("Name"); string res_path = reader.GetAttribute("Path"); this.Logger.WriteDebug("Copying this file: " + res_path + "\\" + res_name); // CAD files end in .1 .2 etc. Pick the latest ones var allFiles = Directory.EnumerateFiles(Path.GetDirectoryName(res_path), "*prt." /*n.b. literal dot*/ + "*") .Concat(Directory.EnumerateFiles(Path.GetDirectoryName(res_path), "*asm.*")) .Select(Path.GetFileName) .Select(filename => new { basename = filename.Substring(0, filename.LastIndexOf('.')), version = filename.Substring(filename.LastIndexOf('.') + 1) }) .Where(p => { int val; return Int32.TryParse(p.version, out val); }) .OrderByDescending(p => Int32.Parse(p.version)) .ToArray(); foreach (var basename in allFiles.Select(p => p.basename).Distinct()) { var latest = allFiles.Where(p => p.basename == basename).FirstOrDefault(); if (latest != null) { string latestFilename = latest.basename + "." + latest.version; // Need to limit this down to just the filename in question // The XML file changes the name to all caps, so compare apples to apples if (latestFilename.ToUpper().StartsWith(res_name.ToUpper())) { string destpathandname = Path.Combine(finalPathName, latestFilename); if (!importedCADFiles.Contains(Path.Combine("CAD", Path.GetFileName(destpathandname)))) { importedCADFiles.Add(Path.Combine("CAD", Path.GetFileName(destpathandname))); } File.Copy(Path.Combine(Path.GetDirectoryName(res_path), latestFilename), destpathandname, true); break; } } } } } } } catch (Exception err_create_proj) { this.Logger.WriteError("Error creating AVM project: " + err_create_proj.Message, " - Extraction Failed. Possible ComponentManagement issue."); cleanup(tempXMLfile, true); return; } } #endregion // - Create Resource objects in the CyPhy Component model that point to these Creo Model files // - Note: The "paths" of these should be the relative path from that component's root folder if (extractor_ran_success) { foreach (var cadFile in importedCADFiles) { CyPhy.Resource ResourceObj = CyPhyClasses.Resource.Create(GetCurrentComp()); ResourceObj.Attributes.ID = Guid.NewGuid().ToString("B"); ResourceObj.Attributes.Path = cadFile; ResourceObj.Attributes.Notes = "CAD Model Import tool added this resource object for the imported CAD file"; ResourceObj.Name = Path.GetFileName(cadFile); // layout Resource just to the side of the CAD model foreach (MgaPart item in (ResourceObj.Impl as MgaFCO).Parts) { item.SetGmeAttrs(null, RESOURCE_START_X, greatest_current_y + RESOURCE_START_Y); } // The "primary" CAD model is the first one -- associate it with the CyPhy CADModel object if (importedCADFiles.IndexOf(cadFile) == 0) { // - Create a UsesResource association between the CyPhy CADModel object and the Resource object that represents the top-level Creo Model file. CyPhyClasses.UsesResource.Connect(ResourceObj, ProcessedCADModel, null, null, GetCurrentComp()); } } } // throw in an ACM file for the current state of the component. if (extractor_ran_success && !test_copy_and_path_only) { var exporter = new CyPhyComponentExporter.CyPhyComponentExporterInterpreter(); String acmPath = Path.Combine(PathforComp,GetCurrentComp().Name + ".component.acm"); CyPhyComponentExporterInterpreter.ExportToFile(GetCurrentComp(), Path.GetDirectoryName(acmPath)); } // Clean up cleanup(tempXMLfile, (extractor_ran_success && !test_copy_and_path_only)); }
public void ImportCADModel(string CADpath = null) { string cadFilename = ""; this.Logger = new CyPhyGUIs.GMELogger(CurrentProj, this.GetType().Name); //The CAT module will perform these steps, in this order: // - Check that the user has Creo (so that the extraction will be successful) // - Implementation Suggestion: The extraction utility may have special flag to have it indicates if all the dependencies are met bool creo_installed = this.CREO_present(); // - Display a dialog box to let the user choose their Creo model file bool cad_file_chosen = false; bool test_copy_and_path_only = false; if (string.IsNullOrWhiteSpace(CADpath)) { cad_file_chosen = get_cad_file(out cadFilename); } else { test_copy_and_path_only = true; cadFilename = CADpath; if (File.Exists(cadFilename)) { cad_file_chosen = true; } else { this.Logger.WriteError("Invalid CAD file path passed in: " + CADpath); } } // - Run the extractor on the Creo model file #region Run the Extractor bool extractor_ran_success = test_copy_and_path_only; string tempXMLfile = Path.GetTempFileName(); this.Logger.WriteDebug("Temporary XML file created: " + tempXMLfile); if (creo_installed && cad_file_chosen && !test_copy_and_path_only) { try { // assemble the arg string // first the input CAD file // include quotation marks to handle paths with white spaces string argstring = "ExtractACM-XMLfromCreoModels -c \""; argstring += cadFilename; argstring += "\""; // add the XML output file name argstring += " -x "; argstring += tempXMLfile; // Debug only: pause before exit, graphics mode. //argstring += " -p -g"; Process firstProc = new Process(); string path = Path.Combine(META.VersionInfo.MetaPath, "bin\\CAD\\Creo\\bin\\CADCreoParametricCreateAssembly.exe"); if (!File.Exists(path)) { this.Logger.WriteError(String.Format("Cannot find '{0}'", path)); throw new Exception("CADCreoParametricCreateAssembly.exe not found."); } firstProc.StartInfo.FileName = path; firstProc.StartInfo.Arguments = argstring; this.Logger.WriteDebug("Calling CADCreoParametricCreateAssembly.exe with argument string: " + argstring); firstProc.Start(); firstProc.WaitForExit(); this.Logger.WriteDebug("CADCreoParametricCreateAssembly.exe ExtractACM-XMLfromCreoModels has completed."); if (firstProc.ExitCode == 0) { extractor_ran_success = true; } else { this.Logger.WriteDebug("CADCreoParametricCreateAssembly.exe ExtractACM-XMLfromCreoModels returned error code " + firstProc.ExitCode.ToString()); throw new Exception("Extract executable returned error code " + firstProc.ExitCode.ToString()); } } catch (Exception ex) { this.Logger.WriteError("An error occurred running Creo parametric: " + ex.Message + " - Extraction Failed. Insure you can access the Creo license server"); cleanup(tempXMLfile, true); return; } } #endregion // - Use a function from CyPhy2ComponentModel to convert the extractor's XML format into a CyPhy model fragment #region Convert_to_XML // used in creating the resource object below CyPhy.CADModel ProcessedCADModel = null; if (extractor_ran_success && !test_copy_and_path_only) { this.Logger.WriteDebug("About to call DeserializeAvmComponentXml..."); StreamReader streamReader = new StreamReader(tempXMLfile); avm.Component ac_import = CyPhyComponentImporter.CyPhyComponentImporterInterpreter.DeserializeAvmComponentXml(streamReader); streamReader.Close(); this.Logger.WriteDebug("... finished DeserializeAvmComponentXml call."); foreach (var cadmodel in ac_import.DomainModel.Where(dm => dm is avm.cad.CADModel) .Cast <avm.cad.CADModel>()) { var rf = CyPhyClasses.RootFolder.GetRootFolder(CurrentProj); AVM2CyPhyML.CyPhyMLComponentBuilder newComponent = new AVM2CyPhyML.CyPhyMLComponentBuilder(rf); ProcessedCADModel = newComponent.process(cadmodel, GetCurrentComp()); ProcessedCADModel.Name = Path.GetFileName(AVM2CyPhyML.CyPhyMLComponentBuilder.GetCreoFileWithoutVersion(cadFilename)); } // find the largest current Y value so our new elements are added below the existing design elements foreach (var child in GetCurrentComp().AllChildren) { foreach (MgaPart item in (child.Impl as MgaFCO).Parts) { int read_x, read_y; string read_str; item.GetGmeAttrs(out read_str, out read_x, out read_y); greatest_current_y = (read_y > greatest_current_y) ? read_y : greatest_current_y; } } // layout CAD model to the "south" of existing elements foreach (MgaPart item in (ProcessedCADModel.Impl as MgaFCO).Parts) { item.SetGmeAttrs(null, CAD_MODEL_START_X, greatest_current_y + CAD_MODEL_START_Y); } // Extend it's properties out to the component level. this.CADModuleImportExtension(ProcessedCADModel); } else if (test_copy_and_path_only) { ProcessedCADModel = CyPhyClasses.CADModel.Create(GetCurrentComp()); ProcessedCADModel.Name = Path.GetFileName(AVM2CyPhyML.CyPhyMLComponentBuilder.GetCreoFileWithoutVersion(CADpath)); } #endregion // - Copy the Creo Model files into the component's backend folder // - Note: The solution includes a function that can find this folder using the project.manifest.json file. // - For nice organization, create them in a subfolder called "CAD" // create avmproj #region Copy files to backend folder // used in creating the resource object below string PathforComp = null; var importedCADFiles = new List <String>(); if (extractor_ran_success) { try { // create the destination path PathforComp = META.ComponentLibraryManager.EnsureComponentFolder(GetCurrentComp()); PathforComp = GetCurrentComp().GetDirectoryPath(ComponentLibraryManager.PathConvention.ABSOLUTE); string finalPathName = Path.Combine(PathforComp, "CAD"); Directory.CreateDirectory(finalPathName); // determine if one part file or all part and assembly files need to be copied string cpsrcfile = System.IO.Path.GetFileName(cadFilename); // copy the selected file string cadFileCopyPath = System.IO.Path.Combine(finalPathName, cpsrcfile); System.IO.File.Copy(cadFilename, cadFileCopyPath, true); // Set "primary" file as the first in the list. importedCADFiles.Add(Path.Combine("CAD", Path.GetFileName(cadFileCopyPath))); if (fileISasm(cpsrcfile) && !test_copy_and_path_only) { // get a string of the XML contents this.Logger.WriteDebug("About to read contents of XML file using class XmlDocument: " + tempXMLfile); XmlDocument doc = new XmlDocument(); doc.Load(tempXMLfile); string xmlcontents = doc.InnerXml; // mine down to the Resource dependencies using (XmlReader reader = XmlReader.Create(new StringReader(xmlcontents))) { // iterate through each file listed in the resourcedependency section while (reader.ReadToFollowing("ResourceDependency") == true) { string res_name = reader.GetAttribute("Name"); string res_path = reader.GetAttribute("Path"); this.Logger.WriteDebug("Copying this file: " + res_path + "\\" + res_name); // CAD files end in .1 .2 etc. Pick the latest ones var allFiles = Directory.EnumerateFiles(Path.GetDirectoryName(res_path), "*prt." /*n.b. literal dot*/ + "*") .Concat(Directory.EnumerateFiles(Path.GetDirectoryName(res_path), "*asm.*")) .Select(Path.GetFileName) .Select(filename => new { basename = filename.Substring(0, filename.LastIndexOf('.')), version = filename.Substring(filename.LastIndexOf('.') + 1) }) .Where(p => { int val; return(Int32.TryParse(p.version, out val)); }) .OrderByDescending(p => Int32.Parse(p.version)) .ToArray(); foreach (var basename in allFiles.Select(p => p.basename).Distinct()) { var latest = allFiles.Where(p => p.basename == basename).FirstOrDefault(); if (latest != null) { string latestFilename = latest.basename + "." + latest.version; // Need to limit this down to just the filename in question // The XML file changes the name to all caps, so compare apples to apples if (latestFilename.ToUpper().StartsWith(res_name.ToUpper())) { string destpathandname = Path.Combine(finalPathName, latestFilename); if (!importedCADFiles.Contains(Path.Combine("CAD", Path.GetFileName(destpathandname)))) { importedCADFiles.Add(Path.Combine("CAD", Path.GetFileName(destpathandname))); } File.Copy(Path.Combine(Path.GetDirectoryName(res_path), latestFilename), destpathandname, true); break; } } } } } } } catch (Exception err_create_proj) { this.Logger.WriteError("Error creating AVM project: " + err_create_proj.Message, " - Extraction Failed. Possible ComponentManagement issue."); cleanup(tempXMLfile, true); return; } } #endregion // - Create Resource objects in the CyPhy Component model that point to these Creo Model files // - Note: The "paths" of these should be the relative path from that component's root folder if (extractor_ran_success) { foreach (var cadFile in importedCADFiles) { CyPhy.Resource ResourceObj = CyPhyClasses.Resource.Create(GetCurrentComp()); ResourceObj.Attributes.ID = Guid.NewGuid().ToString("B"); ResourceObj.Attributes.Path = AVM2CyPhyML.CyPhyMLComponentBuilder.GetCreoFileWithoutVersion(cadFile); ResourceObj.Attributes.Notes = "CAD Model Import tool added this resource object for the imported CAD file"; ResourceObj.Name = Path.GetFileName(AVM2CyPhyML.CyPhyMLComponentBuilder.GetCreoFileWithoutVersion(cadFile)); // layout Resource just to the side of the CAD model foreach (MgaPart item in (ResourceObj.Impl as MgaFCO).Parts) { item.SetGmeAttrs(null, RESOURCE_START_X, greatest_current_y + RESOURCE_START_Y); } // The "primary" CAD model is the first one -- associate it with the CyPhy CADModel object if (importedCADFiles.IndexOf(cadFile) == 0) { // - Create a UsesResource association between the CyPhy CADModel object and the Resource object that represents the top-level Creo Model file. CyPhyClasses.UsesResource.Connect(ResourceObj, ProcessedCADModel, null, null, GetCurrentComp()); } } } // throw in an ACM file for the current state of the component. if (extractor_ran_success && !test_copy_and_path_only) { var exporter = new CyPhyComponentExporter.CyPhyComponentExporterInterpreter(); String acmPath = Path.Combine(PathforComp, GetCurrentComp().Name + ".component.acm"); CyPhyComponentExporterInterpreter.ExportToFile(GetCurrentComp(), Path.GetDirectoryName(acmPath)); } // Clean up cleanup(tempXMLfile, (extractor_ran_success && !test_copy_and_path_only)); }