maarten. Posted May 3, 2008 Share Posted May 3, 2008 I made a little script that does the following: It draws a rectange. When the users presses the Return key, the script will end. If not, then it wil change the mouse cursor to a cross when the user moves over the rectangle. When the user clicks in the rectange, two diagonals are drawn in the rectange and the script will end. It runs fine, but it's using a lot of CPU power (one of my processors runs at 100% when running the script). My first guess went to GetMouse();, so I left that one out of the script (and the rest of the Cursor change part too of course). But nothing changed, the script is still using 100% of the CPU power... So, is it know that MouseDown(); is such a CPU consuming Function? Or am I doing something wrong in my script? Thanks in advance . PROCEDURE Test; LABEL 99; VAR pX, pY : REAL; keyhit : INTEGER; muisBewX : REAL; muisBewY : REAL; muisklik : BOOLEAN; BEGIN keyhit:=13; Rect(0,0,20mm,20mm);; RedrawAll; While Not keydown(keyhit) DO BEGIN {** changes the mouse cursor to a cross is it moves over the rectangle **} GetMouse(muisBewX,muisBewY); IF (muisBewX>0)&(muisBewX<20mm)&(muisBewY>0)&(muisBewY<20mm) THEN SetCursor(LgCrossC) ELSE SetCursor(ArrowC); {** draws two diagonals into the rectangle when the user clicks in the rectange, the script will end **} muisklik:=MouseDown(pX,pY); IF (pX>0)&(pX<20mm)&(pY>0)&(pY<20mm) THEN BEGIN MoveTo(0,0); LineTo(20mm,20mm); MoveTo(20mm,0); LineTo(0,20mm); GOTO 99; END; END; 99: END; RUN(Test); (BTW: why are those CodeBoxes so small??? It's much easier to see the whole script in one in stead of having to scroll up-down, left-right all the time... And if I'm not mistaken, the Code Tags are the only way to make the "tabs" visible...) Quote Link to comment
Tom4 Posted May 6, 2008 Share Posted May 6, 2008 I tried your script and running around 9% the script spiked to 50% on one processor for an instant (had to do it several times to catch it) then returned to under 9%. I have a PC not a MAC but I hope this helps. Tom Quote Link to comment
maarten. Posted May 6, 2008 Author Share Posted May 6, 2008 Thanks for the Feedback Tom, i really apreciate it! I'm kind of puzzled what can be wrong with the script... Maybe it's just a mac problem? Quote Link to comment
Guest Lyndsey Posted May 6, 2008 Share Posted May 6, 2008 The script is doing exactly what you told it to do. It is running through the loop as fast as it can. You have a loop: while the user has not pressed a key if the mouse location is in the rectangle change the cursor do more stuff repeat the above two steps It is a surprise that the Windows XP version of VectorWorks is not doing the same....what you need (and I do not think it is possible) is some way to pause the script -- this kind of updating only needs to happen every 1/30th of a second. Quote Link to comment
maarten. Posted May 7, 2008 Author Share Posted May 7, 2008 Oke, that sounds logic to me, so I changed the script: - I left the cursor change out - I used a BOOLEAN to "pause" the script until the BOOLEAN turns TRUE So, if I get it right, the script only have to do something when there is a mouse click or a key hit. But still, the CPU runs at 100%... So there must be something wrong with my logic . PROCEDURE Test; LABEL 99; VAR pX, pY : REAL; keyhit : INTEGER; BEGIN keyhit:=13; Rect(0,0,20mm,20mm);; RedrawAll; While Not keydown(keyhit) DO BEGIN {** draws two diagonals into the rectangle when the user clicks in the rectange, the script will end **} IF MouseDown(pX,pY) THEN BEGIN IF (pX>0)&(pX<20mm)&(pY>0)&(pY<20mm) THEN BEGIN MoveTo(0,0); LineTo(20mm,20mm); MoveTo(20mm,0); LineTo(0,20mm); GOTO 99; END; END; END; 99: END; RUN(Test); Quote Link to comment
islandmon Posted May 7, 2008 Share Posted May 7, 2008 RedrawAll ; = global routine ... perhaps including FSActiveLayer would limit the procedure. Quote Link to comment
Guest Lyndsey Posted May 7, 2008 Share Posted May 7, 2008 Maarten, You're still not pausing the script, you actually need a way to tell the process, "PAUSE" == > Don't Run. Don't run in a loop, even if that loop is doing nothing. How badly do you need to change this behaviour? Do you really need it to run at less than 100%? Quote Link to comment
Pat Stanford Posted May 7, 2008 Share Posted May 7, 2008 Take a look at the GetKeyDown routine. It does pause the script until a key is pressed. I don't know if that will work in your case. Maybe is you explained more about what you are really trying to do we could help more. Are you trying to create a fill-out form? Maybe a dialog box would be a better interface. Pat Quote Link to comment
maarten. Posted May 7, 2008 Author Share Posted May 7, 2008 (edited) Well, I think (can be wrong of course) that you have to try to keep the CPU use as low as possible while excecuting a program, otherwise the chances of something causing an error will become bigger. But maybe it can be done in an other way, one that doesn't use that much CPU? I'm only scripting for a month or two so I don't know all the possibilities of VS. Maybe I have to work with REPEAT ... UNTIL? But i think that he will still make a loop then.... This little script is a part of a bigger one. My purpose was the follow: I have a pio with some rectangles in it. When the user clicks on a button in the OIP, he can start clicking in the drawing (until he hits a key to end). When he clicks in one of the rectangles, the diagonals are drawn in that rectangle. Are there other ways to accomplish this without making a loop? Here is the stripped down version of the script with the OIP button: (Note: it will only work if you place the pio on the 0,0 point of your drawing (i still have to put a GetBBox in the script)) PROCEDURE RegenWaterFormulier; CONST InternObjectInstellingen = 5; OverschrijfObjectInstellingen = 8; ipWidgetKnop = 12; ipKnop1 = 100; ipKnop2 = 200; ipKnopDruk = 35; ResetHandeling = 3; VAR resultaat : BOOLEAN; pioNaam : STRING; pioHandle : HANDLE; pioRecord : HANDLE; pioMuur : HANDLE; Vraag2Ja : BOOLEAN; DeHandeling : LONGINT; DeKnop : LONGINT; KnopHandeling : INTEGER; PROCEDURE Muisbeweging(pX : REAL); VAR mkX, mkY : REAL; keyhit : INTEGER; mk : BOOLEAN; BEGIN While Not keydown(keyhit) DO BEGIN mk:=MouseDown(mkX,mkY); IF (mkX>pX)&(mkX<(pX+20cm))&((mkY<20cm)&(mkY>0cm)) THEN BEGIN MoveTo(0cm,0cm); LineTo(20cm,20cm); MoveTo(0,20cm); LineTo(20cm,0cm); ResetObject(pioHandle); Redraw; END; END; END; BEGIN resultaat:=GetCustomObjectInfo(pioNaam,pioHandle,pioRecord,pioMuur); vsoGetEventInfo(DeHandeling,DeKnop); CASE DeHandeling OF InternObjectInstellingen: BEGIN resultaat:=SetObjPropVS(OverschrijfObjectInstellingen,TRUE); resultaat:=vsoInsertAllParams; resultaat:=vsoAppendWidget(ipWidgetKnop,ipKnop2,'Beantwoord vragen',0); END; ipKnopDruk: BEGIN CASE DeKnop OF ipKnop2: BEGIN Muisbeweging(0cm); END; END; END; ResetHandeling: BEGIN Rect(0,0,20cm,20cm); END; END; END; RUN(RegenWaterFormulier); EDIT: Thanks PAT, i'll try that one out... And indeed it's to fill in a form. I could use a dialog, but i would like to try the mouse click way first. if that wouldn't work, i can still use a dialog. Edited May 7, 2008 by maarten. Quote Link to comment
Guest Lyndsey Posted May 7, 2008 Share Posted May 7, 2008 Well, I think (can be wrong of course) that you have to try to keep the CPU use as low as possible while excecuting a program, otherwise the chances of something causing an error will become bigger. The only problem this will cause is that you will wear out the battery pretty quick if the user of the script is using a unplugged laptop. GetKeyDown, as documented, should pause the script. There is a better way, the "Wait" function. I didn't know about this one actually, unfortunately it accepts seconds, not milliseconds. So you could pause your script for a second, but then that will be very annoying to the user. What about GetPt, it is documented to get the points of a mouse down. Perhaps it waits as well? Try that out. Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.