Macro: Body registration according to properties of the welded parts list (+drilling count + unfolded)

Hello everyone,

This post to share some macros made with the help of this forum and to ask for your advice.

For context: we are currently looking to automate some of our export procedures with Solidworks (2017) to simplify subcontracting.
We work on projects that are frequently edited and subcontracted regularly by people who are not necessarily the cartoonists, hence the need to have reliable files.

We are "lucky", we can outsource using volume files (sldpart and step), which greatly simplifies the work.

Our solidworks files are multibody models (mostly simple mechanically welded assemblies).
Each multibody part is designated by a letter in each project. (A.sldpart, B.sldpart, etc.)
Each body of each part is designated by a name of type A1, A2, etc. in the welded parts list properties.
We also use the list of welded parts to store other information for each body (number and diameter of holes, subcontracted or not, designation for bills of materials, etc.).

For subcontracting we need to extract volume files corresponding to the fields A1, A2, ..., named such as: A1.sldpart. (note: the management of order quantities is done via our ERP and the bill of materials).

Unable to find an easy way to invoke the "save bodies" function in solidworks by automatically naming based on properties of the welded parts list, we decided to develop a macro for this purpose. (allowing you to filter the exports according to other parameters).

In the attachment of this post you will find 2 macros that allow:

  • The first one exports all bodies based on properties of the welded parts list:
  1. In a part file, scan all top-level elements in the list of welded parts (+/= all different bodies)
  2. For each element of the welded part list: create an entry in 2 different tables: in the first one write the custom properties of the body (in our case ID, laser, P, ...) AND write in a second table the corresponding body.
  3. At the end of this loop, the bodies can be filtered, and are then saved in C:Temp as A1.sldprt A2.sldpart etc.
  • The second, write the number and diameter of each hole in a property of each body:
  1. In a part file, scan all top-level elements in the list of welded parts (+/= all different bodies)
  2. For each item in the welded part list, run a drill search macro on the body concerned (from: https://forum.solidworks.com/thread/201408)
  3. Write the result in the "p" property.

After spending a lot of time snooping around on different forums, I haven't found such macros, so I hope they can be useful to some people.

Several remarks:

  • - First "experience" in macro solidworks for me so it's coded with the feet. These macros usually come from an assembly of pieces of code gleaned from the net, so it's neither optimized nor reliable I think, but it works.
  • - It works for me and on 3 other workstations in 2017 with the following precautions: launch from the macro editor by placing yourself in the main (i.e. click on main), have an existing C:/temp directory.

If some of you have a little time to give me, I'm interested in helping on 3 points:

  1. I'm interested in the automatic generation of dxf (for flat sheets and for unfolded sheets), in the same case as before (naming etc.). I did find the swPart.ExportToDWG2 method, but I can't apply it to a body (ibody2), but only to a complete part. I see only one option (not very aesthetic: delete all bodies except one, export the "part that contains only one body", move on to the next body, etc). Take advice if you have any.
  2. From a broader point of view: I have the impression that my logic is not so bad for the first 2 macros: do you see another (simpler) method to achieve this goal?
  3. If some people feel like cleaning up the code so that it can (better) be used by others, don't hesitate!

Thank you for your feedback,
Q


qc-save_bodies_according_to_id-v0.1.swp
1 Like

And the second macro, I couldn't attach it to the first post.

Have a nice day
Q


qc-append_holes_count_to_p_customproperty-v0.1.swp
1 Like

Hello

Personally for the export of DXF with several flat sheets or with folds in the same part I use the Save as DXF (or DWG) function, SW locates all the sheets or unfolded and you have the option of exporting "separate files" which will automatically create a file cut by piece. If you don't want all the pieces, just uncheck the unwanted body to export.

It works well on the other hand I don't have the skills to modify the numbering imposed by SW.

Have a nice day

Vincent

Hello

Thank you for the quick response.

Indeed I spotted this technique. The problem is that solidworks gets (if I'm not mistaken) the name of the last Feature that affects the body (in the case of a sheet metal body it's often "unfolded state xx") where I would like to make it read A1 or A2.

So it gives me food for thought for my other question about the method: maybe a simpler way would be to rename the features according to the list of soldered parts before launching the "save" and "export" functions automatically.

Good afternoon,
Q

Hello again,

I just tried yes that's what you have to change one by one the name of the unfolded parts in the list of welded parts.

Then save as DXF/separate files.

Have a good end of the day

Vincent

Hello 

Solidworks retrieves the name of  the last feature in the body build tree that you want to save.

I think you need to rename the unfolded function in your case

 

CDLT

Hello

I may have misunderstood the request but why not list the unfolded states, select them one by one to save them in dxf after retrieving the name of the body to which it is attached?

Kind regards

Hello

For each macro, 2 solutions are presented:

1°) Solution without renaming the unfolded states:
    - List the unfolded states.
    - For each unfolded state:
        - List the bodies in order to recover the name of the one attached to it.
        - Set the name of the DXF file according to the name of the body found.
        - Export this unfolded state to DXF under the name thus defined.
        - Loop to the next unfolded state.
        - And so on until the last unfolded state.
    
2°) Solution by renaming the unfolded states:
    - List the unfolded states.
    - For each unfolded state:
        - List the bodies in order to recover the name of the one attached to it.
        - Rename the unfolded state after the name of the body found.
        - Loop to the next unfolded state.
        - And so on until the last unfolded state...
    - Export the complete model in DXF and separate files.

The difference will be mostly visible on the name of the output file, for solution 1 it can be "Body Name.dxf" while for Solution 2 it will be "Body Name - Part Name.dxf".

Kind regards

Hello

Thank you for your feedback.

I dare: do you have any pieces of code that go in the direction of d.roger's proposal?

My heart swings between the 2... On the one hand, the name doesn't require a post pro, on the other hand, the user input allows you to see what's being exported and to choose different projection files with the usual interface.

Have a good end of the day,
Q

 

Hi all

Old subject...

But proposal n°1 by @d.roger corresponds perfectly to what I am looking for.

The problem is that I don't understand macros, in fact I understand the structure of the "sentences" but the vocabulary!

If someone in their spare time could draft a piece of code to allow me to move forward, it would be super nice!

Thank you in advance.