Step and pdf export of all open files

Hi everyone!

Via an Excel macro, I open a number of parts in SolidWorks. I would like to make a macro that would allow you to open the plan of the first active document, save it as a PDF, close the plan, save the part in STEP and then close the room and move on to the next room.

I'm just starting out in the world of macros so I'm looking at everything that's being done and I've picked it up right and left.

To save a plan in WWTP:

    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
      
    Sub main()
      
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    swModel.Extension.SaveAs Left(swModel.GetPathName, InStrRev(swModel.GetPathName, ".")) & "STEP", 0, 0, Nothing, 0, 0
    
    swApp.CloseDoc Path
        
    End Sub

 

To save a plan in WWTP:

Dim swApp As Object

Dim Part As Object

Dim boolstatus As Boolean

Dim longstatus As Long, longwarnings As Long

Dim FeatureData As Object

Dim Feature As Object

Dim Component As Object

 

Sub main()

Set swApp = Application.SldWorks

 

Set Part = swApp.ActiveDoc

Path = Part.GetPathName                                                     'chemin du fichier

Part.SaveAs2 Left(Path, (Len(Path) - 6)) & "PDF", 0, True, False            'Sauvegarde au format PDF

Set Part = Nothing

swApp.CloseDoc Path

End Sub

 

The problems I have are the following:

- I can't find a line of code that allows me to open the plan of the active document (which is still a room). I tried to create this line with the SoildWorks macro recorder and right-click/open the bet on the blalba part. SLDPRT in plan but it saves me as a line of code the opening of the plan blah blah. SLDDRW and not the opening of the plan of the active document.

- And my second problem is to create a loop. I found the structure of my loop:

Sub boucle_while()


   While ?????????????? 'TANT QUE un document est actif
       


    Wend
       
End Sub

 

But I don't know how to ask him to check that there is indeed an active document!

I think I'm not very far from the thing, you just have to check the condition, find how to open the plan...

Thank you to those who will take the time to take a look at my problem =)!

Hello

For the loop condition, it's simple, you have to check that there is indeed an active document. To do this, use:

While Not SwApp.ActiveDoc <> Nothing (or Is Nothing, I can't remember the accepted syntax)

To open the MEP for a part or assembly, go through the referenced documents for that part or assembly.

2 Likes

Hello

By tinkering with the thing in a "hurry" way and if your plans are stored in the same folder as your parts:

Dim swApp As Object
Sub main()
    Dim swModel As SldWorks.ModelDoc2
    Dim longstatus As Long, longwarnings As Long
    Set swApp = Application.SldWorks
    While Not swApp.ActiveDoc Is Nothing
        Set swModel = swApp.ActiveDoc
        Path = swModel.GetPathName
        Path = Left(Path, (Len(Path) - 6)) & "slddrw"
        Set swModel = swApp.OpenDoc6(Path, 3, swOpenDocOptions_Silent, "", longstatus, longwarnings)
        swModel.SaveAs2 Left(Path, (Len(Path) - 6)) & "PDF", 0, True, False
        swApp.CloseDoc Path
        Set swModel = swApp.ActiveDoc
        Path = swModel.GetPathName
        swModel.SaveAs2 Left(Path, (Len(Path) - 6)) & "step", 0, True, False
        swApp.CloseDoc Path
        Set swModel = swApp.ActiveDoc
    Wend
End Sub

 

Be careful, you must add checks such as:

- If my document is a document then ... otherwise...

- If I can't find a plan then ....

-etc

If your plans are not stored in the same folder as your documents, then you have to go through the referenced documents, as Yves T. said.

Kind regards

2 Likes

Hello D.Roger

I know you did this quickly, but in the  conditions relating to the step, it seems to me that we have to distinguish between the different types of step, otherwise it risks excluding certain STEP formats and possibly causing false negatives.

What do you think?

Kind regards

1 Like

Hello Zozo_mp,

Yes, I did that quickly without worrying about the checks to be made to handle the various possible errors or the export options for the step files. , this one being set to "AP203" by default I think. To choose "AP214" you just have to put the line "boolstatus = swApp.SetUserPreferenceIntegerValue(swStepAP, 214)" after the line "Set swApp = Application.SldWorks", it is also possible to manage other export options in the macro code, see http://help.solidworks.com/2017/english/api/swconst/filesaveasstepoptions.htm.

It is possible to do the same with the pdf export to manage certain options, see http://help.solidworks.com/2017/english/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IExportPdfData.html.

It all depends on how useful the macro is in a given context...

Kind regards

2 Likes

Hi thread!

Thank you very much for your answers, I now understand (well I think) how to be able to open the plan/room of a room/plan if they have the same name and the same path and that's great, it will be very useful for me to do so =)!

In short, via an Excel macro, I open all the parts of the same family in a table of materials (machined parts, mechanically welded parts, sheet metal parts, etc.). Then there is the SolidWorks macro to be able to create all the neutral files to simplify shipments to subcontractors because today we record everything by hand and it is a source of forgetfulness, errors and a huge waste of time ^^! That's a bit for context ☺!

As for the code that d.roger has posted, I have put it a little more structured and detailed for me so that I can find my way around more easily:

Dim swApp As Object

Sub main()

'Déclaration des variables :

    Dim swModel As SldWorks.ModelDoc2
    Dim longstatus As Long, longwarnings As Long

'Initialisation de certaines variables :
    
    Set swApp = Application.SldWorks
    boolstatus = swApp.SetUserPreferenceIntegerValue(swStepAP, 214)
    
'Mise en place de la boucle :
    
    While Not swApp.ActiveDoc Is Nothing 'Tant qu'il y a un document actif alors
    
        Set swModel = swApp.ActiveDoc                                                                 'Document sur lequel se passe les actions à venir
        Path = swModel.GetPathName                                                                    'Extraction du chemin d'enregistrement de la pièce
        Path = Left(Path, (Len(Path) - 6)) & "slddrw"                                                 'Concatène le nom et l'extension .SLDDrW pour avoir le nom complet du plan (C/..../plan.SLDDRW)
        Set swModel = swApp.OpenDoc6(Path, 3, swOpenDocOptions_Silent, "", longstatus, longwarnings)  'Ouverture du plan
        swModel.SaveAs2 Left(Path, (Len(Path) - 6)) & "PDF", 0, True, False                           'Enregistrement en PDF
        swApp.CloseDoc Path                                                                           'Fermeture du plan
        
        Set swModel = swApp.ActiveDoc                                                                 'Document sur lequel se passe les actions à venir
        Path = swModel.GetPathName                                                                    'Extraction du chemin d'enregistrement de la pièce
        swModel.SaveAs2 Left(Path, (Len(Path) - 6)) & "step", 0, True, False                          'Enregistrement en STEP
        swApp.CloseDoc Path                                                                           'Fermeture du plan
        
        Set swModel = swApp.ActiveDoc                                                                 'Je ne comprends pas trop à quoi il sert ? Pour activer le document suivant pour que la boucle puisse se répéter ?
        
    Wend
    
End Sub

I started the reading step by step, on the line

Set swModel = swApp.OpenDoc6(Path, 3, swOpenDocOptions_Silent, "", longstatus, longwarnings) 

The plan does not open. So inevitably on the line he tells me that there is an error because he can't save the part in PDF. I also added the line to specify the record in STEP 214 but it's a variable to which I give a value, right? I have to declare the variable above with something like Dim boolstatus As "I don't know what"?

 

Thanks again for the help =)

Hello

I see on your profile that you have Epdm, if the plan is stored in it then you have to repatriate it to your local cache before launching the macro or, better, do it through your macro which will also require reading the EPDM APIs!!

Dim boolstatus As boolean

Kind regards

1 Like