No access to Solidworks when running my macro with a button

Hello

I would like to launch my macro from a "macro button" directly in Solidworks. When I run my macro from visual basic 6 I have no problems while if I launch it from the button I created in Solidworks nothing works. Indeed, I want to select several faces but I have no access to Solidworks once the macro is launched.

Here's a piece of my code:

 

Dim swApp           As SldWorks.SldWorks
Dim swModel         As SldWorks.ModelDoc2
Sun swSelMgr        As SldWorks.SelectionMgr
Dim matefeature     As SldWorks.Mate2
Dim swFace1         As SldWorks.Face2
Dim swFace2         As SldWorks.Face2

Dim bool1           As Boolean
Dim bool2           As Boolean

Dim CurFaceName     As String
Dim FaceName1       As String
Dim FaceName2       As String

Dim MateName        As String
Dim MateName2       As String

Dim Part            As Object

 

Sub Square90XL()

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    
    bool1 = False
    Do Until bool1 = True
        If swSelMgr.GetSelectedObjectType3(1, -1) = swSelFACES Then
            Set swFace1 = swSelMgr.GetSelectedObject6(1, -1)
            bool1 = True
        End If
    Loop
    swModel.ClearSelection
        
    bool2 = False
    Do Until bool2 = True
        If swSelMgr.GetSelectedObjectType3(1, -1) = swSelFACES Then
            Set swFace2 = swSelMgr.GetSelectedObject6(1, -1)
            bool2 = True
        End If
    Loop
    
    Do
    swFace2.Select (0)
    Rotation.Show
    Loop Until value2 = True
    
    swFace1.Select (1)
    swFace2.Select (1)
    Set Part = swApp.ActiveDoc
    Set matefeature = Part.AddMate3(swMateCOINCIDENT, swMateAlignCLOSEST, True, 0, 0, 0, 0, 0, 0, 0, 0, False, mateError)
    matefeature.name = MateName
    Part.ClearSelection

...

 

Thanks in advance,

 

Gael

Which version of SolidWorks are you using?

The version of VBA used on SolidWorks 2013 is VBA7, maybe the problem comes from there but I'm not an expert on development.

4 Likes

Hello

To complete fthomas' answer, I offer you this article that we published and which explains the compatibility problem between the version levels of SW and VBA.

Kind regards

3 Likes

Hello

 

I think you need to modify these lines of code:

    Set swApp = Application.SldWorks

 

By these:
    Set swApp = SldWorks.SldWorks

 

The Application object does not return the current Solidworks session, but a new Solidworks object

2 Likes

Hello

 

If your macro displays a window, make sure it is not modal

 

If not, I can't tell you more with the excerpt you place here,

But if you attach your macro as an attachment, I can take a look at it

 

Kind regards

8 Likes

Hi all

Thank you for your answers!!

 

@jmsavoyat and @fthomas So I'm still using Solidworks 2010 64 bits so no problems with vb7

 

@ etienne.canuel When I change the line of code by what you tell me I get an error mess ...

 

@ jfaradon I think it's not an open window issue or not since I have access to it, I can click on it and it closes fine. I've tried with other programs and I only have the same problem when I launch my macro using a command button (that I created) located in the assembly tab. If I run it from VB6 no worries. The problem is that I have no access to Solidworks which is essential in my program I have to click on 2 sides... The only recourse I have is ctrl + pause...

 

I attach my code.

For those who download it, I put with the parts that should be inserted (will have to change the path for opening the part in the program in the "square" and "table" userforms).

Basically, it's a part insertion with a coincidence constraint.

The insert userform will only work with table and square. The main module is "insertion_contrainte". The table must be inserted in 1st!!

I didn't put the end of my code because it's useless, it's almost the same thing and if it works for the beginning it will work for the rest!

 

Thank you

 

Gael

 


programme.7z

Dsl I hadn't seen but there are 2 errors in my program

Here is the corrected program in the attachment

 

Gael


programme.7z

You probably didn't reference the right libraries/dlls to your macro.

To make sure they are activated, the easiest way is to create a new macro from the solidworks menu , delete the lines of code automatically created by solidworks and then paste your code.

 

Otherwise, if you know a little about it, you can go to VBA and then to the tools->references menu and then choose the solidworks libraries/dll corresponding to your version.

From memory, you have to tick these:

SldWorks xxxx Type Library

SolidWorks xxxx Constant type library

SolidWorks xxxx Command type library

(Replace xxxx with the Solidworks version)

Thank you for your answer but it's still not that...

I keep looking

Hello

If I understand correctly, your macro is used to create a constraint, and when you launch your macro in Microsoft Visual Basic (the one in SW we agree) it works and you can select your faces.

 

But when you run it from SlidWorks, "nothing works":

What does this mean? What's going on?

The macro goes so fast that you don't have time to select the surfaces?

 

 

A solution that could work: select your two sides BEFORE launching your macro.

Maybe because of the speed of execution, you can't select the face in SolidWorks when you launch the macro with a button, the solution: a pause.

You have to insert this line in your code:

Application.Wait Time + TimeSerial(0, 0, 1)  

 

The result:

 

bool1 = False

    Do Until bool1 = True
        If swSelMgr.GetSelectedObjectType3(1, -1) = swSelFACES Then
            Set swFace1 = swSelMgr.GetSelectedObject6(1, -1)
            bool1 = True
        End If

        Application.Wait Time + TimeSerial(0, 0, 1)  
    Loop
    swModel.ClearSelection
        
    bool2 = False
    Do Until bool2 = True
        If swSelMgr.GetSelectedObjectType3(1, -1) = swSelFACES Then
            Set swFace2 = swSelMgr.GetSelectedObject6(1, -1)
            bool2 = True
        End If

        Application.Wait Time + TimeSerial(0, 0, 1)  
    Loop

 

 

Does it work like this? To see to reduce the break time to less than a second if it's too long for you.

Dsl for the delay but I was on vacation..

No, that's not my problem at all since I use a do until I have all the time I need to select my face.

I actually want to launch a macro from a macro button that I created in Solidworks but when I click on it my program launches correctly but I can't select a face whereas when I launch my VB6 macro I can select my faces.

So I'm blocked, I contacted Solidworks and I'm waiting for their answer.

Thank you

Hello

 

"No, that's not my problem at all, since I use a do until I have all the time I need to select my face."

=> FALSE!

 

I just tested with a break and it works!

On the other hand, the pause I proposed doesn't work, you have to use a "do events".

The macro that works is attached.

 

Or an example of the code is here:

    swModel.ClearSelection
   bool1 = False
   Do Until bool1 = True
       If swSelMgr.GetSelectedObjectType3(1, -1) = swSelFACES Then
           Set swFace1 = swSelMgr.GetSelectedObject6(1, -1)
           bool1 = True
       End If
        For Y = 1 TB 50000
            DoEvents
        Next Y
    Loop
 


insertion__contrainte.swp