0% found this document useful (0 votes)
89 views6 pages

New Body CATIA Macro Tutorial

The document provides a tutorial for creating a CATIA macro to generate a new part. It explains how to record macros to insert a new body, save the part, and change the part name. The recorded macros are then combined and edited into a single macro that prompts the user for a part name, generates a new part with that name, sets the part number, and saves the part file in a specified folder. Final usable code for the new part CATIA macro is presented.

Uploaded by

Việt Đỗ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views6 pages

New Body CATIA Macro Tutorial

The document provides a tutorial for creating a CATIA macro to generate a new part. It explains how to record macros to insert a new body, save the part, and change the part name. The recorded macros are then combined and edited into a single macro that prompts the user for a part name, generates a new part with that name, sets the part number, and saves the part file in a specified folder. Final usable code for the new part CATIA macro is presented.

Uploaded by

Việt Đỗ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

New body CATIA macro tutorial

This post is about basic CATIA programming knowledge.

Introduction
In this tutorials section, I will post simple macros, so you can learn some basic stuff. In this first
post, I will talk about basic stuff, like how to record a macro and make some changes. The best
way to learn how to make macro is to record one. Also, you can record more than one and then
join them into one complex. To record macro go to Tools/Macros/Start recording and after that
select CATScript and Start recording.
An important rule is when u recording macro to not make unnecessary clicks. So let’s make a macro
for inserting a new body. I will insert a new body and this action will be saved so we can stop
recording after that. You will get this cod.

Edit

Language="VBSCRIPT"

Sub CATMain()

Dim partDocument1 As Document

Set partDocument1 = CATIA.ActiveDocument

Dim part1 As Part

Set part1 = partDocument1.Part

Dim bodies1 As Bodies

Set bodies1 = part1.Bodies

Dim body1 As Body

Set body1 = bodies1.Add()

next

End Sub

This new body CATIA macro code is very simple so you need to understand it first. Sub CATMain()
is the main function, it always goes on the first place of the code, in the end you have End Sub for
end of that main function. Inside this function, you have the code for making a new body. Dim is
used for the declaration of variables. You need to declare a variable before you use it. After
declaration, you need to set value for the variable.
In my case, partDocument1 is declared as Document, but it can be named differently. You can give
names like TestDocument, Test or whatever you want to set. The value of variable partDocument1 is
CATIA.ActiveDocument means that this variable is an active document. It can be any active
document like part, product, etc.
In the same way, we define part1, first, we declare it as Part and then we set value for the part. Part1
is CATIA.ActiveDocument and at the same time CATIA Part. Here you can see that kind of active
document is very important. You will get an error If you try to run this macro in the product. In the
end, we declared the body as part1.bodies. Also, u can write just one declaration like
bodies1=CATIA.ActiveDocument.Part.Bodies

Editing macro
In the end when we understand this recorded new body CATIA macro code we can change if for a
bit. For example, we can add the name of the body and for the loop to create more bodies.
Add-on for VB code is making error it converts “&” to “&”

Edit
Edit
Language="VBSCRIPT"
Sub CATMain()
Dim partDocument1 As Document
Set partDocument1 = CATIA.ActiveDocument
Dim part1 As Part
Set part1 = partDocument1.Part
for i=1 to Num_B
Dim bodies1 As Bodies
Set bodies1 = part1.Bodies
Dim body1 As Body
Set body1 = bodies1.Add()
body1.name=Name_B & "__" & i
next
End Sub

We made just simple changes, we use simple for loop and name property for the body. In this case, we
can’t control this for loop, to do that we must use InputBox.
Dim Num_B as Integer
Num_B = InputBox(“Number of Bodies”)

Final code
Add-on for VB code is making error it converts “&” to “&”

Language="VBSCRIPT"
Sub CATMain()
Dim partDocument1 As Document
Set partDocument1 = CATIA.ActiveDocument
Dim part1 As Part
Set part1 = partDocument1.Part
Dim Num_B as Integer
Num_B = InputBox("Enter number of bodies")
Dim Name_B as Integer
Name_B = InputBox("Enter name for bodies")
for i=1 to Num_B
Dim bodies1 As Bodies
Set bodies1 = part1.Bodies
Dim body1 As Body
Set body1 = bodies1.Add()
body1.name=Name_B & "__" & i
next
End Sub

This is our final new body CATIA macro, you can just copy-paste this code. If you have any
questions feel free to contact me.
Check my YouTube channel for more cool videos CATIA Tutorials or section on this site for free
macros.
New Part CATIA macro tutorial
Learn how to make simple but very useful CATIA macros.

Introduction
First of all, making a new part in every CATIA session is like a must to take action. Certainly
using an integrated new part option is not very acceptable. It is due to many reasons. Also
defining the name of the part is not practical. Even more, you must save this file to a folder and
so on. As a result, I decided to show you how to make a new part CATIA macro in this tutorial.

Recording New Part CATIA macro


The best and easiest way to make the new part CATIA macro is to record one. So after recording you
can edit it. Therefore let’s record this macro then. So we got this code.

Language= "VBSCRIPT"
Sub CATMain()
Dim documents1 As Documents
Set documents1 = CATIA.Documents
Dim partDocument1 As Document
Set partDocument1 = documents1.Add( "Part" )
End Sub

Certainly, this is a very simple code to understand. If you want to read even more about the
declaration of variables you can do it on this link. First of all, a very important line for us in this
macro is “Set partDocument1 = documents1.Add(“Part”)”. This line code defines making the new
part and part name. Also, let’s record saving this macro in the folder.
Language= "VBSCRIPT"
Sub CATMain()
Dim partDocument1 As Document
Set partDocument1 = CATIA.ActiveDocument
partDocument1.SaveAs "C:\Users\Me\Desktop\New folder\Part1.CATPart"
End Sub

Also, we need to record one more macro for changing the name of this part.
Language= "VBSCRIPT"
Sub CATMain()
Dim partDocument1 As Document
Set partDocument1 = CATIA.ActiveDocument
Dim product1 As CATBaseDispatch
Set product1 = partDocument1.GetItem("Part5")
product1.PartNumber = "dd"
End Sub

This line of code “Set product1 = partDocument1.GetItem(“Part5″)” is very important. We


don’t want “Part5”, because it will work only for that part.

Editing New Part CATIA macro


As a result, we have three macro codes. So let’s combine them into one new macro. Consequently,
you can see we have some same lines in these three macros. So we can add the last two lines of code
from the second macro to the first. In contrast to the third macro, we must make a change. So we
must change “Part5” with Partdocument.part.name.

Language="VBSCRIPT"
Sub CATMain()
Dim documents1 As Documents
Set documents1 = CATIA.Documents
Dim partDocument1 As Document
Set partDocument1 = documents1.Add("Part")
Set partDocument1 = CATIA.ActiveDocument
Set product1 = partDocument1.GetItem(partDocument1.part.name)
product1.PartNumber = "dd"
partDocument1.SaveAs "C:\Users\Me\Desktop\New folder\Part1.CATPart"
End Sub

Hence try to run this code now. It will make a new part and save it to the location. In my case
location is “C:\Users\Me\Desktop\New folder\Part1.CATPart”.Finally, we want to edit that part
name. Due we will use an input box. So we will ask a user to input part name information for this
new part CATIA macro.
Dim Part_name as string
Part_name = InputBox("Please enter part name and use define profile")

Certainly very good practice is to previously define name profile like.


00_00_PART_NAME_________________00-000
Therefore a user needs to copy this profile and just edit it. As a result, you will get very
organized part names. Finally, we just need to change two more things and we got a new part
CATIA macro.

Final code for New Part CATIA


macro
Final usable code for New Part CATIA macro.
Language="VBSCRIPT"
Sub CATMain()
Dim Part_name as string
Part_name = InputBox("Please enter part name and use define profile")
Dim documents1 As Documents
Set documents1 = CATIA.Documents
Dim partDocument1 As Document
Set partDocument1 = documents1.Add("Part")
Set partDocument1 = CATIA.ActiveDocument
Set product1 = partDocument1.GetItem(partDocument1.part.name)
product1.PartNumber = Part_name
partDocument1.SaveAs "C:\Users\Me\Desktop\New folder\" & Part_name &
".CATPart"
End Sub

New Part CATIA macro tutorial


Learn how to make simple but very useful CATIA macros

You might also like