Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

catia macro for auto fixing of parts going through the tree.

Status
Not open for further replies.

phillcom3

Aerospace
Jul 1, 2014
6
GB
hi there first post here.

i have years of catia under me but not in scripting

i need to make a macro as per the titler but i cant seem to get it to be selectable such that it works on any part not the one it was recorded with.

i need it to work down the tree fixing inside sub branches as it goes and then hiding the fix anchors

please help!!
 
Replies continue below

Recommended for you

Hi Phill,
This is a really fun question...of course I say that because I made this up a few years ago :)
The trick in this is to begin to understand how a recursive function works.
The following is a code to run in catvba. I won't say it's great, or well commented, or has great error handling, but I think it works pretty well. There's parts of it, particularly how the recursion call works, that I've been able to reuse in other applications.
Hope that helps.

Mark

Code:
'***************************************************************
'Macro to Delete existing constraints, and create FixInSpace constraints
'Operates recursively on all levels within the current document
'By Mark Forbes
'Original Version 0.1.0 May 19 2010
'Version 0.1.1 June 8 2010 Handles deactivated components
'Version 0.1.2 Sept 16 2010 Handles CATProducts only once, better error handling
'   on deactivated products, and prompts to skip Flexible Subassemblies
'Version 0.1.3 Feb 1 2011 Hide constraints, Late Bind oCurrentProd for R19
'Version 0.1.4 March 10 2011 : Adding more error handling
'***************************************************************

Public oList As Variant
Public oSelection As Selection
Public oVisProp As VisPropertySet

Option Explicit

Sub CATMain()

'Declarations
Dim oTopDoc As Document
Dim oTopProd As ProductDocument
Dim oCurrentProd As Object
'Dim oVisProp As VisPropertySet



'Check if the active document is an assembly, else exit
Set oTopDoc = CATIA.ActiveDocument
If Right(oTopDoc.Name, 7) <> "Product" Then
    MsgBox "Active document should be a product"
    Exit Sub
End If

Set oSelection = oTopDoc.Selection
Set oVisProp = oSelection.VisProperties

Set oCurrentProd = oTopDoc.Product

Set oList = CreateObject("Scripting.dictionary")

'CATIA.StatusBar = "Working On" & " " & oCurrentProd.Name

Call FixSingleLevel(oCurrentProd)    'Call the subroutine, it is a recursive loop

   
CATIA.StatusBar = "Macro Done"
MsgBox "Fixing Macro Finished"


End Sub

Private Sub FixSingleLevel(ByRef oCurrentProd As Object)

On Error GoTo Err_Handling

'More declarations
Dim ItemToFix As Product
Dim iProdCount As Integer
Dim i As Integer
Dim j As Integer
Dim oConstraints As Constraints
Dim oReference As Reference
Dim sItemName As String
Dim constraint1 As Constraint
Dim pActivation As Parameter
Dim n, m As Integer
Dim sActivationName As String

Err.Clear
CATIA.StatusBar = "Working On" & " " & oCurrentProd.Name
Set oCurrentProd = oCurrentProd.ReferenceProduct
iProdCount = oCurrentProd.Products.Count
Set oConstraints = oCurrentProd.Connections("CATIAConstraints")

n = oConstraints.Count  'Remove Existing Constraints
m = n
For i = 1 To m
    oConstraints.Remove (n)
    n = n - 1
Next


For i = 1 To iProdCount                             'Cycle through the assembly's children

    Set ItemToFix = oCurrentProd.Products.Item(i)

CreateReference:
    
    sItemName = ItemToFix.Name
    CATIA.StatusBar = "Working On " & oCurrentProd.Name & " / " & sItemName
    
    Set oReference = oCurrentProd.CreateReferenceFromName(sItemName & "/!" & "/")
   
    Set constraint1 = oConstraints.AddMonoEltCst(catCstTypeReference, oReference)
    constraint1.ReferenceType = catCstRefTypeFixInSpace
    
    oSelection.Add constraint1      'Set visibility to hidden
    oVisProp.SetShow catVisPropertyNoShowAttr
    oSelection.Clear

RecursionCall:
    If ItemToFix.Products.Count <> 0 Then        'Recursive Call
        If oList.exists(ItemToFix.PartNumber) Then GoTo Finish
        
        If ItemToFix.PartNumber = ItemToFix.ReferenceProduct.Parent.Product.PartNumber Then oList.Add ItemToFix.PartNumber, 1
        Call FixSingleLevel(ItemToFix)
    End If

Finish:

Next

GoTo End1:

'*****Error Handling
Err_Handling:

    
sActivationName = oCurrentProd.Name + "\" + ItemToFix.Name + "\Component Activation State"  'Build the reference Name
 Set pActivation = ItemToFix.Parameters.GetItem(sActivationName)
    If pActivation.ValueAsString = "false" Then
       CATIA.StatusBar = "Error, Try To Activate " & ItemToFix.Name 'Tell the user what is happening
       pActivation.ValuateFromString ("true")
       
     ElseIf pActivation.ValueAsString = "true" Then  'Assume this is a flexibe component
       j = MsgBox("Error on " & oCurrentProd.Name + "\" & ItemToFix.Name & vbCrLf _
            & "This element may be a flexible component, have an invalid" & vbCrLf _
            & "Instance Name, or other error" & vbCrLf _
            & vbCrLf _
            & "Skip component and continue?", vbOKCancel, "Error")
            Err.Clear
       If j = 1 Then Resume RecursionCall
       If j = 2 Then
        CATIA.StatusBar = "Fix All Aborted"
        End
       End If
       Else: Resume RecursionCall
      
    End If
'*****End of Error Handling

End2:
Resume

End1:
End Sub
 
Thankyou for your help, not knowning vb script even jsut looking through that it seems simple enough that i can see what it is doing. the perticular line of "get item" was where i had the need of help.

ill try this when i get an assembily that needs it tehn forward it through for some formal testing witha few tweeks for what i need aditionaly doing. such as hiding all teh constraints once correct.

how would i do this? recursive hide of constraints??
 
I would suggest that you take some time to run this macro on your own test assembly and understand how it works. Then you will answer your own questions, such as how to hide the constraints (the macro already does that!). Run the macro by stepping through one line at a time and you should find your answers. The recursive operation is how it operates on multiple levels in the structure tree, that will take some investigation to understand.

It's not VB Script, it's CATVBA, which means you'll need to copy all that into a module in a catvba file. If you search the forum and google, there's lots of resources about how to use that.

Mark
 
thankyou mark,

i have had time to and noticed that it does hide, i still woudl hve to go through and hide the constraints node but thats not an issue.

is there any method to stop it from going through anything more than what is blue boxed?
 
Hi mark,

I have tried today as i was given time to work on this and it keeps runign into a out of memory error or a script entry point error, this was not happening the other day when i tried have you any ideas what i am doing wrong.

i closed everything on the desk other than catia and tried again and still no look. vb help says its somthign to do with 64bit memory block runing out and it stops at the first stage. this is a small assembily only 126 parts.

regards Phiill

thankyou for your help.
 
Sorry, I'm afraid I don't know at this point. Can you step through the code one line at a time (hitting F8) and find where there's an error?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Top