Thursday, May 26, 2011

Toggle Component Designators

Toggle all component designators on PCB from shown to hidden.

Sub ToggleDesignators

Dim Board
Dim Component

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

Iterator = Board.BoardIterator_Create
Iterator.AddFilter_ObjectSet(MkSet(eComponentObject))
Iterator.AddFilter_LayerSet(AllLayers)
Iterator.AddFilter_Method(eProcessAll)
Set Component= Iterator.FirstPCBObject
PCBServer.PreProcess

While Not(Component is Nothing)
  Component.NameOn = Not(Component.NameOn)
  Set Component= Iterator.NextPCBObject
Wend

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

End Sub

 


www.tdpcb.com

Tuesday, May 24, 2011

Open a text file and read the contents line by line.

As written this needs to be run with a PCB file active.

Sub ReadTextFile

'Constants for File Handling
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const TristateUseDefault = -2
Const TristateTrue = -1
Const TristateFalse = 0
Dim Board
Dim FileName
Dim oFS
Dim oFile
Dim oStream

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

'File Handling - Place a text file named "TextFile.txt" in
'the same directory as the current board that is open.
FileName=Left(Board.FileName, InstrRev(Board.FileName, "\") ) & "TextFile.txt"

'Check for the text file
If Not FileExists(FileName) Then
   ShowMessage ( FileName & " was not found.")
   Exit Sub
End If
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oFile = oFS.GetFile(FileName)
Set oStream = oFile.OpenAsTextStream(ForReading, TristateUseDefault)

I = 1
'Read the file in
Do While Not oStream.AtEndOfStream
      sRecord = oStream.ReadLine
      ShowMessage ( "Line #" & I & " " & sRecord )
      I = I + 1
Loop
oStream.Close

End Sub


www.tdpcb.com

Friday, May 20, 2011

Place a track and via with the choosen pad net assigned to them.

Select a pad with a net and a stinger (a track and a via) will be placed with the pads net.

Sub TagPadWithNet

Dim Board
Dim Track
Dim Via
Dim NetObject
Dim PadX
Dim PadY
Dim PadNet
Dim ViasSize
Dim ViaHole

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

NetObject = Board.GetObjectAtCursor(MkSet(ePadObject),AllLayers,"Select Net")
PadX = CoordToMils(NetObject.X)
PadY = CoordToMils(NetObject.Y)
PadNet = NetObject.Net.Name
X1 = PadX
X2 = PadX - CoordToMils(NetObject.TopXSize) - 10
Y1 = PadY
Y2 = PadY
Layer = NetObject.Layer
Width = 10
Call PCBServer.PreProcess

'Add the Track
Track           = PCBServer.PCBObjectFactory(eTrackObject, eNoDimension, eCreate_Default)
Track.X1        = MilsToCoord(X1)
Track.X2        = MilsToCoord(X2)
Track.Y1        = MilsToCoord(Y1)
Track.Y2        = MilsToCoord(Y2)
Track.Layer     = Layer
Track.Net       = NetObject.Net
Track.Width     = MilsToCoord(Width)
Board.AddPCBObject(Track)
Call PCBServer.SendMessageToRobots(Board.I_ObjectAddress,_
 c_Broadcast, PCBM_BoardRegisteration, Track.I_ObjectAddress)

'Add the via
ViaSize = 26
ViaHole = 12
Via           = PCBServer.PCBObjectFactory(eViaObject, eNoDimension, eCreate_Default)
Via.X         = MilsToCoord(X2)
Via.Y         = MilsToCoord(Y2)
Via.Size      = MilsToCoord(ViaSize)
Via.HoleSize  = MilsToCoord(ViaHole)
Via.LowLayer  = eTopLayer
Via.HighLayer = eBottomLayer
Via.Net       = NetObject.Net
Board.AddPCBObject(Via)
Call PCBServer.SendMessageToRobots(Board.I_ObjectAddress,_
 c_Broadcast, PCBM_BoardRegisteration, Via.I_ObjectAddress)

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

End Sub



www.tdpcb.com

Thursday, May 19, 2011

Get the board dimensions as defined by the board outline.

Sub BoardDimension
Dim Board
Set Board = PCBServer.GetCurrentPCBBoard
If Board is Nothing Then Exit Sub

ShowMessage ( "X=" & CoordToMils(Board.BoardOutline.BoundingRectangle.right _
 - Board.BoardOutline.BoundingRectangle.left) & " mils,Y=" & _
 CoordToMils(Board.BoardOutline.BoundingRectangle.top _
  - Board.BoardOutline.BoundingRectangle.bottom) & " mils")

End Sub



www.tdpcb.com

Wednesday, May 18, 2011

Identify a pads net.

Simple way to get a pads net.

Sub GetPadNet

Dim Board
Dim NetObject
Dim tmpString
Set Board = PCBServer.GetCurrentPCBBoard
If Board is Nothing Then Exit Sub

NetObject = Board.GetObjectAtCursor(MkSet(ePadObject),AllLayers,"Select Net")
tmpString = NetObject.Net.Name
ShowMessage (tmpString)

End Sub



www.tdpcb.com

Monday, May 16, 2011

Report a list of net names in PCB to a file.

Get a list of net names in the PCB and write a report (text file).

Sub ReportNets

Dim Board
Dim FileName
Dim ReportFile
Dim ReportDocument
Dim fso

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

FileName =  Left(Board.FileName, InstrRev(Board.FileName, "\") ) & "ReportNets.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ReportFile = fso.CreateTextFile(FileName, True)

NetIterator = Board.BoardIterator_Create
NetIterator.AddFilter_ObjectSet(MkSet(eNetObject))
NetIterator.AddFilter_LayerSet(AllLayers)
NetIterator.AddFilter_Method(eProcessAll)
Set NetFound = NetIterator.FirstPCBObject

While Not (NetFound Is Nothing)
     Call ReportFile.WriteLine( NetFound.Name )
     Set NetFound = NetIterator.NextPCBObject
Wend

Board.BoardIterator_Destroy(NetIterator)
Call AddStringParameter("Action", "Redraw")
RunProcess("PCB:Zoom")
ReportFile.Close
Set ReportDocument = Client.OpenDocument("Text", FileName)
If Not (ReportDocument Is Nothing) Then
       Client.ShowDocument(ReportDocument)
End If
EndHourGlass

End Sub



www.tdpcb.com

Saturday, May 14, 2011

Basic's to write a report text file.

Run this with a PCB file active.
Sub WriteFile

Dim FileName
Dim ReportDocument
Dim fso
Set Board = PCBServer.GetCurrentPCBBoard
If Board is Nothing Then Exit Sub
'Run With a PCB file open

FileName =  Left(Board.FileName, InstrRev(Board.FileName, "\") ) & "temp.Txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ReportFile = fso.CreateTextFile(FileName, True)

Call ReportFile.WriteLine (Now)
Call ReportFile.WriteLine ("Report File Example")

ReportFile.Close
Set ReportDocument = Client.OpenDocument("Text", FileName)
If Not (ReportDocument Is Nothing) Then
       Client.ShowDocument(ReportDocument)
End If

End Sub

www.tdpcb.com

Friday, May 13, 2011

Get pads that make up a component using a group iterator.

Select a PCB component with the GetObject and then using a Group Iterator list all pads and the location of the pads that make up the choosen component.

Sub GetCompPads

Dim Board
Dim Comp
Dim CompGroup
Dim CompPads
Dim x,y
Set Board = PCBServer.GetCurrentPCBBoard
If Board is Nothing Then Exit Sub
While Board.ChooseLocation(x,y, "Choose Pad") = True
      Set Comp = Board.GetObjectAtXYAskUserIfAmbiguous(x,y,MkSet(_
      eComponentObject),AllLayers,eEditAction_Focus)

      If Not(Comp is Nothing)  Then
         Set CompGroup = Comp.GroupIterator_Create
         CompGroup.AddFilter_ObjectSet(MkSet(EpadObject))
         Set CompPad = CompGroup.FirstPCBObject

         While Not(CompPad is Nothing )
           ShowMessage("Pad=" & CompPad.Name & " X=" & CoordToMils(CompPad.X)_
           & " Y=" & CoordToMils(CompPad.Y))
           Set CompPad = CompGroup.NextPCBObject
          Wend

      End If
Wend

End Sub




www.tdpcb.com

Monday, May 09, 2011

Rotate and Center Designators of Selected Parts.

Build upon a previous script, rotate and center designators for only selected parts.

Sub RotateSelectedDesignators

Dim Board
Dim Component
Dim CompDes
Dim I
Set Board = PCBServer.GetCurrentPCBBoard
If Board is Nothing Then Exit Sub
BeginHourGlass
Iterator = Board.BoardIterator_Create
Iterator.AddFilter_ObjectSet(MkSet(eComponentObject))
Iterator.AddFilter_LayerSet(AllLayers)
Iterator.AddFilter_Method(eProcessAll)
Set CompDes = Iterator.FirstPCBObject
PCBServer.PreProcess
I = 0

While Not (CompDes Is Nothing)

  If CompDes.Selected = True Then
    Call PCBServer.SendMessageToRobots(CompDes.Name.I_ObjectAddress,_
    c_Broadcast, PCBM_BeginModify, c_NoEventData)
    I = I + 1
    If CompDes.Layer = eTopLayer then           'Component is on the top
       Select Case CompDes.Rotation
           Case 0, 180, 360
                CompDes.Name.Rotation  = 0
           Case 90, 270
                CompDes.Name.Rotation  = 90
       End Select
       else                                     'Component is on the bottom
       Select Case CompDes.Rotation
           Case 0, 180, 360
                CompDes.Name.Rotation  = 0
           Case 90, 270
                CompDes.Name.Rotation  = 270
       End Select
    End If
       Call PCBServer.SendMessageToRobots(CompDes.Name.I_ObjectAddress,_
       c_Broadcast, PCBM_EndModify , c_NoEventData)
       Call PCBServer.SendMessageToRobots(CompDes.Name.I_ObjectAddress,_
       c_Broadcast, PCBM_BeginModify, c_NoEventData)
       CompDes.ChangeNameAutoposition = eAutoPos_CenterCenter
       Call PCBServer.SendMessageToRobots(CompDes.Name.I_ObjectAddress,_
       c_Broadcast, PCBM_EndModify , c_NoEventData)
  End if
       Set CompDes = Iterator.NextPCBObject
Wend

'Uncomment this line if you want a message after it has finished.
'ShowMessage(I & " were found")
If I = 0 then
   ShowMessage("No parts were selected.")
End If
Board.BoardIterator_Destroy(Iterator)
Pcbserver.PostProcess
EndHourGlass
Call AddStringParameter("Action", "Redraw")
RunProcess("PCB:Zoom")
End Sub



Thursday, May 05, 2011

GetObject at cursor to pick a component.

Using the GetObject to choose what component to reset the designator to center/center.
Press escape to end action.

Sub ChooseDesReset

Dim Board
Dim Comp
Dim x,y

Set Board = PCBServer.GetCurrentPCBBoard
Call PCBServer.PreProcess

While Board.ChooseLocation(x,y, "Choose Component") = True
  Set Comp = Board.GetObjectAtXYAskUserIfAmbiguous(x,y,MkSet(_
  eComponentObject),AllLayers,eEditAction_Focus)

  If Not(Comp is Nothing) Then
     Call PCBServer.SendMessageToRobots(Comp.Name.I_ObjectAddress,_
     c_Broadcast, PCBM_BeginModify, c_NoEventData)
     Comp.ChangeNameAutoposition = eAutoPos_CenterCenter
     Call PCBServer.SendMessageToRobots(Comp.Name.I_ObjectAddress,_
     c_Broadcast, PCBM_EndModify , c_NoEventData)
  End If
Wend

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

End Sub



http://www.tdpcb.com/

Tuesday, May 03, 2011

Selected Components

Count how many components are currently selected in the PCB.

Sub HowManyCompsSelected

Dim Board
Dim Component
Dim I

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

Iterator = Board.BoardIterator_Create
Iterator.AddFilter_ObjectSet(MkSet(eComponentObject))
Iterator.AddFilter_LayerSet(AllLayers)
Iterator.AddFilter_Method(eProcessAll)

Set Component= Iterator.FirstPCBObject
PCBServer.PreProcess
I = 0

While Not(Component is Nothing)

  If Component.Selected = True Then
     I = I + 1
  End If
  Set Component= Iterator.NextPCBObject

Wend
ShowMessage(I & " selected.")
Board.BoardIterator_Destroy(Iterator)
Pcbserver.PostProcess
ResetParameters
Call AddStringParameter("Action", "Redraw")
RunProcess("PCB:Zoom")

End Sub

 
 
http://www.tdpcb.com/