DXF export of the current tab of a solidworks drawing

Hello

I searched and I couldn't find a small macro that exports the current tab of a solidworks drawing in dxf format (for laser cutting) with the exact name of the current plan tab as the file name

Anyone have the walkthrough?

Note that I suck at VBA programming

Thanks in advance

Hello

look at this macro, try it and tell me if it's okay with you

Sub main()

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim montab As Variant
Dim montab2 As Variant
Dim montab3 As Variant
Dim inintern As String
Dim Name As String
Dim Path As String
Dim name As String
Dim FilePath        As String
Dim PathSize        As Long
Dim PathNoExtension As String
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

FilePath = swModel.GetPathName
PathSize = Strings.Len(FilePath)
PathNoExtension = Strings.Left(FilePath, PathSize - 7)

montab = Split(swModel.GetPathName, "\", -1)
interm = montab(UBound(montab))
name = Mid(interm, 1, Len(interm) - 7)
montab2 = Split(name, ".", 2)
Name = montab2(0)

'montab3 = Split(name, "$", 2)
'Name = Montab3(1)

path = PathNoExtension & ".dxf" 'format in which you want to save
'pathMEP = swModel.GetPathName
'FullFileName = Mid(MEPPATH, 1, Len(MEPPATH) - 7)
'Elements = Split(FullFileName, "\", -1)
'FullFileName = Elements(0)

'MessAlert = MsgBox("Do you accept this file?" + FullFileName, vbYesNo)
'If MessAlert = vbNo Then Exit Sub
name = name & " - Sheet1"
Set Part = swApp.ActiveDoc
Set SelMgr = Part.SelectionManager
Part.SaveAs2 path, 0, True, False
Part.Save2 False
Set Part = Nothing
swApp.CloseDoc swModel.GetTitle
'Set swModel = Nothing: Set swApp = Nothing
 
End Sub

 

 

Hello gwygwy

Thank you, it works!

But it saves with the file name and not with the name of the current tab

Is it possible to modify?

Thanks in advance

 

Hello

I'm not a specialist so I was able to put the name of the document and the name of the sheet but no better.

Sub main()

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim montab As Variant
Dim montab2 As Variant
Dim montab3 As Variant
Dim inintern As String
Dim Name As String
Dim Name2 As String
Dim Path As String
Dim name As String
Dim FilePath        As String
Dim PathSize        As Long
Dim PathSizeTitle   As Long
Dim PathNoExtension As String
Dim PathNoExtension2 As String
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

FilePath = swModel.GetPathName
PathSize = Strings.Len(FilePath)
PathNoExtension = Strings.Left(FilePath, PathSize - 7)


name2 = swModel.GetTitle

montab = Split(swModel.GetPathName, "\", -1)
interm = montab(UBound(montab))
name = Mid(interm, 1, Len(interm) - 7)
montab2 = Split(name, ".", 2)
Name = montab2(0)

PathSizeTitle = Strings.Len(name)
PathNoExtension2 = Strings.Left(PathNoExtension, PathSize - PathSizeTitle - 7)

'montab3 = Split(name, "$", 2)
'Name = Montab3(1)

path = PathNoExtension2 & name2 & ".dxf" 'format in which format you want to save
'pathMEP = swModel.GetPathName
'FullFileName = Mid(MEPPATH, 1, Len(MEPPATH) - 7)
'Elements = Split(FullFileName, "\", -1)
'FullFileName = Elements(0)

'MessAlert = MsgBox("Do you accept this file?" + FullFileName, vbYesNo)
'If MessAlert = vbNo Then Exit Sub
'name = name & " - Sheet1"
Set Part = swApp.ActiveDoc
Set SelMgr = Part.SelectionManager
Part.SaveAs2 path, 0, True, False
Part.Save2 False
Set Part = Nothing
'swApp.CloseDoc swModel.GetTitle
Set swModel = Nothing: Set swApp = Nothing
 
End Sub

 

@gwygwy the proposed macro is far from clean in terms of code,  duplicate, useless code... And moreover I don't see how you get the Name of the sheet. The only leaf name is typed in the mano "Leaf1"

To export the file with the name of the tab, see this topic to which I replied without knowing if the answer suited you or not.

https://www.lynkoa.com/forum/solidworks/export-diff%C3%A9renci%C3%A9-en-pdf-et-dxf-des-onglets-de-mise-en-plan-par-une-macro

If necessary it can be adapted without putting the condition if Cutout present in the tab name and without removing it from the file name, as needed.

But in general we avoid opening 2 very close or identical subjects.

 

Hi @sbadenis 

Yes the code is not clean, I'm not a pro. I've modified an existing macro where there's still some code that doesn't serve any purpose, but I'm keeping it in case I need it in another macro.

And no, he's not hit as you say, there's a ' in front of the line. So this line is useless.

I recover with get.title, that's all I found. But if you have a macro that works for him, do it you seem to code well.

1 Like

@gwygwy no worries, it was just for you! And indeed I hadn't seen the get.title, but in itself you get the name of the document + the name of the active sheet

To retrieve the name of each sheet and export each sheet with the name of the sheet as the export name, you must create a loop on the sheets as on the macro provided in the link. If you're interested I invite you to watch, there's nothing too complicated, 2 years ago I had no knowledge of vba and it's by trial and error that I got into it.

For the macro I made one in the other topic but no answer @Fennec_Flegmatique seems to be absent to the subscribers!

Friends, I'm not at the abscent subscribers

Thank you for your contributions and for the time you spend responding to me

I'm making progress in understanding the codes (I'm a Boeotian), but it's a bit long

I haven't solved my subject yet

I'll let you know if I find it or if I'm worth!