home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 March / Chip_1998-03_cd.bin / tema / MINICAD / MC7DEMO / MINICAD.1 / DRAWSPAC.MPC < prev    next >
Text File  |  1997-04-30  |  7KB  |  219 lines

  1. PROCEDURE DrawSpaces;
  2. {** This procedure reads the first two columns from each row of the selected worksheet and,
  3.  if there is a string in the first column amd a number in the second column, lays out a group 
  4.  with a named square polygon and a text object in a non-overlapping pattern about the origin
  5.  In a layer called 'Space Planning'. The name of the poly and the string of the text object
  6.  is the string in column 1. The length of each side of the poly is the square root of the
  7.  value in column 2. }
  8.  
  9. LABEL 1, 2;
  10. CONST
  11.     kLayerName = 'Space Planning';
  12.     kWorksheetName = 'Area Worksheet';
  13.     kGapFactor = 50;
  14.     kTextSize = 9;
  15.     kFont = 3; {Geneva}
  16.     kPointsPerInch = 72;
  17. VAR
  18.     x, y : ARRAY [1..4, 1..4] of REAL; {storage of coord points}
  19.     f : ARRAY [1..4] of BOOLEAN;    {first time through flags}
  20.     xf, yf : ARRAY [1..4] of REAL;  {x and y quadrant factors}
  21.     wsHan, layerhan : HANDLE;
  22.     spaceArea, spaceLen, spaceSep, myScale, textOffset, upi : REAL;
  23.     startRow, numRows, numCols, numNames, row, i, format : INTEGER;
  24.     worksheetName, spaceName, skipped, uMark, squMark : STRING;
  25.     frac, disAcc : LONGINT;
  26.     
  27. PROCEDURE CenterDialog(dX1,dX2 : INTEGER; VAR x1,x2 : INTEGER);
  28. VAR
  29. scrX1,scrY1,scrX2,scrY2,w : INTEGER;
  30. BEGIN
  31. GetScreen(scrX1,scrY1,scrX2,scrY2);
  32. w := dX2 - dX1;
  33. x1 := ((scrX1 + scrX2) DIV 2) - (w DIV 2);
  34. x2 := x1 + w;
  35. END;
  36.  
  37. FUNCTION PermissionDeniedToDelete : BOOLEAN;
  38. VAR
  39.     result, finished, cancel : BOOLEAN;
  40.     item, x1, x2 : INTEGER;
  41.     layerHan : HANDLE;
  42. BEGIN
  43.     finished := FALSE;
  44.     cancel:= FALSE;
  45.     layerHan := GetObject(kLayerName);
  46.     IF FActLayer <> Nil THEN BEGIN
  47.     {IF ( NumObj ( layerHan ) ) <> 0 THEN BEGIN}
  48.         CenterDialog(0,320,x1,x2);
  49.         BeginDialog(1,1,x1,189,x2,323);
  50.             AddButton('Delete',1,1,211,82,275,105);
  51.             AddButton('Cancel',2,1,128,82,192,105);
  52.             AddField('Existing items in layer',3,1,49,24,208,42);
  53.             AddField( Concat( ' ╘',kLayerName,'╒ will be deleted.' ), 4,1,49,47,277,65);
  54.     EndDialog;
  55.     GetDialog(1);
  56.     REPEAT DialogEvent(item);
  57.         IF item = 2 THEN BEGIN
  58.             finished := TRUE;
  59.             cancel := TRUE;
  60.         END;
  61. IF item = 1 THEN finished := TRUE;
  62. UNTIL finished;
  63. CLRDIALOG;
  64. IF cancel THEN result := TRUE
  65. ELSE BEGIN
  66.     DoMenuText('Active Only');
  67.     SelectObj(L=kLayerName);
  68.     DeleteObjs;
  69.     END;
  70.     END;
  71.     PermissionDeniedToDelete := result;
  72. END;
  73.  
  74. BEGIN
  75.     PushAttrs;
  76.     SetCursor(watchC);
  77.     IF PermissionDeniedToDelete THEN GoTo 1;
  78.     DSelectall;
  79.     
  80.     {** Set up required attributes. }
  81.     AngleVar;
  82.     ClosePoly;
  83.     TextJust(2);
  84.     TextSize(kTextSize);
  85.     TextFont(kFont);
  86.     GetUnits(frac, disAcc, format, upi, uMark, squMark);
  87.     
  88.     {** Calculate vertical offset for centering text placement. }
  89.     textOffset := upi/((kTextSize/kPointsPerInch)/2);
  90.     
  91.     {** Try named worksheet constant. }
  92.     wsHan := GetObject(kWorksheetName);
  93.     
  94.     {** Try active worksheet. }
  95.     IF (wsHan = nil) THEN wsHan := ActSSheet;
  96.     
  97.     {** Try getting worksheet name from user. }
  98.     IF (wsHan = nil) THEN BEGIN
  99.         worksheetName := StrDialog('Enter the name of the worksheet to be used.', 'Worksheet 1');
  100.         wsHan := GetObject(worksheetName);
  101.     END;
  102.     
  103.     {** If no worksheet then punch out with dialog╔ }
  104.     IF ((wsHan = nil) | (GetType(wsHan) <> 18)) THEN BEGIN
  105.         SysBeep;
  106.         AlrtDialog('I can╒t find a worksheet by that name.');
  107.         GOTO 1;
  108.     END;
  109.     SelectSS(wsHan);
  110.     SprdSize(wsHan, numRows, numCols);
  111.     
  112.     {** Determine number of names and average area factor. }
  113.     numNames := 0;
  114.     IF GetCellStr(wsHan, 1, 1) = 'Name' THEN startRow := 2
  115.     ELSE startrow := 1;
  116.     IF GetCellStr(wsHan, 1, 1) = '' THEN startRow := 2;
  117.     
  118.     {** Calculate separation between spaces in layout. }
  119.     spaceSep := 0;
  120.     FOR row := startRow to numRows DO BEGIN
  121.         IF CellHasStr(wsHan, row, 1) THEN BEGIN
  122.             numNames := numNames + 1;
  123.             spaceSep := spaceSep + Str2Num(GetCellStr(wsHan, row, 2));
  124.         END;
  125.     END;
  126.     spaceSep := (spaceSep / numNames) / kGapFactor;
  127.     
  128.     {** Initialize first time through flags. }
  129.     FOR i := 1 to 4 DO f[i] := true;
  130.     
  131.     {** Initialize x and y quadrant factors. }
  132.     xf[1] := 1;
  133.     yf[1] := 1;
  134.     xf[2] := 1;
  135.     yf[2] := -1;
  136.     xf[3] := -1;
  137.     yf[3] := 1;
  138.     xf[4] := -1;
  139.     yf[4] := -1;
  140.     
  141.     {** Initialize error string. }
  142.     skipped:='Areas missing. Skipped row ';
  143.     
  144.     {** Process each row of worksheet. }
  145.     FOR row := startRow to numRows DO BEGIN
  146.         IF CellHasStr(wsHan, row, 1) THEN BEGIN
  147.             spaceName := getcellstr(wsHan, row, 1);
  148.             IF CellHasNum(wsHan, row, 2) THEN
  149.                 spaceArea := Str2Num(GetCellStr(wsHan, row, 2))
  150.             ELSE BEGIN
  151.                 {** Add skipped row to error string and goto next row. }
  152.                 skipped := Concat(skipped, row, ', ');
  153.                 GoTo 2;
  154.             END;
  155.             spaceLen := Sqrt(spaceArea);
  156.             
  157.             {** Create quadrant index for this row; q1 = 1, q2 = 2, etc. }
  158.             i := ((row + 4) MOD 4) +1;
  159.             IF f[i] THEN BEGIN
  160.                 {** First time through; load initial values. }
  161.                 f[i] := false;
  162.                 x[i, 1] := (spaceSep/2) * xf[i];
  163.                 y[i, 1] := (spaceSep/2) * yf[i];
  164.                 x[i, 2] := x[i, 1];
  165.                 y[i, 2] := y[i, 1];
  166.                 x[i, 3] := x[i, 1] + (spaceLen * xf[i]);
  167.                 y[i, 3] := y[i, 1] + (spaceLen * yf[i]);
  168.                 x[i, 4] := x[i, 3];
  169.                 y[i, 4] := y[i, 3];
  170.                 BeginGroup;
  171.                     {** draw first box and text group }
  172.                     NameObject(spaceName);
  173.                     Poly(x[i, 2], y[i, 2], x[i, 2], y[i, 3], x[i, 3], y[i, 3], x[i, 3], y[i, 2]);
  174.                     TextOrigin(x[i, 2] + ((spaceLen/2) * xf[i]), y[i, 2] + ((spaceLen/2) * yf[i]) + textOffset);
  175.                     BeginText;
  176.                         spaceName
  177.                     EndText;
  178.                 EndGroup;
  179.                 END
  180.             ELSE BEGIN
  181.                 {** Compare new values; load to build layout vertically or horizontally. }
  182.                 IF Abs(y[i, 4]) >= Abs(x[i, 4]) THEN BEGIN
  183.                     x[i, 2] := x[i, 4] + (spaceSep * xf[i]);
  184.                     y[i, 2] := y[i, 1];
  185.                     END
  186.                 ELSE BEGIN
  187.                     x[i, 2] := x[i, 1];
  188.                     y[i, 2] := y[i, 4] + (spaceSep * yf[i]);
  189.                 END;
  190.                 x[i, 3] := x[i, 2] + (spaceLen * xf[i]);
  191.                 y[i, 3] := y[i, 2] + (spaceLen * yf[i]);
  192.                 BeginGroup;
  193.                     {** Draw next box and text group. }
  194.                     NameObject(spaceName);
  195.                     Poly(x[i, 2], y[i, 2], x[i, 2], y[i, 3], x[i, 3], y[i, 3], x[i, 3], y[i, 2]);
  196.                     TextOrigin(x[i, 2] + ((spaceLen/2) * xf[i]), y[i, 2] + ((spaceLen/2) * yf[i]) + textOffset);
  197.                     BeginText;
  198.                         spaceName
  199.                     EndText;
  200.                 EndGroup;
  201.                 {** Update largest x and y values so far, if nessicary. }
  202.                 IF Abs(x[i, 3]) >= Abs(x[i, 4]) THEN x[i, 4] := x[i, 3];
  203.                 IF Abs(y[i, 3]) >= Abs(y[i, 4]) THEN y[i, 4] := y[i, 3];
  204.             END;
  205.         END;
  206. 2:    END;
  207.     
  208.     {** Cleanup and report errors. }
  209.     DSelectAll;
  210.     
  211.     {** Report if any lines were skipped. }
  212.     IF skipped <> 'Areas missing. Skipped row ' THEN BEGIN
  213.         Delete(skipped, len(skipped) - 1, 2);
  214.         AlrtDialog(Concat(skipped, '.'));
  215.     END;
  216. 1:    PopAttrs;
  217. END;
  218. RUN(DrawSpaces);
  219.