Jump to content
Maccadin

AutoCAD script using bitmaps or alternatives

Recommended Posts

Hi all,

 

I started trying to understand all this scripting stuff so forgive me for dumb questions in advance.

 

I'm attempting to set up some scripts to make my job as a draftsman that little bit more efficent. I mainly use AutoCAD and at the start of every feature survey I do theres a few things I like to do as cleanup.

 

The first two are setting the viewres to 6000 which I've managed to do and it works (procedure Viewres;) and quick selecting all blocks with a certain name CCAD_CROSS and deleting them.

 

To avoid as much confusion as possible I've attached a video of me manually deleting the blocks and I'm just trying to re-create this in SCAR so all my cleanup is done much faster. I know AutoCAD works with LISPS but I'd prefer to learn SCAR so i can replicate this across other programs that don't have the luxury.

 

All i have to do to start is type QSELECT and press enter which I have and it works, then I'm under the impression I should maybe make SCAR search for a bitmap so i created one for the 'Object Type: Multiple' (see video) and then click on it then search for 'Block Reference' maybe with another bitmap? etc. etc. until it has completed all the steps I showed in the video.

 

I can do this with co-ordinates and it works fine but I want to be able to give it to other people in my office to use and they may have their screens setup differently or at different resolutions.

 

My script at the moment works if I just say IF bitmap found movemouse but I want it to click in the middle of my bitmap if it's found.

 

If there is an easier way of achieving what I want im all for that. Attached is the mess of script I've attempted haha.

 

Thanks in advance,

Maccadin

 

[sCAR]program Acad_Cleanup;

 

var

Bmp: Integer;

x, y: Integer;

Box1: Tbox;

Desktop: TSCARWindowClient;

 

procedure LoadBitmaps;

begin

Bmp := TSCARBitmap.Create('deNrNVO0RwyAIZYGs0SEYiHmchmUcptWggp9nz' +

'l6v/IkhDx48MC+44P178w4BgLh6R+eHyKH/KSwCUDFSwIKxJJxl3mJ0jjKICYl' +

'wh/GoR+c5UcYnd5mVK1QDICL0TpmNFUHcjWRV5TexXzB2qsa0CRePgTMBTHzTt' +

'QKImTRkn7HaAWKtxJqhzCGyQVGXE0YLMxdgPOiCKQqXkHBcqZpgnarNh/lqma6' +

'zJFjNWpZEC0C0ymlCQc4u28EP5Kv5/pHxuX0AQRuCJA==');

Desktop := TSCARWindowClient.Create(GetDesktopWindow);

SetClient(Desktop);

Box1 := GetWindowBox(GetDesktopWindow);

end;

 

procedure Viewres;

begin

wait(300)

TypeText('VIEWRES' + Chr(13) + Chr(13) + '6000' + Chr(13));

end;

 

procedure CCAD_CROSS;

begin

wait(1000)

TypeText('QSELECT' + Chr(13));

if(FindBitmap(x, y, Bmp, 0, 0, Box1.X2 - 1, Box1.Y2 - 1)) then

begin

MoveMouse(500, 500);

end;

 

end;

 

begin

LoadBitmaps;

Viewres;

CCAD_CROSS;

end.[/sCAR]

cleanup.rar

Link to comment
Share on other sites

I've always wanted to learn CAD. Anyway right now I am pretty busy but when I have the chance over the next couple days I'll try to help you out with this. Right now Im on my phone. I'll be back for sure monday sometime. (out of town).

 

Welcome to the SCAR community.

 

As for your script. If you want it to work on multiple screens SIZE is very important. A picture of a button on one screen size could be totally different on another screen size. E.g. 800x600 vs 1600x1240

 

We really need to develop some advanced methods that utilize the power of color finding or make something that will search for a resized bitmap. I guess you could use FindDeformedBitmap

 

-LordJashin

Edited by LordJashin
Link to comment
Share on other sites

You can use relative positioning too though. That and different systems/configurations might render things differently. Even if only slightly, this can be a reason why the bitmaps or the script with the bitmaps doesn't work as intended anymore.

It would have been better for me to say. Just be aware that sometimes problems may arise with the bitmaps because of the possibility of change on different computers. Colors could be different or size even on THAT system. So if it fails this could be why, just a heads up.

Link to comment
Share on other sites

Okay, this looks a little complex, but with some thinking this can be done.

From looking at your video. You popup a window, and I'm guessing that window can popup where ever it wants. Is this a fullscreen application? I don't understand. But SCAR can do any keyboard shortcuts as long as it works on the application fine.

 

But if you wanted this to work on different resolutions, for your co-workers, who have any setup. They would all have to install SCAR, so what I would do, so you don't have to install it on every computer. Is get the portable one, install it on a flash drive. Then open up SCAR, paste the script in there. Then go to File->Save As Default

Then just copy the folder to every computer, and when they open SCAR the script should already be loaded.

 

Because the windows, and things can be in different positions, I would set the client as the desktop. Then look for the bitmap of certain windows that pop up. It would be better if you could just make it all based on keyboard shortcuts. Like if they hit CTRL+Delete you could use that to make it look for the window and start deleting stuff.

 

So what the scructure of the script could look like:

Just run it and Press F12 :D

[scar]

program AutoCad;

 

var

w, h: Integer;

bmpMultiple: TSCARBitmap;

 

procedure ScriptTerminate; // this EXACT named procedure always runs once the script terminates

begin

bmpMultiple.Free; // frees the bitmap we created

end;

 

procedure SetupEverything;

var

Desktop: TSCARWindowClient;

begin

ClearDebug;

Desktop := TSCARWindowClient.Create(GetDesktopWindow);

SetClient(Desktop); // Can't activate the desktop client ofc so no GetClient.Activate

bmpMultiple := TSCARBitmap.Create('deNrNVO0RwyAIZYGs0SEYiHmchmUcptWggp9nz' + 'l6v/IkhDx48MC+44P178w4BgLh6R+eHyKH/KSwCUDFSwIKxJJxl3mJ0jjKICYl' + 'wh/GoR+c5UcYnd5mVK1QDICL0TpmNFUHcjWRV5TexXzB2qsa0CRePgTMBTHzTt' + 'QKImTRkn7HaAWKtxJqhzCGyQVGXE0YLMxdgPOiCKQqXkHBcqZpgnarNh/lqma6' + 'zJFjNWpZEC0C0ymlCQc4u28EP5Kv5/pHxuX0AQRuCJA==');

// creates the bitmap

with GetClient.ImageArea do // has to be done with with statement

begin

w := X2; // sets w var to width of the image area of GetClient

h := Y2; // same thing but with h var

end;

MoveToTray; // Moves SCAR Divi window to the taskbar

end;

 

procedure MainLoop;

var

x, y: Integer;

acc: Extended;

begin

repeat

if GetKeyState(VK_F12) then

begin

MoveMouse(300, 300); // Just to check if pressing F12 works

if FindBitmap(x, y, bmpMultiple, 0, 0, w, h) then // Will check the whole screen for the bmpMultiple

ClickMouse(x + 5, y + 2, mbLeft); // whever it finds bmp multiple X, Y is the top left corner of it

// so we did x + 5, and y + 2 to try and click the center of the button (mbLeft) is mouse button left

MoveMouse(0, 0); // Its hard to find the bitmaps on the screen sometimes if the mouse is ontop of it

// Very important because if the script cant find the bitmap it could be beecause the mouse is over it

wait(1000); // Wait a second between checks, prevents lagg, and SCAR from going crazy

end;

 

until False; // this is an Infinite loop.... NOT RECOMMENDED

end;

 

 

begin

SetupEverything;

MainLoop;

end.

 

[/scar]

 

I think in the Windows API, there is a way to actually move windows around. Like you could make it so that every child window of that application is centered on the screen automatically. SCAR as far as I know can't do this yet?

I could be wrong. i thought there was a SetWindowPos, function?

Edited by LordJashin
Link to comment
Share on other sites

First of all let me say thank you for the detailed responses, I really appreciate the information especially it being my first post.

 

To clear some of your questions up right away AutoCAD is run in full screen at all times and 90% of commands can be typed so the QSELECT part of the procedure was easy enough. In my video I turned on my text at my crosshair just to show the command is typed. The other 10% input being selecting variables from drop down menus as shown in my video.

 

The menu that appears isn't in relation to anything it is just where the user decided to last put that menu. Putting SCAR on all the draftsmans computers isn't something I worry about as we only have 4 computers inclusive of my own that would be using this and I have plans to make multiple scripts so having them be able to just go open and choose the one they want isn't a big deal.

 

The detail I should have mentioned in my first post is all draftsman that work here use two monitors, both monitors are of the same resolution but vary at each workstation.

 

Colours I was thinking to not be the go because all the menus are that generic grey?

 

I have attached a screenshot showing AutoCAD's working layout on both my screens, anything that is a button in this program has a keyboard command to execute it so theres no need to worry about any of the toolbars. The large black space is my 'model space' which is where I draw and create everything and need my mouse to reside before I execute commands.

 

The green text in your script makes things 100x easier to understand, also after studying it I'm confident i understand everything you did and after putting in my bitmap everything ran as expected.

 

I think before i continue trying to make a script I need to sort out the best way to execute what I want, but what you have given me has helped me along so greatly thanks in advance for any replies.

 

P.S. my Skype is Maccadin if you want to contact me easier, for some reason the forum doesn't show me online even though my skype is on 24/7

 

*EDIT* I just took your script with the bitmap I defined to another machine and we didn't have any success, so i tried using it on my computer again with his resolution and it worked fine at different resolutions. We use the same windows 7 apperance and layout so the button looks exactly the same? This has me super confused.

 

http://img.photobucket.com/albums/v655/ADobeman/workspace.jpg

Edited by Maccadin
Link to comment
Share on other sites

Okay, yeah forgot to mention the Windows Version part. Like how all the styles are different. Didn't expect you to come and reply so fast :D, Nice

 

The last thing I can tell you to try for bitmaps is..replace the FindBitmap, with this and see if it does you any good:

 

FindDeformedBitmapToleranceIn(bmpMultiple, x, y, 0, 0, w, h, 15, 2, true, acc)

 

But you also need

 

var

acc: Extended;

 

 

About the FindBitmap:

 

Frankly, I've had so much trouble in the past with scripts. AND I MEAN BIG TIME. Janilabo did too, when me and him helped this guy out. We both probably changed the bitmaps a couple times. A script I made for this game, I tried it on different comps, and over teamviewer with people. It just never worked out. FindBitmapTol didn't help much either...

 

I just assume that, things are subject to change with all that. Unless you have two computers that are exactly the same in every aspect maybe? I just don't understand it sometimes, it drives me crazy

 

So what to use instead? There is some nice alternatives:

DTMS, color finding, maybe text finding, and some other really advanced stuff.

 

So how to use dtms. Go to Tools->Dtm Editor. Now load a picture in there, you can use the picture of that button the Multiple button or whatever. Now here is how DTMs work. You pick a Center point, and then every other point is outside points. When you use FindDTM. Well the DTM's Points, it also has the color at each point you picked from the picture. So FindDTM will check for the EXACT SPACING of Points (Colors), around the center point.

 

So if FindDTM returns false, one point could be wrong or off, or whatever.

I need to make a tutorial on this eventually, because it is very nice!

 

Test using your bmpMultiple:

 

[scar]

program DTMTest;

var

DTM, x, y: Integer;

begin

DTM := DTMFromString('78DA631464606060664001565613C0342394C' +

'F280D249850D5FCBF8DA646118B9AFFFF51D5C801097634BB18D2' +

'50D57001092E02E670639A83A1860548B0A2AAB9FD7F1BAA1A264' +

'C3763F89D0D4870E0F73B000D2613D0');

if FindDTM(DTM, x, y, 0, 0, 500, 500) then

MoveMouse(x,y);

FreeDTM(DTM);

end.

 

[/scar]

 

ALSO THE COLORS can have Tolerance, meaning similarity somewhat. E.g. Total black, and one shade lighter

 

DTM Editor:

 

hRlr7RrHMXdUWiL0-Region.png

 

Yeah for some reason Skype on here always shows me Offline too. I'll add you on Skype eventually, I go on all the time too, but I'm out of town atm.

 

Final Thoughts on DTMs, Bitmaps, etc.:

 

They can only be so accurate, timely, and usable. And frankly that FindDeformed bitmap, is the extent I've seen bitmaps go. DTMs have to be exact too, with their spacing. So screen res, and other factors COULD screw it up too. But less likely with only a couple points and good tolerance. How do we get around all of that though? FindDeformedDTM? nah no such thing. I think what we could do is, get ALL the colors out of bmpMultiple into an array. So it would be an array of colors. Then try to find that array of colors, with a tolerance.

 

So what we need is a way too. Change spacing, and etc just in case of the changing from computer to computer?

Edited by LordJashin
Link to comment
Share on other sites

Okay, yeah forgot to mention the Windows Version part. Like how all the styles are different. Didn't expect you to come and reply so fast :D, Nice

 

The last thing I can tell you to try for bitmaps is..replace the FindBitmap, with this and see if it does you any good:

 

FindDeformedBitmapToleranceIn(bmpMultiple, x, y, 0, 0, w, h, 15, 2, true, acc)

 

But you also need

 

var

acc: Extended;

 

 

About the FindBitmap:

 

Frankly, I've had so much trouble in the past with scripts. AND I MEAN BIG TIME. Janilabo did too, when me and him helped this guy out. We both probably changed the bitmaps a couple times. A script I made for this game, I tried it on different comps, and over teamviewer with people. It just never worked out. FindBitmapTol didn't help much either...

 

I just assume that, things are subject to change with all that. Unless you have two computers that are exactly the same in every aspect maybe? I just don't understand it sometimes, it drives me crazy

 

So what to use instead? There is some nice alternatives:

DTMS, color finding, maybe text finding, and some other really advanced stuff.

 

So how to use dtms. Go to Tools->Dtm Editor. Now load a picture in there, you can use the picture of that button the Multiple button or whatever. Now here is how DTMs work. You pick a Center point, and then every other point is outside points. When you use FindDTM. Well the DTM's Points, it also has the color at each point you picked from the picture. So FindDTM will check for the EXACT SPACING of Points (Colors), around the center point.

 

So if FindDTM returns false, one point could be wrong or off, or whatever.

I need to make a tutorial on this eventually, because it is very nice!

 

Test using your bmpMultiple:

 

[scar]

program DTMTest;

var

DTM, x, y: Integer;

begin

DTM := DTMFromString('78DA631464606060664001565613C0342394C' +

'F280D249850D5FCBF8DA646118B9AFFFF51D5C801097634BB18D2' +

'50D57001092E02E670639A83A1860548B0A2AAB9FD7F1BAA1A264' +

'C3763F89D0D4870E0F73B000D2613D0');

if FindDTM(DTM, x, y, 0, 0, 500, 500) then

MoveMouse(x,y);

FreeDTM(DTM);

end.

 

[/scar]

 

ALSO THE COLORS can have Tolerance, meaning similarity somewhat. E.g. Total black, and one shade lighter

 

DTM Editor:

 

hRlr7RrHMXdUWiL0-Region.png

 

Yeah for some reason Skype on here always shows me Offline too. I'll add you on Skype eventually, I go on all the time too, but I'm out of town atm.

 

Final Thoughts on DTMs, Bitmaps, etc.:

 

They can only be so accurate, timely, and usable. And frankly that FindDeformed bitmap, is the extent I've seen bitmaps go. DTMs have to be exact too, with their spacing. So screen res, and other factors COULD screw it up too. But less likely with only a couple points and good tolerance. How do we get around all of that though? FindDeformedDTM? nah no such thing. I think what we could do is, get ALL the colors out of bmpMultiple into an array. So it would be an array of colors. Then try to find that array of colors, with a tolerance.

 

So what we need is a way too. Change spacing, and etc just in case of the changing from computer to computer?

 

Great success with the DTM it worked on all computers at different resolutions I myself can't derive anything from this but by the looks of it im going to make DTM's of the few buttons i need it to find.

 

I've since made a huge amount of progression thanks to you. I have another question though, if I end up making heaps of DTM's can i define my variables in a seperate script to keep my actual script a bit cleaner?

Edited by Maccadin
Link to comment
Share on other sites

Yes you can. Its called using includes. Here's an example:

 

FileToBeIncluded:

[scar]

procedure HelloWorld;

begin

WriteLn('Hello World!');

end;

[/scar]

 

[scar]

program New;

{$I FileToBeIncluded.scar} // include a scar file that DOESNT have a begin end. block in it. Can't have program name; either...

{$L IrrlichtForSCAR.dll} // Load a DLL library

begin

HelloWorld;

end.

 

[/scar]

Edited by LordJashin
Link to comment
Share on other sites

Yes you can. Its called using includes. Here's an example:

 

FileToBeIncluded:

[scar]

procedure HelloWorld;

begin

WriteLn('Hello World!');

end;

[/scar]

 

[scar]

program New;

{$I FileToBeIncluded.scar} // include a scar file that DOESNT have a begin end. block in it. Can't have program name; either...

{$L IrrlichtForSCAR.dll} // Load a DLL library

begin

HelloWorld;

end.

 

[/scar]

 

Thanks again this worked great.

 

I have an update on my bitmaps though the reason it didn't work on the other computer is because i assume it doesn't run as fast as mine because of the specs, so i added a wait time of just 100ms before searching the bitmap and great success on all machines and resolutions.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...