'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'-------------------------------------------------Externe Referenzen isolieren------------------------------------------------------------------------------------------------------
'Author:       Daniel Frauenrath
'Department:   L/B2/EK-IN
'Phone:        +49 841 881-2363
'Mail:         daniel.frauenrath@contiautomotive.com
'Release:      19.12.2007
'
'*************************************************************************************************************************************************************************
'<<<<<<<<<<<<<<<<Beschreibung / Discription>>>>>>>>>>>>>>>>>>>>
'**************************************************************************************************************************************************************************
'
'DE: Das Makro erkennt alle 2D-Komponenten die in allen Views des Hauptblattes liegen und fuehrt einen
'    Explode Befehl aus. Anschliessend koenne die nicht mehr benoetigten Detail-Sheet automatisch
'    geloescht werden. Ausserdem koennen alle Views auf der Zeichnung isoliert werden.
'    Die Zeichnung hat im Anschluss keine externen Referenzen mehr
'
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'
'EN: The macro recognizes all 2D-instances in all views of the main sheet, and introduces
'    a Explode command. Afterwards you can delete the no longer needed Detail Sheet automatically.
'    Moreover, all views on the drawing can be isolated.
'    The following Drawing has no external references (triggered through 2D-instances)
'
'************************************************************************************************************************************************

Sub CATMain()

Views_unlock            'Starten der Routine zum Entsperren (unlock) der Views
Komponenten_zerlegen    'Starten der Routine zum Zerlegen und Loeschen der 2D-Komponenten
Detail_Sheets_loeschen  'Starten der Routine zum Loeschen der nicht mehr benoetigten Detail-Sheets
Views_isolieren         'Starten der Routine zum Isolieren aller Views
Views_locken            'Starten der Routine zum Sperren (lock) der Views
Blatt_umbenennen	 	'Starten der Routine zum Umbenennen des Blattes

End Sub


'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'---------------------------2D-Komponenten auswaehlen und zerlegen (Explode)
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sub Komponenten_zerlegen()

    Dim oDRWDoc As DrawingDocument
    Dim oSheet As DrawingSheet
    Dim DrwSheet As Object
    Dim oView As DrawingView
    Dim oDRWComp As DrawingComponent
    Dim oSel As Selection

    Set oDRWDoc = CATIA.ActiveDocument
    Set oSel = oDRWDoc.Selection

l = 0                                                       'Komponentenzaehler null setzen
    For i = 1 To oDRWDoc.Sheets.Count                       'Schleife fuer alle Sheets
        Set oSheet = oDRWDoc.Sheets.Item(i)
        If Not oSheet.IsDetail Then                         'Ist das Sheet kein Detail-Sheet?
                For j = 1 To oSheet.Views.Count             'Schleife fuer alle Views im Sheet
                Set oView = oSheet.Views.Item(j)
                For k = 1 To oView.Components.Count         'Schleife fuer alle 2D-Komponenten der View
                Set oDRWComp = oView.Components.Item(k)
                     oDRWComp.Explode                       '2D-Komponente zerlegen
                l = l + 1                                   'Komponenten Zaehler hochsetzen
                Next
            Next
        End If
    Next
oSel.Search "CATDrwSearch.DrwDitto,all"                     'Alle 2D-Komponenten werden selektiert
On Error Resume Next                                        'Fehlererkennung wird abgeschaltet
oSel.Delete                                                 'Alle 2D-Kompoenten mit Verbindungen werden geloescht
    If l <> 0 Then                                          'Schleife fuer Zahlenangabe der MsgBox
        s = l
    Else
        s = "keine"
    End If
Box = MsgBox(" Es wurden " & s & " 2D-Komponenten zerlegt",,"Hinweis")
End Sub

'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'---------------------------Alle Views unlocken
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Sub Views_unlock()

    Dim oDRWDoc As DrawingDocument
    Dim oSheet As DrawingSheet
    Dim DrwSheet As Object
    Dim oView As DrawingView
    Dim oDRWComp As DrawingComponent
    Dim oSel As Selection

    Set oDRWDoc = CATIA.ActiveDocument
    Set oSel = oDRWDoc.Selection

    For i = 1 To oDRWDoc.Sheets.Count                       'Schleife fuer alle Sheets
        Set oSheet = oDRWDoc.Sheets.Item(i)
        If Not oSheet.IsDetail Then                         'Ist das Sheet kein Detail-Sheet?
                For j = 1 To oSheet.Views.Count             'Schleife fuer alle Views im Sheet
                Set oView = oSheet.Views.Item(j)
                On Error Resume Next                        'Fehlerbehandlung abschalten
                        Select Case CStr(oView.LockStatus)  'Auswahl des richtigen Falls
                            Case "Wahr"                     'Fall_1: Eine oder mehrere Views sind gesperrt (lock)
                                 oView.LockStatus = False   'Eine oder mehrere Views werden entsperrt (unlock)
                         End Select                         'Beenden der Fallauswahl
              Next
        End If
    Next
End Sub

'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'---------------------------Alle Views locken
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sub Views_locken()

    Dim oDRWDoc As DrawingDocument
    Dim oSheet As DrawingSheet
    Dim DrwSheet As Object
    Dim oView As DrawingView
    Dim oDRWComp As DrawingComponent
    Dim oSel As Selection

    Set oDRWDoc = CATIA.ActiveDocument
    Set oSel = oDRWDoc.Selection
    
    For i = 1 To oDRWDoc.Sheets.Count                        'Schleife fuer alle Sheets
        Set oSheet = oDRWDoc.Sheets.Item(i)
        If Not oSheet.IsDetail Then                          'Ist das Sheet kein Detail-Sheet?
                For j = 1 To oSheet.Views.Count              'Schleife fuer alle Views im Sheet
                Set oView = oSheet.Views.Item(j)
                On Error Resume Next                         'Fehlerbehandlung abschalten
                        Select Case CStr(oView.LockStatus)   'Auswahl des richtigen Falls
                            Case "Falsch"                    'Fall_2: Eine oder mehrere Views sind gesperrt (lock)
                                 oView.LockStatus = True     'Eine oder mehrere Views werden entsperrt (unlock)
                         End Select                          'Beenden der Fallauswahl
              Next
        End If
    Next
End Sub

'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'----------------------------------------Loeschvorgang fuer nicht mehr genutze Detail-Sheets
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sub Detail_Sheets_loeschen()

    Dim oDRWDoc As DrawingDocument
    Dim oSheet As DrawingSheet
    Dim DrwSheet As Object
    Dim oView As DrawingView
    Dim oDRWComp As DrawingComponent
    Dim oSel As Selection

    Set oDRWDoc = CATIA.ActiveDocument
    Set oSel = oDRWDoc.Selection
    Set SelSheet = oDRWDoc.Selection

r = 0                                   'Zaehler Null setzen
For i = 1 To oDRWDoc.Sheets.Count       'Schleife fuer alle Sheets
Set oSheet = oDRWDoc.Sheets.Item(i)
   If oSheet.IsDetail Then              'Pruefrotine fuer Detail Sheets (Zahlangabe in MsgBox)
    r = r + 1                           'Zaehler hochzaehlen
   End If                               'Pruefrotine beenden
Next

If r = 0 Then
    Box = MsgBox("Es wurden keine Detail-Sheets in der Zeichnung gefunden", , "Hinweis")
    Exit Sub
Else
  Abfr = MsgBox("Wollen Sie alle nicht mehr genutzen Detail-Sheets löschen?", vbYesNo, "Detail-Sheets löschen")
    If Abfr = 7 Then
        Box = MsgBox("Sie haben sich gegen die automatische Löschung entschieden." + Chr(10) + "Klicken Sie OK um mit dem Makro fortzufahren und löschen Sie die nicht mehr genutzen Detail-Sheets händisch", , "Hinweis")
    Else
        f = 0                                                   'Zaehler null setzen
        For i = 1 To oDRWDoc.Sheets.Count                       'Schleife fuer alle Detailblaetter (Angabe Schleifenrotine)
        On Error Resume Next                                    'Error-Handler ausschalten
        Set oSheet = oDRWDoc.Sheets.Item(i)                     'Zaehler Scheife
        If oSheet.IsDetail Then                                 'Nun Detail-Blaetter sollen ausgewaehlt werden
            Set SelSheet = CATIA.ActiveDocument.Selection       'Definition Speichervariable
            Set oSheet = CATIA.ActiveDocument.Sheets.Item(2)    'Zuweisung eines Detail-Blattes auf Variable
            SelSheet.Add (oSheet)                               'Uebergabe der Variable an den Speicher
            SelSheet.Delete                                     'Speicher loeschen
            Set oSheet = Nothing
            f = f + 1                                           'Zaehler hochzaehlen
        End If
        Next
    Box = MsgBox("Es wurden " & r & " Detail-Sheets geloescht!" + Chr(10) + "Klicken Sie OK um mit dem Makro fortzufahren", , "Hinweis")
    End If
End If
End Sub

'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'----------------------------------------Views isolieren
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sub Views_isolieren()

    Dim oDRWDoc As DrawingDocument
    Dim oSheet As DrawingSheet
    Dim DrwSheet As Object
    Dim oView As DrawingView
    Dim oDRWComp As DrawingComponent
    Dim oSel As Selection

    
    Set oDRWDoc = CATIA.ActiveDocument
    Set oSel = oDRWDoc.Selection
    
AbfrgView = MsgBox("Wollen Sie alle verlinken Views isolieren?", vbYesNo, "Views isolieren")
If AbfrgView = 7 Then
    Box = MsgBox("Die Views wurden nicht isoliert. Bitte isolieren Sie die Views haendisch um alle externen Referenzen der Zeichnung zu isolieren" + Chr(10) + "Klicken Sie OK um das Makro zu beenden", , "Hinweis")
    Exit Sub
Else
    For i = 1 To oDRWDoc.Sheets.Count                                     'Schleife fuer alle Sheets
        Set oSheet = oDRWDoc.Sheets.Item(i)
        If Not oSheet.IsDetail Then                                       'Ist das Sheet kein Detail-Sheet?
          For j = 1 To oSheet.Views.Count                                 'Schleife fuer alle Views im Sheet
          Set oView = oSheet.Views.Item(j)
          oView.Isolate
          Next
        End If
    Next
Box = MsgBox("Es wurden alle Views isoliert", , "Hinweis")
End If
End Sub


'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'----------------------------------------Blatt umbenennen
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sub Blatt_umbenennen()

Dim oDRWDoc As DrawingDocument
Dim oSheets As DrawingSheets
Dim oSheet As DrawingSheet
Dim oView As DrawingView
Dim oDRWComp As DrawingComponent
  
Set oDRWDoc = CATIA.ActiveDocument
Set oSheets = oDRWDoc.Sheets

Set oSheet = CATIA.ActiveDocument.Sheets.Item(1)
If oSheet.Name <> "BLATT001" Then
oSheet.Name = "BLATT001"
oSheet.Activate
End If

oSheets.Item("BLATT001").Activate

End Sub
