Macro for Custom Property

I have no knowledge of how a Macro works but I am reading the discussions and what I am thinking of doing seems possible.

We use the same file name format for all our files. Parts are "XXXX XXX XX D###", while assemblies are "XXXX XXX XX A###" We have a custom property of "01-DetailNumber" that we currently manually type in every time. This number corresponds to the file name. It will be D### or A### depending on if assembly or part and it matches whatever that filename is. I hope this makes sense. I will give an example to illustrate what I am needing or asking about. I have a file name that is

241B CHL 01 D001

I want the 01-DetailNumber to be "D001"

241B CHL 01 A001

I want the 01-DetailNumber to be "A001"

Can someone tell me if this is possible, how to figure out how to do this, or be able to write the macro for it? Any help would be appreciated.

SolidworksApi macros
Answers 8
JB
JB 2017-09-25 Best Answer

Enjoy!

<p>Sub main()</p><p>Application.SldWorks.ActiveDoc.Extension.CustomPropertyManager("").Add3 "01-DetailNumber", 30, Left(Right(Application.SldWorks.ActiveDoc.GetPathName, 11), 4), 2</p><p>End Sub</p>
AJ
AJ 2017-09-25

This doesn't consider the part-assembly condition the op is asking, does it?

CC
CC 2017-09-25

Why don't you use SW buiild-in "Property Tab Builder"

JB
JB 2017-09-26

Part/assembly is a red herring... extraneous information.  It's all just the last 4 characters of the filename.  The person naming the file has already (supposedly) taken care of deciding if it's an assembly or not.

DG
DG 2017-09-26

Hi Kevin, might be worth checking out CustomTools​ where you can automate part numbering quite easily.
check out the video here: Automatic File Naming​

KO
KO 2017-10-04

This is exactly what I was thinking, now this worked for the current document I had open, but how do I get it to work with all files in the folder or more specifically only ones with  either an A### or D### ( I do know worse case I could copy and paste all the files specific to what I want modified to another folder and then copy and paste them back after I finish running the macro.). There are purchased items in the same folder that I do not want them changed to the last four digits. And Maybe I am doing something wrong. I opened a file and ran the macro inside Solidworks and it worked in the open file.

DG
DG 2017-10-04

You can use #TASK tool to batch run this macro. Just drag the files on the #TASK interface, add you macro and run.

JB
JB 2017-10-04

Nope, you're doing it right.  You never mentioned batch running until now, so it just works on the open file.