home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 March / Chip_1998-03_cd.bin / tema / jpcad / ADK / SAMPLES / DLINE / DLINE.CP_ / DLINE.CP
Text File  |  1998-01-21  |  8KB  |  291 lines

  1. /*                                                                           090896
  2.         
  3.     DLINE.CPP (c) 1996, ANTEK CS
  4.  
  5.     Description:
  6.       Sample file for demonstrating the use of ADK (Amoeba Development Kit).
  7.       Draws double lines with chosen width.
  8.  
  9.       THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED
  10.       WARRANTY.  ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR
  11.       PURPOSE AND OF MERCHANTABILITY ARE HEREBY DISCLAIMED.
  12.  
  13. */
  14.  
  15.  
  16. #include <windows.h>
  17. #include <math.h>
  18. #include <stdio.h>
  19.  
  20. #include <adk.h>
  21.  
  22. #define SQR(x) ((x) * (x))
  23. #define EVNAMES_ENTITIES_COLOR          "COLOR"
  24. #define EVNAMES_ENTITIES_LINETYPE       "LINETYPE"
  25.  
  26.  
  27. const double    EPSILON = 1E-9;  // smallest distance between start and end point
  28. const char    APP_NAME[] = "Dline";
  29.  
  30. static HINSTANCE g_hInst;
  31.  
  32. static void    ProcedureCall(long);
  33. static void    Dline(void);
  34. static int DrawDoubleLine(A_PointS, A_PointS, double, A_PointS, A_EntH*, A_EntH*);
  35.  
  36. long A_CALLBACK DragLine(void FAR*, A_PointS, A_MatrixS FAR*);
  37. void A_CALLBACK Status(long,long);
  38. void A_CALLBACK Error(long);
  39.  
  40. ///////////////////////////////////////////////////////////////////////////////////////////////
  41. #ifndef    A_16BIT
  42. int WINAPI    WinMain(
  43.         HINSTANCE,
  44.         HINSTANCE,
  45.         LPSTR,
  46.         int)
  47.     {
  48.      long    lReturn = A_Adk(A_VERSION,Status,Error);
  49.  
  50.      if (lReturn!=A_ERROR_OK)
  51.      {
  52.          char    Buffer[128];
  53.  
  54.          sprintf(Buffer, "Failed to initiate communication with JPCAD (0x%lX)", lReturn);
  55.          MessageBox(NULL,Buffer, APP_NAME, MB_ICONSTOP);
  56.          return -1;
  57.      }
  58.      return 0;
  59.     }
  60. #else
  61. int PASCAL    WinMain(
  62.         HINSTANCE hInstance,
  63.         HINSTANCE,
  64.         LPSTR,
  65.         int)
  66.     {
  67.      g_hInst = hInstance;
  68.      long lReturn;
  69.  
  70.      A_StatusF FAR*lpfnStatus = (A_StatusF FAR*)MakeProcInstance ((FARPROC) Status, g_hInst);
  71.      A_ErrorF FAR*lpfnError = (A_ErrorF FAR*)MakeProcInstance ((FARPROC) Error, g_hInst);
  72.                           
  73.      if ((lReturn = A_Adk(A_VERSION,lpfnStatus,lpfnError))!=A_ERROR_OK)
  74.          {
  75.          char    Buffer[128];
  76.  
  77.          sprintf(Buffer, "Failed to initiate communication with JPCAD (0x%lX)", lReturn);
  78.          MessageBox(NULL,Buffer, APP_NAME, MB_ICONSTOP);
  79.          }
  80.      
  81.      FreeProcInstance((FARPROC)(lpfnStatus));
  82.      FreeProcInstance((FARPROC)(lpfnError));
  83.  
  84.      return 0;
  85.     }
  86. #endif
  87.  
  88. ///////////////////////////////////////////////////////////////////////////////////////////////
  89. void A_CALLBACK Error(long nErrorCode)
  90. {
  91.     char s[80];
  92.     sprintf(s,"\nError code %d",nErrorCode);
  93.     A_Prompt(s);
  94. }
  95.  
  96. ///////////////////////////////////////////////////////////////////////////////////////////////
  97. void A_CALLBACK Status(long nStatus, long nCmdID)
  98. {
  99.         switch(nStatus)
  100.             {
  101.             case A_LOAD:
  102.                 A_Prompt("\nDLINE - defining new command.");
  103.                 A_DefCmd("dline", 0, FALSE);
  104.                 break;
  105.             case A_CMD_CALL:
  106.                  // procedure was called
  107.                 ProcedureCall(nCmdID);
  108.                 break;
  109.             case A_UNLOAD:
  110.                  // unloading was requested
  111.                 A_Prompt("\nDLINE - unloading.");
  112.                 return;
  113.             default:
  114.                 // unknown status code
  115.                 break;
  116.             }
  117. }
  118.  
  119. ///////////////////////////////////////////////////////////////////////////////////////////////
  120. static void    ProcedureCall(long CmdID)
  121.     {
  122.     switch(CmdID)
  123.     // launch procedure with given code
  124.         {
  125.         case 0:
  126.             Dline();
  127.             break;
  128.         default:
  129.             A_Prompt("Bad procedure code!");
  130.             break;
  131.         }
  132.     }
  133.  
  134.  
  135. ///////////////////////////////////////////////////////////////////////////////////////////////
  136. long A_CALLBACK DragLine(void FAR*Start, A_PointS End, A_MatrixS FAR*n)
  137.     {   
  138.      A_DrawLine(*(A_PointS FAR*)Start,End);
  139.      return 1;
  140.     };
  141.  
  142.  
  143. ///////////////////////////////////////////////////////////////////////////////////////////////
  144. static void    Dline(void)
  145.     {
  146.     double DlineWidth = 10.0;
  147.     BOOL IsPrevLine=FALSE;
  148.     A_PointS StartPoint,EndPoint,LineVect;
  149.     A_PointS L1StartPoint,L2StartPoint,L1EndPoint,L2EndPoint,Vector,PrevVector,Vector1,Vector2;
  150.     A_VectorS PVector,V;
  151.     A_PointS S1,S2,E1,E2,Intersect1, Intersect2;
  152.     A_EntH PrevLine1,PrevLine2;
  153.     double pWidth,t1,t2;
  154.     long result;
  155.  
  156.     // Get line width or set it as 10.0 if the user chooses default
  157.     long retvalue;
  158.     retvalue=A_GetDouble("Width ",NULL,"10.0",&DlineWidth);
  159.     if (retvalue==A_GET_CANCEL)
  160.      return;
  161.     else 
  162.         if (retvalue==A_GET_DEFAULT)
  163.             DlineWidth=10.0;
  164.     // Get first point of DLINE
  165.     if (A_GetPoint("First dline point",NULL,NULL,&StartPoint)==A_GET_CANCEL)
  166.         return;
  167.  
  168. #ifdef    A_16BIT
  169.     A_GetDragF *lpfnDragLine = (A_GetDragF*)MakeProcInstance ((FARPROC) DragLine, g_hInst);
  170. #endif
  171.  
  172.     for(;;)
  173.     // while there are any points...  
  174.      {
  175.         result = A_GetPointDrag("Next dline point",NULL,"",A_A_EMPTY,&StartPoint,
  176. #ifdef    A_16BIT
  177.                     lpfnDragLine,
  178. #else
  179.                     DragLine,
  180. #endif
  181.                     &StartPoint,&EndPoint);
  182.  
  183.         if ((result==A_GET_CANCEL)||(result==A_GET_DEFAULT))
  184.             break;
  185.         LineVect=A_G_SubVV(StartPoint,EndPoint);
  186.         if(A_G_LenV(LineVect)<EPSILON)
  187.          {
  188.            continue;
  189.         };
  190.           Vector=A_G_SubVV(StartPoint,EndPoint);
  191.           PVector=A_G_PerpenV(Vector,1);
  192.           V=A_G_MulVR(A_G_NormV(PVector),DlineWidth/2);
  193.           S1=A_G_AddVV(StartPoint,V);
  194.           E1=A_G_AddVV(EndPoint,V);
  195.           PVector=A_G_PerpenV(Vector,0);
  196.           V=A_G_MulVR(A_G_NormV(PVector),DlineWidth/2);
  197.           S2=A_G_AddVV(StartPoint,V);
  198.           E2=A_G_AddVV(EndPoint,V);
  199.  
  200.         if (IsPrevLine)
  201.          {
  202.  
  203.           A_LINE_Get(PrevLine1,&L1StartPoint,&L1EndPoint,
  204.                 /*layer*/NULL,/*color*/NULL,/*ltype*/NULL,&pWidth);
  205.           A_LINE_Get(PrevLine2,&L2StartPoint,&L2EndPoint,
  206.                 /*layer*/NULL,/*color*/NULL,/*ltype*/NULL,&pWidth);
  207.           Vector1=A_G_SubVV(S1,E1);
  208.           
  209.           // compute intersection of first lines...
  210.           if (A_G_IntersLL(L1StartPoint,A_G_SubVV(L1StartPoint,L1EndPoint),
  211.                   S1,Vector1,&t1,&t2)==0)
  212.                      {
  213.                           Intersect1 = A_G_AddVV(A_G_MulVR(A_G_SubVV(L1StartPoint,L1EndPoint),t1),L1StartPoint);
  214.  
  215.                           // change previous first line so as to end on the intersection point
  216.                           A_LINE_Change(PrevLine1,L1StartPoint,Intersect1,
  217.                                 A_USE_CURRENT_ENT,// layer
  218.                                 A_USE_CURRENT,// color
  219.                                   A_USE_CURRENT,// ltype
  220.                                 pWidth);
  221.                       }
  222.                  // ... or let it be the ending point of the previous first line
  223.                  else
  224.                         {
  225.                           A_PointS v,w;
  226.                        v = A_G_NormV(PrevVector);
  227.                        w = A_G_NormV(Vector);
  228.  
  229.                           if (A_G_SMulVV(v,w)<0)
  230.                         Intersect1=L2EndPoint;
  231.                        else
  232.                            Intersect1=L1EndPoint;
  233.                         };
  234.             // draw first line
  235.             A_LINE_Make(Intersect1,E1,A_USE_CURRENT_ENT,A_USE_CURRENT,A_USE_CURRENT,0,FALSE,&PrevLine1);
  236.           Vector2=A_G_SubVV(S2,E2);
  237.  
  238.           // compute intersection of second lines...
  239.           if (A_G_IntersLL(L2StartPoint,A_G_SubVV(L2StartPoint,L2EndPoint),
  240.                   S2,Vector2,&t1,&t2)==0)
  241.                       {
  242.                         Intersect2 = A_G_AddVV(A_G_MulVR(A_G_SubVV(L2StartPoint,L2EndPoint),t1),L2StartPoint);
  243.                           // change previous second line so as to end on intersection point
  244.                           A_LINE_Change(PrevLine2,L2StartPoint,Intersect2,
  245.                                 A_USE_CURRENT_ENT,// layer
  246.                                 A_USE_CURRENT,// color
  247.                                   A_USE_CURRENT,// ltype
  248.                                 pWidth);
  249.                     }
  250.                 // ... or let it be the ending point of the previous second line
  251.                 else
  252.                     {
  253.                           A_PointS v,w;
  254.                        v = A_G_NormV(PrevVector);
  255.                        w = A_G_NormV(Vector);
  256.                           if (A_G_SMulVV(v,w)<0)
  257.                         Intersect2=L1EndPoint;
  258.                        else
  259.                            Intersect2=L2EndPoint;
  260.                     };
  261.           
  262.         // draw second line
  263.         A_LINE_Make(Intersect2,E2,A_USE_CURRENT_ENT,A_USE_CURRENT,A_USE_CURRENT,0,FALSE,&PrevLine2);
  264.  
  265.          }
  266.         else
  267.          {
  268.             // draw first line
  269.             if (A_LINE_Make(S1,E1,A_USE_CURRENT_ENT,A_USE_CURRENT,A_USE_CURRENT,0,FALSE,&PrevLine1)<0)
  270.               {
  271.                   A_Prompt("Error drawing line 1!");
  272.               }
  273.             // draw second line
  274.             if (A_LINE_Make(S2,E2,A_USE_CURRENT_ENT,A_USE_CURRENT,A_USE_CURRENT,0,FALSE,&PrevLine2)<0)
  275.                {
  276.                    A_Prompt("Error drawing line 2!");
  277.              };
  278.           }
  279.         StartPoint=EndPoint;
  280.         PrevVector=Vector;
  281.         IsPrevLine=TRUE;
  282.  
  283.      // end while
  284.      }
  285.      
  286. #ifdef    A_16BIT
  287.       FreeProcInstance((FARPROC)(lpfnDragLine)); 
  288. #endif
  289.     };
  290.  
  291.