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.
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.
2. Click Add Formula.
3. Go to Measures then select distance (Body, Body); Length
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):
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 |
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.
Please notice these options need to be checked for automation of some measures:
Tools – Options – General – Parameters and Measure – Knowledge Environment – Language
Load extended language libraries
All packages
Thanks Jason!
Is there any way to use the macro to visualize the measurements instead of only showing it in the tree as explained above? My project really requires me to visualize it with macro as how the ‘Measure Between’ manual measurement works.
You could display it in a message box.
Hello i need to use this method to get curve lenght, curvature and distance between two points and i’ve done that exactly as you did but when i try to do it inside a for loop it doesnt work cause in the formula definition we have for example Set1.Point.25 and when i try to make i=25 and Set1\Point. & i it doesnt work any help??
How would you rename the “measurebetween”?
Sorry Greg, I don’t understand what you’re asking, what do you mean?
How would you write a code to change the name of the measure name? In this case the measure name is “MeasureBetween.1” and “MeasureBetween.2” and I want them to say “Distance1” and “Distance2”.
How would you write a code to change the name of the measure name? In this case the measure name is “MeasureBetween.1” and “MeasureBetween.2” and I want them to say “Distance1” and “Distance2”.
HI.
I am an engineer who is designing an airplane.
I have some inquiries about the above macros and leave a comment.
– For PARAMETERS AND RELATIONS METHOD
Generating points using macros rather than generated points Is there a parameter and relations method that measures the distance of the points created?
No matter how I try, it does not work.
The macro used is length (curve, point, point)
Dim length1 As Dimension
Set length1 = parameters1.CreateDimension (“”, “LENGTH”, 0)
Dim formula1 As Formula
Set formula1 = relations1.CreateFormula (“Formula.2”, “”, length1, “lengthg (` macro generation curve`, `macro generation point 1`,` macro generation point 2`)
– For SPAWORKBENCH METHOD
In ‘3D EXPERIENCE’, I would be grateful if you could tell me how to apply it.
The purpose of the above purpose is to create a point on the curve by dividing it by the distance between the ends of both curves in one curve for use in creating a ‘Fastner location’.
I would appreciate it if you could give me an example.
Hey,
Thank you for your code, it is realy helpful. But I did a loop which create a lot of lines, I would like to measure the distance between two lines ( n and n+1).
I would appreciate it if you could give me an example.
Hey,
Thank you for your code, it is really helpful. But I did a loop which create a lot of lines, I would like to measure the distance between two lines ( n and n+1).
I would appreciate it if you could give me an example.
Can you not use this example and change from points to lines?
Hi,
Thank you for the example codes and detailed explanations of how they work. I would like to measure the distance btw two selected points, but I am not able to make it work. Could you please help me find what I am doing wrong? Below is my codes.
Option Explicit
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 reference2 As Reference
Dim hybridBody1 As HybridBody
Dim hybridShapes1 As HybridShapes
Set hybridBody1 = hybridBodies1.Item(1)
Set hybridShapes1 = hybridBody1.HybridShapes
‘prompt a user to select a point
Dim sel
Set sel = CATIA.ActiveDocument.Selection
Dim rPoint(0)
rPoint(0) = “Point”
Dim oStatus
Dim SelectedElement1
Dim SelectedElement2
MsgBox “Select the first Reference Point”
oStatus = sel.SelectElement2(rPoint, “Select a Reference Point”, True)
Set SelectedElement1 = sel.Item(1)
sel.Clear
MsgBox “Select the second Reference Point”
oStatus = sel.SelectElement2(rPoint, “Select a Reference Point”, True)
Set SelectedElement2 = sel.Item(1)
MsgBox SelectedElement2.Type
Set reference1 = SelectedElement1 ‘hybridShapes1.Item(“MyEndPt1”) <———– Error here
'if code not working properly use msgbox to check reference name
'MsgBox ("ref1=" & reference1.Name)
Set reference2 = SelectedElement2 '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
What was wrong?
Hi,
I figured out what was wrong with my code! Thanks!
Aki
What was your error??????
yes what was it please.
Hi Jason,
Hope you’re doing well! I would like to know more about the Distance and Band Analysis command. Once we get output under applications, is it possible to control the definition of it? How can we export the Band Analysis as .model using VBA?
Any leads will be highly praised.
Regards
Rahul
Hi I want to find out the length of electrical protection in CATIA. Your method seems a nice workarround for this. First I need to changed the formula to find out the distance on a curve using 2 points. I wrote the formula manualy but as vba I having a hard time making it work:
Set formula1 = relations1.CreateFormula(“Formula.2”, “”, length1, “lenght(reference1 , reference2, reference3) “)
And the other thing I want to ask you is if you worked with electrical parts. The Electrical components usualy contain parts and I’m having also a hard time to access the parameters in them.
Hi,
Thank you for providing such a great content.
I want to know whether we can activate and deactivate measures through macros like any other object because in my macro I am using set of measures which will get failed for particular values of for loop and will work for other values of for loop and incase of failed measures I don’t have use of those macros for that particular run. Due to above stated problem macro is failing to work. So, Can I know is there any other way get away from this problem?
Do you know how to write macro for exporting line endpoint X Y point into excel sheet with Line name . I have multiple lines (imported file with out history) with specific name . I need to export these line parameter value of X1 y1 andX2 y2 of the line into excel sheet
Great content, thanks, starting to understand parameters a little now. But still struggling to output point coordinates to a spreadsheet – MeasurePoint.GetCoordinates (coords) records all points as coincident (they are not), while MeasurePoint at least displays true values – but how to get them out to Excel or csv? Any pointers (or a nice subroutine 🙂 ) grateful for any help? Many thanks
Hello, interesting solution. I’d like to modify it to work between two any objects (products, parts, frames etc.) and also to measure angles between them to get results similar to Compass Manipulation. Is it possible except using Position.GetComponents which is more troublesome?