Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I would like to give me your advice about using cadence orcad
so I can run sequentially cir or net(netlist) files with pspice.exe in cmd of my pc.
I use tcl/tk language.I have tried a few things without any results.
I want to make something similar to this one:
set top {C:\Users\file1.net C:\Users\file2.net};
foreach a $top
{exec D:\\tools\\bin\\pspice.exe -r $a}
There are two problems in your code.
The first problem is that \f is an escape character sequence in lists (for “go down one line”, IIRC; point is you don't want that interpretation). The second problem is that you've got your brace placement wrong in your foreach.
The first problem is best addressed by using / instead of \, and then using file nativename on the value fed to the OS. (You have to do that manually for argument to executables in expr; Tcl can't fix that for you entirely automatically.) The second problem is just a syntax error.
Try this:
set top {C:/Users/file1.net C:/Users/file2.net}
set pspice D:/tools/bin/pspice.exe
foreach a $top {
# Tcl knows how to convert executable names for you; not the other args though
exec $pspice -r [file nativename $a]
}
On Windows you may also try:
package require twapi
set top {C:/Users/file1.net C:/Users/file2.net}
foreach a $top {
twapi::shell_execute -path [file nativename $a]
}
This will work only if *.net files are already associated with PSpice application.
The code above rely on TWAPI extension (if you have it) and its shell_execute function, to open a document just like double-click works.
It's always a good idea to avoid backslashes in your code (no need to put it twice to escape them), file nativename will do the job for you.
Source: https://twapi.magicsplat.com/v4.5/shell.html#shell_execute
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I downloaded a 203775480 bytes (~200 MiB, exact size is important for a later error) JSON file which has all entries all on one line. Needless to say, my text editor (ViM) cannot efficiently navigate in it and I'm not able to understand anything from it. I'd like to prettify it. I tried to use cat file.json | jq '.', jq '.' file.json, cat file.json | python -m json.tool but none worked. The former two commands print nothing on stdout while the latter says Expecting object: line 1 column 203775480 (char 203775479).
I guess it's broken somewhere near the end, but of course I cannot understand where as I cannot even navigate it.
Have you got some other idea for prettifying it? (I've also tried gg=G in ViM: it did not work).
I found that the file was indeed broken: I accidentally noticed a ']' at the beginning of the file so I struggled to go to the end of the file and added a ']' at the end (it took me maybe 5 minutes).
Then I've rerun cat file.json | python -m json.tool and it worked like a charm.
I saw multiple answers to this question but none of them were specific to TCL 8.4 or prior.
So here's the code that I tried but didn't work:
set files [ glob home/*.tcl]
file copy {*}$files dest/
Copying issue is sorted by using eval as one of the comments suggests.
Moving the file should be done with:
eval file rename -force $flist dest/
Referring to the 8.5+ solution from How to copy or move multiple files with same extension?: There is no expansion operator prior to 8.5.
The example would become:
set files [glob home/*.tcl]
eval file copy $files dest/
That said, if possible, you should upgrade to 8.5+ (for a number of reasons), but avoiding [eval] in such expansion scenarios is advised. Alternativey, you might want to expand by looping explicitly through $files, but this has also a performance downside.
My problem is simple: I'm trying to write a tcl script to use $grofile instead writing every time I need this file name.
So, what I did in TkConsole was:
% set grofile "file.gro"
% mol load gro ${grofile}
and, indeed, I succeeded uploading the file.
In the script I have the same lines, but still have this error:
wrong # args: should be "set varName ?newValue?"
can't read "grofile": no such variable
I tried to solve my problem with
% set grofile [./file.gro]
and I have this error,
invalid command name "./file.gro"
can't read "grofile": no such variable
I tried also with
% set grofile [file ./file.gro r]
and I got the first error, again.
I haven't found any simple way to avoid using the explicit name of the file I want to upload. It seems like you only can use the most trivial, but tedious way:
mol load file.gro
mol addfile file.xtc
and so on and so on...
Can you help me with a brief explanation about why in the TkConsole I can upload the file and use it as a variable while I can not in the tcl script?
Also, if you have where is my mistake, I will appreciate it.
I apologize if it is basic, but I could not find any answer. Thanks.
I add the head of my script:
set grofile "sim.part0001_protein_lipid.gro"
set xtcfile "protein_lipid.xtc"
set intime "0-5ms"
set system "lower"
source view_change_render.tcl
source cg_bonds.tcl
mol load gro $grofile xtc ${system}_${intime}_${xtcfile}
It was solved, thanks for your help.
You may think you've typed the same thing, but you haven't. I'm guessing that your real filename has spaces in it, and that you've not put double-quotes around it. That will confuse set as Tcl's general parser will end up giving set more arguments than it expects. (Tcl's general parser does not know that set only takes one or two arguments, by very long standing policy of the language.)
So you should really do:
set grofile "file.gro"
Don't leave the double quotes out if you have a complicated name.
Also, this won't work:
set grofile [./file.gro]
because […] is used to indicate running something as a command and using the result of that. While ./file.gro is actually a legal command name in Tcl, it's… highly unlikely.
And this won't work:
set grofile [file ./file.gro r]
Because the file command requires a subcommand as a first argument. The word you give is not one of the standard file subcommands, and none of them accept those arguments anyway, which look suitable for open (though that returns a channel handle suitable for use with commands like gets and read).
The TkConsole is actually pretty reasonable as quick-and-dirty terminal emulations go (given that it omits a lot of the complicated cases). The real problem is that you're not being consistently accurate about what you're really typing; that matters hugely in most programming languages, not just Tcl. You need to learn to be really exacting; cut-n-paste when creating a question helps a lot.
I’m a newbie to Tcl and code writing in general. I have what seems like a simple coding issue that I have about 10 hrs into that I can’t seem to resolve.
I have a file that contains a list of nets clk123, n789, clk456, n246…. I need to reorder the list so that the clk* nets appear first when outputted. I can read the files in question and output the contents to the monitor or a file. But, I’m not able to find a way to reorder the list. I’ve spent so much time researching this that I’m now completely confused. Can someone offer a suggestion?
If there's just clk* and n* nets, a simple sort should be sufficient:
package require fileutil
proc sort data {
set lines [split $data \n]
set lines [lsort $lines]
join $lines \n
}
::fileutil::updateInPlace thefile.txt sort
Documentation: fileutil package, join, lsort, package, proc, set, split
Say I have a tcl script and I want to pass some arguments to the second script file which is being sourced in the first tcl:
#first tcl file
source second.tcl
I want to control the flow of second.tcl from first.tcl and I read that tcl source does not accept arguments. I wonder how I can do then.
source does not accept any additional arguments. But you can use (global) variables to pass arguments, e.g.:
# first tcl file
set ::some_variable some_value
source second.tcl
The second TCL file can reference the variable, e.g.:
# second tcl file
puts $::some_variable
Remark:
Sourcing a file means that the content of the sourced script is executed in the current context. That means that the sourced script has access to all variables existing in that context. The above code is the same as:
# one joint tcl file
set ::some_variable some_value
puts $::some_variable
Regarding the "::" thing -- see the explanation here (sorry, I don't have enough rep. to leave comments yet).
I should also add that the original question discusses a problem which appears to be quite odd: it seems that it could be better to provide a specific procedure in your second source file that would set up a state pertaining to what is defined by that script.
Something like:
source file2.tcl
setup_state $foo $bar $baz
Making [source] behave differently based on some global variables looks too obscure to me. Of course you might have legitimate reasons to do this, but anyway...