Yes it's possible, you can get the give custom properties and even the one specific to the configuration.
Want to retrieve drawing information? Want to fill in custom properties based on the file name? Or make a 'save' button that saves the file according to the 'plan number' and 'description' property?
In principle, to retrieve the file name and put it in a custom property, you can do something like this:
Dim swApp As Object
Dim swModel As ModelDoc2
Dim config As SldWorks.Configuration
Dim swCustProp As CustomPropertyManager
Dim lRetVal As Long
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set config = swModel.GetActiveConfiguration
Set cusPropMgr = config.CustomPropertyManager
lRetVal = cusPropMgr.Add3("FileName", swCustomInfoType_e.swCustomInfoText, swModel.GetTitle, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
End Sub
All that remains is to rework the text you retrieve from the swModel.GetTitle to get the 2 values you are interested in and store these 2 values in 2 separate properties.
Hello A macro such as the one proposed by @d.roger can do the trick. You can also take a look at the presentation of all the tools in the myCADtools suite. These are about fifty utilities that automate repetitive tasks. The SmartProperties utility is the tool for you! I invite you to look at this document. https://www.lynkoa.com/contenu/pr%C3%A9sentation-des-utilitaires-mycadtools-2018 All tools are SW2016-2017 and 2018 compatible. Kind regards
@mandragore, I'm just trying to retrieve the info contained in the file name.
@d.Roger, indeed it would seem (as little as I understand VB) that this partly answers my problem. Having never used macros on SW, I'll try to get by with all this, I'll keep you informed if I can find a solution.
@d. Roger's proposal for a solution seems interesting to me . But I've never used macros and I don't know where to start. And obviously I don't have the basics to understand their programming. If you don't mind too much, could you explain to me in detail how to use this macro.
Thank you @sbadenis for this response although it is sarcastic.
I had already read this page and integrated the raw code given by @d.roger into VBA.
I obviously tried to run the macro as it is from solidworks, which didn't work, as you can imagine.
Not understanding the ins and outs of this script, I don't know what to modify to make it work in my case.
However, I made a second attempt by integrating the name of my piece instead of the "FileName" which is written in red in the original text. Which doesn't seem to have worked any better.
I want to retrieve two custom properties from the name of my part. The first one has the first 14 characters of the name of my piece and the second the following characters that make up the name of my part.
If anyone here agrees to explain to me how to achieve this, I thank them in advance.
To the remark "I obviously tried to run the macro as it is from solidworks, which didn't work, as you can imagine.", are you sure it didn't work? This bit of macro just allows you to create a custom property (in the active configuration) named "FileName" whose value is the name of the part, so did you go to the custom properties to see if this new property has been created?
So to create 2 custom properties from the file name, you have to get the file name by the "swModel.GetTitle" function, then rework this value to cut it in 2 according to the desired division and finally store each end of this value in 2 different properties:
Dim swApp As Object
Dim swModel As ModelDoc2
Dim config As SldWorks.Configuration
Dim swCustProp As CustomPropertyManager
Dim lRetVal As Long
Dim maValeur0 As String
Dim maValeur1 As String
Dim maValeur2 As String
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
maValeur0 = swModel.GetTitle 'On récupère le nom du fichier
maValeur1 = Left(maValeur0, 8) 'On récupère les 8 premiers caractères du nom du fichier
maValeur2 = Right(maValeur0, 12) 'On récupère les 12 derniers caractères du nom du fichier
Set config = swModel.GetActiveConfiguration
Set cusPropMgr = config.CustomPropertyManager
lRetVal = cusPropMgr.Add3("MaPropriete-1", swCustomInfoType_e.swCustomInfoText, maValeur1, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
lRetVal = cusPropMgr.Add3("MaPropriete-2", swCustomInfoType_e.swCustomInfoText, maValeur2, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
End Sub
Thank you very much @d.roger for your precious help.
After posting my second message, I noticed that these values had been created in the configuration-specific properties.
Thanks to your explanation I understand your scripts better.
And the second version has made me progress enormously, I almost get the desired result.
All I have to do now is find a way to recover all the characters after the 14th. Since the number of characters corresponding to the part number is constant, unlike the number of characters in the part name.
Thank you for this clarification. I figured out how to filter the file extension, avoid the first few characters so that I only have a part of the reference in plan number. Perfect. Thank you very much.
So I share ^^
Dim swApp As Object
Dim swModel As ModelDoc2
Dim config As SldWorks.Configuration
Dim swCustProp As CustomPropertyManager
Dim lRetVal As Long
Dim maValeur0 As String
Dim maValeur1 As String
Dim maValeur2 As String
Dim maValeur3 As String
Dim maValeur4 As String
Dim maValeur5 As String
Dim maValeur6 As String
Dim maValeur7 As String
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
maValeur0 = swModel.GetTitle 'On récupère le nom du fichier
maValeur1 = Left(maValeur0, 6) 'On récupère les 6 premiers caractères du nom du fichier'
maValeur2 = Left(maValeur0, 12) 'On récupère les 12 premiers caractères du nom du fichier'
maValeur3 = Right(maValeur2, Len(maValeur2) - Len(maValeur1)) '2 moins 1 pour avoir le n° du plan'
maValeur4 = Right(maValeur0, 7) 'On récupère l'extension'
maValeur5 = Left(maValeur0, 13) 'On récupère les 13 premiers caractères du nom du fichier'
maValeur6 = Right(maValeur0, Len(maValeur0) - Len(maValeur5)) 'On récupére tout au delà du 13éme caractére'
maValeur7 = Left(maValeur6, Len(maValeur6) - Len(maValeur4)) 'on enlève l'extention du fichier à 5'
Set config = swModel.GetActiveConfiguration
Set cusPropMgr = config.CustomPropertyManager
lRetVal = cusPropMgr.Add3("N° plan", swCustomInfoType_e.swCustomInfoText, maValeur3, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
lRetVal = cusPropMgr.Add3("Nom de la pièce", swCustomInfoType_e.swCustomInfoText, maValeur7, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
End Sub