Пример #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);
                    }
                }
            }
        }
Пример #2
0
 // Callback invoked if the executable startup has failed
 private void ExeStartupFailed(string stderr, string logfilePath)
 {
     // Unhighlight tree and remove item from synced components
     if (LastStartedInstance != null)
     {
         try
         {
             addon.Project.BeginTransactionInNewTerr();
             ISIS.GME.Common.Interfaces.Base model = CyphyMetaLinkUtils.GetComponentAssemblyByGuid(addon.Project, LastStartedInstance.Id);
             if (model == null)
             {
                 model = CyphyMetaLinkUtils.GetComponentByAvmId(addon.Project, LastStartedInstance.Id);
                 SendDisinterest(true, LastStartedInstance.InstanceId);
             }
             if (model != null)
             {
                 AssemblyID = null;
                 HighlightInTree(model, 0);
                 SendDisinterest(true, LastStartedInstance.InstanceId);
             }
             syncedComponents.Remove(LastStartedInstance.Id);
             // FIXME designIdToCadAssemblyXml.Remove(
         }
         finally
         {
             addon.Project.AbortTransaction();
         }
     }
     LastStartedInstance = null;
     ShowStartupDialog(false);
 }
Пример #3
0
        private void VerifyCADResources(CyPhyML.ComponentAssembly assembly, List <string> errorList)
        {
            var componentlist = CyphyMetaLinkUtils.CollectComponentsRecursive(assembly);

            foreach (var component in componentlist)
            {
                VerifyCADResources(component, errorList);
            }
        }
Пример #4
0
        private Edit CreateComponentEditMessage(string instanceId, CyPhyML.Component component, CyPhyML.CADModel cadModel)
        {
            string cadModelRelativePath = "";

            if (false == cadModel.TryGetResourcePath(out cadModelRelativePath) || cadModelRelativePath == "")
            {
                // TODO log
                //return null;
            }

            var message = new Edit()
            {
                editMode = MetaLinkProtobuf.Edit.EditMode.POST,
                guid     = Guid.NewGuid().ToString(),
                //sequence = 0,
            };

            message.origin.Add(GMEOrigin);
            message.topic.Add(instanceId);
            edu.vanderbilt.isis.meta.Action action = new edu.vanderbilt.isis.meta.Action();
            message.actions.Add(action);
            action.actionMode = MetaLinkProtobuf.Action.ActionMode.UPDATE_CAD_COMPONENT;
            action.subjectID  = component.Attributes.AVMID;
            action.environment.Add(new edu.vanderbilt.isis.meta.Environment()
            {
                name = SearchPathStr,
            });
            AddSearchPathToEnvironment(component, action.environment[0]);
            if (cadModelRelativePath.Length != 0)
            {
                action.payload = new Payload();
                CADComponentType cadComponent = new CADComponentType()
                {
                    AvmComponentID = component.Attributes.AVMID,
                    CADModelID     = CyphyMetaLinkUtils.GetResourceID(cadModel),             // Using CADModelID to transport this information (the resource id)
                    Name           = Path.GetFileNameWithoutExtension(cadModelRelativePath), // the partial creo file name (less .prt or .asm)
                    Type           = (Path.GetExtension(cadModelRelativePath).EndsWith(".prt") || Path.GetExtension(cadModelRelativePath).EndsWith(".PRT")) ? "PART" : "ASSEMBLY"
                };
                foreach (var connector in component.Children.ConnectorCollection)
                {
                    cadComponent.Connectors.Add(new ConnectorType()
                    {
                        ID = connector.Guid.ToString(), DisplayName = connector.Name
                    });
                }
                foreach (var datum in cadModel.Children.CADDatumCollection)
                {
                    cadComponent.Datums.Add(new ConnectorDatumType()
                    {
                        ID = datum.Attributes.DatumName, DisplayName = datum.Name
                    });
                }
                action.payload.components.Add(cadComponent);
            }
            return(message);
        }
Пример #5
0
 private void ConnectionClosed(Exception e)
 {
     SyncControl.BeginInvoke((System.Action) delegate
     {
         if (addon != null)
         {
             GMEConsole console = GMEConsole.CreateFromProject(addon.Project);
             console.Error.WriteLine("Connection to MetaLink lost.");
             //componentEditMessages.Clear();
             interests.Clear();
             AssemblyID = null;
             CloseMetaLinkBridge();
             if (console.gme != null)
             {
                 // Marshal.FinalReleaseComObject(console.gme); is this needed?
             }
             Enable(false);
             if (syncedComponents.Count > 0)
             {
                 bool inTx = ((addon.Project.ProjectStatus & 8) != 0);
                 if (inTx == false)
                 {
                     addon.Project.BeginTransactionInNewTerr();
                 }
                 try
                 {
                     foreach (string id in syncedComponents.Keys)
                     {
                         ISIS.GME.Common.Interfaces.Base model = CyphyMetaLinkUtils.GetComponentAssemblyByGuid(addon.Project, id);
                         if (model == null)
                         {
                             model = CyphyMetaLinkUtils.GetComponentByAvmId(addon.Project, id);
                         }
                         if (model != null)
                         {
                             HighlightInTree(model, 0);
                         }
                     }
                 }
                 finally
                 {
                     if (inTx == false)
                     {
                         addon.Project.AbortTransaction();
                     }
                 }
                 syncedComponents.Clear();
                 designIdToCadAssemblyXml.Clear();
             }
         }
     });
 }
Пример #6
0
        private void VerifyCADResources(CyPhyML.Component component, List <string> errorList)
        {
            string comppath = component.GetDirectoryPath(ComponentLibraryManager.PathConvention.ABSOLUTE);

            foreach (var res in component.Children.ResourceCollection)
            {
                if (CyphyMetaLinkUtils.IsCADResource(res))
                {
                    string   fullpath = Path.Combine(comppath, res.Attributes.Path);
                    string[] files    = Directory.GetFiles(Path.GetDirectoryName(fullpath), Path.GetFileName(fullpath) + "*");
                    if (files.Length == 0)
                    {
                        errorList.Add("Missing CAD resource file: " + fullpath);
                    }
                }
            }
        }
Пример #7
0
        public void StartEditingComponent(CyPhyML.Component component, MgaFCO cadModel, bool createNewComponent)
        {
            addon.Project.BeginTransactionInNewTerr();
            string avmid = component.Attributes.AVMID;
            SyncedComponentData syncedCompData;

            try
            {
                if (syncedComponents.TryGetValue(avmid, out syncedCompData))
                {
                    GMEConsole.Error.WriteLine("This component is already open in Creo, AVMID: " + component.Attributes.AVMID);
                    return;
                }
                else
                {
                }
                List <string> errorList = new List <string>();
                VerifyCADResources(component, errorList);
                if (errorList.Count != 0)
                {
                    foreach (var err in errorList)
                    {
                        GMEConsole.Error.WriteLine(err);
                    }
                    System.Windows.Forms.MessageBox.Show("Errors found in component. Can't open in Meta-Link. Please check the log.");
                    return;
                }
                CyPhyML.Component newComponent = CyphyMetaLinkUtils.CopyComponent(component, createNewComponent);
                avmid = newComponent.Attributes.AVMID;
                if (createNewComponent && GMEConsole.gme != null)
                {
                    // META-2109: open copy in model editor, select in Browser
                    try
                    {
                        string newComponentID   = newComponent.ID;
                        bool   closeModelEditor = false;
                        if (GMEConsole.gme.OleIt != null)
                        {
                            closeModelEditor = GMEConsole.gme.OleIt.MgaModel.ID == component.ID;
                        }
                        SyncControl.BeginInvoke((System.Action) delegate
                        {
                            try
                            {
                                if (closeModelEditor)
                                {
                                    GMEConsole.gme.OleIt.Close();
                                }
                                GMEConsole.gme.ShowFCO((MgaFCO)newComponent.Impl, false);
                                object browser = GetBrowser();
                                if (browser != null)
                                {
                                    browser.GetType().InvokeMember("FocusItem", System.Reflection.BindingFlags.InvokeMethod, null, browser, new object[] { newComponentID });
                                }
                            }
                            catch (Exception) { }
                        });
                    }
                    catch (Exception) { }
                }
                HighlightInTree(newComponent, 1);
            }
            finally
            {
                addon.Project.CommitTransaction();
            }

            syncedCompData = new SyncedComponentData()
            {
                Type       = SyncedComponentData.EditType.Component,
                InstanceId = (IdCounter++).ToString(),
                Id         = avmid
            };
            syncedComponents.Add(avmid, syncedCompData);

            SendInterest(StartComponentEditAction, syncedCompData.InstanceId);
        }
Пример #8
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;
            }
        }