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!

Copy paste break link macro

Status
Not open for further replies.

kevin.rahhal

New member
Mar 16, 2020
2
CA
Hello,

I am writting a macro to be able to copy/paste and breaklink products between 2 different windows. I am able to do it manually so I know it's possible.
I've also written a macro that does it but limited to same window for paste.

I'm wondering what I would have to change with my code to be able to paste to another window.
Any help is appreciated.

Here is what I have :

"Dim selection1 As Object, selStatus, InputObject(0) ' Objects used for selection
InputObject(0) = "Product" ' Only allow products to be selected

Set selection1 = CATIA.ActiveDocument.Selection ' Object used to handle selections
selStatus = selection1.SelectElement3(InputObject, "Select items to copy", True, CATMultiSelTriggWhenUserValidatesSelection, True)

If (selStatus <> "Normal") Then
MsgBox ("Selection cancelled of items to copy. Macro exiting") ' Inform user macro has been cancelled.
Exit Sub
End If

Dim arraySelectedElements() As SelectedElement
ReDim arraySelectedElements(selection1.Count2)

Dim arrayElementCount
For arrayElementCount = 1 To UBound(arraySelectedElements)
Set arraySelectedElements(arrayElementCount - 1) = selection1.Item2(arrayElementCount)
Next

selection1.Clear

selStatus = selection1.SelectElement2(InputObject, "Select where you want to paste the items", False)

If (selStatus <> "Normal") Then
MsgBox ("Selection of location to paste products cancelled. Macro exiting") ' Inform user macro has been cancelled.
Exit Sub
End If

Dim pasteSelectedElement As SelectedElement
Set pasteSelectedElement = selection1.Item2(1)
selection1.Clear

For arrayElementCount = 1 To UBound(arraySelectedElements)
selection1.Add arraySelectedElements(arrayElementCount - 1).LeafProduct
Next

selection1.Copy 'Copies current selected items
selection1.Clear ' Clears the selection of items, so the new product can be selected

selection1.Add pasteSelectedElement.LeafProduct
selection1.PasteSpecial ("CATSpecBreakLink") ' Paste & break link on root product"


When I execute this macro, it will always paste within the same window even if I select a different one
 
Replies continue below

Recommended for you

Hello, you need to switch between windows.
For example, you copy from one window, switch to another and then paste.
Same as manually.

I have some similar macro for the kinda same operation.

[link catiavbmacro.com]catiavbmacro.com[/url]
 
I tried changing the last few lines to this, but still no cigar.

"selection1.Copy 'Copies current selected items
selection1.Clear ' Clears the selection of items, so the new product can be selected

Dim specsAndGeomWindow2 As SpecsAndGeomWindow
Set specsAndGeomWindow2 = CATIA.Windows.Item("ENOVIA5\XXXX.CATProduct")
specsAndGeomWindow2.Activate

selection1.Add pasteSelectedElement.LeafProduct
selection1.PasteSpecial ("CATSpecBreakLink") ' Paste & break link on root product

End Sub"

I checked your site, but wasn't able to spot the similar macro. Could you link me to it please?
 
I have some code that copies pastes from one window to another. It's similar but not exactly what you're doing. Maybe it'll give you a new idea.

I've been meaning to change this code myself as it tends to error if I have more than just the source document open. It indexes to the first item open in CATIA ( CATIA.Windows.Item(1) ) rather than the correct destination document.

Hope it helps.

Code:
'Change window and start adding CATParts from bodies
    Dim window1 As Window
    Set window1 = CATIA.Windows.Item(1)
    window1.Activate
    Set Activdoc1 = CATIA.ActiveDocument


'unrelated object renaming code is here...


 'copy part body to memory
        Activdoc1.Selection.Clear
        Activdoc1.Selection.Add Body1
        Activdoc1.Selection.Copy
        Activdoc1.Selection.Clear
        
        'Start adding CATParts to the new assembly
        Dim Part1 As Product
        Set Part1 = NewProd1.Products.AddNewComponent("CATPart", CStr(bodyName))
        
        NewProd1.Parent.Activate
        
        NewProd1.Parent.Selection.Clear
        NewProd1.Parent.Selection.Add NewProd1.Products.Item(Part1).ReferenceProduct.Parent.Part
        NewProd1.Parent.Selection.Paste
        NewProd1.Parent.Selection.Clear
 
Hello,
I was busy to answer you, today I find some time to do it.

Here are lines that I used
Code:
        Dim partDocument1 As Document

        partDocument1 = MyCATIA.ActiveDocument
        Dim K = partDocument1.Name

        Dim selection1 As Selection
        selection1 = partDocument1.Selection
        selection1.Clear()
        

         Dim part1 As Part
         part1 = partDocument1.Part

         selection1.Add(metoda(i))
         selection1.Copy()

          Dim windows1
            windows1 = MyCATIA.Windows
            Dim specsAndGeomWindow1 As SpecsAndGeomWindow
            specsAndGeomWindow1 = windows1.Item(TextBox37.Text & ".CATPart")
            
            'this textbox is part that I selected before all of this

            specsAndGeomWindow1.Activate()
            Dim partDocument2 As PartDocument
            partDocument2 = MyCATIA.ActiveDocument

            Dim selection2 As Selection
            selection2 = partDocument2.Selection
            selection2.Clear()
            Dim part2 As Part
            part2 = partDocument2.Part
            selection2.Add(part2)

            selection2.PasteSpecial("CATPrtResultWithOutLink")
            Dim specsAndGeomWindow1x As SpecsAndGeomWindow
            specsAndGeomWindow1x = windows1.Item(K)
            specsAndGeomWindow1x.Activate()
            selection1.Clear()
            selection2.Clear()

here are those lines you need, this is not full code, I just took lines you need.

See more cool macros [link catiavbmacro.com]catiavbmacro.com[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top