Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Get Value By Name? (SWAPI)

Status
Not open for further replies.

SWVB101

Mechanical
Jun 25, 2003
85
0
0
US
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

 
Replies continue below

Recommended for you

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

 
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.
 
Have you tried swModel.GetPathname? That will get the fully-qualified file name.

Evan T. Basalik, MCSD
--------------------------------
It's all about prioritization...
 
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)
 
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

 
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, "")
    [b]FileExt = Split(FileName, ".")(UBound(Split(FileName, ".")))
    DrawName = Replace(FileName, "." & FileExt, "")[/b]
  
    MsgBox "Full Name: " & FullName & vbCrLf & _
           "File Name: " & FileName & vbCrLf & _
           "File Path: " & FilePath & vbCrLf & _
           [b]"File Ext: " & FileExt & vbCrLf & _
           "Model Name: " & DrawName & vbCrLf & _
           "Title: " & .GetTitle[/b]
  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

 
Status
Not open for further replies.
Back
Top