Beispiel #1
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);
 }
Beispiel #2
0
        public void StartCreoEmpyMode()
        {
            string tmpid = Guid.NewGuid().ToString();
            SyncedComponentData cdata = new SyncedComponentData()
            {
                Type       = SyncedComponentData.EditType.Empty,
                WorkingDir = GetProjectDir(),
                InstanceId = (IdCounter++).ToString(),
                Id         = tmpid
            };

            syncedComponents.Add(tmpid, cdata);
            StartAssemblyExe(CreoOpenMode.OPEN_EMPTY, tmpid, false, "");
            SendInterest(null, cdata.InstanceId);
        }
        private void ProcessEditMessage(MetaLinkProtobuf.Edit message)
        {
            if (GMEConsole == null)
            {
                GMEConsole = GMEConsole.CreateFromProject(addon.Project);
            }
            // This is the first message from Creo, hide the Startup Dialog
            if (StartupDialog.Visible && message.origin.Contains(CadAssemblyOrigin) &&
                message.editMode == Edit.EditMode.INTEREST && message.topic.Count == 2 &&
                message.topic[1] == LastStartedGuid)
            {
                ShowStartupDialog(false);
            }
            // Drop a warning message to the console if it's a suspicious response
            if (message.actions.Count > 0)
            {
                foreach (var action in message.actions)
                {
                    foreach (var notice in action.notices)
                    {
                        if (notice.noticeMode == Notice.NoticeMode.DONE)
                        {
                            GMEConsole.Info.WriteLine("Done response from Meta-Link: " + notice.msg);
                        }
                        else if (notice.noticeMode == Notice.NoticeMode.ACK)
                        {
                            GMEConsole.Info.WriteLine("Ack response from Meta-Link: " + notice.msg);
                        }
                        else if (notice.noticeMode == Notice.NoticeMode.WARN)
                        {
                            GMEConsole.Warning.WriteLine("Warn response from Meta-Link: " + notice.msg);
                        }
                    }
                }
            }
            if (message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.INTEREST, MetaLinkProtobuf.Edit.EditMode.NOTICE }))
            {

                // Start Creo in design editing mode
                if (message.topic.Count == 2 && message.topic[0] == CadAssemblyTopic
                    && message.notices.Count == 1 && message.notices[0].noticeMode == Notice.NoticeMode.ACK)
                {
                    // message.topic[1] is the assembly GUID
                    string assemblyGuid = Guid.Parse(message.topic[1]).ToString();

                    string exeparams = "";
                    addon.Project.BeginTransactionInNewTerr();
                    try
                    {
                        exeparams = GetStartupParams(assemblyGuid);
                    }
                    finally
                    {
                        addon.Project.AbortTransaction();
                    }

                    StartAssemblyExe(CreoOpenMode.OPEN_COMPONENT, assemblyGuid, true, exeparams);
                }

                // Start Creo in AVM component editing mode
                if (message.topic.Count >= 2 && message.topic[0] == ComponentUpdateTopic
                    && message.notices.Count == 1 && message.notices[0].noticeMode == Notice.NoticeMode.ACK)
                {
                    Action<MetaLinkProtobuf.Edit> action;
                    if (noticeActions.TryGetValue(message.guid, out action))
                    {
                        noticeActions.Remove(message.guid);
                        action(message);
                    }
                }
                
            }

            if (message.mode.Count > 0 && message.mode.Last() == MetaLinkProtobuf.Edit.EditMode.NOTICE)
            {
                foreach (var notice in message.actions.SelectMany(a => a.notices)
                    .Where(n => n.noticeMode == Notice.NoticeMode.FAULT || n.noticeMode == Notice.NoticeMode.FAIL ||
                            n.noticeMode == Notice.NoticeMode.REJECT))
                {
                    GMEConsole.Error.WriteLine("Meta-Link error: " + notice.msg);
                }
            }


            if (message.origin.Contains(GMEOrigin)) // ignore messages from ourselves
                return;

            // DISINTEREST: The user closed Creo (component)
            if (message.topic.Count == 2 && (message.topic[0] == ComponentUpdateTopic || message.topic[0] == CadPassiveTopic) &&
                message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.DISINTEREST }))
            {
                if (message.topic[0] == ComponentUpdateTopic)
                {
                    String componentAVMID = message.topic[1];
                    //componentEditMessages.Remove(componentAVMID);
                    syncedComponents.Remove(componentAVMID);
                    SendDisinterest(ComponentUpdateTopic, componentAVMID);
                    addon.Project.BeginTransactionInNewTerr();
                    try
                    {
                        var component = CyphyMetaLinkUtils.GetComponentByAvmId(addon.Project, componentAVMID);
                        if (component != null)
                        {
                            HighlightInTree(component, 0);
                        }
                    }
                    finally
                    {
                        addon.Project.AbortTransaction();
                    }
                }
                else
                {
                    syncedComponents.Remove(""); // CAD has been started in an empty mode
                }
            }

            // DISINTEREST: The user closed Creo (design)
            if (message.topic.Count == 2 && message.topic[0] == CadAssemblyTopic &&
                message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.DISINTEREST }))
            {
                Guid componentAssemblyID = Guid.Parse(message.topic[1]);
                /*try{
                    Directory.Delete(syncedComponents[componentAssemblyID.ToString()].WorkingDir);
                } catch (Exception e)
                {
                    GMEConsole.Warning.WriteLine("Unable to delete working directory: " + e.Message);
                }*/
                syncedComponents.Remove(componentAssemblyID.ToString());
                SendDisinterest(CadAssemblyTopic, message.topic[1]);
                addon.Project.BeginTransactionInNewTerr();
                try
                {
                    CyPhyML.ComponentAssembly assembly = CyphyMetaLinkUtils.GetComponentAssemblyByGuid(addon.Project, AssemblyID);
                    if (assembly != null)
                    {
                        HighlightInTree(assembly, 0);
                    }
                }
                finally
                {
                    addon.Project.AbortTransaction();
                }
                AssemblyID = null;
            }

            if (message.topic.Count == 2 && message.topic[0] == ComponentAnalysisPointTopic &&
                message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.POST }))
            {
                foreach (var action in message.actions)
                {
                    ProcessAnalysisPointMessage(message.topic[1], action);
                }
            }                
                

            // Design edit message (insert/select/discard) 
            if (message.topic.Count == 2 && message.topic[0] == CadAssemblyTopic &&
                message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.POST }))
            {
                foreach (var action in message.actions)
                {
                    if (action.actionMode == MetaLinkProtobuf.Action.ActionMode.INSERT && action.payload != null)
                    {
                        // Add an AVM component
                        ProcessEditInsert(message.topic[1], action);
                    }
                    if (action.actionMode == MetaLinkProtobuf.Action.ActionMode.SELECT && action.payload != null)
                    {
                        // Select a component ref
                        ProcessEditSelect(message.topic[1], action);
                    }
                    if (action.actionMode == MetaLinkProtobuf.Action.ActionMode.DISCARD)
                    {
                        // Remove a component ref
                        ProcessEditDiscard(message.topic[1], action);
                    }
                }
            }

            // Re-sync
            if (message.topic.Count == 2 && message.topic[0] == ResyncTopic &&
                message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.POST }))
            {
                ProcessResync(message.topic[1]);
            }
            

            if (message.topic.Count == 2 && message.topic[0] == CadAssemblyTopic &&
                message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.INTEREST }))
            {
                string designId = message.topic[1];
                string xml;
                if (designIdToCadAssemblyXml.TryGetValue(designId, out xml))
                {
                    addon.Project.BeginTransactionInNewTerr();
                    try
                    {
                        SendCadAssemblyXml(xml, designId, false);
                        SaveCadAssemblyXml(xml, designId);
                    }
                    finally
                    {
                        addon.Project.AbortTransaction();
                    }
                }
                else
                {
                    GMEConsole.Warning.WriteLine("MetaLink: unknown assembly " + designId);
                }
            }

            // Request component list message from Creo
            if (message.topic.Count > 0 && message.topic[0] == ComponentManifestTopic &&
                message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.INTEREST }))
            {
                addon.Project.BeginTransactionInNewTerr();
                try
                {
                    SendComponentManifest();
                }
                finally
                {
                    addon.Project.AbortTransaction();
                }
            }

            // Update AVM Component with the information from Creo
            if (message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.POST }) &&
                message.topic.Count >= 2 && message.topic[0] == ComponentUpdateTopic && message.actions.Count == 1 &&
                message.actions[0].alien != null && message.actions[0].alien.encodingMode == Alien.EncodingMode.XML)
            {
                string componentId = message.topic[1];
                string component_xml = Encoding.UTF8.GetString(message.actions[0].alien.encoded);
                ProcessAVMComponentUpdate(componentId, component_xml);
            }

            // Some type of AVM component update (INSERT, DISCARD, etc.)
            if (message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.POST }) &&
                message.topic.Count >= 2 && message.topic[0] == ComponentUpdateTopic && message.actions.Count >= 1)
            {
                string componentId = message.topic[1];
                foreach (var action in message.actions)
                {
                    if (action.actionMode == MetaLinkProtobuf.Action.ActionMode.INSERT && action.payload != null)
                    {
                        ProcessAVMComponentInsert(message.topic[1], action);
                    }
                }
                
            }
            // Create new AVM component
            if (message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.POST }) &&
                message.topic.Count <= 2 && message.topic[0] == ComponentCreateTopic && message.actions.Count == 1 &&
                message.actions[0].alien != null && message.actions[0].alien.encodingMode == Alien.EncodingMode.XML)
            {
                string component_xml = Encoding.UTF8.GetString(message.actions[0].alien.encoded);
                ProcessAVMComponentCreate(component_xml, message.topic.Count>1?message.topic[1]:null);
            }

            // Connect component within a design
            if (message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.POST }) &&
                message.topic.Count >= 1 && message.topic[0] == ConnectTopic)
            {
                try
                {
                    string conn1 = message.actions[0].payload.components[1].ComponentID.Substring(0, message.actions[0].payload.components[1].ComponentID.IndexOf('_'));
                    string ref1 = message.actions[0].payload.components[1].ComponentID.Substring(message.actions[0].payload.components[1].ComponentID.IndexOf('_')+1);
                    string conn2 = message.actions[0].payload.components[2].ComponentID.Substring(0, message.actions[0].payload.components[2].ComponentID.IndexOf('_'));
                    string ref2 = message.actions[0].payload.components[2].ComponentID.Substring(message.actions[0].payload.components[2].ComponentID.IndexOf('_') + 1);
                    ProcessConnect(message.actions[0].payload.components[0].ComponentID, conn1, ref1, conn2, ref2);
                }
                catch (Exception ex)
                {
                    GMEConsole.Warning.WriteLine("Error during processing " + ConnectTopic + " message: " + ex.Message);
                }
            }

            // Send AVM component update to Creo
            if (message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.INTEREST }) &&
                message.topic.Count >= 2 && message.topic[0] == ComponentUpdateTopic)
            {
                addon.Project.BeginTransactionInNewTerr();
                try
                {
                    CyPhyML.Component avmcomp = CyphyMetaLinkUtils.FindAVMComponent(addon.Project, message.topic[1], null);

                    if (avmcomp == null)
                        throw new Exception(String.Format("Can't find AVM component to open with id {0}.", message.topic[1]));

                    var outmessage = CreateComponentEditMessage(avmcomp, CyphyMetaLinkUtils.FindCADModelObject(avmcomp));

                    if (outmessage != null)
                    {
                        SendInterest(null, ResyncTopic, message.topic[1]);
                        bridgeClient.SendToMetaLinkBridge(outmessage);
                    }
                    else
                    {
                        GMEConsole.Warning.WriteLine("MetaLink: don't know about component " + message.topic[1]);
                    }
                    // This is a recently created component
                    if (!syncedComponents.ContainsKey(avmcomp.Attributes.AVMID))
                    {
                        SyncedComponentData syncedCompData = new SyncedComponentData()
                        {
                            Type = SyncedComponentData.EditType.Component
                        };
                        syncedComponents.Add(avmcomp.Attributes.AVMID, syncedCompData);
                        HighlightInTree(avmcomp, 1);
                    }
                }
                finally
                {
                    addon.Project.AbortTransaction();
                }
            }
        }
Beispiel #4
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);
        }
        /// <summary>
        /// The main dispatch handler for incoming messages
        /// </summary>
        /// <param name="message"></param>
        private void ProcessEditMessage(MetaLinkProtobuf.Edit message)
        {
            if (GMEConsole == null)
            {
                GMEConsole = GMEConsole.CreateFromProject(addon.Project);
            }
            // This is the first message from Creo, hide the CAD Startup Dialog
            if (StartupDialog.Visible && message.origin.Contains(CadAssemblyOrigin) &&
                message.editMode == Edit.EditMode.INTEREST && message.topic.Count == 1 &&
                message.topic[0] == LastStartedInstance.InstanceId)
            {
                ShowStartupDialog(false);
            }
            // Drop a message to the console with the acknowledgement of a previous message
            if (message.actions.Count > 0)
            {
                foreach (var action in message.actions)
                {
                    foreach (var notice in action.notices)
                    {
                        if (notice.noticeMode == Notice.NoticeMode.DONE)
                        {
                            GMEConsole.Info.WriteLine("Done response from Meta-Link: " + notice.msg);
                        }
                        else if (notice.noticeMode == Notice.NoticeMode.ACK)
                        {
                            GMEConsole.Info.WriteLine("Ack response from Meta-Link: " + notice.msg);
                        }
                        else if (notice.noticeMode == Notice.NoticeMode.WARN)
                        {
                            GMEConsole.Warning.WriteLine("Warn response from Meta-Link: " + notice.msg);
                        }
                    }
                }
            }
            if (!TestMode && message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.INTEREST, MetaLinkProtobuf.Edit.EditMode.NOTICE }))
            {
                // Start Creo in design editing mode
                if (message.notices.Count == 1 && message.notices[0].noticeMode == Notice.NoticeMode.ACK)
                {
                    SyncedComponentData cdata = GetSyncedCompDataByInstanceId(message.topic[0]);
                    if (cdata == null)
                    {
                        GMEConsole.Error.WriteLine("Fatal error: can't find component data for synced component: " + message.topic[0]);
                    }
                    else if (cdata.Type == SyncedComponentData.EditType.Design)
                    {
                        // message.topic[1] is the assembly GUID
                        string assemblyGuid = Guid.Parse(cdata.Id).ToString();

                        string exeparams = "";
                        addon.Project.BeginTransactionInNewTerr();
                        try
                        {
                            exeparams = GetStartupParams(assemblyGuid);
                        }
                        finally
                        {
                            addon.Project.AbortTransaction();
                        }

                        StartAssemblyExe(CreoOpenMode.OPEN_COMPONENT, assemblyGuid, true, exeparams);
                    }
                    else
                    {
                        Action<MetaLinkProtobuf.Edit> action;
                        if (noticeActions.TryGetValue(message.guid, out action))
                        {
                            noticeActions.Remove(message.guid);
                            action(message);
                        }
                    }
                }
            }

            // Write message to console if it's an error response
            if (message.mode.Count > 0 && message.mode.Last() == MetaLinkProtobuf.Edit.EditMode.NOTICE)
            {
                foreach (var notice in message.actions.SelectMany(a => a.notices)
                    .Where(n => n.noticeMode == Notice.NoticeMode.FAULT || n.noticeMode == Notice.NoticeMode.FAIL))
                {
                    GMEConsole.Error.WriteLine("Meta-Link error: " + notice.msg);
                }
            }

            // ignore messages from ourselves
            if (message.origin.Contains(GMEOrigin))
            {
                return;
            }

            // DISINTEREST: The user closed Creo (component)
            if (message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.DISINTEREST }))
            {
                SyncedComponentData cdata = GetSyncedCompDataByInstanceId(message.topic[0]);
                if (cdata.Type == SyncedComponentData.EditType.Component)
                {
                    String componentAVMID = cdata.Id;
                    //componentEditMessages.Remove(componentAVMID);
                    syncedComponents.Remove(componentAVMID);
                    SendDisinterest(false, cdata.InstanceId);
                    addon.Project.BeginTransactionInNewTerr();
                    try
                    {
                        var component = CyphyMetaLinkUtils.GetComponentByAvmId(addon.Project, componentAVMID);
                        if (component != null)
                        {
                            HighlightInTree(component, 0);
                        }
                    }
                    finally
                    {
                        addon.Project.AbortTransaction();
                    }
                }
                else if (cdata.Type == SyncedComponentData.EditType.Empty)
                {
                    syncedComponents.Remove(""); // CAD has been started in an empty mode
                }

                else
                {
                    Guid componentAssemblyID = Guid.Parse(cdata.Id);

                    syncedComponents.Remove(componentAssemblyID.ToString());
                    SendDisinterest(true, cdata.InstanceId);
                    addon.Project.BeginTransactionInNewTerr();
                    try
                    {
                        CyPhyML.ComponentAssembly assembly = CyphyMetaLinkUtils.GetComponentAssemblyByGuid(addon.Project, AssemblyID);
                        if (assembly != null)
                        {
                            HighlightInTree(assembly, 0);
                        }
                    }
                    finally
                    {
                        addon.Project.AbortTransaction();
                    }
                    AssemblyID = null;
                }
            }
            else
                // Create analysis point
                if (message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.POST }) &&
                    message.actions.Count >= 1 && message.actions[0].actionMode == MetaLinkProtobuf.Action.ActionMode.CREATE_ANALYSIS_POINTS)
            {
                foreach (var action in message.actions)
                {
                    ProcessAnalysisPointMessage(action.subjectID, action);
                }
            }
            else
                // Design edit message (insert/select/discard) 
                if (message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.POST }) && message.actions.Count >= 1 &&
                    (message.actions[0].actionMode == MetaLinkProtobuf.Action.ActionMode.ADD_COMPONENT_TO_CYPHY_DESIGN ||
                    message.actions[0].actionMode == MetaLinkProtobuf.Action.ActionMode.SELECT_CYPHY_COMPONENT ||
                    message.actions[0].actionMode == MetaLinkProtobuf.Action.ActionMode.REMOVE_CYPHY_DESIGN_COMPONENT))
            {
                foreach (var action in message.actions)
                {
                    if (action.actionMode == MetaLinkProtobuf.Action.ActionMode.ADD_COMPONENT_TO_CYPHY_DESIGN && action.payload != null)
                    {
                        // Add an AVM component
                        ProcessEditInsert(action.subjectID, action);
                    }
                    else if (action.actionMode == MetaLinkProtobuf.Action.ActionMode.SELECT_CYPHY_COMPONENT && action.payload != null)
                    {
                        // Select a component
                        ProcessEditSelect(action.subjectID, action);
                    }
                    else if (action.actionMode == MetaLinkProtobuf.Action.ActionMode.REMOVE_CYPHY_DESIGN_COMPONENT)
                    {
                        // Remove a component
                        ProcessEditDiscard(action.subjectID, action);
                    }
                    else
                    {
                        GMEConsole.Warning.WriteLine("Unknown action: " + message.ToString());
                    }
                }
            }
            else
                // Re-sync
                if (message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.POST }) &&
                    message.actions.Count >= 1 && message.actions[0].actionMode == MetaLinkProtobuf.Action.ActionMode.RESYNC)
            {
                ProcessResync(message.actions[0].subjectID);
            }
            else
                // Send assembly/component info to the client
                if (message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.INTEREST }))
            {
                SyncedComponentData cdata = GetSyncedCompDataByInstanceId(message.topic[0]);
                if (cdata.Type == SyncedComponentData.EditType.Component)
                {
                    addon.Project.BeginTransactionInNewTerr();
                    try
                    {
                        CyPhyML.Component avmcomp = CyphyMetaLinkUtils.FindAVMComponent(addon.Project, cdata.Id, null);

                        if (avmcomp == null)
                        {
                            throw new Exception(String.Format("Can't find AVM component to open with id {0}.", cdata.Id));
                        }

                        var outmessage = CreateComponentEditMessage(cdata.InstanceId, avmcomp, CyphyMetaLinkUtils.FindCADModelObject(avmcomp));

                        if (outmessage != null)
                        {
                            //SendInterest(null, ResyncTopic, message.topic[1]);
                            bridgeClient.SendToMetaLinkBridge(outmessage);
                        }
                        else
                        {
                            GMEConsole.Warning.WriteLine("MetaLink: don't know about component " + cdata.Id);
                        }
                        // This is a recently created component
                        if (!syncedComponents.ContainsKey(avmcomp.Attributes.AVMID))
                        {
                            SyncedComponentData syncedCompData = new SyncedComponentData()
                            {
                                Type = SyncedComponentData.EditType.Component,
                                Id = avmcomp.Attributes.AVMID
                            };
                            syncedComponents.Add(avmcomp.Attributes.AVMID, syncedCompData);
                            HighlightInTree(avmcomp, 1);
                        }
                    }
                    finally
                    {
                        addon.Project.AbortTransaction();
                    }
                }
                else
                {
                    string designId = cdata.Id;
                    string xml;
                    if (designIdToCadAssemblyXml.TryGetValue(designId, out xml))
                    {
                        addon.Project.BeginTransactionInNewTerr();
                        try
                        {
                            SendCadAssemblyXml(message.topic[0], xml, designId, false);
                            SaveCadAssemblyXml(xml, designId);
                        }
                        finally
                        {
                            addon.Project.AbortTransaction();
                        }
                    }
                    else
                    {
                        GMEConsole.Warning.WriteLine("MetaLink: unknown assembly " + designId);
                    }
                }
            }
            else
                // Request component list message from Creo
                if (message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.POST }) &&
                    message.actions.Count >= 1 && message.actions[0].actionMode == MetaLinkProtobuf.Action.ActionMode.REQUEST_COMPONENT_LIST)
            {
                addon.Project.BeginTransactionInNewTerr();
                try
                {
                    SendComponentList(message.topic[0]);
                }
                finally
                {
                    addon.Project.AbortTransaction();
                }
            }
            else
                // Update AVM Component with the information from Creo
                if (message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.POST }) &&
                    message.actions.Count == 1 && message.actions[0].actionMode == MetaLinkProtobuf.Action.ActionMode.UPDATE_CYPHY_COMPONENT &&
                    message.actions[0].alien != null && message.actions[0].alien.encodingMode == Alien.EncodingMode.XML)
            {
                string componentId = message.actions[0].subjectID;
                string component_xml = Encoding.UTF8.GetString(message.actions[0].alien.encoded);
                ProcessAVMComponentUpdate(componentId, component_xml);
            }
            else
                // Create a connector in component
                if (message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.POST }) &&
                    message.actions.Count >= 1 && message.actions[0].actionMode == MetaLinkProtobuf.Action.ActionMode.ADD_CONNECTOR_TO_COMPONENT)
            {
                string componentId = message.actions[0].subjectID;
                ProcessInsertComponentConnector(componentId, message.actions[0]);
            }
            else
                // Create new CyPhy component
                if (message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.POST }) &&
                    message.actions.Count == 1 && message.actions[0].actionMode == MetaLinkProtobuf.Action.ActionMode.CREATE_CYPHY_COMPONENT &&
                    message.actions[0].alien != null && message.actions[0].alien.encodingMode == Alien.EncodingMode.XML)
            {
                string component_xml = Encoding.UTF8.GetString(message.actions[0].alien.encoded);
                ProcessAVMComponentCreate(component_xml, message.topic[0]);
            }
            else
                // Connect component within a design
                if (message.mode.SequenceEqual(new Edit.EditMode[] { Edit.EditMode.POST }) &&
                    message.actions.Count >= 1 && message.actions[0].actionMode == MetaLinkProtobuf.Action.ActionMode.CONNECT_COMPONENTS_CYPHY)
            {
                try
                {
                    string assemblyID = message.actions[0].payload.components[0].ComponentID;
                    string conn1 = message.actions[0].payload.components[1].ComponentID.Substring(0, message.actions[0].payload.components[1].ComponentID.IndexOf('_'));
                    string ref1 = message.actions[0].payload.components[1].ComponentID.Substring(message.actions[0].payload.components[1].ComponentID.IndexOf('_') + 1);
                    string conn2 = message.actions[0].payload.components[2].ComponentID.Substring(0, message.actions[0].payload.components[2].ComponentID.IndexOf('_'));
                    string ref2 = message.actions[0].payload.components[2].ComponentID.Substring(message.actions[0].payload.components[2].ComponentID.IndexOf('_') + 1);
                    ProcessConnect(assemblyID, conn1, ref1, conn2, ref2);
                }
                catch (Exception ex)
                {
                    GMEConsole.Warning.WriteLine("Error during processing connect message: " + ex.Message);
                }
            }
            else
            {
                GMEConsole.Warning.WriteLine("Message left unprocessed: " + message.ToString());
            }
        }
Beispiel #6
0
        private void StartAssemblyExe(CreoOpenMode mode, string guid, bool isComponentAssembly, string exeparams, string workingDir = null)
        {
            SyncedComponentData syncedCompData = null;

            if (!syncedComponents.TryGetValue(guid, out syncedCompData))
            {
                GMEConsole.Warning.WriteLine("StartAssemblyExe(): Can't find guid " + guid);
                return;
            }

            string createAssemblyExe = Path.Combine(META.VersionInfo.MetaPath, "bin", "CAD", "Creo", "bin", "CADCreoParametricMetaLink.exe");

            if (File.Exists(createAssemblyExe) == false)
            {
                GMEConsole.Error.WriteLine(String.Format("Could not find CADCreoParametricMetaLink.exe at '{0}'", createAssemblyExe));
                return;
            }
            if (workingDir == null)
            {
                workingDir = syncedCompData.WorkingDir;
            }

            try
            {
                File.Copy(Path.Combine(GetProjectDir(), "constraintdata.xml"), Path.Combine(workingDir, "constraintdata.xml"));
            }
            catch (Exception)
            {
                //  not an issue
            }

            string logfile   = Path.Combine(GetProjectDir(), "log", Path.ChangeExtension("MetaLink_CreoCreateAssembly_" + Path.GetRandomFileName(), "log"));
            string arguments = String.Format("-v debug -g -s 127.0.0.1:{4} -w \"{0}\" -l \"{1}\" {2}, -id {3}", workingDir, logfile, exeparams, syncedCompData.InstanceId, SocketQueue.port);

            arguments += " -d " + guid;

            ProcessStartInfo info = new ProcessStartInfo()
            {
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                RedirectStandardInput  = true,
                UseShellExecute        = false,
                WindowStyle            = ProcessWindowStyle.Hidden,
                CreateNoWindow         = true,
                FileName  = createAssemblyExe,
                Arguments = arguments
                            // TODO -p ?
            };

            if (mode == CreoOpenMode.OPEN_EMPTY)
            {
                info.Arguments += " -m passive";
            }
            else if (!isComponentAssembly)
            {
                info.Arguments += " -m component";
            }
            if (Configuration.ConfigProFile.Length > 0)
            {
                info.Arguments += " -c " + Configuration.ConfigProFile;
            }
            if (!string.IsNullOrWhiteSpace(syncedCompData.AuxDir))
            {
                info.Arguments += String.Format(" -a \"{0}\"", syncedCompData.AuxDir);
            }
            Process createAssembly = new Process();

            createAssembly.StartInfo = info;
            StringBuilder stdoutData = new StringBuilder();
            StringBuilder stderrData = new StringBuilder();

            createAssembly.OutputDataReceived += (o, args) =>
            {
                lock (stdoutData)
                {
                    stdoutData.Append(args.Data);
                    stdoutData.Append(System.Environment.NewLine);
                }
            };
            createAssembly.ErrorDataReceived += (o, args) =>
            {
                lock (stderrData)
                {
                    stderrData.Append(args.Data);
                    stderrData.Append(System.Environment.NewLine);
                }
            };
            createAssembly.EnableRaisingEvents = true;
            createAssembly.Exited += (o, args) =>
            {
                lock (stderrData)
                {
                    if (GMEConsole != null && createAssembly.ExitCode != 0)
                    {
                        string message = String.Format("CADCreoParametricMetaLink exited with code {0}", createAssembly.ExitCode);
                        // CADCreoParametricMetaLink attempts to log everything in its log file
                        // Ignore stderr if it doesn't contain anything useful
                        string stdErrString = stderrData.ToString();
                        if (Regex.Match(stdErrString, "\\S").Success)
                        {
                            string errlog = "CADCreoParametricCreateAssembly_err.log";
                            using (StreamWriter writer = new StreamWriter(errlog))
                            {
                                writer.Write(stdErrString);
                                writer.Close();
                            }
                            message += String.Format(", the logfile is {0} ", errlog);
                        }
                        GMEConsole.Error.WriteLine(message);
                        SyncControl.Invoke((System.Action)(() => StartupFailureCallback(stdErrString, logfile)));
                    }
                }
            };
            LastStartedInstance = syncedCompData;
            createAssembly.Start();
            if (TestMode_CreoJobObject != IntPtr.Zero)
            {
                JobObjectPinvoke.AssignProcessToJobObject(createAssembly, TestMode_CreoJobObject);
            }
            createAssembly.BeginOutputReadLine();
            createAssembly.BeginErrorReadLine();
            createAssembly.StandardInput.Close();
            GMEConsole.Info.WriteLine("Creo is starting, the logfile is " + SmartLogger.GetGMEConsoleFileLink(logfile));
            ShowStartupDialog(true);
            // createAssembly.WaitForExit(10 * 1000);
        }
        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);
        }
 public void StartCreoEmpyMode()
 {
     string tmpid = Guid.NewGuid().ToString();
     SyncedComponentData cdata = new SyncedComponentData()
     {
         Type = SyncedComponentData.EditType.Empty,
         WorkingDir = GetProjectDir(),
         InstanceId = (IdCounter++).ToString(),
         Id = tmpid
     };
     syncedComponents.Add(tmpid, cdata);
     StartAssemblyExe(CreoOpenMode.OPEN_EMPTY, tmpid, false, "");
     SendInterest(null, cdata.InstanceId);
 }
 // Callback invoked if the executable startup has failed
 private void ExeStartupFailed()
 {
     // 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);
         }
         finally
         {
             addon.Project.AbortTransaction();
         }
     }
     LastStartedInstance = null;
     ShowStartupDialog(false);
 }
        private void StartAssemblyExe(CreoOpenMode mode, string guid, bool isComponentAssembly, string exeparams, string workingDir = null)
        {
            SyncedComponentData syncedCompData = null;
            if (!syncedComponents.TryGetValue(guid, out syncedCompData))
            {
                GMEConsole.Warning.WriteLine("StartAssemblyExe(): Can't find guid " + guid);
                return;
            }

            string proeIsisExtensionsDir = System.Environment.GetEnvironmentVariable("PROE_ISIS_EXTENSIONS");
            string createAssemblyExe = Path.Combine(proeIsisExtensionsDir ?? "", "bin", "CADCreoParametricMetaLink.exe");
            if (File.Exists(createAssemblyExe) == false)
            {
                GMEConsole.Error.WriteLine("CADCreoParametricMetaLink.exe could not be found");
                return;
            }
            if (workingDir == null)
            {
                workingDir = syncedCompData.WorkingDir;
            }

            try
            {
                File.Copy(Path.Combine(GetProjectDir(), "constraintdata.xml"), Path.Combine(workingDir, "constraintdata.xml"));
            }
            catch (Exception)
            {
                //  not an issue
            }

            string logfile = Path.Combine(GetProjectDir(), "log", Path.ChangeExtension("MetaLink_CreoCreateAssembly_" + Path.GetRandomFileName(), "log"));
            string arguments = String.Format("-v debug -g -s 127.0.0.1:15150 -w \"{0}\" -l \"{1}\" {2}, -id {3}", workingDir, logfile, exeparams, syncedCompData.InstanceId);
            arguments += " -d " + guid;

            ProcessStartInfo info = new ProcessStartInfo()
            {
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                UseShellExecute = false,
                WindowStyle = ProcessWindowStyle.Hidden,
                CreateNoWindow = true,
                FileName = createAssemblyExe,
                Arguments = arguments
                // TODO -p ?
            };
            if (mode == CreoOpenMode.OPEN_EMPTY)
            {
                info.Arguments += " -m passive";
            }
            else if (!isComponentAssembly)
            {
                info.Arguments += " -m component";
            }
            if (Configuration.ConfigProFile.Length > 0)
            {
                info.Arguments += " -c " + Configuration.ConfigProFile;
            }
            if (!string.IsNullOrWhiteSpace(syncedCompData.AuxDir))
            {
                info.Arguments += String.Format(" -a \"{0}\"", syncedCompData.AuxDir);
            }
            Process createAssembly = new Process();
            createAssembly.StartInfo = info;
            StringBuilder stdoutData = new StringBuilder();
            StringBuilder stderrData = new StringBuilder();
            createAssembly.OutputDataReceived += (o, args) =>
            {
                lock (stdoutData)
                {
                    stdoutData.Append(args.Data);
                    stdoutData.Append(System.Environment.NewLine);
                }
            };
            createAssembly.ErrorDataReceived += (o, args) =>
            {
                lock (stderrData)
                {
                    stderrData.Append(args.Data);
                    stderrData.Append(System.Environment.NewLine);
                }
            };
            createAssembly.EnableRaisingEvents = true;
            createAssembly.Exited += (o, args) =>
            {
                lock (stderrData)
                {
                    if (GMEConsole != null && createAssembly.ExitCode != 0)
                    {
                        string errlog = "CADCreoParametricCreateAssembly_err.log";
                        StreamWriter writer = new StreamWriter(errlog);
                        writer.Write(stderrData.ToString());
                        writer.Flush();
                        writer.Close();
                        GMEConsole.Error.WriteLine(String.Format("CADCreoParametricCreateAssembly exited with code {0}, the logfile is {1}", createAssembly.ExitCode, errlog));
                        SyncControl.Invoke(startupFailedCallback);
                    }
                }
            };
            LastStartedInstance = syncedCompData;
            createAssembly.Start();
            createAssembly.BeginOutputReadLine();
            createAssembly.BeginErrorReadLine();
            createAssembly.StandardInput.Close();
            GMEConsole.Info.WriteLine("Creo is starting, the logfile is at: <a href=\"file:///{0}\" target=\"_blank\">{0}</a>", Path.Combine(workingDir, "log", logfile));
            ShowStartupDialog(true);
            // createAssembly.WaitForExit(10 * 1000);
        }
 public void StartCreoEmpyMode()
 {
     SyncedComponentData cdata = new SyncedComponentData()
     {
         Type = SyncedComponentData.EditType.Component,
         WorkingDir = GetProjectDir()
     };
     string tmpid = Guid.NewGuid().ToString();
     syncedComponents.Add(tmpid, cdata);
     StartAssemblyExe(CreoOpenMode.OPEN_EMPTY, tmpid, false, "");
     SendInterest(null, ComponentCreateTopic, tmpid);
 }