Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-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
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

FYI, for the extraction of the material I got this
Code:
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
 
plz remove on error resume next

Eric N.
indocti discant et ament meminisse periti
 
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?
 
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
 
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
 
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
 
objProductMat comes from a selection: Set objProductMat = objGCATIASelection1.Item(k)
Each component of the assembly here is a product
Though the selection is
Code:
If objGCATIASelection1.Count = 0 Then
    objGCATIASelection1.Search "(CATLndSearch.Product),all"
  End If
 
ok so you give a product to oManager.GetMaterialOnPart ?

and you expect it to work?

[bugeyed]

Eric N.
indocti discant et ament meminisse periti
 
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
 
no problem... glad you fixed your code.

is it working now?

Eric N.
indocti discant et ament meminisse periti
 
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
 
may be you need to type your variable when you Dim it

Eric N.
indocti discant et ament meminisse periti
 
dim objProductMat as Part

Eric N.
indocti discant et ament meminisse periti
 
Humm, it's still not working.
Is there a special command to activate GetItem("CATMatManagerVBExt") before?
 
your code with the 4 corrections explain bellow your post works for me

Eric N.
indocti discant et ament meminisse periti
 
Code:
  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 :)
 
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
 
Humm, my selection is made this way:
Code:
  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
 
so you have Product in your selection and you passing a product to oManager.GetMaterialOnPart

Eric N.
indocti discant et ament meminisse periti
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor