Friday, August 20, 2010

MyLine Command (A Complete Example)

We have finally reached the point where we can produce an entire example.   In my previous post I explained how, conceptually, this command will work.  Now, here is the actual code for the command along with a lot of comments.

I have also copied and commented out the definitions for the relevant elements that we are going to create.

I hope that you fire up Visual Studio and try building this code; if you are impatient though and want to just run it, here is the dll.

Code Snippet - MyLine Command (A Complete Example)
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. //    Written by: L. Lee Saunders
  4. //    Date: 8/15/2010
  5. //    (C)2010 BeerAndPretzelGames.com
  6. //    All rights reserved
  7. //
  8. ////////////////////////////////////////////////////////////////////////////
  9. #include <windows.h>
  10.  
  11. extern "C"
  12. {  
  13.     #include <XP.H>
  14.     #include <Extend/Mysvc.h>
  15. }
  16.  
  17. #define XPID 0xF000
  18.  
  19. void XPCALL About(void);
  20. void XPCALL MYLINE(void);
  21.  
  22. char CList[]="MYLINE\0";
  23. PCMDPROC PList[]={About,MYLINE};
  24.  
  25. XP MyXP =
  26.    { 0, CList, PList, 0, 0, 0, XPID, 0, 500, 0, 0, 500 };
  27. ////////////////////////////////////////////////////////////////////////////
  28.  
  29.  
  30. ////////////////////////////////////////////////////////////////////////////
  31. FORMST(MyAPkt,"BLOG DLL commands:\n\n"
  32.        "\tMYLINE - Duplicating the Line Command #s\n\0")
  33.  
  34. void XPCALL About (void)
  35. {
  36.     FormSt(&MyAPkt,RSC(FD_MsgBox));
  37. }
  38. ////////////////////////////////////////////////////////////////////////////
  39.  
  40.  
  41. ////////////////////////////////////////////////////////////////////////////
  42. void XPCALL MYLINE2 (int Result,int Result2,int Result3);
  43. void XPCALL MYLINE3 (int Result,int Result2,int Result3);
  44.  
  45. //This is the definition of LINE2
  46. //==========================================================================
  47. //typedef struct
  48. //{
  49. //    CSTUFF    CStuff;              // entity properties
  50. //    GLINE2    Line;                // line properties
  51. //} LINE2;
  52.  
  53. //This is the definition of GLINE2
  54. //==========================================================================
  55. //typedef struct
  56. //{
  57. //    GPOINT2 p1;                    // starting point
  58. //    GPOINT2 p2;                    // ending point
  59. //} GLINE2;
  60.  
  61. //This is the definition of GPOINT2
  62. //==========================================================================
  63. //typedef struct
  64. //{
  65. //    float x;
  66. //    float y;
  67. //} GPOINT2;
  68.  
  69. //This is the definition of CSTUFF
  70. //==========================================================================
  71. //typedef struct
  72. //{
  73. //    int    ERLen;                    // entity record length
  74. //    unsigned char    EType;          // entity type code
  75. //    char    EFlags;                  // erase/select bits
  76. //    char    EFlags2;                 // extra flags
  77. //    unsigned char    EColor;         // entity color
  78. //    unsigned char    EColor2;        // fill (2nd) color
  79. //    char    EThick;                  // pen thickness 0..25.4 mm
  80. //    short    WPlane;                 // workplane (0 = XY plane)
  81. //    short    ELayer;                 // layer
  82. //    short    ELStyle;                // line style (0=solid)
  83. //    short    GroupID;                // group id (0 = not grouped)
  84. //    short    EFStyle;                // fill style (0=hollow)
  85. //    float    LWidth;                 // line width
  86. //    int    Tag;                      // entity tag id
  87. //} CSTUFF;
  88.  
  89. //This is the working structure that we will use to create all the lines
  90. LINE2 BuildL = {
  91.     sizeof(LINE2),                // entity record length
  92.     ET_LINE2,                     // entity type code
  93.     0,                            // erase/select bits
  94.     0,                            // extra flags
  95.     1,                            // entity color
  96.     1,                            // fill (2nd) color
  97.     0,                            // pen thickness 0..25.4 mm
  98.     0,                            // workplane (0 = XY plane)
  99.     1,                            // layer
  100.     0,                            // line style (0=solid)
  101.     0,                            // group id (0 = not grouped)
  102.     0,                            // fill style (0=hollow)
  103.     0.0,                          // line width
  104.     0,                            // entity tag id
  105.     0.0,                          // starting point: x
  106.     0.0,                          // starting point: y
  107.     1.0,                          // ending point: x
  108.     1.0                           // ending point: y
  109. };
  110.  
  111. FORMST(lpszFirstPoint,"1st point:\0")
  112.  
  113. FORMST(lpszNextPoint,"Next point:\0")
  114.  
  115. RDATA P1Req =
  116.    { sizeof(RDATA), RD_2DC, NULL, RDF_C, (DWORD*)&BuildL.Line.p1,
  117.     (DWORD*)&lpszFirstPoint, RDC_XH, MYLINE2, NULL, NULL, 0, NULL, 0};
  118.  
  119. RDATA P2Req =
  120.    { sizeof(RDATA), RD_2DC, NULL, RDF_C, (DWORD*)&BuildL.Line.p2,
  121.     (DWORD*)&lpszNextPoint, RDC_RBAND, MYLINE3, NULL, NULL, 0, NULL, 0};
  122.  
  123. ////////////////////////////////////////////////////////////////////////////
  124.  
  125.  
  126. ////////////////////////////////////////////////////////////////////////////
  127. void XPCALL MYLINE (void)
  128. {
  129.     ReqData(&P1Req);  //get first point
  130. }
  131. ////////////////////////////////////////////////////////////////////////////
  132.  
  133.  
  134. ////////////////////////////////////////////////////////////////////////////
  135. void XPCALL MYLINE2 (int Result,int Result2,int Result3)
  136. {
  137.     if (Result != X_OK) { CmdEnd(); return; } //If we did not get valid info
  138.     
  139.     MarkUndoAdd();  //Enable Undo
  140.     NewCsrOrg(BuildL.Line.p1.x, BuildL.Line.p1.y);  //Set cursor origin
  141.     ReqData(&P2Req);  //Get next point
  142. }
  143. ////////////////////////////////////////////////////////////////////////////
  144.  
  145.  
  146. ////////////////////////////////////////////////////////////////////////////
  147. void XPCALL MYLINE3 (int Result,int Result2,int Result3)
  148. {
  149.     pENTREC pEntRec;  //Create a new drawing database entity
  150.  
  151.     if (Result != X_OK) { CmdEnd(); return; } //If we did not get valid info
  152.  
  153.     //This gets all the current stuff like current color and assigns it
  154.     //to our line entity.
  155.     GetCStuff(&BuildL.CStuff);
  156.  
  157.     //Append a copy of our line to the DB and return a pointer to the new
  158.     //entity in the database.  We assign it to pEntRec.
  159.     pEntRec=DLApnd(NULL,(pENTREC)&BuildL);
  160.     
  161.     EDraw(pEntRec);  //Draw our line
  162.     ShowChanges(); //Needed for CC3 to "Show Changes" to the DB
  163.  
  164.     BuildL.Line.p1.x=BuildL.Line.p2.x;  //move 2nd x to 1st x
  165.     BuildL.Line.p1.y=BuildL.Line.p2.y;  //move 2nd y to 1st y
  166.     
  167.     NewCsrOrg(BuildL.Line.p1.x,BuildL.Line.p1.y);  //Set cursor origin
  168.  
  169.     ReqData(&P2Req);  //Get next point
  170. }
  171. ////////////////////////////////////////////////////////////////////////////
  172.  
  173.  
  174. ////////////////// DllMain - XP initialization & Unload code ///////////
  175. BOOL WINAPI DllMain (HINSTANCE hDLL, DWORD dwReason, LPVOID lpReserved)
  176. {
  177.     switch (dwReason)
  178.     {
  179.         case DLL_PROCESS_ATTACH:
  180.         {
  181.             MyXP.ModHdl=hDLL;
  182.             XPRegCmd(&MyXP);
  183.             break;
  184.         }
  185.         case DLL_PROCESS_DETACH:
  186.         {
  187.             XPUnregCmd(&MyXP);
  188.             break;
  189.         }
  190.     }
  191.     return TRUE;
  192. }
  193. ////////////////////////////////////////////////////////////////////////////

Sunday, August 15, 2010

Beads – How to Wait for Input

[Note: This is copied from the "FastCAD XP Programming Reference" that comes with the XP toolkit.  I copied this, with editing only for clarity, because it was so descriptive]

Nowhere in the Windows programming environment will you find a greater need to change your thinking than with regard to getting input.

In Windows, our programs receive messages that inform us of discrete user input - a mouse movement, button click, or a keyboard press. Other than dialog boxes, there is no way provided to wait for a complete word or line of text or other aggregate input in your program and then continue. We must write our program so that each keystroke is processed and then control is returned all the way to Windows.

This means that in a piece of code (creating a text entity, for example) we can not have one procedure that calls another to get a line of text and then continues with the next instruction when it is available, as would be done in a DOS or Unix program. The current position within that first command procedure is normally maintained on the stack, perhaps as a nested chain of procedure calls.

In Windows, after every event (such as a single key press), we have to return all the way up the stack back to Windows! This is why most Windows programs do all of their input through dialog boxes.

The FastCAD solution to this problem is to think of a command as a string of "beads" - little pieces of a command that each do one small part of the overall command, each a piece that can be done in response to an appropriate input event.

Lets take as an example a command to create a line entity. In traditional programming, its logic might be:

LineCommand:

prompt the user for the first point
- Get the first point if there was a problem getting it, end the command

prompt the user for the second point
- Get the second point if there was a problem getting it, end the command

Create the line entity

Append it to the drawing database

Draw the line entity

End the command

In FastCAD, we have an API service, ReqData, that handles data requests. We call ReqData with a packet that tells it about the data request, and then we go back to Windows.

When the data becomes available, ReqData will call another procedure specified in our original request packet to continue with our command. This means that we write our command as a series of procedures, linked together by RDATA packets, each one executing when its data becomes available, and returning to Windows when it needs to wait for more input.

The sequence looks like this:
==========================
Event: Action (Bead procedure):
- LINE function entered
- - Request first point by calling ReqData
- Line function ends

User enters the first request for a point:

Event: Action (Bead procedure):
- Second LINE function entered
- - Did the user cancel the request?
- - - End Command
- - Request second point by calling ReqData
- Second Line function ends

User enters the second request for a point:

Event: Action (Bead procedure):
- Third LINE function entered
- - Did the user cancel the request?
- - - End Command
- - Else
- - - Create the line entity
- - - Append it to the drawing database
- - - Draw the line entity
- - - End Command
-  Third Line function ends

The LINE command is implemented in 3 beads, each of which is called in response to a single event, such as a mouse click or a RETURN keypress. The ReqData service provides all of the intermediate beads that track mouse movement and build up strings of characters into words like "LINE" for you.

Next post, we will start to implement the LINE command.

Tuesday, August 3, 2010

Ok, you requested data, now what did you get?

Ok, so you have messed around with the RDATA function and you want to select an element, like a circle.  So you create a new RDATA function with the fallowing parameters:
  • Cursor:  RDC_PICK
  • Flags:  RDF_C
  • Type:  RD_Pick1
 Now create a data store variable of type pEntRec.

A pEntRec type is a very important type in writing XP's.  The pEntRec type is the generic entity pointer that can point to any type of drawing entity.  So, if you selected a circle with your RDATA call, pEntRec will point to the the circle entry within the "Drawing Database"

The "Drawing Database" is the collection of all the visible elements in the map.  When we select elements, Campaign Cartographer returns a pointer to the selected element in the "Drawing Database".

Once you get a pointer to an entity, you can edit, interrogate or delete it.

Every entity shares some common data - appropriately named: CSTUFF

CStuff is defined as:
Code Snippet - CSTUFF
  1. typedef struct
  2. {
  3.     int              ERLen;        // entity record length
  4.     unsigned char    EType;        // entity type code
  5.     char             EFlags;       // erase/select bits
  6.     char             EFlags2;      // extra flags
  7.     char             EColor;       // entity color
  8.     char             EColor2;      // fill (2nd) color
  9.     char             EThick;       // pen thickness 0..25.4 mm
  10.     short            WPlane;       // workplane (0 = XY plane)
  11.     short            ELayer;       // layer
  12.     short            ELStyle;      // line style (0=solid)
  13.     short            GroupID;      // group id (0 = not grouped)
  14.     short            EFStyle;      // fill style (0=hollow)
  15.     float            LWidth;       // line width
  16.     int              Tag;          // entity tag id
  17. } CSTUFF;


For example you have a pEntRec named pENTREC and you want to get the entities color:
pENTREC->CStuff.EColor