Insert a block in AutoCAD with specific number of loops and wait for the user interaction first

Get help with using AutoHotkey and its commands and hotkeys
Coder
Posts: 29
Joined: 08 Feb 2018, 09:06

Insert a block in AutoCAD with specific number of loops and wait for the user interaction first

10 Feb 2018, 12:29

Hello everyone.

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.

Anyone can help me with those two problems and find a work around?

Thank you.

Code: Select all

^k::
num := 0
Loop 
{
if (num = 3)
	break
else
{
	Send, INSERT {ENTER}
	Sleep, 100
Send, MyBlock
Send, {TAB}									
Send, {TAB}
Send, {TAB}
Send, {TAB}
Send, {TAB}
Send, {TAB}
Send, {TAB}
Send, {TAB}
Send, {TAB}
Send, {TAB}
Send, {ENTER}
Sleep, 5000
num++ 
}
}
ExitApp
BoBo
Posts: 2515
Joined: 13 May 2014, 17:15

Re: Insert a block in AutoCAD with specific number of loops and wait for the user interaction first

12 Feb 2018, 01:46

[Insert a block in AutoCAD with specific number of loops and] wait for the user interaction first
I'd shrink the subject line to attract more supporters.

Code: Select all

^k::
Loop 3 {
	 Send, INSERT {ENTER}
     Sleep, 100
     Send, MyBlock
     Send, {TAB 10}
     Send, {ENTER}
     Sleep, 5000
     }
Return
reducing the number of redundant code (cosmetics)
BoBo
Posts: 2515
Joined: 13 May 2014, 17:15

Re: Insert a block in AutoCAD with specific number of loops and wait for the user interaction first

12 Feb 2018, 17:01

Coder wrote:Thank you BoBo, That is awesome.
TBH, I haven't changed anything that would have solved the issue you've mentioned. :lolno:
User avatar
divanebaba
Posts: 744
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Insert a block in AutoCAD with specific number of loops and wait for the user interaction first

12 Feb 2018, 21:01

Hello.
I've changed something but not the professional way. Very quick solution :mrgreen: :mrgreen:

Code: Select all

SetTitleMatchMode, 1
^k::
{
	ifwinexist, Autodesk AutoCAD 2018
	WinActivate
	else
	return
	ControlSetText,, INSERT, ahk_id 0x404D8 ; HWND found via Window Spy
	Send, {ENTER}
	WinWaitActive, Einfügen
	ControlSetText, Edit1, MyBlock, Einfügen ; Edit1 found via Window Spy
	sleep 500
	Control, Check,, Button13, Einfügen ; Button13 found via Window Spy
	; Control, Check,, Button13, Insert ; <--- change the window title and maybe numeber of the OK-button
}
return
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.
[Starklichtbirne IQ >> 250!!] Dank deutscher Lehrerempfehlung, nur Süperdepp und Hartz4-Bezieher, statt Abitürient und Einkommensbezieher.
[Strong light bulb IQ >> 250!!] Thanks to german teacher's recommendation, willful and welfare recipient, instead of high school graduate and moneymaker.

Entering high school for foreign children is not prohibited in GERMANY, it's only made nearly impossible by government.
Virtually the only way - visiting high school abroad.

ADVERTISING: Patent pending strip accumulator - süper advanced technology - priceless inexpensive, space-saving, energy efficient - Patent priority for sale.
FanaticGuru
Posts: 1225
Joined: 30 Sep 2013, 22:25

Re: Insert a block in AutoCAD with specific number of loops and wait for the user interaction first

13 Feb 2018, 00:30

If you want to delve into the magical world of COM, the code below will demonstrate how to create a block and insert it into the current drawing.

Code: Select all

; Create AutoCAD Application object of currently running AutoCAD program
acApp := ComObjActive("AutoCAD.Application")

; Create Default Point Structure
acPoint := acApp.ActiveDocument.ModelSpace.Origin

; Create Empty Block
acPoint[0] := 0, acPoint[1] := 0, acPoint[2] := 0 ; x,y,z
acBlock := acApp.ActiveDocument.Blocks.Add(acPoint, "TestBlock")

; Add Circle to Block
acPoint[0] := 0, acPoint[1] := 0, acPoint[2] := 0 ; x,y,z
Radius := 1
acCircle := acBlock.AddCircle(acPoint, Radius)

; Insert Block
acPoint[0] := 0, acPoint[1] := 0, acPoint[2] := 0 ; x,y,z
acBlockRef := acApp.ActiveDocument.ModelSpace.InsertBlock(acPoint, "TestBlock", 1, 1, 1, 0) ; Xscale, Yscale, Zscale, Rotation
Of course if you already have a block defined you can skip a couple of the steps in the example above.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
Coder
Posts: 29
Joined: 08 Feb 2018, 09:06

Re: Insert a block in AutoCAD with specific number of loops and wait for the user interaction first

13 Feb 2018, 00:40

Thank you for your help divanebaba.

Sorry the codes did not work for me although that I did change the AutoCAD version to 2015 to suit mine and translated the insert word in German to English ( INSERT ) but still run with no luck.

Can you please tell how can I get the HWND via Window Spy as per your kind comments in the script?

Thank you.
User avatar
divanebaba
Posts: 744
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Insert a block in AutoCAD with specific number of loops and wait for the user interaction first

13 Feb 2018, 00:59

Hi.

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.

Code: Select all

ControlSetText,, INSERT, ahk_id 0x404D8 ; HWND found via Window Spy
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. :D :D

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]

Code: Select all

SetTitleMatchMode, 1
^k::
{
	ifwinexist, Autodesk AutoCAD 2015
	WinActivate
	else
	return
	; ControlSetText,, INSERT, ahk_id 0x404D8 ; HWND found via Window Spy
	Sendinput insert
	Sendinput {ENTER}
	WinWaitActive, Insert ; <--- Maybe title is different then "Insert" - look for the name of the little AutoCad window
	ControlSetText, Edit1, MyBlock, Einfügen ; Edit1 found via Window Spy - It is the Edit field, where you normaly write the name of the block.
	sleep 500
	Control, 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.
[Starklichtbirne IQ >> 250!!] Dank deutscher Lehrerempfehlung, nur Süperdepp und Hartz4-Bezieher, statt Abitürient und Einkommensbezieher.
[Strong light bulb IQ >> 250!!] Thanks to german teacher's recommendation, willful and welfare recipient, instead of high school graduate and moneymaker.

Entering high school for foreign children is not prohibited in GERMANY, it's only made nearly impossible by government.
Virtually the only way - visiting high school abroad.

ADVERTISING: Patent pending strip accumulator - süper advanced technology - priceless inexpensive, space-saving, energy efficient - Patent priority for sale.
Coder
Posts: 29
Joined: 08 Feb 2018, 09:06

Re: Insert a block in AutoCAD with specific number of loops and wait for the user interaction first

13 Feb 2018, 01:20

FanaticGuru wrote:If you want to delve into the magical world of COM, the code below will demonstrate how to create a block and insert it into the current drawing.

Code: Select all

; Create AutoCAD Application object of currently running AutoCAD program
acApp := ComObjActive("AutoCAD.Application")

; Create Default Point Structure
acPoint := acApp.ActiveDocument.ModelSpace.Origin

; Create Empty Block
acPoint[0] := 0, acPoint[1] := 0, acPoint[2] := 0 ; x,y,z
acBlock := acApp.ActiveDocument.Blocks.Add(acPoint, "TestBlock")

; Add Circle to Block
acPoint[0] := 0, acPoint[1] := 0, acPoint[2] := 0 ; x,y,z
Radius := 1
acCircle := acBlock.AddCircle(acPoint, Radius)

; Insert Block
acPoint[0] := 0, acPoint[1] := 0, acPoint[2] := 0 ; x,y,z
acBlockRef := acApp.ActiveDocument.ModelSpace.InsertBlock(acPoint, "TestBlock", 1, 1, 1, 0) ; Xscale, Yscale, Zscale, Rotation
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.
Coder
Posts: 29
Joined: 08 Feb 2018, 09:06

Re: Insert a block in AutoCAD with specific number of loops and wait for the user interaction first

13 Feb 2018, 01:24

divanebaba wrote:Hi.

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.

Code: Select all

ControlSetText,, INSERT, ahk_id 0x404D8 ; HWND found via Window Spy
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. :D :D

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]

Code: Select all

SetTitleMatchMode, 1
^k::
{
	ifwinexist, Autodesk AutoCAD 2015
	WinActivate
	else
	return
	; ControlSetText,, INSERT, ahk_id 0x404D8 ; HWND found via Window Spy
	Sendinput insert
	Sendinput {ENTER}
	WinWaitActive, Insert ; <--- Maybe title is different then "Insert" - look for the name of the little AutoCad window
	ControlSetText, Edit1, MyBlock, Einfügen ; Edit1 found via Window Spy - It is the Edit field, where you normaly write the name of the block.
	sleep 500
	Control, 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 :bravo:

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

And thanks for the PM by the way.

Many many thanks.
Coder
Posts: 29
Joined: 08 Feb 2018, 09:06

Re: Insert a block in AutoCAD with specific number of loops and wait for the user interaction first

13 Feb 2018, 03:13

Wow very informative post divanebaba.

Have many things to discover and to ready about.

Many thanks again and have a great day.
FanaticGuru
Posts: 1225
Joined: 30 Sep 2013, 22:25

Re: Insert a block in AutoCAD with specific number of loops and wait for the user interaction first

13 Feb 2018, 04:54

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:

Code: Select all

acApp := ComObjActive("AutoCAD.Application")
acApp.ActiveDocument.SendCommand("_move`n") ; Start MOVE command
`n is the same as Enter key.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
User avatar
divanebaba
Posts: 744
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Insert a block in AutoCAD with specific number of loops and wait for the user interaction first

13 Feb 2018, 06:10

FanaticGuru wrote:...

Code: Select all

acApp := ComObjActive("AutoCAD.Application")
acApp.ActiveDocument.SendCommand("_move`n") ; Start MOVE command
...
Super stuff !! :D :D Looks high professional. :mrgreen: :mrgreen:
[Starklichtbirne IQ >> 250!!] Dank deutscher Lehrerempfehlung, nur Süperdepp und Hartz4-Bezieher, statt Abitürient und Einkommensbezieher.
[Strong light bulb IQ >> 250!!] Thanks to german teacher's recommendation, willful and welfare recipient, instead of high school graduate and moneymaker.

Entering high school for foreign children is not prohibited in GERMANY, it's only made nearly impossible by government.
Virtually the only way - visiting high school abroad.

ADVERTISING: Patent pending strip accumulator - süper advanced technology - priceless inexpensive, space-saving, energy efficient - Patent priority for sale.
Coder
Posts: 29
Joined: 08 Feb 2018, 09:06

Re: Insert a block in AutoCAD with specific number of loops and wait for the user interaction first

13 Feb 2018, 07:05

FanaticGuru wrote:
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:

Code: Select all

acApp := ComObjActive("AutoCAD.Application")
acApp.ActiveDocument.SendCommand("_move`n") ; Start MOVE command
`n is the same as Enter key.

FG
I am really sorry if my last reply to your post meant of any offence. I did not mean that at all. :oops:
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.

Have a great day.
User avatar
divanebaba
Posts: 744
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Insert a block in AutoCAD with specific number of loops and wait for the user interaction first

13 Feb 2018, 18:58

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.
[Starklichtbirne IQ >> 250!!] Dank deutscher Lehrerempfehlung, nur Süperdepp und Hartz4-Bezieher, statt Abitürient und Einkommensbezieher.
[Strong light bulb IQ >> 250!!] Thanks to german teacher's recommendation, willful and welfare recipient, instead of high school graduate and moneymaker.

Entering high school for foreign children is not prohibited in GERMANY, it's only made nearly impossible by government.
Virtually the only way - visiting high school abroad.

ADVERTISING: Patent pending strip accumulator - süper advanced technology - priceless inexpensive, space-saving, energy efficient - Patent priority for sale.
Coder
Posts: 29
Joined: 08 Feb 2018, 09:06

Re: Insert a block in AutoCAD with specific number of loops and wait for the user interaction first

15 Feb 2018, 06:17

divanebaba wrote:
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?

Code: Select all

pt := '(0. 0. 0.)
str := "Just a test"
      
(entmake (list '(0 . "TEXT") (cons 10 pt) (cons 11 pt) '(40 . 0.2) (cons 1 str)))
Thank you.
User avatar
divanebaba
Posts: 744
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Insert a block in AutoCAD with specific number of loops and wait for the user interaction first

15 Feb 2018, 06:59

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:

Code: Select all

acApp := ComObjActive("AutoCAD.Application")
acApp.ActiveDocument.SendCommand("_move`n") ; Start MOVE command
`n is the same as Enter key. ...
I can not translate your code into AHK, or something else. I can only show you, how to send your example lines into ACAD.

Code: Select all

acApp := ComObjActive("AutoCAD.Application") ; activate an existing AutoCad session
acApp.ActiveDocument.SendCommand("pt := '(0. 0. 0.)`n")
acApp.ActiveDocument.SendCommand("str := ""Just a test""`n")
acApp.ActiveDocument.SendCommand("(entmake (list '(0 . ""TEXT"") (cons 10 pt) (cons 11 pt) '(40 . 0.2) (cons 1 str)))`n")
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.
[Starklichtbirne IQ >> 250!!] Dank deutscher Lehrerempfehlung, nur Süperdepp und Hartz4-Bezieher, statt Abitürient und Einkommensbezieher.
[Strong light bulb IQ >> 250!!] Thanks to german teacher's recommendation, willful and welfare recipient, instead of high school graduate and moneymaker.

Entering high school for foreign children is not prohibited in GERMANY, it's only made nearly impossible by government.
Virtually the only way - visiting high school abroad.

ADVERTISING: Patent pending strip accumulator - süper advanced technology - priceless inexpensive, space-saving, energy efficient - Patent priority for sale.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], DRocks, Google [Bot], vvhitevvizard and 138 guests