Wednesday, October 12, 2011

How to automatically take a picture using a catscript

Catscripts can be a powerful tool when needing to automate processes in CATIA V5. One common process which is helpful to automate is when you want to take a screenshot of a .catpart or .catproduct with a white background (to put into a powerpoint or Excelfile). This sounds like a daunting task if you are new to VB programming. I find the best thing to do is to break down this large, challenging project into smaller, more manageable tasks. Let's think about each stage we want the macro to complete:
1. Reframe on the part
2. Hide the specification tree
3. Change the background color to white
4. Take a screenshot and save it as a JPEG file
5. Change the background display color back to what it was
6. Unhide the specification tree
Now, if we take each of those steps one at a time it's not so big of a problem. I'll be explaining each of these steps in the next couple of posts starting with step one today- how to automatically reframe (or fit all in) on the part in CATIA. We'll create each step individually then combine them all into one CATScript at the end.  This tutorial will also help you learn visual basic programming and will help you write your own CATScripts.
First, go to Tools>Macro>Macros. Click "Create" and change the Macro language to CATScript. Enter a name for your macro. Copy and paste the following code.

Language="VBSCRIPT"
Sub CATMain()


Dim specsAndGeomWindow1 As Window

Set specsAndGeomWindow1 = CATIA.ActiveWindow


Dim viewer3D1 As Viewer

Set viewer3D1 = specsAndGeomWindow1.ActiveViewer


viewer3D1.Reframe


Dim viewpoint3D1 As Viewpoint3D

Set viewpoint3D1 = viewer3D1.Viewpoint3D


End Sub

What does all of the above actually mean? A CATIA VBA program or “macro” consists of a
“Subroutine” named CATMain(), called out with the line Sub CATMain(). Variables are “dimmed” (declared) and then “set” (given a value). Variables are “dimmed” as a type. A type is either a “primitive” type (single, double, integer, string, etc.) or an object type (more complex). 

Stay tuned to learn more in our next article... 

17 comments:

  1. Very nice. I would like to have some to:

    - write in part
    - export part name, material and weight to catdrawing when create.

    but, with this one, it gives me this error:

    CNEXT - CATScript Error Message - ScriptingERR_1002

    http://dl.dropbox.com/u/15932461/CATScript%20Error.jpg

    What the problem

    ReplyDelete
  2. I'd have to see the entire code to help you out.

    ReplyDelete
  3. I make everything as you said, but i didn't create as catscript language. But now i run macro and nothing happens.

    I was thinking that after run macro, you can simply paste as image for example in powerpoint...

    ReplyDelete
  4. Here's the whole code. Posted above is just a portion of it. This should work. For now I save the images in a file but you could change it to export directly to xls or ppt.

    Sub CATMain()

    Dim ObjViewer3D As Viewer3D
    Set objViewer3D = CATIA.ActiveWindow.ActiveViewer

    Dim objCamera3D As Camera3D
    Set objCamera3D = CATIA.ActiveDocument.Cameras.Item(1)

    objViewer3D.Viewpoint3D = objCamera3D.Viewpoint3D

    'Input box to name the screen capture image file
    Dim partname As String
    partName = Inputbox ("Please name the image.")

    'reframe
    objViewer3D.Reframe()

    'zoom in
    objViewer3D.ZoomIn()
    objViewer3D.ZoomIn()

    '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)

    'clear selection for picture
    CATIA.ActiveDocument.Selection.Clear()

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

    'file location to save image
    Dim fileloc As String
    fileloc = "C:\"

    Dim exten As String
    exten = ".bmp"

    Dim strName as string
    strname = fileloc & partName & exten

    'MsgBox strname

    'take picture
    objviewer3D.Capturetofile 4,strname

    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 Sub

    ReplyDelete
    Replies
    1. I know this is a pretty old thread.. but pretty good thread.. how do I resize/Crop the picture to let's say 500x300 pixels..?

      Delete
    2. This comment has been removed by the author.

      Delete
  5. WOW.. this is a bit different from that code that you put in your post!

    But, now it's working good. Just made some modifications, like save as jpg and export to desktop!

    How can i learn to do macros? i have a few ideas, that they can do my work faster!!!

    ReplyDelete
  6. Just curious: what is a purpose of
    objViewer3D.Viewpoint3D = objCamera3D.Viewpoint3D ?

    ReplyDelete
  7. There are a few steps and methods to change the viewpoint of a CATIA document. The object “SpecsAndGeomWindow” contains a 2 or 3D viewer as well as a specification tree viewer. First, you need to access the 3D viewer from the current CATIA window:


    Dim objViewer3D As Viewer3D
    Set objViewer3D = CATIA.ActiveWindow.ActiveViewer

    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:



    Dim objCamera3D As Camera3D

    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

    Does that help?

    ReplyDelete
  8. What is the code for material detail

    ReplyDelete
  9. hello,

    if you want i make some modif and it work^^

    ReplyDelete
  10. hi,

    I didn't manage to save the file, is there a particular "Save" code missing?

    Thanks

    ReplyDelete
  11. I need a macro to change the color of the drafting background from white to gray. We are using the 3DVIA Composer. Any help would be much appreciated.

    ReplyDelete
    Replies
    1. Any way to contact you to send it will be appreciated

      Delete
  12. Hi,

    I'm trying to create images down a Product structure of the parts, you wouldn't happen to have an easy looping through product structure to then run your macro would you

    Regards

    ReplyDelete
    Replies
    1. Hi. See this: http://www.scripting4v5.com/additional-articles/screen-shot-macro/#comment-73007

      Delete

I'd love to hear from you!