Option Explicit
' COPYRIGHT DASSAULT SYSTEMES 2004

' *****************************************************************************
'   Purpose:      Create a schematic document (2).
'   Assumtions:   Product level: Schematic Platform (SDI).
'   Languages:    VBScript
'   Locales:      English 
'   CATIA Level:  V5R15 
' *****************************************************************************

Sub CATMain()

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

    If (Not CATIA.FileSystem.FolderExists(sSavePath)) Then
      Err.Raise 9999,sSavePath,"No Path for saving document"
    End If
    ' ------------------------------------------------------------------------- 

    Dim strMessage As String

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

    '--------------------------------------------------------------------------
    ' Create a CATProduct document
    '--------------------------------------------------------------------------
    Dim objSchDoc As Document
    Set objSchDoc = CATIA.Documents.Add ("CATProduct")
    '
    ' Find the top node of the schematic object tree - schematic root.
    Dim objPrdRoot As Product
    Dim objSchRoot As SchematicRoot

    '--------------------------------------------------------------------------
    ' Associate schematic behavior to the CATProduct document through
    ' the method GetTechnologicalObject.
    '--------------------------------------------------------------------------
    Dim strRootName As String
    Dim strDocName As String
    strRootName = "Sample_SchematicRoot"
    strDocName = CATIA.FileSystem.ConcatenatePaths(sSavePath, _
	             "SampleOutput_SchDoc02.CATProduct")

    If ( Not ( objSchDoc Is Nothing ) ) Then
      Set objPrdRoot = objSchDoc.Product 
      If ( Not ( objPrdRoot Is Nothing ) ) Then
        Set objSchRoot = objPrdRoot.GetTechnologicalObject("SchematicRoot")
        objPrdRoot.PartNumber = strRootName
      End If

      If ( Not ( objSchRoot Is Nothing ) ) Then

         '---------------------------------------------------------------------
         ' Regular CATProduct is a 3D document and is associated with a 3D
         ' editor and a 3D viewer. On the other hand a schematic document
         ' is a special CATProduct document that is associated with a special
         ' 2D viewer and 2D editor. Therefore, we need to trigger the
         ' documentation initialization (which has already been done in 
         ' CATDocuments.Add) again after associating schematic 
         ' behavior to the document, 
         ' by saving the document and re-opening it again.
         '---------------------------------------------------------------------
         objSchDoc.SaveAs strDocName

         objSchDoc.Close

         Set objSchDoc = CATIA.Documents.Open (strDocName)

         Set objPrdRoot = Nothing
         Set objSchRoot = Nothing
         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

         '---------------------------------------------------------------------
         ' Set the drawing standard if needed 
         '---------------------------------------------------------------------
         If ( Not ( objSchRoot Is Nothing ) ) Then
            objSchRoot.SetDrawingStandard catISO
            strMessage = strMessage & "drawing standard set to catISO" & vbCr
            Dim std As CatDrawingStandard
            std = objSchRoot.GetDrawingStandard
            strMessage = strMessage & "drawing standard = " & std & vbCr
         End If

         'objSchDoc.SaveAs strDocName

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


    End If

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

End Sub