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!

Script copy/paste Part bodys in Product context 1

Status
Not open for further replies.

Iqtar

Industrial
Feb 3, 2021
5
FR
hello

I'm looking for a script for copy/Paste Part bodys from a positioned Part in a Product context

the one below copies and Pasts the Part body in the new part without the postion of the source in the Product
(The source part is designed in the origine 0,0,0 and positioned in the product)

Script :
oActiveDoc.Activate: oActiveDoc.Selection.Clear
For Each oBody In oDocToTreat.oRefProd.Parent.Part.Bodies
oActiveDoc.Activate: oActiveDoc.Selection.Clear
If oBody.InBooleanOperation = False Then
oActiveDoc.Selection.Add oBody
oActiveDoc.Selection.Copy: oActiveDoc.Selection.Clear
oNewPartDoc.Activate
oNewPartDoc.Product.PartNumber = oDocToTreat.oRefProd.PartNumber
oNewPartDoc.Selection.Clear
oNewPartDoc.Selection.Add oNewPartDoc.Part
On Error Resume Next
oNewPartDoc.Selection.PasteSpecial "CATPrtResultWithOutLink"
On Error GoTo 0
oNewPartDoc.Selection.Clear
oDocToTreat.oRefProd.Parent.Selection.Clear
End If
Next oBody

oNewPartDoc.Selection.Clear
oDocToTreat.oRefProd.Parent.Selection.Clear


Thanks
 
Replies continue below

Recommended for you

Is your NewPartDoc in the same context/structure as your DocToTreat?

regards,
LWolf
 
hello Lwolf
No, it isn't.
The newPatDoc is in another context structure without any position
 
well, if it was, then I guess you'd have the correct position...

regards,
LWolf
 
has anyone got any Idea's please ?
 
Do you want to keep exactly the same position and orientation in global coordinates?
 
yes, the copied Part bodys must keep the same position and orientation in global coordinates as the source part in the structure context.

In my case :
- DocToTreat (source Part)is designed in the origine 0,0,0 and positioned in the product ( X,Y,Z)
- NewPartDoc (traget) not associated with any structure

expected results:
- the Part bodys in NewPartDoc must be positioned correctly as the same position and orientation in global coordinates as DocToTreat :

the Part bodys must have the position XYZ in the NewPartDoc
 
Well, my understanding is that you have a multi-bodies part in a Product and you'd like to create for each of the bodies a new Part and after the copy the bodies to fall in the same position as they were in the source part. If that's correct all you have to do is to use the Product to create a reference from each of the bodies then use the ProductDocument selection to copy and paste. Mandatory (if I remember correctly): the source part must be opened separately in its own window.

L.E. Something like this:

Code:
Sub CATMain()

    Dim oProdDoc As ProductDocument
    Set oProdDoc = CATIA.ActiveDocument
    
    Dim oRootProd As Product
    Set oRootProd = oProdDoc.Product
    
    Dim oSel As Selection
    Set oSel = oProdDoc.Selection
    
    Dim oSourceProd As Product
    Set oSourceProd = oRootProd.Products.Item(1)
    
    Dim oSourcePart As Part
    Set oSourcePart = oSourceProd.ReferenceProduct.Parent.Part
    
    Dim oRef As Reference
    Dim oBody As Body
    Dim oNewProd As Product
    Dim oNewPart As Part
    
    For Each oBody In oSourcePart.Bodies
        Set oNewProd = oRootProd.Products.AddNewComponent("Part", oBody.Name)
        Set oNewPart = oNewProd.ReferenceProduct.Parent.Part
        Set oRef = oRootProd.CreateReferenceFromName(oRootProd.PartNumber & "/" & oSourceProd.Name & "/!" & oBody.Name)
        With oSel
            .Clear
            .Add oRef
            .Copy
            .Clear
            .Add oNewPart
            .PasteSpecial "CATPrtResultWithOutLink"
            .Clear
        End With
    Next
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top