Creation maccro STEp PDF STEP

Hello team,

I decided to start making a maccro to save time on my work.

Being a novice, I first read a lot of topics on this forum but I didn't necessarily find my answers.

Let me explain: I created a maccro with the "Save" function of it.

The purpose of this maccro would be in order:

  • From a room. SLDPRT
  • Save it in . STEP
  • Open your drawing
  • Save as . DxF
  • Save as PDF
  • Close Drawing
  • Close the room

The code that emerges from it is as follows:

’ ******************************************************************************
' C:\Users\bguyetand\AppData\Local\Temp\swx4952\Macro1.swb - macro recorded on 09/20/23 by BGuyetand
’ ******************************************************************************
Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

' Save As
longstatus = Part.SaveAs3("C:\Users\bguyetand\Desktop\Part1.STEP", 0, 2)

' Open
Set Part = swApp.OpenDoc6("C:\Users\bguyetand\Desktop\Part1.SLDDRW", 3, 0, "", longstatus, longwarnings)
Dim swDrawing As DrawingDoc
Set swDrawing = Part
Set Part = swApp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView
myModelView.FrameLeft = 0
myModelView.FrameTop = 22
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
swApp.ActivateDoc2 "Part1 - Sheet1", False, longstatus
Set Part = swApp.ActiveDoc
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized

' Save As
longstatus = Part.SaveAs3("C:\Users\bguyetand\Desktop\Pièce1.pdf", 0, 2)
Part.SheetPrevious

' Redraw
Part.GraphicsRedraw2

' Zoom to Area
Part.ViewZoomTo2 0, 0, 0, 0.1, 0.1, 0.1

' Zoom to Area
Part.ViewZoomTo2 0, 0, 0, 0.1, 0.1, 0.1

' Save As
longstatus = Part.SaveAs3("C:\Users\bguyetand\Desktop\Part1.DXF", 0, 2)

' Close Document
Set swDrawing = Nothing
Set Part = Nothing
swApp.CloseDoc "Part1 - Sheet1"
Set Part = swApp.ActiveDoc
Set myModelView = Part.ActiveView
myModelView.FrameLeft = 0
myModelView.FrameTop = 0
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
swApp.ActivateDoc2 "Part1.SLDPRT", False, longstatus
Set Part = swApp.ActiveDoc
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized

' Close Document
Set swPart = Nothing
Set Part = Nothing
swApp.CloseDoc "Part1.SLDPRT"
End Sub

My problem is that currently, when I launch this maccro, it opens my test room that I had created (A simple cube) to unfold the steps.
In addition, it records all on the desktop.

Is it possible to explain to me in my maccro:

  • How can I replace my "test" part with the "active" part on my screen?
  • How do I save the files in the open room folder?

I hope I have been clear enough in my explanations :smiley:

Thank you in advance for the time you will give me!! :stuck_out_tongue:

1 Like

Hello
Look at this code to save a part or assembly as a step.
It is well commented it should help you a lot:

    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
      
    Sub main()
      
'1-) on vérifie qu'un document est ouvert
  Debug.Print "1-)On vérifie qu'un document est ouvert"
  Set swApp = CreateObject("SldWorks.Application")
  Set swModel = swApp.ActiveDoc        ' On récupère le document d'ouvert
  If swModel Is Nothing Then           ' On vérifie si un document est ouvert
    MsgBox "Pas de document d'ouvert." + Chr$(13) + _
           "Une pièce ou assemblage SolidWorks doit être ouverte, " + Chr$(13) + _
           "avant de relancer cette macro."
  Else
    FileTyp = swModel.GetType
    If ((FileTyp = swDocPART) Or (FileTyp = swDocASSEMBLY)) Then 'Si le document est une pièce ou un assemblage

'2-)On vérifie si une config sym existe
        'On vérifie si la configuration active est une configuration dérivée (Si Symétrique retour config defaut)
        Set swCompModelConfig = swModel.GetActiveConfiguration
        Dim vConfigName As Variant
        Dim swParentConfig As SldWorks.Configuration
        Dim swConfMgr As SldWorks.ConfigurationManager
        Dim partTitle As String
        partTitle = swModel.GetTitle
        Debug.Print "partTitle:" & partTitle
                        
        If swCompModelConfig.IsDerived Then
        Debug.Print "Configuration dérivée:" & swCompModelConfig.IsDerived
        Debug.Print "Pause"
                Dim swConfig As SldWorks.Configuration
                    Set swConfig = swModel.GetConfigurationByName(vConfigName)
                    ' Process parent
                    Set swParentConfig = swCompModelConfig.GetParent
                    If Not swParentConfig Is Nothing Then
                        Debug.Print "      Parent = " & swParentConfig.Name
                        swModel.ShowConfiguration2 (swParentConfig.Name)
                    End If
        End If
        
'2-)On enregistre en step
        Debug.Print "2-)On enregistre en step"
        swModel.Extension.SaveAs Left(swModel.GetPathName, InStrRev(swModel.GetPathName, ".")) & "STEP", 0, 0, Nothing, 0, 0
        MsgBox (Left(swModel.GetPathName, InStrRev(swModel.GetPathName, ".")) & "step sauvegardé")
                  
        
    




'4-)On enregistre en step la config sym si existante
         'Pour toutes les configurations du modèle 3D
            configNames = swModel.GetConfigurationNames
            For Each ConfigName In configNames
            Debug.Print "4-Nom de config:" & ConfigName
            Set swConfig = swModel.GetConfigurationByName(ConfigName)
            Set swCustPropMgr = swConfig.CustomPropertyManager
                If ConfigName Like "*Sym*" Then
                        If ConfigName Like "*Sym*Sym*" Then
                            MsgBox "Attention Symétrie de Symétrie merci de corriger votre assemblage et supprimer cette configuration: " & ConfigName
                        End If
                        'Mis en commentaire jusqu'au passage en pièce SYM sur 2 MEP
                        'If swModel.GetCustomInfoValue(ConfigName, "Symetrie") <> "" Then
                            'On active la config Sym
                            swModel.ShowConfiguration2 (ConfigName)
                            Debug.Print "4)On enregistre en step le Sym"
                            Debug.Print Left(swModel.GetPathName, Len(swModel.GetPathName) - 7) & "-SYM.STEP"
                            swModel.Extension.SaveAs Left(swModel.GetPathName, Len(swModel.GetPathName) - 7) & "-SYM.STEP", 0, 0, Nothing, 0, 0
                            MsgBox (Left(swModel.GetPathName, Len(swModel.GetPathName) - 7) & "-SYM" & ".step sauvegardé")
                        'End If
                    End If
                Next
        
        
    Else
      MsgBox "Pas de pièce ou assemblage d'ouvert." + Chr$(13) + _
           "Une pièce ou assemblage SolidWorks doit être ouvert, " + Chr$(13) + _
           "avant de relancer cette macro."
    End If          ' Fin vérification si un plan est ouvert
    End
  End If            ' Fin vérification si un document est ouvert

End Sub


1 Like

Thank you for your answer,

I look into it as soon as I have time.

Hello;

To complete @sbadenis's answer:

The important information of his proposal is:

=>… Set swModel = swApp.ActiveDoc (to work on the active Solidworks document)
=> … swModel.GetPathName (to retrieve the (full) path of the document opened in Solidworks)

to gently get your hands on macros, I advise you (in addition to the Solidworks API help):

and

(Very interesting videos on CadSharp)

and more generally (mainly Excel but VBA is VBA):

And always remember to comment on your creations (because, in ten years, you won't necessarily understand what you did at the time...)

Kind regards.

2 Likes

Hello

if it may interest you, here I have the macro to do in PDF, so you will have a code for the PDF :slight_smile:
For PDF.swp printing (23 KB)

And here for the dxf output in 1:1 scale
Enregistre_DXF_echel_1-1.swp (34 KB)

It may be useful to you

1 Like

Thank you all for your answers,

I already think I can get out of my mess a little more.

That's great :slight_smile:

And not only in 10 years... Sometimes, within 15 days!

Otherwise, I think there have been a lot of links for the help already but don't hesitate to come back to share your final macro with us :slight_smile:

Hello

To be quite frank, I looked into it yesterday, but it still doesn't work :smiley:

In fact, I have the impression that the codes that come out of the "Save" mode of the maccros are not the same as the ones you seem to type by hand.
So I scratch my head a little before coming here to cry out for help :smiley:

The code generated by the recorder is full of errors and is just used to identify a few possible functions.
Anything related to MyModelView is completely useless, for example.
Here is the clean code and seems all more functional, with the comments that go well.
To replace part1 with the active part, you retrieve the name of the active part with: swModel.GetPathName
Then via VBA manip we remove the extension for the different recordings.
The code:

'Déclarations
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()
Set swApp = Application.SldWorks


'1-) on vérifie qu'un document est ouvert
  Set swModel = swApp.ActiveDoc        ' On récupère le document d'ouvert
  If swModel Is Nothing Then           ' On vérifie si un document est ouvert
    MsgBox "Pas de document d'ouvert." + Chr$(13) + _
           "Une pièce ou assemblage SolidWorks doit être ouverte, " + Chr$(13) + "avant de relancer cette macro."
  Else
    FileTyp = swModel.GetType
    If ((FileTyp = swDocPART) Or (FileTyp = swDocASSEMBLY)) Then 'Si le document est une pièce ou un assemblage


        
'2-)On enregistre en step
        swModel.Extension.SaveAs Left(swModel.GetPathName, InStrRev(swModel.GetPathName, ".")) & "STEP", 0, 0, Nothing, 0, 0 'on sauvegarde l'assemblage ou pièce active en step en récupérant le nom sans l'extension
        MsgBox (Left(swModel.GetPathName, InStrRev(swModel.GetPathName, ".")) & "step sauvegardé") 'On affiche dans une box le chemin du step. Au besoin ajouter le symbol' au début de la ligne pour la passer en commentaire
        
'3-)On ouvre la MEP du modèle actif
        Set swDraw = swApp.OpenDoc6(Left(swModel.GetPathName, InStrRev(swModel.GetPathName, ".")) & "slddrw", 3, 0, "", longstatus, longwarnings) 'Ouverture de la MEP
        longstatus = swDraw.SaveAs3(Left(swModel.GetPathName, InStrRev(swModel.GetPathName, ".")) & "pdf", 0, 2) 'On l'enregistre en pdf avec le même nom .pdf
        MsgBox (Left(swModel.GetPathName, InStrRev(swModel.GetPathName, ".")) & "pdf sauvegardé") 'On affiche dans une box le chemin du pdf. Au besoin ajouter le symbol' au début de la ligne pour la passer en commentaire

'4-)On enregistre la MEP en dxf
        longstatus = swDraw.SaveAs3(Left(swDraw.GetPathName, InStrRev(swModel.GetPathName, ".")) & "dxf", 0, 2) 'On l'enregistre en pdf avec le même nom .dxf
        MsgBox (Left(swModel.GetPathName, InStrRev(swModel.GetPathName, ".")) & "dxf sauvegardé") 'On affiche dans une box le chemin du pdf. Au besoin ajouter le symbol' au début de la ligne pour la passer en commentaire

'5-)On ferme les documents
        swApp.CloseDoc Left(swModel.GetPathName, InStrRev(swModel.GetPathName, ".")) & "slddrw" 'On ferme la MEP
        swApp.CloseDoc swModel.GetPathName 'On ferme le model actif au besoin ajouter ' pour mettre la ligne en commentaire et ne pas fermer le modèle actif

        Set swDrawing = Nothing 'On vide la variable
        Set swModel = Nothing 'On vide la variable
        
    Else
      MsgBox "Pas de pièce ou assemblage d'ouvert." + Chr$(13) + _
           "Une pièce ou assemblage SolidWorks doit être ouvert, " + Chr$(13) + _
           "avant de relancer cette macro."
    End If          ' Fin vérification si un plan est ouvert
    End
  End If            ' Fin vérification si un document est ouvert

End Sub
1 Like

Sbadenis,

A huge thank you for your help,

I would never have found the solution alone in such a short time,
The comments are great and will allow me to guide me on my next maccros.

I'll post my creations that work when I have some :slight_smile:

Thanks again to the team for your time!

If that's what you wanted, don't forget to close the topic by selecting the answer that solved your problem. Thank you

2 Likes

It's done :ok_hand: