×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

API - False Negatives with Active Doc

API - False Negatives with Active Doc

API - False Negatives with Active Doc

(OP)
Hi Guys

     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


Consciousness: That annoying time between naps.

RE: API - False Negatives with Active Doc

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.

RE: API - False Negatives with Active Doc

Here is a more detailed function for Option 2 mentioned above. You can modify your code to include the test.

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...

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.

RE: API - False Negatives with Active Doc

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.

RE: API - False Negatives with Active Doc

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.

RE: API - False Negatives with Active Doc

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 "v" & "h" 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

RE: API - False Negatives with Active Doc

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.

RE: API - False Negatives with Active Doc

(OP)
Dsi

     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("SldWorks.Application")
  Set Model = swApp.ActiveDoc
  If Model Is Nothing Then
    If IsFalsePositive = False Then
      swApp.SendMsgToUser2 "A file must be opened before these keys work.", 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 "*Bottom", 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("swApp.EnumDocuments2")
And added
  Set swApp = CreateObject("SldWorks.Application")
to the function

Lee


Consciousness: That annoying time between naps.

RE: API - False Negatives with Active Doc

StarrRider,

CreateObject is VBA.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources