Catia V5 PDF
Catia V5 PDF
Catia V5 PDF
For CATIA V5
Nick Weisenberger
Contents
Copyright Information.................................................................................................... 5
Disclaimer....................................................................................................................... 5
Chapter 1: Getting Started..................................................................................................... 6
What is a Macro and why do we use them?.................................................................. 6
What is VB script? .......................................................................................................... 6
How to Create Macros ................................................................................................... 7
How to Add an Existing Macro Library........................................................................... 7
How to Open and Run Macros....................................................................................... 8
Create an Icon for each Macro....................................................................................... 8
How to Record a Macro ................................................................................................. 8
Fundamentals for Creating Custom Macros .................................................................. 9
VB Script Syntax ...........................................................................................................10
Programming Concepts: The CATIA Object .................................................................11
Introductory Example ..................................................................................................13
Chapter 2: Fundamentals.....................................................................................................14
Structure ......................................................................................................................14
The Selection Object ....................................................................................................14
Documents versus Windows........................................................................................16
Subroutines and Functions ..........................................................................................16
Save and Save As..........................................................................................................17
Get Workbench ............................................................................................................18
Work with Collections: Count and Item.......................................................................18
Simple Viewer Commands ...........................................................................................19
Chapter 3: Building Blocks ...................................................................................................21
Error Handling ..............................................................................................................21
Create a Yes/No Message Box .....................................................................................24
Working with Parameters ............................................................................................24
2
Nick Weisenberger
Step-by-step Tutorials...60
Workshop 1: Fundamentals
Workshop 2: Creating Your Own VBA Modules and Classes
Workshop 3: Creating a Basic VBA Program from Scratch
Workshop 4: Objects in CATIA VBA
Workshop 5: Navigating a Part Document with Error Handling
Workshop 6: Creating Sketch Geometry
Workshop 7: Using Forms in CATIA VBA
4
Nick Weisenberger
Copyright Information
Copyright 2012 by Nick Weisenberger
All rights reserved. No part of this book 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 author. The only exception is by a reviewer, who may quote short excerpts in
a review.
Disclaimer
5
VB Scripting for CATIA V5: Expanded Edition
Through a series of example codes and tutorials Ill explain how to use and
create CATScript macros for CATIA V5. No programming experience is required!
This information is not featured in the user help documentation. The purpose of this
text is to show beginners how they can approach different problems and for users to
rewrite code shown in the examples to suite their specific needs.
There are many CAD engineers, designers, and technicians who want to
write macros but simply dont have time to sit down and learn everything they need
to know. This text will cover those core items to help teach beginners important
concepts needed to create custom CATScript macros.
What is a macro? If you perform a task repeatedly, you can take advantage
of a macro to automate the task. A macro is a series of functions, written in a
scripting language, that you group in a single command to perform the requested
task automatically. Macros use programming but you dont need to be a
programmer or have programming knowledge to use them (though it definitely
helps).
Macros are used to save time and reduce the possibility of human error by
automating repetitive processes, standardization, improving efficiency, expanding
CATIAs capabilities, and by streamlining tasks.
What is VB script?
(Excel, Word, CATIA, etc.), VBScript, JavaScript, Visual Basic 6.0, Microsoft
Developers Studio.NET, and others. For CATIA V5 running on UNIX, emulators
allow for VBScripts to be run with no interface building tools. Some CATScripts
from this text may work under UNIX OS but not all due to differences between the
two systems. However, this will not be covered in this text.
1. Macro recorder
1. Go to Tools>Macro>Macros
3. Make sure the Library type is set to "Directories" then click "Add existing
library"
5. Close the macros libraries window. You should see a list of .CATscript files.
You only need to do this once as the library should load even after restarting
CATIA.
7
VB Scripting for CATIA V5: Expanded Edition
1. Go to Tools>Macro>Macros
If the macro editor cannot be opened you should talk to your system
administrator because it probably has not been installed. No extra license is
required to run it.
One method for creating macros is by recording your mouse actions. A few
things to keep in mind when recording a macro:
Always UNDO what you just recorded and run the macro. If the macro
works from within CATIA and repeats what you just did, then the macro obviously
works fine. If it does NOT work from within CATIA, you need to fix it. If it does
NOT work from within CATIA it will NOT work once you cut and paste it into a
VB application.
Look through the recorded macro. Many times extra lines of code are added
which are not necessary. This is based on the order of steps that you do as you
record the macro. These unnecessary lines can be removed.
For example, if I record a macro to zoom in and then zoom out I might get the
following code:
Notice how the Set Viewpoint command appears multiple times? This is
unnecessary in this situation. The viewpoint only needs to be set once after the Dim
statement. Did I lose you yet? Dont worry; setting and declaring will be explained
in more detail in the upcoming pages.
Often times you might record a macro with a CATPart active and open it in
its own window. All goes smoothly and the macro replays fine. Then, the next day
you replay the macro again but this time you may have some other document type
open or maybe a part is open but it is in a product assembly. Usually, the macro
will fail because when the code was recorded a part was the active document but
now it is not. This is one advantage to writing custom code and knowing the
fundamentals of VB scripting.
(single, double, integer, string, etc.) or an object type (more complex). Strings are
especially useful because they hold text. Message boxes are frequently used to
display strings to users while the program is running. Looping is often used to
perform iterative actions
Object An entity (in CATIA or VB). Points, Pads, Parameters, etc. are
all examples of CATIA objects.
Property A characteristic of an object. For example, the name of a
PartDocument is a property of that object.
Method An action that an object can perform. For example
PartDocument.SaveAs() is an action that the object can perform.
Collection A group or list of similar objects which are put together for
a specific reason.
Object oriented programming came about due to the need to represent more
complex ideas within a program. For example, you could say that a person is
described by his height, weight, and hair color, and that every person has certain
actions that they can perform, such as walking, eating, and sleeping. These
properties and methods make up the class called Person. Objects of this
class can then be used in a program to represent individual people.
VB Script Syntax
Syntax is defined as the ordering of and relationship between the words and
other structural elements in phrases and sentences. Each scripting language is
composed of its own syntax. Learning the syntax of each programming language is
crucial to creating successful macros. Here are some of the key features of VB
Scripts syntax:
Indentation: Indent or out dent script to reflect the logical structure and nesting
of the statements.
Text Strings: When a value is entered as a text string, you must add quotation
marks before and after the string.
Case Sensitivity: By default, VBScript is not case sensitive and does not
differentiate between upper-case and lower-case spelling of words, for example,
in variables, object and method names, or constants.
10
Nick Weisenberger
Spaces: Add extra blank spaces to your script to improve clarity. These spaces
are ignored by VBScript.
Naming: Avoid naming conflicts. Two variables cannot have the same name.
The CATIA object is usually the first object that is referenced in any
CATIA macro. This object represents the CATIA application itself, from which the
macro is run. The CATIA object has many properties. For instance, it has a
property called FullName which is a string. Another property is called
ActiveDocument. This property is an object itself, and even more specifically, it
is a Document object. More about that later.
The CATIA object is dimmed or declared as it exists by default. The
purpose of declaring and setting variables is to hold the properties of an object.
Variables that hold objects require the Set keyword. The properties of objects can
be accessed using the notation: Object.Property. Here is an object property example
using the FullName property:
11
VB Scripting for CATIA V5: Expanded Edition
Sub CATMain()
strFullName = CATIA.FullName
Set doc1=CATIA.ActiveDocument
End Sub
-or-
12
Nick Weisenberger
Sub CATMain()
Display a message box with the full name of the
document
Msgbox CATIA.ActiveDocument.FullName
Display a message box with the number of
selections in the document
Msgbox CATIA.ActiveDocument.Selection.Count
End Sub
Introductory Example
The following code will display a message box with the text "Hello.
strHello is a variable declared as a string (or text) object. strHello is then defined.
Every Sub must end with "End Sub".
Sub CATMain()
strHello = "Hello"
MsgBox strHello
End Sub
13
VB Scripting for CATIA V5: Expanded Edition
Chapter 2: Fundamentals
Structure
If nothing is selected then the selection is empty. If one or more objects are
selected then the selection contains those one or more objects. To add an element to
the selection:
14
Nick Weisenberger
oSel.Clear
oSel.Add(ObjectToAdd)
A good practice is to always clear the selection before and after you use it.
To check what has been selected you could use the following code, which will loop
through all selected objects and display the name in a message box for each one:
For I = 1 to oSel.Count
Msgbox oSel.Item(i).Value.Name
Next i
The selection object can also be used for a variety of other tasks:
15
VB Scripting for CATIA V5: Expanded Edition
VisProps.GetRealColor(255,0,0)
visProps.SetRealColor(255,0,0,1)
There are two main collections under the CATIA application object:
Documents - There are many types of documents that are used in V5:
CATPart, CATProduct, CATProcess, CATDrawing, CATAnalysis, etc.
These are all housed in the Documents collection and they contain all
geometry, process, and product information.
Windows This collection contains information about how the data from
the documents collection will be seen in the CATIA window. It controls
items such as:
Subroutines and functions are good for encapsulating code that needs to be
called repeatedly. Functions return a value, subroutines do not. Arguments may
be passed in as ByRef or ByVal (ByVal is the default). The following code is
16
Nick Weisenberger
another example of how to display a Hello message box using multiple sub
statements.
Sub CATMain()
CallMe strMessage
MsgBox strMessage
End Sub
strMsg = Hello
End sub
The Document object includes the Save and Save As methods. The Save
method takes no arguments and returns nothing.
CATIA.ActiveDocument.Save
doc1.SaveAs C:\Example.CATPart
17
VB Scripting for CATIA V5: Expanded Edition
Get Workbench
Collections are special kinds of objects that hold a list of objects of a certain
class. The property Count and the method Item() are frequently used on
collections in CATIA. For example:
Sub CATMain()
End Sub
18
Nick Weisenberger
There are a few steps and methods to change the viewpoint of a CATIA
document. First, we need to access the 3D viewer from the current CATIA window:
Next, we need to access one of the Camera objects from the current
document. A camera object is a static version of the window viewer object:
Set objCamera3D=objCATIA.ActiveDocument.Cameras.Item(1)
Unlike CATIA V4, in V5 the views (or Cameras) for a particular document are
saved in that document. So, to change the view of a document we need to set the
3DViewer viewpoint to the Camera viewpoint:
objViewer3D.Viewpoint3D = Camera3D.Viewpoint3D
Once you access the ActiveViewer of the current window you can also control
many other display properties:
objViewer.Translate(translationVector)
objViewer.Rotate(axisOfRotation, rotationAngle)
19
VB Scripting for CATIA V5: Expanded Edition
objViewer.NewCamera()
To reframe a window
objViewer.Reframe()
To Zoom In
objViewer.ZoomIn()
To Zoom Out
objViewer.ZoomOut()
objSpecWindow.Layout = WindowGeomOnly
20
Nick Weisenberger
Now its time to build upon the fundamentals weve learned to make our
programs more complex and robust.
Error Handling
When writing CATScripts, its a good idea to avoid all errors at all cost. Don't
let your users think, they are "only users". Write them out of the equation. It is
important to debug your programs to avoid errors & program crashes. Trust me, its
not much fun if another user runs my code and tells me Hey, your code doesnt
work. Thus, my goal is to never have a code break because of a run-time error. If
you can't avoid an error then use some form of error handling.
Even if the program you write has the correct syntax, it may still encounter a
run time error as it is running. This is why error handling is necessary. For
example, the following code would produce an error if the ActiveDocument in
CATIA was a product document and not a part document.
Sub CATMain()
MsgBox pdoc1.FullName
End Sub
The program would stop at the third line of the above code. By using the
statement On Error Resume Next, the program will ignore errors that are
generated and proceed through the subsequent lines. However, in this example
where an error is ignored and the program proceeds, another error is generated on
the next line. This is because the variable pdoc1 is Nothing. It wasnt
successfully Set in the previous line.
21
VB Scripting for CATIA V5: Expanded Edition
MsgBox pdoc1.FullName
The behavior of the program can be made even more intelligent by using the
Err object, which is an object that exists in every VBA program. The Err object
holds information regarding errors that have been generated. The number
property of the Err object indicates whether an error has been generated or not.
Sub CATMain()
If Err.Number = 0 Then
MsgBox pdoc1.FullName
Else
End If
End Sub
Calling the Clear method on the Err object sets the Number property of
the Err object to zero (clean slate, as they say).
22
Nick Weisenberger
Sub CATMain()
Err.Clear
End Sub
The command On Error Goto 0 sets the execution of the program so that
errors arent ignored for the subsequent lines.
Sub CATMain()
=====================================
proceeds
=====================================
On Error Goto 0
=====================================
will stop
End Sub
23
VB Scripting for CATIA V5: Expanded Edition
Use the following code to create a pop-up message box with the option to click
Yes or No:
Else
End If
To create a parameter:
Set objParm=objParameters.CreateDimension(Name,Length,10.0)
For Loop
Just like any other part of CATIA, there are multiple methods to accomplish
the same task. One example of this in VB scripting is the For Loop. A for-each loop
can be used to iterate through most collections in CATIA.
24
Nick Weisenberger
Method 1:
Sub CATMain()
MsgBox doc1.Name
Next
End Sub
Method 2:
Sub CATMain()
Dim i As Integer
For i = 1 To CATIA.Documents.Count
MsgBox doc1.Name
Next
End Sub
25
VB Scripting for CATIA V5: Expanded Edition
Background Color can be set by passing values to or from an array. One use
of changing the background color may be to take a screen capture with a white
background for easy printing (and using less ink)! Use this code to change the
background color to white:
ReDim dblBackArray(2)
background color
objViewer3D.GetBackgroundColor(dblBackArray)
ReDim dblWhiteArray(2)
dblWhiteArray(0) = 1
dblWhiteArray(1) = 1
dblWhiteArray(2) = 1
objViewer3D.SetBackgroundColor(dblWhiteArray)
objViewer3D.SetBackgroundColor(dblBackArray)
26
Nick Weisenberger
The & symbol works much the same as the concatenate formula in
Microsoft Excel by combining multiple elements. For example, if you want a pop
up message box to read The density is 55 where 55 is a variable which will
update with the part try this code:
Sub CATMain()
End Sub
27
VB Scripting for CATIA V5: Expanded Edition
Documentation can be found from within the CATIA VBA editor by way of
the menu. Go to View>Object Browser or simply hit F2. As an example, lets say
you want to create a pad object, Type in the unknown object type into the search
window at the top of the object browser. Look in the third column of results
(member) for something that makes sense. In this case, AddNewPad sounds
good. Look in the second column (class). This will tell you what object is needed
to use the function in the third column. Now look at the bottom of the window. This
will tell you how to use the function and what objects are needed.
Member of PARTITF.ShapeFactory
Where Argument 1 is a sketch and I means input. The Pad object must be declared.
Windows Explorer
Sub CATMain()
End Sub
User Forms
A great way to enhance the experience for your end users and one of the
most important features of Visual Basic programs is by creating userforms.
28
Nick Weisenberger
Userforms give you the ability to quickly and easily create graphical user interfaces
(GUI) for your macro programs. The foundation of any GUI in a Visual Basic
program is a Form, onto which various buttons, text fields, list fields, etc. may be
dragged onto. Users can input text, choose from a list of options, our click a
command button which runs a subroutine that uses the user input options.
Forms, buttons, text fields, etc. can be thought of as special kinds of objects
that instead of running methods have events. In the same way that a method is a
function or a subroutine that runs whenever called from within a program, an
event is a subroutine that runs whenever a user interaction triggers that event. For
example, each button on a form has a Click event that runs whenever a user clicks
on the button.
To create a new UserForm press ALT-F11 to open the VBA editor. In the
Project explorer window (top left side), right-click on the project then select Insert
UserForm. Double check to make sure it is named UserForm1 in the properties
window. You can drag command button icons to your new UserForm. For more on
UserForms see Workshop 7.
Design Mode
If you're working with large assemblies, you may want to automatically set
each item in the tree to design mode (individually opened parts are typically
automatically opened in design mode already):
Set productDocument1=CATIA.ActiveDocument
Products1.item(i).ApplyWorkMode DESIGN_MODE
29
VB Scripting for CATIA V5: Expanded Edition
parentFileName=products1.item(i).ReferenceProduct.Parent.name
Else
Keep in mind, that when the new part is added, the objProduct object will
reference the Product level of the Part. The Product class in CATIA VBA has a
peculiar property called ReferenceProduct. This property returns a Product.
30
Nick Weisenberger
Also, there are two internal names for products inside CATIA V5: the
Product Name and the Instance Name. These can both be accessed via the CATIA
Object Model using two different properties:
The viewer gives you the capability of exporting the 3D window into a
picture format:
objViewer3D.CaptureToFile(catCaptureFormatJPEG,
C:\myPicture.jpg)
1. BMP
2. TIFF
3. CGM
4. EMF
5. TIFFGreyScale
The size of the picture is, by default, the size of the active window. You can
change the size of the picture with this code:
Set window=CATIA.ActiveWindow
H=win.Height
W=win.Width
sLF=Chr(10)
31
VB Scripting for CATIA V5: Expanded Edition
If win.Height="" Then
Exit Sub
End If
Then do the same thing for the width and restore original settings at the end.
2D Drawing Viewers
Activation State
32
Nick Weisenberger
Measurement Macros
Set objDistanceRel =
Part.Relations.CreateFormula("DistanceForm", "",
myDistance,_ "distance(`Geometrical
Set.1\MyEndPt1`,`Geometrical Set.1\MyEndPt2`)")
33
VB Scripting for CATIA V5: Expanded Edition
1. Reboot your pc
2. Open a command prompt
3. CD to the unload directory of the version of CATIA you want to un-
register
(i.e. c\program files\Dassault systemes\B15\intel_a\code\bin)
4. Run the command cnext-unregserver
5. Open up the task manager and wait until the cnext process stops
running (which may take a few minutes)
6. CD to the unload directory of the version of CATIA you want to
register
(i.e. c\program files\Dassault systems\B16\intel_a\code\bin)
7. Run the command cnext-regserver
8. In the task manager again, wait until the cnext process stops
9. Open up VB and check that the correct version is registered
10. Please note this only needs to be done between full releases and not
service packs!
34
Nick Weisenberger
Spreadsheets are used in the world of engineering to create part lists and
bills of material. These are typically created in Microsoft Excel. A macro can be
created to export data from CATIA into an Excel spreadsheet, quickly automating
this process.
Before launching Excel we need to declare all of our objects and variables
including the Microsoft Excel application itself, each workbook, each worksheet
within each workbook, etc.
35
VB Scripting for CATIA V5: Expanded Edition
Err.Clear
Else
Err.Clear
Exit Sub
End If
Or, we may just want to create a new worksheet and not make the user have
to close Excel first (which is more user friendly), thus we will use this preferred
code:
Err.Clear
End If
36
Nick Weisenberger
When you do not want to see your screen follow the actions of your VBA
procedure (macro), you start and end your code with the following sentences:
At the start:
Application.ScreenUpdating = False
To display your completed Excel file, type this line of code at the point in
your program when you want Excel to appear.
Excel.Visible = True
To write text in a specific cell, such as the first row's header values, use
Excel.Cell (row #, column #).
'Cell A1
Excel.Cells(1,1)="Part Number"
'Cell B1
Excel.Cells(1,2)="Fasteners"
Excel.Cells(1,3)="Name"
Excel.Cells(1,4)="Thickness"
Excel.Cells(1,5)="Material"
Excel.Cells(1,6)="Mass"
Excel.Cells(1,7)="Sorter"
37
VB Scripting for CATIA V5: Expanded Edition
You can use a variable to assign a cell number. For example, if you have a
For Loop and want the Excel row number to increase by one for each iteration of
the loop you might use this:
For I = 1 to 10
Excel.Cells(RwNum, 4) = getThickness
Excel.Cells(RwNum, 5) = getMaterial
Excel.Cells(RwNum, 6) = getMass
RwNum = RwNum + 1
Next 'i
Excel.Cells(RwNum+1,2)= namebody
RwNum = RwNum + 1
End If
Else
RwNum = RwNum
Excel Formulas
You may also need to include formulas to attain the correct cell value. Add
a .Formula after the Excel.Cell().
38
Nick Weisenberger
Excel.Cells(2,13).Formula= "=NOW()"
Excel.Cells(2,13).NumberFormat = "m/d/yyyy"
Some Excel formulas require quotation marks. If this is the case you will
have to use Chr(34) character representation instead.
Excel.Cells(1,9).Formula =
"=SUMIF(I5:I"&RwNum+2&","&Chr(34)&">0"&Chr(34)&")"
Now let's combine a For Loop with a couple of SUM and IF formulas.
RwNumX=1
Excel.Cells(RwNumX,11).Formula =
"=SUM($J$2:J"&RwNumX &")"
Next 'x
39
VB Scripting for CATIA V5: Expanded Edition
Const xlAscending = 1
Const xlYes = 1
Const xlSortOnValues = 0
Const xlSortNormal = 0
Const xlTopToBottom = 1
Const xlPinYin = 1
Const xlThick = 4
Const xlEdgeBottom = 9
Const xlSolid = 1
Const xlThemeColorDark1 = 1
Const xlContinuous = 1
Const xlDiagonalUp = 6
Const xlDiagonalDown = 5
Const xlThin = 2
Const xlEdgeRight = 10
40
Nick Weisenberger
Inserting new rows and columns into your spreadsheet is very easy. First, select a
cell in the spreadsheet and then specify if you want to insert a row above it as in
these examples:
Excel.Cells(1,1).Select
Excel.ActiveCell.EntireRow.Insert
Excel.ActiveCell.EntireColumn.Insert
Excel.Cells(1,8).Select
Excel.ActiveCell.EntireColumn.Offset(0, 1).Insert
Excel.Range("A:G").Select
Excel.Selection.Sort
Excel.Range("G1"),1,Excel.Range("A1"),,1,Excel.Range("B1
"),1,1,1,False
41
VB Scripting for CATIA V5: Expanded Edition
Excel.ActiveWorkbook.ActiveSheet.Rows(2).Delete
Excel.ActiveWorkbook.ActiveSheet.Columns(7).Delete
Formatting Excel
After all the formulas and sorting on the spreadsheet is complete, then its
time to format to get it the way you want it to look. I recommend leaving the
formatting until the end. Some formatting examples:
Excel.Rows("1:1").Select
Excel.Selection.Font.Bold = True
Excel.Selection.Font.Size = 12
42
Nick Weisenberger
More examples:
.Font.Name = "Arial"
.Font.Size = 9
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
'.ColumnWidth = 25
.RowHeight = 20
.Borders.LineStyle = xlContinuous
.Borders.Weight = xlThick
.Borders.ColorIndex = 1
.WrapText = True
.EntireColumn.Autofit
End With
Excel.Range("Z1:AA1").Select
Excel.Selection.Merge
Excel.Range("W3","AA3").WrapText = True
Excel.Cells(2,12).Font.Color = -16776961
43
VB Scripting for CATIA V5: Expanded Edition
Excel.Range("K"&Last,"M"&Last).Select
Excel.Selection.Borders(xlDiagonalDown).LineStyle =
xlNone
With Excel.Selection.Borders(xlDiagonalUp)
.LineStyle = xlContinuous
.ColorIndex = 1
.TintAndShade = 0
.Weight = xlThin
End With
Excel.Cells.Select
With Excel.Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
44
Nick Weisenberger
'export to PowerPoint
Set oPPT=CreateObject("PowerPoint.Application")
End If
oPPT.WindowState=WindowMaximized
Set oPPTPres=oPPT.Presentations.Add()
45
VB Scripting for CATIA V5: Expanded Edition
Now, we are going to add a title slide to our PowerPoint presentation. There
are a number of different slide styles you can add, which is designated by the
second number inside the parenthesis after the Add. The first number is the slide
number. You can also set the header text box of the slide to display the part name of
your CATIA object or any other custom text.
'title slide
objSlide.Shapes(1).TextFrame.TextRange.Text = partName
objSlide.Shapes(2).TextFrame.TextRange.Text =
"Annotation Data"
It is often useful to add images captured from the 3D model into your PPT.
We do this by inserting our image capture macro code within out PowerPoint
exporter.
If you want to add ten slides to your ppt, set i=10 and use a For Loop. Then
when you add new slides to the ppt use the i value again to add the new slide to the
end of the slideshow.
46
Nick Weisenberger
objSlide.Shapes(1).TextFrame.TextRange.Text = capNamer
objSlide.HeadersFooters.Footer.Visible = True
objSlide.HeadersFooters.Footer.Text = partName
objSlide.HeadersFooters.DateAndTime.Visible = True
objSlide.HeadersFooters.DateAndTime.UseFormat = True
objSlide.HeadersFooters.DateAndTime.Format = 1
objSlide.HeadersFooters.SlideNumber.Visible = True
If you insert image captures into your slideshow they may not be the right size
so you might have to adjust them manually. Before you make modifications, it is a
good idea to lock the aspect ratio. Use this code to lock the aspect ratio for all the
pictures as you insert them:
Pic.LockAspectRatio=True
At the end of your code you will probably want to make PowerPoint visible:
oPPT.Visible =True
47
VB Scripting for CATIA V5: Expanded Edition
There are many other useful functions that are available through making calls to
Windows libraries. These are available from Microsofts website. Some examples:
Its always a good idea to add some notes at the beginning of your code
indentifying who wrote or modified the code, when the last revision was made,
what the code actually is supposed to do, etc. The following are a couple of CATIA
macros I wrote and have used throughout my professional career in one form or
another. Please feel free to use them for your own purposes and improve upon
them!
Sub CATMain()
Dim SelectedElement
Set SelectedElement = CATIA.ActiveDocument.Selection
Dim oSelElem
Set oSelElem = SelectedElement.Item(1).Value
48
Nick Weisenberger
Dim info
Set info = oSelElem.Parent.Parent
If Err.Number=0 Then
Else
End If
End Sub
Language="VBSCRIPT"
Sub CATMain()
49
VB Scripting for CATIA V5: Expanded Edition
Else
End If
End If
End Sub
50
Nick Weisenberger
If partName="" Then
Else
'Toggle Compass
CATIA.StartCommand("Compass")
'=======================================================
objViewer3D.Viewpoint3D = objCamera3D.Viewpoint3D
'reframe
objViewer3D.Reframe()
'zoom in
objViewer3D.ZoomIn()
'objViewer3D.ZoomIn()
'MsgBox strname
Else
zoom in
objViewer3D.ZoomIn()
52
Nick Weisenberger
'take picture
objviewer3D.Capturetofile 4,strname
End If
'*******************RESET**********************
objViewer3D.FullScreen = False
'toggle compass
CATIA.StartCommand("Compass")
End If
End Sub
Sub CATMain()
partName = Left(partDocument1.name,
Len(partDocument1.name) - 8)
partDocument1.Selection.Clear()
numSets=oAnnotationSets.Count
numCap=CurAnnoSet.Captures.Count
'error handling
On Error Resume Next
If Err.Number=0 Then
With oSel
.Clear
.Add oAnnotation
Call .VisProperties.SetVisibleColor(0, 0, 0, 0)
End With
oAnnotation.ModifyVisu
End If
Next
Next
capNamer=CurAnnoSet.Captures.Item(i).Name
strname=fileloc&partName&" "&capNamer&exten
'take picture
objviewer3D.Capturetofile 4,strname
Next 'i
End If
With oSel
.Clear
.Add oAnnotation
Call .VisProperties.SetVisibleColor(255, 255, 255, 255)
End With
oAnnotation.ModifyVisu
End If
Next
Next
'================================================
'error handling
Else
Msgbox "Not a part document! Open in new window."
End If
End Sub
Help Documentation
Final Thoughts
Now you know how to write VBScript macros for CATIA V5! Youre on
your way to automating those repetitive tasks and impressing your coworkers and
bosses.
-Nick Weisenberger
57
VB Scripting for CATIA V5: Expanded Edition
58
Nick Weisenberger
Appendix I: Acronyms
The following terms are used throughout this text (in alphabetical order):
59
WORKSHOP Tutorials
Contents
Workshop 1: Fundamentals
Workshop 2: Creating Your Own VBA Modules and Classes
Workshop 3: Creating a Basic VBA Program from Scratch
Workshop 4: Objects in CATIA VBA
Workshop 5: Navigating a Part Document with Error Handling
Workshop 6: Creating Sketch Geometry
Workshop 7: Using Forms in CATIA VBA
Description
VBScript is a subset of the Visual Basic Programming language
(VBA). All elements of VBScript are present in VBA, but some VBA
elements are not implemented in VBScript. The result of the slimming down
process is a very small language that is easy to use. Code specific to CATIA is
saved as .CATScript. In this workshop you will learn some of the basic
fundamentals needed to create and run macros.
Outline
WS-3
Why use macros?
Standardization
Improve efficiency
Expand CATIA capabilities
Streamline tasks
1. Macro recorder
2. Write custom code with the macro editor
WS-4
How to add a macro library
Go to Tools>Macro>Macros
WS-5
Click "Macro libraries"
WS-6
Make sure the Library type is set to "Directories" then click "Add existing library"
WS-7
Browse to "C:\MyCatScripts" or
wherever your catscripts are saved then
click ok.
WS-8
Close the macros libraries window.
After you have created some macros you will see a list of the
.CATscript files here.
You only need to do this once- the library should load even after
restarting CATIA.
WS-9
To open and run macros
WS-10
Recording a Macro
To stop recording:
Tools>Macro>Stop Recording or click the stop button
WS-11
To create an icon for each macro:
WS-12
Under Commands, select the macro then click
the "" box. The icon browser pops up. Over 6000
to choose from. Select one then hit close.
WS-13
Finally, drag and drop the .CATScript file from the command window
to whatever toolbar you would like the icon to appear on
Now you can click the Icon to run your macro!
You can also setup a custom keyboard shortcut as well.
Code can be sent to other user as a lightweight .txt text file
WS-14
Fundamentals Example
Hit alt+F8 to open the macro window. Create a new CATScript
macro named msgbox. Click the Edit button. Copy and paste the
code below. The following code will display a message box with the
text "Hello." strHello is a variable declared as a string (or text) object.
strHello is then defined. Every Sub must end with "End Sub". Click
Save. Close the editor and run your macro. The "Hello" message box
should appear.
Su b CATMain ( )
Dim st rHe llo As St rin g
st r He llo ="He llo "
MsgBo x st r He llo
En d Su b
WS-15
Workshop 1 Conclusion
WS-16
WORKSHOP 2
Creating Your Own VBA
Modules and Classes Tutorial
Description
Although most of the CATIA VBA programming that you will do
will involve the use of classes that are defined by the CATIA
programming API, you will likely find it useful to define your own
classes. In this workshop you will create a custom class in order to
demonstrate the fundamentals of VBA object design.
Outline
WS-18
Step 1
Open the VBA editor by hitting Alt + F11. Create a new macro library and VBA
project, called "myVBA".
Double click on your newly created library (which will appear in Current
Libraries).
WS-19
Step 2
Ensure the project and properties boxes are visible by going to the top menu bar
and clicking View >View Project Explorer and View Properties Window
Right click on VBAProject (myVBA in this example) > Insert> Module.
Use the (Name) field in the properties box to rename it
"Create_New_Part_Document"
WS-20
Step 3
Right click on the Modules folder of the VBA project and select Insert > Class Module.
Rename the resulting class module as Messenger by clicking on Class1 then renaming the
"Name" field in the
properties box.
WS-21
Step 4
WS-22
Step 5
Enter the code seen to the right into the Sub CATMain()
UseTheMessenger module. Note the
following:
Dim oMssgr As Messenger
A Messenger object is dimmed. Set oMssgr = New Messenger
Because you created the class module
oMssgr.Message = "Hello"
named Messenger this class is
MsgBox oMssgr.Message
immediately available in your VBA
project.
End Sub
The New keyword orders the creation
of a Messenger object. The variable
oMssgr is set to this new object
The property Message gets set to a
specific value here.
The Message property then gets used
as it is passed to the message box.
Return to the code window for the class module Messenger" and make the
changes shown below. These changes have the effect of hiding the strMessage
variable, but then create a read-only property named Message whose value is
stored in the strMessage variable.
Now go back to the UseTheMessenger module and make the changes shown
below and to the right.
Sub CATMain()
Private strMessage As String
Dim oMssgr As Messenger
Property Let Message(MessageIN As String) Set oMssgr = New Messenger
strMessage = MessageIN
End Property oMssgr.Message = "Hello"
MsgBox oMssgr.Message
End Sub
WS-24
Step 7
Attempt to run UseTheMessenger and note that although the Message property
can be set, it fails when the property is gotten. This is because no Property Get
method has been defined (although, Property Let has been defined).
WS-25
Step 8
Return to the code window for the class module Messenger and add a Property
Get method as shown below.
Run the UseTheMessenger module again. It should work and the message Hello
should be displayed.
Note: the advantage of strictly defining these Let and Get methods is that it gives
the programmer control over whether a variable is read-only or read-write. Also, the
code that is in the Let and Get methods can contain more complex operations
and logic.
WS-26
Step 9
Assume it would be desirable to keep count of how many times the Message
propertys value is changed. To do this, create a private integer variable iCount,
change the Property Let method of the Message property, and add a Property
Get method for a new property named MsgChangeCount.
Private strMessage As String
Private iCount As Integer
WS-27
Step 10
WS-28
Step 11
Although the class works presently, there is one area where the class code
could be more explicit. The iCount variable is incremented by one every
time the Message property is changed, but it isnt clear what value iCount
starts at. Testing the code has shown that it does start at zero, however its
best to be explicit.
In the code window for the Messenger class module, click the left drop-
down menu and choose Class.
WS-29
Step 12
The result of the previous step should be that the text for the subroutine
Class_Initialize appears in the code window. Enter the code shown below into
this sub. This has the effect of setting the value of the iCount to zero when a
Messenger object is created with the New command.
iCount = 0
End Sub
WS-30
Step 13
Properties have been defined for the messenger class. Now create a method for
this class. This method will capitalize the message that is stored in the Message
property. Enter the code seen below in the Messenger class module.
The function UCase is a standard VBA function that takes a String as an argument
and returns the same string in all capital letters.
Private strMessage As String
Private iCount As Integer
WS-31
Step 14
Change the UseTheMessenger code so that it calls this new capitalize method as
shown.
Run the code. Note that the capitalize method has the intended effect of changing
the message box text from Hello to HELLO.
Sub CATMain()
oMssgr.Message = "Hello"
oMssgr.Capitalize
MsgBox oMssgr.Message
End Sub
WS-32
Workshop 2 Conclusion
Define a read-only property for the class (only provide a Property Let
method)
Define a read-write property for the class (provide both Property Let
and Property Get methods
WS-33
WORKSHOP 3
Creating a Basic VBA Program
from Scratch
Description
In this workshop, as opposed to starting a CATIA VBA
program by recording a macro, you will instead insert a new
module into an existing CATIA VBA library and type the program
statements in manually. You will also see the difference between
Subs and Functions, and get experience using primitive variable
types, arrays, and For loops.
Outline
1. Create a new module in the macro library created in Workshop 2
called myVBA_01
2. Program a routine that creates two integer variables, adds them,
and then displays the result in a message box.
3. Create a new module with a routine using a Sub
4. Create a new module with a routine using a Function
5. Create a new module with a routine using arrays and a For loop.
WS-35
Step 1: Create a new module named Workshop2_01
Right-click on the myVBA project on the Project tree and select Insert > Module.
Rename the newly created module Workshop2_01
WS-36
Step 2
In the code window, type the code shown below
Click Save on the File menu to save the your changes.
Sub CATMain()
'Declaration of variables
Dim intA As Integer
Dim intB As Integer
Dim intC As Integer
End Sub
WS-37
Step 3
Close the VBA editor.
Open the Macros menu through Tools > Macro > Macros.
Select the WorkShop2_01 macro on the list and click the Run button
Note that the value 5 is displayed in the message box.
WS-38
Step 4
Right-click on the myVBA project on the Project tree and select Insert > Module.
Rename the newly created module Workshop2_02.
In the code window, type the code shown below.
Sub CATMain()
Dim intA As Integer
Dim intB As Integer
Dim intC As Integer
intA = 2
intB = 3
WS-39
Step 5
intA = 2
intB = 3
End Sub
End Sub
WS-40
Step 6
Right-click on the myVBA project on the Project tree and select Insert > Module.
Rename the newly created module Workshop2_03
In the code window, type the code shown below
Click Save on the File menu to save the your changes.
Run the macro, this time by using the Run button in the VBA editor. This requires
that the cursor in the code window is sitting within the CATMain Sub.
Sub CATMain()
Dim intA As Integer
Dim intB As Integer
Dim intC As Integer
intA = 2
intB = 3
WS-41
Step 7
Sub CATMain()
For i = 1 To 3
Click Save on the File menu
to save the your changes. intC = DisplaySum(intA(i), intB(i))
MsgBox intC
Run the macro.
Next
End Sub
WS-42
Workshop 3 Conclusion
WS-43
WORKSHOP 4
Objects in CATIA VBA
Description
Outline
WS-45
Step 1
Create a new module in the myVBA
macro library called
CATIAbasicObjects
In the code window type Sub
CATMain() and hit Enter.
WS-46
Step 2
Scroll down the Intellisense drop-down and note that the Windows property is there as well.
So Documents and Windows are both properties of the CATIA object, but they
are also objects themselves. Moreover, they are a special type of object: they
are collections.
Erase the text CATIA. that you just entered.
WS-47
Step 3
Open 3 new parts in CATIA, accepting their default names and save them
anywhere on disk.
Enter the code below into the code window.
Run the macro using the Run button. The count of open documents should be
displayed.
Sub CATMain()
End Sub
WS-48
Step 4
Add the code shown to the right and re-run the macro:
Note: The method Item() was used on the Documents object documents1 in order
to retrieve a member of the collection.Then the Name property and FullName
property of the Document object doc1 were used. These properties return Strings,
which were displayed in the message boxes.
Sub CATMain()
MsgBox doc1.Name
MsgBox doc1.FullName
End Sub
WS-49
Step 5
Modify the previous code as shown in the
code window to the right Sub CATMain()
Note that the For each loop iterates MsgBox "The number of documents is " &
through the Documents collection documents1.Count
documents1. The same effect could be
achieved with the below code, but a For Dim doc1 As Document
Each loop is simpler: Set doc1 = documents1.Item(1)
MsgBox doc1.Name
Dim doc1 As Document MsgBox doc1.FullName
Dim i as Integer
For Each doc1 in documents1
For i = 1 to documents1.Count
MsgBox doc1.Name
Set doc1 = documents1.Item(i)
MsgBox doc1.Name Next
WS-50
Step 6
Modify the previous code as shown in Sub CATMain()
the code window to the right:
Dim documents1 As Documents
Set documents1 = CATIA.Documents
1) Uncomment a couple lines
2) Comment the For Each loop out MsgBox "The number of documents is " &
documents1.Count
completely
3) Add more code Dim doc1 As Document
Set doc1 = documents1.Item(1)
MsgBox partDoc1.Name
End Sub
WS-51
Step 7
Note that the PartDocument object partDoc1 is Set to the Document object
doc1. The setting of a variable to an object of a different type is possible in this
case because the PartDocument class inherits from the Document class. This is
depicted in the CATIA object diagram below. Not only does PartDocument inherit
from Document, but so does ProductDocument, DrawingDocument, etc.
WS-52
Step 8
Modify the previous code as shown in the Sub CATMain()
code window to the right by adding the line: Dim documents1 As Documents
partDoc1.Close Set documents1 = CATIA.Documents
Note in the Intellisense drop-down that the MsgBox "The number of documents is " &
Close method has a green icon to indicate documents1.Count
that it is a method as apposed to a property.
Dim doc1 As Document
The icon for a property can be seen for the Set doc1 = documents1.Item(1)
property FullName
MsgBox doc1.Name
Run the new code using the Run button MsgBox doc1.FullName
Note that the calling of this Close method
performs the action of closing the document For Each doc1 in documents1
MsgBox doc1.Name
Next
MsgBox partDoc1.Name
partDoc1.Close
End Sub
WS-53
Workshop 4 Conclusion
WS-54
WORKSHOP 5
Navigating a Part Document
with Error Handling
Description
In this workshop you will learn to drill down the object structure a
part document while using error handling to deal with certain
programmatic challenges.
Outline
1. Create a new CATPart in CATIA and create a new CATIA VBA
library and module.
2. Use error handling in your code to check that the active document
is a part document
3. Have the program count the number of sketch based features and
datum planes in the part using error handling
WS-56
Step 1
First, we are going to create some geometry to test our macro on. Units and names
do not matter for this exercise.
Create a new part
In the part design workbench, draw a sketch of a square on any plane
Pad the sketch (to any length)
Under Geometrical Set 1, create an offset plane and a datum (explicit) plane
WS-57
Step 2
Create a new module in the myVBA macro library called NavigatePart
Enter the code shown on the right
With the new part open, run the code using either the play button or the Tools >
Macro > Macros menu.
NOTE: Nothing happens
Now, create a new product document and leave it open in CATIA as the active
document. Run the code again and the message box should appear
Sub CATMain()
On Error Resume Next
End If
End Sub
WS-58
Step 3
Open the V5Automation.chm file and go to the object diagram for part documents. The first goal is to add code
that will count the number of sketch-based-features. These feature objects are all different kinds of shapes". The
diagram shows that we will have to drill down like so: Part > Bodies > Body > Shapes. Click on the red arrow
next to the Shape box to see a more detailed view of Shape objects.
WS-59
Step 4: Use the Shape Object Diagram
As you can see, there are many classes that inherit from the more general Shape class.
Here you can see SketchBasedShape, BooleanShape, DressUpShape, and
TransformationShape. We want to count the number of SketchBasedShape objects that
are in the part.
WS-60
Step 4: Add Code to Count SketchBasedShape Objects
Add the code shown to the right to the previously Dim body1 As Body
entered code. Note that this first section of code Set body1 = partDoc1.Part.Bodies.Item(1)
grabs the Shapes collection underneath the first part
body. Dim shapes1 As Shapes
Set shapes1 = body1.Shapes
This next section of code prepares the variables we
will use to grab individual objects from the Shapes Dim shape1 As Shape
collection
Dim sketchShape As SketchBasedShape
The for each loop grabs each Shape in the Shapes Dim count1 As Integer
collection, then attempts to set the more specific count1 = 0
SketchBasedShape object to the more abstract
Shape object. If the Shape object is in fact a For Each shape1 In shapes1
SketchBasedShape, then no error is generated. If no
error is generated, then the sketchShapeCount is Set sketchShape = shape1
increased by 1. If Err.Number = 0 Then
Note that the Error object Err must be cleared each count1 = count1 + 1
time so that errors dont carry over to the next
iteration of the loop. Run the code, you should see End If
this message box: Err.Clear
Next
WS-61
Step 5: Add Code to Count BooleanShape Objects
For i = 1 To shapecount
count1 = count1 + 1
End If
Err.Clear
Next
WS-62
Step 6: Use the Part Document Object Diagram
WS-63
WS-64
Step 7: Use the HybridShape Object Diagram
There are many classes that inherit from the abstract class HybridShape, as can be seen below.
We are looking for a class that is the equivalent of a datum plane. The class
HybridShapePlaneExplicit looks like the right one.
WS-65
Step 8: Add Code to Count Datum Planes
Insert the following code below what we already have. You should get the resulting
message box.
For k = 1 To geocount
Set part1 = partDoc1.Part
Set datumPlane = hybridBody1.HybridShapes.Item(k)
Dim hybridShapeFactory1 As HybridShapeFactory
If Err.Number = 0 Then
Set hybridShapeFactory1 = part1.HybridShapeFactory
count2 = count2 + 1
Dim hybridBodies1 As HybridBodies
Set hybridBodies1 = part1.HybridBodies
End If
Err.Clear
Dim hybridBody1 As HybridBody
Set hybridBody1 = hybridBodies1.Item(1)
Next
geocount = hybridBody1.HybridShapes.count
MsgBox "Number of sketch based shapes is: " &
count1 & vbNewLine & _
Dim k, count2 As Integer
"Number of datum planes is : " & count2
Dim datumPlane As HybridShapePlaneExplicit
count2 = 0
End Sub
WS-66
Workshop 5 Conclusion
WS-67
WORKSHOP 6
Creating Sketch Geometry
Description
In this workshop you will explore the creation of sketch geometry
in a part document. The purpose of this is to illustrate the use of factories
to create objects in a part document.
Outline
1. Start a new macro recording.
3. Open the recorded macro and observe what has been recorded.
WS-69
Step 1: Start Recording a Macro
Create a new part document
Start recording a new macro.
Name it Create_Sketch_01
WS-70
Step 2. Start the sketch
Create a line away from the origin that isnt parallel to to the V axis or H
axis
Sub CATMain()
===========================================
Get the part body
===========================================
Dim partDocument1 As PartDocument
Set partDocument1 = CATIA.ActiveDocument
WS-72
Step 4: Comment and test the code
point2D1.ReportName = 3
Dim point2D2 As Point2D
Set point2D2 = factory2D1.CreatePoint(60#, -50#)
point2D2.ReportName = 4
WS-75
Step 7: Identify code for removal (1/3)
WS-77
Step 7: Identify code for removal (3/3)
WS-78
Step 8: Renaming and more code removal
===============================================
This stage of code, after
Creating Sketch Geometry modifications, is shown to the left
===============================================
part1.InWorkObject = sketch1
Note that first two points are
created, and it appears as though
Dim factory2D1 As Factory2D the line gets created by using the
Set factory2D1 = sketch1.OpenEdition()
same coordinates of the points.
Dim axis2D1 As Axis2D The StartPoint property and
Set axis2D1 =
sketch1.GeometricElements.Item("AbsoluteAxis") EndPoint property need to be
explicitly set to the two points that
Dim point2D1 As Point2D
Set point2D1 = factory2D1.CreatePoint(20#, -20#)
were created.
In order to increase the clarity of
Dim point2D2 As Point2D
Set point2D2 = factory2D1.CreatePoint(60#, -50#)
the code, many objects could be
renamed in a more descriptive
Dim line2D3 As Line2D manner. The Point2D object
Set line2D3 = factory2D1.CreateLine(20#, -20#, 60#, -50#)
point2D1 could be named
line2D3.StartPoint = point2D1 pt2Dstart, and point2D2 could
line2D3.EndPoint = point2D2
be named pt2Dend.
sketch1.CloseEdition These couple lines of code now
part1.InWorkObject = body1
serve no purpose, and may be
part1.Update
End Sub removed.
WS-79
Workshop 6 Conclusion
WS-80
WORKSHOP 7
Using Forms in CATIA VBA
Description
In this workshop you will learn how to use VBA forms in order to
provide users with a user interface from which they can launch CATIA
VBA programs.
Outline
WS-82
Step 1a: Create a new form in the macro library
Open the CATIA VBA editor so that the myVBA macro library is visible. Make sure
the Create_Sketch_01 module is present.
WS-83
Step 1b
Name it the form frmCreateLine by editing the (Name) property.
WS-84
Step 2: View the Code of the Form
With frmCreateLine
selected in the project tree,
click the View Code
button
As a quick experiment,
enter the command featured
to the right in this Click
sub. WS-85
Step 3a: View the Code of the Form
You need to create a module that displays the form you just created. Create the
new module and name it Show_CreateLine_Form
In the blank code window for the newly created module enter the following code:
Sub CATMain()
frmCreateLine.Show
End Sub
WS-86
Step 3b: View the Code of the Form
Run this new macro using the Run button. A blank form should appear over
the CATIA window. Click on the form with your mouse and a message box should
appear displaying hello. As you can see, the form recognizes the click event and
it runs the code each time you click it. Close the form by using the x button in the
top right corner.
WS-87
Step 4: Create a Text Box
Return to the form in the VBA editor,
left-click and hold on the TextBox
button of the form toolbar, and drag
and drop a TextBox onto the form.
WS-88
Step 5: Create a Text Box (continued)
WS-89
Step 5: Create a Text Box (continued)
WS-90
Step 6: Add More Form Objects
Drag and drop the objects pictured below onto the form. Edit their properties as
indicated.
Label Label Label TextBox
Caption = Point A
TextBox
(Name) = tbPointBy
Label
Text = 10
(Name) = lblPointB
Text = 10
TextBox
CommandButton
(Name) = tbPointAx
(Name) = cbCreateLine
Text = 0
WS-91
Step 7: Edit the Click Event of the Command Button
Double-click the Create
Line command button on the
form.
WS-92
9a. Insert the Code from the Create_Sketch_01
Rather than redevelop code that creates a line in a sketch, well just cut and
paste the code previously developed into the Click event of this form. Double-
click the Create_Sketch_01 module to bring its code window up. Select all
the code in the code window (other than the Sub CATMain() at the
beginning and End Sub at the end and hit Ctrl + C.
WS-93
9b. Insert the Code from the Create_Sketch_01
Double-click on frmCreateLine
on the project tree to bring up the
form.
WS-94
Step 10. Edit the Pasted CodeStep
The goal is to edit the code that has been pasted so that when the Create Line button is
clicked, a line gets created whose start point has the coordinates of Point A and whose end
point has the coordinates of Point B
The arrows below show the correspondence between the fields of the form and the
parameters of the methods that are called in the code.
===============================================
Creating Sketch Geometry
===============================================
part1.InWorkObject = sketch1
WS-95
Step 10. Edit the Pasted CodeStep (cont.)
WS-96
Step 11. Save the VBA project and run the macro
Highlight the VBA library on the project tree and save your changes
Close the VBA window, open a new part, and open the macros dialog
through Tools > Macro > Macros, and run the Show_CreateLine_Form
macro.
WS-97
Step 12a. Create a sketch using the form
Click the Create Line button and you will see that a sketch appears
containing a line whose coordinates match those that are in the form TextBox
objects. However, the form remains open, and if you attempt to interact with
CATIA you are unable because the form has been shown in what is called
modal mode. It would be nice to change this so that after a sketch is created,
you could zoom in or rotate, expand the spec tree, etc.
WS-98
Step 12b. Create a sketch using the form
Sub CATMain()
frmCreateLine.Show vbModeless
End Sub
Save the project, close the VBA editor, and run the
Show_CreateLine_Form macro. Note that even before you hit the
Create Line button you are able to interact with CATIA (zoom in, rotate,
etc.). Change point coordinate values on the form and hit the Create
Line button to create another sketch.
WS-99
Workshop 7 Conclusion
WS-100