public BomItem[] GetBomItems() { // Dictionary to hold BomItem(s). Key is the full path to the SolidEdgeDocument. Dictionary <string, BomItem> bomItems = new Dictionary <string, BomItem>(); SolidEdgeFramework.Application application = null; SolidEdgeFramework.Documents documents = null; SolidEdgeAssembly.AssemblyDocument assemblyDocument = null; OleMessageFilter.Register(); try { // Attempt to connect to a running instance of Solid Edge. application = SolidEdgeUtils.Connect(); // Get a reference to the Documents collection. documents = application.Documents; // Make sure a document is open and that it's an AssemblyDocument. if ((documents.Count > 0) && (application.ActiveDocumentType == SolidEdgeFramework.DocumentTypeConstants.igAssemblyDocument)) { // Get a reference to the AssemblyDocument. assemblyDocument = (SolidEdgeAssembly.AssemblyDocument)application.ActiveDocument; // Start walking the assembly tree. ProcessAssembly(0, assemblyDocument, bomItems); } else { throw new System.Exception("No assembly open."); } } catch (System.Runtime.InteropServices.COMException ex) { if (ex.ErrorCode == -2147221021 /* MK_E_UNAVAILABLE */) { throw new System.Exception("Solid Edge is not running."); } else { throw; } } catch { throw; } finally { } return(bomItems.Values.ToArray()); }
/// <summary> /// Registers this instance of IMessageFilter with OLE to handle concurrency issues on the current thread. /// </summary> /// <remarks> /// Only one message filter can be registered for each thread. /// Threads in multithreaded apartments cannot have message filters. /// Thread.CurrentThread.GetApartmentState() must equal ApartmentState.STA. In console applications, this can /// be achieved by applying the STAThreadAttribute to the Main() method. In WinForm applications, it is default. /// </remarks> public static void Register() { IMessageFilter newFilter = new OleMessageFilter(); IMessageFilter oldFilter = null; // 1st check the current thread's apartment state. It must be STA! if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA) { // Call CoRegisterMessageFilter(). Marshal.ThrowExceptionForHR(NativeMethods.CoRegisterMessageFilter(newFilter: newFilter, oldFilter: out oldFilter)); } else { throw new System.Exception("The current thread's apartment state must be STA."); } }
private void MainForm_Load(object sender, EventArgs e) { // Register with OLE to handle concurrency issues on the current thread. OleMessageFilter.Register(); }