Wednesday, June 08, 2011

Swap Component Positions

This will swap the positions of two components that are choosen, including the rotation of the part and the rotation and position of the designators. Based on a script that came with Summer '09 but more useful. If a component is not selected the script will end to avoid a crash.

Sub SwapComps

Dim Board
Dim CompA
Dim CompB
Dim CompX
Dim CompY
Dim CompR
Dim DesX
Dim DesY
Dim DesR

Set Board = PCBServer.GetCurrentPCBBoard
If Board is Nothing Then Exit Sub
Pcbserver.PreProcess

While Board.ChooseLocation(x,y, "Select First Componet") = True

  Set CompA = Board.GetObjectAtXYAskUserIfAmbiguous(x,y,MkSet(eComponentObject)_
  ,AllLayers, eEditAction_Select)
  If CompA Is Nothing Then Exit Sub 'Needed, if no comp is selected, crash

   Call Board.ChooseLocation(x,y, "Select Second Componet")
  Set CompB = Board.GetObjectAtXYAskUserIfAmbiguous(x,y,MkSet(eComponentObject)_
  ,AllLayers, eEditAction_Select)
  If CompB Is Nothing Then Exit Sub 'Needed, if no comp is selected, crash

   CompX = CompA.X
   CompY = CompA.Y
   CompR = CompA.Rotation
   DesX  = CompA.Name.XLocation
   DesY  = CompA.Name.YLocation
   DesR  = CompA.Name.Rotation

   Call PCBServer.SendMessageToRobots(CompA.I_ObjectAddress,c_Broadcast,_
   PCBM_BeginModify, c_NoEventData)
   CompA.X = CompB.X
   CompA.Y = CompB.Y
   CompA.Rotation = CompB.Rotation
   CompA.ChangeNameAutoposition = eAutoPos_Manual
   Call PCBServer.SendMessageToRobots(CompA.I_ObjectAddress,c_Broadcast,_
   PCBM_EndModify , c_NoEventData)

   Call PCBServer.SendMessageToRobots(CompA.Name.I_ObjectAddress, c_Broadcast,_
   PCBM_BeginModify, c_NoEventData)
   CompA.Name.XLocation = CompB.Name.XLocation
   CompA.Name.YLocation = CompB.Name.YLocation
   CompA.Name.Rotation = CompB.Name.Rotation
   Call PCBServer.SendMessageToRobots(CompA.Name.I_ObjectAddress, c_Broadcast,_
   PCBM_EndModify , c_NoEventData)

   Call PCBServer.SendMessageToRobots(CompB.I_ObjectAddress,c_Broadcast,_
   PCBM_BeginModify, c_NoEventData)
   CompB.X = CompX
   CompB.Y = CompY
   CompB.Rotation = CompR
   CompB.ChangeNameAutoposition = eAutoPos_Manual
   Call PCBServer.SendMessageToRobots(CompB.I_ObjectAddress,c_Broadcast,_
   PCBM_EndModify , c_NoEventData)

   Call PCBServer.SendMessageToRobots(CompB.Name.I_ObjectAddress, c_Broadcast,_
    PCBM_BeginModify, c_NoEventData)
   CompB.Name.XLocation = DesX
   CompB.Name.YLocation = DesY
   CompB.Name.Rotation = DesR
   Call PCBServer.SendMessageToRobots(CompB.Name.I_ObjectAddress, c_Broadcast,_
    PCBM_EndModify , c_NoEventData)

Wend

Pcbserver.PostProcess
ResetParameters
Call AddStringParameter("Action", "Redraw")
RunProcess("PCB:Zoom")

End Sub



www.tdpcb.com