Hello all,
I'm trying a coding in vb.net
I select bodies - parts - and target part value from CATIA and adding into listview.
After that I'm planning selected bodies copying from parts and paste to target part.
But something is going wrong.
I tried search body names in selected parts. But not worked as well. Maybe cycle is better. Any idea?
I'm trying to make like this:
Here is my code:
I'm trying a coding in vb.net
I select bodies - parts - and target part value from CATIA and adding into listview.
After that I'm planning selected bodies copying from parts and paste to target part.
But something is going wrong.
I tried search body names in selected parts. But not worked as well. Maybe cycle is better. Any idea?
I'm trying to make like this:
Here is my code:
Code:
Imports INFITF
Imports MECMOD
Imports PARTITF
Imports ProductStructureTypeLib
Imports System.Runtime.InteropServices
Imports System.Windows.Forms.VisualStyles.VisualStyleElement
Public Class Form1
Private bodyToCopy As Body = Nothing
Private targetProduct As Product = Nothing
Private Sub copyBodyBtn_Click(sender As Object, e As EventArgs) Handles copyBodyBtn.Click
Dim CATIA As Application
Try
CATIA = CType(Marshal.GetActiveObject("CATIA.Application"), Application)
Catch ex As Exception
MessageBox.Show("CATIA çalışmıyor. Lütfen başlatın.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
Dim productDoc As ProductDocument
Try
productDoc = CType(CATIA.ActiveDocument, ProductDocument)
Catch ex As Exception
MessageBox.Show("Lütfen bir ProductDocument açın.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
Dim selection As Selection = productDoc.Selection
Dim filter As Object() = {"Body"} ' Filter dizisini Object türüne dönüştür
Dim status As String = selection.SelectElement3(filter, "Lütfen Body'leri seçin.", False, CATMultiSelectionMode.CATMultiSelTriggWhenUserValidatesSelection, False)
If status <> "Normal" Then
MessageBox.Show("Body seçimi iptal edildi.", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
End If
bodyList.Clear()
bodyList.View = View.Details
bodyList.Columns.Add("Body Name", 200) ' "Body Name" sütunu oluştur
For i As Integer = 1 To selection.Count
Dim body As Body = TryCast(selection.Item(i).Value, Body)
If body IsNot Nothing Then
Dim listItem As New ListViewItem(body.Name)
bodyList.Items.Add(listItem)
End If
Next
End Sub
Private Sub partSelectBtn_Click(sender As Object, e As EventArgs) Handles partSelectBtn.Click
Dim CATIA As Application
Try
CATIA = CType(Marshal.GetActiveObject("CATIA.Application"), Application)
Catch ex As Exception
MessageBox.Show("CATIA çalışmıyor. Lütfen başlatın.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
Dim productDoc As ProductDocument
Try
productDoc = CType(CATIA.ActiveDocument, ProductDocument)
Catch ex As Exception
MessageBox.Show("Lütfen bir ProductDocument açın.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
Dim selection As Selection = productDoc.Selection
Dim filter As Object() = {"Product"} ' Product için filtre
Dim status As String = selection.SelectElement3(filter, "Lütfen Part'ları seçin.", False, CATMultiSelectionMode.CATMultiSelTriggWhenUserValidatesSelection, False)
If status <> "Normal" Then
MessageBox.Show("Part seçimi iptal edildi.", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
End If
partsList.Clear()
partsList.View = View.Details
partsList.Columns.Add("Part Name", 200) ' "Part Name" sütunu oluştur
For i As Integer = 1 To selection.Count
Dim selectedProduct As Product = TryCast(selection.Item(i).Value, Product)
If selectedProduct IsNot Nothing Then
Dim listItem As New ListViewItem(selectedProduct.Name)
partsList.Items.Add(listItem)
End If
Next
End Sub
Private Sub targetPart_Click(sender As Object, e As EventArgs) Handles targetPart.Click
Dim CATIA As Application
Try
CATIA = CType(Marshal.GetActiveObject("CATIA.Application"), Application)
Catch ex As Exception
MessageBox.Show("CATIA çalışmıyor. Lütfen başlatın.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
Dim productDoc As ProductDocument
Try
productDoc = CType(CATIA.ActiveDocument, ProductDocument)
Catch ex As Exception
MessageBox.Show("Lütfen bir ProductDocument açın.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
Dim selection As Selection = productDoc.Selection
Dim filter As Object() = {"Product"} ' Filtre "Product" için
Dim status As String = selection.SelectElement2(filter, "Lütfen bir hedef Part seçin.", False)
If status <> "Normal" Then
MessageBox.Show("Part seçimi iptal edildi.", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
End If
Dim selectedProduct As Product = TryCast(selection.Item(1).Value, Product)
If selectedProduct IsNot Nothing Then
targetList.Clear()
targetList.View = View.Details
targetList.Columns.Add("Target Part Name", 200) ' "Target Part Name" sütunu
Dim listItem As New ListViewItem(selectedProduct.Name)
targetList.Items.Add(listItem)
targetProduct = selectedProduct ' Hedef Part'ı kaydet
MessageBox.Show($"Hedef Part seçildi: {selectedProduct.Name}", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Geçersiz seçim yapıldı.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim CATIA As Application
Try
CATIA = CType(Marshal.GetActiveObject("CATIA.Application"), Application)
Catch ex As Exception
MessageBox.Show("CATIA çalışmıyor. Lütfen başlatın.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
Dim productDoc As ProductDocument
Try
productDoc = CType(CATIA.ActiveDocument, ProductDocument)
Catch ex As Exception
MessageBox.Show("Lütfen bir ProductDocument açın.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
Dim rootProduct As Product = productDoc.Product
Dim selection As Selection = productDoc.Selection
' Kullanıcıdan hedef Part ve kaynak Body seçimlerini doğrula
Dim partAName As String = txtMainPart.Text.Trim()
Dim bodyNames As String() = txtBody.Text.Trim().Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
If String.IsNullOrWhiteSpace(partAName) OrElse bodyNames.Length = 0 Then
MessageBox.Show("Lütfen hedef Part ve Body'leri seçin.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
' Hedef parçayı bul
Dim partA As Part = Nothing
Try
For i As Integer = 1 To rootProduct.Products.Count
Dim currentProduct As Product = rootProduct.Products.Item(i)
If currentProduct.Name = partAName Then
partA = CType(currentProduct.ReferenceProduct.Parent, PartDocument).Part
Exit For
End If
Next
If partA Is Nothing Then
Throw New Exception("Hedef Part bulunamadı.")
End If
Catch ex As Exception
MessageBox.Show("Hedef Part bulunamadı: " & ex.Message, "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
' Assembly ortamını kontrol et
Dim designEnv As String = CATIA.GetWorkbenchId()
If designEnv <> "Assembly" Then
CATIA.StartWorkbench("Assembly")
End If
' Body'leri tek tek işleme
Try
For Each bodyName In bodyNames
Dim bodyA As Body = Nothing
' Kaynak Body'yi bul
For i As Integer = 1 To rootProduct.Products.Count
Dim currentProduct As Product = rootProduct.Products.Item(i)
Try
Dim partB As Part = CType(currentProduct.ReferenceProduct.Parent, PartDocument).Part
bodyA = partB.Bodies.Item(bodyName)
Exit For
Catch
' Hata oluşursa bir sonraki ürüne geç
End Try
Next
If bodyA Is Nothing Then
MessageBox.Show($"Body '{bodyName}' bulunamadı.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Continue For
End If
' Kopyalama ve yapıştırma işlemleri
selection.Clear()
selection.Add(bodyA)
selection.Copy()
selection.Clear()
selection.Add(partA)
selection.PasteSpecial("CATPrtResult")
Next
partA.Update()
MessageBox.Show("Tüm Body'ler başarıyla kopyalandı ve hedef Part'a yapıştırıldı.", "Başarılı", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("İşlem sırasında bir hata oluştu: " & ex.Message, "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class
Last edited: