Cancel
Showing results for 
Search instead for 
Did you mean: 

Export a Method to .txt Through SImTalk

Gears Phenom Gears Phenom
Gears Phenom

Hello all

 

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?

 

JamesM

9 REPLIES

Re: Export a Method to .txt Through SImTalk

Siemens Phenom Siemens Phenom
Siemens Phenom

Hello JamesM,

 

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.

 

Regards,

Ralf
------------------------------------------------------------------------------------------------------
Did you like the answer? Then click the Thumbs Up button.
Did the answer solve your problem? Then accept the answer as solution.

Re: Export a Method to .txt Through SImTalk

Gears Phenom Gears Phenom
Gears Phenom

Hello

 

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?

 

JamesM

Re: Export a Method to .txt Through SImTalk

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

you need to use the method ref to access the attribute of the method:

 

ref(object.method).program

 

 

Steffen Bangsow
freelance simulation specialist  
web: www.bangsow.eu
mail: steffen@bangsow.net

Re: Export a Method to .txt Through SImTalk

Siemens Phenom Siemens Phenom
Siemens Phenom

There are two ways to get the source code of a user-defined method attribute:

 

print SingleProc.MyMethod.program

print str_to_method("SingleProc.MyMethod").program

_________________________________________________________________
Did you like the answer? Then click the Thumbs Up button.
Did the answer solve your problem? Then accept the answer as solution.

Re: Export a Method to .txt Through SImTalk

Gears Phenom Gears Phenom
Gears Phenom

Hello

 

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.

 

JamesM

Re: Export a Method to .txt Through SImTalk

Legend
Legend

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:

SaveMethods(ObjectWhereProgramIs, ObjectWhereProgramIs.Program)

 

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
FileInterface.remove
FileInterface.write(SourceCode)
FileInterface.close

 

Re: Export a Method to .txt Through SImTalk

Gears Phenom Gears Phenom
Gears Phenom

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.

 

JamesM

Re: Export a Method to .txt Through SImTalk

Experimenter
Experimenter

Hi,

 

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?

Re: Export a Method to .txt Through SImTalk

Phenom
Phenom
Which methods do you want to retrieve

and

which methods can you not retrieve ?