Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Some questions about macro programing

Status
Not open for further replies.

AtomicNico

Nuclear
May 10, 2016
62
0
0
FR
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
objArrRun As Run​
objArrRuns As Run[highlight #EF2929]s[/highlight]​
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"​
or
oSel.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)​
and
prdProduct As Part
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
 
Replies continue below

Recommended for you

first step will be to get that documentation installed again. after all you paid for it and its really valuable and I can see any reason why not to have it.

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.

search_d8tqy3.png


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)

Eric N.
indocti discant et ament meminisse periti
 
My CATIA release is V5-6R2014

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:
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 Sub

I 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:
Dim objInstProduct 'As Product
Dim objRunParent As Products
Dim objRunParentSelf As Product
Dim objRunParentMove As Move
Set objRunParent = objInstProduct.Parent
Set objParentSelf = objRunParent.Parent
Set objRunParentMove = objRunParentSelf.Move

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.
 
you should take the inertia from the product not from the part

Code:
Set objInertia=prdProduct.[b]Parent.Product[/b].GetTechnologicalObject("Inertia")

Eric N.
indocti discant et ament meminisse periti
 
you did also split your line:

Code:
oManager.GetMaterial
OnPart prdProduct,mat

it should be

Code:
oManager.GetMaterialOnPart prdProduct,mat

Eric N.
indocti discant et ament meminisse periti
 
finally if you want to use the preselection, change the line :

Code:
sStatus = oSel.SelectElement3(strArray, "Select parts", [b]True[/b], CATMultiSelTriggWhenUserValidatesSelection, true)

Eric N.
indocti discant et ament meminisse periti
 
The material still doesn't appear but I have no idea why though

And for the Center of Gravity of a Part, how can I get it?

Thanks for your answers anyway :)
 
material works for me

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:
dim cogxyz(2)
objInertia.GetCOGPosition.cogxyz

and work with the cogxyz array

Eric N.
indocti discant et ament meminisse periti
 
I commented all of my On Error and the only one who give me an error is this one
Code:
'--Verification de l'ouverture d'Excel
'[COLOR=#F57900]On Error Resume Next[/color]
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
And the error given is "This object does not handle this property "GetMaterial"" on line oManager.GetMaterial
 
No sorry, that's not the good error message.

It tells me An ActiveX component cannot create an object "GetObject" on the line
Set Excel = GetObject(, "Excel.Application")
 
For my macro, I got a really useful loop who makes a selection of my runs:
Code:
If oSel.Count = 0 Then
   oSel.Search "(CATLndSearch.ArrangementRun),all")
End If
It selects all my runs perfectly and my macro works.
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
 
Status
Not open for further replies.
Back
Top