home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 13 / 13.iso / p / p024 / 12.img / ADS3.LIB / GRVECS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-12  |  12.3 KB  |  391 lines

  1. /* Next available MSG number is  33 */
  2.  
  3. /***************************************************************************
  4.    Module Name:  grvecs.c
  5.  
  6.    Description:ADS version of the AutLISP program sprial.lsp
  7.  
  8.    Author     :
  9.                  Autodesk, Inc.
  10.                  2320 Marinship Way
  11.                  Sausalito, CA. 94965
  12.                  (415)332-2344
  13.  
  14.       ¬⌐┼v (C) 1992  Autodesk ñ╜Ñq
  15.  
  16.       Ñ╗│n┼ΘºK╢O¿╤▒z╢iªµÑ⌠ª≤Ñ╬│~╗▌¿D¬║½■¿⌐íB¡╫º∩ñ╬╡oªµ, ª²¼O░╚╜╨┐φ┤`ñU¡z
  17.       ¡∞½h :
  18.  
  19.       1)  ñWªC¬║¬⌐┼v│qºi░╚╗▌ÑX▓{ªb¿Cñ@Ñ≈½■¿⌐∙╪íC
  20.       2)  ¼█├÷¬║╗í⌐·ñσÑ≤ñ]Ñ▓╢╖⌐·╕ⁿ¬⌐┼v│qºiñ╬Ñ╗╢╡│\Ñi│qºiíC
  21.  
  22.       Ñ╗│n┼Θ╢╚┤ú¿╤º@¼░└│Ñ╬ñW¬║░╤ª╥, ª╙Ñ╝┴n⌐·⌐╬┴⌠ºtÑ⌠ª≤½O├╥; ╣∩⌐≤Ñ⌠ª≤»S«φ
  23.       Ñ╬│~ñº╛A║┘⌐╩, ÑHñ╬░╙╖~╛P░Γ⌐╥┴⌠ºtÑX¿π¬║½O├╥, ªbª╣ñ@╖ºñ⌐ÑHº_╗{íC
  24.  
  25.  
  26.   **************************************************************************
  27.  
  28.     Function Entry Points:
  29.         void
  30.         main(argc, argv)
  31.         int    argc;              [Number of argument passed in cmd line]
  32.         char   *argv[];           [Array of pointers to arguments]
  33.  
  34.     Exported ADS Functions
  35.         TESTGRVECS                [Test the ADS_GRVECS function]
  36.  
  37.     Modification History:
  38.         Feb 28 1992 - bcm - original creation
  39.  
  40.     Notes and restrictions on use:
  41.  
  42.  
  43. ***************************************************************************/
  44.  
  45. /**************************************************************************/
  46. /*  MODULE NAME  */
  47. /**************************************************************************/
  48. #define    GRVECS
  49.  
  50. /****************************************************************************/
  51. /*  DEFINES  */
  52. /****************************************************************************/
  53. #define ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))
  54. #define SPOINT(p, x, y, z) p[X] = (x); p[Y] = (y); p[Z] = (z)
  55. #define CPOINT(dst, src) dst[X] = src[X]; dst[Y] = src[Y]; dst[Z] = src[Z]
  56. /**************************************************************************/
  57. /*  TYPEDEFS  */
  58. /**************************************************************************/
  59. /* ADS Function Table */
  60. typedef struct {
  61.     char    *name;
  62.     int     (*fptr)();
  63. } ftblent;
  64.  
  65. typedef struct resbuf rbtype;
  66. /**************************************************************************/
  67. /*  INCLUDES  */
  68. /**************************************************************************/
  69.  
  70. #include <stdio.h>
  71. #include <math.h>
  72. #include <memory.h>
  73. #include "adslib.h"
  74. /****************************************************************************/
  75. /*  LOCAL FUNCTION FORWARD DECLARATIONS  */
  76. /****************************************************************************/
  77. int     grspiral();
  78. int     testgrvecs();
  79.  
  80. /**************************************************************************/
  81. /*  GLOBAL VARIABLES  */
  82. /**************************************************************************/
  83. /* Table of ADS functions */
  84. ftblent exfun[] = {
  85.             {/*MSG0*/"C:TESTGRVECS", testgrvecs},
  86.         };
  87.  
  88. /**************************************************************************/
  89. /*  EXTERNAL FUNCTION DECLARATIONS  */
  90. /**************************************************************************/
  91.  
  92. /**************************************************************************/
  93. /*  EXTERNAL VARIABLE DECLARATIONS  */
  94. /**************************************************************************/
  95.  
  96. /****************************************************************************/
  97. /*  LOCAL FUNCTION DECLARATIONS  */
  98. /****************************************************************************/
  99.  
  100. /******************************************************************************/
  101. /*.doc geterrno(internal) */
  102. /*+
  103.     This function is called to obtain the value of the AutoCAD system
  104.     variable ERRNO and return it as a result.
  105. -*/
  106. /******************************************************************************/
  107. int
  108. /*FCN*/geterrno()
  109. {
  110.     rbtype errval;
  111.  
  112.     ads_getvar(/*MSG0*/"ERRNO", &errval);
  113.  
  114.     return errval.resval.rint;
  115. }
  116.  
  117. /******************************************************************************/
  118. /*.doc funcload(internal) */
  119. /*+
  120.     This function is called to define all function names in the ADS
  121.     function table.  Each named function will be callable from lisp or
  122.     invokable from another ADS application.
  123. -*/
  124. /******************************************************************************/
  125. int
  126. /*FCN*/funcload()
  127. {
  128.     int i;
  129.  
  130.     for (i = 0; i < ELEMENTS(exfun); i++) {
  131.         if (!ads_defun(exfun[i].name, i))
  132.             return RTERROR;
  133.     }
  134.  
  135.     return RTNORM;
  136. }
  137.  
  138. /******************************************************************************/
  139. /*.doc funclunoad(internal) */
  140. /*+
  141.     This function is called to undefine all function names in the ADS
  142.     function table.  Each named function will be removed from the
  143.     AutoLISP hash table.
  144. -*/
  145. /******************************************************************************/
  146. int
  147. /*FCN*/funcunload()
  148. {
  149.     int i;
  150.  
  151.     /* Undefine each function we defined */
  152.  
  153.     for (i = 0; i < ELEMENTS(exfun); i++) {
  154.         ads_undef(exfun[i].name,i);
  155.     }
  156.  
  157.     return RTNORM;
  158. }
  159. /******************************************************************************/
  160. /*.doc dofun(internal) */
  161. /*+
  162.     This function is called to invoke the function which has the
  163.     registerd function code that is obtained from  ads_getfuncode.  The
  164.     function will return RTERROR if the function code is invalid, or
  165.     RSERR if the invoked function fails to return RTNORM.  The value
  166.     RSRSLT will be returned if the function code is valid and the
  167.     invoked subroutine returns RTNORM.
  168. -*/
  169. /******************************************************************************/
  170. int
  171. /*FCN*/dofun()
  172. {
  173.     int    val;
  174.     int    rc;
  175.  
  176.     ads_retvoid();
  177.  
  178.     if ((val = ads_getfuncode()) < 0 || val > ELEMENTS(exfun))
  179.         return RTERROR;
  180.  
  181.     rc = (*exfun[val].fptr)();
  182.  
  183.     return ((rc == RTNORM) ? RSRSLT:RSERR);
  184. }
  185.  
  186. /******************************************************************************/
  187. /*.doc main(internal) */
  188. /*+
  189.     This is the main entry point for the ADS application.  All ADS
  190.     requests will be dispatched from this function.  This is your
  191.     standard ADS dispatch loop.
  192. -*/
  193. /******************************************************************************/
  194. void
  195. /*FCN*/main(argc,argv)
  196. int argc;
  197. char *argv[];
  198. {
  199.     short scode = RSRSLT;          /* Normal result code (default) */
  200.     int   stat;
  201.  
  202.     ads_init(argc, argv);          /* Initiate communication with AutoLISP */
  203.  
  204.     for ( ;; ) {                   /* Request/Result loop */
  205.  
  206.         if ((stat = ads_link(scode)) < 0) {
  207.             printf(/*MSG1*/"GRVECS: Ñ╤ ads_link() ╢╟ª^¬║ñú¿╬¬¼║A = %d\n", stat);
  208.             fflush(stdout);
  209.             exit(1);
  210.         }
  211.  
  212.         scode = RSRSLT;           /* Reset result code */
  213.  
  214.         switch (stat) {
  215.  
  216.         case RQXLOAD:             /* Load & define functions */
  217.             scode = funcload() ? -RSRSLT : -RSERR;
  218.             break;
  219.  
  220.         case RQXUNLD:             /* Unload functions */
  221.             scode = funcunload() ? -RSRSLT : -RSERR;
  222.             ads_printf(/*MSG2*/"─└⌐±íC\n");
  223.             break;
  224.  
  225.         case RQSUBR:             /* Handle request for external function */
  226.             dofun();
  227.             break;
  228.  
  229.         default:
  230.             break;
  231.         }
  232.     }
  233. }
  234.  
  235. /******************************************************************************/
  236. /*.doc grspiral(internal) */
  237. /*+
  238.     This function is a C translation of the grspiral.lsp program
  239.     designed to exersise the AutoLISP function grvecs.  The function
  240.     takes the arguments: number of spiral rotations, the center point
  241.     for the spiral, the rate of growth per rotation, and the number of
  242.     points per rotation.
  243.  
  244.     The vectors for the spiral will be generated in the form of a
  245.     linked list of three dimentional point nodes, and color short
  246.     nodes.  The resulting list will be passed to the ads_grvecs
  247.     function which will perform the actual graphical output.  Each
  248.     vector in the list will include a color value which is obtained by
  249.     cycling thru the range of colors specified by the values of minclr,
  250.     and maxclr.
  251.  
  252.     This function always returns RTNORM.
  253. -*/
  254. /******************************************************************************/
  255. int
  256. /*FCN*/grspiral(ntimes, bpoint, cfac, lppass)
  257. int       ntimes;
  258. ads_point bpoint;
  259. ads_real  cfac;
  260. int       lppass;
  261. {
  262.     ads_real     circle;
  263.     ads_real     ainc;
  264.     ads_real     dinc;
  265.     ads_real     ang;
  266.     ads_real     dist;
  267.     ads_real     zdir;
  268.     ads_point    lpoint;
  269.     ads_point    tp;
  270.     rbtype       *plist;
  271.     rbtype       *tplist;
  272.     int          minclr;
  273.     int          maxclr;
  274.     int          color;
  275.     int          n;
  276.     int          l;
  277.     int          sindex;
  278.     ads_matrix   mat;
  279.  
  280.     extern ads_matrix ads_identmat;
  281.     memcpy(mat, ads_identmat, sizeof(ads_matrix));
  282.     CPOINT(lpoint, bpoint);
  283.     circle = 3.141496235 * 2;
  284.     ainc = circle / lppass;
  285.     dinc = cfac / lppass;
  286.     ang = 0.0;
  287.     dist = 0.0;
  288.     minclr = 1;
  289.     maxclr = 200;
  290.     color = minclr;
  291.     zdir = 0.0;
  292.  
  293.     ads_retnil();
  294.     tplist = plist = ads_buildlist(RTSHORT, color, NULL);
  295.  
  296.     for (n = ntimes; n > 0 && tplist != NULL; n--) {
  297.         for (l = lppass; l > 0 && tplist != NULL; l--) {
  298.             ang = ang + ainc;
  299.             dist = dist + dinc;
  300.             ads_polar(bpoint, ang, dist, tp);
  301.  
  302.             /* adjust z coordinate to this point */
  303.             tp[Z] = zdir;
  304.  
  305.             /* append last point and new point to end of grvecs list */
  306.             tplist = tplist->rbnext = ads_buildlist(RT3DPOINT, lpoint, NULL);
  307.             if (tplist == NULL)
  308.                 continue;
  309.             tplist = tplist->rbnext = ads_buildlist(RT3DPOINT, tp, NULL);
  310.             if (tplist == NULL)
  311.                 continue;
  312.  
  313.             /* increment color */
  314.             color++;
  315.  
  316.             /* if color is == max color then reset it to one and
  317.                loop thru them again */
  318.             if (color == maxclr)
  319.                 color = minclr;
  320.  
  321.             /* append new color number to end of grvecs list */
  322.             tplist = tplist->rbnext = ads_buildlist(RTSHORT, color, NULL);
  323.             if (tplist == NULL)
  324.                 continue;
  325.  
  326.             /* save new point as last point */
  327.             CPOINT(lpoint, tp);
  328.  
  329.             /* adjust the z coord for the next point */
  330.             zdir = zdir + cfac;
  331.         }
  332.     }
  333.     if (plist != NULL && tplist != NULL)
  334.         ads_grvecs(plist, NULL);
  335.         for (sindex = 0; sindex < 100; sindex ++) {
  336.             mat[X][X] = mat[Y][Y] = mat[Z][Z] += .05;
  337.             ads_grvecs(plist, mat);
  338.         }
  339.     if (plist != NULL)
  340.         ads_relrb(plist);
  341.     return RTNORM;
  342. }
  343.  
  344.  
  345. /******************************************************************************/
  346. /*.doc testgrvecs(internal) */
  347. /*+
  348.     This function is called from dofun function as a result of RQSUBR
  349.     request being sent to the main dispatch loop.
  350.  
  351.     It prompts the user for the Center point of a spiral, the number of
  352.     rotations in the spiral, the amount of growth per rotation, and the
  353.     number of points in each rotation.   The function can be canceled
  354.     by the user by pressing the Control-C keys.
  355.  
  356.     This function does not perfrom an error checking.  The return value
  357.     for this function is the last value in rc.
  358. -*/
  359. /******************************************************************************/
  360. int
  361. /*FCN*/testgrvecs()
  362. {
  363.     rbtype      *args;
  364.     int         rc;
  365.     ads_point   bp;
  366.     int         nt;
  367.     ads_real    cf;
  368.     int         lp;
  369.  
  370.     args = ads_getargs();
  371.     ads_printf(/*MSG3*/"\n░⌡ªµíuads_grvecsív┤·╕╒\n");
  372.     if (args != NULL) {
  373.         ads_printf(/*MSG4*/"ñú╗▌íuñ▐╝╞ívíC\n");
  374.     }
  375.     rc = ads_getpoint(NULL, /*MSG5*/"\nñññ▀┬I: ", bp);
  376.     if (rc == RTCAN) return RTNORM;
  377.     rc = ads_getint(/*MSG6*/"\n▒█┬╢íu┬α╝╞ív: ", &nt);
  378.     if (rc == RTCAN) return RTNORM;
  379.     rc = ads_getreal(/*MSG7*/"\n¿Cª╕▒█┬╢¬║íu╝W╢qív: ", &cf);
  380.     if (rc == RTCAN) return RTNORM;
  381.     lp = 30;
  382.     ads_initget(0, NULL);
  383.     rc = ads_getint(/*MSG8*/"\n¿Cª╕▒█┬╢¿╧Ñ╬¬║íu┬Iív╝╞ <30>: ", &lp);
  384.     if (rc == RTCAN) return RTNORM;
  385.  
  386.     rc = grspiral(nt, bp, cf, lp);
  387.  
  388.     return rc;
  389. }
  390.  
  391.