Some questions about macro programing
Some questions about macro programing
(OP)
Hello everybody,
As you can see I'm new to this forum and I browsed it searching for my answers and unfortunately I didn't find it, so here I am!
-Can you change of workbench while you're executing a macro through the "CATIA.StartWorkbench ("name of workbench")?
Like I want to extract some datas in analysis workbench then change of workbench to extract mass, all of that in 1 macro
- What is the differences between Product and ArrangementProduct when you declare something? And sometimes I can see
- For the function .Search, I saw some ways to write it and I wanted to the differences and the conditions of writing one or the other:
- I saw several topics about finding the mass and the inertia through a macro which used the 2 sames ways as I saw on other sites, but when I tried it on my computer, none of them worked, so, in which cases do you use these 2 ways:
FYI, I'm working on a computer where the documentation of CATIA was erased
Sorry if bad english, not my first language.
Best regards
As you can see I'm new to this forum and I browsed it searching for my answers and unfortunately I didn't find it, so here I am!
-Can you change of workbench while you're executing a macro through the "CATIA.StartWorkbench ("name of workbench")?
Like I want to extract some datas in analysis workbench then change of workbench to extract mass, all of that in 1 macro
- What is the differences between Product and ArrangementProduct when you declare something? And sometimes I can see
objArrRun As Run
objArrRuns As Runs
Is there a difference? Putting an S won't make a difference?- For the function .Search, I saw some ways to write it and I wanted to the differences and the conditions of writing one or the other:
oSel.Search "'Assembly Design'.Part, all"
oroSel.Search "(CATLndSearch.Part),all"
Does the CAT***Search is depending on the workbench you're currently working in?- I saw several topics about finding the mass and the inertia through a macro which used the 2 sames ways as I saw on other sites, but when I tried it on my computer, none of them worked, so, in which cases do you use these 2 ways:
Dim TheSPAWorkbench As Workbench
Set TheSPAWorkbench = CATIA.ActiveDocument.GetWorkbench(“SPAWorkbench“)
Dim productInertia As Inertia
Set productInertia = TheSPAWorkbench.Inertia.Add(Product)
andSet TheSPAWorkbench = CATIA.ActiveDocument.GetWorkbench(“SPAWorkbench“)
Dim productInertia As Inertia
Set productInertia = TheSPAWorkbench.Inertia.Add(Product)
prdProduct As Part
Dim productInertia As Inertia
On Error Resume Next
Set productInertia = prdProduct.GetTechnologicalObject(“Inertia”)
Dim productInertia As Inertia
On Error Resume Next
Set productInertia = prdProduct.GetTechnologicalObject(“Inertia”)
FYI, I'm working on a computer where the documentation of CATIA was erased
Sorry if bad english, not my first language.
Best regards





RE: Some questions about macro programing
Second step will be to search for your V5Automation.chm file. it comes with the install of CATIA so you should have it. Then read it. it will answer most of your question.
now to your questions:
1: you might not need to switch workbench but navigate the document structure to get the info you need.
2: run and runs are not the same object, usually the xxxxxs object is a collection of xxxxx object. google VBA collection to see what is a collection.
3: the item before the .part is the workbench (App in 3DEXP) from which the object is defined from, selecting one or the other might impact result... you test it.
4: for more help on this, it would be nice to have your CATIA V5 release level and the error you have and location (you might have to remove the on error for that)
indocti discant et ament meminisse periti
RE: Some questions about macro programing
If I remove the On Error, I get the error "GetTechnologicalObject does not handle this property" (sorry, I translated literally because I'm working in French)
Actually the macro I found to extract mass, density and material, works weirdly: I have an assembly and I want to extract those datas, so I launch my macro and I get the Selection Palette Tool. I select everything, validates, Excel opens and I ony get the name of the selected parts.
BUT, if I select BEFORE on the specification tree, launch the macro and hit Escape, then everything pops in the Excel File
Here is the code if you wanna try it
CODE --> CATScript
Sub CATMain() '--Créer un tableau vide Dim table() '--Declarer Excel et l'ouvrir Dim Excel As Object Dim objWorkbook Dim objSheet Dim workbooks As workbooks Dim workbook As workbook Dim Sheets As Object Dim Sheet As Object Dim worksheet As Excel.worksheet Dim myworkbook As Excel.workbook Dim myworksheet As Excel.worksheet '--Verification de l'ouverture d'Excel On Error Resume Next Set Excel = GetObject(, "Excel.Application") If Excel Is Nothing Then IsExcelRunning = False ' l'utilisateur n'a pas ouvert Excel Err.Clear Else IsExcelRunning = True ' l'utilisateur a ouvert Excel End If '--Ouvrir Excel On Error Resume Next Set Excel = GetObject(, "Excel.Application") If Excel Is Nothing Then Set Excel = CreateObject("Excel.Application") Err.Clear Excel.Visible = True End If On Error GoTo 0 '--Ouvrir l'archive de la macro Set objWorkbook = Excel.Workbooks.Open("C:\Users\nmichelet\Desktop\test") objWorkbook.Activate Set objSheet = objWorkbook.WorkSheets.Item(1) '--Selectionner le produit Dim oSel As Selection Set oSel = CATIA.ActiveDocument.Selection ReDim strArray(0) strArray(0)="Part" Dim sStatus As String sStatus = oSel.SelectElement3(strArray, "Select parts", False, CATMultiSelTriggWhenUserValidatesSelection, true) Dim prdProduct as Part Dim iProduct '--Redimensionner le tableau ReDim table(oSel.Count,4) '--Compter le numero de produit et les recompter For iProduct=1 to oSel.Count Set prdProduct=oSel.Item(iProduct).Value '--Charger la part Dim objInertia as Inertia On Error Resume Next Set objInertia=prdProduct.GetTechnologicalObject("Inertia") Dim getMass As String getMass = objInertia.Mass Dim getDensity As String getDensity = objInertia.Density Dim partName As String partName=prdProduct.Name Dim mat As Object Dim oManager As MaterialManager Set oManager = prdProduct.GetItem("CATMatManagerVBExt") oManager.GetMaterial OnPart prdProduct,mat matName=mat.Name MsgBox mat.Name '--Remplir la ligne correspondante table (iProduct,1)=partName table (iProduct,2)=getMass table(iProduct,3)=matName table (iProduct,4)=getDensity Next '--Remplir Excel objSheet.Cells(3,1)="Part Number" objSheet.Cells(3,2)="Mass" objSheet.Cells(3,3)="Material" objSheet.Cells(3,4)="Density" For r=1 to oSel.Count objSheet.Cells(r+5,1)=table(r,1) objSheet.Cells(r+5,2)=table(r,2) objSheet.Cells(r+5,3)=table(r,3) objSheet.Cells(r+5,4)=table(r,4) 'MsgBox "Masse = " & getMass & "kg" Next End SubI watched the automation file but I still don't get when you use Product or ArrangementProduct
For the switch of workbenches: actually, I'm working on the Piping workbench, on Runs. Those runs don't have any physical datas on it (like mass or material, it's just a 3D drawing), so I'd like to get the coordinates datas of those runs, then switch to Assembly design to get the "physical" datas of the pipes and the anchorage for example.
For the .search method: which one is best? oSel.Search "'Assembly Design'.Part, all" or "(CATLndSearch.Part),all"?
What is the function of the .Parent? And .Move?
CODE --> CATScript
I got another question: if I want the coordinates of the center of gravity of a Part, what is the best way to get it?
Product.GetCOGPosition Coordinates?
And how does the procedure dblRunPoint works?
If I wasn't clear, let me know, so I can make my explanations better.
RE: Some questions about macro programing
RE: Some questions about macro programing
CODE -->
Set objInertia=prdProduct.Parent.Product.GetTechnologicalObject("Inertia")indocti discant et ament meminisse periti
RE: Some questions about macro programing
CODE -->
it should be
CODE -->
indocti discant et ament meminisse periti
RE: Some questions about macro programing
CODE -->
indocti discant et ament meminisse periti
RE: Some questions about macro programing
CODE -->
indocti discant et ament meminisse periti
RE: Some questions about macro programing
And for the Center of Gravity of a Part, how can I get it?
Thanks for your answers anyway :)
RE: Some questions about macro programing
did you apply the material on the part or on the body? your script look for material on part.
for center of gravity you should try
CODE -->
and work with the cogxyz array
indocti discant et ament meminisse periti
RE: Some questions about macro programing
For material: on the part I'm trying, the material is applied on Part, don't know why it doesn't show then :/
RE: Some questions about macro programing
indocti discant et ament meminisse periti
RE: Some questions about macro programing
RE: Some questions about macro programing
CODE -->
indocti discant et ament meminisse periti
RE: Some questions about macro programing
RE: Some questions about macro programing
indocti discant et ament meminisse periti
RE: Some questions about macro programing
CODE --> CATScript
'--Verification de l'ouverture d'Excel 'On Error Resume Next Set Excel = GetObject(, "Excel.Application") If Excel Is Nothing Then IsExcelRunning = False ' l'utilisateur n'a pas ouvert Excel Err.Clear Else IsExcelRunning = True ' l'utilisateur a ouvert Excel End IfRE: Some questions about macro programing
It tells me An ActiveX component cannot create an object "GetObject" on the line
Set Excel = GetObject(, "Excel.Application")
RE: Some questions about macro programing
CODE --> CATScript
But there is a 2nd part in my macro where I want to select all the parts so I take the loop again. But since I already got a selection, it won't go into the loop.
How can I clear my selection?
Before the 2nd part of the macro, I tried Set oSel = Nothing, Set oSel = null, Set oSel = 0, oSel.Clear none of that worked.
Any idea on that?
Ps: I still can't get the material
RE: Some questions about macro programing
RE: Some questions about macro programing
CODE --> CATScript
Dim objProductMat 'As Product Dim intNbParts As Integer Dim k As Integer intNbParts = objGCATIASelection1.Count For k = 1 To intNbParts Set objProduct = Nothing Set objProductMat = objGCATIASelection1.Item(k) Err.Clear On Error Resume Next Dim objInertia As Inertia On Error Resume Next Set objInertia = objProduct.GetTechnologicalObject("Inertia") Dim getMass As String getMass = objInertia.Mass Dim Mat As Material Dim oManager As MaterialManager Set oManager = objProductMat.GetItem("CATMatManagerVBExt") oManager.GetMaterialOnPart objProductMat,Mat matName = Mat.Name 'MsgBox matName Dim Coordinates(2) objInertia.GetCOGPosition CoordinatesEverything works fine except the material
RE: Some questions about macro programing
indocti discant et ament meminisse periti
RE: Some questions about macro programing
Is it possible, somehow, I don't have CATManagerVBExt?
RE: Some questions about macro programing
can you tell us what type is objProductMat ? is it a product? should it be a part ?
indocti discant et ament meminisse periti
RE: Some questions about macro programing
RE: Some questions about macro programing
it is bad VBA programming to use on error resume next everywhere for no good reason. I use this only to do missing VBA stuff like try / catch and always put a on error goto 0 to close the effect.
next question is what do you pass on objProductMat from your selection? a product or a part?
indocti discant et ament meminisse periti
RE: Some questions about macro programing
Each component of the assembly here is a product
Though the selection is
CODE --> CATScript
If objGCATIASelection1.Count = 0 Then objGCATIASelection1.Search "(CATLndSearch.Product),all" End IfRE: Some questions about macro programing
and you expect it to work?
indocti discant et ament meminisse periti
RE: Some questions about macro programing
So actually the line is oManager.GetMaterialOnProduct objProductMat,Mat, sorry for this mistake
RE: Some questions about macro programing
is it working now?
indocti discant et ament meminisse periti
RE: Some questions about macro programing
For information, I tried to apply material on the body of the part, on the part and on the product and none of that worked
RE: Some questions about macro programing
indocti discant et ament meminisse periti
RE: Some questions about macro programing
RE: Some questions about macro programing
indocti discant et ament meminisse periti
RE: Some questions about macro programing
Is there a special command to activate GetItem("CATMatManagerVBExt") before?
RE: Some questions about macro programing
indocti discant et ament meminisse periti
RE: Some questions about macro programing
CODE --> CATScript
Dim objProduct As Product Dim objProductMat As Part Dim intNbParts As Integer Dim k As Integer intNbParts = objGCATIASelection1.Count For k = 1 To intNbParts Set objProduct = Nothing Set objProductMat = Nothing Set objProduct = objGCATIASelection1.Item(k).Value Set objProductMat = objGCATIASelection1.Item(k) Err.Clear Dim objInertia As Inertia Set objInertia = objProduct.GetTechnologicalObject("Inertia") Dim getMass As String getMass = objInertia.Mass Dim partName As String partName = objProduct.Name Dim Mat As Material Dim oManager As MaterialManager Set oManager = objProductMat.Item(k).GetItem("CATMatManagerVBExt") oManager.GetMaterialOnProduct objProductMat,Mat matName = Mat.Name 'MsgBox matName(Since I got an Assembly with >200 elements on it, I commented MsgBox)
BTW, I didn't say it earlier, but thanks for your help :)
RE: Some questions about macro programing
indocti discant et ament meminisse periti
RE: Some questions about macro programing
CODE --> CATScript
Set objGCATIADocument0 = CATIA.ActiveDocument Set objGCATIASelection1 = objGCATIADocument0.Selection Set objGCATIAProduct1 = objGCATIADocument0.Product If objGCATIASelection1.Count = 0 Then objGCATIASelection1.Search "(CATLndSearch.Product),all" End IfMaybe I should use objCATIAProduct1 instead of objGCATIASelection1.Item(k)?
And I realise objGCATIAProduct1 is never used, so if not useful, I should erase it
RE: Some questions about macro programing
indocti discant et ament meminisse periti
RE: Some questions about macro programing
Set oManager = objProductMat.Item(k).GetItem("CATMatManagerVBExt")
oManager.GetMaterialOnProduct objProductMat,Mat
RE: Some questions about macro programing
Dim objProductMat As Part
or
Dim objProductMat As Product
RE: Some questions about macro programing
indocti discant et ament meminisse periti
RE: Some questions about macro programing
RE: Some questions about macro programing
CODE -->
does not find material on the product if the product is a part instance... you' ll have to work with the part
so use:
CODE -->
Be aware that this line will fail if objProductMat is not an instance
indocti discant et ament meminisse periti
RE: Some questions about macro programing
RE: Some questions about macro programing
indocti discant et ament meminisse periti
RE: Some questions about macro programing
RE: Some questions about macro programing
indocti discant et ament meminisse periti
RE: Some questions about macro programing
RE: Some questions about macro programing
ALL OF THEM
indocti discant et ament meminisse periti
RE: Some questions about macro programing
Ps: I'm sorry, I'm extracting other things and at each try I'm adding something, and I put the On Error back to try the other informations
RE: Some questions about macro programing
indocti discant et ament meminisse periti