06 Creating Your Own Macros

Document created by David Kosakowski on May 1, 2018Last modified by David Kosakowski on May 7, 2018
Version 4Show Document
  • View in full screen mode

Creating a Macro by Copying and Modifying Another

It is easiest to create your first macro by copying and modifying a pair of existing .py and .xaml files.

  1. In the Solution Explorer > Macros > Trimble, select a pair of .py and .xaml files that have some of the controls you need in your new command.

  2. Right-click, press Ctrl+C, click the Trimble folder, and press Ctrl+V to paste the files at the same level.

  3. Right-click each copy and select Rename to give them a name that to reflect the macro functionality you plan to code.

  4. Double-click each of the copied files to open them in the views.

  5. Right-click one of the tabs and select New Vertical Tab Group.

  6. Click in the Designer view and strip out any controls you do not need.

  7. Add new controls by dragging them from the toolbox into the view or by copying-and-pasting them from other macros.

  8. With any control selected, edit its properties in the XAML code view.

  9. Click in the Python file, and modify the data in the definition setup section:

    The key and command names call/identify the command; they cannot contain spaces or characters. The caption shows in the command’s titlebar; it can include spaces and characters.


    # define the command key and name
    def Setup(cmdData):
    cmdData.Key = "AssignNameFromInsideText"
    cmdData.CommandName = "AssignNameFromInsideText"
    cmdData.Caption = "_Assign Name From Inside Text"
    cmdData.UIForm = "AssignNameFromInsideText
  10. Based on your understanding of Python and the object model framework, edit the other functions. ???

  11. Save the macro and test it in TBC.

  12. Debug your code and make fixes as needed (see Debugging your Macro).

  13. Preview your changes in the edited macro by opening a project in TBC, pressing F12, and clicking the macro name near the top of the All Commands list.

Debugging Your First Macro

See the Microsoft help topic Navigating through Code with the Debugger.

  1. If you hover over a selected line in code, you can see some of the properties.

  2. it would be nice to walk them through simple steps in their own macro.

  3. Edit, save, and run the command again.

  4. Python has Attach; Xaml has Start

  5. Once program reads the file once, it won't compile it until it runs

Creating a Macro from Scratch

This requires training from Trimble. See the Disclaimers.

  1. Copy and paste a pair of .py and .xaml files in the Macros folder, and strip out the contents. OrPress Windows key + E to open the file explorer and navigate to C:\Users<your username>\Desktop\My Macros\Python\Macros\Trimble.

    a. Right-click in the explorer window and select New > Text Document.

    b. Click the new file and change the name to <name your macro>.py to it is recognized as a Python file. Write the macro in Python code.

  2. At the top of the .py file, define the purpose of the macro in case you share it with other users:


<Insert your macro description here.>
  1. Register the objects you want to interact with by importing .NET assemblies, our TBC assemblies and transaction methods. For example:

  2. # Reference the WPF Assemblies
      import clr
      clr.AddReference('IronPython.Wpf')
      import wpf
      from System.IO import StreamReader
      from System.Windows.Controls import StackPanel
      clr.AddReference("Trimble.Vce.Core")
      from Trimble.Vce.Core.Components import WorldView, Project
      from Trimble.Vce.Core import TransactMethodCall
      clr.AddReference("Trimble.Vce.Geometry")
      from Trimble.Vce.Geometry import Point3D, Side
      clr.AddReference("Trimble.Vce.Alignment")
      from Trimble.Vce.Alignment import *
      clr.AddReference("Trimble.Vce.Interfaces")
  1. Add setup function with key, command name, and caption (same as in C# code). For example:

  2. # define the command key and name
    def Setup(cmdData):
      cmdData.Key = "AssignNameFromInsideText"
      cmdData.CommandName = "AssignNameFromInsideText"
      cmdData.Caption = "_Assign Name From Inside Text"
      cmdData.UIForm = "AssignNameFromInsideText
  1. Add an execute method, if needed.

    • Add begin mark in Transition Manager.

    • Pause graphics.

    • Add tri-catch method.

    • Create dictionaries.Loop through, if an exception is caught, roll back:

    • Finally, redraw the graphics.

    • Save the file.

Next topic: 07 Using and Sharing Macros

Attachments

    Outcomes