A simple Macro Help.

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
Answers 7
SF
SF 2009-03-30 Best Answer
Hi Shaodun,

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
SF
SF 2009-03-30
Title will not always be the actual filename can be totallydifferent, blank, with a * if not saved, or custom.




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
SF
SF 2009-03-30

Luke Malpass wrote:

 

Title will not always be the actual filename can be totallydifferent, blank, with a * if not saved, or custom.


End Sub

Perfect !!!
Many Thanks!!!
SF
SF 2009-11-18

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

SF
SF 2009-11-18
The reason is GetOpenFileName is a function to display the user with an open file dialog, nothing to do with filename.
SF
SF 2009-11-18
I see.  That would make sense then.  Where is the best place to find the actual function of objects like that?
SF
SF 2009-11-18
Tend to start by checking the API help file for the function. If that fails add references in Visual Studio to the SolidWorks dll files then open Object Browser (Ctrl+Alt+J) and search for it in there. Combination of the two usually works.