How To Model Gears With VBA User Form Tutorial v1.1
How To Model Gears With VBA User Form Tutorial v1.1
Alireza Reihani
Emmett Ross
1
Copyright Information
VB Scripting for CATIA V5: How to Model Gears with VBA User Form
All rights reserved. No part of this guide may be reproduced or transmitted in any form or by
any means, electronic or mechanical, including photocopying, recording, or by any information
storage and retrieval system, without permission in writing by the authors. The only exception
is by a reviewer, who may quote short excerpts in a review. CATIA is a registered trademark of
Dassault Systèmes. No affiliation with, or endorsed by anyone associated, or in any way
connected with Dassault Systèmes, Microsoft Corporation, UNIX, or any of their fantastic
products. We recognize that some words, model names and designations, for example,
mentioned herein are the property of the trademark owner. We use them for identification
purposes only. This is not an official publication.
Disclaimer
Although the authors have attempted to exhaustively research all sources to ensure the accuracy
and completeness of information on the subject matter, the authors assume no responsibility for
errors, inaccuracies, omissions, or any other inconsistencies herein. The data contained herein is
for informational purposes only and is not represented to be error free. Information may be
rendered inaccurate by changes made to the subject of the material, such as the applicable
software. No consequential damages can be sought against the authors for the use of these
materials by any third parties or for any direct or indirect result of that use. The purpose of this
text is to complement and supplement other texts and resources. You are urged to read all the
available literature, learn as much as you can and adapt the information to your particular needs.
There may be mistakes within this manual. Therefore, the text should only be used as a general
and introductory guide and not as the sole source for CATIA macro programming. The
information contained herein is intended to be of general interest to you and is provided “as is”,
and it does not address the circumstances of any particular individual or entity. Nothing herein
constitutes professional advice, nor does it constitute a comprehensive or complete statement of
the issues discussed thereto. Readers should also be aware that internet websites listed in this
work may have changed or disappeared between when this work was written and when it is read.
A prerequisite for this guide is to know the basics of CATIA, programming by Visual Basic, and
VBScript for CATIA. To start learning programming for CATIA V5 from scratch, please read VB
SCRIPTING FOR CATIA V5 by Emmett Ross.
2
Contents
Copyright Information .................................................................................................................... 2
Disclaimer ....................................................................................................................................... 2
Introduction ....................................................................................................................................... 5
Structure of this VB Program ......................................................................................................... 5
Initial Setting ...................................................................................................................................... 7
Step 1: Drawing Gear Types in CATIA ................................................................................................ 8
Step 2: Programming in CATIA step by step .................................................................................... 16
2-1) Getting Data and controlling them (sub: common) ............................................................. 16
2-1-1) Create Form in a new VBA project .................................................................................... 16
2-1-2) Get the Number of Gear Teeth and check its value .......................................................... 44
2-1-3) Get the Module of the Gear and check its value............................................................... 45
2-1-4) Get the thickness of the gear and check its value ............................................................. 46
2-1-5) Get the Diameter of the Shaft Hole and check its value ................................................... 47
2-1-6) Get the width and depth of the Key and check their values ............................................. 48
2-1-7) Get the Chamfer Data and check its value ........................................................................ 49
2-1-8) Get the Helix angle ............................................................................................................ 50
2-1-9) Get the Scale of Diameter and check its value .................................................................. 51
2-1-10) Calculate the five Radiuses necessary to draw Gear Tooth ............................................ 52
2-1-11) Get the gear pressure angle and calculate the other angles to draw ............................. 53
2-1-12) Control: shaft hole diameter must be smaller than Gear Dedendum ............................ 54
2-1-13) Check: Width of Key must be smaller than Shaft hole Diameter .................................... 55
2-1-14) Check: Corner point of Key must be Inside of Gear Dedendum ..................................... 56
2-1-15) Check: Chamfer................................................................................................................ 57
2-1-16) Find the center point of the pitch circle .......................................................................... 58
2-1-17) Find a point on pitch circle .............................................................................................. 59
2-1-18) Calculate the pitch circle radius ...................................................................................... 60
2-1-19) Find the intersection point on Addendum circle by Functions Xn,Yp ............................. 61
2-1-20) Control existence of the Addendum circle ...................................................................... 62
3
2-1-21) Find the center point of the fillet circle ........................................................................... 63
2-1-22) Find the intersection point of pitch circle and fillet circle .............................................. 64
2-1-23) Find the intersection point of the Dedendum circle and fillet circle .............................. 65
2-1-24) Find the last point on Dedendum circle .......................................................................... 66
2-1-25) Control the existence of the Dedendum circle ............................................................... 67
2-2) Create Body (Sub: common) .............................................................................................. 68
2-3) Create Plane for Tooth Profile Sketch .................................................................................. 69
2-4) Create Base Sketch (Tooth Profile Sketch) ........................................................................... 70
2-4-1) Find the Center points of circles and Start point and End point of Arcs ........................... 71
2-5) Create Full Teeth Profile (Circular Pattern) and Join them .................................................. 81
2-6) Type 1: Create Solid (Pad) ..................................................................................................... 85
2-7) Type 2: Create Solid (Multi-section solid) ............................................................................ 86
(Sub: Gear2).................................................................................................................................. 86
2-8) Type 3: Create Solid (Multi-section Solid) ............................................................................ 90
2-9) Type 4: Create Solid (Multi-section Solid) ............................................................................ 95
2-10) Create Hole and Key (Pocket) ........................................................................................... 103
2-11) Create Chamfer (Groove).................................................................................................. 107
2-12) Hiding main planes: .......................................................................................................... 114
2-13) Assign a Toolbar in CATIA ................................................................................................. 115
Thank You! ..................................................................................................................................... 118
Video Demo ................................................................................................................................... 119
Tips and Troubleshooting .............................................................................................................. 120
Appendix I: Keyboard Shortcuts .................................................................................................... 121
Appendix II: Resources................................................................................................................... 122
4
Introduction
A good practice to take your CATIA programming skills to the next level is to model a complex
example like a gear. In this guide, you’ll learn how to develop a CATVBA program to model
different types of gears in CATIA V5.
In this case, step one is as important as step two, because there are many ways to model a gear
but some of them are not good and are inefficient. This is especially evident in Helical Gears
where sometimes they create errors in CATIA or the file size and/or time of creation is very high.
Before starting these two steps, let’s briefly review the structure of this program.
5
Fig 1: Project explorer and Program window in design mode
The UserForm consists of seven subroutine and two functions. At first, Subroutine common
creates Full Gear Profile, and then, depending on the Gear Type, one of four subroutines that
draws the desired gear type:
Two functions Xn and Yp are used to calculate coordinates of intersection of two circles (to draw
Gear profile). Subroutines HoleKey and Chamfer draw Hole of Shaft and Chamfer.
6
Initial Setting
This program creates Hybrid design (Planes, Points,…) in Body and not in Geometrical Set, so it’s
necessary that you check Enable hybrid design when you want to create a new Part (Fig 3).
You can also change this setting by this method (Fig 4):
7
Step 1: Drawing Gear Types in CATIA
Before starting a Sketch of the Tooth Profile (manually or by programming), it’s necessary to have
equations for drawing a gear tooth profile.
Beta
Ru
Rf Ro
Rt
Rd
Alpha Gamma
8
Note 1: Normally to draw a tooth profile, you must draw circles and lines, then trim them. This
can be very difficult to do by programming so use intersection points of two circles by these
equations:
𝐿 ∗ (𝑋2 − 𝑋1 ) ℎ ∗ (𝑌2 − 𝑌1 )
𝑥= ± + 𝑋1
𝑑 𝑑
𝐿 ∗ (𝑌2 − 𝑌1 ) ℎ ∗ (𝑋2 − 𝑋1 )
𝑦= ∓ + 𝑌1
𝑑 𝑑
𝑑 = �(𝑋1 − 𝑋2 )2 + (𝑌1 − 𝑌2 )2
𝑅12 − 𝑅22 + 𝑑 2
𝐿=
2∗𝑑
ℎ = �𝑅12 − 𝐿2
where x,y are coordinates of intersection points, X1,Y1 are coordinates of center point of first
circle with radius=R1 and X2,Y2 are coordinates of center point of second circle with radius=R2.
The fillets will also be drawn in sketch rather than using the fillet tool because it will be easier to
do programmatically.
In this guide, you will draw the Gear Sketch in a plane parallel to XZ plane. As a practice, you can
develop this program and then draw a gear in each plane.
9
Fig 1-2: Tooth Profile drawn by finding intersection points
10
Note 2: Drawing Full Gear Profile
For drawing full teeth profile, it doesn’t appear there is a VB function to have Circular Pattern in
Fig 1-4: Full Teeth Profile and Create Pad for Gear Type 1
11
In type 2 (helical gear); after Translating of full teeth profile; it must be rotated (Fig. 1-5).
12
Fig 1-7: Gear Type 2
In type 3 (straight bevel); after Translating of full teeth profile; it must be scaled (Fig. 1-8).
13
Fig 1-9: Gear Type 3 (straight bevel)
In type 4 (spiral bevel); after Translating of full teeth profile; it must be rotated and be scaled. Do
it two times (Fig. 1-10).
Fig 1-10: Profile Translated, Rotated and Scaled in normal view (two times)
14
Fig 1-11: Gear Type 4 (spiral bevel)
We have just created four different types of gear. Now it’s time to add automation so a user can
change the size of gears through a user form.
15
Step 2: Programming in CATIA step by step
2-1) Getting Data and controlling them (sub: common)
Fig 2-1-1: Form to get data for creating the gears to the specified size
16
For creating this User Form, we need to create a new program file. In CATIA, click on the
Tools>Macro>Visual Basic Editor... or press Alt+F11 (Fig 2-1-2).
17
If there is not a loaded CATVBA file, one message appears (Fig 2-1-3).
Press Yes to create one VBA project. Now Macro libraries window appears (Fig 2-1-4)
18
Fig 2-1-4) Macro libraries window
Click on Create new library... button (Library type must be VBA Project Fig 2-1-5). Now
Create a new VBA project window appears (Fig. 2-1-5). Enter a path and name for your VBA
project. (This path must be valid, otherwise an error message appears and CATIA doesn’t help
you to create these Folders.)
19
Fig 2-1-5) Create a new VBA project window
By clicking on the Ok button you will come back to Macro libraries window and you will see
your new full path of new VBA project in this window (Fig 2-1-6).
20
Fig 2-1-6: new VBA project in Macro libraries window
By clicking on Close button you will enter into the VBA environment (Fig 2-1-7)
21
Fig 2-1-7) VBA environment
To create a Form, Right click on the Project window (left side in Fig 2-1-7) then click on
Insert>UserForm (Fig 2-1-8) or use menu Insert>UserForm (Fig 2-1-9). New Form appears
(Fig 2-1-10).
22
Fig 2-1-8: Add a new Form in your VBA project by right click
23
Fig 2-1-10: new Form
click on Properties window icon ( ) or right-click on the form and select properties (Fig 2-
1-11) . Now properties window of Form appears (Fig 2-1-12). Change the Name property of
form to FrmGear.
24
Fig 2-1-11: Access to properties of Form by right-click
25
To access CATIA when the Gear program is running, then set ShowModal property to False
(Fig 2-1-13). Forms are displayed by the Show method of the form object. This method has an
optional argument to specify whether the form should be displayed as either modal or
modeless. A modal form will not let you interact with any other part of the application until
that form is closed. This means that you cannot interactively use other CATIA commands or
interact with any other forms that might be displayed at the time. Forms are, by default, modal
unless otherwise specified. We want to interact with CATIA so set ShowModal property to
False.
Change the Caption property to 3D Gear (Fig 2-1-14). Finally, change the size of the Form (by
dragging of corners of Form or by setting width and Height property).
26
Fig 2-1-14: Caption property of Form
To classify the controls such as TextBoxes, Labels,... , insert a Frame (Fig 2-1-15) by dragging
its icon on Toolbox window (Fig 2-1-16) to Form or by clicking on its icon and click a point on
Form. (If you don’t see this window click on menu: View>Toolbox or click on its icon in
Standard Toolbars )
27
Fig 2-1-15: Add a Frame
28
Fig 2-1-17: Add Label
29
Change the Text Background Color by BackColor property of TextBox (Fig 2-1-19).
30
To show the user that TextBox is a place to enter text, change MousePointer (Fig 2-1-20) and
MouseIcon property of TextBox.
31
Change Text Color of TextBox by ForeColor property of TextBox (Fig 2-1-21).
32
Change Font of TextBox and its Height to better viewing of texts by Font property of TextBox
(Fig 2-1-22).
33
Change BackStyle property of Label to 0 (Transparency) (Fig 2-1-23) now its color is
dependent of Frame color.
34
To show pictures, put image control and set its Picture property. Pictures should be in format
.bmp, .gif or .jpg. (Fig 2-1-24)
35
Each type of gear can have a shaft hole or not. If a shaft hole exists, it can have a key or not.
The best control for this purpose is to use a CheckBox (Fig 2-1-25). Put two CheckBoxes and
change their Name property to ChHole and ChKey. In MouseUp event of ChHole, write the
following code to control the key. Remember: the key can only exist when the shaft hole
exists.
ChKey.Value = False
ChKey.Enabled = False
Else
ChKey.Enabled = True
End If
End Sub
36
In addition, for gear types 1 and 2, we will add a chamfer option.
ChChamfer.Enabled = True
End Sub
ChChamfer.Enabled = True
End Sub
37
Private Sub Op3_Click()
ChChamfer.Value = False
ChChamfer.Enabled = False
End Sub
ChChamfer.Value = False
ChChamfer.Enabled = False
End Sub
This program can draw four different types of gear but only one at a time can be select. Each
time one type is drawn and the user can select only one type so we need four OptionButtons.
You can add an image beside each optionButton to visually show the user what each type of
gear is.
38
We need a Command button to execute the create gear program. Add a new command button
and change the BackColor and ForeColor property (Black and white).
Continue adding controls and set their properties. After setting properties of a control, you can
use Copy-Paste to quickly create the other controls.
39
Fig 2-1-28 shows the Name property of controls that will be used in the VBA program code.
ChHole
TxtZ
CmbM TxtShaftDiae
TxtAlpha
TxtT
TxtY Op4
Op1
Op3
Op2
TxtChX TxtScale
TxtChY
ChChamfer
CmdDraw
TxtHelixAngle
40
To show the form of program, add a module to the project. A new module can be added just
like adding a form: right-click on the project window and select Insert>Module (Fig 2-1-29).
41
Fig. 2-1-30: New module
Now add this code to your newly created module. This code will show the form when the
program is run.
Sub CATMain()
FrmGear.Show
End Sub
Later on you’ll learn how you can assign this module to an icon in CATIA to show the Form.
42
When the program runs, after clicking on Draw command button, the variable ProfileError is set
to false, a subroutines HidePlanes (to be explained later) and common will be executed.
ProfileError = Falsend
Call HidePlanes
Call Common
End Sub
Subroutine common gets the information from the form, analyzes it, creates the body and a plane
parallel to XZ plane and finally draws a full gear profile. After assigning the input information to
variables, they must be controlled. For example, if the teeth number, module or thickness is zero,
the program must alert the user to change it. This is called error handling.
Sub Common()
Me.Hide
43
2-1-2) Get the Number of Gear Teeth and check its value
QtyZ = Val(TxtZ.Text)
Dim Z As Double
Z = CDbl(QtyZ)
FrmGear.Show
TxtZ.SetFocus
Exit Sub
End If
44
2-1-3) Get the Module of the Gear and check its value
m = Val(CmbM.Text)
If m = 0 Then
FrmGear.Show
CmbM.SetFocus
Exit Sub
End If
45
2-1-4) Get the thickness of the gear and check its value
t = Val(TxtT.Text)
If t = 0 Then
FrmGear.Show
TxtT.SetFocus
Exit Sub
End If
46
2-1-5) Get the Diameter of the Shaft Hole and check its value
SD = Val(TxtShaftDia.Text)
If SD = 0 Then
FrmGear.Show
TxtShaftDia.SetFocus
Exit Sub
End If
End If
47
2-1-6) Get the width and depth of the Key and check their values
KW = Val(TxtKeyWidth.Text)
If KW = 0 Then
FrmGear.Show
TxtKeyWidth.SetFocus
Exit Sub
End If
KD = Val(TxtKeyDepth.Text)
If KD = 0 Then
FrmGear.Show
TxtKeyDepth.SetFocus
Exit Sub
End If
End If
48
2-1-7) Get the Chamfer Data and check its value
ChX = Val(TxtChX.Text)
If ChX = 0 Then
FrmGear.Show
TxtChX.SetFocus
Exit Sub
End If
ChY = Val(TxtChY.Text)
If ChY = 0 Then
FrmGear.Show
TxtChY.SetFocus
Exit Sub
End If
End If
49
2-1-8) Get the Helix angle
HelixAngle = Val(TxtHelixAngle.Text)
50
2-1-9) Get the Scale of Diameter and check its value
Sc = Val(TxtScale.Text)
FrmGear.Show
TxtScale.SetFocus
Exit Sub
End If
51
2-1-10) Calculate the five Radiuses necessary to draw Gear Tooth
Dim Rt As Double
Dim Ro As Double
Ro = m * Z / 2#
Rt = 0.94 * Ro
Rd = Ro - 1.25 * m
Ru = Ro + m
Rf = 0.35 * m
If Rd <= 0 Then
FrmGear.Show
TxtZ.SetFocus
Exit Sub
End If
52
2-1-11) Get the gear pressure angle and calculate the other angles to draw
Alpha = Val(TxtAlpha.Text)
Beta = 90 / Z
TotalAngle = 360 / Z
53
2-1-12) Control: shaft hole diameter must be smaller than Gear Dedendum
If SD >= Rd * 2 Then
FrmGear.Show
TxtShaftDia.SetFocus
Exit Sub
End If
End If
54
2-1-13) Check: Width of Key must be smaller than Shaft hole Diameter
If KW >= SD Then
FrmGear.Show
TxtKeyWidth.SetFocus
Exit Sub
End If
55
2-1-14) Check: Corner point of Key must be Inside of Gear Dedendum
FrmGear.Show
TxtKeyDepth.SetFocus
Exit Sub
End If
End If
56
2-1-15) Check: Chamfer
Chamfer in Y (V) Direction must be smaller than Gear Addendum and in X (H) Direction must be
smaller than (or equal to) Half of Gear thickness
FrmGear.Show
TxtChY.SetFocus
Exit Sub
End If
FrmGear.Show
TxtChX.SetFocus
Exit Sub
End If
End If
57
2-1-16) Find the center point of the pitch circle
Dim X0 As Double
Dim Y0 As Double
Dim Z0 As Double
X0 = 0
Y0 = Val(TxtY0.Text)
Z0 = 0
Dim H0 As Double
Dim V0 As Double
H0 = 0
V0 = 0
XPtOc1 = Rt * Cos(GamaRad)
YPtOc1 = Rt * Sin(GamaRad)
58
2-1-17) Find a point on pitch circle
YPt1 = Ro * Cos(BetaRad)
59
2-1-18) Calculate the pitch circle radius
Dim R1 As Double
60
2-1-19) Find the intersection point on Addendum circle by Functions Xn,Yp
61
2-1-20) Control existence of the Addendum circle
XPt2 must be negative.
FrmGear.Show
Exit Sub
End If
62
2-1-21) Find the center point of the fillet circle
63
2-1-22) Find the intersection point of pitch circle and fillet circle
64
2-1-23) Find the intersection point of the Dedendum circle and fillet circle
FrmGear.Show
Exit Sub
End If
65
2-1-24) Find the last point on Dedendum circle
YPt5 = Rd * Cos(TetaRad)
66
2-1-25) Control the existence of the Dedendum circle
XPt5 must be smaller than XPt4.
FrmGear.Show
Exit Sub
End If
67
2-2) Create Body (Sub: common)
'---Starting of Base Wireframe---
'File: Gear1~4.CATPart/Body.2
68
2-3) Create Plane for Tooth Profile Sketch
Sub: Common
'Define ZX Plane
'File: Gear1~4.CATPart/Body.2/Plane.1
myBody.InsertHybridShape myPlaneY
myPart.Update
69
2-4) Create Base Sketch (Tooth Profile Sketch)
Sub: common.
This sketch (like the sketch of Chamfer and Shaft Hole) is not fully defined. As a practice, you can
add constraints to the objects.
70
2-4-1) Find the Center points of circles and Start point and End point of Arcs
Use the Intersection points of the circles.
71
Dim mySketches As sketches
myPart.InWorkObject = mySketch
'File: Gear1~4.CATPart/Body.2/Sketch.1/Point.1
'File: Gear1~4.CATPart/Body.2/Sketch.1/Point.2
'File: Gear1~4.CATPart/Body.2/Sketch.1/Point.3
72
Dim Pt2 As Point2D
'File: Gear1~4.CATPart/Body.2/Sketch.1/Point.4
'File: Gear1~4.CATPart/Body.2/Sketch.1/Point.5
'File: Gear1~4.CATPart/Body.2/Sketch.1/Point.6
'File: Gear1~4.CATPart/Body.2/Sketch.1/Point.7
Circle1.CenterPoint = PtOc1
Circle1.StartPoint = Pt2
Circle1.EndPoint = Pt3
73
Dim Circle2 As Circle2D
Circle2.CenterPoint = PtOc2
Circle2.StartPoint = Pt4
Circle2.EndPoint = Pt3
'File: Gear1~4.CATPart/Body.2/Sketch.1/Point.8
'File: Gear1~4.CATPart/Body.2/Sketch.1/Point.9
'File: Gear1~4.CATPart/Body.2/Sketch.1/Point.10
Circle1m.CenterPoint = PtOc1m
Circle1m.StartPoint = Pt3m
Circle1m.EndPoint = Pt2m
74
Dim PtOc2m As Point2D
'File: Gear1~4.CATPart/Body.2/Sketch.1/Point.11
'File: Gear1~4.CATPart/Body.2/Sketch.1/Point.12
'File: Gear1~4.CATPart/Body.2/Sketch.1/Circle.4
Circle2m.CenterPoint = PtOc2m
Circle2m.StartPoint = Pt3m
Circle2m.EndPoint = Pt4m
'File: Gear1~4.CATPart/Body.2/Sketch.1/Point.13
'----------------------------------------------
'File: Gear1~4.CATPart/Body.2/Sketch.1/Circle.5
CircleU.CenterPoint = PtCtr
CircleU.StartPoint = Pt2m
CircleU.EndPoint = Pt2
75
Dim CircleD As Circle2D
'File: Gear1~4.CATPart/Body.2/Sketch.1/Circle.6
CircleD.CenterPoint = PtCtr
CircleD.StartPoint = Pt4
CircleD.EndPoint = Pt5
'File: Gear1~4.CATPart/Body.2/Sketch.1/Circle.7
Circle1Const.Construction = True
'File: Gear1~4.CATPart/Body.2/Sketch.1/Circle.8
Circle2Const.Construction = True
CircleUConst.Construction = True
CircleDConst.Construction = True
76
Set CircletConst = F2D.CreateClosedCircle(0, 0, Rt)
'File: Gear1~4.CATPart/Body.2/Sketch.1/Circle.11
CircletConst.Construction = True
CircleOConst.Construction = True
Line1Const.Construction = True
Line2Const.Construction = True
'---------------------------------------------
mySketch.CloseEdition
myPart.InWorkObject = mySketch
myPart.Update
'-------------------------------------------------------
77
'Join of first Profile Tooth of Gear
'File: Gear1~4.CATPart/Body.2/Join.1
myBody.InsertHybridShape myJoin
myPart.Update
Dim D As Double
Dim L As Double
Dim H As Double
Epsilon = 0.00001
L = (R1 ^ 2 - R2 ^ 2 + D ^ 2) / (2 * D)
ProfileError = True
Exit Function
End If
78
If Abs(R1 - L) < Epsilon Then
H = 0
Else
H = Sqr(R1 ^ 2 - L ^ 2)
End If
End Function
Dim D As Double
Dim L As Double
Dim H As Double
Epsilon = 0.00001
L = (R1 ^ 2 - R2 ^ 2 + D ^ 2) / (2 * D)
ProfileError = True
Exit Function
End If
79
H = 0
Else
H = Sqr(R1 ^ 2 - L ^ 2)
End If
End Function
80
2-5) Create Full Teeth Profile (Circular Pattern) and Join them
(Sub: common)
'File: Gear1~4.CATPart/Body.2/CircPattern.1
myCP1.CircularPatternParameters = catInstancesandAngularSpacing
angle1.Value = 360 / Z
81
Set intParam1 = angularRepartition2.InstancesCount
intParam1.Value = Z + 1
Dim HS As HybridShapeFactory
Set HS = myPart.HybridShapeFactory
myCP1.SetRotationAxis RefYDir
myPart.Update
myBody.InsertHybridShape myJoin2
82
myPart.Update
HS.GSMVisibility RefmyJoin, 0
HS.GSMVisibility RefmyJoin2, 0
End If
End If
83
End If
End If
End If
End If
End If
84
2-6) Type 1: Create Solid (Pad)
(Sub: Gear1)
'Create Pad
myPart.InWorkObject = myBody
myPart.Update
End Sub
85
2-7) Type 2: Create Solid (Multi-section solid)
(Sub: Gear2)
2-7-1) Translate and Rotate of first profile
'MULTI-SECTION
myPart.InWorkObject = myBody
Set HS = myPart.HybridShapeFactory
'File: Gear2.CATPart/Body.2/Translate.1
myTranslate1.ElemToTranslate = RefmyCP1
myTranslate1.VectorType = 0
86
myTranslate1.Direction = YDir
myTranslate1.DistanceValue = -t
myTranslate1.VolumeResult = False
myBody.InsertHybridShape myTranslate1
myPart.InWorkObject = myTranslate1
Set RefmyTranslate1 =
myPart.CreateReferenceFromObject(myTranslate1)
myPart.Update
'File: Gear2.CATPart/Body.2/Rotate.1
myRotate1.ElemToRotate = RefmyTranslate1
myRotate1.VolumeResult = False
myRotate1.RotationType = 0
myRotate1.Axis = RefYDir
myRotate1.AngleValue = HelixAngle
myBody.InsertHybridShape myRotate1
87
myPart.InWorkObject = myRotate1
HS.GSMVisibility RefmyTranslate1, 0
myPart.Update
myBody.InsertHybridShape myJoin3
myPart.Update
myPart.Update
Dim SF As ShapeFactory
Set SF = myPart.ShapeFactory
88
'File: Gear2.CATPart/Body.2/Multi-sections Solid.1
myPart.Update
HS.GSMVisibility RefmyJoin, 0
HS.GSMVisibility RefmyJoin2, 0
HS.GSMVisibility RefmyJoin3, 0
''''''''''''''
myPart.Update
End Sub
89
2-8) Type 3: Create Solid (Multi-section Solid)
(Sub: Gear3)
'---MULTI-SECTION
myPart.InWorkObject = myBody
Set HS = myPart.HybridShapeFactory
'File: Gear3.CATPart/Body.2/Translate.1
90
myTranslate1.ElemToTranslate = RefmyCP1
myTranslate1.VectorType = 0
myTranslate1.Direction = YDir
myTranslate1.DistanceValue = -t
myTranslate1.VolumeResult = False
myBody.InsertHybridShape myTranslate1
myPart.InWorkObject = myTranslate1
Set RefmyTranslate1 =
myPart.CreateReferenceFromObject(myTranslate1)
HS.GSMVisibility RefmyTranslate1, 0
myPart.Update
'File: Gear3.CATPart/Body.2/Point.1
myBody.InsertHybridShape PtCtr2
91
Dim myScaling1 As HybridShapeScaling
'File: Gear3.CATPart/Body.2/Scaling.1
myScaling1.VolumeResult = False
myBody.InsertHybridShape myScaling1
Set RefmyScaling1 =
myPart.CreateReferenceFromObject(myScaling1)
HS.GSMVisibility RefmyScaling1, 0
HS.GSMVisibility RefPtCtr2, 0
myBody.InsertHybridShape myJoin3
myPart.Update
92
Dim RefmyJoin3 As Reference
myPart.Update
Dim SF As ShapeFactory
Set SF = myPart.ShapeFactory
93
myPart.Update
HS.GSMVisibility RefmyJoin, 0
HS.GSMVisibility RefmyJoin2, 0
HS.GSMVisibility RefmyJoin3, 0
'-------------------------------
myPart.Update
End Sub
94
2-9) Type 4: Create Solid (Multi-section Solid)
(Sub: Gear4)
'---MULTI-SECTION
myPart.InWorkObject = myBody
Set HS = myPart.HybridShapeFactory
'File: Gear4.CATPart/Body.2/Translate.1
myTranslate1.ElemToTranslate = RefmyCP1
myTranslate1.VectorType = 0
95
myTranslate1.Direction = YDir
myTranslate1.DistanceValue = -t / 2
myTranslate1.VolumeResult = False
myBody.InsertHybridShape myTranslate1
myPart.InWorkObject = myTranslate1
Set RefmyTranslate1 =
myPart.CreateReferenceFromObject(myTranslate1)
myPart.Update
'File: Gear4.CATPart/Body.2/Rotate.1
myRotate1.ElemToRotate = RefmyTranslate1
myRotate1.VolumeResult = False
myRotate1.RotationType = 0
myRotate1.Axis = RefYDir
myRotate1.AngleValue = HelixAngle
myBody.InsertHybridShape myRotate1
96
myPart.InWorkObject = myRotate1
HS.GSMVisibility RefmyTranslate1, 0
myPart.Update
HS.GSMVisibility RefmyRotate1, 0
'File: Gear4.CATPart/Body.2/Point.1
myBody.InsertHybridShape PtCtr2
'File: Gear4.CATPart/Body.2/Scaling.1
myScaling1.VolumeResult = False
myBody.InsertHybridShape myScaling1
Set RefmyScaling1 =
myPart.CreateReferenceFromObject(myScaling1)
97
HS.GSMVisibility RefmyScaling1, 0
HS.GSMVisibility RefPtCtr2, 0
myBody.InsertHybridShape myJoin3
myPart.Update
myPart.Update
'File: Gear4.CATPart/Body.2/Translate.2
myTranslate2.ElemToTranslate = RefmyCP1
myTranslate2.VectorType = 0
myTranslate2.Direction = YDir
myTranslate2.DistanceValue = -t
myTranslate2.VolumeResult = False
myBody.InsertHybridShape myTranslate2
myPart.InWorkObject = myTranslate2
98
Dim RefmyTranslate2 As Reference
Set RefmyTranslate2 =
myPart.CreateReferenceFromObject(myTranslate2)
myPart.Update
'File: Gear4.CATPart/Body.2/Rotate.2
myRotate2.ElemToRotate = RefmyTranslate2
myRotate2.VolumeResult = False
myRotate2.RotationType = 0
myRotate2.Axis = RefYDir
myRotate2.AngleValue = 3 * HelixAngle / 2
myBody.InsertHybridShape myRotate2
myPart.InWorkObject = myRotate2
HS.GSMVisibility RefmyTranslate2, 0
HS.GSMVisibility RefmyRotate2, 0
99
myPart.Update
'File: Gear4.CATPart/Body.2/Point.2
myBody.InsertHybridShape PtCtr3
myScaling2.VolumeResult = False
myBody.InsertHybridShape myScaling2
Set RefmyScaling2 =
myPart.CreateReferenceFromObject(myScaling2)
HS.GSMVisibility RefmyScaling2, 0
HS.GSMVisibility RefPtCtr3, 0
100
'Joining Scaled Profile
myBody.InsertHybridShape myJoin4
myPart.Update
myPart.Update
Dim SF As ShapeFactory
Set SF = myPart.ShapeFactory
101
Set RefmyLoft1 = myPart.CreateReferenceFromObject(myLoft)
myPart.Update
HS.GSMVisibility RefmyJoin, 0
HS.GSMVisibility RefmyJoin2, 0
HS.GSMVisibility RefmyJoin3, 0
HS.GSMVisibility RefmyJoin4, 0
'------------------------------------------------
myPart.Update
End Sub
102
2-10) Create Hole and Key (Pocket)
(Sub: HoleKey)
'File: Gear1~4.CATPart/Body.2/Pocket.1/Sketch.2
myPart.InWorkObject = mySketchHole
103
Set F2D = mySketchHole.OpenEdition()
XPt1Key = KW / 2
XPt2Key = KW / 2
YPt2Key = YPt1Key + KD
XPt3Key = -KW / 2
YPt3Key = YPt2Key
XPt4Key = -KW / 2
YPt4Key = YPt1Key
104
Dim Pt1Key As Point2D
'File: Gear1~4.CATPart/Body.2/Pocket.1/Sketch.2/Point.1
'File: Gear1~4.CATPart/Body.2/Pocket.1/Sketch.2/Point.2
'File: Gear1~4.CATPart/Body.2/Pocket.1/Sketch.2/Point.3
'File: Gear1~4.CATPart/Body.2/Pocket.1/Sketch.2/Point.4
'File: Gear1~4.CATPart/Body.2/Pocket.1/Sketch.2/Line.1
Line12Key.StartPoint = Pt1Key
Line12Key.EndPoint = Pt2Key
'File: Gear1~4.CATPart/Body.2/Pocket.1/Sketch.2/Line.2
Line23Key.StartPoint = Pt2Key
Line23Key.EndPoint = Pt3Key
105
Dim Line34Key As Line2D
'File: Gear1~4.CATPart/Body.2/Pocket.1/Sketch.2/Line.3
Line34Key.StartPoint = Pt3Key
Line34Key.EndPoint = Pt4Key
'File: Gear1~4.CATPart/Body.2/Pocket.1/Sketch.2/Point.5
'File: Gear1~4.CATPart/Body.2/Pocket.1/Sketch.2/Circle.1
Else
End If
mySketchHole.CloseEdition
myPart.InWorkObject = mySketchHole
myPart.Update
myPart.Update
End Sub
106
2-11) Create Chamfer (Groove)
(Sub: Chamfer)
107
Fig 2-11-2: Tree of Sketch Chamfer
Dim myPlaneYZ
108
Dim mySketchChamfer As Sketch
'File: Gear1,2.CATPart/Body.2/Groove.1/Sketch.3
myPart.InWorkObject = mySketchChamfer
XPt1Ch = Y0 - ChX
YPt1Ch = Ru
XPt2Ch = Y0 + 0
YPt2Ch = Ru - ChY
XPt3Ch = Y0 + 0
YPt3Ch = Ru + 2
109
XPt11Ch = XPt1Ch - t + 2 * ChX
YPt11Ch = YPt1Ch
XPt22Ch = XPt2Ch - t
YPt22Ch = YPt2Ch
XPt33Ch = XPt3Ch - t
YPt33Ch = YPt3Ch
'File: Gear1,2.CATPart/Body.2/Groove.1/Sketch.3/Point.1
'File: Gear1,2.CATPart/Body.2/Groove.1/Sketch.3/Point.2
'File: Gear1,2.CATPart/Body.2/Groove.1/Sketch.3/Point.3
110
'File: Gear1,2.CATPart/Body.2/Groove.1/Sketch.3/Point.4
'File: Gear1,2.CATPart/Body.2/Groove.1/Sketch.3/Point.5
'File: Gear1,2.CATPart/Body.2/Groove.1/Sketch.3/Point.6
'File: Gear1,2.CATPart/Body.2/Groove.1/Sketch.3/Line.1
Line12Ch.StartPoint = Pt1Ch
Line12Ch.EndPoint = Pt2Ch
'File: Gear1,2.CATPart/Body.2/Groove.1/Sketch.3/Line.2
Line23Ch.StartPoint = Pt2Ch
Line23Ch.EndPoint = Pt3Ch
'File: Gear1,2.CATPart/Body.2/Groove.1/Sketch.3/Line.3
111
Line333Ch.StartPoint = Pt3Ch
Line333Ch.EndPoint = Pt33Ch
'File: Gear1,2.CATPart/Body.2/Groove.1/Sketch.3/Line.4
Line3322Ch.StartPoint = Pt33Ch
Line3322Ch.EndPoint = Pt22Ch
'File: Gear1,2.CATPart/Body.2/Groove.1/Sketch.3/Line.5
Line2211Ch.StartPoint = Pt22Ch
Line2211Ch.EndPoint = Pt11Ch
'File: Gear1,2.CATPart/Body.2/Groove.1/Sketch.3/Line.6
Line111Ch.StartPoint = Pt11Ch
Line111Ch.EndPoint = Pt1Ch
112
Dim CenterLine As Line2D
'File: Gear1,2.CATPart/Body.2/Groove.1/Sketch.3/Line.7
mySketchChamfer.CenterLine = CenterLine
mySketchChamfer.CloseEdition
myPart.InWorkObject = mySketchChamfer
myPart.Update
myPart.Update
End Sub
113
2-12) Hiding main planes:
(sub HidePlanes)
This subroutine just hides Planes XY, YZ and ZX for better viewing of Gears.
Sub HidePlanes()
Set HS = myPart.HybridShapeFactory
HS.GSMVisibility RefmyPlaneXY, 0
HS.GSMVisibility RefmyPlaneYZ, 0
HS.GSMVisibility RefmyPlaneZX, 0
myPart.Update
End Sub
114
2-13) Assign a Toolbar in CATIA
As a last step, you can add an icon to a toolbar to quickly execute your program. Follow these
pictures to add an icon assigned to your VB program.
115
Now you can Drag &Drop your Module in a Toolbar.
116
117
Thank You!
Now you know how to model and program gears in CATIA V5! You're on your way to automating
even more repetitive processes and impressing your co-workers and bosses. For more real world
examples, articles, tutorials, and how-to videos please visit www.scripting4v5.com
I hope you’ve enjoyed this program as much as we loved writing it for you. We really appreciate
each and every one of you for taking time out of your day or evening to read this, and if you have
an extra second, we would love to hear what you think about it. Please leave a comment or
forum post at http://www.scripting4v5.com, or if you’d rather reach us in private, don’t hesitate
to shoot us an email.
We’ve worked very hard on this guide and spent countless hours working on the website and
simply ask that you do not share this document with anyone who has not purchased it. Please
refer all friends and colleagues to the website, www.scripting4v5.com.
To all of my subscribers, followers and friends out there, old and new, thank you for the gift of
your support. I only hope this guide can begin to repay you for the time and attention that you’ve
given me. Here’s to you and your continued success!
118
Video Demo
https://www.youtube.com/watch?v=Q7pERp0PfvI
119
Tips and Troubleshooting
If your code is not working, please double check these sections of code:
Confused or still stuck? Post your question in the forum if you get stuck or have any suggestions
for improving the tutorial.
http://www.scripting4v5.com/forums/forum/catia-macro-programming/
120
Appendix I: Keyboard Shortcuts
Default CATIA and VBA editor shortcuts:
CATIA V5:
VBA Editor:
121
Appendix II: Resources
The following is a list of resources used when creating this tutorial and other recommended tools.
https://www.youtube.com/watch?v=dvyLuOPvMV8
https://www.youtube.com/watch?v=H989ooFHrxo
http://math.stackexchange.com/questions/256100/how-can-i-find-the-points-at-which-two-
circles-intersect
Types of gears:
http://sdp-si.com/resources/gears/pdf/gear_types_manufacturing.pdf
122