Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

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

Status
Not open for further replies.

nickjb

Mechanical
Jun 19, 2007
35
0
0
GB
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
 
Replies continue below

Recommended for you

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
 
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".

[bat]Honesty may be the best policy, but insanity is a better defense.[bat]
-SolidWorks API VB programming help
 
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
 
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!


Artem Taturevich, CSWP
Software and Design Engineer
AMCBridge LLC
 
Thanks. Just written a basic test macro and it works perfectly (after a little debugging). Now to write the real thing.
 
Status
Not open for further replies.
Back
Top