Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

CATIA VBA to deactivate products in assembly 1

Status
Not open for further replies.

FabienF

New member
Jul 5, 2017
19
0
0
FR
Hello,
I need to deactivate some products in an assembly comprised of products at different levels( in the tree)
For first level products my code is working:
'--------------------------------------------
Dim CATIA As Object
Set CATIA = GetObject(, "CATIA.Application")
Dim productDocument1 As Document
Dim Selection1 As selection
Set productDocument1 = CATIA.ActiveDocument
Set Selection1 = productDocument1.selection
Set product1 = productDocument1.Product
Set products1 = product1.Products
Set product2 = products1.Item("productnamelevel1")
Selection1.Clear
Selection1.Add (product2)
CATIA.StartCommand ("Activate / Deactivate Component")
Selection1.Clear
'--------------------------------------------
However for level2 products this code does not work:
Dim CATIA As Object
Set CATIA = GetObject(, "CATIA.Application")
Dim productDocument1 As Document
Set productDocument1 = CATIA.ActiveDocument
Dim Selection1 As selection
Set Selection1 = productDocument1.selection
Dim product1 As Product
Set product1 = productDocument1.Product
Dim products1 As Products
Set products1 = product1.Products
Dim product2 As Product
Set product2 = products1.Item("productnamelevel1")
Dim products2 As Products
Set products2 = product2.Products
Dim product3 As Product
Set product3 = products2.Item("productnamelevel2")
Selection1.Clear
Selection1.Add (product3)
CATIA.StartCommand ("Activate / Deactivate Component")
Selection1.Clear

Does anyone has an idea of the solution please ? I know that it is breaking at line catia.startCommand.

Thanking you in advance

 
Replies continue below

Recommended for you

I had a similar problem, in the past.

And if I remember well, the problem was like this the selection wasn't adding the component to deactivate. Try to stop the code right after the selection.add. At that time i didn't understand why, and I change the way of how to do it. Then I used the selectElement3, selected the parts or products, and then make the needed actions.


Tiago Figueiredo
Tooling Engineer

Youtube channel:
 
Thank you TiagoFigueiredo for your response.
However, with selectElement3 you need to ask the end user to select the products, if I'm not wrong. For my code, this is not possible and it is infact the purpose of the code im trying to create:
I have a general assembly with lots of products and sub products at different levels. Some of them overlap, that is, depending on the configuration I want, I need to deactivate/activate the appropriate products and find the mass of that configuration(there are about 2000 configurations!) So, I really need to automate this.
So far, I succeeded in creating a code to find the mass of one product ( Set oInertia = product1.GetTechnologicalObject("Inertia") ) but I did not find a way to apply the GetTechnologicalObject method to an object that contains more than one product. That is why I decided to apply that working code to the general product(one product), for each configuration, after having deactivated the appropriate products. There comes the error, because as specified in my first post, the code succeeds in deactivating product at first level but not 2nd or other level...
Can you help me?

Thank you
 
Hello,

Try to debug your code, and see if the selection is happening.
if it's not happening the only way that i found to solve it, is really retrograde and slow. Is read the partname (if you already know the partname, before the code start you can do directly), and make a search to find those components, after that deactivate them. If the assembly is quite big, then it can be a bit slow.

Tiago Figueiredo
Tooling Engineer

Youtube channel:
 
Hi,
again thank you.
indeed, the selection was not happening... but I think that the problem was with the first parts of the code, when I was specifying the level where the product was located. It is, as you say very slow, because the assembly is big but the code works now (I added Set products2 = product3.Parent at the end)
Sub level2()
Dim CATIA As Object
Set CATIA = GetObject(, "CATIA.Application")
Dim productDocument1 As Document
Set productDocument1 = CATIA.ActiveDocument
Dim Selection1 As selection
Set Selection1 = productDocument1.selection
Dim product1 As Product
Set product1 = productDocument1.Product
Dim products1 As Products
Set products1 = product1.Products
Dim product2 As Product
Set product2 = products1.Item("level1productname")
Dim products2 As Products
Set products2 = product2.Products
Dim product3 As Product
Set product3 = products2.Item("level2productname")
Set products2 = product3.Parent

Selection1.Clear
Selection1.Add product3
CATIA.StartCommand ("Activate / Deactivate Component")
Selection1.Clear

End Sub
 
Hello,

Why don't you directly affect the Activation State parameter value?

Code:
Dim oParam As Parameter
Dim oRootProd As Product
oRootProd = CATIA.ActiveDocument.product

Dim prodName As String = "Put here the instance name of the product"
oParam = oRootProd.Products.Item(prodName).Parameters.Item(oRootProd.Name & "\" & oRootProd.Products.Item(prodName).Name & "\Component Activation State")
If someBooleanCondition Then oParam.ValuateFromString("true") Else oParam.ValuateFromString("False")

That would avoid user's selection and it would also be more elegant.
 
Status
Not open for further replies.
Back
Top