RobotStudio event

Copy Part

Options

Hello

I am attempting to use the API to copy a part in the active station.

The following code works well, except when the part has many entities, it takes some time to complete.

 

Private Sub CopyPart()
   
    Dim p As Part
    Dim e As Entity
    Dim cbpCommandBarPopup As RSEXT.CommandBarPopup
   
   
    ActiveStation.UCS = ActiveStation
    Set p = ActiveStation.Parts.Add

'catches the edit menu for the copy paste commands

    Set cbpCommandBarPopup = RSE.CommandBars("Menu Bar").Controls("Edit")       
    'Copies the parts
    ActiveStation.Selections.RemoveAll
    For Each e In ActiveStation.Parts("part_to_copy").Entities()
     ActiveStation.Selections.Add e
    Next e
    cbpCommandBarPopup.Controls("Copy").Execute
    ActiveStation.Selections.RemoveAll
    ActiveStation.Selections.Add p
    cbpCommandBarPopup.Controls("Paste").Execute
   
End Sub

 

How does one just copy a part, without haveing to traverse through all of the entities?

I tried the following without success:

Private Sub CopyPart()
   
    Dim p As Part
    Dim cbpCommandBarPopup As RSEXT.CommandBarPopup
    
   
'catches the edit menu for the copy paste commands

    Set cbpCommandBarPopup = RSE.CommandBars("Menu Bar").Controls("Edit")       
    'Copy the part
    ActiveStation.Selections.RemoveAll
     ActiveStation.Selections.Add ActiveStation.Parts("part_to_copy")
    cbpCommandBarPopup.Controls("Copy").Execute
    ActiveStation.Selections.RemoveAll
    ActiveStation.Selections.Add ActiveStation
    cbpCommandBarPopup.Controls("Paste").Execute
   
End Sub

The key must be the line :ActiveStation.Selections.Add ActiveStation

But I have tried many different things without success.

I was successful in copying into an assembly by just the part reference, but how do you paste into the components part of the station? image

Looking forward to your reply.

Tom

 

Thomas H. Johnston
PACs Application Engineer

Comments

  • John Wiberg
    Options

    imageYou don't.

    It's not possible from the API to directly select the 'components' in the browser.

    But... If you want to do something really 'baaad' you could always use the "sendkeys" to go to the components.

    So my recomendation would be to do it like you did first. Put it in an assembly and be happy.

    image

  • A bit more to the copy paste issue - I tried the same thing, using the CommandBarPopUp approach, and it worked fine when I ran the macro, but during run time, 'Operation is not allowed'.

    Is there a way to copy/paste during run time? (ie for adding a new box to a conveyor by copying a reference box, rather than creating a new one from scratch)

    Dean Thomas
    ABB Support Engineer
    Channel Partners Australia
  • Hello Dean,

    Here comes two code examples that work when you "Play" a simulation. I'm running on RobotStudio 3.0, what version do you have?

     

    Sub CopyExample()
    'When you copy you need to copy on child level and paste on parent level
        Dim prtP1 As Part
        Dim prtP2 As Part
        Dim entE As Entity
        Dim tfmT As New Transform
        'We will now create two parts, one with a box and one that is empty
        Set prtP1 = ActiveStation.Parts.Add
        Set prtP2 = ActiveStation.Parts.Add
        Set entE = prtP1.CreateSolidBox(tfmT, 0.5, 0.5, 0.5)
        MsgBox "2 parts 1 entity"
       
        'To be able to copy stuff we need to have them selected
        ActiveStation.Selections.RemoveAll
        ActiveStation.Selections.Add entE
        MsgBox "The box is selected"
       
        'Since copy doesn't exist on the object we need to call the menu
        Application.RSE.CommandBars("Menu Bar").Controls("Edit").Controls("Copy").Execute
       
        'Now we need to select the other part
        ActiveStation.Selections.RemoveAll
        ActiveStation.Selections.Add prtP2
        MsgBox "2nd part selected"
       
        'Finally we paste in the object, again from the menu
        Application.RSE.CommandBars("Menu Bar").Controls("Edit").Controls("Paste").Execute
        MsgBox "Now the second part should have a box copy"
    End Sub

     

     

    Sub CopyExample2()
    'When you copy you need to copy on child level and paste on parent level
        Dim prtP1 As Part
        Dim prtP2 As Part
        Dim entE As Entity
        Dim tfmT As New Transform
        'We will now create two parts, one with a box and one that is empty
        Set prtP1 = ActiveStation.Parts.Add
        Set prtP2 = ActiveStation.Parts.Add
        Set entE = prtP1.CreateSolidBox(tfmT, 0.5, 0.5, 0.5)
        MsgBox "2 parts 1 entity"
       
        'To be able to copy stuff we need to have them selected
        ActiveStation.Selections.RemoveAll
        ActiveStation.Selections.Add entE
        MsgBox "The box is selected"
       
        'Since copy doesn't exist on the object we need to call the menu
        'Application.RSE.CommandBars("Menu Bar").Controls("Edit").Controls("Copy").Execute
        Dim cbc As CommandBarControl
        Set cbc = Application.RSE.CommandBars.FindControl(, 57634)
        cbc.Execute
        'Now we need to select the other part
        ActiveStation.Selections.RemoveAll
        ActiveStation.Selections.Add prtP2
        MsgBox "2nd part selected"
       
        'Finally we paste in the object, again from the menu
        'Application.RSE.CommandBars("Menu Bar").Controls("Edit").Controls("Paste").Execute
        Set cbc = Application.RSE.CommandBars.FindControl(, 57637)
        cbc.Execute
        MsgBox "Now the second part should have a box copy"
    End Sub

     

    'To get hold of the ID's above run the following.

    Sub id()
        MsgBox Application.RSE.CommandBars("Menu Bar").Controls("Edit").Controls("Paste").id
        MsgBox Application.RSE.CommandBars("Menu Bar").Controls("Edit").Controls("Copy").id
    End Sub

    Ulrika Mor?n38036,4532638889