×
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

CATIA VBA to deactivate products in assembly

CATIA VBA to deactivate products in assembly

CATIA VBA to deactivate products in assembly

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

RE: CATIA VBA to deactivate products in assembly

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:
https://www.youtube.com/channel/UC1qdlBeJJEgMgpPLV...

RE: CATIA VBA to deactivate products in assembly

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

RE: CATIA VBA to deactivate products in assembly

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:
https://www.youtube.com/channel/UC1qdlBeJJEgMgpPLV...

RE: CATIA VBA to deactivate products in assembly

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

RE: CATIA VBA to deactivate products in assembly

(OP)
yes

RE: CATIA VBA to deactivate products in assembly

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.

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