Tuesday, September 28, 2010

The Spiral Command - In Three Parts (Part #1)

The Spiral Command - In Three Parts (Part #1)
In this post and the next two posts, I will be building a spiral command.  In this post we will be dealing with the algorithm.  When we are done with this post, we will have a command that draws a spiral that would have 20 loops if it started in the middle, but we also told the algorithm to not draw the innermost 10 loops.  We have also hard currently coded the command to draw the spiral with a distance of 10 between each loop.

I will be using the name MYSPIRALM for this temporary command.  When finished, this will be the command for the macro version.

Note:  There is a naming convention that states if you have both a dialog driven command and a comand that is all command line for macros, the macro version ends with an M.

 Hardcoded values:
  • Distance Between Loops: 10
  • Number of Inner Loops Not Drawn: 10
  • Total Number of Loops: 20
  • Clockwise/Counter Clockwise: Clockwise
  • Growth Between Loops: None
Our next post will implement RecData's to get the values that we currently are hardcoded.  Then, we will finish up with a dialog driven command.

Friday, August 20, 2010MyLine 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

Code Snippet - MySpiralM Command
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. //    File Name: MySpiral.cpp
  4. //    Written by: L. Lee Saunders
  5. //    (C)2010 Beer & Pretzel Games
  6. //    All rights reserved
  7. //
  8. ////////////////////////////////////////////////////////////////////////////
  9.  
  10. #include <windows.h>
  11. #include <math.h>
  12.  
  13. extern "C"
  14. {
  15.     #include <xp.h>
  16.     #include <Extend/Mysvc.h>
  17. }
  18.  
  19. extern XP MyXP;
  20.  
  21. ////////////////////////////////////////////////////////////////////////////
  22.   
  23. void XPCALL DrawSpiral (int Result,int Result2,int Result3);
  24. GPOINT2 CenterPoint;
  25.  
  26. FORMST(lpszCenterPoint,"Center Point:\0")
  27.   
  28. RDATA PCenterReq =
  29.    { sizeof(RDATA), RD_2DC, NULL, RDF_C, (DWORD*)&CenterPoint,
  30.   (DWORD*)&lpszCenterPoint, RDC_XH, DrawSpiral, NULL, NULL, 0, NULL, 0};
  31. ////////////////////////////////////////////////////////////////////////////
  32.   
  33.   
  34. ////////////////////////////////////////////////////////////////////////////
  35. void XPCALL MySpiralM (void)
  36. {
  37.     ReqData(&PCenterReq);  //get center point
  38. }
  39. ////////////////////////////////////////////////////////////////////////////
  40.  
  41.  
  42. ////////////////////////////////////////////////////////////////////////////
  43. void XPCALL DrawSpiral (int Result,int Result2,int Result3)
  44. {
  45.     if (Result != X_OK) { CmdEnd(); return; }
  46.  
  47.     float Growth = 1;
  48.     float LoopDistance = 10;
  49.  
  50.     int Rotations = 20;
  51.     int InnerLoopsToSkip = 10;
  52.     const char * Direction = "0\0";
  53.  
  54.     pENTREC pEntRec;  //Create a new drawing database entity
  55.     
  56.     float CenterX = CenterPoint.x;
  57.     float CenterY = CenterPoint.y;
  58.  
  59.     int iDirection;
  60.  
  61.     MarkUndo();
  62.  
  63.     // Insert an empty path into the drawing list
  64.     pEntRec=DLApndE(NULL, ET_PATH2, sizeof(PATH2)+sizeof(GPOINT2));
  65.  
  66.     // Get the common stuff
  67.     GetCStuff(pEntRec);
  68.  
  69.     // Set the smoothing to Parabolic Blend (through-point)
  70.     pEntRec->Path.Path.SmType = SM_PB;
  71.  
  72.     // Set the centerpoint of the spiral
  73.     pEntRec->Path.Path.Nodes[0].x = CenterPoint.x;    
  74.     pEntRec->Path.Path.Nodes[0].y = CenterPoint.y;
  75.  
  76.     // How many points to draw per circle
  77.     const double STEPS_PER_ROTATION = 50;
  78.     
  79.     // Amount to add to angle at each step
  80.     double increment = M_PI/STEPS_PER_ROTATION;  // Splitting up
  81.  
  82.     double theta = 2*M_PI*InnerLoopsToSkip;  // This allows for an open area
  83.     // inside the spiral
  84.  
  85.     // Determines if the loops rotates clockwise or counter clockwise
  86.     iDirection = strcmp(Direction, "0") == 0 ? 1 : -1;
  87.  
  88.     while(theta < (Rotations * 2) * M_PI)
  89.     {
  90.         pEntRec=DLResize(pEntRec, pEntRec->Path.CStuff.ERLen +
  91.           sizeof(GPOINT2));
  92.  
  93.         pEntRec->Path.Path.Nodes[pEntRec->Path.Path.Count].x=(float)
  94.           (CenterX + theta * cos(theta) * (1/(M_PI * 2)) * LoopDistance *
  95.           iDirection);
  96.         pEntRec->Path.Path.Nodes[pEntRec->Path.Path.Count].y=(float)
  97.           (CenterY + theta * sin(theta) * (1/(M_PI * 2)) * LoopDistance);
  98.         
  99.         pEntRec->Path.Path.EParm=(float)pEntRec->Path.Path.Count;
  100.         pEntRec->Path.Path.Count++;
  101.         
  102.         LoopDistance = LoopDistance * Growth;
  103.         theta = theta + increment; //If theta grows faster then increment,
  104.         //it will have wider spaces between each loop    
  105.     }
  106.                 
  107.     EDraw(pEntRec);  //Draw our line
  108.     ShowChanges();   //Needed for CC3 to "Show Changes" to the DB
  109.  
  110.     CmdEnd();
  111.     return;
  112. }
  113. ////////////////////////////////////////////////////////////////////////////

2 comments:

  1. Hi.

    I have a small question. When I try to build this, the linker complains about "unresolved external symbol _ShowChanges@0", and the linking fails.

    I am able to compile and link it by removing the call to ShowChanges() in the code, and the XP works perfectly as far as I can tell, but I am wondering what I am doing wrong since the linker can't find this symbol?

    ReplyDelete
  2. Remy, ShowChanges() is a call that was added due to the major number of changes in CC3. I'll inform Peter Olsen (The "Owner" of the ToolKit). I've recently noticed a number of other things that are in the version I have on my box (Sent to me by Peter when CC3 was first being developed) and the one that is on Peters website.

    ReplyDelete