Macro to open an assembly

Hello

 

I'm trying to create a macro to open an assembly. I was inspired by the document proposed by Dassault but I can't do it...

I get a Run Time Error type 13 (typ mismatch). At the level of my line:

swModel = swApp.OpenDoc6(assemblyFile, swDocumentTypes_e.swDocASSEMBLY, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)

I'm attaching my macro.... Could you tell me what I'm doing wrong?

thank you in advance for your help

LSE

 

 

Sub main()

Dim swModel As ModelDoc2
Dim swModelDocExt As ModelDocExtension
Sun swSelMgr As SelectionMgr
Dim partFile As String
Dim assemblyFile As String
Dim drawingFile As String
Dim errors As Integer
Dim warnings As Integer


Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized


' Open an assembly document and select all components in the assembly

assemblyFile = "C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\samples\introsw\bolt-assembly.sldasm"
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.GetOpenDocumentByName(assemblyFile)

swModel = swApp.OpenDoc6(assemblyFile, swDocumentTypes_e.swDocASSEMBLY, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)
swModelDocExt = swModel.Extension
swSelMgr = swModel.SelectionManager


End Sub

Several errors in your macro:

no declaration for swApp:

Dim swApp As SldWorks.SldWorks

Same for Part (you get the active document but you don't expose it in the following)

In addition, there are still other mistakes then I don't know which model you started from but it's not the cleanest.

Try this and adapt it later:

https://help.solidworks.com/2020/English/api/sldworksapi/Open_Assembly_Document_Example_VB.htm?verRedirect=1

1 Like

Not sure that your link is the easiest.

The simple version, solidworks is open. It opens the file you want (hard-written path, PART version and not SLDASM

See here: https://help.solidworks.com/2018/english/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.ISldWorks~OpenDoc6.html

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim doc As SldWorks.ModelDoc2
Dim fileerror As Long
Dim filewarning As Long

Sub main()

    Set swApp = Application. SldWorks
    swApp.Visible = True
   

    ' Get the current working directory before opening the document
    Debug.Print "Current working directory is " & swApp. GetCurrentWorkingDirectory
   

    Set doc = swApp. OpenDoc6("C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2018\samples\tutorial\api\chair.sldprt", swDocPART, swOpenDocOptions_Silent, "", fileerror, filewarning)

End Sub

2 Likes

I chose this version because in his he opened the assemblies in masked (option_silent)

But indeed yours is simpler @mandragore .

Hello

I haven't tried the macro but to the remark: "I get a Run Time Error type 13 (typ mismatch)", I wouldn't be surprised if it comes from typing the variables "Dim errors As Integer" and "Dim warnings As Integer" which normally should be "Long". Of course, you also have to check that the file defined in the "assemblyFile" variable exists.

For the question "Could you tell me what I'm doing wrong?" I will not dwell on myself, as my imminent colleagues Sbadenis and Mandrake have already begun to respond... But I agree, or even more, with the remark "but it's not the cleanest" of Sbadenis.

Kind regards

1 Like

Thank you very much for your answers and your responsiveness!

 I just tested the 2 proposals  but I don't understand because nothing happens.

I don't get any more error message but the requested assembly doesn't open...

Do you have an idea...

Thank you

 

In both of them, make sure that the link points to an assembly. Otherwise it doesn't open anything.

I invite you to replace with the link of one of your assemblies.

If mine doesn't work it's normal it's specified 'Part' and not ASM. I just rewrote it with your variable and corrected my mistake. 

 

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim doc As SldWorks.ModelDoc2
Dim fileerror As Long
Dim filewarning As Long

Dim assemblyFile As String

Sub main()

    Set swApp = Application.SldWorks
    swApp.Visible = True
   

assemblyFile = "C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\samples\introsw\bolt-assembly.sldasm"

 

    Set doc = swApp.OpenDoc6(assemblyFile , 2, 1, "", fileerror, filewarning)

End Sub

 

there it should work, I replaced 'swDocPART' with 2 which corresponds to 'swDocASSEMBLY'

Hello you 2, 

It works. Thank you very much.

The system accepts the opening of an independent assembly but not that of an assembly constituting ...

Let me explain

I'm in 130100 which is made up of 130101, 130102... 

I can open 110101 but not 130101 year running the macro since 130100... Is this normal?

thank you for your enlightenment

In theory, the assembly is already open, so solidworks is not going to re-open it.

 

You have to play with other functions I think, put your 130101 lighten and activate your macro, see if it goes to solve.

 

It's computer science, software can be very 'stupid'. There are a lot of different functions for each function, I saw a '

swApp. CreateNewWindow' which can surely help you forced the software to open your 130101 in a new window.

 

After that, we have a lot of people who do api here if you enlighten us a little more on what you want to do and why someone can already do it or we can guide you more quickly.

Uh... I'm not a specialist at all...

so, what should we use instead of  "Set doc = swApp.OpenDoc6  .... "?

If your subassembly is already open in SW then you have to use the "ActivateDoc3" function to open it (in reality it's activate that you have to say since it's already open) in a new SW window.

Kind regards

1 Like

@mandragore: I don't quite agree with the remark "It's computer science, software can be very 'stupid'.   There are a lot of different functions for each function", I find that APIs are rather logical precisely. In this case there is a difference between "open" and "activate"...

Kind regards

I just tested. It works flawlessly. So it was a question of vocabulary. Thank you again very much. I continue...