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

  1. /* Next available MSG number is   9 */
  2.  
  3. /*    ARBMAT.C
  4.       ¬⌐┼v (C) 1990-1992  Autodesk ñ╜Ñq
  5.  
  6.       Ñ╗│n┼ΘºK╢O¿╤▒z╢iªµÑ⌠ª≤Ñ╬│~╗▌¿D¬║½■¿⌐íB¡╫º∩ñ╬╡oªµ, ª²¼O░╚╜╨┐φ┤`ñU¡z
  7.       ¡∞½h :
  8.  
  9.       1)  ñWªC¬║¬⌐┼v│qºi░╚╗▌ÑX▓{ªb¿Cñ@Ñ≈½■¿⌐∙╪íC
  10.       2)  ¼█├÷¬║╗í⌐·ñσÑ≤ñ]Ñ▓╢╖⌐·╕ⁿ¬⌐┼v│qºiñ╬Ñ╗╢╡│\Ñi│qºiíC
  11.  
  12.       Ñ╗│n┼Θ╢╚┤ú¿╤º@¼░└│Ñ╬ñW¬║░╤ª╥, ª╙Ñ╝┴n⌐·⌐╬┴⌠ºtÑ⌠ª≤½O├╥; ╣∩⌐≤Ñ⌠ª≤»S«φ
  13.       Ñ╬│~ñº╛A║┘⌐╩, ÑHñ╬░╙╖~╛P░Γ⌐╥┴⌠ºtÑX¿π¬║½O├╥, ªbª╣ñ@╖ºñ⌐ÑHº_╗{íC
  14.  
  15.  
  16.  
  17.       DESCRIPTION:
  18.  
  19.       Prints out the axis of the entity coordinate system (ECS)
  20.       specified by the entity's X,Y and Z DXF fields.
  21.  
  22.       Usage: (arbmat X Y Z)
  23.  
  24.       where X,Y and Z are real numbers.
  25.  
  26.       For more information on ECS and the arbitrary axis
  27.       algorithm see the AutoCAD Reference manual.
  28. */
  29.  
  30. #include <stdio.h>                    /* printf, sprintf, sscanf etc... */
  31. #include <math.h>                     /* for sqrt() */
  32. #include "adslib.h"
  33.  
  34. #define ExtFuncCount    1             /* Must increase this count if new
  35.                                          external functions are added */
  36.  
  37. char *exfun[] = {/*MSG0*/"arbmat"};   /* No "C:" -- to be called as an AutoLISP
  38.                                          function, not as an AutoCAD command */
  39.  
  40. #define GOOD       0
  41. #define BAD        (-1)
  42.  
  43. #define ARBBOUND   0.015625           /*  1/64th  */
  44. #define EPS        (1E-10)
  45.  
  46. typedef double real;
  47. typedef real vec[3];
  48. typedef vec matrix[3];
  49.  
  50. void    main            _((int, char **));
  51. int     funcload        _((void));
  52. int     dofun           _((void));
  53. int     arbmat          _((matrix, vec));
  54. int     univec          _((vec, vec));
  55. real    rabs            _((real));
  56. void    cross           _((vec, vec, vec));
  57. void    prtaxis         _((char *, vec));
  58.  
  59.  
  60. void
  61. main(argc, argv)
  62.   int argc;
  63.   char *argv[];
  64.  
  65. {
  66.     int stat;
  67.     short scode = RSRSLT;             /* This is the default result code */
  68.  
  69.     ads_init(argc, argv);             /* Initialize the interface */
  70.  
  71.     for ( ;; ) {                      /* Note loop conditions */
  72.  
  73.         if ((stat = ads_link(scode)) < 0) {
  74.             printf(/*MSG1*/"ARBMAT: Ñ╤ ads_link() ╢╟ª^¬║ñú¿╬¬¼║A = %d\n", stat);
  75.  
  76.             /* Can't use ads_printf to display
  77.                this message, because the link failed */
  78.             fflush(stdout);
  79.             exit(1);
  80.         }
  81.  
  82.         scode = RSRSLT;               /* Default return value */
  83.  
  84.  
  85.         /* Check for AT LEAST the following cases here */
  86.  
  87.         switch (stat) {
  88.  
  89.         case RQXLOAD:                 /* Load & define functions */
  90.             scode = funcload() ? RSRSLT : -RSERR;
  91.             break;
  92.  
  93.         case RQSUBR:                  /* Handle external function request */
  94.             scode = dofun() ? RSRSLT: RSERR;
  95.             break;
  96.  
  97.         default:
  98.             break;
  99.         }
  100.     }
  101. }
  102. /*-----------------------------------------------------------------------*/
  103. /* FUNCLOAD  --  Define this application's external functions  */
  104.  
  105. int funcload()
  106. {
  107.     int i;
  108.     for (i = 0; i < ExtFuncCount; i++) {
  109.         if (!ads_defun(exfun[i], i))
  110.             return RTERROR;
  111.     }
  112.     return RTNORM;
  113. }
  114. /*-----------------------------------------------------------------------*/
  115. /* DOFUN -- Execute external function (called upon an RQSUBR request) */
  116.  
  117. int
  118. /*FCN*/dofun()
  119. {
  120.     struct resbuf *getargs = NULL, *rb = NULL;
  121.     int val;
  122.     matrix m;
  123.     vec zaxis;
  124.     int count = 0;                    /* counter for # of arguments */
  125.  
  126.     /* Get the required arguments */
  127.  
  128.     if ((getargs = ads_getargs()) == NULL)
  129.         return 0;
  130.  
  131.     /* Check the type and number of args */
  132.  
  133.     rb = getargs;
  134.     while ( rb != NULL ) {
  135.         if (rb->restype != RTREAL) {
  136.             ads_printf(/*MSG2*/"\nÑHíu├■ºO %dív╢╟│Ω ARBMAT íC", rb->restype);
  137.             ads_printf(/*MSG3*/"\n┐∙╗~:  ñ▐╝╞ %d í╨ ╢╖¼░íu╣Ω╝╞ívíC", count);
  138.             ads_relrb(getargs);
  139.             return 0;
  140.         }
  141.         zaxis[count] = rb->resval.rreal;
  142.         rb = rb->rbnext;
  143.         count++;
  144.     }
  145.  
  146.     /* Release the argument list */
  147.     ads_relrb(getargs);
  148.  
  149.     /* Make sure we have enough arguments after leaving while loop */
  150.     if ( count < 2 ) {
  151.         ads_printf(/*MSG4*/"\n\
  152. ┐∙╗~: ñ▐╝╞ %d í╨ íuñ▐╝╞ív╝╞Ñ╪ñú¿¼íC", count);
  153.         ads_printf(/*MSG5*/"\nÑ╬¬k: (arbmat X Y Z)\n");
  154.         return 0;
  155.     }
  156.  
  157.     if ((val = ads_getfuncode()) < 0)
  158.         return 0;
  159.  
  160.     switch (val) {
  161.  
  162.     case 0:
  163.         prtaxis(/*MSG6*/"┐ΘñJ", zaxis);
  164.         ads_printf("\n");
  165.  
  166.         if (arbmat(m, zaxis) == BAD) {
  167.             ads_printf(/*MSG7*/"XíBY ⌐╬ Z ñúÑi¼░íu0ívíC\n");
  168.         } else {
  169.             prtaxis(/*MSG0*/"X", m[X]);
  170.             prtaxis(/*MSG0*/"Y", m[Y]);
  171.             prtaxis(/*MSG0*/"Z", m[Z]);
  172.             ads_printf("\n");
  173.         }
  174.         break;
  175.     default:
  176.         break;
  177.     }
  178.  
  179.     ads_retvoid();
  180.  
  181. }
  182.  
  183.  
  184. int
  185. arbmat(m, zaxis)
  186.   matrix m;                           /* output matrix */
  187.   vec zaxis;                          /* X, Y, Z */
  188. {
  189.     static matrix ref = {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}};
  190.  
  191.     if (univec(m[Z], zaxis) == BAD)
  192.         return (BAD);
  193.  
  194.     if (rabs(m[Z][X]) < ARBBOUND && rabs(m[Z][Y]) < ARBBOUND)
  195.         cross(m[X], ref[Y], m[Z]);
  196.     else
  197.         cross(m[X], ref[Z], m[Z]);
  198.  
  199.     univec(m[X], m[X]);
  200.  
  201.     cross(m[Y], m[Z], m[X]);
  202.     univec(m[Y], m[Y]);
  203.  
  204.     return (GOOD);
  205. }
  206.  
  207.  
  208. int
  209. univec(a, b)
  210.   vec a, b;
  211. {
  212.     real d;
  213.  
  214.     if ((d = (b[X] * b[X] + b[Y] * b[Y] + b[Z] * b[Z])) < EPS)
  215.         return (BAD);
  216.  
  217.     d = 1.0 / sqrt(d);
  218.  
  219.     a[X] = b[X] * d;
  220.     a[Y] = b[Y] * d;
  221.     a[Z] = b[Z] * d;
  222.     return (GOOD);
  223. }
  224.  
  225.  
  226. real
  227. rabs(a)
  228.   real a;
  229. {
  230.     return (a > 0.0 ? a : -a);
  231. }
  232.  
  233.  
  234. void
  235. cross(a, b, c)
  236.   vec a, b, c;
  237. {
  238.     a[X] = b[Y] * c[Z] - b[Z] * c[Y];
  239.     a[Y] = b[Z] * c[X] - b[X] * c[Z];
  240.     a[Z] = b[X] * c[Y] - b[Y] * c[X];
  241. }
  242.  
  243.  
  244. void
  245. prtaxis(s, a)
  246.   char *s;
  247.   vec a;
  248. {
  249.     int i;
  250.     char temp[50];
  251.  
  252.     ads_printf(/*MSG8*/"\n%s ╢b: ", s);
  253.     for (i = X; i <= Z; i++) {
  254.         sprintf(temp, "%f", a[i]);    /* printf can disagree with < 0.0 ! */
  255.         ads_printf("%s%f ", (temp[0] == '-') ? "" : "+", a[i]);
  256.     }
  257. }
  258.