×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

SW Custom Property API

SW Custom Property API

SW Custom Property API

(OP)
All,
 I was hoping someone could assist me in figuring this out. I have limited knowledge in API. What I was trying to do is this. We have Custom Properties in our part files like: "Release/ECO # Date" or "New Rev". I wanted to run a macro where I could grab the existing Value we have in "Release/ECO # Date", assign a new property called "ReleaseDate" and write that info to it. Would anyone have any examples or ideas??

Thank You

RE: SW Custom Property API

Check the SolidWorks web site for Custom Properties API's.

Bradley

RE: SW Custom Property API

Try this for a start (it is an extremely simple rebuild benchmark that writes the required time for a full rebuild to a property):

CODE

Option Explicit

Public Enum swUserPreferenceIntegerValue_e

    swAutoSaveInterval = 3

End Enum

Sub main()
    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.ModelDoc2
    Dim nStart                      As Single
    Dim i                           As Long
    Dim bRet                        As Boolean
    Dim propRet                     As Boolean
    Dim runTime                     As String
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    ' Turn off automatic save
    bRet = swApp.SetUserPreferenceIntegerValue(swAutoSaveInterval, 0)

    nStart = Timer

    bRet = swModel.ForceRebuild3(False)
    runTime = Timer - nStart & " seconds"
    propRet = swModel.AddCustomInfo3("", "LastRebuildTime", swCustomInfoText, runTime)
End Sub

This code is almost straight from the solidworks API help, so send SW your thanks if you can make use of the code! winky smile

Stefan Hamminga
EngIT Solutions
CSWP/Mechanical designer/AI student

RE: SW Custom Property API

If you're wanting to do this same action on multiple files you could do it with VBScript or an Excel macro without having to have SW open.   I believe there's even a free program out there that can do certain batch processing for custom properties.   

RE: SW Custom Property API

I have looked at "dsofile" and haven't figured out how to use it.

Please, people who have use it to read and update custom properties without opening files, post some of your code.

RE: SW Custom Property API

Here's some code out of an Excel sheet I used to update all custom properties from an old job to a new job.  I wrote this as a one-time shot, so it's pretty brute-force, and could use more comments.  Basically, it uses a text file as input for the names of the files to edit.  It then searches each custom property (regardless of its name) for specific strings and replaces them with different ones.  I had it dump the custom prop name, old value, and new value to the Excel sheet so I could do a quick visual check.

Of course, for this to work you have do download and register the dll first, and include it in the references.

Hope this helps!

-Josh

Sub DoTheSwap()


Dim PropReader As DSOleFile.PropertyReader
Dim DocProps As DSOleFile.DocumentProperties
Dim CustProp As DSOleFile.CustomProperty
Dim PropVal As String

Dim fso As Scripting.FileSystemObject

Dim sFile As String
Dim sFilesPath As String
Dim sFilesList As Scripting.TextStream
Dim CurPath As String
Dim OldJobNo As String
Dim NewJobNo As String
Dim OldProjName As String
Dim NewProjName As String
Dim OldMcNo As String
Dim NewMcNo As String
Dim OldDrawn As String
Dim NewDrawn As String
Dim CurRow As Long

sFilesPath = "C:\Documents and Settings\tnjbrady\Desktop\out.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
Set sFilesList = fso.OpenTextFile(sFilesPath, ForReading)
Set PropReader = New DSOleFile.PropertyReader

OldJobNo = "TC04152"
NewJobNo = "TC05083"
OldMcNo = "AS134"
NewMcNo = "AS144"
OldProjName = "STEPPER SUB-ASSY CELL"
NewProjName = "STEPPER SUB-ASSY #2"
OldDrawn = "John Doe"
NewDrawn = "Jim Smith"
CurRow = 1
While Not sFilesList.AtEndOfStream
    CurPath = sFilesList.ReadLine
    Set DocProps = PropReader.GetDocumentProperties(CurPath)
    For Each CustProp In DocProps.CustomProperties
        On Error GoTo SKIP
        PropVal = CustProp.Value
        ActiveSheet.Cells(CurRow, 1).Value = CustProp.Name
        ActiveSheet.Cells(CurRow, 2).Value = PropVal
        PropVal = Replace(PropVal, OldJobNo, NewJobNo)
        PropVal = Replace(PropVal, OldProjName, NewProjName)
        PropVal = Replace(PropVal, OldMcNo, NewMcNo)
        PropVal = Replace(PropVal, OldDrawn, NewDrawn)
        CustProp.Value = PropVal
        ActiveSheet.Cells(CurRow, 3).Value = PropVal
        CurRow = CurRow + 1
SKIP:
    Next
Wend
End Sub

RE: SW Custom Property API

(OP)
Thank you everyone for you help. I do however have hopefully one more question. This is what I have so far:

areco = Part.GetCustomInfoValue("", "Release/ECO # Next")
ar = Part.GetCustomInfoValue("", "Release/ECO #")
arecodate = Part.GetCustomInfoValue("", "Release/ECO # Date Next")

aBool = Part.AddCustomInfo3("", "AR-ECO #", 30, areco)
aBool = Part.AddCustomInfo3("", "AR #", 30, ar)
aBool = Part.AddCustomInfo3("", "AR-ECO Date", 30, arecodate)

What this is doing is pulling the existing part custom property info "Release/ECO # Next", then writing a new property "AR-ECO #" and placing the existing info into that new property.

Now what I would like to do is, if there is no value in "Release/ECO # Next" field, then have it look to another existing field, "AR" and place that information in "AR-ECO #". Any ideas??

Thanks Again.

RE: SW Custom Property API

Immediately after your line

areco = Part.GetCustomInfoValue("", "Release/ECO # Next")

put

if areco = "" then
   areco = Part.GetCustomInfoValue("", "AR")
end if

RE: SW Custom Property API

(OP)
handleman,
 Thank You!! You just made my day. Appreciate the help!

RE: SW Custom Property API

Thanks, handleman. A star for you.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources