Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

API - False Negatives with Active Doc

Status
Not open for further replies.

StarrRider

Mechanical
Sep 15, 2002
151
0
0
US
Hi Guys
030203usf_prv.gif


A friend of mine called attention to a minor problem he is having with some of my macros. The macros are assigned to the numeric keypad and when the SW Browser is opened inside of SW an error message is generated if the keypad is used. Here is the offending section of code:

' *******************************************************
Const swMbWarning = 1
Const swMbOk = 2
Dim swApp As Object
Dim Model As Object

Sub main()
Set swApp = CreateObject("SldWorks.Application")
Set Model = swApp.ActiveDoc

If Model Is Nothing Then

swApp.SendMsgToUser2 "A file must be opened before these keys work.", swMbWarning, swMbOk
Exit Sub
End If

' the remainder of the code

End Sub

This error message is only supposed to be displayed if there is not a file open in SW. Instead – the error message is displayed while the SW Browser is active - even if there is an Active Document open in SW. I would be willing to bet the same thing happens when any other add-in (like Cosmos) is active.

One solution would be to eliminate the error message. This does allow the keys to operate normally but a better solution would be to test to see if a SW or an Add-In was active. I did do a fast search of the API but I came up with nothing.

Does anybody else have any suggestions or ideas?

Lee
040103star_tip_hat_md_clr_prv.gif



Consciousness: That annoying time between naps.
 
Replies continue below

Recommended for you

Option 1:
You may have to dig into the Windows API to make sure that SolidWorks is the active window. However, I am not sure why the macro would trap the keypad use if it's not the active application.

Option 2:
Put an additional trap to catch this situation. You obviously need to use the ActiveDoc for "the remainder of the code", so you can't get rid of that. But, once Model Is Nothing is trapped, you could use the EnumDocuments to ensure that no files are opened. Refer to thread559-68057. TheTick brought this to light and it would be a good way to check for open files. If this method indicates that a file is open you would just exit without displaying the message.

Option 3:
anyone? anyone?

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
Here is a more detailed function for Option 2 mentioned above. You can modify your code to include the test.
Code:
If Model Is Nothing Then
  If IsFalsePositive = False Then
    swApp.SendMsgToUser2 "A file must be opened..."...
  Else
    'At least one file is open, but SW is not the active
    'application. The macro should not be triggered at
    'this stage. No need to warn the user.
  End If
  Exit Sub
End If
Now, the function...
Code:
Public Function IsFalsePositive() As Boolean
    Dim eList As SldWorks.EnumDocuments2
    Dim eItem As SldWorks.ModelDoc2
    Dim rGelt As Object
    Dim pCeltFetched As Long
    'Check for Open Files - Returns True if Files are Open
    Set eList = swApp.EnumDocuments2
    On Error Resume Next
    eList.Next 1, rGelt, pCeltFetched
    If Err.Number <> 0 Then
        Err.Clear
        IsFalsePositive = False
    ElseIf pCeltFetched < 1 Then
        IsFalsePositive = False
    Else
        IsFalsePositive = True
    End If
    Set eItem = Nothing
    Set eList = Nothing
End Function
Hope that helps...

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
hmmm.... the enumdocuments works around this? I thought the 'activedoc' failed because the file dialog was app modal, and SW API was just plain unable to respond until the dialog was closed.

I suppose, there *is* a mention in the API help about being careful with activedoc... but the mention was in reference to to actual doc being USED, vs the actual doc being programmatically worked on.

I gotta check to see if there are any differences between how in process, and out-of-process code behaves in this instance.

 
rocheey:
Actually, I am not sure. I just assumed that the ActiveDoc failed because a document was not active. I also thought that this method would work around that issue because it returns all documents, regardless of their active status. Since the swApp is still hooked, it should function (at least in my mind). All of this may be for nothing if SW can't respond until the window is closed, although the macros are triggered at this stage, leading me to believe that it just affects the handle to the active doc.

Let me know how it goes...

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
Let me explain a bit more. The issue arises when SW Explorer is opened from inside SW. Everything works fine as long as no file has been opened from inside SW. As soon as you click into SW and open a file, then click back into SW Explorer, the macros assigned to the num keys generate the errors. ALso, an interesting happening is that the hotkeys &quot;v&quot; & &quot;h&quot; for Tile Vertically, and Tile Horizontally also generate their actions, but I couldn't find any other hotkeys that misbehaved. If you open SW Explorer as an application out of the start menu, all work just fine. However, I tried all this with CosmosXpress, which runs inside SW, and it worked just fine. Baffling, to say the least.

WT
 
Does the above method not work for trapping the situation?

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
Dsi
030203usf_prv.gif


First – I want to thank you for the effort – But No – I am sorry to say that it crashed and burned. I have not figured out WHY it did as yet – It looks good. Part of the problem is that I am still using SW2001+. After several attempts to make it work, I made the additions and sent one of the files to WMT because I knew he was running SW2003. It crashed and burned for him in the same place. The entire macro is given below with the offending line highlighted.


Const swMbWarning = 1
Const swMbOk = 2
Const swDocDRAWING = 3
Dim swApp As Object
Dim Model As Object
Dim DwgDoc As Object
Dim X, Y, Z, PI As Double
Dim SheetArray As Variant

Public Function IsFalsePositive() As Boolean
Dim eList As SldWorks.EnumDocuments2
Dim eItem As SldWorks.ModelDoc2
Dim rGelt As Object
Dim pCeltFetched As Long
'Check for Open Files - Returns True if Files are Open
Set eList = swApp.EnumDocuments2
On Error Resume Next
eList.Next 1, rGelt, pCeltFetched
If Err.Number <> 0 Then
Err.Clear
IsFalsePositive = False
ElseIf pCeltFetched < 1 Then
IsFalsePositive = False
Else
IsFalsePositive = True
End If
Set eItem = Nothing
Set eList = Nothing
End Function

Sub main()
Set swApp = CreateObject(&quot;SldWorks.Application&quot;)
Set Model = swApp.ActiveDoc
If Model Is Nothing Then
If IsFalsePositive = False Then
swApp.SendMsgToUser2 &quot;A file must be opened before these keys work.&quot;, swMbWarning, swMbOk
Exit Sub
End If
End If
If (Model.GetType = swDocDRAWING) Then
Set DwgDoc = Model.GetCurrentSheet
SheetArray = DwgDoc.GetProperties
X = SheetArray(5)
Y = SheetArray(6)
Z = 0
Model.ViewZoomTo2 X / 4, Y / 2, Z, (3 * X) / 4, 0, Z '2 Lower
Else
Model.ShowNamedView2 &quot;*Bottom&quot;, 6 '2
Model.ViewZoomtofit
End If
Set DwgDoc = Nothing
Set Model = Nothing
Set swApp = Nothing
End Sub


For SW2001+ I made the following modifications but it died in the same place – I did find it slightly amusing to discover that the SW-API Help file does not even have an entry for CreateObject
Personally - I think it would be worth the subscription price if SW2005 did NOTHING MORE than provide useful documentation. The only thing good that I can say about the SW-API Help file is that it is better than nothing.


Public Function IsFalsePositive() As Boolean
Dim eList As Object
Dim eItem As Object
Dim rGelt As Object
Dim pCeltFetched As Long
'Check for Open Files - Returns True if Files are Open

Set eList = swApp.EnumDocuments2
I also tried
Set eList = CreateObject(&quot;swApp.EnumDocuments2&quot;)
And added
Set swApp = CreateObject(&quot;SldWorks.Application&quot;)
to the function

Lee
040103star_tip_hat_md_clr_prv.gif



Consciousness: That annoying time between naps.
 
Status
Not open for further replies.
Back
Top