Let’s Start Programming! Forums CATIA Macro Programming rename part in product

This topic contains 13 replies, has 3 voices, and was last updated by  dariooliviero 2 years ago.

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #1276

    ramzar
    Participant

    Hello all, i’m trying to do a macro in VB to rename all the part i have selected by a name enter in a Inputbox. Also, with another inputbox i want to put a stating number and rename all item in my selection by the name and the number (indexing by 1 for each part following). Here is a macro i try, but don’t work (Thanks to Emmett, i took some of those lines in a email i receive)

    Also when i try to put a “+” to add a number at “namer2” the “+” sign disapear. I don’t know why.

    Sub CATMain()

    Dim productDocument1 As Document
    Dim selectionpart As selection
    Dim collection As New collection
    Dim selection1 As selection

    Set selectionpart = CATIA.ActiveDocument.selection
    Set productDocument1 = CATIA.ActiveDocument

    ‘count the number of CATParts within the selection
    partcount = selectionpart.Count

    Dim reponse1 As Integer
    reponse1 = MsgBox(“The number of part is: ” & partcount, vbOKOnly)

    collection.AddItem.selectionpart <———-ERROR HERE

    Dim Namer As String
    Namer = InputBox(“Put SOB name.”, “Préfixe”, “SOB-XXXX”)

    Dim Namer2 As Integer
    Namer2 = InputBox(“Put the start number.”, “Numéro de départ”)

    Dim k As Integer

    ‘loop
    For k = 1 To partcount

    Set selection1 = collection.Item(k)

    ‘apply design mode to each part
    selection1.ApplyWorkMode DESIGN_MODE

    Dim partDoc1 As PartDocument
    Set partDoc1 = selection1

    partDoc1.PartNumber = Namer & namer2
    partDoc1.Name = Namer & namer2

    Namer2 + 1

    Next ‘k

    End Sub

    • This topic was modified 3 years ago by  ramzar.
    • This topic was modified 3 years ago by  ramzar.
    #1280

    Matt
    Participant

    Are you in vba or vbscript? Collections only work in vba.

    You have 2 selection objects, you only need 1

    Selectionpart is the selection object, but you did not tell it to select anything, so you can’t put “nothing” into a collection.

    Selection part.count would return 0 because there is nothing selected

    Looping through the parts is EXACTLY like we did it in the last macro. The difference is you are adding the parts to a collection

    ‘Loop through all parts and add to collection
    For i = 1 to oPartProducts.Count
    Collection.add oPartProducts.item(i).ReferenceProduct.Parent.Part
    Next

    #1282

    ramzar
    Participant

    The selection would be made before starting the macro. I’ll select all the thing i want to rename before starting the macro, so there will be something in the selection.

    #1315

    Matt
    Participant

    That is fine if the selection is done ahead of time. Did you get the part to go into your collection? Also, you need Namer=Namer+1

    #1327

    ramzar
    Participant

    I’m in catvba. Can i get collection in catvba?

    And i try namer=namer+1 and the + symbol disapear after i put it.

    #1329

    Matt
    Participant

    So you press alt+F11 to write your macros?

    #1359

    ramzar
    Participant

    Yep, exactly

    #1360

    ramzar
    Participant

    Here is my new code with a selection in, instead of renaming all the parts in the assembly.

    Sub CATMain()

    Dim productDocument1 As Document
    Dim product1 As Product
    Dim products1 As Products
    Dim selection As selection

    Set productDocument1 = CATIA.ActiveDocument
    Set product1 = productDocument1.Product
    Set products1 = product1.Products
    Set selection = CATIA.ActiveDocument.selection

    ‘count the number of CATParts within the catproduct
    partcount = selection.Count
    MsgBox (“The number of part is ” & partcount)

    If selection.Count = 0 Then
    MsgBox (“Sélectionner les parts à renommer et relancer la macro.”)
    End
    End If

    Dim Namer As String
    Namer = InputBox(“Put SOB name.”, “Préfixe”, “SOB-XXXX”)

    Dim Namer2 As Integer
    Namer2 = InputBox(“Put the start number.”, “Numéro de départ”, “1”)

    Dim i As Integer

    ‘Loop through all parts
    For i = 1 To partcount

    ‘apply design mode to each part
    selection.Item(i).ApplyWorkMode DESIGN_MODE <——Error here

    Dim getDes, getDes2 As String
    getDes = selection.Item(i).PartNumber <—-Here too
    getDes2 = selection.Item(i).Name <—-Here too

    partDoc1.PartNumber = Namer & i
    partDoc1.Name = Namer & i

    Namer2 = Namer2 + 1

    Next ‘i
    End Sub

    #1365

    ramzar
    Participant

    ok i change my macro again! and now it work well. My only concern is that i want to replace “namer3” inputbox by a selection of the line i want to start renaming my part. But i didnt find any way to know at witch line my selection is in “partcount” Any ideas?

    Sub CATMain()

    Dim productDocument1 As Document
    Dim product1 As Product
    Dim products1 As Products
    Dim selection As selection

    Set productDocument1 = CATIA.ActiveDocument
    Set product1 = productDocument1.Product
    Set products1 = product1.Products
    Set selection = CATIA.ActiveDocument.selection

    ‘count the number of CATParts within the catproduct
    partcount = products1.Count

    Dim Namer As String
    Namer = InputBox(“Put SOB name.”, “Préfixe”, “SOB-XXXX”)

    Dim Namer2 As Integer
    Namer2 = InputBox(“Put the start number.”, “Numéro de départ”, “1”)

    Dim namer3 As Integer
    namer3 = InputBox(“Put the number relative to the begining line”, “Line start”, “1”)

    Dim i As Integer

    ‘loop through all parts
    For i = namer3 To partcount

    ‘apply design mode to each part
    products1.Item(i).ApplyWorkMode DESIGN_MODE

    Dim getDes, getDes2 As String
    getDes = products1.Item(i).PartNumber
    getDes2 = products1.Item(i).Name

    products1.Item(i).PartNumber = Namer & Namer2
    products1.Item(i).Name = Namer & Namer2

    Namer2 = Namer2 + 1

    Next ‘i

    End Sub

    #1367

    Matt
    Participant

    What do you mean by “the selection of the line to start renaming your part”?

    #1371

    ramzar
    Participant

    Hi, sorry for the late reply, i mean that i want my macro to start at a place i decide. Sometime, in my product, i put some part and rename it and after, i put other block at the end and want to rename only those one, i dont want my macro to run from the begining all the time.

    I made a macro that work, that start from where i want. The only thing is that i need to put the word “ici” (french term for “here”) in the description of the part so the macro is looking for the word “ici” and then start renaming every part after (and erasing the work “ici”). Also, i know that in my part i want to rename the “main body” so i made my macro so it rename the part and the main body. So here is my macro, if this could help some people!!!

    Sub CATMain()

    Dim productDocument1 As Document
    Dim product1 As Product
    Dim products1 As Products
    Dim partdoc As partdocument
    Dim partdocument As Document
    Dim part As part
    Dim partproduct As Product
    Dim bodies1 As AnyObject
    Dim counter As Integer

    Set productDocument1 = CATIA.ActiveDocument
    Set product1 = productDocument1.Product
    Set products1 = product1.Products

    counter = 1

    ‘count the number of CATParts within the catproduct
    partcount = products1.Count

    Dim Namer As String
    Namer = InputBox(“Put SOB name.”, “Préfixe”, “SOB-XXXX”)

    Dim Namer2 As Integer
    Namer2 = InputBox(“Put the start number.”, “Numéro de départ”, “1”)

    Dim i As Integer
    ‘loop through all parts to find the place to begin renaming
    For i = 1 To partcount

    Dim getdes3 As String
    getdes3 = products1.Item(i).DescriptionInst
    If getdes3 = “ici” Then
    counter = 0
    products1.Item(i).DescriptionInst = “”

    Dim k As Integer
    ‘loop through all parts
    For k = i To partcount

    Set partproduct = products1.Item(k)

    ‘apply design mode to each part
    products1.Item(k).ApplyWorkMode DESIGN_MODE

    Dim getDes, getDes2 As String
    getDes = products1.Item(k).PartNumber
    getDes2 = products1.Item(k).Name

    products1.Item(k).PartNumber = Namer & Namer2
    products1.Item(k).Name = Namer & Namer2

    Set partdocument = partproduct.ReferenceProduct.Parent
    Set part = partdocument.part
    Set bodies1 = part.MainBody

    bodies1.Name = Namer2

    Namer2 = Namer2 + 1

    Next ‘k
    End
    End If

    Next ‘i
    If counter = 1 Then
    MsgBox (“Put -ici- in a part”)
    End
    End If
    End Sub

    #2342

    dariooliviero
    Participant

    Hello Ramzar
    I copyed and paste your code vut doesnt’ work :/ you can please help me in let me know if your macro still works or not and if you have any new version??
    Thanks
    Dario

    #2347

    ramzar
    Participant

    Hello dariooliviero, for my macro to work, you need to have a product with part in it. After, you open the propriety of the first item you want to rename then in the second box from the top, you put “-ici-” (you can chage it for whatever word you want by editing the macro). Let me know if it works.

    #2354

    dariooliviero
    Participant

    Hey Ramzar,
    unfortnuatly doesn’t work…:-( if you have skype or teamviewer probably can help directly and we can solve the issue straight away.
    my mail is dario@ido-innovation.com if you can help let me know via mail
    thanks
    Dario

Viewing 14 posts - 1 through 14 (of 14 total)

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