Macro to adjust the partnames in a STEP export

Programming and macros
Rogier
Posts: 3
Joined: Tue Aug 08, 2023 5:44 am
Answers: 0
x 3
x 4

Macro to adjust the partnames in a STEP export

Unread post by Rogier »

Hi all,

wow what an amazing community and source of knowledge! New here and tagging along for the ride :)
Little introduction:
I'm a design engineer from the Netherlands and using Solidworks 20+ years.
I don't like when things don't work the way I would like them to, so that's why I ventured into the wonderfull world of macro's. After some successful bits of script that helped me in changing files and properties in batch operations, I feel more confident in seeking a solution in the form of macro's.

I have a long wish to change the name of the parts/bodies in a STEP export.

Why?:
Often we export our work in progress assembly to a STEP file, for our client to use for images/work instructions/ visualisation/ etc.
The problem is that you can Export the assembly with a revision in the name, say: assembly Rev1.STEP, but when our client opens/imports the STEP assy in Solidworks, it creates an assembly with parts again. Problem is that the parts in that assembly have all the partnames, without revisions or other differentiators. And we have seen this going wrong and being mixed up in the past, so we got cautious doing this.

What I would like:
A STEP export with al the body names with a (date) prefix.

How we do this now:
- Pack and go the assembly to a TEMP folder and add a prefix.
- open the new assy and save as a STEP file
- delete the TEMP folder

I can do the same process with a macro, but was trying to find out how I can do this without creating files and deleting them. So do this all in memory, like a pack and go. So basically a pack and go with a STEP file as deliverable.

Can you give me any inspiration on how to attack this? or if it is far fetched and better to automate our current process?

Thanks in advance!
by Stefan Sterk » Wed Aug 09, 2023 3:51 am
JSculley wrote: Tue Aug 08, 2023 12:25 pm Rather than muck around with temporarily renaming things, you can simply post process the STEP file. It's text after all. The components are identified by the name followed by an underscore and then the configuration name. If you use C# or VB.NET you have a Regex.Replace method you can use to prepend the date or replace an existing date with the latest.
A rather ingenious approach, particularly suitable when you solely intend to append a basic prefix or suffix. However, I would recommend opting for a temporary renaming strategy if you plan on incorporating a component revision number state.

Regarding @Rogier, here's some information on how you could approach this using regular expressions (Regex).

Search Pattern could be.

Code: Select all

(^#\d*?=?PRODUCT?\(?')(.*)(',?')(.*)(',?'',?\([^\)]*\)?\)?;)
with the replace/substitution pattern as.

Code: Select all

$1$2_DATE$3$4_DATE$5
where $2 and $4 are the names of the component/product.

For example, using the above patterns on the following lines.
#132 = PRODUCT ( '20232496-C-100', '20232496-C-100', '', ( #219 ) ) ;
#732 = PRODUCT ( '20232496-C-101_Default<As Machined>', '20232496-C-101_Default<As Machined>', '', ( #365 ) ) ;
#1347 = PRODUCT ( '20232496-C-03 A', '20232496-C-03 A', '', ( #1406 ) ) ;

Will result in.
#132 = PRODUCT ( '20232496-C-100_DATE', '20232496-C-100_DATE', '', ( #219 ) ) ;
#732 = PRODUCT ( '20232496-C-101_Default<As Machined>_DATE', '20232496-C-101_Default<As Machined>_DATE', '', ( #365 ) ) ;
#1347 = PRODUCT ( '20232496-C-03 A_DATE', '20232496-C-03 A_DATE', '', ( #1406 ) ) ;

You could use Notepad++, a text editor that comes equipped with built-in Regex replace functionality.
Go to full post
User avatar
Stefan Sterk
Posts: 37
Joined: Tue Aug 10, 2021 2:40 am
Answers: 3
x 51
x 77

Re: Macro to adjust the partnames in a STEP export

Unread post by Stefan Sterk »

simple answer, yes you can. check Name2.

1. open assembly
2. traverse through components
2.a rename component with Name2.
2.b list comp in already renamed list
3.SaveAs (Export)
4.close assembly without saving.

aslong as you dont save the document(s) the name changes wil only be in memory.

other links:
Renaming permanent and virtual components using SOLIDWORKS API (codestack.net)

Let me know if you would like some example code ;)
User avatar
JSculley
Posts: 638
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 868

Re: Macro to adjust the partnames in a STEP export

Unread post by JSculley »

Rather than muck around with temporarily renaming things, you can simply post process the STEP file. It's text after all. The components are identified by the name followed by an underscore and then the configuration name. If you use C# or VB.NET you have a Regex.Replace method you can use to prepend the date or replace an existing date with the latest.
User avatar
Stefan Sterk
Posts: 37
Joined: Tue Aug 10, 2021 2:40 am
Answers: 3
x 51
x 77

Re: Macro to adjust the partnames in a STEP export

Unread post by Stefan Sterk »

JSculley wrote: Tue Aug 08, 2023 12:25 pm Rather than muck around with temporarily renaming things, you can simply post process the STEP file. It's text after all. The components are identified by the name followed by an underscore and then the configuration name. If you use C# or VB.NET you have a Regex.Replace method you can use to prepend the date or replace an existing date with the latest.
A rather ingenious approach, particularly suitable when you solely intend to append a basic prefix or suffix. However, I would recommend opting for a temporary renaming strategy if you plan on incorporating a component revision number state.

Regarding @Rogier, here's some information on how you could approach this using regular expressions (Regex).

Search Pattern could be.

Code: Select all

(^#\d*?=?PRODUCT?\(?')(.*)(',?')(.*)(',?'',?\([^\)]*\)?\)?;)
with the replace/substitution pattern as.

Code: Select all

$1$2_DATE$3$4_DATE$5
where $2 and $4 are the names of the component/product.

For example, using the above patterns on the following lines.
#132 = PRODUCT ( '20232496-C-100', '20232496-C-100', '', ( #219 ) ) ;
#732 = PRODUCT ( '20232496-C-101_Default<As Machined>', '20232496-C-101_Default<As Machined>', '', ( #365 ) ) ;
#1347 = PRODUCT ( '20232496-C-03 A', '20232496-C-03 A', '', ( #1406 ) ) ;

Will result in.
#132 = PRODUCT ( '20232496-C-100_DATE', '20232496-C-100_DATE', '', ( #219 ) ) ;
#732 = PRODUCT ( '20232496-C-101_Default<As Machined>_DATE', '20232496-C-101_Default<As Machined>_DATE', '', ( #365 ) ) ;
#1347 = PRODUCT ( '20232496-C-03 A_DATE', '20232496-C-03 A_DATE', '', ( #1406 ) ) ;

You could use Notepad++, a text editor that comes equipped with built-in Regex replace functionality.
Rogier
Posts: 3
Joined: Tue Aug 08, 2023 5:44 am
Answers: 0
x 3
x 4

Re: Macro to adjust the partnames in a STEP export

Unread post by Rogier »

Hey thanks for the quick reply.
@Stefan Sterk ok did not think that renaming and not saving would work, but it is worth a try! (I am not really getting with you mean with your point 2b?)
@JSculley Good thing, for this particular case this can work really well.
I must admit I have never worked with Regex and search patterns you are showing, but I will read in to it and report later (probably try it end of the week or next week)

again, thanks a lot for the pointers!
Rogier
Posts: 3
Joined: Tue Aug 08, 2023 5:44 am
Answers: 0
x 3
x 4

Re: Macro to adjust the partnames in a STEP export

Unread post by Rogier »

Update:
I've got the edit STEP file part working. :)
So it opens the STEP file, searches for the lines with the partnames in them and adds a prefix in front of the file name. Then saves it.
Need some more time to complete it to a nice user friendly macro with user interface, so you can set the save location and prefix. And probably some extra error handling would be safe.

edit: sorry, can only mark one post as an answer. The idea that worked best for my problem was JSculleys, but Stefan's reply helpt me the most with figuring out how to use the Regex function. Thank you both with working this out!
Post Reply