mnash60
Materials
- Feb 21, 2012
- 29
I'm new to catia vb hoping theirs someone that can help me. I found this script that works for me except for to things. One it errors out if there is a multiple bodies with the same name and i need the body color of the initial body copied. below is the code.
Dim KomponenteNeu As Products
Dim KoerperName
Dim OpenKoerperName
Dim hybridBodies As Document
Dim Koerper As Object
Dim QuellFenster As Window
Dim Letztekoerper
Dim UserSel As Selection
Sub CATMain()
Dim Activdocu As Document
Set Activdocu = CATIA.ActiveDocument
'---------------------------------------------------
' Neue Product
'---------------------------------------------------
Dim PosString As Long
partName = CATIA.ActiveDocument.name
Dim docu As Documents
Set docu = CATIA.Documents
Dim productDocu As Document
Set productDocu = docu.Add("Product")
Dim ProductNeu As Product
Set ProductNeu = productDocu.Product
PosString = InStr(1, partName, ".CATPart")
ProductNeu.PartNumber = Mid(partName, 1, PosString - 1)
'------------------------------------------------------
FensterNebeneinander
Set QuellFenster = CATIA.Windows.Item(1)
QuellFenster.Activate
Dim partBodies As Bodies
'Set Activdocu = CATIA.ActiveDocument
Set partBodies = Activdocu.Part.Bodies
Dim koerperAnzahl
koerperAnzahl = partBodies.Count
Dim UserSel As Object
For i = 1 To koerperAnzahl
Set Koerper = partBodies.Item(i)
KoerperName = Koerper.name
If Right(KoerperName, 1) = "\" Then
KoerperName = Left(KoerperName, Len(KoerperName) - 1)
End If
KoerperName = Replace(KoerperName, "\", "_")
'Koerper kopieren
Activdocu.Selection.Clear
Activdocu.Selection.Add Koerper
Activdocu.Selection.Copy
Activdocu.Selection.Clear
'if part already exist
'Part erzeugen und Koerper einfuegen
Dim PartNeu As Product
Set PartNeu = ProductNeu.Products.AddNewComponent("Part", CStr(KoerperName))
' Fenster mit neue Product activieren
ProductNeu.Parent.Activate
' Alle Parts suchen
PartSuchen ProductNeu.Parent, UserSel
'ProductNeu.parent.Selection.Clear
'ProductNeu.parent.Selection.Add UserSel.Item(UserSel.Count).Value
ProductNeu.Parent.Selection.Clear
ProductNeu.Parent.Selection.Add ProductNeu.Products.Item(PartNeu).ReferenceProduct.Parent.Part
ProductNeu.Parent.Selection.PasteSpecial "CATPrtResult"
ProductNeu.Parent.Selection.Clear
Next
Dim hybridBodies As hybridBodies
'Set Activdocu = CATIA.ActiveDocument
Set hybridBodies = Activdocu.Part.hybridBodies
koerperAnzahl = hybridBodies.Count
For i = 1 To koerperAnzahl
Set Koerper = hybridBodies.Item(i)
KoerperName = Koerper.name
If Right(KoerperName, 1) = "\" Then
KoerperName = Left(KoerperName, Len(KoerperName) - 1)
End If
KoerperName = Replace(KoerperName, "\", "_")
'Koerper kopieren
Activdocu.Selection.Clear
Activdocu.Selection.Add Koerper
Activdocu.Selection.Copy
Activdocu.Selection.Clear
'Part erzeugen und Koerper einfuegen
Set PartNeu = ProductNeu.Products.AddNewComponent("Part", CStr(KoerperName))
' Fenster mit neue Product activieren
ProductNeu.Parent.Activate
' Alle Parts suchen
PartSuchen ProductNeu.Parent, UserSel
'ProductNeu.parent.Selection.Clear
'ProductNeu.parent.Selection.Add UserSel.Item(UserSel.Count).Value
ProductNeu.Parent.Selection.Clear
ProductNeu.Parent.Selection.Add ProductNeu.Products.Item(PartNeu).ReferenceProduct.Parent.Part
ProductNeu.Parent.Selection.Paste
ProductNeu.Parent.Selection.Clear
Next
' Product actualisieren
ProductNeu.ApplyWorkMode DESIGN_MODE
On Error Resume Next
ProductNeu.Update
If Err <> 0 Then
MsgBox "Problem with update!" & vbLf & vbLf & "Please update manual!", vbCritical + vbOKOnly, "Update-Error"
End If
On Error GoTo 0
End Sub
Sub PartSuchen(oPartDoc1, UserSel)
Dim E As Object 'CATBSTR
Dim Was(0)
Was(0) = "Part"
'Dim UserSel As Object
Set UserSel = oPartDoc1.Selection
UserSel.Clear
'Let us first fill the CSO with all the objects of the model
UserSel.Search ("CATPrtSearch.PartFeature,all")
'E = UserSel.SelectElement2(Was, "Alle CATPart wählen", True)
'Letztekoerper = UserSel.Count
End Sub
Sub FensterNebeneinander()
Dim windows1 As Windows
Set windows1 = CATIA.Windows
windows1.Arrange catArrangeTiledVertical
End Sub
Can someone please help me out?
Dim KomponenteNeu As Products
Dim KoerperName
Dim OpenKoerperName
Dim hybridBodies As Document
Dim Koerper As Object
Dim QuellFenster As Window
Dim Letztekoerper
Dim UserSel As Selection
Sub CATMain()
Dim Activdocu As Document
Set Activdocu = CATIA.ActiveDocument
'---------------------------------------------------
' Neue Product
'---------------------------------------------------
Dim PosString As Long
partName = CATIA.ActiveDocument.name
Dim docu As Documents
Set docu = CATIA.Documents
Dim productDocu As Document
Set productDocu = docu.Add("Product")
Dim ProductNeu As Product
Set ProductNeu = productDocu.Product
PosString = InStr(1, partName, ".CATPart")
ProductNeu.PartNumber = Mid(partName, 1, PosString - 1)
'------------------------------------------------------
FensterNebeneinander
Set QuellFenster = CATIA.Windows.Item(1)
QuellFenster.Activate
Dim partBodies As Bodies
'Set Activdocu = CATIA.ActiveDocument
Set partBodies = Activdocu.Part.Bodies
Dim koerperAnzahl
koerperAnzahl = partBodies.Count
Dim UserSel As Object
For i = 1 To koerperAnzahl
Set Koerper = partBodies.Item(i)
KoerperName = Koerper.name
If Right(KoerperName, 1) = "\" Then
KoerperName = Left(KoerperName, Len(KoerperName) - 1)
End If
KoerperName = Replace(KoerperName, "\", "_")
'Koerper kopieren
Activdocu.Selection.Clear
Activdocu.Selection.Add Koerper
Activdocu.Selection.Copy
Activdocu.Selection.Clear
'if part already exist
'Part erzeugen und Koerper einfuegen
Dim PartNeu As Product
Set PartNeu = ProductNeu.Products.AddNewComponent("Part", CStr(KoerperName))
' Fenster mit neue Product activieren
ProductNeu.Parent.Activate
' Alle Parts suchen
PartSuchen ProductNeu.Parent, UserSel
'ProductNeu.parent.Selection.Clear
'ProductNeu.parent.Selection.Add UserSel.Item(UserSel.Count).Value
ProductNeu.Parent.Selection.Clear
ProductNeu.Parent.Selection.Add ProductNeu.Products.Item(PartNeu).ReferenceProduct.Parent.Part
ProductNeu.Parent.Selection.PasteSpecial "CATPrtResult"
ProductNeu.Parent.Selection.Clear
Next
Dim hybridBodies As hybridBodies
'Set Activdocu = CATIA.ActiveDocument
Set hybridBodies = Activdocu.Part.hybridBodies
koerperAnzahl = hybridBodies.Count
For i = 1 To koerperAnzahl
Set Koerper = hybridBodies.Item(i)
KoerperName = Koerper.name
If Right(KoerperName, 1) = "\" Then
KoerperName = Left(KoerperName, Len(KoerperName) - 1)
End If
KoerperName = Replace(KoerperName, "\", "_")
'Koerper kopieren
Activdocu.Selection.Clear
Activdocu.Selection.Add Koerper
Activdocu.Selection.Copy
Activdocu.Selection.Clear
'Part erzeugen und Koerper einfuegen
Set PartNeu = ProductNeu.Products.AddNewComponent("Part", CStr(KoerperName))
' Fenster mit neue Product activieren
ProductNeu.Parent.Activate
' Alle Parts suchen
PartSuchen ProductNeu.Parent, UserSel
'ProductNeu.parent.Selection.Clear
'ProductNeu.parent.Selection.Add UserSel.Item(UserSel.Count).Value
ProductNeu.Parent.Selection.Clear
ProductNeu.Parent.Selection.Add ProductNeu.Products.Item(PartNeu).ReferenceProduct.Parent.Part
ProductNeu.Parent.Selection.Paste
ProductNeu.Parent.Selection.Clear
Next
' Product actualisieren
ProductNeu.ApplyWorkMode DESIGN_MODE
On Error Resume Next
ProductNeu.Update
If Err <> 0 Then
MsgBox "Problem with update!" & vbLf & vbLf & "Please update manual!", vbCritical + vbOKOnly, "Update-Error"
End If
On Error GoTo 0
End Sub
Sub PartSuchen(oPartDoc1, UserSel)
Dim E As Object 'CATBSTR
Dim Was(0)
Was(0) = "Part"
'Dim UserSel As Object
Set UserSel = oPartDoc1.Selection
UserSel.Clear
'Let us first fill the CSO with all the objects of the model
UserSel.Search ("CATPrtSearch.PartFeature,all")
'E = UserSel.SelectElement2(Was, "Alle CATPart wählen", True)
'Letztekoerper = UserSel.Count
End Sub
Sub FensterNebeneinander()
Dim windows1 As Windows
Set windows1 = CATIA.Windows
windows1.Arrange catArrangeTiledVertical
End Sub
Can someone please help me out?