PDF-DXF-STEP from a plan

Hello
I managed to combine existing macros that I gleaned from right and left.
Thanks to you, my job is much easier.

I managed to make this macro with an index management that avoids overwriting an existing index but I don't understand why the .step doesn't work for an overview plan and opens the first piece and does the . Step. I thought I would open the file from the first view of the plan.
I go around the problem without seeing it.

There must be small errors or inconsistencies in all this, but I feel that I am touching the solution.

The error is in the paragraph "preparing the 3D for STEP"

Thank you to those for whom VBA is a natural language. I admire you^^

saisissez ou collez d'PDF DXF & STEP indicé.swp - ------------10 / 11 / 2023'

Dim swApp           As SldWorks.SldWorks
Dim swModel         As SldWorks.ModelDoc2
Dim swDraw          As SldWorks.DrawingDoc
Dim swView          As SldWorks.View
Dim Filepath        As String
Dim FileName        As String
Dim boolstatus      As Boolean
Dim longstatus      As Long
Dim longwarnings    As Long
Dim swCustProp      As SldWorks.CustomPropertyManager
Dim Value           As String
Dim réponse         As Integer


Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

' Vérifie si un plan est ouvert'
If (swModel Is Nothing) Or (swModel.GetType <> swDocDRAWING) Then

swApp.SendMsgToUser ("Seulement à partir d'un plan !!")

Exit Sub

End If

'-------------------------------------------------- Récupération de l'indice

Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swDraw = swModel
Set swView = swDraw.GetFirstView 'Active le fond de plan
Set swView = swView.GetNextView 'Active la première vue après le fond de plan
Set swModel = swView.ReferencedDocument 'Récupère le fichier associé à la première vue
Set swModelDocExt = swModel.Extension
Set swCustProp = swModelDocExt.CustomPropertyManager("")
swCustProp.Get3 "Indice en cours", False, "", Value 'Récupération de la propriété "Indice en cours"

'-------------------------------------------------- Préparation du 3D pour STEP

Filepath = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\"))

Set swModel = swApp.ActiveDoc
Set myModelView = swModel.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
swApp.ActivateDoc2 "", False, longstatus
Set swModel = swApp.ActiveDoc
swModel.ClearSelection2 True
Set myModelView = swModel.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized

FileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
FileName = Left(FileName, Len(FileName) - 7) & "-" & Value & ".step"

'--------------------------------------------------- Vérif avant écrasemant ou création'

If Len(Dir(Filepath & FileName)) = 0 Then

GoTo CréationEcrasement
Else
réponse = MsgBox("Ecraser l'existant", vbOKCancel + vbQuestion, "/!\ INDICE EXISTANT /!\")

If réponse = vbOK Then
GoTo CréationEcrasement
End If

If réponse = vbCancel Then
End If
Exit Sub

CréationEcrasement:

'-------------------------------------------------- Enregistrement du 3D en STEP

swModel.SaveAs3 Filepath & FileName & "", 0, 0

Set swModel = Nothing
swApp.CloseDoc ""
Set swModel = swApp.ActiveDoc


'-------------------------------------------------- Enregistrement du plan en PDF

Filepath = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\"))

FileName = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1)
FileName = Left(FileName, Len(FileName) - 7) & "-" & Value & ".pdf"

swDraw.SaveAs3 Filepath & FileName & "", 0, 0


'-------------------------------------------------- Enregistrement du plan en DXF

Filepath = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\"))

FileName = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1)
FileName = Left(FileName, Len(FileName) - 7) & "-" & Value & ".dxf"

swDraw.SaveAs3 Filepath & FileName & "", 0, 0

End If
End Sub


u code ici

Hello

Try it like this:


Filepath = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\"))

FileName = swView.GetReferencedModelName
swApp.ActivateDoc2 FileName, False, longstatus
Set swModel = swApp.ActiveDoc
swModel.ClearSelection2 True
Set myModelView = swModel.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized

FileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
FileName = Left(FileName, Len(FileName) - 7) & "-" & Value & ".step"

The code is optimizeable, there are useless function calls that can overload the memory

1 Like

Yes, same as @Cyril.f
You do a SetModel several times that is a problem.
With this code it works, but there is probably still a bit of cleaning to do.
The debug.print allows you to see the name of the current model in the edit window. (To comment on or delete later)


Dim swApp           As SldWorks.SldWorks
Dim swModel         As SldWorks.ModelDoc2
Dim swDraw          As SldWorks.DrawingDoc
Dim swView          As SldWorks.View
Dim Filepath        As String
Dim FileName        As String
Dim boolstatus      As Boolean
Dim longstatus      As Long
Dim longwarnings    As Long
Dim swCustProp      As SldWorks.CustomPropertyManager
Dim Value           As String
Dim réponse         As Integer


Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

' Vérifie si un plan est ouvert'
If (swModel Is Nothing) Or (swModel.GetType <> swDocDRAWING) Then

swApp.SendMsgToUser ("Seulement à partir d'un plan !!")

Exit Sub

End If

'-------------------------------------------------- Récupération de l'indice

Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swDraw = swModel
Set swView = swDraw.GetFirstView 'Active le fond de plan
Set swView = swView.GetNextView 'Active la première vue après le fond de plan
Set swModel = swView.ReferencedDocument 'Récupère le fichier associé à la première vue
Set swModelDocExt = swModel.Extension
 Debug.Print swModel.GetPathName & "  [" & swModel.Visible & "]"

Debug.Print "  " & swModel.GetTitle & " [" & swModel.GetType & "]"
Set swCustProp = swModelDocExt.CustomPropertyManager("")
swCustProp.Get3 "Indice en cours", False, "", Value 'Récupération de la propriété "Indice en cours"

'-------------------------------------------------- Préparation du 3D pour STEP

Filepath = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\"))
Debug.Print Filepath

'Set swModel = swApp.ActiveDoc
'Set myModelView = swModel.ActiveView
'myModelView.FrameState = swWindowState_e.swWindowMaximized
'swApp.ActivateDoc2 "", False, longstatus
'Set swModel = swApp.ActiveDoc
swModel.ClearSelection2 True
'Set myModelView = swModel.ActiveView
'myModelView.FrameState = swWindowState_e.swWindowMaximized

FileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
FileName = Left(FileName, Len(FileName) - 7) & "-" & Value & ".step"
Debug.Print FileName
'--------------------------------------------------- Vérif avant écrasemant ou création'

If Len(Dir(Filepath & FileName)) = 0 Then

GoTo CréationEcrasement
Else
réponse = MsgBox("Ecraser l'existant", vbOKCancel + vbQuestion, "/!\ INDICE EXISTANT /!\")

If réponse = vbOK Then
GoTo CréationEcrasement
End If

If réponse = vbCancel Then
End If
Exit Sub

CréationEcrasement:

'-------------------------------------------------- Enregistrement du 3D en STEP

swModel.SaveAs3 Filepath & FileName & "", 0, 0

Set swModel = Nothing
swApp.CloseDoc ""
Set swModel = swApp.ActiveDoc


'-------------------------------------------------- Enregistrement du plan en PDF

Filepath = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\"))

FileName = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1)
FileName = Left(FileName, Len(FileName) - 7) & "-" & Value & ".pdf"

swDraw.SaveAs3 Filepath & FileName & "", 0, 0


'-------------------------------------------------- Enregistrement du plan en DXF

Filepath = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\"))

FileName = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1)
FileName = Left(FileName, Len(FileName) - 7) & "-" & Value & ".dxf"

swDraw.SaveAs3 Filepath & FileName & "", 0, 0

End If
End Sub

Filepath is rewritten 3-4 times with the same content (so value)= useless.
Add debug.print to see the values of your variables as you run in step this will help you a lot.

1 Like

For me it's more the set swapp that is the problem. It loads the SW process in memory several times

2 Likes

Correct @Cyril.f , I hadn't actually seen to comment on all the lines except the 1st with Set swApp.
The set is used to initialize your variable so normally 1 set per variable is enough.

2 Likes

Thank you gentlemen,

You're on top. it works like hell!!
I understand a little better what I did.

the first solution proposed by Cyril had unblocked me but now you can't even see the 3D open. Perfect!

Well done, it seemed to me that opening the file was necessary/mandatory to export. But as it is loaded with memory when the drawing is opened, it can be dispensed with.

1 Like