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
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).
“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...
Very nice. I would like to have some to:
ReplyDelete- 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
I'd have to see the entire code to help you out.
ReplyDeleteI make everything as you said, but i didn't create as catscript language. But now i run macro and nothing happens.
ReplyDeleteI was thinking that after run macro, you can simply paste as image for example in powerpoint...
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.
ReplyDeleteSub 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
This comment has been removed by the author.
DeleteHello, any one who know how to modify this macro to save the picture with the catpart name instead of the dialogue that is now in the macro?
DeleteChange the partName variable to something else.
DeleteWOW.. this is a bit different from that code that you put in your post!
ReplyDeleteBut, 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!!!
Just curious: what is a purpose of
ReplyDeleteobjViewer3D.Viewpoint3D = objCamera3D.Viewpoint3D ?
See the comment below.
DeleteThere 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:
ReplyDeleteDim 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?
What is the code for material detail
ReplyDeletehello,
ReplyDeleteif you want i make some modif and it work^^
hi,
ReplyDeleteI didn't manage to save the file, is there a particular "Save" code missing?
Thanks
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.
ReplyDeleteAny way to contact you to send it will be appreciated
DeleteHi,
ReplyDeleteI'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
Hi. See this: http://www.scripting4v5.com/additional-articles/screen-shot-macro/#comment-73007
Delete