×
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

Get Value By Name? (SWAPI)

Get Value By Name? (SWAPI)

Get Value By Name? (SWAPI)

(OP)
You can set properties and other values, such as dimensions, to notes via SW Addressing, For example:
DWG Number:    $PRP:"SW-File Name"
Scale:        $PRP:"SW-Sheet Scale"
Current Sheet:    $PRP:"SW-Current Sheet"
Total Sheets:    $PRP:"SW-Total Sheets"
Weight:        "SW-Mass@@Default@partnum.SLDASM" LBS


But is it possible to set these values to Variables in VB...

Such as:

CODE

Dim X as String
X = Y("SW-Mass@@Default@partnum.SLDASM")

Where Y() is the qualified function that retrieves the values from SolidWorks, and assigns to X...?

If this is confusing, please say so, and I will try to clairify...

Thanks in advance,
-Josh

When dealing with computers, there are 10 things you need to know: one, zero, and the rest is Binary.
-Josh S

RE: Get Value By Name? (SWAPI)

Please do clarify.  I am confused...

:)

Evan T. Basalik, MCSD
--------------------------------
It's all about prioritization...

RE: Get Value By Name? (SWAPI)

(OP)
as far as the notes in a SW Drawing go...

you can make a reference to other values by using strings to reference them...

such as...
If your File is named "12345.123.slddrw", and you place:
  $PRP:"SW-File Name"
in a note, the note will display: "12345.123"

If an assembly (12345.123.sldasm) weighs 123.5 lbs, and you place:
  "SW-Mass@@Default@12345.123.SLDASM" LBS
in a note, the note will display: "123.5 LBS"

My question is if there is a way to get these values the same way via VB, to set them to a variable rather than display them in a note...

For use with a Form, or exporting them to a text file, etc...

When dealing with computers, there are 10 things you need to know: one, zero, and the rest is Binary.
-Josh S

RE: Get Value By Name? (SWAPI)

Have you tried CustomInfoValue?

Evan T. Basalik, MCSD
--------------------------------
It's all about prioritization...

RE: Get Value By Name? (SWAPI)

I have the same problem.  I can't find a way to directly access the file name in VBA, but you can use the CustomInfo to link it.

In the drawing template, under the Custom tab in Summary Info (accessed from File->Properties), add a File Name (or whatever you want to call it) property which links to the SolidWorks file name property using $PRP:"SW-File Name".

In the VBA code you can then just access the file name value by setting a variable equal to swModel.GetCustomInfoValue("", "File Name").

This seems like a bass-ackwards way of doing it, since that file name property should already be available somewhere already, but it does work.

RE: Get Value By Name? (SWAPI)

Have you tried swModel.GetPathname?  That will get the fully-qualified file name.

Evan T. Basalik, MCSD
--------------------------------
It's all about prioritization...

RE: Get Value By Name? (SWAPI)

I use this code to get the file name from the path name

CODE

    Dim swApp                   As Object
    Dim swModel                 As Object
    Dim swDraw                  As Object
    Dim sFileName               As String
    Dim sPathName               As String
    Dim nErrors                 As Long
    Dim nWarnings               As Long
    Dim nRetval                 As Long
    Dim bShowMap                As Boolean
    Dim bRet                    As Boolean
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
       
    sPathName = swModel.GetPathName
    sPathName = Left(sPathName, Len(sPathName) - 6)
    
    sFileName = swModel.GetTitle
    sFileName = Left(sFileName, Len(sFileName) - 9)

RE: Get Value By Name? (SWAPI)

(OP)
Here is a fancy way...

Throgh this in a macro and run it...

CODE

Sub main()
  Dim FullName As String, FileName As String, FilePath As String
  With Application.SldWorks
    FullName = .ActiveDoc.GetPathName
    FileName = Split(FullName, "\")(UBound(Split(FullName, "\")))
    FilePath = Replace(FullName, FileName, "")
  End With
  
  MsgBox "Full Name: " & FullName & vbCrLf & _
         "File Name: " & FileName & vbCrLf & _
         "File Path: " & FilePath & vbCrLf
End Sub

Minimum variable useage... No objects declared

Enjoy,
-Josh

When dealing with computers, there are 10 things you need to know: one, zero, and the rest is Binary.
-Josh S

RE: Get Value By Name? (SWAPI)

(OP)
And for the Name without extention...

CODE

Sub main()
  Dim FullName As String, FileName As String, FilePath As String
  Dim FileExt As String, DrawName As String
  With Application.SldWorks.ActiveDoc
    FullName = .GetPathName
    FileName = Split(FullName, "\")(UBound(Split(FullName, "\")))
    FilePath = Replace(FullName, FileName, "")
    FileExt = Split(FileName, ".")(UBound(Split(FileName, ".")))
    DrawName = Replace(FileName, "." & FileExt, "")

  
    MsgBox "Full Name: " & FullName & vbCrLf & _
           "File Name: " & FileName & vbCrLf & _
           "File Path: " & FilePath & vbCrLf & _
           "File Ext: " & FileExt & vbCrLf & _
           "Model Name: " & DrawName & vbCrLf & _
           "Title: " & .GetTitle

  End With
End Sub

Note that you can also use ModelDoc.GetTitle but for drawings, it will return the sheet name as well...

So, if you just want the Drawing name, Do this...
DrawingName = Split(ModelDoc.GetTitle & " - ", " - ")(0)

When dealing with computers, there are 10 things you need to know: one, zero, and the rest is Binary.
-Josh S

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