Skip to content

Writing VBA macros in Alphacam: where to start

Alphacam VBA macro guide: the API tab and editor, the App/Drawing/Path/Layer objects, a first macro (dimensioned rectangle), layer-name discipline, compiling to .arb and distributing. With pitfalls met in production.

Published: · 9 min read

The VBA inside Alphacam lets a shop write tools for its own work. It is familiar to anyone who has written an Excel macro and accessible enough that a beginner gets a first result in a few evenings. This article answers where to start and where you get stuck, from what we learned building GMacros Toolss. No programming background is assumed.

Reaching the VBA editor

The ribbon has an API tab (older releases: an API menu). From there VBA opens the Visual Basic Editor — identical to the one in Office: project tree on the left, code in the middle. Add a Module, write a Sub in it; F5 or the run button runs the macro on Alphacam's open drawing. The same tab has the Add-In Manager, which loads and unloads compiled .arb files.

Object model: App, Drawing, Path, Layer

The Alphacam API is object based. Four objects cover most of the work:

The full list is in the API documentation shipped with Alphacam (Help menu or the documentation in the installation folder). Every member has parameters and an example; it is worth reading as a guide.

First macro: a button that draws a rectangle from typed dimensions

The most useful first macro takes width and height from the keyboard and draws a rectangle on the KESIM layer — half of all cabinet carcass parts are exactly that. The flow:

  1. Read width and height with InputBox; convert to numbers (mind the decimal separator — Turkish Windows expects a comma in CDbl).
  2. Take App.ActiveDrawing; if there is no drawing, warn and exit.
  3. Find the KESIM layer, create it if missing, make it the current layer.
  4. Draw with Drawing.CreateRectangle x1, y1, x2, y2.
  5. Refresh with Drawing.Redraw.

These five steps are the core of the Dimensioned Rectangle Part tool in GMacros Cabinet; the extra work there is validation, multiple parts and labels. Once you combine such a macro with Auto Style, the "type dimensions → get NC" flow is complete.

Layer-name discipline: the macro's real value

The first trap every macro author falls into is drawing on the current layer. Geometry lands on whatever layer the operator happened to be on; Auto Style assigns the wrong operation or none. Rule: for every geometry it draws, the macro selects the layer by name (KESIM, DELIK, CEP, KANAL, ETIKET) and restores the operator's layer when done. Keep the layer names in one constant list; the shop's Auto Style rules use the same list.

Thinking parametrically: dimension, allowance, quantity

A good production macro does not say "draw this door"; it says "take width, height, frame allowance and number of inner offsets, and derive the door from them". The same macro then draws 300×700 and 600×1200. Taking parameters through a form (UserForm) is more comfortable: each field has a default, the last value is remembered, and a dimension list (width×height×quantity) is entered in one go. How parametric door macros are structured is detailed in How Alphacam-based parametric door and cabinet door macros work.

Compiling and distributing: .arb

When the macros are ready, the project is saved/compiled as .arb and placed in Startup; Alphacam loads it at the next start. Ribbon buttons and the tab name are defined in the project. When distributing to other computers, watch three things: Alphacam version compatibility (compiled for an older release runs on a newer one, not necessarily the reverse), 32/64-bit (recent releases are 64-bit) and Windows regional settings (decimal separator). How add-ins load is described in What is an Alphacam add-in, how is it installed and how does it work?.

Pitfalls we met in production

Write it or buy it?

If the shop's job is single and specific (one part type), a macro written in an evening is enough. If the job covers doors, interior doors and carcasses in full; membrane, framed, glazed, louvred types; handle, hinge and push-to-open holes — writing and testing all of that on every Alphacam release takes months. GMacros packages exist for exactly that: Cabinet Door, Door and Cabinet macros plus GMacros Tools. For those who keep writing their own, the layer and parameter discipline in this article applies unchanged.

Frequently asked

Which language is used for Alphacam VBA?

Visual Basic for Applications (VBA). Python or C# are not supported directly; external automation over COM is possible, but VBA is the standard for production macros.

Will my macros break when Alphacam updates?

Usually not; the API is backward compatible. Moving from 32-bit to 64-bit requires updating Windows API declarations (Declare); pure Alphacam API code is unaffected.

Where is the Alphacam API documentation?

In the API help file installed with Alphacam and on Hexagon's customer portal. Object model, members and examples are there.

Related products

Tools that do the work described here with one click inside Alphacam: the GMacros Alphacam Package. Purchasing and licensing are explained in the FAQ.

Written by the GMacros team. Alphacam is a registered trademark of Hexagon AB; this guide is independent.

GMacros add-ins and macros run inside Alphacam Router. An Alphacam software license is not included in the product price; you buy Alphacam separately from Hexagon or an authorised reseller. Supported versions: 2019 R1 and later.

WhatsApp