home *** CD-ROM | disk | FTP | other *** search
/ BUG 11 / BUGCD1998_02.ISO / aplic / turbocad / tcw.z / padwiz.bas < prev    next >
BASIC Source File  |  1997-05-05  |  9KB  |  395 lines

  1. ' This sample program is to illustrate how to create Wizard-like dialogs for TurboCAD 3.0
  2. '
  3. ' In this example I explore the possibilities of parametric drawing using scripts in the
  4. ' PCB disciplne.
  5. '
  6. ' Author    : Mike Cartwright, Tamara Cartwright (updated for 4.0)
  7. ' Date        : 10/21/95, 02/06/97
  8. '
  9.  
  10.  
  11.  
  12. ' DBAPI Constants
  13. Global Const BrushSolid = 1
  14. Global Const NULL     = 0
  15.  
  16. ' Result is a global variable returned by each page to tell the
  17. ' state machine which button was pressed :
  18. Global Const NextID   = 1
  19. Global Const CancelID = 2
  20. Global Const BackID   = 3
  21. Global Const CreateID = 4
  22.  
  23.  
  24. Dim Result As Long
  25.  
  26.  
  27. Dim Orientation As Long
  28. Dim PadGap      As Double
  29. Dim RowGap         As Double
  30. Dim PadSize        As Double
  31. Dim Offset         As Long
  32. Dim TotalPads    As Long
  33. Dim PadShape    As Long
  34.  
  35. Sub Main ()
  36.     Dim dActive As Long
  37.      dActive        = TCWDrawingActive ()
  38.     ' Check for valid drawing
  39.     If dActive = NULL Then
  40.         MsgBox "Program requires active drawing. Open any drawing and try again."
  41.         ' Terminate the program
  42.         Stop
  43.     End If
  44.     
  45.     
  46.     Orientation = 1      ' Default is Horizontal
  47.     Offset = 0          ' Default is not to offset the one row
  48.     TotalPads = 16        ' Total in each row
  49.     PadGap = 0.25        ' Gap between pads in the row
  50.     RowGap = 0.5        ' Gap between rows
  51.     PadSize    = .125        ' Diameter of pads
  52.     PadShape = 0        ' Circle is 0, Square is 1
  53.     
  54.     ' State is actually an index to tell which page we are currently on
  55.     Dim State As Long
  56.     Dim LastState As Long
  57.     LastState = 3
  58.  
  59.     ' Start on the first page
  60.     State = 0
  61.  
  62.     Do
  63.  
  64.         ' Page "0" - Which way up Vertical or Horizontal?
  65.         If State = 0 Then
  66.             OrientationDlg
  67.         End If
  68.  
  69.         ' Page "1" - Total number of Pads per Row and whether offset or not
  70.         If State = 1 Then
  71.             TotalDlg
  72.         End If
  73.  
  74.         ' Page "2" - Inter-row gap and inter-pad gaps
  75.         If State = 2 Then
  76.             GapDlg
  77.         End If
  78.  
  79.         ' Page "LastState" - What shape, circle or square? What Size?
  80.         If State = LastState Then
  81.             ShapeDlg
  82.         End If
  83.         
  84.         If Result = CreateID Then
  85.             CreatePads
  86.             Exit Do
  87.         End If
  88.  
  89.         If Result = CancelID Then
  90.             MsgBox "The Pad Wizard was cancelled."
  91.             Exit Do
  92.         End If
  93.     
  94.         If Result = NextID And State < LastState Then
  95.             State = State + 1
  96.         End If            
  97.                 
  98.         If Result = BackID And State > 0 Then
  99.             State = State - 1
  100.         End If
  101.  
  102.     Loop
  103.     
  104.         
  105. End Sub
  106.  
  107. Sub OrientationDlg ()
  108.  
  109.     Begin Dialog OrientationDialog 31, 32, 185, 96, "Pad Wizard"
  110.         PushButton 104, 79, 35, 14, "&Next>"             ' Button 1
  111.         PushButton 24,  79, 35, 14, "Cancel"             ' Button 2
  112.         '                            "<&Back"    
  113.         PushButton 144, 79, 35, 14, "&Finish"            ' Button 4
  114.         GroupBox 1, 75, 183, 1, ""
  115.  
  116.         GroupBox 100, 12, 72, 48, "Orientation"
  117.         OptionGroup .grp1
  118.             OptionButton 108, 24, 55, 9, "&Vertical"   ' Option 0
  119.             OptionButton 108, 40, 55, 9, "&Horizontal"      ' Option 1
  120.  
  121.         GroupBox 10, 12, 82, 48, ""
  122.         Text      14, 18, 74, 40, "If the pads run across the page then choose Horizontal. If not, then choose vertical."
  123.     End Dialog    
  124.  
  125.     Dim Dlg1 As OrientationDialog
  126.     Dlg1.grp1 = Orientation
  127.     Result = Dialog(Dlg1)
  128.     if Result = 3 then
  129.         Result = 4
  130.     End If    
  131.     Orientation = Dlg1.grp1
  132.  
  133. End Sub
  134.  
  135. Sub ShapeDlg ()
  136.     
  137.     Begin Dialog ShapeDialog 31, 32, 185, 96, "Pad Wizard"
  138.         '                            "&Next>"
  139.         PushButton 24,  79, 35, 14, "Cancel"             ' Button 2
  140.         PushButton 64,  79, 35, 14, "<&Back"             ' Button 3
  141.         PushButton 144, 79, 35, 14, "&Finish"            ' Button 4
  142.         GroupBox 1, 75, 183, 1, ""
  143.  
  144.         GroupBox 100, 12, 72, 48, "Pad Shape"
  145.         OptionGroup .grp1
  146.             OptionButton 108, 24, 55, 9, "&Circle"   ' Option 0
  147.             OptionButton 108, 40, 55, 9, "&Square"      ' Option 1
  148.         
  149.         GroupBox 10, 12, 82, 48, "Pad Size"
  150.         Text      14, 21, 74, 30, "This is the diameter or width of each pad:"
  151.         TextBox  14, 42, 50, 12, .size
  152.     End Dialog    
  153.  
  154.     Dim Dlg2 As ShapeDialog
  155.     Dlg2.grp1 = PadShape
  156.     Do
  157.         Dlg2.size = PadSize
  158.         Result = Dialog(Dlg2)
  159.         Result = Result + 1
  160.         PadSize = Dlg2.size
  161.  
  162.         If Result = CancelID Then
  163.             Exit Do
  164.         End If
  165.  
  166.         If PadSize > 0.1 And PadSize < 1.0 Then
  167.             Exit Do
  168.         End If
  169.  
  170.         MsgBox "Pad Size of " & PadSize &" is not in the range [0.1 .. 1.0]"
  171.     Loop
  172.  
  173.     PadShape = Dlg2.grp1
  174.  
  175.  
  176. End Sub
  177.  
  178.  
  179. Sub TotalDlg ()
  180.     
  181.     Begin Dialog TotalDialog 31, 32, 185, 96, "Pad Wizard"
  182.         PushButton 104, 79, 35, 14, "&Next>"             ' Button 1
  183.         PushButton 24,  79, 35, 14, "Cancel"             ' Button 2
  184.         PushButton 64,  79, 35, 14, "<&Back"             ' Button 3
  185.         PushButton 144, 79, 35, 14, "&Finish"            ' Button 4
  186.         GroupBox 1, 75, 183, 1, ""
  187.  
  188.         GroupBox 100, 12, 72, 48, "Offset Pads"
  189.         OptionGroup .grp1
  190.             OptionButton 108, 24, 55, 9, "DI&P - in line"   ' Option 0
  191.             OptionButton 108, 40, 55, 9, "DI&N - offset"    ' Option 1
  192.         
  193.         GroupBox 10, 12, 82, 48, "Number of Pads"
  194.         Text      14, 21, 74, 30, "The number of pads in the longest row:"
  195.         TextBox  14, 42, 50, 12, .total
  196.     End Dialog    
  197.  
  198.     Dim Dlg3 As TotalDialog
  199.     Dlg3.grp1 = Offset
  200.     Do
  201.         Dlg3.total = TotalPads
  202.         Result = Dialog(Dlg3)
  203.         TotalPads = Dlg3.total
  204.  
  205.         If Result = CancelID Then
  206.             Exit Do
  207.         End If
  208.  
  209.         If TotalPads > 1 And TotalPads < 100 Then
  210.             Exit Do
  211.         End If
  212.  
  213.         If TotalPads > 100 Then
  214.             MsgBox TotalPads &" is too many pads"
  215.         End If
  216.         If TotalPads < 2 Then
  217.             MsgBox TotalPads &" is too few pads"
  218.         End If
  219.     Loop
  220.  
  221.     Offset = Dlg3.grp1
  222.     
  223.     
  224.  
  225. End Sub
  226.  
  227.  
  228. Sub GapDlg ()
  229.     
  230.     Begin Dialog GapDialog 31, 32, 185, 96, "Pad Wizard"
  231.         PushButton 104, 79, 35, 14, "&Next>"             ' Button 1        
  232.         PushButton 24,  79, 35, 14, "Cancel"             ' Button 2
  233.         PushButton 64,  79, 35, 14, "<&Back"             ' Button 3
  234.         PushButton 144, 79, 35, 14, "&Finish"            ' Button 4
  235.         GroupBox 1, 75, 183, 1, ""
  236.  
  237.         GroupBox 100, 12, 76, 48, "Pad Gap"
  238.         Text      104, 21, 69, 30, "This is the space between pad centers:"
  239.         TextBox  104, 42, 50, 12, .padg
  240.  
  241.         GroupBox 10, 12, 82, 48, "Row Gap"
  242.         Text      14, 21, 74, 30, "This is the space between row centers:"
  243.         TextBox  14, 42, 50, 12, .rowg
  244.     End Dialog    
  245.  
  246.     Dim Dlg4 As GapDialog
  247.     
  248.     Do
  249.         Dlg4.rowg = RowGap
  250.         Dlg4.padg = PadGap
  251.         Result = Dialog(Dlg4)
  252.         
  253.         PadGap = Dlg4.padg
  254.         RowGap = Dlg4.rowg
  255.  
  256.         If Result = CancelID Then
  257.             Exit Do
  258.         End If
  259.  
  260.         If PadGap > PadSize And RowGap > PadSize Then
  261.             Exit Do
  262.         End If
  263.         If PadGap <= PadSize Then
  264.             MsgBox "Pad Gap of " & PadGap & " should be greater than " & PadSize
  265.         End If
  266.         If RowGap <= PadSize Then
  267.             MsgBox "Row Gap of " & RowGap & " should be greater than " & PadSize
  268.         End If
  269.     Loop
  270. End Sub
  271.  
  272.  
  273.  
  274. ' Called on "Create" to actually create the graphics and add them
  275. ' to the drawing. They are left selected to make it easier to move them.
  276. Sub CreatePads ()
  277.     Dim dActive  As Long
  278.     
  279.     Dim xc As Double
  280.     Dim yc As Double
  281.     Dim dx1 As Double
  282.     Dim dx2 As Double
  283.     Dim dx3 As Double
  284.     Dim dy1 As Double
  285.     Dim dy2 As Double
  286.     Dim dy3 As Double
  287.     
  288.     Dim row As Long
  289.     Dim col As Long
  290.  
  291.     dActive        = TCWDrawingActive ()
  292.     TCWUndoRecordStart dActive, "Create Pads"
  293.     
  294.     xc = (TCWViewExtentsGetX1() + TCWViewExtentsGetX2())/2.0
  295.     yc = (TCWViewExtentsGetY1() + TCWViewExtentsGetY2())/2.0
  296.     
  297.  
  298.     If Orientation = 1 Then     ' Horizontal
  299.         dx1 = PadGap
  300.         dy1 = 0
  301.         dx2 = 0
  302.         dy2 = -RowGap
  303.     Else                        ' Vertical
  304.         dx1 = 0
  305.         dy1 = -PadGap
  306.         dx2 = RowGap
  307.         dy2 = 0
  308.     End If
  309.  
  310.     ' Move starting point
  311.  
  312.     xc = xc - dx1*TotalPads/2.0 - dx2/2.0
  313.     yc = yc - dy1*TotalPads/2.0    - dy2/2.0
  314.     
  315.  
  316.     For row = 0 to 1
  317.         For col = 0 to TotalPads-1
  318.  
  319.             ' Call our function which creates a pad 
  320.             MakeObject xc + col*dx1 + row*dx2, yc + col*dy1 + row*dy2
  321.  
  322.         Next col
  323.  
  324.         ' For the offset option an extra calculation is required when
  325.         ' switching rows to get the next row's start position
  326.         If Offset Then
  327.             TotalPads = TotalPads - 1
  328.             xc = xc + dx1/2.0
  329.             yc = yc + dy1/2.0
  330.         End If
  331.         
  332.     Next row
  333.  
  334.     TCWSelectAll
  335.     TCWGroupCreate "Custom Pads"
  336.     TCWUndoRecordEnd dActive
  337.         
  338. End Sub
  339.  
  340. Sub MakeObject (ByVal x As Double, ByVal y As Double)
  341.     
  342.     Dim gP As Long
  343.     Dim g1 As Long
  344.     Dim g2 As Long
  345.     Dim r As Double
  346.  
  347.     r = PadSize/2.0
  348.  
  349.     If PadShape = 0 Then
  350.  
  351.         g1 = TCWCircleCenterAndPoint(x, y, 0#, x-2*r, y, 0# )
  352.         
  353.     Else
  354.         
  355.         ' Create the empty rectangle graphic
  356.         g1 = TCWLineRectangle(x-r, y-r, 0#, x+r, y+r, 0#)
  357.  
  358.     End If                                
  359.  
  360.     ' Color is set as R, G, B - this is black
  361.     result = TCWGraphicPropertySet(g1, "PenColor", &H000000 )
  362.  
  363.     ' Fill Pattern is a style. We know that style 1 is always solid
  364.     result = TCWGraphicPropertySet(g1, "BrushStyle", BrushSolid)
  365.  
  366.     ' Add the black background of the pad to the pad group
  367.  
  368.     ' Make the hole 1/3 of the size of the pad unless it is smaller
  369.     ' then a minimum, in which case it is set to that minimum
  370.     r = PadSize/6.0
  371.     If r > 0.0625 Then
  372.         r =    0.0625
  373.     End If
  374.     
  375.  
  376.     If PadShape = 0 Then
  377.         
  378.         ' Create the empty arc graphic
  379.         g2 = TCWCircleCenterAndPoint(x, y, 0#, x-2*r, y, 0# )
  380.  
  381.     Else
  382.         
  383.         ' Create the empty rectangle graphic
  384.         g2 = TCWLineRectangle(x-r, y-r, 0#, x+r, y+r, 0#)
  385.     End If                                
  386.  
  387.      ' Color is set as R, G, B - this is white
  388.     result = TCWGraphicPropertySet(g2, "PenColor", &H00FFFFFF)
  389.  
  390.     ' Fill Pattern is a style. We know that style 1 is always solid
  391.     result = TCWGraphicPropertySet(g2, "BrushStyle", BrushSolid)
  392.     
  393. End Sub
  394.  
  395.