Let’s Start Programming! Forums CATIA Macro Programming Activate solid in a part

This topic contains 30 replies, has 3 voices, and was last updated by  ramzar 3 years ago.

Viewing 15 posts - 16 through 30 (of 31 total)
  • Author
    Posts
  • #1270

    ramzar
    Participant

    The macro language is MS VBA??? Because i past it in MS VBA and i run it and got nothing. No error but dosen’t work. I don’t know how work the macro module (ALT+F11). I’ll try to investigate a bit!

    #1271

    ramzar
    Participant

    The macro work now, i just find the answer, well a part of it.

    bIsInactive = oPart.IsInactive(oShape)
    If bIsInactive = True Then

    You where doing a verification if Solide.42 was desactivated. I erase those 2 lines and the macro works perfectly. In fact, If Solide.42 is activated, it can activate it again, it change nothing. I don’t know why it didn’t work, logically, it look ok to verify if “Solide.42” is activate or not and if not, do nothing and go next. Maybe doing a Search, like i did in my first macro attempt would be more simple. I’ll try it.

    Now i want to do a line that verify if i’m active in a product when i start the macro.if not, it show a message box telling me to go in product and re-run the macro.

    • This reply was modified 3 years, 1 month ago by  ramzar.
    • This reply was modified 3 years, 1 month ago by  ramzar.
    #1274

    Matt
    Participant

    Great news! To test if you are in a product,

    If instr(ucase(CATIA.ActiveDocument.Name), “CATPRODUCT”) then

    Set oRootProductDocument = CATIA.ActiveDocument
    ‘Rest of code here
    Else
    Msgbox “You must have a product loaded”
    End if

    End sub

    #1275

    ramzar
    Participant

    Hi Matt, the code you gave me to verify if i’m in a product don’t work. Instead, it make x number of product and part open where x is the number of part in my assembly.

    #1279

    Matt
    Participant

    Sorry, I forgot a piece, try this.

    Sub CATMain()

    Dim oRootProductDocument as Document
    Dim oRootProduct As Product
    Dim oPartProducts As Products
    Dim oPartProduct As Product
    Dim oPartDocument As Document
    Dim oPart As Part
    Dim oSelection As Selection
    Dim oBodies as Bodies
    Dim oBody as Body
    Dim oShapes as Shapes
    Dim oShape as Shape

    Dim i As Integer

    Dim bIsInactive As Boolean

    If instr(ucase(CATIA.ActiveDocument.Name), “CATPRODUCT”)<> 0 then

    Set oRootProductDocument = CATIA.ActiveDocument
    Set oRootProduct = oRootProductDocument.Product
    Set oPartProducts = oRootProduct.Products
    Set oSelection = CATIA.ActiveDocument.Selection

    ‘Turn off refresh display
    CATIA.RefreshDisplay = False

    ‘If you are using cache apply design mode to the entire assembly instead of each part
    oRootProduct.ApplyWorkMode DESIGN_MODE

    ‘Loop through all parts
    For i = 1 to oPartProducts.Count
    Set oPartProduct = oPartProducts.item(i)
    Set oPartDocument = oPartProduct.ReferenceProduct.Parent
    Set oPart = oPartDocument.Part
    Set oShape = oPart.FindObjectByName(“Solide.42″)

    If TypeName(oShape) <> “Nothing” Then
    oSelection.Clear
    oSelection.Add oPart
    CATIA.StartWorkbench (“PrtCfg”) ‘starts part design
    oSelection.Clear

    oPart.Activate(oShape)

    End if

    oSelection.Clear
    oSelection.Add oPartProduct
    CATIA.StartWorkbench (“Assembly”) ‘starts assembly design
    oSelection.Clear

    oPart.Update ‘Be careful updating the entire part…if there is something that
    ‘won’t update, the macro will fail. You can update a specific object using
    ‘oPart.UpdateObject oShape
    Next

    ‘Turn on refresh display
    CATIA.RefreshDisplay = True

    Else
    Msgbox “You must have a product loaded”
    End if

    End Sub

    #1281

    ramzar
    Participant

    Still dosen’t work. It’s not really important, i know that i need to be in a product.

    #1283

    ramzar
    Participant

    I just try to use the macro on another job and it don’t work. I go step by step trying to see where it don’t work and it seems that even if the macro read that “Solide.42” is Inactivate, it skip the activate line and go next.

    Also, i just realize that at the end of my tree, i have a product containing screws. My macro fails when it get there at this line: “Set oPart = oPartDocument.Part”. Is there any way to control if it’s a part or a product, and then skip the product?

    Thank you!

    • This reply was modified 3 years ago by  ramzar.
    #1285

    Matt
    Participant

    ‘Loop through all parts
    For i = 1 to oPartProducts.Count
    Set oPartProduct = oPartProducts.item(i)

    If oPartProduct.products.count=0 then ‘New line

    Set oPartDocument = oPartProduct.ReferenceProduct.Parent
    Set oPart = oPartDocument.Part
    Set oShape = oPart.FindObjectByName(“Solide.42″)
    ‘Rest of the code
    oPart.Activate(oShape)
    End if
    End if ‘New line

    #1313

    ramzar
    Participant

    Hi Matt, thanks for your last answer it work perfectly. And for the problem with my macro at “Activate” i found that to write it good, it was like this:”oPart.Activate oShape” instead of :”oPart.Activate (oShape).

    I don’t know why… i searched in the help and normally, we are supposed to write “oshape” with(). But now it works!!! the only thing is to verify if im in product because if i’m in a part, because we change workbench. it start a new product, then start a new part, then a product, etc and it goes all over the tree like this so with 100+ part…… it open alot and catia close. I try the line you send me before but it didn’t work.

    For all your help, seriously, i cant thank you enough. Thank you!Thank you! Thank you!

    #1314

    Matt
    Participant

    You’re very welcome. Sometimes in vbScript you have to remove some pare this…not sure why.

    Are you saying you need to check that you have a product loaded? How about

    If UCase(TypeName(CATIA.ActiveDocument)) <> “PRODUCTDOCUMENT) Then
    MsgBox “You must load a product, exiting macro”
    Exit sub
    end If

    #1326

    ramzar
    Participant

    i try “If UCase(TypeName(CATIA.ActiveDocument)) <> “PRODUCTDOCUMENT) Then
    MsgBox “You must load a product, exiting macro”
    Exit sub
    end If”
    I didnt work. I change “PRODUCTDOCUMENT” for “CATPRODUCT” and didn’t work either.

    And now the macro dont work with:
    ‘bIsInactive = oPart.IsInactive(oShape)
    ‘If bIsInactive = True Then
    So i just put a ‘ and know its ok.

    #1328

    Matt
    Participant

    Are you trying to check if you have a product loaded into CATIA?

    #1358

    ramzar
    Participant

    I have a product, and in it, i have alot of part. I want to check if i’m in the product before starting the macro because in the macro, i change workbench and if i start the macro while being in part, it open x number of part and product, where x is the number of part in my assembly.

    #1366

    Matt
    Participant

    I see. The easiest way may be to have a vbOkCancel message box that tells the user that the top assembly has to be in edit mode. Then they have the option to cancel out. You could also count the number of windows before the macro, if the number of windows gets larger, exit the macro.

    #1372

    ramzar
    Participant

    Yeah that could be a possibility. But from now, it work good, only need to remember to put it in product. If not, because there is too much windows opening, catia fail and close… It appen 1, not 2!!!

    I’ll check for the vb OKCancel message box, i’ll try to figure out how it works and will post my code after.

Viewing 15 posts - 16 through 30 (of 31 total)

The forum ‘CATIA Macro Programming’ is closed to new topics and replies.