Macro Solidworks

Hi all

I want to make a macro to export my presentations in PDF & DWG format.

So far no worries I'll get there, but I'd like my PDF & DWG file created via this macro to have the name of my open presentation?

That's where I drank, I understood the macro editing system, but I don't know what to enter.

' Save As
longstatus = Part.SaveAs3("C:\Users\dao\Desktop\MACRO_PDF-DWG\ here. DWG", 0, 0)
End Sub

I think everything is playing where I wrote here , you probably have to enter something like "Filename" right?
Thanks in advance;)

EDIT: If you have links from where I can retrieve all the macro commands I'm interested:D

Hello

It's apparently the GetTitle function you're looking for. So in your macro you have to retrieve this GetTitle and rework the path of your file with the result obtained from the GetTitle.

Kind regards

 

2 Likes

Hello and thank you for the answer,

Sorry, it's the 1st time I've set foot in the Macro, I don't understand, it doesn't work^^

' Save As
longstatus = Part.SaveAs3("C:\Users\dao\Desktop\MACRO_PDF-DWG\GetTitle.PDF", 0, 0)
End Sub

Isn't it?

 

Hello

No, it's not like that at all!

GetTitle is an API function that allows you to retrieve, in text form, the name of the file opened in Solidworks, there you just wrote that your PDF file should be called GetTitle.PDF.

If you've never done a macro and/or programming, you'll either have to do a little research on how a programming language works before doing it, or you will have to post your full macro and explain how you want to change the way it works, hoping that someone will be willing to stick to it.

I admit I don't understand the remark "So far no worries I'm getting there" at the beginning of your question.

Kind regards

Yes, you have to get the name of your file first with the getTitle function.

I also admit that I don't really understand the "So far no worries I'm getting there"...

Try this:


'déclaration des variables
Dim swApp As Object
Dim Part As Object
Dim longstatus As Long
dim nomfichier as string

Sub enregistrement()

'initialisation des variables objet
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc

'récupération du nom fichier part
Nomfichier = Part.GetTitle

'suppression de l'extension solidworks
Nomfichier = Strings.Left(Nomfichier, Len(Nomfichier) - 7)

'enregistrement du fichier en pdf
longstatus = Part.SaveAs3("C:\Users\dao\Desktop\MACRO_PDF-DWG\" & nomfichier & ".PDF", 0, 0)

end sub

 

 

2 Likes

Ok no problem.

Basically I understand how it works if you will, but indeed I have to ask myself more about the problem and the programming.

In fact it's relatively simple and I even think that this macro must already exist.

I just wish that once my drawing is finished, I click on a button, which generates a PDF and a DWG for me in a folder but with the name of the drawing open.

If I open a new drawing, I want this macro to generate other distinct and named files with this drawing.

Sorry not sure I'm too clear...

This is what the example given by bengous does, all you have to do is add the DWG recording on the same principle as for the PDF ...

1 Like

Yes I put you with the pdf extension but I think it should work if you add the line

longstatus = Part.SaveAs3("C:\Users\dao\Desktop\MACRO_PDF-DWG\" & nomfichier & ".DWG", 0, 0)

 

Just after the one who creates your pdf. And I don't know if it works, I haven't run this macro.

1 Like

I know well D.roger there was a bug I couldn't see the comments anymore we had to answer at the same time in fact.
I'll test it and get back to you to tell you;)
Thank you in any case:D

EDIT: My visual basic is in English, can it play?

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
Part.SheetPrevious
Part.SheetNext
boolstatus = Part.EditRebuild3()

' 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

' Zoom To Fit
Part.ViewZoomtofit2

' Redraw
Part.GraphicsRedraw2

' Save As
longstatus = Part.SaveAs3("C:\Users\dao\Desktop\MACRO_PDF-DWG\???.DWG", 0, 0)

' Save As
longstatus = Part.SaveAs3("C:\Users\dao\Desktop\MACRO_PDF-DWG\???", 0, 0)
End Sub

 

I made you a code that you didn't even use...

Well try to do it like this

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
dim nomfichier as string

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
Part.SheetPrevious
Part.SheetNext
boolstatus = Part.EditRebuild3()

' 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

' Zoom To Fit
Part.ViewZoomtofit2

' Redraw
Part.GraphicsRedraw2

'récupération du nom fichier part
Nomfichier = Part.GetTitle

'suppression de l'extension solidworks
Nomfichier = Strings.Left(Nomfichier, Len(Nomfichier) - 7)

' Save As
longstatus = Part.SaveAs3("C:\Users\dao\Desktop\MACRO_PDF-DWG\" & nomfichier & ".DWG", 0, 0)

' Save As
longstatus = Part.SaveAs3("C:\Users\dao\Desktop\MACRO_PDF-DWG\" & nomfichier & ".pdf", 0, 0)
End Sub

 

And I think your zooms will depend on the starting position, so I think it's better that you frame yourself before recording.

1 Like

Bengous doesn't take it badly, I'm new to the thing^^
If I tried your code well, don't take it badly at all, it's just that I'm discovering

By the time I get to this line:

' Save As
longstatus = Part.SaveAs3("C:\Users\dao\Desktop\MACRO_PDF-DWG\ & "nomfichier" & ".DWG", 0, 0)

 

He said to me:
"Compilation error
Whereas: list separator or )"

It's okay it works, thank you very much bengous for your patience.

Sorry again but I have to understand everything.

yes I had forgotten the "I correct

No worries, I take it quite a bit

Do you know a site or other where I can learn some basics etc

Have a nice day, thank you again;)

Hello Maxbzh,

You will find HERE a discussion thread that dealt with the subject of learning programming on Solidworks, I advise you to read it to the end, you will find a whole bunch of interesting links.

Kind regards