Need macro to export to 2 diffrent locations

Library for macros
User avatar
pitbullxp
Posts: 14
Joined: Tue May 11, 2021 3:13 am
Answers: 0
x 4
x 4

Need macro to export to 2 diffrent locations

Unread post by pitbullxp »

Hi all,

currently we use a macro to export PDF/DXF and STEP files to the folder of the part/drawing. the macro takes the filename and add the revision to it and it works great(unless file has the same name and is checked in, then whole solidworks will stop). We can simply locate the files and copy them to the right place so the purchasing team can start their job. But there are time's that we forget something, they will search for the file in the project folder, they cant find it, then we get a coll, drop everything, look up the file and place it in the right folder. Give them a call to say we placed the file.

To fix that i want to implement a dump folder. So when we use the macro, it will place a file in the folder of the part/drawing, but it will also place te same file in a folder tekening dump (drawing dump)

I've spent to much time searching for a macro with the same idea, but i only get results like, multiple drawings in 1 pdf, multiple parts at the same time ect.

I don't have any experience in coding, but i want to learn and understand them, so if you can help me with the code en explain it a bit, it would be very appreciated
User avatar
zwei
Posts: 701
Joined: Mon Mar 15, 2021 9:17 pm
Answers: 18
Location: Malaysia
x 185
x 600

Re: Need macro to export to 2 diffrent locations

Unread post by zwei »

Without looking at the actual code i cant really explain it well...

But ideally what you need is just a separate line that call out a 2nd save function, somethings like the below
You might need to pre-process your file

Code: Select all

swModel.SaveAs SaveFileName
swModel.SaveAs SaveFileNameDump
SaveFileNameDump is point to your dump folder

Edit:
See example below, it will save the file as step into two folder, colored in red
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim SavePath As String
Dim SavePathDump As String
Dim SaveFileName As String

'Example macro
'Macro will save as step into 2 different folder,

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc 'Get active document
If swModel Is Nothing Then 'Display message and terminate if nothing is open
MsgBox "Please open a file first.", vbOKOnly
End
End If

'Folder path, path need to end with \
SavePathDump = "C:\CADForum\6303\DumpFolder" & "\"
SavePath = "C:\CADForum\6303\PartFolder" & "\"

SaveFileName = swModel.GetPathName 'Get path name of active document
SaveFileName = Mid(SaveFileName, InStrRev(SaveFileName, "\") + 1) 'Remove address, keep only file name (with extension)
SaveFileName = Left(SaveFileName, InStrRev(SaveFileName, ".") - 1) 'Remove file extension

swModel.SaveAs SavePath & SaveFileName & ".step"
swModel.SaveAs SavePathDump & SaveFileName & ".step"

Set swModel = Nothing


End Sub
Far too many items in the world are designed, constructed and foisted upon us with no understanding-or even care-for how we will use them.
User avatar
pitbullxp
Posts: 14
Joined: Tue May 11, 2021 3:13 am
Answers: 0
x 4
x 4

Re: Need macro to export to 2 diffrent locations

Unread post by pitbullxp »

Hi thanks for the help.

The macro i use to save .STEP is by Deepak and i had to make some changes to some values so it would work for me.
With the changes i made with insperation from your code it worked. It can now save on the original folder and the dump folder.



I also changed the .STEP to a combination for .DXF and .PDF but it will add "sheet 1" in the middle

Code: Select all

    Set Part = swApp.ActiveDoc
    longstatus = Part.SaveAs3(FilePath & swModel.GetTitle & "-" & Rev & ".DXF", 0, 0)
    longstatus = Part.SaveAs3(FilePathDump & swModel.GetTitle & "-" & Rev & ".DXF", 0, 0)
    longstatus = Part.SaveAs3(FilePath & swModel.GetTitle & "-" & Rev & ".PDF", 0, 0)
    longstatus = Part.SaveAs3(FilePathDump & swModel.GetTitle & "-" & Rev & ".PDF", 0, 0) 
Here the code i changed, if you see something that's unnecessary or can be better i like to know.

Code: Select all


' ------------------------------------------------------------------------------
' DXF Saving part Written by: Deepak Gupta (http://gupta9665.wordpress.com/)
' ------------------------------------------------------------------------------

Option Explicit

    Dim swApp               As Object
    Dim swModel             As SldWorks.ModelDoc2
    Dim swCustPrpMgr        As SldWorks.CustomPropertyManager
    Dim nResponse           As Integer
    Dim Rev                 As String
    Dim Thickness           As String
    Dim FilePath            As String
    Dim FilePathDump        As String
    Dim Part                As Object
    Dim SelMgr              As Object
    Dim boolstatus          As Boolean
    Dim longstatus As Long, longwarnings As Long
    Dim Feature As Object
    Dim Filename, Name As String
    Dim Pos, Length As Long

Sub DesignExport()

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

'Get Document Path

    FilePath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\"))
    FilePathDump = "\\SANINAS\Productie\2. projecten\#Tekening DUMP" & "\"
    

'Get Custom Property
  
   Set swCustPrpMgr = swModel.Extension.CustomPropertyManager("")
   
   swCustPrpMgr.Get3 "Revision", False, "##", Rev

   

    
    
'Save As STEP
    Set Part = swApp.ActiveDoc
    longstatus = Part.SaveAs3(FilePath & swModel.GetTitle & "-" & Rev & ".STEP", 0, 0)
    longstatus = Part.SaveAs3(FilePathDump & swModel.GetTitle & "-" & Rev & ".STEP", 0, 0)





End Sub

What can i change so the drawing export part will work?
User avatar
zwei
Posts: 701
Joined: Mon Mar 15, 2021 9:17 pm
Answers: 18
Location: Malaysia
x 185
x 600

Re: Need macro to export to 2 diffrent locations

Unread post by zwei »

pitbullxp wrote: Wed May 12, 2021 9:07 am What can i change so the drawing export part will work?
Sorry i am abit confused... What is not working?
I took a quick glance at the macro and everything seem fine


PS:
Some suggestion... personally i will try to avoid using GetTitle as if i remember correctly the value return by GetTitle could varies depend on user Window explorer setting. My personal preference is to use GetPathName and then strip the path and extension (if any) using Mid and Left

Code: Select all

SaveFileName = swModel.GetPathName 'Get path name of active document
SaveFileName = Mid(SaveFileName, InStrRev(SaveFileName, "\") + 1) 'Remove address, keep only file name (with extension)
SaveFileName = Left(SaveFileName, InStrRev(SaveFileName, ".") - 1) 'Remove file extension
Far too many items in the world are designed, constructed and foisted upon us with no understanding-or even care-for how we will use them.
User avatar
pitbullxp
Posts: 14
Joined: Tue May 11, 2021 3:13 am
Answers: 0
x 4
x 4

Re: Need macro to export to 2 diffrent locations

Unread post by pitbullxp »

When I get back to work I will look at the code to change it to getpathname.

When I change the export ripe to PDF or dxf it will add the sheet name. In the middle.

So filename-sheetname-revision.pdf
User avatar
zwei
Posts: 701
Joined: Mon Mar 15, 2021 9:17 pm
Answers: 18
Location: Malaysia
x 185
x 600

Re: Need macro to export to 2 diffrent locations

Unread post by zwei »

pitbullxp wrote: Thu May 13, 2021 6:48 am When I get back to work I will look at the code to change it to getpathname.

When I change the export ripe to PDF or dxf it will add the sheet name. In the middle.

So filename-sheetname-revision.pdf
Ah yes, thats the issue with GetTitle
GetTitle will get whatever that is display on your SOLIDWORKS window, for drawing it will be XXXXX-SheetX, thats why you get the sheet name when you export as pdf/dxf

Change it to GetPathName and it should work properly :D.
Far too many items in the world are designed, constructed and foisted upon us with no understanding-or even care-for how we will use them.
User avatar
pitbullxp
Posts: 14
Joined: Tue May 11, 2021 3:13 am
Answers: 0
x 4
x 4

Re: Need macro to export to 2 diffrent locations

Unread post by pitbullxp »

That was the solution, it works. Thank you
User avatar
Stefan Sterk
Posts: 37
Joined: Tue Aug 10, 2021 2:40 am
Answers: 3
x 51
x 77

Re: Need macro to export to 2 diffrent locations

Unread post by Stefan Sterk »

Why would you perform the export twice and not just copy paste the file that you have exported with the FileCopy function?

Code: Select all

FirstPathName = FilePath & SaveFileName  & "-" & Rev & ".STEP"
SecondPathName = FilePathDump & SaveFileName & "-" & Rev & ".STEP"
' Export STEP to first path
longstatus = Part.SaveAs3(FirstPathName , 0, 0)
' Copy paste exported STEP to second path
FileCopy FirstPathName, SecondPathName 
Or use

Code: Select all

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Call fso.CopyFile FirstPathName, SecondPathName
Jean-Paul Commeene
Posts: 1
Joined: Mon Nov 07, 2022 12:46 am
Answers: 0

Re: Need macro to export to 2 diffrent locations

Unread post by Jean-Paul Commeene »

Hi all,

As i am new here on the forum i don't know if my question will be on the correct place.

For quit some time i'm looking for a macro/solution to save my part and drawing to different file types in the same folder. the SLDPRT file should be saved as a flatt patern dxf file and a step file. the slddrw should be saved as a PDF.

writinig a macro can be the solution but i do not have any experience in this.

does anybody have this on the shelf?

Many thanks!

JP
User avatar
gupta9665
Posts: 410
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 433
x 447

Re: Need macro to export to 2 diffrent locations

Unread post by gupta9665 »

JP, how you want the macro to work? I mean do you want it to save STEP when part model is active and save PDF when drawing is active. OR do you want it to save PDF and STEP both when drawing is active.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
J_the_King
Posts: 1
Joined: Tue Apr 04, 2023 8:55 am
Answers: 0

Re: Need macro to export to 2 diffrent locations

Unread post by J_the_King »

Hi @gupta9665,

I also used your code as reference.
But I have an issue when I have multiple documents open. Creating a PDF does work, but when creating a STEP the previous used part will be used instead of the part referenced to the drawing.

Can you help me with that?

I already appreciate your code very much.

Kind regards,

Jordy
Post Reply