• No results found

Additional Information, Help, and Examples Useful Windows Functions Useful Windows Functions

In document VB Scripting for CATIA V5 eBook (Page 48-60)

There are many other useful functions that are available through making calls to Windows libraries. These are available from Microsoft’s website. Some examples:

• Sleep – insert a delay into the VB application

• Killing processes (like CNEXT to instantly stop CATIA)

• Checking for processes (to see if something is running)

Complete Macro Examples

It’s 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!

Return the Parent Name

'Script by Nick Weisenberger 'Revised 11/7/11

Set SelectedElement = CATIA.ActiveDocument.Selection

On Error Resume Next

Dim oSelElem

Set oSelElem = SelectedElement.Item(1).Value

49 Dim info

Set info = oSelElem.Parent.Parent

If Err.Number=0 Then

MsgBox "Parent Name = " & info.Name

Else

Msgbox "You must select an object to return the parent name."

End If

End Sub

Export Spec Tree

'Script by Nick Weisenberger 'Revised November 18, 2011

'this script exports the specification tree to Excel or

‘txt file based upon user input

'=======================================

Language="VBSCRIPT"

Sub CATMain()

Dim productDocument1 As Document

Set productDocument1 = CATIA.ActiveDocument

'Input box to select txt or xls Dim exportFormat As String

exportFormat = Inputbox ("Please choose format to export the tree as. Type either 'xls' or 'txt'")

50

IF exportFormat <> "xls" THEN

IF exportFormat <> "txt" THEN

msgbox "Did not enter txt or xls. Program cancelled, please retry macro."

Else

'Input box to enter name of file Dim partName As String

partName = Inputbox ("Please enter the file name.")

'Input box to enter file location Dim oLocation As String

'oLocation = Inputbox ("Please enter the location to save the file. Example: C:\temp\")

oLocation = "C:\Macro Files\"

productDocument1.ExportData oLocation & partName & "." &

exportFormat, "txt"

End If End If End Sub

Screen Capture - Complete Code

'Script by Nick Weisenberger 'Last revised January 10, 2012

'This macro takes a screen shot with a white background

‘and saves it in a folder

'==================================================

Sub CatMAIN()

Dim ObjViewer3D As Viewer3D

Set objViewer3D = CATIA.ActiveWindow.ActiveViewer

Dim objCamera3D As Camera3D

51 Set objCamera3D = CATIA.ActiveDocument.Cameras.Item(1)

'Input box to name the screen capture image file Dim partname As String

partName = Inputbox ("Please name the image.")

If partName="" Then

MsgBox "No name was entered. Operation aborted.", vbExclamation, "Cancel"

Else

'turn off the spec tree

Dim objSpecWindow As SpecsAndGeomWindow Set objSpecWindow = CATIA.ActiveWindow objSpecWindow.Layout = catWindowGeomOnly

'Toggle Compass

CATIA.StartCommand("Compass")

'change background color to white Dim DBLBackArray(2)

objViewer3D.GetBackgroundColor(dblBackArray) Dim dblWhiteArray(2)

dblWhiteArray(0) = 1 dblWhiteArray(1) = 1 dblWhiteArray(2) = 1

objViewer3D.PutBackgroundColor(dblWhiteArray)

'file location to save image Dim fileloc As String

fileloc = "C:\Macro Files\"

Dim exten As String exten = ".png"

Dim strName as string

strname = fileloc & partName & exten

52

'=======================================================

If MsgBox ("To reframe and automatically switch to ISO view click Yes. To take the image as shown on screen click No.", vbYesNo) = vbYes Then

objViewer3D.Viewpoint3D = objCamera3D.Viewpoint3D

'reframe

objViewer3D.Reframe() 'zoom in

objViewer3D.ZoomIn() 'objViewer3D.ZoomIn()

'clear selection for picture

CATIA.ActiveDocument.Selection.Clear()

'increase to fullscreen to obtain maximum resolution objViewer3D.FullScreen = True

'MsgBox strname

'take picture with auto ISO view and reframe ON***

objviewer3D.Capturetofile 4,strname

'****take picture as is with NO reframe or iso view****

Else zoom in

objViewer3D.ZoomIn()

'clear selection for picture

CATIA.ActiveDocument.Selection.Clear()

'increase to fullscreen to obtain maximum resolution objViewer3D.FullScreen = True

53 'take picture

objviewer3D.Capturetofile 4,strname

End If

'*******************RESET**********************

objViewer3D.FullScreen = False

'change background color back

objViewer3D.PutBackgroundColor(dblBackArray) 'turn the spec tree back on

objSpecWindow.Layout = catWindowSpecsAndGeom

'toggle compass

CATIA.StartCommand("Compass")

End If End Sub

Cycle through 3D Annotation Data

‘Script by Nick Weisenberger

‘Last revised December 1, 2011 Sub CATMain()

Dim partDocument1 As PartDocument

Set partDocument1= CATIA.ActiveDocument partName = Left(partDocument1.name, Len(partDocument1.name) - 8)

Dim oAnnotationSets As AnnotationSets Set oAnnotationSets=

partDocument1.Product.GetTechnologicalObject("CATAnnotat ionSets")

54

Dim viewer1 As Viewer

Set viewer1= CATIA.ActiveWindow.ActiveViewer Dim viewpoint1 As Viewpoint3D

Set viewpoint1= viewer1.Viewpoint3D Dim ObjViewer3D As Viewer3D

Set objViewer3D = CATIA.ActiveWindow.ActiveViewer partDocument1.Selection.Clear()

numSets=oAnnotationSets.Count Dim CurAnnoSet As AnnotationSet

Set CurAnnoSet = oAnnotationSets.Item(1) numCap=CurAnnoSet.Captures.Count

msgbox "Number of annotation sets is: "&numSets& "

Number of captures is: "&numCap 'error handling

On Error Resume Next

Set partDoc = CATIA.ActiveDocument Set Part = partDoc.Part

If Err.Number=0 Then 'change text to black Dim oSel As Selection

Dim visProperties1 As INFITF.VisPropertySet Set oAnnotationSets = Part.AnnotationSets Set oSel = CATIA.ActiveDocument.Selection

Set objViewer3D = CATIA.ActiveWindow.ActiveViewer For IdxSet = 1 To oAnnotationSets.Count

Set oAnnotationSet = oAnnotationSets.Item(IdxSet) Set oAnnotations = oAnnotationSet.Annotations For IdxAnnot = 1 To oAnnotations.Count

Set oAnnotation = oAnnotations.Item(IdxAnnot) If Not oAnnotation Is Nothing Then

55 With oSel

.Clear

.Add oAnnotation

Call .VisProperties.SetVisibleColor(0, 0, 0, 0) End With

Set oCapture = CurAnnoSet.Captures.Item(i) oCapture.DisplayCapture()

capNamer=CurAnnoSet.Captures.Item(i).Name

Set objViewer3D = CATIA.ActiveWindow.ActiveViewer

Set objCamera3D = CATIA.ActiveDocument.Cameras.Item(1) 'increase to fullscreen to obtain maximum resolution objViewer3D.FullScreen = True

fileloc = "M:\Macro Files\"

exten = ".png"

strname=fileloc&partName&" "&capNamer&exten 'take picture

objviewer3D.Capturetofile 4,strname Next 'i

End If

'change text back to white

For IdxSet = 1 To oAnnotationSets.Count

Set oAnnotationSet = oAnnotationSets.Item(IdxSet) Set oAnnotations = oAnnotationSet.Annotations For IdxAnnot = 1 To oAnnotations.Count

Set oAnnotation = oAnnotations.Item(IdxAnnot) If Not oAnnotation Is Nothing Then

56

With oSel .Clear

.Add oAnnotation

Call .VisProperties.SetVisibleColor(255, 255, 255, 255) End With

Msgbox "Not a part document! Open in new window."

End If End Sub

Help Documentation

The file B18\intel_a\code\bin\V5Automation.chm is a useful source for CATIA “Application Programming Interface” (or API) documentation. Navigate to the directory B18\intel_a\code\bin, right-click on the file V5Automation.chm, and choose “Create Shortcut”, which will create a shortcut to this file in this same directory. Move the shortcut to your desktop and double-click on it to open it.

After opening the .chm file, click on the “PartDocument” element of the diagram, which should navigate to the API documentation of the PartDocument class. The API documentation shows the inheritance tree, and a listing of the Properties and Methods of the PartDocument class. Note there is no “Close”

method listed. This is because the “Close” method is inherited from the Document class. To see all of the methods available for a particular class, you should check every class that it inherits from up to “AnyObject”. Classes above that are not of any interest. To navigate back to the CATIA object diagram, click on the

“Contents” tab, expand the “CAA V5 Visual Basic Reference” node, and select

“CAA V5 Objects.”

Online help documentation is another source of API documentation. The Automation Documentation is found under the “Infrastructure” Icon. There is also a special set of On-Line help that is available with the CAA RADE interfaces (it’s on this CD). A RADE license is NOT required to access the help files.

57 For additional help and examples, please visit http://scripting4v5.com or http://cadsystemshelp.blogspot.com.

Final Thoughts

Now you know how to write VBScript macros for CATIA V5! You’re on your way to automating those repetitive tasks and impressing your coworkers and bosses.

Good luck and happy programming!

-Nick Weisenberger

58

About the Author

Nick Weisenberger grew up in Columbus, Ohio and graduated from the University of Toledo, in Toledo, Ohio, with a degree in Mechanical Engineering.

An avid traveler, one of Nick’s favorite pastimes is visiting amusement parks all over the country. You can read more about his adventures at http://www.coaster101.com. He has also combined his love of coasters, engineering, and writing in his latest book Coasters 101: An Engineering Guide to Roller Coaster Design. He’s also written a non-fiction story about an engineer’s adventures in China, entitled Nike’s Chinese New Year. Nick currently works as an engineer and resides with his wife near Columbus, Ohio.

59 Appendix I: Acronyms

The following terms are used throughout this text (in alphabetical order):

API: Application Programming Interface

CAD: Computer Aided Design

CATIA: Computer Aided Three Dimensional Interactive Application

COM: Component Object Model

DLL: Dynamic Linked Library

DMU: Digital Mock-Up

GUI: Graphical User Interface

MDB: Model Based Definition

PBD: Product Based Definition

TLB: Type Library File

VBA: Visual Basic for Applications

Appendix II: Resources

For more information see the following list of resources:

1. VB Scripting for CATIA V5: http://www.scripting4v5.com 2. CAD Systems Help: http://cadsystemshelp.blogspot.com

3. Excel Spreadsheets Help: http://excelspreadsheetshelp.blogspot.com 4. CATIA V5: http://www.3ds.com

WS-1

In document VB Scripting for CATIA V5 eBook (Page 48-60)

Related documents