Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

MACRO QUESTION -

Status
Not open for further replies.

sirbartoo

Mechanical
Sep 8, 2005
53
I can take characters from the sw file NAME and place them in the sw custom properties of the dwg using a macro. Does anybody have a macro that would allow me to take parts of a file PATH and apply them to a sw property?
ex.

file path: J:1234/abcd/567.slddwg.

custom property would read: 1234567.
 
Replies continue below

Recommended for you

You need to write a function or sub that parses the path string. Start from one end of the path and chop off the bits you nees every time you come across a "\" or "/". Use the VB Mid function to examine one character at a time in a loop that goes from 1 to Len (PathString).

[bat]Honesty may be the best policy, but insanity is a better defense.[bat]
-SolidWorks API VB programming help
 

Tempval = LTrim(RTrim(Document.GetCustomInfoValue("", PartNoPropName)))
ModelName = Mid(NewParseString(UCase(Document.GetTitle), ".SLD", 0, 0), 17, 3)


This is the line in my macro that trims the file name. I'm not sure how to change this into cutting out specific characters from a file path. I know enough about programing to change the 17 and 3 to get what I want, but anything more is a little over my head. If I could apply this part of the macro to the file path and run this part of the macro twice, that would solve my problem. I don't think it would take much effort, but i'm not sure how to do it.
 
my inefficient, slow, simplistic way of doing things.
Enjoy! Chris


Sub main()

s = "C:\123\i need a beer\456A.sldprt" 'your input

L = Len(s) 'length of the string

For X = L To 1 Step -1 'find the leftmost "\"
If Mid(s, X, 1) = "\" Then a = X
Next X

For X = L To a + 1 Step -1 'find the second leftmost \
If Mid(s, X, 1) = "\" Then b = X
Next X

For X = 1 To L 'find the rightmost \
If Mid(s, X, 1) = "\" Then c = X
Next X

For X = 1 To L 'find the .
If Mid(s, X, 1) = "." Then d = X
Next X


s1 = Mid(s, a + 1, (b - a - 1)) 'cutout the first part number
s2 = Mid(s, c + 1, (d - c - 1)) 'cutout the second part number
partno = s1 + s2 'patch them together
MsgBox (partno) 'done

End Sub
 
DO NOT USE GETTITLE, it's output is dependent upon a Windows setting, therefore you could get a different value on different computers.

Enjoy,
Ken

Code:
Sub main()

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim sFileNameAndPath As String
Dim sFileName As String

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

sFileNameAndPath = swModel.GetPathName
sFileName = Right$(swModel.GetPathName, (Len(swModel.GetPathName) - InStrRev(swModel.GetPathName, "\")))

Debug.Print sFileNameAndPath
Debug.Print sFileName

End Sub
 
sirbartoo,

Looks like I didn't read your original post close enough. The macro above will only parse out the filename.

I have something that can do as you requested but I need a little clarification first...is the filename and path actually "J:1234/abcd/567.slddwg". Usually a path is something like "J:\1234\abcd\567.slddwg".

Ken
 
You're close. Actually, you are working from right to left (usually the safest way to go).

You should test for both "\" and "/", as both can be delimiters for path strings. You probably want to stage your lops as follows:
For X=L to 1 step -1
For X=a-1 to 1 step -1
For X=b-1 to 1 step -1
etc.

This will parse your path from right to left.

The best thing would be to write this as a function so you can reuse it many times in your code. Perhaps have your input be the path string, and output be your desired result.
 
THE FILE NAME IS ...J:\XXXXX\XXX\XXXX.SLDDWG. AS I READ THROUGH YOUR RESPONSES, I AM QUICKLY REALIZING THAT THIS IS OVER MY HEAD. I WAS HOPING TO REPLACE ONE SPECIFIC LINE OF CODE. I HAVE ATTACHED THE MACRO I AM ATTEMPTING TO ALTER (BORROWED FROM LENNY'S SITE). IF ANYBODY IS GENEROUS ENOUGH TO PEEK AT IT I WOULD BE GREATFUL.
 
 http://files.engineering.com/getfile.aspx?folder=4d829ba6-8435-4bc6-8849-e5e2b951e74a&file=PartNoProperty.zip
Status
Not open for further replies.

Part and Inventory Search

Sponsor