PolyWorks Macro Tutorial

Published by CMMXYZ on May 11, 2021

PolyWorks Tech Tip: In this PolyWorks tutorial you’ll learn how to write a macro scrip that creates an interactive dialog box to gather project information. The script will then transfer that information to the inspection report.

Click Here to learn more about our affordable Software Support Packages - the same group that makes these Tech Tips!

In this video, I'll show you how to create a user prompt that will gather information, which will populate the cover page of your PolyWorks inspection report. Let me demonstrate, and I'll show you what I mean. I've added a custom icon to this toolbar that contains a Macro Script that I wrote ahead of time to create this dialog box. Let's just press it. It's this letter R here. You'll see the dialog box pop up. This is information that will be plugged into the bottom section of that cover page of my inspection report.

What's going on here is I have a few different types of items inside my dialog box. I have text boxes. Some of the text boxes have suggested text. Others are blank. Then I have drop-down lists for different options. Part name, I'll just type that in and Part number. Then for Device, I have the choice between ROMER arm and CMM, so whichever applies. Let's hit OK, and you'll see that populate on the bottom of my cover page. There we go. That worked.

I'll show you how it's done. The first thing we need to do is add four new items to our Project Properties. To do that, we open Project Properties by right-clicking the project name and select Project Properties. On my list, the four last items are the ones that I added as custom properties. The top items are known as standard properties, so they come included with the software. To add a new item, you just press the + button. Give it a name. It's as simple as that.

Now that I've added those new items, I need to tie that to my report. I need to link that or reference these new items. Let's go ahead and close that. If I go to my other report, report 2, this is one that has not been edited. The reason I had to add those four items was because, originally, they're referencing Piece properties and not Project properties. If I double-click that, you can see the Operator field is actually linked to Piece Properties. What I've done is I've linked it to Project Properties.

I'm going to delete that, that reference. Now, I'm just going to right-click. Go to Insert and you can see that here down at the bottom I have access to all of the variables or references inside each of the property types. What I want to do is go to Project Properties and you can see at the bottom, those are the four that I've added. I would need now to tie that to Operator and you can see the code there tying it to a Custom Project Property, not a standard one. That's it for that step.

Next, let's take a look at the Macro. By right-clicking the top of any window, I have access to the Macro Script Editor. To load my Macro, there's a quick shortcut I can use. I can just right-click on my custom icon, select Edit Macro Script and that will load the script that that icon launches. If you're new to Macros, or even if you're experienced, you might just want to take a screen capture when the video is paused, and you can see all my code here. I won't go into every single line. I'll just talk about this one more in general terms.

It basically has three sections. The first section is where I declare variables. I give them names. I give them values in some cases. In this Macro, I had to create a variable for each of the items in the dialog box and also each item in the drop-down list where that is applicable. The middle section is where I create my dialog box. I give it a name. I list the contents of that dialog box in different types, so edit box, drop-down list, and so forth. Tie those to variables.

The last section of my Macro is where I add that user input information into the Project Properties. I think the best way to explain this to you is to actually by way of a tutorial just to create a brand new Macro with one item in the dialog box. Okay, so let's go ahead and do that. Okay, so in the Macro Script Editor, we're going to select File, New. This gives us a blank slate. Step one is to declare variables. We need a variable for our dialog box. The name of this will be dialog. I will just type the word DECLARE. I'll call this shift. The value of this variable shift will be whatever the operator inputs. We need to be able to have a container to contain that information.

Next variable, we need to define each item in the drop-down list. Because I'm asking for the operator shift, this will be Day, Afternoon, and Night. I'll first give it a name. Just call it d1, something like that. In quotations, I'll put the word "Day." That will be the first selection, and we'll just go through the list. That takes care of the dialog box, the item in the dialog box, and the items in the drop-down list. This last variable I'll explain later on in the Macro.

That's step one, declare variables. Step two, we have to create our dialog box. The way we do that is the word MACRO, hit the Tab key. Input dialog box. Whenever we see the word highlighted in a box like that, if we just hit the Tab key, it will accept that selection and advance to the next argument. In this case, we need the word DEFINE. This is the first step. When we create a dialog box, we need to tie it to the variable name that we declared, which in this case is dialog. I'll just hit the Tab key, comma.

The next argument is the name of the dialog box. This will just appear on the top. In this case, I'm going to actually use this as an instruction. You'll see that when I run this Macro. The next step is we have to deal with the actual item inside the Macro. Again, MACRO INPUT DIALOG BOX. In this case, the item is a drop-down list. We'll find that in the selection. Hit tab. Tie that to the variable, which is dialog. The next item is the text that will appear beside the drop-down list, in this case, Shift. That's just the wording.

Next, we will choose Off for the next argument because we don't want multiple selections. If we chose On, the user could pick more than one selection from the drop-down. We only want one selection to be chosen. This will be Off, I'll hit the Tab key, comma. Here, we will input our variables for each item in the drop-down list, day, afternoon, and night. I don't need to type the words Day, Afternoon, and Night. I just need to reference the variables. In this case, I need the squiggly brackets there, the curly brackets. Then from my variable list, I'll pick d1, hit Tab, and just pick them all. We'll have to close that out with curly brackets, and we're done. We've defined the dialog box. We've said what we want to put in it is a drop-down list with those items, Day, Afternoon, and Night.

Last step, we need to close it out with the word SHOW. Link it to the variable, again, dialog. Here we need to specify the label of the OK button. This doesn't really come into play for us. We will also label the Cancel button with the word Cancel. An example of when you might want to change this wording is if you use a yes, no. Then you could tie that to which of those buttons was clicked and if we did use a yes, no, then we could use the result, which in this case, is a variable I created that we actually don't need, but we have to put something there. The last part is I need to tie my variable from my list item to the name of the variable, which is shift. That's it.

All right, so let's go to the last step where I tie the operator input to add those items to Project Properties. The way this code works is that the word PROJECT, hit the Tab key, PROPERTIES, Tab. Here we have to use the word CUSTOM because notice how there's CUSTOM and STANDARD. A custom item is the one I added as a user. Here, the first argument is the name that I added to the Project Property. It's the word shift. Here I need to tie my variable. This will be my shift variable with a dollar sign. That's it.

Like I said, that's about as simple as it gets. Before I run this, I need to actually save it. Let's do that right now. I'll just call it "Script Demo 2." and that's good to go. Before I run this, I need to actually input the code to tie in my particular report this field to the project property. Right-click, Insert, Project Property, and we'll go down to the bottom and select shift. I had added that before. That should work. Fingers crossed, let's run this and see how that goes.

To run a Macro, we'll press this running man arrow, F5 on your keyboard. It looks good. Now you can see all of my options are there as I type them in. Let's switch that to Afternoon and press OK. You can see there that's updated. That is just a quick tutorial on how to make the simplest dialog box ever. There are so many options here. I could have presented this in numerous different ways. I just found one relatively straightforward way that works. If you use your imagination and the tools I showed you, you should be able to make your own Macros actually quite easily, and they can be a lot of fun.

The last thing I want to show you is how to put this script onto a toolbar icon so that we can just simply click and run it. To do that, let's go up here and right-click. Select Customize. What we want to do is go to the Macro Scripts tab, and let's import that from where we saved it. I call this script demo 2. Let's open that. I'll select it from the list, and I want to change the image. My other one had that letter R, so I'll just pick some other letter, like just anything, letter T.

Now that we've imported that and given it, like, an image for an icon, what we can do now is just simply drag it out and add it to a toolbar. You see the little + symbol. Now that's the letter T. That's it. Let's test that. I'll simply press my letter T and see if that runs. Looks good. I'll change that to Night just to make sure that's working. There we go. That's how you add your script to a toolbar icon and save it for later use. That's about it. I hope you found that useful. Thanks for joining me, and we'll see you next time.

Related Articles

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram