×
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

Using a macro to read a text file to update custom properties

Using a macro to read a text file to update custom properties

Using a macro to read a text file to update custom properties

(OP)
I need a bit of help with custom properties.

I'm trying to set up a custom propery that contains a detailed material spec. What I want is the user to choose a material from the list as normal but once they have chosen the material a second property is populated with more detail. Ideally the list of detailed specs would be stored in a text file and read from there.

eg Property: Material; Value: Steel A1
   Property: Material spec; Value: ASTM A228 spring temper

I have a horridly clunky way of doing it but I'm sure there is a better way.

swCustPropMgr.Get2 "Material", valOut, evalValOut
Select Case evalValOut
Case Is = "Steel A1"
swCustPropMgr.Delete ("MaterialSpec")
Success = swCustPropMgr.Add2("MaterialSpec", swCustomInfoText, "ASTM A228 spring temper")
End Select

RE: Using a macro to read a text file to update custom properties

I think better to use Dictionary object instead of "Select Case" statements.

And there is no need to delete and recreate the property field. Just use ::Set method.

For example:

Dim dic As Object

Set dic = CreateObject("Scripting.Dictionary")
   
dic.Add "Steel A1", "ASTM A228 spring temper"
'Add more lines here
'...

swCustPropMgr.Get2 "Material", valOut, evalValOut

If dic.Exists(evalValOut) Then
    Success = swCustPropMgr.Set("MaterialSpec", dic.Item(evalValOut))
End If

 

Artem Taturevich, CSWP
Software and Design Engineer
AMCBridge LLC
www.amcbridge.com

RE: Using a macro to read a text file to update custom properties

Assumption: your master list is in a delimited text file,
Artem's idea is good.  I'd start there.

i.e. with short material spec and longer spec on same line.
After the property is assigned, open the master list and read the entire list into 2-dimensional array, then look for a match in the array.

An alternative would be to read the master list into a Collection.  Objects in a collection can be tagged with unique names.  Instead of reading your master list into an array, read it into a collection where the longt spec is the item and the short spec is the name for the item.

Do you know how to read a text file from VB?  Learn about the TextStream object.  It also requires a FileSystemObject, and you need to add a project reference to "Microsoft Scripting Runtime".

batHonesty may be the best policy, but insanity is a better defense.bat
http://www.EsoxRepublic.com-SolidWorks API VB programming help

RE: Using a macro to read a text file to update custom properties

(OP)
Thanks ArtemTat, that makes sense and looks pretty neat.

TheTick, I like the sound of reading from a delimited text file but no idea how to do it.  I'll look up VB textStreams.  Do you have any (simple) sample code?

Thanks to both of you

RE: Using a macro to read a text file to update custom properties

Check this:

Const ForReading = 1
Dim fso, MyFile, FileName, TextLine

Set fso = CreateObject("Scripting.FileSystemObject")

' Open the file for output.
FileName = "c:\testfile.txt"

' Open the file for input.
Set MyFile = fso.OpenTextFile(FileName, ForReading)

' Read from the file and display the results.
Do While MyFile.AtEndOfStream <> True
    TextLine = MyFile.ReadLine
Loop
MyFile.Close

Do not forget to add reference to "Microsoft Scripting Runtime" library!

http://msdn.microsoft.com/en-us/library/h7se9d4f(VS.85).aspx

Artem Taturevich, CSWP
Software and Design Engineer
AMCBridge LLC
www.amcbridge.com

RE: Using a macro to read a text file to update custom properties

(OP)
Thanks.  Just written a basic test macro and it works perfectly (after a little debugging).  Now to write the real thing.

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