Catia VBA Looping through PartBody and Pasting to New Part
Catia VBA Looping through PartBody and Pasting to New Part
(OP)
Hello,
Apologies if this has been posted before, I have searched and have not found a solution. I am new to Catia VBA, but have used Excel VBA before.
What do I want to do? I have created a Part with a number of Surfaces. I need to create an IGES files containing a single Surface for all the Surfaces with the Part.
How I have approached the problem, I have a Part that contains 10+ surfaces, using 'SelectElement3' I can select the the surfaces I want to export and I have created a loop that loops through the number of Surfaces I have selected. I then create a new part and copy & paste the Surface and then create the IGES file.
What is going wrong, Instead of pasting a single surface into the New Part the code pastes all the selected surfaces and then creates the IGES files! I feel I am struggling to understand the differences between Objects, Selection, PartDocument and Part. I also do not understand how to loop through the selected PartBody and then pass this to the object that pastes the Surface into the New Part.
Here is my Code,
Option Explicit
Private Sub subCreateIGES()
' Objects
Dim objSel As Selection
Dim objSelLB As Object
Dim objTgtDoc As PartDocument
Dim objTgt As Part
Dim objTgtSel As Selection
' Array
Dim InputObjectType(0)
' String
Dim Status As String
Dim strPartName As String
' Integer
Dim intCounter As Integer
Set objSel = CATIA.ActiveDocument.Selection
Set objSelLB = objSel
InputObjectType(0) = "Face"
Status = objSelLB.SelectElement3(InputObjectType, "Select points", True, CATMultiSelTriggWhenUserValidatesSelection, False)
If Status = "Cancel" Then Exit Sub
For intCounter = 1 To objSelLB.Count2
Set objTgtDoc = CATIA.Documents.Add("Part")
Set objTgt = objTgtDoc.Part
Set objTgtSel = objTgtDoc.Selection
Set objTgtDoc = CATIA.ActiveDocument
' Sets and Pastes in Target file as result
objTgtSel.Clear
objTgtSel.Add objTgt.Bodies.Item("PartBody")
objTgtSel.PasteSpecial ("CATPrtResult")
' Creates IGES file
CATIA.DisplayFileAlerts = False
objTgtDoc.ExportData "P:\Temp\IGES_" & intCounter & ".igs", "igs"
CATIA.DisplayFileAlerts = True
objTgtDoc.Close
Next intCounter
End Sub
Thanks in advance any help you may be able to provide.
Thanks James
Apologies if this has been posted before, I have searched and have not found a solution. I am new to Catia VBA, but have used Excel VBA before.
What do I want to do? I have created a Part with a number of Surfaces. I need to create an IGES files containing a single Surface for all the Surfaces with the Part.
How I have approached the problem, I have a Part that contains 10+ surfaces, using 'SelectElement3' I can select the the surfaces I want to export and I have created a loop that loops through the number of Surfaces I have selected. I then create a new part and copy & paste the Surface and then create the IGES file.
What is going wrong, Instead of pasting a single surface into the New Part the code pastes all the selected surfaces and then creates the IGES files! I feel I am struggling to understand the differences between Objects, Selection, PartDocument and Part. I also do not understand how to loop through the selected PartBody and then pass this to the object that pastes the Surface into the New Part.
Here is my Code,
Option Explicit
Private Sub subCreateIGES()
' Objects
Dim objSel As Selection
Dim objSelLB As Object
Dim objTgtDoc As PartDocument
Dim objTgt As Part
Dim objTgtSel As Selection
' Array
Dim InputObjectType(0)
' String
Dim Status As String
Dim strPartName As String
' Integer
Dim intCounter As Integer
Set objSel = CATIA.ActiveDocument.Selection
Set objSelLB = objSel
InputObjectType(0) = "Face"
Status = objSelLB.SelectElement3(InputObjectType, "Select points", True, CATMultiSelTriggWhenUserValidatesSelection, False)
If Status = "Cancel" Then Exit Sub
For intCounter = 1 To objSelLB.Count2
Set objTgtDoc = CATIA.Documents.Add("Part")
Set objTgt = objTgtDoc.Part
Set objTgtSel = objTgtDoc.Selection
Set objTgtDoc = CATIA.ActiveDocument
' Sets and Pastes in Target file as result
objTgtSel.Clear
objTgtSel.Add objTgt.Bodies.Item("PartBody")
objTgtSel.PasteSpecial ("CATPrtResult")
' Creates IGES file
CATIA.DisplayFileAlerts = False
objTgtDoc.ExportData "P:\Temp\IGES_" & intCounter & ".igs", "igs"
CATIA.DisplayFileAlerts = True
objTgtDoc.Close
Next intCounter
End Sub
Thanks in advance any help you may be able to provide.
Thanks James





RE: Catia VBA Looping through PartBody and Pasting to New Part
Those surfaces are joined surfaces, or single surfaces in your file?
You need to see, what's happening after the objTgtSel.PasteSpecial ("CATPrtResult"). Probably when you make the paste, every surface is already there, and then all surfaces are converted.
Tiago Figueiredo
Tooling Engineer
Youtube channel:
https://www.youtube.com/channel/UC1qdlBeJJEgMgpPLV...
RE: Catia VBA Looping through PartBody and Pasting to New Part
Thank you for your response.
They are single surfaces.
Correct, after the code 'objTgtSel.PasteSpecial ("CATPrtResult")' all of the selected surfaces have been pasted into the new Part, which are then converted into the IGES file. I want to loop through the selected surfaces and only single surfaces to be pasted and converted to IGES with each loop.
Thanks James
RE: Catia VBA Looping through PartBody and Pasting to New Part
CODE -->
Private Sub subCreateIGES() ' Objects Dim objSel As Selection Dim objSelLB As Object Dim objTgtDoc As PartDocument Dim objTgt As Part Dim objTgtSel As Selection ' Array Dim InputObjectType(0) ' String Dim Status As String Dim strPartName As String ' Integer Dim intCounter As Integer Dim Num_Surfaces As Integer Dim Surface() Set objSel = CATIA.ActiveDocument.Selection Set objSelLB = objSel InputObjectType(0) = "Face" Status = objSelLB.SelectElement3(InputObjectType, "Select points", True, CATMultiSelTriggWhenUserValidatesSelection, False) If Status = "Cancel" Then Exit Sub Num_Surfaces = objSelLB.Count2 ReDim Preserve Surface(Num_Surfaces) For intCounter = 1 To Num_Surfaces Surface(intCounter) = objSelLB.Item(intCounter).Value Next For intCounter = 1 To objSelLB.Count2 objSelLB.Clear objSelLB.Add (Surface(intCounter)) objSelLB.Copy Set objTgtDoc = CATIA.Documents.Add("Part") Set objTgt = objTgtDoc.Part Set objTgtSel = objTgtDoc.Selection Set objTgtDoc = CATIA.ActiveDocument ' Sets and Pastes in Target file as result objTgtSel.Clear objTgtSel.Add objTgt.Bodies.Item("PartBody") objTgtSel.PasteSpecial ("CATPrtResult") ' Creates IGES file CATIA.DisplayFileAlerts = False objTgtDoc.ExportData "P:\Temp\IGES_" & intCounter & ".igs", "igs" CATIA.DisplayFileAlerts = True objTgtDoc.Close Next intCounter End SubTiago Figueiredo
Tooling Engineer
Youtube channel:
https://www.youtube.com/channel/UC1qdlBeJJEgMgpPLV...
RE: Catia VBA Looping through PartBody and Pasting to New Part
You are a star, thank you so much.
Cheers James
RE: Catia VBA Looping through PartBody and Pasting to New Part
Did you understand the differences?
Tiago Figueiredo
Tooling Engineer
Youtube channel:
https://www.youtube.com/channel/UC1qdlBeJJEgMgpPLV...