Create a Dialog Box with an Image
By Mark Boucher
This is an excerpt from the Learn How to Work VB Script in CMM Manager Book. A link is provided at the bottom of the article.
To create a dialog with an attached image by itself is not possible within CMM Manager and we must use a HTML code. The part must be aligned with a fully constrained alignment.
ISSUE: We need to measure CYNDR1 first for form. If that is not correct there is no sense moving forward. We will display the Form value then let the operator decide what to do next.
This is the end result.
First, we must create a file folder on the C:// drive that will accept the new files and the image location.
· Create C://CMMDialogs file folder
Create the image and store it in the CMMDialogs file folder
· We named this jpeg “critical hole.jpg”
Secondly, Measure the CYLNDR1 and Report the Size and Form values.
Next, we must create a .HTA file. This will contain the creation of the formvalue file input and allow CMM Manager to call that file. It creates the operator selections, calls and places the image file, and creates a shell that is called by CMM Manager VBScript.
Copy this code completely and paste into Notepad.
<!DOCTYPE html>
<html>
<head>
<title>Form Check</title>
<hta:application
applicationname="FormDecision"
border="thin"
caption="yes"
maximizebutton="no"
minimizebutton="no"
sysmenu="yes"
scroll="no"
singleinstance="yes"
windowstate="normal"
/>
<script type="text/javascript">
function readFormValue() {
try {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.OpenTextFile("C:\\CMMDialogs\\formvalue.txt", 1);
var value = file.ReadLine();
file.Close();
document.getElementById("formValue").innerText = "Current Form Value is: " + value;
} catch (e) {
document.getElementById("formValue").innerText = "Unable to load form value.";
}
}
function submitChoice() {
var options = document.getElementsByName("decision");
var selected = null;
for (var i = 0; i < options.length; i++) {
if (options[i].checked) {
selected = options[i].value;
break;
}
}
if (selected == null) {
alert("Please select an option before continuing.");
return;
}
try {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.CreateTextFile("C:\\CMMDialogs\\selections.txt", true);
file.WriteLine(selected);
file.Close();
window.close();
} catch (e) {
alert("Error saving selection: " + e.message);
}
}
window.onload = readFormValue;
</script>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
img {
width: 300px;
height: auto;
margin-bottom: 15px;
}
.option {
margin: 10px 0;
}
button {
padding: 8px 16px;
font-size: 14px;
margin-top: 15px;
}
</style>
</head>
<body>
<h3>Operator Inspection Decision</h3>
<img src="C:\\CMMDialogs\\critical hole.jpg" alt="Critical Hole Image" />
<div id="formValue" style="margin-top: 10px; font-weight: bold;"></div>
<form>
<div class="option">
<input type="radio" name="decision" id="opt1" value="Unacceptable: End Program">
<label for="opt1">Unacceptable: End Program</label>
</div>
<div class="option">
<input type="radio" name="decision" id="opt2" value="Acceptable: Continue Program">
<label for="opt2">Acceptable: Continue Program</label>
</div>
<div class="option">
<input type="radio" name="decision" id="opt3" value="Unacceptable: Continue Program">
<label for="opt3">Unacceptable: Continue Program</label>
</div>
<button type="button" onclick="submitChoice()">Submit</button>
</form>
</body>
</html>
In the above file you will need to possibly change some items to fit your needs.
RED – Image file name and image text
Green – Operator selection options
Save the file in Notepad as ImageTextDialog.hta
· If you select another name for this file then you must change the reference in the VBSscript below. This is indicated by the bold line below""C:\CMMDialogs\ImageTextDialog.hta"""
Now, open the VBScript
Insert the VBScript below and insert after the measurement and reporting of the cylinder.
VBScript
' === Operator Form Check & Decision ===
Dim formValue, formattedValue, decision
Dim fso, file, shell
' Default: continue
decisionFlag = 0
' Get and format form of CYLNDR1
formValue = GetFeaturePropertyAct("CYLNDR1", 9)
formattedValue = FormatNumber(formValue, 5, -1, 0, -1)
Set fso = CreateObject("Scripting.FileSystemObject")
' Write form value for HTA
Set file = fso.CreateTextFile("C:\CMMDialogs\formvalue.txt", True)
file.WriteLine formattedValue
file.Close
' Run dialog
Set shell = CreateObject("WScript.Shell")
shell.Run "mshta.exe ""C:\CMMDialogs\ImageTextDialog.hta""", 1, True
' Read decision
If fso.FileExists("C:\CMMDialogs\selections.txt") Then
Set file = fso.OpenTextFile("C:\CMMDialogs\selections.txt", 1)
decision = Trim(file.ReadLine)
file.Close
Else
MsgBox "No decision recorded. Ending program for safety."
decisionFlag = 1
End If
If decision = "Unacceptable: End Program" Then
decisionFlag = 1
End If
' Clean up
If fso.FileExists("C:\CMMDialogs\selections.txt") Then fso.DeleteFile("C:\CMMDialogs\selections.txt")
If fso.FileExists("C:\CMMDialogs\formvalue.txt") Then fso.DeleteFile("C:\CMMDialogs\formvalue.txt")
' === Final Decision ===
If decisionFlag = 1 Then
MsgBox "Operator selected to end the program. Please press the Stop button to end Run Mode"
STOP
End If
Now finish programming the part as required.
If the operator chooses to end the program because the form is unacceptable then he will be prompted to push the Stop button on the Run Mode screen.
If the operator selects the other 2 options, the program will continue to run as normal.
This reference manual was developed to explain the VB Script dialog box. While VB scripting can be a extremely large topic to cover, this book does not intend to tackle that, we have targeted the CMM and many situations that you might run into while programming. The contents are one solution to each task.
The beginning of the book covers the VB scripting process and the syntax that will achieve the goals. Later on, are the examples of situations that you might come across as a CMM programmer.
Revision 2 adds more examples and detailed descriptions of the process of writing in VB Script.
This book is a digital download