Solidworks Macro Save As Pdf And Dxf

0 views
Skip to first unread message

Melissa Russian

unread,
Aug 3, 2024, 6:06:22 PM8/3/24
to resrekento

This VBA macro allows to save all documents currently opened and modified in SOLIDWORKS silently using SOLIDWORKS API. Unlike default save as command where the various warning messages can be displayed while saving the files this macro will save documents without showing any popup messages.

Hoping to get some assistance. As per the code exerpt below, I cobbled together a Macro that when initiated will force the user to save their drawing and will then proceed to export a PDF and DWG, or DXF. (Comes up with a selection menu).

However rather than having the user click on a Macro button in Solidworks, I wanted to automate this further so that every time as user clicks the default save button in Solidworks, this Macro is initiated.

Unfortunately on the other forum I am unable to get logged in right now, so I wanted to pop my head in over here So far as that solution, I mentioned in my post that I have already tried that but it is clear that either I am misunderstanding something regarding the instructions or that in someway the Macro codes does not play well with that method.

I am unsure which on it is. The Macro on its own works just fine. However when I implement it using that resource and I then do a test run, nothing happens. No errors, just seems as if it does not run at all

If you read the comments carefully on CodeStack website, it says that the macro still needs to be run once when you start SolidWorks each time. Once you run the macro once and then click on save button, the macro will work fine.

In your case, ideal solution would be to convert this macro into an addin if you would like to fully automate it. But if you are a novice, I would not recommend doing it yourself. Either contact @AmenJlili or my self personally and I am sure one of us would be able to help you out. You will probably have to pay to the service though.

I would also not recommend exporting DXF, PDF and/ or DWG every time user saves the drawing as it will be annoying and time consuming. If you really like to make sure that you always have the latest copy, you can change it to export files just before the drawing is closed. I would also recommend removing the selection every time the export happens and instead do this selection by other means e.g. settings or configuration file or something else.

This can be done with VBA through one of the FileSave event handlers. However, this introduces an additional level of complexity because you need to manage those event handlers through the lifecycle of the drawings (open and close events).

At the moment I have it configured so that my export macro is run upon a Save trigger. The only issue is that my macro is run before Solidworks has actually saved the drawing. My Macro can only export upon a saved drawing.

Or do I alter the Macro code so that the pre conditions are that the drawing does not already have to be saved in order for the exports to happen ? That being said, I am unsure which part of the code I would have to alter to change that behaviour.

To re-state your problem, you need an add-in that triggers your macro logic after/post the save event. The event handler that you should targeting is DAssemblyDocEvents_FileSavePostNotifyEventHandler Delegate (SolidWorks.Interop.sldworks) - 2017 - SOLIDWORKS API Help.

You may notice this non-saving behavior when you are working on a macro in Visual Basic for Applications, or VBA for short. You will click on save and you assume the macro has been stored for all eternity. Then you close the editor and you get the following pop-up:

When multiple users make use of macro files on a network drive, the early bird gets the worm. That person will obtain saving rights, and keeps them until he closes SolidWorks. Find that early bird and get them to open the macro file in the VBA editor and close the file using File > Close and Return to SolidWorks. Closing the window in the upper right corner will not work! This will only close the editor, not the file.

Being the last one on the floor has a few more advantages: the quiet office will aid you in your quest to build the ultimate time-saver, and nobody can hear you blame the computer for the bugs in your program!

I've been using Solidworks as my main design software, but it always bugged me to not have an export button that directly opens in PrusaSlicer (similar to Fusion360), so I made one. It exports the 3MF file in the same directory as where the part is saved, so you can always find it later.

It is a custom VBA macro that is quite easy to install. In Solidworks, go to Tools > Macro > New. Save the file in a directory that will not be removed, and give it a reasonable name (like ExportToPrusaSlicer). Press Save and the VBA edit screen opens. Paste this:

To add it to the toolbar, we need to create and add the shortcut. In the topbar, press the arrow next to the settings icon -> customize. New screen opens, click tab Commands, search for macro. Drag the most right button (New Macro Button) to the bar you want it. I added it to features. A screen opens, select the macro we just created. For the icon, use this:. Should be a 16x16 4bit BMP icon. If this is unclear, you can always use this guide: _assigning_macro_toolbar_button.htm?rid=137228

Second, do you think it would also be possible to open it up in PrusaSlicer window that i have open?
I usually just add things in the open window and work on multiple parts at the same time to make it time effective.

SOLIDWORKS API is a gateway to automating your design process and data management. Using .NET compatible languages (VB, C#, C++) one can write small macros or build full-blown add-ins for modelling, routine edits and feeding data from one software package to another.

Okay, this is where things get tricky. You will need some SOLIDWORKS API and VBA programming knowledge from here onward. I created some textured appearances and saved them as .p2m files in a sub folder next to my macro file. Then we add some bits of intelligence to the already existing macro:

Hello Im working on a macro to Save my project into a designated directory on my PC based on the Model Name in PMill, The following macro gets me 99% of the way there I just have an extra "}" Character at the end of the save path that I cannot figure out why it is even there in the first place. Maybe its just something simple I have overlooked.

Thanks It works, just sort of curious to why it works. I'm pretty new to writing macros like this, and if no one already can tell I do work with zakary aka EDM on this forum, both of us are trying to learn to write / understand powemill macros

One thing that helped me immensely was being able to see what was going on in the background. If you right mouse click on the macro in the explorer, you can select the debug option. This pops open anther window, and allows you to step through the macro line by line. It also gives you another Window that will show you the value of all variables and strings that are in use.

i learned the same way that @Scoobyvroom mentioned, as well as just posting stuff here and getting help from some of the expert users and the old Delcam staffers who used to frequent the old forum often

This Macro works great but I want to create a few If Else statements in here. I am trying to get the macro to decide which folder to place the file in. For example I have folders for each specific job. 6XXXX, 6AXXX, 6BXXX and so on. Right now if my model name is 6H809 this macro below will create only a 6XXXX folder and even if the part number is 6HXXX. It creates a directory like:

In the past I have just simply replaced STRING X4 = "XXXX" with STRING X4 = "AXXX" or STRING X4 = "BXXX" but I would rather not have a bunch of the same macro buttons on my ribbon when I could have one macro that does the deciding for me.

I want to be able to have PMILL Create a folder or if it exists already just get into the correct folder to create the correct save path. I will post pictures below to hopefully better describe the scenario. I am not sure what terms I need to use to make this macro look at the line 16 (below) and have the macro determine whether this is an A, B, C, D or just another number.

Sometimes the further in depth you think about things the harder it may seem when in reality the solution is very simple. This has happened to me a few times now. although I am still pretty new to the macro language. Hopefully this will help someone else with the same file save directory in the future! I will post the entire code below to show the full macro. Refer to my post above to note changes. And thanks again everyone else for the help.

c80f0f1006
Reply all
Reply to author
Forward
0 new messages