Making a ruler and other measuring device. Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
iilabs
Posts: 123
Joined: 07 Jun 2020, 16:57

Making a ruler and other measuring device.

22 Jul 2020, 13:37

Hello,

I am trying to make a ruler for screen. My current ruler in the native app measures but I have to retype the measurements in a document. Is there a way to make my own ruler and calibrate it with the native ruler and use it to store variable sizes and past them from the clipboard?

Any guidance would be greatly appreciated.
colt
Posts: 176
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

Re: Making a ruler and other measuring device.  Topic is solved

22 Jul 2020, 17:00

Code: Select all

scaleFactor := .568 ;your calibration factor to convert pixels to units

MButton:: ;hold down middle mouse button to start measuring
	mouseGetPos,sx,sy ;start position to measure from
	setTimer updatePos,50
return

MButton Up:: ;release to capture measurement
	setTimer updatePos,off	
	tooltip distance measured is : %distCalibrated% units
	clipboard := distCalibrated
return

updatePos:
	mouseGetPos,x,y
	dx := x-sx
	dy := sy-y
	dist := round(  ((dx)**2 + (dy)**2) **.5  ,3)
	distCalibrated := round(dist * scaleFactor,3)
	tooltip [%dx%:%dy%]`n%dist% px`n%distCalibrated% units
return
User avatar
iilabs
Posts: 123
Joined: 07 Jun 2020, 16:57

Re: Making a ruler and other measuring device.

25 Jul 2020, 08:14

Thank you very much. I appreciate you actually giving me the entire code! I will certainly use this tool and adapt to others. I figure I can use the same concept for getting any area of pixel intensity and calibrate that as well. I will try it today. I have been so busy with work and Covid related here in Florida. This will really help with productivity and decrease errors. I will update this thread!
User avatar
iilabs
Posts: 123
Joined: 07 Jun 2020, 16:57

Re: Making a ruler and other measuring device.

31 Jul 2020, 13:41

This worked great. Will mess with the code. Thank you for the start!
User avatar
jackdunning
Posts: 120
Joined: 01 Aug 2016, 18:17
Facebook: https://www.facebook.com/ComputorEdgeEBooks/posts
Contact:

Re: Making a ruler and other measuring device.

01 Aug 2020, 17:36

I was intrigued by the problem so I decided to use colt's code to add calibration:

Code: Select all

; core code provided by colt: https://www.autohotkey.com/boards/viewtopic.php?f=76&t=78930&hilit=ruler
Pixels := 96
NumUnits := 1
Units := Inches

Gui, +ToolWindow
Gui, Add, Text, , Use Control+LeftMouseClick, Hold, and Drag`rfor Auto-Calibrate
Gui, Add, Edit, vPixels w75 Section, %Pixels%
Gui, Add, Text, ys , Pixel Range
Gui, Add, Edit, vNumUnits w75 xm section, 1
Gui, Add, Text, ys , Calibration range
Gui, Add, Edit, VUnits w75 xm section, Inches
Gui, Add, Text, ys , Calibration Units
Gui, Add, Button, gCalibrate ys ,Submit

scaleFactor := NumUnits/Pixels ; .568 ;your calibration factor to convert pixels to units

Help = 
(
To Calibrate Range:

	1. Cursor at start point — CTRL+LButton and hold.
	2. Drag to end of scale and release.
	3. Enter length of scale and units i.e. 10 miles.

Manual Calibration: CTRL+ALT+Z

To Measure Distance:

	1. Cursor at start point — SHIFT+LButton and hold.
	2. Drag to end of measurement and release.
	3. Distance displayed and saved to Clipboard.

CTRL+ALT+F12 for this Help Message.
)

HelpMe:
MsgBox, 1, MouseMeasure Help, % Help
Return

^!F12::GoSub, HelpMe


Ctrl & LButton::
Shift & LButton:: ;hold down shift and left mouse button to start measuring
	mouseGetPos,sx,sy ;start position to measure from
	setTimer updatePos,50
return


Shift & LButton Up:: ;release to capture measurement
	setTimer updatePos,off	
	tooltip distance measured is : %distCalibrated% %units%  
	clipboard := distCalibrated
	Sleep, 10000
	ToolTip
return

Ctrl & LButton Up::
	setTimer updatePos,off
	ToolTip
	GuiControl, , Pixels, %dist%

ManSet:
	Gui, Show, , Scale Calibration
Return

^!Z::GoSub, ManSet

updatePos:
	mouseGetPos,x,y
	dx := x-sx
	dy := sy-y
	dist := round(  ((dx)**2 + (dy)**2) **.5  ,3)
	distCalibrated := round(dist * scaleFactor,3)
	tooltip [%dx%:%dy%]`n%dist% px`n%distCalibrated% %units%
return

Calibrate:
 Gui, Submit
 scaleFactor := NumUnits/Pixels
Return

User avatar
iilabs
Posts: 123
Joined: 07 Jun 2020, 16:57

Re: Making a ruler and other measuring device.

02 Aug 2020, 04:54

Definitely going maniacal. Your books are the right step and awesome! Thanks for your time!
User avatar
iilabs
Posts: 123
Joined: 07 Jun 2020, 16:57

Re: Making a ruler and other measuring device.

03 Aug 2020, 12:21

Hi Jack,

This worked beautifully. I tried to modify the code so that I could get two positions and hopefully get the clipboard to output at the end. This measures 45 x 23 cm. Or this measures 44 x 33 x 22 cm. How would I go about adding more dimensions to make a final sentense. Two would be great but if I were to measure say in 3D, how would I go about it. I figured maybe add more functions for 2nd and 3rd measurements but got a weird response. Here is my code.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


; core code provided by colt: https://www.autohotkey.com/boards/viewtopic.php?f=76&t=78930&hilit=ruler
Pixels := 96
NumUnits := 1
Units := Inches

Gui, +ToolWindow
Gui, Add, Text, , Use Control+LeftMouseClick, Hold, and Drag`rfor Auto-Calibrate
Gui, Add, Edit, vPixels w75 Section, %Pixels%
Gui, Add, Text, ys , Pixel Range
Gui, Add, Edit, vNumUnits w75 xm section, 1
Gui, Add, Text, ys , Calibration range
Gui, Add, Edit, VUnits w75 xm section, Inches
Gui, Add, Text, ys , Calibration Units
Gui, Add, Button, gCalibrate ys ,Submit

scaleFactor := NumUnits/Pixels ; .568 ;your calibration factor to convert pixels to units

Help = 
(
To Calibrate Range:

	1. Cursor at start point — CTRL+LButton and hold.
	2. Drag to end of scale and release.
	3. Enter length of scale and units i.e. 10 miles.

Manual Calibration: CTRL+ALT+Z

To Measure Distance:

	1. Cursor at start point — SHIFT+LButton and hold.
	2. Drag to end of measurement and release.
	3. Distance displayed and saved to Clipboard.

CTRL+ALT+F12 for this Help Message.
)

HelpMe:
MsgBox, 1, MouseMeasure Help, % Help
Return

^!F12::GoSub, HelpMe


Ctrl & LButton::
Shift & LButton:: ;hold down shift and left mouse button to start measuring
	mouseGetPos,sx,sy ;start position to measure from
	setTimer updatePos,50
        mouseGetPos,sx2,sy2
        setTimer updatePos2,50
return


Shift & LButton Up:: ;release to capture measurement
	setTimer updatePos,off	
        setTimer updatePos2,off	
	tooltip distance measured is : %distCalibrated% %distCalibrated2% %units%  
	clipboard := distCalibrated
        clipboard := distCalibrated2
	Sleep, 10000
	ToolTip
return

Ctrl & LButton Up::
	setTimer updatePos,off
        setTimer updatePos2,off
	ToolTip
	GuiControl, , Pixels, %dist%

ManSet:
	Gui, Show, , Scale Calibration
Return

^!Z::GoSub, ManSet

updatePos:
	mouseGetPos,x,y
	dx := x-sx
	dy := sy-y
	dist := round(  ((dx)**2 + (dy)**2) **.5  ,3)
	distCalibrated := round(dist * scaleFactor,3)
	tooltip [%dx%:%dy%]`n%dist% px`n%distCalibrated% %units%
return

updatePos2:
	mouseGetPos,x2,y2
	dx := x2-sx2
	dy := sy2-y2
	dist2 := round(  ((dx2)**2 + (dy2)**2) **.5  ,3)
	distCalibrated2 := round(dist2 * scaleFactor,3)
	tooltip [%dx2%:%dy2%]`n%dist2% px`n%distCalibrated2% %units%
return


Calibrate:
 Gui, Submit
 scaleFactor := NumUnits/Pixels
Return
[Mod edit: [code][/code] tags added. Please use them. Thank you!]
Last edited by gregster on 03 Aug 2020, 13:05, edited 2 times in total.
User avatar
jackdunning
Posts: 120
Joined: 01 Aug 2016, 18:17
Facebook: https://www.facebook.com/ComputorEdgeEBooks/posts
Contact:

Re: Making a ruler and other measuring device.

03 Aug 2020, 18:34

iilabs wrote:
03 Aug 2020, 12:21
Hi Jack,

This worked beautifully. I tried to modify the code so that I could get two positions and hopefully get the clipboard to output at the end. This measures 45 x 23 cm. Or this measures 44 x 33 x 22 cm. How would I go about adding more dimensions to make a final sentense. Two would be great but if I were to measure say in 3D, how would I go about it. I figured maybe add more functions for 2nd and 3rd measurements but got a weird response. Here is my code.
Wow! You may be trying to add too much complication all at once.

First, I'm not sure what you want to do. If you want to save two measurements with different calibration, then you need to create separate variables and add them to the GUI. Next, when ever you save to the Clipboard, the new save replaces the previous save—unless you concatenate the new value to the Clipboard.

As for taking 3D measurements, the fundamental problem involves using a 2D monitor to capture 3D coordinates. Unless you have a special tool for a specific 3D program or a screen separate image showing the z view in 2D for separate measuring, the problem presents significant barriers. However, if you do have a method for capturing all three coordinates (x,y,z) then you can make the calculation—although a little more complex.

Without knowing the specific program (and having it available), I'd be shooting in the dark. You need a 3D practitioner for this one.

I've written a post describing the posted script "Capturing Computer Screen Measurements."
User avatar
iilabs
Posts: 123
Joined: 07 Jun 2020, 16:57

Re: Making a ruler and other measuring device.

04 Aug 2020, 09:37

Great point. I realize with programming there are many ways to skin a cat and sometimes picking the right option is the real challenge.

Let me clarify since the next step will really help me understand how variables are used in ahk and the clipboard. I’m sure there is a more simplistic approach.

If I explain the environment, I think it will settle some confusion. My work is mainly in medical imaging with numerous tasking using 4 monitors. Let say I need to capture at least two measurements of a lesion found on a CT scan. CT scans are performed in the axial plane and the sagittal and coronal planes are reformatted from the axial data set. So depending on the screen your on, you can technically get x,y, and z data from your ruler. Currently I use voice recognition but it doesn’t work that well or creates errors and tires you out. In the end you are re-typing some of the data, turning your neck for extended periods of time on the word processing end of reporting. The conventional way seems redundant as well. Measure get data and then redictate data back into essentially the same system. This is where I think AHK will come to the rescue and hopefully the steps to my mania with AHK. If I can measure length, save it in a variable until I get width and height and then it prints out a generic sentence to the clipboard such as "This measures x, y, z cms." I will be on the right path and save my neck literally.

I figure save all the measurements in a string array as you take them and resets the array after 3? or reset after a certain time frame to capture the measurements?
Send the string to the clipboard instead of dealing with individual data to the clipboard?
awel20
Posts: 211
Joined: 19 Mar 2018, 14:09

Re: Making a ruler and other measuring device.

04 Aug 2020, 10:32

That description sounds kind of similar to another existing script that I know about. I modified the hotkey from the exiting script so that it records the last 3 measurements and copies them to the clipboard when done. (You will also need to copy and paste the existing script I mentioned which can be found here https://www.autohotkey.com/boards/viewtopic.php?f=6&t=7225)

Code: Select all

#SingleInstance Force
#NoEnv

MsgBox, 64, Screen Scale Instructions,
(
Hold down the keys while measuring.

Ctrl + Alt + RightMouse
    Measure a known distance. Sets the scale factor for measuring subsequent distances.

Ctrl + Alt + LeftMouse
    Measure a distance or angle.

Ctrl + Win + RightMouse
    Clear the scale factor. Distance will not be calculated for subsequent measurements.

Ctrl + F11
    Reset the measurement count.

Ctrl + F12
    Toggle snap to horizontal/vertical when within +/-3 degrees.
)
Compass.ToggleSnap() ; Turn Snap on at startup (snaps to right angles when within 3 degrees)
MeasureCount := 1
return

^!LButton:: ; Ctrl+Alt+LButton
    Len := Compass.Measure(1) ; measure distance
    Len := Round(Len, 1) ; round number (optional)
    if (MeasureCount = 1) ; store 1st measurement
        Output := "This measures " Len
    else if (MeasureCount = 2) ; store 2nd measurement
        Output .= ", " Len
    else if (MeasureCount = 3) ; store 3rd measurement
    {
        Output .= ", " Len " " Compass.Units "."
        Clipboard := Output ; put text on the clipboard
        MeasureCount := 0 ; reset the measurement number count
        MsgBox % "Measurements were copied to the clipboard`n`n" Output
    }
    MeasureCount++
return
 
; Measure a known distance. Sets the scale factor for measuring subsequent distances.
^!RButton::Compass.SetScale(1, 1)       ; Ctrl+Alt+RButton
 
; Clear the scale factor. Distance will not be calculated for subsequent measurements.
^#RButton::Compass.SetScale(1, 2)       ; Ctrl+Win+RButton
 
 ; Reset the measure count
 ^F11::
    MeasureCount := 1                   ; Ctrl+F11
 return
 
; Toggle snap to horizontal/vertical when within +/-3 degrees.
^F12::
    Compass.ToggleSnap()                ; Ctrl+F12
return

 ; Copy and paste the Compass class here
 ; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=7225
Last edited by awel20 on 04 Aug 2020, 10:41, edited 1 time in total.
User avatar
jackdunning
Posts: 120
Joined: 01 Aug 2016, 18:17
Facebook: https://www.facebook.com/ComputorEdgeEBooks/posts
Contact:

Re: Making a ruler and other measuring device.

04 Aug 2020, 10:41

I suspect that learning how to use Arrays is the secret to your solution. You can then save an unlimited number of data points, make the needed calculations, and use ControlSend (or one of the similar commands) to insert the calculated text statements directly into a document. That will allow you the drop the Clipboard and its limitations.

While I haven't written extensively about them, you can find a mini-tutorial on arrays at "Using Associative Arrays to Solve the Instant Hotkey Data Recall Problem" plus other examples by checking the indexes in my books.

I recently wrote a blog about sending data to an alternative window which includes various options and references to more examples in my books ("Channeling Text to a Tagged Window").

I think these AutoHotkey tools will give you what to need to get the job done.
User avatar
iilabs
Posts: 123
Joined: 07 Jun 2020, 16:57

Re: Making a ruler and other measuring device.

04 Aug 2020, 11:23

Great! This code will become my main focus and will use as a framework. Just with the recent modifications has been a great way to learn. I wish I had more time!

Dealing with the Covid flux in Florida has sucked up a lot of my free time. I had some vacation time but had to work due to the need. Who wants to take vacation during these times anyways.

Are you sure I can’t hire your brain and your experience? How about as a tutor? I have a list of challenges to make my work as smooth as possible and with less errors to report. I think these challenges will certainly interest your brain! Thank you tremendously with this. I wish I discovered AHK 10 years earlier.
User avatar
iilabs
Posts: 123
Joined: 07 Jun 2020, 16:57

Re: Making a ruler and other measuring device.

05 Aug 2020, 12:03

That compass will be a great add on!

Unfortunately, I am not getting the compass to draw those lines. It seems to get the measurements. I thought maybe related to the GDIP files? I've created libary folder in my documents and also in the program file where AHK resides. I am using both Gdip_All,ahk and Gdip.ahk but still no go. Do you have better copies of Gdip or where to best place them? Right now all in a desktop folder AHK.

Sorry for all the Noob errors. Ill overcome this steep curve.

Thank you!

Return to “Ask For Help”

Who is online

Users browsing this forum: Billsaw, Bing [Bot], DoneQ, FrancoisM, Google [Bot], mikeyww and 62 guests