The “Measure Between” measurement tool in CATIA V5 is not exposed to VBA; the macro recorder will not record anything if you try. But there are a few work-arounds in order to measure distance between two points using a CATScript macro.measurement tool not exposed to CATIA VBA

Parameters and Relations Method


The first method is to create a parameter then add a formula to it. Before we create a macro, it’s a good idea to understand exactly how it will work. If you were to do this manually, you would follow these steps:

1. Create a new parameter of type length. Set default value to 0mm and rename it if you want.

catia parameter length macros

2. Click Add Formula.

3. Go to Measures then select distance (Body, Body); Length

catia formula editor

4. Select the two bodies (objects or pieces of geometry) that are to be measured. That’s it!

Now we will recreate this using the following CATScript:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'this macro creates a parameter and relation to measure the distance between two points 
Language="VBSCRIPT"
 
Sub CATMain()
 
'active document is a single part file 
Dim partDocument1 As Document 
Set partDocument1 = CATIA.ActiveDocument
 
Dim part1 As Part 
Set part1 = partDocument1.Part
 
Dim parameters1 As Parameters 
Set parameters1 = part1.Parameters
 
'create a new length type parameter, set its value to 0 for now 
Dim length1 As Dimension 
Set length1 = parameters1.CreateDimension("", "LENGTH", 0.000000)
 
'if you want to rename the parameter 
length1.Rename "MeasureDistance"
 
'create a new formula to link to the parameter 
Dim relations1 As Relations 
Set relations1 = part1.Relations
 
'make sure points are labeled MyEndPt1 and MyEndPt2 respectively 
Dim formula1 As Formula 
Set formula1 = relations1.CreateFormula("Formula.2", "", length1, "distance(`Geometrical Set.1\MyEndPt1` ,`Geometrical Set.1\MyEndPt2` ) ")
 
'rename the formula 
formula1.Rename "Distance"
 
'display the distance the endpoints are apart in a messagebox 
Msgbox "The endpoints are " & length1.ValueAsString & " apart."
 
End Sub

Your CATPart should look like this. Notice the parameter and relation that were created automatically (the measure between shown was created manually to double check the macro worked properly):

how to measure distance between two points catia

This code can be used to measure between more than just two points. For example, you can change it to measure a point to a plane by changing this line of code:

Set formula1 = relations1.CreateFormula("Formula.2", "", length1, "distance(`Geometrical Set.1\MyEndPt1` ,`Geometrical Set.1\MyEndPt2` ) ")

Into this:

Set formula1 = relations1.CreateFormula("Formula.2", "", length1, Distance(‘Geometrical Set.1\MyEndPt1’ , ‘Geometrical Set.1\Plane.1’)")

 

SPAWorkbench Method

An alternative method to measure the distance between two points with a CATIA macro is to use the SPAWorkbench properties and methods. This requires a license of DMU. Without the license, the calls will not work. The CATScript code is shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Sub CATMain() 
 
'active document must be a CATPart 
Dim documents1 As Documents 
Set documents1 = CATIA.Documents 
 
Dim pDocument1 As PartDocument 
Set pDocument1 = CATIA.ActiveDocument 
 
Dim part1 As Part 
Set part1 = pDocument1.Part 
 
Dim hybridBodies1 As HybridBodies 
Set hybridBodies1 = part1.HybridBodies 
 
Dim reference1 As Reference 
Dim hybridBody1 As HybridBody 
 
Set hybridBody1 = hybridBodies1.Item(1) 
Set hybridShapes1 = hybridBody1.HybridShapes 
Set reference1 = hybridShapes1.Item("MyEndPt1") 
 
'if code not working properly use msgbox to check reference name 
'MsgBox ("ref1=" & reference1.Name) 
 
  
Dim reference2 As Reference 
Set reference2 = hybridShapes1.Item("MyEndPt2") 
 
'built in check if needed 
'MsgBox ("ref2=" & reference2.Name) 
 
'get the SPAworkbench 
Dim TheSPAWorkbench As Workbench 
Set TheSPAWorkbench = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench") 
 
Dim TheMeasurable As Measurable 
Set TheMeasurable = TheSPAWorkbench.GetMeasurable(reference1) 
 
Dim MinimumDistance As Double 
MinimumDistance = TheMeasurable.GetMinimumDistance(reference2) 
 
'display the result 
MsgBox MinimumDistance 
 
End Sub

spaworkbench measure

P.S. The GetWorkbench command takes a string as an argument and returns a Workbench object. Each Workbench has an associated ID. To determine the ID of a workbench, open a workbench and the script that can be downloaded here. There’s also a list of workbench IDs in the latest edition of VB Scripting for CATIA V5 here.