Extract part number and name CATIA macro
Introduction
Almost every serious project has a lot of parts inside. Hence you must order those parts and have some sort of stock-list. So two basic parameters are part number and part name. There are a lot of different ideas about how to do this specific task. In this post, we want to show you one interesting one. Also in this post, you will learn how to work with strings. For example how to take and extract string, how to split string etc. Therefore with this and CATIA macro knowledge, we will write code for extract part number and name CATIA macro.
Understanding string operations
Extracting part number
First of all our major goal in this process is how to work with strings. Even more, you can read a little bit on this topic http://catiadoc.free.fr/online/cfyugkwr_C2/cfyugkwrhlpString.htm. At the end of this page, you can download the part with examples inside.
All explained options on the link from before are here. First, we need to figure out how to take a string from a string. So we can use that extract option (String -> Extract(integer, integer): String). This is a classic split string option because first, you need string and then interval. Like in many other programming languages it starts from 0, not from 1.
01_200_00__EXAMPLE1___________________000000000000 (example part name).
We will extract two first numbers.
So we want to replace that “_” with “.”. `01_201_00__EXAMPLE1___________________000000000000\Part Number` ->Extract(0,2) + “.” . Now we just need to continue with extraction of other numbers and replacement.
You will get this at the end: `01_201_00__EXAMPLE1___________________000000000000\Part Number` ->Extract(0,2) + “.” +`01_201_00__EXAMPLE1___________________000000000000\Part Number` ->Extract(3,3)+ “.” +`01_201_00__EXAMPLE1___________________000000000000\Part Number` ->Extract(7,2) .
And the result of this code is: 01.201.00
Now we need to take the name from this part name string.
Extracting part name
In our case, first 11 places in the part name are for part number, after that 25 places are for part name. So we need to extract from 11 to 25. So extraction will start from 11th place of the string and go 25 strings forward.
`01_201_00__EXAMPLE1___________________000000000000\Part Number` ->Extract(11,25)
Resault: EXAMPLE1_________________
We need to remove those underlines, so we will search for “_” in this result string.
`01_201_00__EXAMPLE1___________________000000000000\Part Number` ->Extract(11,25) .Search(“_”)
Resault:8
In this case result is integer, and it gives number of letters not the number of “_”. But we can use this number for interval. There is a problem in this approach when you have something like “EXAMPLE_1____”. It will take only “EXAMPLE” not “EXAMPLE_1”. Consequently it is problem when you have those kind of names.
So let’s use that number and split this string again.
Result: EXAMPLE1
We combined it all in one relation. You can always make three steps, but you will need two additional parameters then.
Now we can write our macro code with this information.
Writing the macro
Sub CATMain() 'Standard declarations for part Dim partDocument1 As Document Set partDocument1 = CATIA.ActiveDocument Dim part1 As Part Set part1 = partDocument1.Part 'We need relation Dim relations1 As Relations Set relations1 = part1.Relations 'Declaration of the parameter for part number Dim par1 Set par1 = part1.Parameters.Item("Positionsnummer") Dim formula1 As Formula 'This is most important part of the code, that is same relation from before we just replace "01_201_00__EXAMPLE1___________________000000000000" with part1.name so it can work in any part Set formula1 = relations1.CreateFormula("Number", "Get number from name", par1, "`" & part1.Name & "\Part Number` ->Extract(0,2) + ""."" +`" & part1.Name & "\Part Number` ->Extract(3,3)+ ""."" +`" & part1.Name & "\Part Number` ->Extract(7,2)") formula1.Rename("Number") Dim relations2 As Relations Set relations2 = part1.Relations 'Same for second parameter Dim par2 Set par2 = part1.Parameters.Item("Produktbezeichnung") Dim formula2 As Formula Set formula2 = relations2.CreateFormula("Name", "Get name from name", par2, "`" & part1.Name & "\Part Number` ->Extract(11,25) ->Extract(0,`" & part1.Name & "\Part Number` ->Extract(11,25) .Search(""_""))") formula2.Rename("Name") part1.Update() End Sub
This code will work only in one part, but you can see how to write it.
You can also download this extracting part number and name CATIA macro and use it in the product.
If you have any issues feel free to contact me.
hello
I’m NC PGM engineer
I’m Using CATIA V5R26 control, i saw your macro, and i just want you to build or suggest for me a macro that can automatically numbering the operations in machining file, ex: i have 5 operations, that belong to 1 Subject
#1: Face/ RGH
#1.1: FACE/ RGH
#1.2:
#2: GRV/ RGH
….
The form is like above example, i mean after finish program, i just want to numbering all operations.
Hello, can you send me some machining file as example, I m not familier with those files. I ll sent you mail