Using Macros with VSTA

This task explains how to use macros in the VSTA environment.

This task shows you how to:

Accessing VSTA IDE from Tools > Macro... Menu

This task shows how to access the VSTA IDE tools.

  • Select the Tools > Macro > Visual Studio Tools for Applications... command to display the VSTA IDE user interface:



    If you have not created or opened a VSTA macro library before, the Macros GUI will prompt you to do so.

Creating a New VSTA Project

This tasks explains how to create a new VSTA project.

  1. Select the Tools > Macro > Macros... command to display the Macros dialog box:



  2. Click the Macro Libraries... button to display the Macro Libraries... dialog box:



  3. Select PLM VSTA projects as the Library type then click the Create new library... button:



    to display the New VSTA project dialog box:



  4. Select the Macro language.

    The macro language can be:

    • C#
    • VB.Net

    then click OK.

  5. Name the project in the following dialog box:



    then click the Finish button.

    A progress bar appears while the new project is created in the ENOVIA V6 database.

  6. In the the Macro Libraries... dialog box:



    click the Close button.

    Clicking the Close button loads the project into the VSTA IDE environment. This will take a few seconds.

Opening an Existing VSTA Project

This task explains how to open an existing VSTA project.

  1. Select the Tools > Macro > Macros... command to display the Macros dialog box:



  2. Click the Macro Libraries... button to display the Macro Libraries... dialog box:



  3. Select PLM VSTA projects as the Library type then click the Add existing library... button:



    to display the Macro Library Chooser dialog box:



  4. Enter * in the search field and click the Search button to display the projects saved in the database:



  5. Select the project:



    then click OK to display the Macro Libraries... dialog box:



    Note that the Remove selected library button deletes the macro library from the current session, but does NOT delete it from the database.

  6. Click the Close button to load the project into your current session:



Creating, Editing, Renaming and Deleting Macros in a VSTA Project

This task explains how to create, edit, rename and delete macros in a VSTA project.

  1. With the current macro library selected:



    click the Create... button to display the Create a new macro dialog box:



    Select the Macro language and the Macro name.

    The Macro name must begin with the string "Macro". Any attempt to assign a different name will produce an error.

  2. Click the OK button.

    The macro name is displayed in the Macros dialog box:



  3. To edit the macro, click the Edit... button to open the VSTA IDE editor:



    Check the Activate debug mode check button to run the editor debug mode allowing you, for example, to place breakpoints.

  4. To delete the macro, select the macro and click the Delete... button:



  5. To rename the macro, select the macro and click the Rename... button:



    Rename the macro and click OK.

    The Macro name must begin with the string "Macro". Any attempt to assign a different name will produce an error.

Solving Buildtime and Runtime Errors

This task explains how to solve buildtime and runtime errors.

The following tutorial will show you how to simulate and solve buildtime and runtime errors, that you are likely to face when developing VSTA macros.

  1. Navigate to Tools>Macros>Macros...

  2. Click Macro libraries.

  3. Choose PLM VSTA Project in the combo box. Click Create new library. Choose C#, and click OK.

  4. Select an appropriate name in the PLM New window. Click OK.

    The empty VSTA project is created.

  5. Create a new macro named "Macro1".

  6. Edit it as follows:

    public void Macro1(){System.Windows.Forms.MessageBox.Show(CATIA.FullName);}

  7. Go back to CATIA, re-open Tools>Macros>Macros..., the macro project should build in the background, and if you run Macro1, the message box should be displayed showing the full path to your installation of CATIA.

  8. Introduce a build-time error as follows (the semicolon is missing):

    public void Macro1(){System.Windows.Forms.MessageBox.Show(CATIA.FullName)}

  9. Go back to CATIA, re-open the Macros GUI, you get the following error:



  10. Edit the macro, remove the build-time error. Introduce a run-time error as follows (there is no editor currently opened in CATIA, hence we cannot get the active object's name):

    public void Macro1(){System.Windows.Forms.MessageBox.Show(CATIA.ActiveEditor.ActiveObject.get_Name());}

  11. If you go back to CATIA, and re-open the Macros GUI, Macro1 is displayed. Run it. You get the following error:



  12. Edit the macro, add a breakpoint (F9) on the faulty source line:



  13. Go back to CATIA, check the Activate debug mode check button, and run the macro. The VSTA IDE should stop on your breakpoint and display the source code:



  14. Check that you can watch some variables, as when debugging inside Visual Studio. Next press F8 to execute the faulty line. You will get a message box in CATIA (note that the error line is now displayed):



  15. You may now correct the error.