[share] Macro to create a 3D of the assembly, step or edrawing

Hello
I'm sharing with you the code of my macros. Today it is to automatically create the STEP or eDrawing file of the assembly in progress, by saving it in a predefined folder:

Dim swApp As Object
Dim Part As Object


Sub main()

Set swApp = Application.SldWorks

'bRet = swApp.SetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swStepAP, 214)
'Step = swApp.SetUserPreferenceIntegerValue(swStepAP, 214)

Set Part = swApp.ActiveDoc

sPathName = Part.GetPathName

sPathName = Mid(sPathName, InStrRev(sPathName, "\") + 1, Len(sPathName) - InStrRev(sPathName, "\") - Len(".sldasm"))

'sPathName = sPathName + ".STEP"

folderRoot = "\\nom_du_serveur\nom_du_dossier\"

Part.SaveAs2 folderRoot & sPathName & ".easm", 0, True, False

MsgBox ("Fichier enregistré :  sPathName & ".easm")

End Sub

You have to replace folderRoot = "\nom_du_serveur\nom_du_dossier" with your own path
I deliberately left some commented lines, it's for the WWTP. Here you can use them to define version 214 for example
I hope this helps you
Ced

3 Likes

Thanks for sharing @cedric_keiflin ...
However, I will override the " Rigid " selection of

by opening a Windows browser with a request to select the location of the file to be saved:

Dim fso As FileSystemObject
Dim oF1 As FileSystemObject
Dim folderRoot As folder
Dim fldr As FileDialog
Dim sItem As String

Sub main ()
Set fso = New FileSystemObject               'Appel du script FileSystemObject
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False		‘False = Ne pas autoriser la Multi-Selection ; True = Autoriser la Multi-Selection 
        .InitialFileName = "LE NOM DU DOSSIER PREDEFINI"       'Répertoire initial de l'explorateur windows
         
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With
    Set folderRoot = fso.GetFolder(sItem)           'Mettre le nom de la racine du dossier à scanner
End sub

Warning: it is necessary to load the Reference " Microsoft Scripting Runtime" to use Windows Explorer...

Note: In the example above, for compatibility, I have kept the Name "folderRoot" as the location variable.
Then you have to replace the NAME OF THE PREDEFINED FOLDER with the path of the starting point of the navigation window...

Example:
. InitialFileName = " THE NAME OF THE PREDEFINED FOLDER"
Becomes
. InitialFileName = "G:\3D_Solidworks\Deal" (here " G " can be a Network location letter)

It's up to you to see if it seems interesting to you to associate it with your macro.

3 Likes

of course we can adapt :slight_smile:
In my case the hard path is a will because I want this file to always go in the same folder (export for the workshop)
In my code, you click and it creates the file with the name in the desired folder, out of need to browse

Merci @cedric_keiflin !