Automatic macro export when a clip is opened

Hello

I want to create a macro to automatically export a plan to PDF and JPEG as soon as it is opened, then close it again.

I know this feature may seem strange, but I have my reasons, and the explanation will be long...

I found this Macor as an example, which seems to contain pieces of what I'm looking for:

https://www.codestack.net/solidworks-api/document/macro-feature/model-load-watcher/

In this example, a "log" text file is filled in each time the file in which the macro is iterated is opened.

As always I don't understand much but I try to decipher to progress.

The macro is composed of two modules: Macro and HandlerModule. I thought that one allowed you to lace the macro at startup and the other contained the task to be performed, but when reading the code I'm not really sure anymore. I do have a macro that allows you to export in PDF or / and JPEG, but I'm not sure if pasting it into the HandlerModule is the right procedure.

Another point that bothers me, and that may have also stung your eyes, is the automatic closing of the document after export. If I want to work on my document in the normal way, there will be a problem: I open, and bam, it closes. If I don't embed my macro in the document, I should be able to trick me by temporarily moving the macro to another directory. But is there another way?

Finally, and even if it is very secondary, is it possible to export folio "1" in PDF and folio "2" in JPEG?

Thank you for your help

Hello

First of all, I won't use feature macros but rather Solidworks event management (detection of the opening of a file and then processing by the macro).

This would require modifying the SW launch executable to add a macro to load into memory when SW is opened.

For the slightly incompatible side of wanting to work and export automatically, apart from opening a window (msgbox) allowing you to select what you want to do, I don't see too much of a solution.

Otherwise, use the SW task scheduler to do these batch exports.

For the last question, yes it's possible, you just have to be sure on the one hand that there are only 2 folios otherwise you have to do additional processing to know which file format to export to.

1 Like

The feature macro embeds the macro (like a function) in your assembly so that the code restarts each time the file is opened. So every time you open the file the macro will launch and export the files to you in pdf and jpg.

Not sure if this is what you are looking for.

If you just want to do batch processing from a list of files, this is clearly not the macro for you.

In this case this type of program or macro is more suitable for your request:

http://swtools.cad.de/us_prog_pac.htm

https://cadplus.xarial.com/batch/

https://www.youtube.com/watch?v=jpaOxtsZSKA

https://www.cadsharp.com/blog/run-solidworks-macros-using-task-scheduler/

There was also a program that worked well but I can't find it anymore that allowed you to launch your own macro.

For my part, I use Integration (a MycadTools tool from Visiativ - https://help.visiativ.com/mycadtools/2020/fr/Integration.html)

 

@ Cyril.f:

modifying the SW  launch executable seems to me a bit too intrusive for my stupid level

using SW's task scheduler does not suit my needs: the export must be done without touching Solidworks. In this case, an average person (or even a computer program) clicks on a button (a .bat), and retrieves the PDF from a given directory.

@ Sbadenis:

The feature macro embeds the macro (like a function) in your assembly so that the code restarts each time the file is opened. So every time you open the file the macro will launch and export the files to you in pdf and jpg. Not sure if this is what you are looking for.

Yes, exactly!

Hello

The modification of the executable is not very intrusive, it's just additional command parameters on the shortcuts, such as: 

"C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\SLDWORKS.EXE" /m "C:\My Macros\Macro1.swb"

Thus, the macro starts as soon as it opens.

For the feature macro, yes it meets your needs but not the need to be able to work on the files since it will systematically export and then close the file. Basically, your file becomes totally unusable in my opinion, unless you add a validation window each time to know what to do (close the file or leave it open).

Regarding the task scheduler, I don't really understand. In any case, SW must open to open the drawing and export it. There's nothing done in the background without ever opening the main software.

1 Like

Completely agree with @Cyril.f  unless you never use the part or assembly again, it's impossible to make a macro feature.

What is the purpose of getting the pdf and jpg of a defined part set or assembly from time to time?

If so, from what from a file list or a 3d assembly containing the part or assembly file to be extracted?

Without answering your entire process, these questions seem essential in order to provide a correct answer.

 

Thank you Cyril.f . I just understood that the "launch executable" is just the .bat that orders the opening of my file...

I wrote this:

start "C:\Program Files\SLDWORKS.exe" "C:\Plan1.SLDDRW" /m "C:\Export PDF.swp"

The Plan1.SLDDRW file opens well but the macro does not launch. I think this is because there is no default app set for .swp files

I tried to apply swvbaserver.exe and drawcompare.exe as the default app but without success.

Hello

I don't think it's coming from an application not defined for swp files but rather linked to the macro that has no "automatic" execution. A bit like Word/Excel macros, it takes an execution at the time of the opening event to trigger actions afterwards.

Hello

 

 @Legnano

Another point that bothers me, and that may have also stung your eyes, is the automatic closing of the document after export. If I want to work on my document in the normal way, there will be a problem: I open, and bam, it closes. If I don't embed my macro in the document, I should be able to trick me by temporarily moving the macro to another directory. But is there another way?

 

As you may have guessed, it's problematic to do this kind of macro. The best, in my opinion, is to make a macro button that will allow you to save your document in PDF/JPEG in the folders of your choice and close the drawing. 

You can also make a macro that opens the drawing of the document you have selected in your tree, saved and closed the drawing.

 

You also have to be careful because when opening some drawings the documents are lightened and therefore the 'room bubble' is invisible (this is one of my drawing problems and I am looking for macro action to make the view solved)

 

Personally, I don't use any launch executable , I find it a bit too intrusive even if on some of my macros the application could be useful but they are only useful on old files and I think that I can't incorporate this kind of macro to existing files .

Back to the starting point/ according to all your remarks, what suits me best is a macro A integrated into the plan which asks when the plan is opened to execute a macro B present in a directory. This macro B should ask:

1: loading the plan and components in solved

2: Export the plan

3: Closing the Plan Without Saving

If I want to work normally on the plan, I delete macro B from its directory, which blocks the previous actions. I handle the error message "macro B not found" manually because it is an exceptional special case.

For those who find my request strange, I want to explain why, but I don't want it to turn this conversation into a battlefield to tell me if they think it's a good or bad idea. I have an excel file that modifies the custom properties of a room without having to open it. The geometry of the part is updated with the values of the excel. A simple click of a button should allow the publication of a plan of the room in pdf. All this so that the user does not have to use solidworks.

Up...

Why not do all the code from excel in this case?

You can indeed control SW from Excel (even if I have never tried it personally)

See this topic and more specifically CONTROLLING Solidworks In an Excel Macro:

https://www.cadsharp.com/blog/using-microsoft-excel-with-the-solidworks-api/

EDIT: another forum topic dealing with how to do it from Excel: https://www.lynkoa.com/forum/solidworks/macro-solidworks-excel

Thank you but it's not going to be suitable for my application.
I will continue to work on the integrated macro solution that calls for the opening of other macros. For the moment it's skating

Yet you say:

For those who find my request strange, I want to explain why, but I don't want it to turn this conversation into a battlefield to tell me if they think it's a good or bad idea. I have an excel file that modifies the custom properties of a room without having to open it. The geometry of the part is updated with the values of the excel. A simple click of a button should allow the publication of a plan of the room in pdf. All this so that the user does not have to use solidworks.

It would therefore be enough on your excel button to program the opening of your part and your plan in solidworks, to export your folio plan 1 in pdf and your folio plan 2 in jpeg. Then close the part and the assembly. All this while coding in excel.

Here is a piece of code that from excel opens a room and an MEP, I just tested out of curiosity and it works perfectly.

Sub EssaiSW()

 Dim swApp As SldWorks.SldWorks
    Dim swModelUn As SldWorks.ModelDoc2
    Dim swModelDeux As SldWorks.ModelDoc2

    If swApp Is Nothing Then
        Set swApp = CreateObject("SLDWORKS.application")
        swApp.Visible = True
    Else
        Set swApp = Application.SldWorks
    End If

    Set swDocSpecification = swApp.GetOpenDocSpec("C:\Users\sebastien.denis\Desktop\Pièce5.sldprt")
    Set swModelUn = swApp.OpenDoc7(swDocSpecification)
    MsgBox swModelUn.GetTitle
    
    Set swDocSpecification = swApp.GetOpenDocSpec("C:\Users\sebastien.denis\Desktop\Pièce5.slddrw")
    Set swModelUn = swApp.OpenDoc7(swDocSpecification)
    MsgBox swModelUn.GetTitle
    
    swApp.CloseDoc swModelUn.GetTitle
    
    Set swModelUn = swApp.ActiveDoc
    MsgBox swModelUn.GetTitle
    
    Set swDocSpecification = swApp.GetOpenDocSpec("C:\Users\sebastien.denis\Desktop\Pièce6.sldprt")
    Set swModelDeux = swApp.OpenDoc7(swDocSpecification)
    
    swApp.CloseDoc swModelUn.GetTitle

    MsgBox swModelDeux.GetTitle
    
    swApp.CloseDoc swModelDeux.GetTitle
    
    
End Sub

Thank you @d.roger for this code available elsewhere in my link above.

Then there is no more to export the pdf and the jpeg. Which is largely achievable and should be fairly straightforward.

Unless of course I still miss a point?

For your information, the built-in macros for having tested several of them are complicated to implement and remain dangerous, not to mention the crashes with them.

 

1 Like

I am of the same opinion that @sbadenis macro incorporated into the plan/room I have never practiced but it seems to me that it is quite complex and I find it too 'intrusive'. 

Since you start from an excel file where you fill in the dimensions to be modified I think that the simplest remains an excel macro button that sends worked on Solidworks.

 

The button can allow you to open the room, fill in the dimensions, open the saved plan and then close, or continue to work by modifying the dimensions and then save again if you have a side table which can save you a few seconds each time you open/close.

 

If you want to avoid your users using Solidworks I think it's the best solution. You may quickly be annoyed if you have to change a plan and you forgot to move the macro, or if Solidworks opens a window for you every time you modify/create a plan saying that it didn't find the macro without mentioning the possible errors/bugs of Solidworks.

 

Otherwise, if I understand your request correctly, you would like the user to click on the DRWG file of the part in Windows Explorer. Solidworks opens this plan , correlates with the open excel file , updates the data and then automatically saves/closes the plan without the user seeing it.

1 Like

I'd still like to try the built-in macro, if it doesn't bother you too much. 

Can you explain to me what is the method to merge this macro that launches an action when opening a file, with this macro that executes a series of macros?

Here is a macro used on your plan and changed the path of the macro in the 'HandlerModule'

It will create an object for you in the design tree.

 

Normally it should work, launch when opening the file (plan/part or asm)


macrotest2.swp
2 Likes

Great, THANK YOU Mandragore!

Plus, it's super simple to deploy on a series of documents.

I struggled a bit with the references to activate, but it's progressing well.

I still have a small bug: the "exortPDF" macro called pat the integrated macro crashes on the file path recovery, while if I run it manually it works.

It seems that it's because the "exportPDF" macro starts before the solid file is fully loaded (the error window offers me the debugging, but the solidworks file doesn't appear) Is it possible to put some kind of delay to see if that's it?

Thanks again

I had the same problem, impossible to launch an external macro with the method it offers and I didn't have time to dig into the subject.

 

I just managed to launch one try with this:

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim boolstatus As Boolean
Sub main()
   Set swApp = Application.SldWorks
   Dim runMacroError As Long
   boolstatus = swApp.RunMacro2("path", "module", "name", 1, 0)
End Sub

 

I also changed 'swRunMacroUnloadAfterRun' to 1 and 'runMacroError' to 0

[ Unfortunately, it doesn't work anymore... The changes you make only affect the handlermodule, but it's in the other module that it gets stuck...] Error resolved

I replaced the content of the handlermodul with what you offered me, it still doesn't work. I still get the error message at the macro called by the andlermodule while the plan is not yet displayed in SW. If the macro tries to export a plan that is not yet open, it's not surprising. But I thought that the handlermodule was only triggered once the plan was completely loaded...

By the way, I don't understand what you mean by "I also replaced 'swRunMacroUnloadAfterRun' with 1 and 'runMacroError' with 0"