What's new

Tutorial - Creating a Hole Comparer Utility in WizoScript

ajayre

Alibre Super User
In this tutorial we are going to build a small utility that shows the diameters of two holes so they can be easily compared. Not very useful considering that this can easily be shown in Alibre Design, but demonstrates how to create a utility, and can be used as a starting point for something more complex.

Here is a screenshot of the finished utility:

Util-Dialog.png

The test part:

Util-TestPart.png

And the result of choosing the edges of the two holes and clicking on the 'Compare Holes' button:

Util-Result.png

This tutorial requires WizoScript version 4.00 or later.

The first step is to give our utility a name:

Code:
Name = 'Hole Comparer'

We need access to the 'Windows' module in order to create...windows!:

Code:
Win = Windows()

The next step is to define what is going to go into the user interface. This is in the form of a list of options. Each option is defined by:

- text to show
- type of input (in this case edges)
- a default value to use (not needed for this tutorial)

Here is the code:

Code:
Options = []
Options.append(['Hole Edge A', WindowsInputTypes.Edge, None])
Options.append(['Hole Edge B', WindowsInputTypes.Edge, None])

Pretty simple - we have two input fields for two edges, one for each hole.

Finally we show this to the user. We are using the 'UtilityDialog' because this stays open for as long as the user wants. They can repeat an action over and over and when they are done close the window:

Code:
Win.UtilityDialog(Name, 'Compare Holes', CompareHoles, None, Options, 400)

The first argument is the name of the utility.
The second argument is the text to put on the 'action button' The action button is the button that the user clicks on to perform some operation or action.
The third argument is a function that is called when the action button is clicked on. We will get to that in a minute.
The fourth argument is a function that is called when a value is changed by the user. We don't need it for this tutorial so we can set it to None.
The fifth argument is the list of options to show in the window.
The final argument specifies the width of the input area of the window in pixels.

When the user clicks on the action button a function is called, in this case the function is called 'CompareHoles'. we need to define that:

Code:
def CompareHoles(Values):
  HoleEdgeA = Values[0]
  HoleEdgeB = Values[1]
  Win.InfoDialog('Hole A diameter is %.2f mm, hole B diameter is %.2f mm' % (HoleEdgeA.Diameter, HoleEdgeB.Diameter), Name)

It is important to note that all lines inside a function have to be indented. The first two lines get the user's selections for edges. The first option in the window has the index value zero. The second option in the window has the index one. Values is a list of choices the user has made, one for every option in the window.

Once we have the edges we can show a message box to the user with the diameters of each hole.

There is one last step we need to do before we can run our script. The definition of the 'CompareHoles' function must be earlier in the script than the call to Win.UtilityDialog. So cut and paste it and move it to somewhere near the start.

If you want to get fancy you can add a check to make sure the user did choose two edges:

Code:
  if HoleEdgeA == None:
    Win.ErrorDialog('Choose first edge', Name)
    return

Same for the second input field.

That's it! Here is the final script:

Code:
# name of utility
Name = 'Hole Comparer'

# compare holes
def CompareHoles(Values):
  # get values
  HoleEdgeA = Values[0]
  if HoleEdgeA == None:
    Win.ErrorDialog('Choose a first hole edge', Name)
    return

  HoleEdgeB = Values[1]
  if HoleEdgeB == None:
    Win.ErrorDialog('Choose a second hole edge', Name)
    return
  
  Win.InfoDialog('Hole A diameter is %.2f mm, hole B diameter is %.2f mm' % (HoleEdgeA.Diameter, HoleEdgeB.Diameter), Name)

# get access to windows functionality
Win = Windows()

# create dialog window
Options = []
Options.append(['Hole Edge A', WindowsInputTypes.Edge, None])
Options.append(['Hole Edge B', WindowsInputTypes.Edge, None])

# show dialog window to user
Win.UtilityDialog(Name, 'Compare Holes', CompareHoles, None, Options, 400)

Andy
 
Last edited:

ajayre

Alibre Super User
Bonus addition:

The dialog can be made a bit more interesting by adding an image:

Util-Dialog2.png

Example code:

Code:
Options.append(['', WindowsInputTypes.Image, r'Util-img.png', 400])

Andy
 
Top