I am currently looking into a way to export methods out from a project to create a backup of them all as text files (so they can be easily viewed and read). I know that this can be done for each method through a button on the ribbon menu but I got a lot of methods to backup and want to create a little peice of code to do it for me.
I have already got a way to retrive all of the methods that I want to save without knowing the structure of the system, but have now found a lack of way to export them through SimTalk code.
Is their any method to do this?
Solved! Go to Solution.
if you already have the code to access all methods then you can access the 'Program' attribute of a method to access its source code. This could then be written using the 'FileInterface' object.
Thank you for the quick reply and what sounds like a really nice solution the how to save out the method's contents. I am having issue now accessing the method, so let me explain what I am trying to do a bit better and that is happening.
I am trying to save out the methods that are saved as user drfined variables on a object (I like to store methods within objects as it makes the model a lot more tidy instead of having them as their own objects) and have got some code to cycle through the user defined attrbitues based on the numAttr value, and checking through the getAttrType to see if it is a method or not.
I have tried both using the getAttrValue command and using the Str_To_Obj command (recreating the object path from the data I have) and neither will let me run the Program command on this method. Is it possible to run commands on methods stored inside objects or just on stand alone methods?
There are two ways to get the source code of a user-defined method attribute:
Have tried to use the ref approtach already and not to much luck, even using a ref of the Str_To_Obj did not work in my case.
I did not notice their was a Str_To_Method, will take a closer look at this when I am next on the softwear and will report back as to if it has worked or not.
This code will work to save both method objects and user-defined attributes of type method. Here I call it SaveMethods.
Calling with method object:
Calling with user-defined attribute:
SaveMethods(ObjectWhereProgramIs, ObjectWhereProgramIs.getSubAttribute("NameOfUserDefinedAttribute", "Program"))
// Method to save source code for other methods.
param MethodObject : object, SourceCode : string
// Just to clean up the code.
var FileInterface : object := .InformationFlow.FileInterface
var newFileName : string := to_str(getCurrentDirectory, "\\", MethodObject.Name, ".txt")
FileInterface.FileName := newFileName
Right, have got into the office now and can say that for me trying to mostly work with userdefined attributes of methods that the Str_To_Method command works quite well and gives me access to the attributes of the method nicely.
Also got to say that the code that Verbalins provided works nicely as well but have found that when running it on multiple methods in the same object that the file names match so it replaces itself multiple times so have modfied it slightly so it dont do this. Really like the way of creating the file interface as a variable of the method and not as its own object, a much nicer solution, will defently be modfieing some of my other code to match this as its a lot cleaner than creating and destroying them as needed.
Thank you everyone for your help.
I am working currently on the same problem but couldn't come up with the solution yet to retrieve all methods in a complex simulation structure. Could you please share your approach?