Macro - Import block + save

Hello Community,

Thank you all for your valuable advice through various posts. This is my first message on the forum :-)

Here is my request: I have a hundred blocks (. SLDBLK) saved individually on my PC in the same folder.

I'd like to create/run a macro that 

Create a new part (or insert as a new sketch into an existing part)

insert the block on the origin (put a block scale x4) /on  the front plane for example

close and save the room (with the same name as the block even better)

for my entire "blocks" folder automatically 

 

Solidworks version 2020

TB SW knowledge, beginner macro

Thank you all for your contribution

Kind regards

Franck / CAD draughtsman for 8 years

To be tested on a block to see if it works but here is the link to the subject:

https://r1132100503382-eu1-3dswym.3dexperience.3ds.com/#community:yUw32GbYTEqKdgY7-jbZPg/iquestion:G8lFvoZCTzWDcYa61U3jaA

And the code to change the scale put scl=4

Take RiriFifi's answer and test the code (usually its code is efficient.

If it works well, you will have to adapt this code to all the files in a directory, by retrieving the name of the block for the recording.

Hello

For noobs like us in macro creation, there is a feature to capture functions in SW. You start the recording, you do your manipulation and SW automatically creates the code of what you did on the screen.

With that you have 90% of your macro.

All you will need is the loop(s) to change the .sldblk file as well as the declaration of the variables (name of the record file, probably some more temporary variables..).

 

Good luck

Thank you for your feedback!

I looked at the link on 3dexperience, I updated the data (path of my files and others) but nothing happens when launching the macro. I ran the said macro with an open file, from a sketch, without anything open nothing happens... Shame

I did record a macro that does the job for about 1 file.

On the other hand, I don't know how to integrate a loop and a recording under the same name as the block. Is there a topic about these elements, it could be an alternative to my search.

Thank you for your time

 

For the macro you copied the one from RIRIFIFI? 

Did you create the 2nd class 1 module?

Attached the macro from a selected room, a shot then launch the macro and it works (except that the block is not fixed)

It needs to be improved but we are getting closer.

After seeing your request if you only have 100 blocks to modify, not sure that developing a macro that doesn't exist is faster for 1000 blocks or more, on the other hand, I don't say. 100 block = 1 to 2 hours is much less than the dev of a macro even for someone in the know.


block.swp

Hello sbadenis,

Thank you for your feedback. Yes, I think I used the ririfififi macro with class1 and module1 separate and data (file path...) in accordance with my files.

Thank you for your macro but it doesn't really meet my needs. For my part, with my macro, I manage to make a new room, insert the block to scale, save and close. For 1 defined model.

I lack the "loop" aspect that would automate the opening of the next block and reproduce the actions.

I looked but unfortunately I get stuck... I'm going to switch to "manual" mode so

Hello Franck,

Answer may be a little late, but the attached macro should answer your problem.
Generation of the parts with sketch in the face plane, insertion at the origin, at scale 4.
3 lines are to be modified to adapt it to your system:
- line 35: source directory of block documents (with extension "SLDBLK");
- line 36: destination directory of the parts files, with the same name as the blocks, with the extension "SLDPRT";
- Line 40: The path and name of the default part model for your version of SolidWorks.

To test on your PC...

Kind regards.

 


insert.swp
2 Likes

Super! Thank you m.blt

I nevertheless have a bug during execution, I'll try to understand ...

line 42

Set swSkMgr = swModel.SketchManager

I emphasize that I have set up my paths etc...

If anyone has an idea?

Thank you

 

Hello

What is the type of error? This?

If this is the case, you need to check that the Part and swModel variables are not empty: in debug mode, you just have to leave the cursor above one of these two variables to display its content.
If they are equal to Nothing, it means that the previous statement Part = swApp.NewDocument... (line 40) does not return anything, and the SolidWorks document is not open. Hence the error when creating the sketch manager.
The string that defines the template file is probably wrong, you have to check, very carefully, that it exists, with the path indicated (on my source file it is in the SW 2018 folder).

1 Like

WONDERFUL, everything works wonderfully now.

My template is called "part" and not "part" in my folder

A big THANK YOU to all and especially to  m.blt