Run CATIA macro from Excel
Run CATIA macro from Excel
A person who won’t read has no advantage over one who can’t read. Mark Twain
Introduction
How to run CATIA macro from Excel is very interesting topic. We already covered CATIA macro and Excel connections in this post. This will be an extension to that post. There are a lot of ways how can you run CATIA macro. There are also a lot of programming languages in which can you write CATIA macro.
On this link you can check basic ways to run CATIA macro. There are just a few simple ones.
On the web, there are not too many topics about how to run CATIA macro from Excel. Our goal is to present this topic in an easy way so you can use the benefits of this approach.
Why run CATIA macro from Excel?
There are a few reasons:
- store a lot of necessary input data
- easy way to change the input data
- use Excel advantages
Before we start with our macro, you can also check these posts on how to write macros in Excel.
Of course, there are a lot of posts about how to write Excel macro, so if you need more knowledge about that, you can search the web.
First Excel ...
Before we start with writing the macro, we need to cover some things in Excel.
- Start Excel Application
- Go to File > Options > Customize Ribbon and Check the Developer option.
- Go to Developer now and insert a new button
- When you draw the button, you will get this menu pop up
- You can go now on the New button to create a new macro. If you just click OK on this menu, it is possible to come back on this menu.
- You will get a new window Microsoft Visual Basic for Applications (VBA) where you can start to write the macro.
...and then CATIA macro
In this section, we can write more about macros. We will use our old macros from the website and just change some things. We will use three macros:
You can read more about these macros on the links. In this way, it is easier to understand the used macro in this example.
This is an Excel file with the created macros inside.
Code for New Part.
Sub Make_new_part() 'conn with CATIA Set CATIA = GetObject(, "CATIA.APPLICATION") Dim Part_name Part_name = Worksheets("Sheet1").Cells(8, 3).Value Dim documents1 Set documents1 = CATIA.Documents Dim partDocument1 Set partDocument1 = documents1.Add("Part") Set partDocument1 = CATIA.ActiveDocument Set product1 = partDocument1.GetItem(partDocument1.Part.Name) product1.PartNumber = Part_name partDocument1.SaveAs "C:\Harun\WEB\Design\33_Run_CATIA_macro_from_excel\" & Part_name & ".CATPart" End Sub
Code for color macro.
Sub Black_color() 'conn with CATIA Set CATIA = GetObject(, "CATIA.APPLICATION") Dim oSelection oSelection = CATIA.ActiveDocument.Selection.VisProperties.SetRealColor(0, 0, 0, 0) End Sub Sub White_color() 'conn with CATIA Set CATIA = GetObject(, "CATIA.APPLICATION") Dim oSelection oSelection = CATIA.ActiveDocument.Selection.VisProperties.SetRealColor(240, 240, 240, 0) End Sub
Code for Add new Body macro.
Sub Add_bodies() 'conn with CATIA Set CATIA = GetObject(, "CATIA.APPLICATION") Dim partDocument1 Set partDocument1 = CATIA.ActiveDocument Dim part1 Set part1 = partDocument1.Part Dim Num_B Num_B = Worksheets("Sheet1").Cells(8, 10).Value Dim Name_B Name_B = Worksheets("Sheet1").Cells(9, 10).Value For i = 1 To Num_B Dim bodies1 Set bodies1 = part1.Bodies Dim body1 Set body1 = bodies1.Add() body1.Name = Name_B & "__" & i Next End Sub
There are a lot of macros that you can create in Excel and benefit from it. These are just simple examples that help you better understand this topic.
Please watch the video for a better understanding.
You can download the Excel file here.
Hi,
You are doing a great work. I need to ask if the same applies also for the drawing workbench? so I mean can we make a macro in excel for texts in 2D Drawing? is it too difficult. Any comments are high appreciable. Thanks
Kind Regards
Samkit
Hi, thx a lot. Yes, I think there are no problems with that. I don’t know what do you want to archive?
The procedure is the same.
Hey your contact window has some sort of glitch
I have some doubts please mail me