×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Some questions about macro programing

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
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"
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

RE: Some questions about macro programing

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.



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

RE: Some questions about macro programing

(OP)
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 --> 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 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 --> CATScript

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.

RE: Some questions about macro programing

(OP)
*Made an error and created a double post but now I can't delete it, sorry*

RE: Some questions about macro programing

you should take the inertia from the product not from the part

CODE -->

Set objInertia=prdProduct.Parent.Product.GetTechnologicalObject("Inertia") 

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

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

RE: Some questions about macro programing

you have to correct

CODE -->

Dim mat as Material ' not Object 

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

finally if you want to use the preselection, change the line :

CODE -->

sStatus = oSel.SelectElement3(strArray, "Select parts", True, CATMultiSelTriggWhenUserValidatesSelection, true) 

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

(OP)
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 :)

RE: Some questions about macro programing

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

RE: Some questions about macro programing

(OP)
Thanks for COG, works now :)

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

msgbox is showing mat.name ?

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

(OP)
No, the MsgBox pops up but it is empty

RE: Some questions about macro programing

did you update

CODE -->

Dim mat as Material 
?

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

(OP)
Yes I did :/

RE: Some questions about macro programing

remove all your on error ... and check when the error is

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

(OP)
I commented all of my On Error and the only one who give me an error is this one

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 If 
And the error given is "This object does not handle this property "GetMaterial"" on line oManager.GetMaterial

RE: Some questions about macro programing

(OP)
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")

RE: Some questions about macro programing

(OP)
For my macro, I got a really useful loop who makes a selection of my runs:

CODE --> CATScript

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

RE: Some questions about macro programing

(OP)
Ok, it's oSel.Clear, but I didn't put it in the place of the macro so it didn't work as it should

RE: Some questions about macro programing

(OP)
FYI, for the extraction of the material I got this

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 Coordinates 

Everything works fine except the material

RE: Some questions about macro programing

plz remove on error resume next

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

(OP)
Ok I did it, there are no On Error Resume Next in the loop but I still can't get material, in fact, it gives me an error: "Method GetItem failed" line "Set oManager = objProductMat.GetItem("CATMatManagerVBExt")"

Is it possible, somehow, I don't have CATManagerVBExt?

RE: Some questions about macro programing

ok i was expecting this line to fail...

can you tell us what type is objProductMat ? is it a product? should it be a part ?

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

(OP)
I declared objProductMat as a Part at first, then as a Product, and finally I commented the "As ****", just letting "Dim objProductMat" and there were no differences at all

RE: Some questions about macro programing

if you removed all your on error resume next it should make a difference.

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?

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

(OP)
objProductMat comes from a selection: Set objProductMat = objGCATIASelection1.Item(k)
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 If 

RE: Some questions about macro programing

ok so you give a product to oManager.GetMaterialOnPart ?

and you expect it to work?

bugeyed

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

(OP)
Oh yes indeed, I noticed it on my code before so I changed it but I didn't change it here on the forum.

So actually the line is oManager.GetMaterialOnProduct objProductMat,Mat, sorry for this mistake

RE: Some questions about macro programing

no problem... glad you fixed your code.

is it working now?

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

(OP)
No, since I removed the On Error Resume Next, I still get "the Method GetItem failed"

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

may be you need to type your variable when you Dim it

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

(OP)
Which variable? I thought everything was declared ponder

RE: Some questions about macro programing

dim objProductMat as Part

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

(OP)
Humm, it's still not working.
Is there a special command to activate GetItem("CATMatManagerVBExt") before?

RE: Some questions about macro programing

your code with the 4 corrections explain bellow your post works for me

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

(OP)

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 
Here is my loop which is working except for the material, and I don't get the error about GetItem anymore
(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

are you sure Set objProductMat = objGCATIASelection1.Item(k) is a part? I have the feeling it is a selectedElement

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

(OP)
Humm, my selection is made this way:

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 If 

Maybe 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

so you have Product in your selection and you passing a product to oManager.GetMaterialOnPart

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

(OP)
No, it's a product I'm passing
Set oManager = objProductMat.Item(k).GetItem("CATMatManagerVBExt")
oManager.GetMaterialOnProduct objProductMat,Mat

RE: Some questions about macro programing

(OP)
And it doesn't change if I put
Dim objProductMat As Part
or
Dim objProductMat As Product

RE: Some questions about macro programing

and the material is apply to what?

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

ok i did test your code and I have the same problem

CODE -->

oManager.GetMaterialOnProduct objProductMat,Mat 


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

oManager.GetMaterialOnPart objProductMat.ReferenceProduct.Parent.Part,Mat 


Be aware that this line will fail if objProductMat is not an instance

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

(OP)
Ok, thanks Eric, I will try it Tuesday when I come back to the office but what is an instance exactly?

RE: Some questions about macro programing

this is 3dx 2016X but V5 logic is the same:

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

where is the error message / line?

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

(OP)
Still no error message, just MsgBox shows up empty

RE: Some questions about macro programing

REMOVE ALL ON ERROR RESUME NEXT !

ALL OF THEM

Eric N.
indocti discant et ament meminisse periti

RE: Some questions about macro programing

(OP)
The error is still on the GetItem method then

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

use VBA debug tool (Locals) to make sure variable are Dimed properly use V5Automation.chm as reference in chapter : Applying or Retrieving a Material on a Product, a Part, or a Body

Eric N.
indocti discant et ament meminisse periti

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources