I am new here and this is my first post in this forum and hope that I would get the help that I am looking for.

I am trying to insert a specific block in AutoCAD program with the use of AutoHotKey scripts and I successfully wrote a working script as posted below but the problem that I am facing is that I need to the script to wait for the user to specify the insertion point then repeat the process as per the loop number which is 3 in my example.

The user may take a few seconds before specifying a point but it seems is that the script does not wait for that time then it moves to second loop then to third then the user has only one repeat ( loop ) and not 3 as stated in the script.

Besides all of that, I would like to remove the Sleep, 100 to let the scripts invokes the command as soon as the user picked a point on the screen and not to wait for that 100 of seconds.

I'm typing sending "INSERT" directly to the ACAD-text-input-control.As ACAD is active, I send an Enter to ACAD, so that text-input-control takes "INSERT" as a command and open "insert-window".I send "MyBlock" directly into control numbered as "Edit1". Last step, triggering the OK button with reliable command.

This way, using "Control..." is a bit more professional then sending simulated keystrokes.

Step by step?I hope you know a bit to use Window Spy. It is located in AHK-Programfiles (C:\Program Files\AutoHotkey\WindowSpy.ahk, OR C:\Program Files (x86)\AutoHotkey\WindowSpy.ahk)Activate on right top side the option "Follow Mouse". When you now point with mouse cursor over any control, most time some useful information about is displayed.

To figure out the code above, try to locate the bottom line of the AutoCad text-input-window.A HWND should be displayed in Window Spy.When you type "Insert" and you press {Enter}, a little AutoCad window should be opened. Get the title of this window (in the top caption border or use Window Spy)Even for the edit control and for the buttons use Window Spy.

As I don't know your grade of knowledge, I hope this will help. Otherwise, please ask again.

BTW: The HWND of text input field was only shown by ACC-Viever. (Look for ACC.ahk)

EDIT: Sending keystrokes to AutoCad main window is captured by the text-input-field. That's great, because we don't need the name of the control.Your code could look like so: [UNTESTED FOR ACAD 2015]

SetTitleMatchMode,1^k::{ifwinexist, Autodesk AutoCAD 2015WinActivateelsereturn; ControlSetText,, INSERT, ahk_id 0x404D8 ; HWND found via Window SpySendinputinsertSendinput{ENTER}WinWaitActive,Insert; <--- Maybe title is different then "Insert" - look for the name of the little AutoCad windowControlSetText, Edit1, MyBlock, Einfügen ; Edit1 found via Window Spy - It is the Edit field, where you normaly write the name of the block.sleep500Control, Check,, Button13, Einfügen ; Button13 found via Window Spy - Identify the OK button with Window Spy; Control, Check,, Button13, Insert ; <--- change the window title and maybe number of the OK-button}return

EDIT: spelling corrections

Last edited by divanebaba on 13 Feb 2018, 01:38, edited 1 time in total.

Of course if you already have a block defined you can skip a couple of the steps in the example above.

FG

Thank you.

That's really wonderful to know that AHK can use the VBA although that I am not doing well at it but can understand the process and the way the program runs and I was not encouraged to learn it further since its not going to be supported by Autodesk with AutoCAD anymore as known for all.

I am good with AutoLISP, Vlisp, DCL and Active-X coding and I tried to use Lisp codes as well as you have did but it did not success with apostrophe ( ' ) since LISP code uses this a lot.

Step by step?I hope you know a bit to use Window Spy. It is located in AHK-Programfiles (C:\Program Files\AutoHotkey\WindowSpy.ahk, OR C:\Program Files (x86)\AutoHotkey\WindowSpy.ahk)Activate on right top side the option "Follow Mouse". When you now point with mouse cursor over any control, most time some useful information about is displayed.

To figure out the code above, try to locate the bottom line of the AutoCad text-input-window.A HWND should be displayed in Window Spy.When you type "Insert" and you press {Enter}, a little AutoCad window schould be opened. Get the title of this window (in the top caption border or use Window Spy)Even for the edit control and for the buttons use Window Spy.

As I don't know your grade of knowledge, I hope this will help. Otherwise, please ask again.

BTW: The HWND of text input field was only shown by ACC-Viever. (Look for ACC.ahk)

EDIT: Sending keystrokes to AutoCad main window is captured by the text-input-field. Thats great, because we don't need tne name of the control.Your code could look like so: [UNTESTED FOR ACAD 2015]

SetTitleMatchMode,1^k::{ifwinexist, Autodesk AutoCAD 2015WinActivateelsereturn; ControlSetText,, INSERT, ahk_id 0x404D8 ; HWND found via Window SpySendinputinsertSendinput{ENTER}WinWaitActive,Insert; <--- Maybe title is different then "Insert" - look for the name of the little AutoCad windowControlSetText, Edit1, MyBlock, Einfügen ; Edit1 found via Window Spy - It is the Edit field, where you normaly write the name of the block.sleep500Control, Check,, Button13, Einfügen ; Button13 found via Window Spy - Identify the OK button with Window Spy; Control, Check,, Button13, Insert ; <--- change the window title and maybe number of the OK-button}return

Hey, that is more than awesome. lol

I need time to read your precious reply very carefully and I will reply as soon as possible after breakfast of course.

Coder wrote:That's really wonderful to know that AHK can use the VBA although that I am not doing well at it but can understand the process and the way the program runs and I was not encouraged to learn it further since its not going to be supported by Autodesk with AutoCAD anymore as known for all.

I am good with AutoLISP, Vlisp, DCL and Active-X coding and I tried to use Lisp codes as well as you have did but it did not success with apostrophe ( ' ) since LISP code uses this a lot.

I don't know about not being supported in the future. I guess it depends if you mean 2 years in the future or 20 years in the future. They have been suggesting developers move to .NET since 2010 and still ActiveX (which allows VBA type code which allows AHK to use COM) is supported. Mainly because it is so easy for them to continue to support ActiveX and VBA.

In all likelyhood AutoCAD will support VBA type code as long as Windows continues to support it.

Also for the record AHK does not really use VBA to control AutoCAD through COM, it just uses the same COM system that ActiveX uses which makes the code look similar to VBA.

If you just want to keep it simple you can use COM to send commands just like you typed them in AutoCAD:

Coder wrote:That's really wonderful to know that AHK can use the VBA although that I am not doing well at it but can understand the process and the way the program runs and I was not encouraged to learn it further since its not going to be supported by Autodesk with AutoCAD anymore as known for all.

I am good with AutoLISP, Vlisp, DCL and Active-X coding and I tried to use Lisp codes as well as you have did but it did not success with apostrophe ( ' ) since LISP code uses this a lot.

I don't know about not being supported in the future. I guess it depends if you mean 2 years in the future or 20 years in the future. They have been suggesting developers move to .NET since 2010 and still ActiveX (which allows VBA type code which allows AHK to use COM) is supported. Mainly because it is so easy for them to continue to support ActiveX and VBA.

In all likelyhood AutoCAD will support VBA type code as long as Windows continues to support it.

Also for the record AHK does not really use VBA to control AutoCAD through COM, it just uses the same COM system that ActiveX uses which makes the code look similar to VBA.

If you just want to keep it simple you can use COM to send commands just like you typed them in AutoCAD:

I am really sorry if my last reply to your post meant of any offence. I did not mean that at all. Your suggestion with VBA is really superb and excellent and in other words its really magic and I do agree with what you have mentioned above about the usage of VBA with Windows.

Coder wrote:... but it did not success with apostrophe ( ' ) since LISP code uses this a lot.

You can change the Escapechar, but I think this is not recommended.Easier way is to escape the escapechar. Sounds funny, looks like below ``.Typing two apostrophe has following function. The first apostrohe escapes the second apostrophe.

You can even mask (escape) letters like a comma `, a semicolon `;, a percent sign `% and so on.Coding with LISP should be makeable.

Coder wrote:... but it did not success with apostrophe ( ' ) since LISP code uses this a lot.

You can change the Escapechar, but I think this is not recommended.Easier way is to escape the escapechar. Sounds funny, looks like below ``.Typing two apostrophe has following function. The first apostrohe escapes the second apostrophe.

You can even mask (escape) letters like a comma `, a semicolon `;, a percent sign `% and so on.Coding with LISP should be makeable.

Wow I hope so.Here is a simple function to create a text object with a string entitled "Just a test"Can you please show us a way of using this function with AHK please?

Twenty years ago, I learned using LISP for AutoCad. The scripts we made, were designed to directly type or import into ACAD-text-input-control.If the three lines in your example has to be inserted into the text-input-control, FanaticGuru, gave best suggestion.

FanaticGuru wrote:... If you just want to keep it simple you can use COM to send commands just like you typed them in AutoCAD:

As you can see, escaping " is done with another " and looks so "". Look into help-file how to escape special chars.Maybe this is not what you desired, but according to my knowledge, it is everthing german school system has offered me.