Check if no documents are open ( this method crashes when editing in assembly )

Creator
Creator

Hallo people,

 

Let's explain something first,

I've created a macro with multiple forms for different document types.

 

I've checked this by: ( for part ) 

    Public Function SENoPartDocument() As Boolean
        Dim SEPartDocument As String = seApp.ActiveDocumentType <> SolidEdgeFramework.DocumentTypeConstants.igPartDocument
        Return SEPartDocument
    End Function

 

If no document is open, I check this by:

 

 Public Function SENoDocuments() As Boolean
        Dim SEOpenDocuments As String = SEApplication.Documents.Count = 0
        Return SEOpenDocuments
    End Function

My main form has a timer which checks in which document type SE is. After that, he shows the right form ( timer interval = 1000 ).

 

The problem

When i click on edit in an assembly, the computer thinks a bit of a second, in this time he crashes at the nodocuments function. The SEOpendocuments is 0 then ( where it should be true or false )

 

Also crashes happen when the program have to think long.

 

For extra explanation:

 

Spoiler
  Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            SEApplication = Marshal.GetActiveObject("SolidEdge.Application")
            seApp = GetObject(, "SolidEdge.Application")
            Timer_Main.Start()
        Catch ex As Exception
            Me.Hide()
            Form_InfoNoApplication.Show()
            Form_InfoNoApplication.Timer_ApplicationClose.Start()
        End Try
    End Sub


' --------------------------------------------------------------------------------------------------------------------  
Private Sub Timer_Main_Tick(sender As Object, e As EventArgs) Handles Timer_Main.Tick

        If SENoDocuments() = True Then
            Form_NoDocuments.Show()
            Me.Hide()
            Form_SheetMetal.Hide()
            Form_Assembly.Hide()
            Form_Part.Hide()
            Form1.Hide()
        ElseIf SENoDraftDocument() = False Then
            Form1.Show()
            Me.Hide()
            Form_NoDocuments.Hide()
            Form_SheetMetal.Hide()
            Form_Assembly.Hide()
            Form_Part.Hide()
        ElseIf SENoPartDocument() = False Then
            Form_Part.Show()
            Me.Hide()
            Form_NoDocuments.Hide()
            Form_SheetMetal.Hide()
            Form_Assembly.Hide()
            Form1.Hide()
            Form_Part.Show()
        ElseIf SENoSheetDocument() = False Then
            Form_SheetMetal.Show()
            Me.Hide()
            Form_NoDocuments.Hide()
            Form_Assembly.Hide()
            Form_Part.Hide()
            Form1.Hide()
        ElseIf SENoAssemblyDocument() = False Then
            Form_Assembly.Show()
            Me.Hide()
            Form_NoDocuments.Hide()
            Form_SheetMetal.Hide()
            Form_Part.Hide()
            Form1.Hide()
        End If
    End Sub

I hope that you can give me a solution for this problem ( and why this problem occurs ).

 

4 REPLIES

Re: Check if no documents are open ( this method crashes when editing in assembly )

Solution Partner Legend Solution Partner Legend
Solution Partner Legend

JuRi,

 

instead of polling via a timer the type of the active document I suggest to use the application events instead. You find an example in jason's sample code on GitHub under General\ApplicationEvents\vb\ApplicationEvents

 

 

Kind regards,
Wolfgang Kunert - www.wksyspro.de

Re: Check if no documents are open ( this method crashes when editing in assembly )

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

wku wrote:

JuRi,

 

instead of polling via a timer the type of the active document I suggest to use the application events instead. You find an example in jason's sample code on GitHub under General\ApplicationEvents\vb\ApplicationEvents

  


Especially check the ApplicationEvents::AfterActiveDocumentChange() event

Re: Check if no documents are open ( this method crashes when editing in assembly )

Creator
Creator

Thanks for the replies!

That is an interesting way to check the application events, thanks for pointing those out.

I've gone on to make it work using the timers.

 

And i now have a way that results in a workable solution.

What i've done is that I don't refer to the Public Functions any more, plus i threw it in a try catch.

Which ensures it don't give me an error when changing environments like dynamic edit.

 

Private Sub Timer_Main_Tick(sender As Object, e As EventArgs) Handles Timer_Main.Tick
        Try
            Timer_Main.Interval = 750
            If SEApplication.Documents.Count = 0 Then
                Form_NoDocuments.Show()
                Me.Hide()
                Form_SheetMetal.Hide()
                Form_Assembly.Hide()
                Form_Part.Hide()
                Form1.Hide()
            ElseIf seApp.ActiveDocumentType = SolidEdgeFramework.DocumentTypeConstants.igDraftDocument Then
                Form1.Show()
                Me.Hide()
                Form_NoDocuments.Hide()
                Form_SheetMetal.Hide()
                Form_Assembly.Hide()
                Form_Part.Hide()
            ElseIf seApp.ActiveDocumentType = SolidEdgeFramework.DocumentTypeConstants.igPartDocument Then
                Form_Part.Show()
                Me.Hide()
                Form_NoDocuments.Hide()
                Form_SheetMetal.Hide()
                Form_Assembly.Hide()
                Form1.Hide()
                Form_Part.Show()
            ElseIf seApp.ActiveDocumentType = SolidEdgeFramework.DocumentTypeConstants.igSheetMetalDocument Then
                Form_SheetMetal.Show()
                Me.Hide()
                Form_NoDocuments.Hide()
                Form_Assembly.Hide()
                Form_Part.Hide()
                Form1.Hide()
            ElseIf seApp.ActiveDocumentType = SolidEdgeFramework.DocumentTypeConstants.igAssemblyDocument Then
                Form_Assembly.Show()
                Me.Hide()
                Form_NoDocuments.Hide()
                Form_SheetMetal.Hide()
                Form_Part.Hide()
                Form1.Hide()
            End If
        Catch ex As Exception
            Try
                SEApplication = Marshal.GetActiveObject("SolidEdge.Application")
            Catch ex2 As Exception
                Me.Close()
            End Try
        End Try
    End Sub

Re: Check if no documents are open ( this method crashes when editing in assembly )

Solution Partner Legend Solution Partner Legend
Solution Partner Legend

JuRi,

 

the code looks much cleaner now, avoiding the previous dirty implicit type conversions also. But there might still arise stability issues doing the task by unnecessary polling.

Kind regards,
Wolfgang Kunert - www.wksyspro.de