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
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
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
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".
http://www.EsoxRepublic.com-SolidWorks API VB programming help
RE: Using a macro to read a text file to update custom properties
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
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://msd
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