New part CATIA macro tutorial + free code
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
You can see also my video tutorial, how to make this macro.
If you want to download free Visual Basic new part CATIA macro you can do it on this link.
Hi,
Please tell me what is the code to get Instance name using part number in a cat product using CATIA VBA.
Thanks,
Venkat Sai
Hello,
here are few lines of code that can help you.
Sub CATMain()
Dim oSel
Set oSel = CATIA.Activedocument.Selection
Dim strArray(0)
strArray(0) = “AnyObject”
Dim sStatus As String
sStatus = oSel.SelectElement3(strArray, “Select Part”, False, CATMultiSelectionMode.CATMultiSelTriggWhenUserValidatesSelection, False)
MsgBox oSel.item(1).value.name
MsgBox oSel.item(1).value.ReferenceProduct.Parent.Part.name
MsgBox oSel.item(1).value.parent.parent.name
End Sub
It depends on what do you want to select and what do you want to get.
Are you want to select that part from the user or?
In that case, code will be diferent.
Hi,
Please tell me what is the code to get make Custom templates option drafting using CATIA VBA.
Thanks,
SURENDHARAN