home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 January / Chip_1997-01_cd.bin / ms95 / disk21 / dir03 / f013560.re_ / f013560.re
Text File  |  1996-04-02  |  16KB  |  584 lines

  1. /*----------------------------------------------------------------------+
  2. |                                    |
  3. |  Copyright (1995) Bentley Systems, Inc., All rights reserved.        |
  4. |                                    |
  5. |  "MicroStation" is a registered trademark and "MDL" and "MicroCSL"    |
  6. |  are trademarks of Bentley Systems, Inc.                    |
  7. |                                    |
  8. |  Limited permission is hereby granted to reproduce and modify this    |
  9. |  copyrighted material provided that the resulting code is used only     |
  10. |  in conjunction with Bentley Systems products under the terms of the    |
  11. |  license agreement provided therein, and that this notice is retained    |
  12. |  in its entirety in any such reproduction or modification.        |
  13. |                                    |
  14. +----------------------------------------------------------------------*/
  15. /*----------------------------------------------------------------------+
  16. |                                    |
  17. |   $Workfile:   cellpick.mc  $
  18. |   $Revision:   1.10  $
  19. |       $Date:   18 Dec 1995 16:50:26  $
  20. |                                    |
  21. +----------------------------------------------------------------------*/
  22. /*----------------------------------------------------------------------+
  23. |                                    |
  24. |   Function -                                |
  25. |                                    |
  26. |    Example MDL program for picking cells                |
  27. |                                    |
  28. |    - - - - - - - - - - - - - - - - - - - - - - - - - - - - -    |
  29. |                                    |
  30. |   Public Routine Summary -                        |
  31. |                                    |
  32. |    cellPick_cellButtonHook - Cell button hook            |
  33. |    cellPick_drawButton - Handle draw event processing        |
  34. |    cellPick_drawCell - Draw cell in window                |
  35. |    cellPick_getCellUorRange - Get cell range block            |
  36. |    cellPick_initCellInfo - Initialize cell information        |
  37. |    cellPick_loadLibraryIfNeeded - Load cell library        |
  38. |    cellPick_reloadFunction - Application reload function        |
  39. |    cellPick_showCell - Draw cell in dialog box            |
  40. |    main - main entry point                        |
  41. |                                    |
  42. +----------------------------------------------------------------------*/
  43. /*----------------------------------------------------------------------+
  44. |                                    |
  45. |   Include Files                               |
  46. |                                    |
  47. +----------------------------------------------------------------------*/
  48. #include    <mdl.h>
  49. #include    <mselems.h>
  50. #include    <dlogitem.h>
  51. #include    <userfnc.h>
  52. #include    <tcb.h>
  53. #include    <cmdlist.h>
  54. #include    <ctype.h>
  55. #include    <stdlib.h>
  56. #include    <string.h>
  57.  
  58. #include    "cellpick.h"        /*  Need to know the dialog id to open */
  59.  
  60. #include    <msfile.fdf>
  61. #include    <msview.fdf>
  62. #include    <mssystem.fdf>
  63. #include    <msrmatrx.fdf>
  64. #include    <msrsrc.fdf>
  65. #include    <msvec.fdf>
  66. #include    <mselmdsc.fdf>
  67. #include    <mscell.fdf>
  68. #include    <mscnv.fdf>
  69. #include    <dlogman.fdf>
  70.  
  71. /*----------------------------------------------------------------------+
  72. |                                    |
  73. |   Local type declarations                        |
  74. |                                    |
  75. +----------------------------------------------------------------------*/
  76. typedef struct cellbuttoninfo
  77.     {
  78.     char    cellName[7];
  79.     char    libName[MAXFILELENGTH];
  80.     boolean libAvailable;
  81.     }
  82. CellButtonInfo;
  83.  
  84. /*----------------------------------------------------------------------+
  85. |                                    |
  86. |   Local function declarations                     |
  87. |                                    |
  88. +----------------------------------------------------------------------*/
  89. /*----------------------------------------------------------------------+
  90. |                                    |
  91. |   Private Global variables                        |
  92. |                                    |
  93. +----------------------------------------------------------------------*/
  94.  
  95. /*----------------------------------------------------------------------+
  96. |                                    |
  97. | name        cellpick_checkCellLibrary                |
  98. |                                    |
  99. | author    BSI                    11/95        |
  100. |                                    |
  101. +----------------------------------------------------------------------*/
  102. Private int cellpick_checkCellLibrary 
  103. (
  104. boolean    *isThreeD,            /* <= TRUE if file is 3d */
  105. char    *fileName            /* => filename */
  106. )
  107.     {
  108.     int     status=SUCCESS;
  109.     FILE    *fp;
  110.     short   firstWord;
  111.     short   element, dimens;
  112.  
  113.     if ((NULL == fileName) || (!isThreeD))
  114.     return MDLERR_BADARG;
  115.  
  116.     if (NULL != (fp = fopen (fileName, "rb")))
  117.     {
  118.  
  119.     fread (&firstWord, 1, sizeof(firstWord), fp);
  120.  
  121.     /* get the fread status */
  122.     status = ferror(fp);
  123.     if (SUCCESS != status)
  124.         {
  125.         fclose (fp);
  126.         return MDLERR_READFAILED;
  127.         }
  128.  
  129. #       if !defined (BIG_ENDIAN)
  130.         element = (firstWord & 0xff00) >> 8;
  131.         dimens  = (firstWord & 0x00c0) >> 6;
  132. #       else
  133.         element = (firstWord & 0xff);
  134.         dimens  = (firstWord & 0xc000);
  135. #       endif
  136.  
  137.         /* check if the elment type is cell library header */
  138.         if (GROUP_DATA_ELM == element)
  139.         {
  140.         if (0 != dimens)
  141.             *isThreeD = TRUE;
  142.         else
  143.             *isThreeD = FALSE;
  144.         }
  145.     else
  146.         {
  147.         status = MDLERR_BADFILETYPE;
  148.         }
  149.     
  150.     fclose (fp);
  151.     return status;
  152.     }
  153.     else
  154.     {
  155.     return MDLERR_CANNOTOPENFILE;
  156.     }
  157.  
  158.     return  status;
  159.     }
  160.  
  161. /*----------------------------------------------------------------------+
  162. |                                    |
  163. | name        cellPick_getCellUorRange                |
  164. |                                    |
  165. | author    BSI                    7/90        |
  166. |                                    |
  167. +----------------------------------------------------------------------*/
  168. Private void cellPick_getCellUorRange
  169. (
  170. Dvector3d   *cellRange,
  171. MSElement   *cellHeader
  172. )
  173.     {
  174.     long    iCellRange[6];
  175.     int        i;
  176.  
  177.     memcpy (iCellRange, &cellHeader->ehdr.xlow, sizeof(iCellRange));
  178.  
  179.     for (i=0; i<6; i++)
  180.     iCellRange[i] = mdlCnv_fromScanFormat (iCellRange[i]);
  181.  
  182.     cellRange->org.x = (double) iCellRange[0];
  183.     cellRange->org.y = (double) iCellRange[1];
  184.     cellRange->org.z = (double) iCellRange[2];
  185.     cellRange->end.x = (double) iCellRange[3];
  186.     cellRange->end.y = (double) iCellRange[4];
  187.     cellRange->end.z = (double) iCellRange[5];
  188.     }
  189.  
  190. /*----------------------------------------------------------------------+
  191. |                                    |
  192. | name        cellPick_initCellInfo                    |
  193. |                                    |
  194. | author    BSI                    6/91        |
  195. |                                    |
  196. +----------------------------------------------------------------------*/
  197. Private void cellPick_initCellInfo
  198. (
  199. char    *labelField,
  200. char    *libName,
  201. char    *cellName,
  202. boolean *libAvailable
  203. )
  204.     {
  205.     int     status;
  206.     char    libFile[MAXFILELENGTH];
  207.     int        libFileLength;
  208.     boolean isThreeD;
  209.  
  210.     *libAvailable = TRUE;
  211.  
  212.     if (libFileLength = strcspn (labelField, ";"))
  213.     {
  214.     strncpy (libFile, labelField, libFileLength);
  215.     libFile[libFileLength] = '\0';
  216.     strncpy (cellName, labelField + libFileLength +1, 6);
  217.     cellName[6] = '\0';
  218.     if (mdlFile_find (libName, libFile, "MS_CELL", "cel"))
  219.         {
  220.         *libName = '\0';
  221.         *libAvailable = FALSE;
  222.         return;
  223.         }
  224.  
  225.     /* check if the file is a cell library & get its dimension*/
  226.     status = cellpick_checkCellLibrary (&isThreeD, libName);
  227.     if (SUCCESS == status)
  228.         {
  229.         /* check for a dimension mismatch (2d dgn file & 3d cell library)*/
  230.         if ((FALSE == mgds_modes.three_d) && (TRUE == isThreeD))
  231.             {
  232.             *libAvailable = FALSE;
  233.  
  234. #            if defined (DEBUG_PRINT)
  235.         printf("For cell %s, cell library %s is 3D & dgn file is 2D\n",
  236.             cellName, libName);
  237. #            endif
  238.             }
  239.         }
  240.     else
  241.         {
  242.         *libAvailable = FALSE;
  243.         }
  244.     }
  245.     else
  246.     {
  247.     strncpy (cellName, labelField, 6);
  248.     cellName[6] = *libName = '\0';
  249.     *libAvailable = FALSE;
  250.     }
  251.     }
  252.  
  253. /*----------------------------------------------------------------------+
  254. |                                    |
  255. | name        cellPick_loadLibraryIfNeeded                |
  256. |                                    |
  257. | author    BSI                    6/91        |
  258. |                                    |
  259. +----------------------------------------------------------------------*/
  260. Private int cellPick_loadLibraryIfNeeded
  261. (
  262. char    *libToLoad,
  263. boolean *libAvailable
  264. )
  265.     {
  266.     int     status = SUCCESS;
  267.     char    libName[MAXFILELENGTH];
  268.     boolean isThreeD;
  269.  
  270.     if (*libToLoad)
  271.     {
  272.          if (0 == strcmp (libToLoad, tcb->celfilenm))
  273.         {
  274.         *libAvailable = TRUE;
  275.         return status;
  276.         }
  277.  
  278.     /* check if the file is a cell library & get its dimension*/
  279.     status = cellpick_checkCellLibrary (&isThreeD, libToLoad);
  280.     if (SUCCESS == status)
  281.         {
  282.         /* check for a dimension mismatch (2d dgn file & 3d cell library)*/
  283.         if ((FALSE == mgds_modes.three_d) && (TRUE == isThreeD))
  284.             {
  285.             *libAvailable = FALSE;
  286.             }
  287.         else
  288.             {
  289.             status=mdlCell_attachLibrary (libName, libToLoad, NULL, TRUE);
  290.         if (SUCCESS == status)
  291.             {
  292.             *libAvailable = TRUE;
  293.             }
  294.             else
  295.             {
  296.             *libAvailable = FALSE;
  297.             }
  298.             }
  299.         }
  300.     else
  301.         {
  302.         *libAvailable = FALSE;
  303.         }
  304.     }
  305.     return status;
  306.     }
  307.  
  308. /*----------------------------------------------------------------------+
  309. |                                    |
  310. | name        cellPick_drawCell                    |
  311. |                                    |
  312. | author    BSI                    7/90        |
  313. |                                    |
  314. +----------------------------------------------------------------------*/
  315. Private int    cellPick_drawCell
  316. (
  317. DialogBox    *dbP,
  318. Rectangle    *displayRectP,
  319. MSElementDescr    *edP,
  320. Dvector3d    *cellRangeP,
  321. int         stdViewNum,
  322. int         threeD
  323. )
  324.     {
  325.     RotMatrix        rotMatrix;
  326.     Dpoint3d        cellExtent, origin;
  327.     Dvector3d        localRange = *cellRangeP;
  328.     Viewflags        viewflags;
  329.  
  330.     mdlView_getStandard (&rotMatrix, stdViewNum);
  331.     mdlRMatrix_rotateRange (&localRange.org, &localRange.end, &rotMatrix);
  332.  
  333.     memset (&viewflags, 0, sizeof(viewflags));
  334.     viewflags.patterns   = 1;
  335.     viewflags.ed_fields  = 1;
  336.     viewflags.on_off     = 1;
  337.     viewflags.points     = 1;
  338.     viewflags.constructs = 1;
  339.     viewflags.dimens     = 1;
  340.  
  341.     mdlVec_subtractPoint (&cellExtent, &localRange.end, &localRange.org);
  342.     mdlRMatrix_unrotatePoint (&localRange.org, &rotMatrix);
  343.  
  344.     return (mdlElmdscr_displayToWindow (dbP, displayRectP, &viewflags, edP,
  345.             &rotMatrix, &localRange.org, &cellExtent, threeD, -1));
  346.     }
  347.  
  348. /*----------------------------------------------------------------------+
  349. |                                    |
  350. | name        cellPick_showCell                    |
  351. |                                    |
  352. | author    BSI                    6/89        |
  353. |                                    |
  354. +----------------------------------------------------------------------*/
  355. Private void    cellPick_showCell
  356. (
  357. DialogBox    *dbP,
  358. char            *cellName,
  359. Rectangle    *cellRect,
  360. int         threeD
  361. )
  362.     {
  363.     Rectangle        displayRect;
  364.     Rectangle        quadRect;
  365.     Dvector3d        cellUorRange;
  366.     Point2d        center;
  367.     int            whichView;
  368.     MSElementDescr *cellDescrP;
  369.  
  370.     /* figure out the area available for our rectangle */
  371.     displayRect = *cellRect;
  372.     mdlDialog_rectInset (&displayRect, 3, 3);
  373.  
  374.     if (cellName == NULL || !mdlCell_existsInLibrary (cellName))
  375.     return;
  376.  
  377.     /* get the cell element */
  378.     if (mdlCell_getElmDscr (&cellDescrP, NULL, NULL, NULL, NULL,
  379.                 NULL, NULL, 0, FALSE, cellName))
  380.     return;
  381.  
  382.     mdlElmdscr_validate (cellDescrP, CELL_LIB);
  383.  
  384.     cellPick_getCellUorRange (&cellUorRange, &cellDescrP->el);
  385.  
  386.     if (threeD)
  387.     whichView = STDVIEW_ISO;
  388.     else
  389.     whichView = STDVIEW_TOP;
  390.  
  391.     cellPick_drawCell (dbP, &displayRect, cellDescrP, &cellUorRange,
  392.             whichView, threeD);
  393.  
  394.     /* free the element descriptor */
  395.     mdlElmdscr_freeAll (&cellDescrP);
  396.     }
  397.  
  398. /*----------------------------------------------------------------------+
  399. |                                    |
  400. | name        cellPick_drawButton                    |
  401. |                                    |
  402. | author    BSI                    6/90        |
  403. |                                    |
  404. +----------------------------------------------------------------------*/
  405. Private void cellPick_drawButton
  406. (
  407. RawItemHdr  *rihP,
  408. char        *cellName,
  409. int        eraseFirst
  410. )
  411.     {
  412.     DialogBox   *db = rihP->ownerDialogP;
  413.     Rectangle   *itemRectP = &rihP->diP->rect;
  414.  
  415.     if (eraseFirst)
  416.     mdlWindow_rectClear (db, itemRectP, NULL);
  417.  
  418.     mdlDialog_rectDrawBeveled (db, itemRectP,
  419.                    !rihP->highlightOn, TRUE);
  420.  
  421.     cellPick_showCell(db, cellName, itemRectP, tcb->fbfdcn.library3d);
  422.     }
  423.  
  424. /*----------------------------------------------------------------------+
  425. |                                    |
  426. | name        cellPick_cellButtonHook                    |
  427. |                                    |
  428. | author    BSI                    6/90        |
  429. |                                    |
  430. +----------------------------------------------------------------------*/
  431. Private void    cellPick_cellButtonHook
  432. (
  433. DialogItemMessage   *dimP
  434. )
  435.     {
  436.     DialogItem    *diP = dimP->dialogItemP;
  437.     RawItemHdr    *rihP = dimP->dialogItemP->rawItemP;
  438.     CellButtonInfo  *cellInfoP = rihP->userDataP;
  439.  
  440.     dimP->msgUnderstood = TRUE;
  441.  
  442.     switch (dimP->messageType)
  443.     {
  444.     case DITEM_MESSAGE_CREATE:
  445.         diP->attributes.acceptsKeystrokes = FALSE;
  446.         break;
  447.  
  448.     case DITEM_MESSAGE_INIT:
  449.         {
  450.         cellInfoP  = calloc (1, sizeof (CellButtonInfo));
  451.         if (NULL == cellInfoP)
  452.         {
  453.         dimP->u.init.initFailed = TRUE;
  454.         break;
  455.         }
  456.  
  457.         cellPick_initCellInfo (rihP->labelP, cellInfoP->libName,
  458.                cellInfoP->cellName, &cellInfoP->libAvailable);
  459.  
  460.         rihP->userDataP = cellInfoP;
  461.         }
  462.         break;
  463.  
  464.     case DITEM_MESSAGE_DRAW:
  465.  
  466.         if (SUCCESS == 
  467.             cellPick_loadLibraryIfNeeded (cellInfoP->libName, 
  468.                     &cellInfoP->libAvailable))
  469.             {
  470.         if (TRUE == cellInfoP->libAvailable)
  471.             {
  472.             
  473.             /* Set the text of the label displaying the name of the cell,
  474.                 this label follows immediately after the cell button
  475.                 in the dialog definition in the .r file */
  476.             mdlDialog_itemSetLabel (dimP->db, dimP->itemIndex+1,
  477.                     cellInfoP->cellName);
  478.  
  479.             cellPick_drawButton (rihP, cellInfoP->cellName,
  480.                     dimP->u.draw.eraseFirst);
  481.             }
  482.         else
  483.             {
  484.             mdlDialog_itemSetLabel (dimP->db, dimP->itemIndex+1, "");
  485.             }
  486.  
  487.             }
  488.  
  489.         break;
  490.  
  491.     case DITEM_MESSAGE_HIGHLIGHT:
  492.  
  493.         /* If the cell library is not available then break */
  494.         if (FALSE == cellInfoP->libAvailable)
  495.             break;
  496.  
  497.         mdlDialog_rectDrawBeveled (dimP->db, &diP->rect,
  498.                     !dimP->u.highlight.highlightOn, TRUE);
  499.         break;
  500.  
  501.     case DITEM_MESSAGE_BUTTON:
  502.         if ((dimP->u.button.buttonNumber != DATAPNT) ||
  503.         (dimP->u.button.buttonTrans != BUTTONTRANS_UP))
  504.         break;
  505.  
  506.         /* If the cell library is not available then break */
  507.         if (FALSE == cellInfoP->libAvailable)
  508.             break;
  509.  
  510.         /* If the user lets up on the mouse button, make the button look
  511.         like it popped back up... */
  512.         mdlDialog_rectDrawBeveled (dimP->db, &diP->rect,
  513.                     TRUE, TRUE);
  514.  
  515.         /* make sure the correct library is attached.*/
  516.         if (SUCCESS == cellPick_loadLibraryIfNeeded (cellInfoP->libName,
  517.                     &cellInfoP->libAvailable))
  518.             {
  519.         if (TRUE == cellInfoP->libAvailable)
  520.             /* and fire off a command to change the active cell. */
  521.             mdlDialog_cmdNumberQueue (FALSE, rihP->itemHookArg,
  522.                     cellInfoP->cellName, FALSE);
  523.             }
  524.         break;
  525.  
  526.     default:
  527.         dimP->msgUnderstood = FALSE;
  528.         break;
  529.         }
  530.     }
  531.  
  532. /*----------------------------------------------------------------------+
  533. |                                    |
  534. | name        cellPick_reloadFunction                    |
  535. |                                    |
  536. | author    BSI                    9/90        |
  537. |                                    |
  538. +----------------------------------------------------------------------*/
  539. Private void cellPick_reloadFunction 
  540. (
  541. void
  542. )
  543.     {
  544.     /* Open the dialog */
  545.     mdlDialog_open (NULL, DIALOGID_CellPicker);
  546.     }
  547.  
  548. /*----------------------------------------------------------------------+
  549. |                                    |
  550. | name        main                            |
  551. |                                    |
  552. | author    BSI                     8/89        |
  553. |                                    |
  554. +----------------------------------------------------------------------*/
  555. static DialogHookInfo uHooks[] =
  556.     {
  557.     {HOOKITEMID_CellButton,        cellPick_cellButtonHook},
  558.     };
  559.  
  560. Public int  main
  561. (
  562. int        argc,
  563. char        *argv[]
  564. )
  565.     {
  566.     char        *setP;
  567.     RscFileHandle   rfHandle;
  568.  
  569.     /* Open our file for access to dialog */
  570.     mdlResource_openFile (&rfHandle, NULL, RSC_READ);
  571.  
  572.     /* Publish the dialog item hooks */
  573.     mdlDialog_hookPublish (sizeof(uHooks)/sizeof(DialogHookInfo), uHooks);
  574.  
  575.     /* If user tries to load the application when it's already loaded,
  576.     bring the dialog back up. */
  577.     mdlSystem_setFunction (SYSTEM_RELOAD_PROGRAM, cellPick_reloadFunction);
  578.  
  579.     cellPick_reloadFunction();
  580.  
  581.     return SUCCESS;
  582.     }
  583.  
  584.