OpenDoc6 works correctly only in debug mode

Programming and macros
User avatar
Hansjoerg
Posts: 106
Joined: Thu Apr 01, 2021 4:17 pm
Answers: 3
x 72
x 55

OpenDoc6 works correctly only in debug mode

Unread post by Hansjoerg »

Hello to all,

has anyone besides me ever had the problem that the OpenDoc6 method only works in debug mode, or if you force a pause by a message after the command?
As long as you are still programming, you can also insert a breakpoint, then the method will work correctly.

I add the macro and a testfile to the topic. If anybody wants to test copy the stepfile to the c:/temp folder.

Code: Select all

Option Explicit
      
Sub SelectStepDialog(swApp As SldWorks.SldWorks, StepFileFullName As String)
    Dim lfileoptions As Long
    Dim fileConfig As String
    Dim fileDispName As String
    
    'Show File Dialog to select a Step File
    StepFileFullName = swApp.GetOpenFileName("Select Step File", "", "3D-Step Files (*.stp; *.step)|*.stp; *.step|", lfileoptions, fileConfig, fileDispName)
     
    If StepFileFullName = "" Then
        swApp.SendMsgToUser2 "No Step File selected!", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
        End
    End If
         
End Sub

Sub ImportStepFile(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, StepFileFullName As String)

    Dim swImportStepData As SldWorks.ImportStepData
    Dim lerrors As Long

    'Stepdatei importieren
    Set swImportStepData = swApp.GetImportFileData(StepFileFullName)
    swImportStepData.MapConfigurationData = True

    'Load the STEP file
    Set swModel = swApp.LoadFile4(StepFileFullName, "r", swImportStepData, lerrors)

    If Not lerrors = "0" Then
        swApp.SendMsgToUser2 "Fehler beim Step Importieren!", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
        End
    End If
  
End Sub

Sub OpenSLA(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2)
    Dim swRootComp As SldWorks.Component2
    Dim swComp As SldWorks.Component2
    Dim swConf As SldWorks.Configuration
    Dim TlaName As String
    Dim SlaName As String
    Dim CompFullFileName As String
    Dim CompName As String
    Dim lwarnings As Long
    Dim lerrors As Long
    
    'Select Top Level Assembly
    Set swModel = swApp.ActiveDoc
    Set swConf = swModel.GetActiveConfiguration
    Set swRootComp = swConf.GetRootComponent3(True)
    TlaName = swRootComp.Name2
      
    'Select Second Level Assembly
    Set swComp = swRootComp
    Dim vChildComp As Variant
    Dim swChildComp As SldWorks.Component2
    vChildComp = swComp.GetChildren

    Set swChildComp = vChildComp(0)
    SlaName = swChildComp.Name2
    swChildComp.Select2 False, 0
    Debug.Print "TLA: " & TlaName
    Debug.Print "SLA: " & SlaName
    
    'Compare Name of TLA and SLA, if equal use SLA
    If Left(TlaName, InStr(TlaName, "_") - 1) = Left(SlaName, InStr(TlaName, "_") - 1) Then
        CompFullFileName = swChildComp.GetPathName
        CompName = Right(swChildComp.GetPathName, CLng(Len(swChildComp.GetPathName)) - CLng(InStrRev(swChildComp.GetPathName, "\")))
        Debug.Print "CompFullFileName: " & CompFullFileName
        Debug.Print "CompName: " & CompName
        
        Set swModel = swApp.OpenDoc6(CompFullFileName, swDocumentTypes_e.swDocASSEMBLY, swOpenDocOptions_e.swOpenDocOptions_LoadExternalReferencesInMemory, "", lerrors, lwarnings)
        Debug.Print "OpenDoc6 errors: " & lerrors
        Debug.Print "OpenDoc6 warnings: " & lwarnings
        
        'With this Hack the macro also works well, but i don´t want an additional message
        'swApp.SendMsgToUser "Hack"
        
        swApp.ActivateDoc3 CompName, False, swRebuildOnActivation_e.swDontRebuildActiveDoc, lerrors
        Debug.Print "ActivateDoc3 errors: " & lerrors
        Set swModel = swApp.ActiveDoc

    End If
End Sub

Sub main()
   
    Dim swApp As SldWorks.SldWorks
    Set swApp = Application.SldWorks

    swApp.SetCurrentWorkingDirectory ("C:\temp\")

    'Call Sub to Select Step File
    Dim StepFileFullName As String
    SelectStepDialog swApp, StepFileFullName

    'Call Sub to import selected Step File
    Dim swModel As SldWorks.ModelDoc2
    ImportStepFile swApp, swModel, StepFileFullName


    'Call Sub to check if TLA and SLA are equal
    Dim CurrCompName As String
    OpenSLA swApp, swModel
    
    'Message to show Macro is ready
    swApp.SendMsgToUser "Ready"
     
End Sub

Attachments
Load Stepfile and open SLA.swp
(70.5 KiB) Downloaded 29 times
bezel moldbase_0815.STP
(3.24 MiB) Downloaded 26 times
All the "good" news about SWX makes me feel like I'm driving a truck with two trailers straight into a dead end.
User avatar
gupta9665
Posts: 359
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 383
x 414

Re: OpenDoc6 works correctly only in debug mode

Unread post by gupta9665 »

I tested (normal running and not debugging) the macro and it worked OK. Are you getting any errors?
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
Hansjoerg
Posts: 106
Joined: Thu Apr 01, 2021 4:17 pm
Answers: 3
x 72
x 55

Re: OpenDoc6 works correctly only in debug mode

Unread post by Hansjoerg »

Hello Gupta,

no there is no error message.
Does the macro open the component on the second level in the feature tree in a separate window?
All the "good" news about SWX makes me feel like I'm driving a truck with two trailers straight into a dead end.
User avatar
AlexB
Posts: 434
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 21
x 240
x 383

Re: OpenDoc6 works correctly only in debug mode

Unread post by AlexB »

I am noticing the same thing as you're describing @Hansjoerg

It seems that it's trying to activate the document before the application is fully finished loading the file(s). I'm not sure off the top of my head how to wait for that to be complete before proceeding, but that's what the breakpoint is simulating with the delay it causes.
User avatar
SPerman
Posts: 1819
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 13
x 2003
x 1677
Contact:

Re: OpenDoc6 works correctly only in debug mode

Unread post by SPerman »

Would the "sleep" function help?
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
User avatar
Hansjoerg
Posts: 106
Joined: Thu Apr 01, 2021 4:17 pm
Answers: 3
x 72
x 55

Re: OpenDoc6 works correctly only in debug mode

Unread post by Hansjoerg »

The sleep function is definitely an option I will try out.
In my distress I already put the OpenDoc6 method into a Do Loop Until loop, and took the variable Longerrors as exit condition, but unfortunately this led to an endless loop.

If nothing helps I have to send the problem to the API support.
All the "good" news about SWX makes me feel like I'm driving a truck with two trailers straight into a dead end.
User avatar
gupta9665
Posts: 359
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 383
x 414

Re: OpenDoc6 works correctly only in debug mode

Unread post by gupta9665 »

Hansjoerg wrote: Wed Aug 30, 2023 12:38 pm Hello Gupta,

no there is no error message.
Does the macro open the component on the second level in the feature tree in a separate window?
Yes the macro opens the component. I have 3dinterconnect set to on. And then macro open the selected STEP file and then the first sub assembly/component.

Do you have 3dinterconnect selected? If not then select it and then run the macro.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
Hansjoerg
Posts: 106
Joined: Thu Apr 01, 2021 4:17 pm
Answers: 3
x 72
x 55

Re: OpenDoc6 works correctly only in debug mode

Unread post by Hansjoerg »

Hello Gupta,

you are right. If I use 3dinterconnect for the import, then the macro works. I am a bit surprised that it can open the subassembly via macro, because it does not work for me via the user interface.
However, the 3d interconnect also brings two small disadvantages.
a. The subassembly I need is no longer on the 2 stage but on the 3 stage.
b. In the file names still everywhere a ".STP" is added.

Maybe I have also found a solution to monitor if the open commands are fully executed.

In the answers to this thread from the old forum I found this code:

Code: Select all

Private Function swApp_DocumentLoadNotify2(ByVal docTitle As String, ByVal docPath As String) As Long
 IsDocumentLoaded = True
End Function
Private Function swApp_FileOpenPostNotify(ByVal FileName As String) As Long
 IsDocumentLoaded = True
End Function
Private Function swApp_FileOpenPreNotify(ByVal FileName As String) As Long
 IsDocumentLoaded = False
End Function‍‍‍‍‍‍‍‍‍
Now I just need to understand how this works and how to incorporate it into my code.

Whereas in the long run the 3d interconet function is surely the better way.
All the "good" news about SWX makes me feel like I'm driving a truck with two trailers straight into a dead end.
User avatar
mihkov
Posts: 33
Joined: Sun Feb 05, 2023 2:01 am
Answers: 0
x 14
x 20

Re: OpenDoc6 works correctly only in debug mode

Unread post by mihkov »

Hansjoerg wrote: Thu Aug 31, 2023 5:45 am The sleep function is definitely an option I will try out.
If nothing helps I have to send the problem to the API support.
Perhaps tracking events will help? I have not used it, but there are such functions.
DSldWorksEvents_DocumentLoadNotify2EventHandler - Post-notifies the user program when a SOLIDWORKS document is
loaded. (https://help.solidworks.com/2022/englis ... ndler.html)

Consider this as a starting point

Code: Select all

Dim WithEvents swApp As SldWorks.SldWorks

Private Sub swApp_DocumentLoadNotify2(ByVal DocTitle As String, ByVal DocPath As String)
     ' This event fires when the document is loaded
    
     ' Checking if this is the right document (eg by name or path)
     If InStr(DocTitle, "My_Document") > 0 Then
         ' Here you can perform actions with the downloaded document (after opendoc6)
     End If
End Sub

Sub Main()
     ' Initialize SolidWorks and subscribe to the DocumentLoadNotify2 event
     Set swApp = Application.SldWorks
     swApp.SetAddinCallbackInfo 0, Me
       DoEvents ' Waiting for work to finish (until you close SolidWorks)
End Sub
I haven't tested this at work.
User avatar
Hansjoerg
Posts: 106
Joined: Thu Apr 01, 2021 4:17 pm
Answers: 3
x 72
x 55

Re: OpenDoc6 works correctly only in debug mode

Unread post by Hansjoerg »

I tried to implement the code snippets in my macro.
I started with the line "Dim WithEvents swApp as....." but the line was marked with red letters.

In my seconde try i have started with a blank macro, but it happend the same as in my first try, the line with "Dim WithEvents swapps..." was also marked with red letters.

What am i not in the right (except that I do not know what I am doing right now) :-)

i want to test and understodd test this methode, even though i don't actually need them when i read in with the interconnect interface.
All the "good" news about SWX makes me feel like I'm driving a truck with two trailers straight into a dead end.
User avatar
mihkov
Posts: 33
Joined: Sun Feb 05, 2023 2:01 am
Answers: 0
x 14
x 20

Re: OpenDoc6 works correctly only in debug mode

Unread post by mihkov »

Hansjoerg wrote: Tue Sep 05, 2023 2:49 am i want to test and understodd test this methode, even though i don't actually need them when i read in with the interconnect interface.
https://blog.codestack.net/handling-solidworks-events

Try to look here. You need to create a class to track events. I haven't been able to make it work.
User avatar
Hansjoerg
Posts: 106
Joined: Thu Apr 01, 2021 4:17 pm
Answers: 3
x 72
x 55

Re: OpenDoc6 works correctly only in debug mode

Unread post by Hansjoerg »

mihkov wrote: Tue Sep 05, 2023 5:04 am ....I haven't been able to make it work.
I also not!

I add also the event handlers i found in the old forum.

In the debug mode i can see the eventhandlers will be adressed, but in run mode nothing happens

I have add the message function to all eventhandlers, but only the handler FileOpenPostNotify popps up the message, and only in debug mode, not in run mode.

Has any body else an idee to solve this issue. In my project i reached a second possition where i have to wait untill the Document is loaded. And in this position i cant use the workaround with the 3d Interconnect Interface
All the "good" news about SWX makes me feel like I'm driving a truck with two trailers straight into a dead end.
User avatar
JSculley
Posts: 574
Joined: Tue May 04, 2021 7:28 am
Answers: 52
x 7
x 806

Re: OpenDoc6 works correctly only in debug mode

Unread post by JSculley »

Calling OpenDoc6 on a file that doesn't actually exist in the file system seems like a risky thing to do. I'm surprised it ever works at all.

Using the Windows Sleep function will not work.

A call to GetModelDoc2 (before the OpenDoc6 call) for your child component seems to work.

In fact, doing this lets you eliminate the call to OpenDoc6 altogether, since you can simply set the Visible property to True for the ModelDoc2 object to make it active. And since OpenDoc6 is gone, so is most of the code inside the IF statement of the OpenSLA function:

Code: Select all

If Left(TlaName, InStr(TlaName, "_") - 1) = Left(SlaName, InStr(TlaName, "_") - 1) Then
    Dim compDoc As ModelDoc2
    Set compDoc = swChildComp.GetModelDoc2
    compDoc.Visible = True
    Set swModel = swApp.ActiveDoc
End If
User avatar
mihkov
Posts: 33
Joined: Sun Feb 05, 2023 2:01 am
Answers: 0
x 14
x 20

Re: OpenDoc6 works correctly only in debug mode

Unread post by mihkov »

Hansjoerg wrote: Tue Sep 05, 2023 10:31 am I also not!
I managed to somehow get it to work.
I do not understand one point: I have the execution of the macro code to pause until the file is opened. And my event listener doesn't make sense - it listens when things happen anyway. I was not able to simulate non-synchronous code operation.
You can build on this option in your work. I had to turn to primary sources for help (GPT). Perhaps this is the only such working option on the entire Internet, take care of it ;)

I think that you do not need all this code, and the working version is presented in the message above.

Code: Select all

-----------------------------------------------------------------------------
Class EventsHandler

Public WithEvents swApp As SldWorks.SldWorks


Private Sub Class_Initialize()
   		Set swApp = Application.SldWorks
End Sub

Private Function swApp_FileOpenNotify2(ByVal FileName As String) As Long
		MsgBox FileName & " opened", vbInformation
		swApp_FileOpenNotify2 = 0
End Function
---------------------------------------------------------------------------------
Module mm1

Dim swEventsHandler As EventsHandler
    
' Main module for running a macro
Sub RunSolidWorksExample()
        'Declare the event listener as a class (class name)
        Set swEventsHandler = New EventsHandler
        
        ' We start the function of opening a file through OpenDoc6 ("D:\mm.SLDPRT")
        OpenSolidWorksDocument "D:\mm.SLDPRT"
        DoEvents ' Waiting for work to finish (until you close SolidWorks)
End Sub
    

Function OpenSolidWorksDocument(ByVal FilePath As String)
        Dim docPath As String
        Dim docType As Long
        Dim swDoc As Object
        Dim swModel As SldWorks.ModelDoc2
        Dim ErrorCode1 As Long
        Dim ErrorMsg1 As Long
    
        docPath = FilePath
        docType = swDocumentTypes_e.swDocPART 'Document type (part)
        'Since swApp is defined in the class, we give the full path through the class.
        Set swDoc = swEventsHandler.swApp.OpenDoc6(docPath, docType, swOpenDocOptions_e.swOpenDocOptions_Silent, "", ErrorCode1, ErrorMsg1)
    
        'We explicitly check in the code whether there is something in the open document object
        If Not swDoc Is Nothing Then
            Set swModel = swDoc
        Else
            MsgBox "Failed to open document: " & docPath
        End If
End Function

Attachments
mm.swp
(63 KiB) Downloaded 17 times
User avatar
Hansjoerg
Posts: 106
Joined: Thu Apr 01, 2021 4:17 pm
Answers: 3
x 72
x 55

Re: OpenDoc6 works correctly only in debug mode

Unread post by Hansjoerg »

JSculley wrote: Tue Sep 05, 2023 1:28 pm Calling OpenDoc6 on a file that doesn't actually exist in the file system seems like a risky thing to do. I'm surprised it ever works at all.
not only the opening of the file, but also the closing of the file causes problems, especially if the component is installed several times in the assembly.
In fact, doing this lets you eliminate the call to OpenDoc6 altogether, since you can simply set the Visible property to True for the ModelDoc2 object to make it active. And since OpenDoc6 is gone, so is most of the code inside the IF statement of the OpenSLA function:
Gigantic, how do you come up with this way. Even the macro recorder doesn't record the steps like that.

However, I have a small new problem with this code. In run mode everything works without problems. But as soon as I go through the program in debug mode, Solidworks hangs.
It looks like a message pops up, but it doesn't contain any text.
image.png
I had the problem with the empty message before in an older macro project. But I used the method OpenCompFile (IAssemblydoc) to open a file.
So I came to OpenDoc6 and ActivateDoc2.
All the "good" news about SWX makes me feel like I'm driving a truck with two trailers straight into a dead end.
Post Reply