-
Re: Get the model name as a custom property
John Stoltzfus Dec 4, 2018 8:27 AM (in response to Mahmoud Ebesh)Did you try SW-File Name??
-
Re: Get the model name as a custom property
Mahmoud Ebesh Dec 4, 2018 8:33 AM (in response to John Stoltzfus)John, thanks for your input but as I mentioned above, I don't want to place an expression, what I need is the value of the expression, you can see the attached picture for clarification.
-
-
Re: Get the model name as a custom property
Fifi Riri Dec 4, 2018 3:01 PM (in response to Mahmoud Ebesh)Hello, try this:
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swCustProp As CustomPropertyManager
Dim PartName As String
Dim intstatus As Integer
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
PartName = swModel.GetPathName
PartName = Right(PartName, Len(PartName) - InStrRev(PartName, "\"))
PartName = Left(PartName, (InStrRev(PartName, ".") - 1))
Debug.Print PartName
Set swCustProp = swModel.Extension.CustomPropertyManager("")
intstatus = swCustProp.Add3("PartNo.", swCustomInfoType_e.swCustomInfoText, PartName, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
End Sub
-
Re: Get the model name as a custom property
Mahmoud Ebesh Dec 4, 2018 9:16 AM (in response to Fifi Riri)-
Re: Get the model name as a custom property
Fifi Riri Dec 4, 2018 9:22 AM (in response to Mahmoud Ebesh)Bienvenue!
-
-
Re: Get the model name as a custom property
Matt Gjertson Dec 4, 2018 1:41 PM (in response to Fifi Riri)Be careful with GetTitle, as it will display the filename as displayed in Windows Explorer. If you have the option to hide file extensions, then the title will not have the file extension in it and your InStrRev will will return 0, and your PartName will fail. You're better off using GetPathName and stripping the path and extension.
-
Re: Get the model name as a custom property
Mahmoud Ebesh Dec 4, 2018 1:47 PM (in response to Matt Gjertson)Hello Matt,
Thank you for your concern and follow up, but looks like the macro is working with the extensions shown or hidden, I already test it.
I appreciate your input anyway.
-
Re: Get the model name as a custom property
Matt Gjertson Dec 4, 2018 1:49 PM (in response to Mahmoud Ebesh)Close and reopen the part (or SolidWorks) to refresh it, then try again. You can see the title, it's what's displayed at the top of SolidWorks.
-
Re: Get the model name as a custom property
Mahmoud Ebesh Dec 4, 2018 2:00 PM (in response to Matt Gjertson)-
Re: Get the model name as a custom property
Matt Gjertson Dec 4, 2018 2:20 PM (in response to Mahmoud Ebesh)Did you rerun the macro with the extension hidden? I just tried it with this code (copied and pasted) running 2019 and Win10, and got an error with file extensions hidden. By all means if it's working for you, then use it. However, I've seen this fail before when deploying a macro that used GetTitle on a PC that had that setting to hide extensions.
-
Re: Get the model name as a custom property
Mahmoud Ebesh Dec 4, 2018 2:27 PM (in response to Matt Gjertson)Yes, I did rerun it both ways, that is weird.
Anyway, like you said, it is working with me, so for now I will use it as is, and we are not that many users, so I can control the windows options for everyone to be the same.
I really appreciate your input on this discussion, I will consider changing the GetTitle to GetPathName in the future (Maybe when upgrading to 2019 I guess).
BTW, I'm running SW 2018 SP 4.0 with Win10 if this helps.
-
Re: Get the model name as a custom property
Mahmoud Ebesh Dec 4, 2018 2:34 PM (in response to Matt Gjertson)Hello Matt, I know now what happened.
Looks like when I paste the codes into my VBA, somehow
VB change "PartName = swModel.GetTitle" into "PartName = swModel.GetPathName"
and don't ask me how, coz I don't know, I was checking the macro and noticed this change.
So the final Macro that I have now is like this:
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swCustProp As CustomPropertyManager
Dim PartName As String
Dim intstatus As Integer
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
PartName = swModel.GetPathName
PartName = Right(PartName, Len(PartName) - InStrRev(PartName, "\"))
PartName = Left(PartName, (InStrRev(PartName, ".") - 1))
Debug.Print PartName
Set swCustProp = swModel.Extension.CustomPropertyManager("")
intstatus = swCustProp.Add3("PartNo.", swCustomInfoType_e.swCustomInfoText, PartName, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
End Sub
So I guess you were right about the GetTitle, but still, I don't know how VB change this automatically
-
Re: Get the model name as a custom property
Fifi Riri Dec 4, 2018 2:43 PM (in response to Mahmoud Ebesh)What happened is I posted my reply with GetPathName but then edited it with GetTitle because it was shorter.
Sorry about the confusion.
-
Re: Get the model name as a custom property
Mahmoud Ebesh Dec 4, 2018 2:48 PM (in response to Fifi Riri)Aha, that's why, thanks for the explanation.
I guess it's better to change your reply back because the GetTitle will create issues as Matt said.
GetPathName works great.
-
Re: Get the model name as a custom property
Fifi Riri Dec 4, 2018 3:01 PM (in response to Mahmoud Ebesh)Done
-
-
-
-
-
-
-
-
-