// Running CAD flattener on the assembly void TestCadFlattener() { SetupTest(); RunCyPhyMLSync( (project, propagate, interpreter) => { project.BeginTransactionInNewTerr(); CyPhyML.ComponentAssembly assembly = CyPhyMLClasses.ComponentAssembly.Cast(GetTestAssembly(project)); Type t = Type.GetTypeFromProgID("MGA.Interpreter.CyPhyElaborateCS"); IMgaComponentEx elaborator = Activator.CreateInstance(t) as IMgaComponentEx; DateTime t1 = DateTime.Now; elaborator.Initialize(project); elaborator.ComponentParameter["automated_expand"] = "true"; elaborator.ComponentParameter["console_messages"] = "off"; elaborator.InvokeEx(project, GetTestAssembly(project), (MgaFCOs)Activator.CreateInstance(Type.GetTypeFromProgID("Mga.MgaFCOs")), 0); CyPhy2CAD_CSharp.Logger.Instance.Reset(); CyPhy2CAD_CSharp.CADFlatDataCreator datacreator = new CyPhy2CAD_CSharp.CADFlatDataCreator("", Path.GetDirectoryName(project.ProjectConnStr.Substring("MGA=".Length)), CyPhyMLClasses.CADModel.AttributesClass.FileFormat_enum.Creo, metalink: true); datacreator.CreateFlatData(assembly); CyPhy2CAD_CSharp.DataRep.CADContainer cadcontainer = datacreator.CreateCADDataContainer(assembly.Guid.ToString(), CyPhy2CAD_CSharp.UtilityHelpers.CleanString2(assembly.Name)); // IF this is an empty design, add a root component if (cadcontainer.assemblies.Count == 0) { cadcontainer.AddRootComponent(assembly); } Xunit.Assert.Equal(CyPhy2CAD_CSharp.Logger.Instance.ErrorCnt, 0); project.AbortTransaction(); } ); }
private void GenerateCADAssemblyXml(MgaProject project, MgaFCO currentobj, int param) { int origPrefs = project.Preferences; try { project.Preferences = project.Preferences | (int)GME.MGA.preference_flags.MGAPREF_IGNORECONNCHECKS | (int)GME.MGA.preference_flags.MGAPREF_FREEINSTANCEREFS; CyPhyML.ComponentAssembly assembly = CyPhyMLClasses.ComponentAssembly.Cast(currentobj); List <string> errorList = new List <string>(); if (!VerifyAssembly(assembly, errorList)) { foreach (string s in errorList) { GMEConsole.Warning.WriteLine(s); } System.Windows.Forms.MessageBox.Show("Model verification failed for Meta-Link and it may not work properly for this model.", "Warning", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning); } SyncedComponentData syncedCompData; if (!syncedComponents.TryGetValue(assembly.Guid.ToString(), out syncedCompData)) { throw new Exception("Internal error in GenerateCADAssemblyXml, can't find data for guid " + assembly.Guid); } HashSet <CyPhyML.Component> comps = CyphyMetaLinkUtils.CollectComponentsRecursive(assembly); // Prepare Meta-Link specific data based on the original model CyPhy2CAD_CSharp.MetaLinkData metaLinkData = new CyPhy2CAD_CSharp.MetaLinkData(); try { PrepareMetaLinkData(metaLinkData, assembly, ""); } catch (Exception ex) { throw new Exception("Error during collecting metalink data." + ex.Message); } DateTime t1 = DateTime.Now; // call elaborator and expand the references // Please note that elaborator will change "currentobj"'s internal structure Type t = Type.GetTypeFromProgID("MGA.Interpreter.CyPhyElaborateCS"); if (t == null) { throw new NullReferenceException("Cannot find MGA.Interpreter.CyPhyElaborate. Is it registered?"); } IMgaComponentEx elaborator = Activator.CreateInstance(t) as IMgaComponentEx; elaborator.Initialize(project); elaborator.ComponentParameter["automated_expand"] = "true"; elaborator.ComponentParameter["console_messages"] = "off"; elaborator.InvokeEx(project, currentobj, (MgaFCOs)Activator.CreateInstance(Type.GetTypeFromProgID("Mga.MgaFCOs")), param); TimeSpan ts = DateTime.Now - t1; GMEConsole.Info.WriteLine("Elaborator was running for " + ts.TotalMilliseconds + "ms"); CyPhy2CAD_CSharp.CyPhy2CADSettings cadSettings = new CyPhy2CAD_CSharp.CyPhy2CADSettings(); cadSettings.AuxiliaryDirectory = syncedCompData.AuxDir; cadSettings.OutputDirectory = syncedCompData.WorkingDir; cadSettings._metaLink = true; CyPhy2CAD_CSharp.Logger.Instance.Reset(); CyPhy2CAD_CSharp.CADFlatDataCreator datacreator = new CyPhy2CAD_CSharp.CADFlatDataCreator(cadSettings.OutputDirectory, GetProjectDir(), CadFormat, metalink: true); datacreator.CreateFlatData(assembly); CyPhy2CAD_CSharp.DataRep.CADContainer cadcontainer = datacreator.CreateCADDataContainer(assembly.Guid.ToString(), CyPhy2CAD_CSharp.UtilityHelpers.CleanString2(assembly.Name)); cadcontainer.MergeAssemblies(); // IF this is an empty design, add a root component if (cadcontainer.assemblies.Count == 0) { cadcontainer.AddRootComponent(assembly); } cadcontainer.InjectMetaLinkData(assembly, metaLinkData); CyPhy2CAD_CSharp.TestBenchModel.TestBenchBase testbenchbase = new CyPhy2CAD_CSharp.TestBenchModel.TestBenchBase(cadSettings, cadSettings.OutputDirectory, GetProjectDir()); testbenchbase.cadDataContainer = cadcontainer; testbenchbase.CollectDirectories(); testbenchbase.GenerateCADXMLOutput(); testbenchbase.GenerateScriptFiles(); bool stop = CyPhy2CAD_CSharp.Logger.Instance.ErrorCnt > 0; CyPhy2CAD_CSharp.Logger.Instance.DumpLog(GMEConsole, ""); if (stop) { throw new Exception("There were errors during processing the model. Please look at the console log and fix these."); } string CADAssembly = File.ReadAllText(Path.Combine(cadSettings.OutputDirectory, CyPhy2CAD_CSharp.TestBenchModel.TestBenchBase.CADAssemblyFile)); string designId = new Guid(currentobj.GetGuidDisp()).ToString("D"); SendInterest(null, syncedCompData.InstanceId); foreach (var comp in comps) { // SendInterest(null, ComponentTopic, comp.Attributes.AVMID); } SaveCadAssemblyXml(CADAssembly, designId); } finally { project.Preferences = origPrefs; } }
// Running CAD flattener on the assembly void TestCadFlattener() { SetupTest(); RunCyPhyMLSync( (project, propagate, interpreter) => { project.BeginTransactionInNewTerr(); CyPhyML.ComponentAssembly assembly = CyPhyMLClasses.ComponentAssembly.Cast(GetTestAssembly(project)); Type t = Type.GetTypeFromProgID("MGA.Interpreter.CyPhyElaborateCS"); IMgaComponentEx elaborator = Activator.CreateInstance(t) as IMgaComponentEx; DateTime t1 = DateTime.Now; elaborator.Initialize(project); elaborator.ComponentParameter["automated_expand"] = "true"; elaborator.ComponentParameter["console_messages"] = "off"; elaborator.InvokeEx(project, GetTestAssembly(project), (MgaFCOs)Activator.CreateInstance(Type.GetTypeFromProgID("Mga.MgaFCOs")), 0); CyPhy2CAD_CSharp.Logger.Instance.Reset(); CyPhy2CAD_CSharp.CADFlatDataCreator datacreator = new CyPhy2CAD_CSharp.CADFlatDataCreator("", Path.GetDirectoryName(project.ProjectConnStr.Substring("MGA=".Length)), metalink: true); datacreator.CreateFlatData(assembly); CyPhy2CAD_CSharp.DataRep.CADContainer cadcontainer = datacreator.CreateCADDataContainer(assembly.Guid.ToString(), CyPhy2CAD_CSharp.UtilityHelpers.CleanString2(assembly.Name)); // IF this is an empty design, add a root component if (cadcontainer.assemblies.Count == 0) { cadcontainer.AddRootComponent(assembly); } Xunit.Assert.Equal(CyPhy2CAD_CSharp.Logger.Instance.ErrorCnt, 0); project.AbortTransaction(); } ); }
private void GenerateCADAssemblyXml(MgaProject project, MgaFCO currentobj, int param) { int origPrefs = project.Preferences; try { project.Preferences = project.Preferences | (int)GME.MGA.preference_flags.MGAPREF_IGNORECONNCHECKS | (int)GME.MGA.preference_flags.MGAPREF_FREEINSTANCEREFS; CyPhyML.ComponentAssembly assembly = CyPhyMLClasses.ComponentAssembly.Cast(currentobj); List<string> errorList = new List<string>(); if (!VerifyAssembly(assembly, errorList)) { System.Windows.Forms.MessageBox.Show("Model verification failed for Meta-Link and it may not work properly for this model.", "Warning", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning); foreach (string s in errorList) GMEConsole.Warning.WriteLine(s); } SyncedComponentData syncedCompData; if (!syncedComponents.TryGetValue(assembly.Guid.ToString(), out syncedCompData)) { throw new Exception("Internal error in GenerateCADAssemblyXml, can't find data for guid " + assembly.Guid); } HashSet<CyPhyML.Component> comps = CyphyMetaLinkUtils.CollectComponentsRecursive(assembly); // Prepare Meta-Link specific data based on the original model CyPhy2CAD_CSharp.MetaLinkData metaLinkData = new CyPhy2CAD_CSharp.MetaLinkData(); try { PrepareMetaLinkData(metaLinkData, assembly, ""); } catch (Exception ex) { throw new Exception("Error during collecting metalink data." + ex.Message); } DateTime t1 = DateTime.Now; // call elaborator and expand the references // Please note that elaborator will change "currentobj"'s internal structure Type t = Type.GetTypeFromProgID("MGA.Interpreter.CyPhyElaborateCS"); if (t == null) { throw new NullReferenceException("Cannot find MGA.Interpreter.CyPhyElaborate. Is it registered?"); } IMgaComponentEx elaborator = Activator.CreateInstance(t) as IMgaComponentEx; elaborator.Initialize(project); elaborator.ComponentParameter["automated_expand"] = "true"; elaborator.ComponentParameter["console_messages"] = "off"; elaborator.InvokeEx(project, currentobj, (MgaFCOs)Activator.CreateInstance(Type.GetTypeFromProgID("Mga.MgaFCOs")), param); TimeSpan ts = DateTime.Now - t1; GMEConsole.Info.WriteLine("Elaborator was running for " + ts.TotalMilliseconds + "ms"); CyPhy2CAD_CSharp.CyPhy2CADSettings cadSettings = new CyPhy2CAD_CSharp.CyPhy2CADSettings(); cadSettings.AuxiliaryDirectory = syncedCompData.AuxDir; cadSettings.OutputDirectory = syncedCompData.WorkingDir; cadSettings.MetaLink = true; CyPhy2CAD_CSharp.Logger.Instance.Reset(); CyPhy2CAD_CSharp.CADFlatDataCreator datacreator = new CyPhy2CAD_CSharp.CADFlatDataCreator(cadSettings.OutputDirectory, GetProjectDir(), metalink: true); datacreator.CreateFlatData(assembly); CyPhy2CAD_CSharp.DataRep.CADContainer cadcontainer = datacreator.CreateCADDataContainer(assembly.Guid.ToString(), CyPhy2CAD_CSharp.UtilityHelpers.CleanString2(assembly.Name)); cadcontainer.MergeAssemblies(); // IF this is an empty design, add a root component if (cadcontainer.assemblies.Count == 0) { cadcontainer.AddRootComponent(assembly); } cadcontainer.InjectMetaLinkData(assembly, metaLinkData); CyPhy2CAD_CSharp.TestBenchModel.TestBenchBase testbenchbase = new CyPhy2CAD_CSharp.TestBenchModel.TestBenchBase(cadSettings, cadSettings.OutputDirectory, GetProjectDir()); testbenchbase.cadDataContainer = cadcontainer; testbenchbase.CollectDirectories(); testbenchbase.GenerateCADXMLOutput(); testbenchbase.GenerateScriptFiles(); bool stop = CyPhy2CAD_CSharp.Logger.Instance.ErrorCnt > 0; CyPhy2CAD_CSharp.Logger.Instance.DumpLog(GMEConsole, ""); if (stop) { throw new Exception("There were errors during processing the model. Please look at the console log and fix these."); } string CADAssembly = File.ReadAllText(Path.Combine(cadSettings.OutputDirectory, CyPhy2CAD_CSharp.TestBenchModel.TestBenchBase.CADAssemblyFile)); string designId = new Guid(currentobj.GetGuidDisp()).ToString("D"); SendInterest(null, syncedCompData.InstanceId); foreach (var comp in comps) { // SendInterest(null, ComponentTopic, comp.Attributes.AVMID); } SaveCadAssemblyXml(CADAssembly, designId); } finally { project.Preferences = origPrefs; } }