30
Getting Started with KBE: Working With the CATIA / Visual Basic Interface Nathan Clark and Tom Schneider Vought Aircraft Industries November 14, 2003

presentation_schneider_111403

Embed Size (px)

Citation preview

Getting Started with KBE: Working With the

CATIA / Visual Basic Interface

Nathan Clark and Tom SchneiderVought Aircraft Industries

November 14, 2003

Slide 2

12/2/2003

AGENDAè Automate CATIA? HOW? WHY?

– Discuss CATIA V5 Automation Approaches– What about Knowledgeware?

è Macro / .CATScript Demonstration– Recording Macros– Review and Reuse Recorded Code– Introduce Automation Objects in CATIA V5

è VBA Project Demonstration– Introduce VBA Editor Included with CATIA V5– Create User Interface Using VBA Form– Incorporate User Input in Existing Code as Variable

è Summary and Questions

Working with the CATIA / VB Interface

Slide 3

12/2/2003

Working with the CATIA / VB InterfaceèCATIA V5 Automation Approaches

– Macros / .CATScripts¬ Free, Rapid Deployment, Easy to Reuse Recorded

macros, Limited Flexibility, Difficult to Debug– VBA / .CATvba

¬ Free, Graphical User interface (Forms), Editor Provides Debugging Capability

– VB6 & VB.NET¬ Runs Outside of CATIA, Increased Implementation of

Components and Type Libraries, Individual Module Files Makes Collaboration and Sharing Code Easier

– CAA¬ More Powerful and Faster than VB, Harder to Learn,

Provides Access to Interfaces Not Currently Available to Visual Basic

Slide 4

12/2/2003

Working with the CATIA / VB InterfaceèAutomation Versus CATIA V5 Knowledgeware

– Knowledgeware Products¬ Pros: Integrated Into CATIA, Persistent, Supported and

Documented by Dassault.¬ Cons: Complex Models Difficult to Maintain, Lacks

Debugging Help, Requires Additional Licenses.– Automation Using Visual Basic

¬ Pros: External to CATIA, Graphical User Interface, Coding Editor, Functions Without Existing Seed Model, Supports Large Complex Applications.

¬ Cons: Not Persistent, All CATIA Functionality Not Exposed, Limited Applicable Documentation.

– Combining Both Approaches¬ With Some Creativity, You can Get The Best Of Both

Worlds to Produce Efficient and Powerful Applications.

Slide 5

12/2/2003

Macro / .CATScript Demonstration

Working with the CATIA / VB Interface

Slide 6

12/2/2003

èRecording Macro Demo– Tools / Macro / Start Recording…

– Macro Libraries: Directories – folder location to store recorded macrosVBA Products – recorded code inserted into VBA project

– Language Used:MS VBScript – does not include dim statementsCATScript – includes dim statements (preferred)

Working with the CATIA / VB Interface

Slide 7

12/2/2003

Working with the CATIA / VB InterfaceCreating RUG Demo

è Start Recording Macro (CATScript)– Open New CATPart– Create Three (3) Closed

Sketches on XY Plane– Create Three (3) Pads

From Sketches– Apply Graphic Color To

The Pads– No Show the Origin Planes– Apply “Fit-All-In” Button

to Reframe Padsè Stop Recording Macro

Slide 8

12/2/2003

Working with the CATIA / VB InterfaceèLet’s Look At The Recorded Code

– Open text-file using built-in editor or use any simple text editor (such as WinWord).

– Lines that begin with ‘ have been manually added. These lines are called comment lines and are skipped during runtime.

– All measurements are in millimeters and not inches (multiply or divide by 25.4 to convert).

– Lines containing “ReportName” have no discernable function and can be safely deleted.

– Sometimes the recording of a particular operation will be blank.This does not necessarily mean that coding is impossible, just more challenging.

– Tip: You must exit a sketch before stopping the recording or else the macro will be blank.

Slide 9

12/2/2003

Working with the CATIA / VB InterfaceèLet’s Look At The Modified Code

– Additional comment lines have been added.

– The code responsible for creating geometry in all three sketcheshave been replaced with new code that creates customized geometry.

– Lines containing “ReportName” have been deleted.

– A section of code has been re-written (condensed) into more efficient code.

– Since the as-recorded code did not record applying colors to the pads and no-showing the origin planes, the appropriate code has been inserted here.

– To tweak the orientation of each completed pad, the values assigned to the ‘SetAbsoluteAxisData’ array for each sketch was modified. (advanced topic).

Slide 10

12/2/2003

Working with the CATIA/ VB Interfaceè Introduction to Automation Objects in CATIA V5

Dim documents1 As DocumentsSet documents1 = CATIA.Documents

Dim partDocument1 As PartDocumentSet partDocument1 = documents1.Add("Part")

Dim part1 As PartSet part1 = partDocument1.Part

Dim bodies1 As BodiesSet bodies1 = part1.Bodies

Dim body1 As BodySet body1 = bodies1.Item("PartBody")

Dim sketches1 As SketchesSet sketches1 = body1.Sketches

Dim originElements1 As OriginElementsSet originElements1 = part1.OriginElements

Dim reference1 As ReferenceSet reference1 = originElements1.PlaneXY

Dim sketch1 As SketchSet sketch1 = sketches1.Add(reference1)

Slide 11

12/2/2003

Working with the CATIA/ VB Interfaceè Introduction to Automation Objects in CATIA V5

Dim documents1 As DocumentsSet documents1 = CATIA.Documents

Dim partDocument1 As PartDocumentSet partDocument1 = documents1.Add("Part")

Dim part1 As PartSet part1 = partDocument1.Part

Dim bodies1 As BodiesSet bodies1 = part1.Bodies

Dim body1 As BodySet body1 = bodies1.Item("PartBody")

Dim sketches1 As SketchesSet sketches1 = body1.Sketches

Dim originElements1 As OriginElementsSet originElements1 = part1.OriginElements

Dim reference1 As ReferenceSet reference1 = originElements1.PlaneXY

Dim sketch1 As SketchSet sketch1 = sketches1.Add(reference1)

Slide 12

12/2/2003

Working with the CATIA/ VB InterfaceèRecorded Macro Can Be Coded More Efficiently

Dim reference2 As ReferenceSet reference2 = part1.CreateReferenceFromObject(sketch1)

Dim pad1 As PadSet pad1 = shapeFactory1.AddNewPadFromRef(reference2, 0.1 * 25.4)

Dim reference1 As ReferenceSet reference1 = part1.CreateReferenceFromName("")

Dim pad1 As PadSet pad1 = shapeFactory1.AddNewPadFromRef(reference1, 20.000000)

Dim reference2 As ReferenceSet reference2 = part1.CreateReferenceFromObject(sketch1)

pad1.SetProfileElement reference2

Dim limit1 As LimitSet limit1 = pad1.FirstLimit

Dim length1 As LengthSet length1 = limit1.Dimension

length1.Value = 2.540000

As Recorded Code:Creates Generic Pad; Then Assigns Sketch and Length Parameters.

Efficient Code:Assigns Sketch and Length Parameters While Creating Pad.

Slide 13

12/2/2003

Working with the CATIA/ VB InterfaceèReference CATIA Automation Documentation

For Shape Factory Methods

Slide 14

12/2/2003

Working with the CATIA/ VB InterfaceèBecome Familiar With The Selection Object

'APPLY COLORSCATIA.ActiveDocument.Selection.ClearCATIA.ActiveDocument.Selection.Add pad1CATIA.ActiveDocument.Selection.Add pad2CATIA.ActiveDocument.Selection.VisProperties.SetRealColor 151, 150, 155, 0CATIA.ActiveDocument.Selection.ClearCATIA.ActiveDocument.Selection.Add pad3CATIA.ActiveDocument.Selection.VisProperties.SetRealColor 50, 78, 139, 0

'NO SHOW ORIGIN PLANESCATIA.ActiveDocument.Selection.ClearCATIA.ActiveDocument.Selection.Add part1.OriginElements.PlaneXYCATIA.ActiveDocument.Selection.Add part1.OriginElements.PlaneYZCATIA.ActiveDocument.Selection.Add part1.OriginElements.PlaneZXCATIA.ActiveDocument.Selection.VisProperties.SetShow CatVisPropertyNoShowAttrCATIA.ActiveDocument.Selection.Clear

The Selection Object is Extremely Important When Working With User Interfaces

The Selection Object Has a Property Called VisProperties that manages the graphical properties of all elements in the current selection.

Slide 15

12/2/2003

Working with the CATIA / VB Interface

Slide 16

12/2/2003

VBA Project Demonstration

Working with the CATIA / VB Interface

Slide 17

12/2/2003

Tools > Macro > Visual Basic Editor (Alt + F11)

Working with the CATIA / VB Interface

Slide 18

12/2/2003

Working with the CATIA / VB InterfaceèA VBA Project is made up of a set of

components that eventually compile into a single application– Forms– Modules– Class Modules

èThere must be a CATMain subroutine used as a start-up procedure

èExpanded power and flexibility – and the best part…ITS FREE

Slide 19

12/2/2003

è VBA Project - Form– An encapsulated, visual interface

containing a set of controls which allow the user to interact with the application

– Event Driven¬ Click, DoubleClick, Initialize,

GotFocus Events¬ Use events to run specific code

and/or change the look and feel of the form

– Can contain form-level declaration of Variables, Functions, or Procedures

– Code in a form is usually specific to the application using the form

Working with the CATIA / VB Interface

Slide 20

12/2/2003

èVBA Project - Module– A container for code in the form

of Subroutines or Functions– Can contain Global or Module-

level declarations of Variables, Constants, or Procedures

– Code without external dependencies can be used regardless of the application containing the module

Working with the CATIA / VB Interface

Slide 21

12/2/2003

Insert > ModuleInsert > UserForm Paste Script into Module1

Working with the CATIA / VB Interface

Slide 22

12/2/2003

Change Name Property

Drag Controls onto the Form

Label

Text Box

Command Button

(Double Click Here to add code)

èCreate an input form

Working with the CATIA / VB Interface

Slide 23

12/2/2003

Working with the CATIA / VB Interfaceè Write the code that executes when the button is clicked

Define a variable for the Pad Thickness

Store the value in the text box (convert it to a double)

Pass the value to the subroutine in RugDemoModule

Slide 24

12/2/2003

èModify existing script

Customize Name Property

Rename Script Routine and Add a Passed in Argument

Create CATMain routine to display the form

Working with the CATIA / VB Interface

Slide 25

12/2/2003

è Replace Hard Code thickness values with a variable

Insert Variable Name (Convert to mm)

Working with the CATIA / VB Interface

Slide 26

12/2/2003

Working with the CATIA / VB Interfaceè Some Variables must be late-bound in a VBA Project

Comment variable type declarations as required to prevent errors

Slide 27

12/2/2003

Working with the CATIA / VB Interface

Slide 28

12/2/2003

Hands On Summary

è Macros / .CATScripts– Great tools for rapid deployment of simple automation

applications– Personal time saving operations– They are limited in scope and flexibility.

è VBA Projects– Offer increased flexibility and complexity through

implementation of user interface forms and modules– Requires a short learning curve

Working with the CATIA / VB Interface

Slide 29

12/2/2003

QUESTIONS: