I want to assign custom properties to part file or assemblyfile base on the file name structure.
The file name is like 1234567890BasePlate.sldprt, which is 10 digitnumber plus part description.
So how can I assign the first 10 digit number to "PartNo" customproperties and assign the Base Plate ( the rest of the strings) as" Description" custom properties.
Thanks...SolidworksApi macros
The file name is like 1234567890BasePlate.sldprt, which is 10 digitnumber plus part description.
So how can I assign the first 10 digit number to "PartNo" customproperties and assign the Base Plate ( the rest of the strings) as" Description" custom properties.
Thanks...SolidworksApi macros

What you can do is strip the extension(always 7 characters) of thefile name and then get the first 10 characters to use for "PartNo"and the rest for Description".
Code wise this can be done as seen below:
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim custPropMgr As SldWorks.CustomPropertyManager
Dim title As String, name As String, descr As String
Dim retVal As Long
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
title = swModel.GetTitle
title = Left(title, (Len(title) - 7))
name = Left(title, 10)
descr = Right(title, Len(title) - 10)
Set custPropMgr = swModel.Extension.CustomPropertyManager("")
'if the custom properties do not exist if they do exist yu need touse CustomPropertyManager::Set instead
retVal = custPropMgr.Add2("PartNo",swCustomInfoType_e.swCustomInfoText, name)
retVal = custPropMgr.Add2("Description",swCustomInfoType_e.swCustomInfoText, descr)
End Sub
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim cpm As CustomPropertyManager
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set cpm = swModel.Extension.CustomPropertyManager("")
Dim path As String, filename As String, partno As String, desc AsString
path = swModel.GetPathName
filename = Mid$(path, InStrRev(path, "\") + 1) ' With extension
filename = Left$(filename, InStrRev(filename, ".") - 1) ' Removeextension
partno = Left(filename, 10)
desc = Right(filename, Len(filename) - 10)
cpm.Delete "PartNo"
cpm.Delete "Description"
cpm.Add2 "PartNo", swCustomInfoText, partno
cpm.Add2 "Description", swCustomInfoText, desc
End Sub
Luke Malpass wrote:
Title will not always be the actual filename can be totallydifferent, blank, with a * if not saved, or custom.
End Sub
Many Thanks!!!
Luke this is great. I needed something very similar for PDM use. My question is why can't I replace "path = swModel.GetPathName" with "path = swModel.GetOpenFileName". I tried this thinking it might simplify the code a little bit by not having to use the whole path but it won't work. It says "object doesn't support this property or method".
I've also added some lines to replace underscores with a space. We also have some models in the old file system with a "T" at the beginning which changes our right(string, length) function so I've added an if statement for that.
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim cpm As CustomPropertyManager
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set cpm = swModel.Extension.CustomPropertyManager("")
Dim path As String, filename As String, Description As String
path = swModel.GetPathName
filename = Mid$(path, InStrRev(path, "\") + 1) ' With extension
filename = Left$(filename, InStrRev(filename, ".") - 1) ' Remove extension
If InStr(Left(filename, 1), "t") = True Then
Description = UCase(Right(filename, Len(filename) - 7))
Else
Description = UCase(Right(filename, Len(filename) - 6))
End If
Description = Replace(Description, "_", " ")
cpm.Delete "Description"
cpm.Add2 "Description", swCustomInfoText, Description
End Sub