Recorded script fails but works if pasted to command line
I posted about this a while back in the drawing forum, and no one responded, and I just realized I should be asking in the programming/customization forum. The following is a snippet of a script that I originally recorded from the command line, and it worked for a couple days. (CT and CV are some custom commands). I made some revisions, and the script would no longer work. I thought perhaps my text editor (Notepad++) was leaving some artifacts, so I tried recording from the command prompt again, but still no joy.
Now if I cut and paste from the file to the command line it works right, but if I invoke the script it fails. Depending on how many CR-LFs I put between 0.87 and ELECTRICAL, it will either fail to complete entity selection and think I'm still in entity selection when it hits "ELECTRICAL", or it appears to have terminated the CT command and be back to the command prompt again and fails to recognize "ELECTRICAL" as a valid command (I included the response below). It seems like there might be a difference in timing, in the rate at which the string is evaluated depending on whether the script in invoked normally or pasted into the command line, and it's not correctly evaluating the CR-LF's when invoked.
Any thoughts?
ZE ct c 28.97,0.77 32.95,0.87 ELECTRICAL SCHEMATIC - Job Name cv c 30.10,0.11 31.84,0.31
: '_script
" ZOOM EXTENTS "
Select entities:c
First corner of crossing window: 28.97,0.77
Opposite Corner: 32.95,0.87
Entities in set: 1
Select entities:
Unable to recognize command "ELECTRICAL". This error can occur when the command is not supported for the active license.
Comments
-
Post the script file (put it in a ZIP if required). The Forum does not handle code very well.
0 -
Well, since CT is a custom function, and since the script fails while interacting with CT, I was going to create a similar script from native commands as a more accessible demonstration of the problem and post that as file rather than as code. But here's the interesting part -- if I use an internal command like ERASE, the script doesn't fail! While trying to troubleshoot this, I had already tried to do noun/verb operation of CT by running SELECT, then using CT with a "p" for previous sset, but the SELECT runs and CT doesn't complete entity selection after the 'p'.
So....apparently this runs differently (specifically the (ssget)) when CT is called manually or "CT" is pasted into the command line vs. when called from a script. Why? Here's what's in CT:
(defun c:CT (/ count ent newval sset sub1) (graphscr) (setq sset (ssget) count 0) (while (< count (sslength sset)) (setq ent (entget(ssname sset count))) (if (equal "TEXT" (cdr (assoc 0 ent))) (progn (setq newval (getstring T (strcat "\nNew value <" (cdr (assoc 1 ent)) ">: "))) (if (/= newval "") (gced ent 1 newval) );end if );end progn ); end if (setq count (1+ count)) );end while (princ) );end defun
0 -
The Lisp (I have used vle-entmod instead of gced) and the script seem to work. But there is an obvious issue: The selection set may not contain a text entity. In which case the Lisp does not prompt for a new text value.
0 -
You mean that's an issue for CT in general or when called from script? If invoked manually, and the operator doesn't pick a text entity, so what? It just returns to the command line and he's more careful the next time. If you're talking about the current issue with script execution, there should always be text at that location, and I don't think that relates to the script failure because it's still in the (ssget) when it fails. I've not seen any failures due to an otherwise valid selection set that doesn't contain any text.
0 -
Are you saying you copied my Lisp into your own Lisp file, called it from a script, and it completed entity selection?
If so, please post your script file!
0 -
I had to modify your Lisp code. I have used vle-entmod (built-in) to replace gced (which you did not include). Maybe you need to look at gced?
0 -
There's nothing of note in gced (Group Code EDitor). Just a wrapper for cleaner interface to entmod. Here you go.
(defun gced (entity groupCode newValue / sub1 ) (setq sub1 (assoc groupCode entity)) (entmod (subst (cons groupCode newValue) sub1 entity)) );end defun
0 -
Wow -- in a strange way you called it! By inserting debugging statements into CT, I found that in fact entity selection was completing (command prompt showed with 1 entity as expected -- assumed but later found not to be the correct entity), then completing CT with no changes because the selected entity was NOT a text entity. I verified the coordinates in the script (and remember it works fine if you paste it), and they're correct.
- So now I have a script that does nothing but draw a line between those same two points -- doesn't draw a thing. But enter the exact same command manually to the command prompt and it work perfectly. Here's a cap of the command line with the unsuccessful script calling an internal function, followed by it working right manually, with the same keystrokes. This is actually a verbatim cut and paste from my command window.
-
'_script
- l
" LINE " - LINE
Start of line or [Follow] :28.97,0.77
Set end point or [Angle/Length/Undo]:32.95,0.87
Set end point or [Angle/Length/Follow/Undo]: - l
- " LINE "
- LINE
Start of line or [Follow] :
Start of line or [Follow] :28.97,0.77
Set end point or [Angle/Length/Undo]:32.95,0.87
Set end point or [Angle/Length/Follow/Undo]:
0 -
OK, now were getting somewhere. It seems object snaps are in effect when you run the script but not when you type or cut and paste to the command line (and you wouldn't expect them to be in crossing selection). There is some linework within one aperture of the text I'm trying to select, and if I set OSMODE to 0 (normally 3071), then the script works. That's probably why it worked initially for a couple days, I had no object snaps on.
Still don't understand why points would be interpreted differently in script than when entered manually to the command line. Weird. Well, at least there's a workaround.
0 -
Glad to hear you got it solved.
Personally I would avoid scripts and only rely on Lisp code whenever possible.Some info that may be of interest:
- There is a related variable: OSNAPCOORD.
- If you select with a crossing in Lisp code, the selection area must be on-screen.
0 -
Agreed, I rarely use scripts. This seemed like a good application.
Wow, in 35 years I've never come across OSNAPCOORD. Since a value of 2 is 'Keyboard entry overrides object snap settings except in scripts', what I thought must be a bug is obviously intentional. And that's the default! Very odd.
0