Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catia VBA Looping through PartBody and Pasting to New Part

Status
Not open for further replies.

James Eng

Aerospace
Jul 18, 2017
3
GB
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
 
Replies continue below

Recommended for you

Hello,

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:
 
Hello Tiago,

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
 
Try this:

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 Sub

Tiago Figueiredo
Tooling Engineer

Youtube channel:
 
Hello Tiago,

You are a star, thank you so much.

Cheers James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top