Exemplo n.º 1
0
        private void PrepareMetaLinkData(CyPhy2CAD_CSharp.MetaLinkData data, CyPhyML.ComponentAssembly assembly, String prefix)
        {
            Dictionary <string, CyPhyML.ComponentRef> instanceguids = new Dictionary <string, CyPhyML.ComponentRef>();

            CyphyMetaLinkUtils.CollectInstanceGUIDs(instanceguids, "", assembly);
            if (assembly.Children.ComponentCollection.Any())
            {
                GMEConsole.Warning.WriteLine("Assembly contains components, Meta-Link may not work properly on this model.");
            }
            //List<CyPhyML.ComponentRef> comps = CyphyMetaLinkUtils.CollectComponentRefsRecursive(assembly);
            foreach (var item in instanceguids)
            {
                CyPhy2CAD_CSharp.MetaLinkData.Component mcomp = new CyPhy2CAD_CSharp.MetaLinkData.Component();
                mcomp.ID    = item.Key;
                mcomp.AvmID = (item.Value.AllReferred as CyPhyML.Component).Attributes.AVMID;
                data.Components.Add(mcomp);
                foreach (CyPhyML.Connector conn in (item.Value.AllReferred as CyPhyML.Component).Children.ConnectorCollection)
                {
                    CyPhy2CAD_CSharp.MetaLinkData.Connector mconn = new CyPhy2CAD_CSharp.MetaLinkData.Connector();
                    mconn.ID          = conn.Guid.ToString();
                    mconn.DisplayName = conn.Name;
                    mcomp.Connectors.Add(mconn);
                    foreach (CyPhyML.CADDatum d in conn.Children.CADDatumCollection)
                    {
                        CyPhy2CAD_CSharp.MetaLinkData.Datum md = new CyPhy2CAD_CSharp.MetaLinkData.Datum();
                        md.ID          = d.Guid.ToString();
                        md.DisplayName = d.Name;
                        mconn.Datums.Add(md);
                    }
                }
            }
        }
Exemplo n.º 2
0
        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;
            }
        }
        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;
            }
        }