Option Explicit
' COPYRIGHT DASSAULT SYSTEMES 2004

' *****************************************************************************
'   Purpose:      Add/List graphical representations to a reference component. 
'   Languages:    VBScript
'   Locales:      English 
'   CATIA Level:  V5R15 
' *****************************************************************************

Sub CATMain()

    ' ------------------------------------------------------------------------- 
    ' Optional: allows to find the sample wherever it's installed
    dim sDocPath As String 
    sDocPath=CATIA.SystemService.Environ("CATDocView")

    If (Not CATIA.FileSystem.FolderExists(sDocPath)) Then
      Err.Raise 9999,sDocPath,"No Doc Path Defined"
    End If
    ' ------------------------------------------------------------------------- 
    ' Open the schematic document 
    Dim sFilePath
    sFilePath = CATIA.FileSystem.ConcatenatePaths(sDocPath, _
            "online\CAAScdSchUseCases\samples\CAASCH_Detail03.CATProduct")

    Dim objSchDoc As Document
    Set objSchDoc = CATIA.Documents.Open(sFilePath)

    Dim strMessage As String

    strMessage = _
      "--------------------------------------------------------------------" & vbCr
    strMessage = strMessage & _
      "Output traces from CAASchCompRefGraphic.CATScript" & vbCrLf

    ' Find the top node of the schematic object tree - schematic root.
    Dim objPrdRoot As Product
    Dim objSchRoot As SchematicRoot
    If ( Not ( objSchDoc Is Nothing ) ) Then
      Set objPrdRoot = objSchDoc.Product 
      If ( Not ( objPrdRoot Is Nothing ) ) Then
        Set objSchRoot = objPrdRoot.GetTechnologicalObject("SchematicRoot")
      End If
    End If

    Dim objLCompRefs As SchListOfObjects
    Dim objCompRefGraphic As SchCompGraphic

    If ( Not ( objSchRoot Is Nothing ) ) Then

       '-----------------------------------------------------------------------
       ' Find a list of reference component in the model
       '-----------------------------------------------------------------------
       Set objLCompRefs = objSchRoot.GetRefComponents

       If ( Not ( objLCompRefs Is Nothing ) ) Then

          '--------------------------------------------------------------------
          ' Get a SchCompGraphic interface handle from a reference
          ' component
          '--------------------------------------------------------------------
          Set objCompRefGraphic = objLCompRefs.Item (1,"CATIASchCompGraphic")

          If ( Not ( objCompRefGraphic Is Nothing ) ) Then

              Dim objUnassocSymbol As AnyObject

              '----------------------------------------------------------------
              ' Find a symbol that is not associated with any reference in
              ' the model
              '----------------------------------------------------------------
              Set objUnassocSymbol = GetComponentSymbol (objSchRoot)

              If ( Not ( objUnassocSymbol Is Nothing ) ) Then

                '--------------------------------------------------------------
                '  Add the second graphical representation to the reference
                '  component using the symbol just found
                '--------------------------------------------------------------
                objCompRefGraphic.AddGraphicalRepresentation objUnassocSymbol

                strMessage = strMessage & "Successfully add the second graphical representation" & vbCr

              End If

              '----------------------------------------------------------------
              ' Find another symbol that is not associated with 
              ' any reference in the model
              '----------------------------------------------------------------
              Set objUnassocSymbol = GetComponentSymbol (objSchRoot)
              If ( Not ( objUnassocSymbol Is Nothing ) ) Then

                '--------------------------------------------------------------
                '  Add the third graphical representation to the reference
                '  component using the symbol just found
                '--------------------------------------------------------------
                objCompRefGraphic.AddGraphicalRepresentation objUnassocSymbol

                strMessage = strMessage & "Successfully add the third graphical representation" & vbCr

              End If

              Dim intNbGraphic As Integer
              Dim objGRR As SchGRR
              Dim objLGraphic As SchListOfObjects
              Set objLGraphic = objCompRefGraphic.ListGraphicalRepresentations
             
              If ( Not ( objLGraphic Is Nothing ) ) Then
                 intNbGraphic = objLGraphic.Count
                 strMessage = strMessage & "Number of graphical representations"
                 strMessage = strMessage & "= " & intNbGraphic & vbCr

                 Set objGRR = Nothing
                 If (intNbGraphic > 1) Then
                    Set objGRR = objLGraphic.Item(intNbGraphic,"CATIASchGRR")
                    If ( Not (objGRR Is Nothing ) ) Then
                       objCompRefGraphic.RemoveGraphicalRepresentation objGRR
                       Set objLGraphic = objCompRefGraphic.ListGraphicalRepresentations
                       If ( Not ( objLGraphic Is Nothing ) ) Then
                          intNbGraphic = objLGraphic.Count
                          strMessage = strMessage & "Number of graphical representations"
                          strMessage = strMessage & "after calling "
                          strMessage = strMessage & " RemoveGraphicalRepresentation = " & intNbGraphic & vbCr
                       End If 
                    End If
                 End If ' --- If (intNbGraphic > 1)
              End If '--- If ( Not ( objLGraphic Is Nothing ) ) Then

          End If '--- If ( Not ( objCompRefGraphic Is Nothing )...

       End If '--- If ( Not ( objLCompRefs Is Nothing ) ...

    End If '--- If ( Not ( objSchRoot Is Nothing )...

    strMessage = strMessage & _
      "--------------------------------------------------------------------" & vbCr
    MsgBox strMessage


End Sub

' -----------------------------------------------------------------------------
' | Find a component symbol that has not been associated with a schematic
' | component from a document root.
' | Input: objSchRootArg:  the root of the document.
' | Returns: component symbol object.
' -----------------------------------------------------------------------------
Private Function GetComponentSymbol (objSchRootArg As SchematicRoot) As AnyObject
   Dim objSchLSymbols As SchListOfObjects
   If ( Not ( objSchRootArg Is Nothing ) ) Then
      Set objSchLSymbols = objSchRootArg.GetUnassociatedSymbols
      If ( Not ( objSchLSymbols Is Nothing ) ) Then
         Set GetComponentSymbol = objSchLSymbols.Item (1,"CATIASchGRR")
      End If
   End If
End Function