I made a macro to take only project number and writes it into custom properties but If I could also take part number and part name from file name that would be awesome.
taking the project number was easy because it is always 6 digits
sPathName = swModel.GetTitle 'Get File Name & Path
Partnumber = Left(sPathName, 6) 'take part number
Set swCustProp = swModel.Extension.CustomPropertyManager("")
swCustProp.Set "PROJECT #", Partnumber 'write part number in custom properties
I almost want to do remove Left(sPathName - Parnumber - " - ", 2)
and just remove " - " from the rest of it to pull part name but I don't know hot to do that or is it even possible to do?
below is my file name structure.
Project number - part number - part name
200471 - 01 - Tray.sldprt
200471 - 02 - Length Panel.sldprt
SolidworksApi/macros
What error are you getting?
You can also use the split function.
Hadn't thought of that, Deepak. Split is a much cleaner option, IMO.
This is awesome. I am trying to reduce manual work as much as possible. I can think the algorithm but don't know all the functions. Next project is automate this for all the .sldprt in the entire asm.
what is the syntax for that function? it would be great if you have an example.
This example will be good start for me. Thanks for the ideas.
Split function (Visual Basic for Applications) | Microsoft Docs
Dim strFull As String
Dim arrSplitStrings1() As Variant
Dim arrSplitStrings2() As Variant
Dim strSingleString1 As String
Dim strSingleString2 As String
Dim strSingleString3 As String
Dim i As Long
strFull = "Some - Old - Hags - Can - Always - Hide - Their - Old - Age" ' String that will be used.
arrSplitStrings1 = Split(strFull, "-") ' arrSplitStrings1 will be an array from 0 To 8.
' arrSplitStrings1(0) = "Some " and arrSplitStrings1(1) = " Old ".
' The delimiter did not include spaces, so the spaces in strFull will be included in the returned array values.
arrSplitStrings2 = Split(strFull, " - ") ' arrSplitStrings2 will be an array from 0 To 8.
' arrSplitStrings2(0) = "Some" and arrSplitStrings2(1) = "Old".
' The delimiter includes the spaces, so the spaces will not be included in the returned array values.
'Multiple examples of how to return the value "Can" (array position 3).
strSingleString1 = arrSplitStrings2(3) ' strSingleString1 = "Can".
strSingleString2 = Split(strFull, " - ")(3) ' strSingleString2 = "Can".
' This syntax can be used if the entire array is not needed, and the position in the returned array for the desired value is known.
For i = LBound(arrSplitStrings2, 1) To UBound(arrSplitStrings2, 1)
If InStr(1, arrSplitStrings2(i), "Can", vbTextCompare) > 0 Then
strSingleString3 = arrSplitStrings2(i)
Exit For
End If
Next i
swCustProp.Set "Item #", PartNumber
swCustProp.Set "Description", PartName
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim sFileName As String
Dim Path As String
Dim nErrors As Long
Dim nWarnings As Long
Dim QualityNum As Integer
Dim swCustProp As CustomPropertyManager
Dim sPathName As String
Dim PathSize As String
Dim FileName As String
Dim PartNumber As String
Dim PartName As String
Dim arrFileName As Variant
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object
Sub main()
Set swApp = Application.SldWorks
'Get active document
Set swModel = swApp.ActiveDoc
MsgBox (Path)
sFileName = Dir(Path & "*.sldprt")
Set swModel = swApp.OpenDoc6(Path + sFileName, swDocPART, swOpenDocOptions_Silent, "", nErrors, nWarnings)
Set swModel = swApp.ActiveDoc
swApp.Visible = False
PathSize = Strings.Len(sPathName)
FileName = Strings.Left(sPathName, PathSize - 7)
arrFileName = Split(FileName, " - ")
PartNumber = arrFileName(1)
PartName = arrFileName(2)
swCustProp.Set "Description", PartName
swModel.Save 'Should use save3 for proper control
Set swModel = Nothing
sFileName = Dir
Loop
Set swModel = Nothing
End Sub
You are close, look for the Add3 function in API help files.
swCustProp.Add3 "Item #", swCustomInfoType_e.swCustomInfoText, PartNumber, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd
swCustProp.Add3 "Description", swCustomInfoType_e.swCustomInfoText, PartName, swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd
Yes I found after I sent the message. This works much better
but now I have another issue. Some people have the same custom property in configuration specific tab and It dominates in the BOM. I am looking for the code that deletes in the configuration specific (if it is exist) before goes the to above code.
Look into the Delete2 method.
I am getting an error at this line. Can't figure out the issue
Set cusPropMgr = config.CustomPropertyManager
Fixed my issue. swModel SldWorks.ModelDoc2 was conflicting with active file and the file opening so I defined another SldWorks.ModelDoc2 and called it sModel. Fixed the problem.
Hi Mehmet,
Utilize the functions below to manipulate the path name that you need:
InStr function (Visual Basic for Applications) | Microsoft Docs
InStrRev function (Visual Basic for Applications) | Microsoft Docs
Left function (Visual Basic for Applications) | Microsoft Docs
Mid function (Visual Basic for Applications) | Microsoft Docs
Right function (Visual Basic for Applications) | Microsoft Docs
Something like this could work. Look for the hyphen separators, and then strip the part name from its extension with the last line.
firstHyphenLocation = InStr(1, pthName, " - ") - 1
secHyphenLocation = InStrRev(pthName, " - ") - 1
projNum = Left(pthName, firstHyphenLocation)
partNum = Mid(pthName, firstHyphenLocation + 4, 2)
prtNameExtension = Right(pthName, secHyphenLocation)
prtName = Left(prtNameExtension, InStr(1, prtNameExtension, ".", 0) - 1)