LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/dgn - dgnhelp.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 69 468 14.7 %
Date: 2024-11-25 13:07:18 Functions: 4 11 36.4 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  Microstation DGN Access Library
       4             :  * Purpose:  Application visible helper functions for parsing DGN information.
       5             :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2002, Avenza Systems Inc, http://www.avenza.com/
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #include "dgnlibp.h"
      14             : 
      15             : static const unsigned char abyDefaultPCT[256][3] = {
      16             :     {255, 255, 255}, {0, 0, 255},     {0, 255, 0},     {255, 0, 0},
      17             :     {255, 255, 0},   {255, 0, 255},   {255, 127, 0},   {0, 255, 255},
      18             :     {64, 64, 64},    {192, 192, 192}, {254, 0, 96},    {160, 224, 0},
      19             :     {0, 254, 160},   {128, 0, 160},   {176, 176, 176}, {0, 240, 240},
      20             :     {240, 240, 240}, {0, 0, 240},     {0, 240, 0},     {240, 0, 0},
      21             :     {240, 240, 0},   {240, 0, 240},   {240, 122, 0},   {0, 240, 240},
      22             :     {240, 240, 240}, {0, 0, 240},     {0, 240, 0},     {240, 0, 0},
      23             :     {240, 240, 0},   {240, 0, 240},   {240, 122, 0},   {0, 225, 225},
      24             :     {225, 225, 225}, {0, 0, 225},     {0, 225, 0},     {225, 0, 0},
      25             :     {225, 225, 0},   {225, 0, 225},   {225, 117, 0},   {0, 225, 225},
      26             :     {225, 225, 225}, {0, 0, 225},     {0, 225, 0},     {225, 0, 0},
      27             :     {225, 225, 0},   {225, 0, 225},   {225, 117, 0},   {0, 210, 210},
      28             :     {210, 210, 210}, {0, 0, 210},     {0, 210, 0},     {210, 0, 0},
      29             :     {210, 210, 0},   {210, 0, 210},   {210, 112, 0},   {0, 210, 210},
      30             :     {210, 210, 210}, {0, 0, 210},     {0, 210, 0},     {210, 0, 0},
      31             :     {210, 210, 0},   {210, 0, 210},   {210, 112, 0},   {0, 195, 195},
      32             :     {195, 195, 195}, {0, 0, 195},     {0, 195, 0},     {195, 0, 0},
      33             :     {195, 195, 0},   {195, 0, 195},   {195, 107, 0},   {0, 195, 195},
      34             :     {195, 195, 195}, {0, 0, 195},     {0, 195, 0},     {195, 0, 0},
      35             :     {195, 195, 0},   {195, 0, 195},   {195, 107, 0},   {0, 180, 180},
      36             :     {180, 180, 180}, {0, 0, 180},     {0, 180, 0},     {180, 0, 0},
      37             :     {180, 180, 0},   {180, 0, 180},   {180, 102, 0},   {0, 180, 180},
      38             :     {180, 180, 180}, {0, 0, 180},     {0, 180, 0},     {180, 0, 0},
      39             :     {180, 180, 0},   {180, 0, 180},   {180, 102, 0},   {0, 165, 165},
      40             :     {165, 165, 165}, {0, 0, 165},     {0, 165, 0},     {165, 0, 0},
      41             :     {165, 165, 0},   {165, 0, 165},   {165, 97, 0},    {0, 165, 165},
      42             :     {165, 165, 165}, {0, 0, 165},     {0, 165, 0},     {165, 0, 0},
      43             :     {165, 165, 0},   {165, 0, 165},   {165, 97, 0},    {0, 150, 150},
      44             :     {150, 150, 150}, {0, 0, 150},     {0, 150, 0},     {150, 0, 0},
      45             :     {150, 150, 0},   {150, 0, 150},   {150, 92, 0},    {0, 150, 150},
      46             :     {150, 150, 150}, {0, 0, 150},     {0, 150, 0},     {150, 0, 0},
      47             :     {150, 150, 0},   {150, 0, 150},   {150, 92, 0},    {0, 135, 135},
      48             :     {135, 135, 135}, {0, 0, 135},     {0, 135, 0},     {135, 0, 0},
      49             :     {135, 135, 0},   {135, 0, 135},   {135, 87, 0},    {0, 135, 135},
      50             :     {135, 135, 135}, {0, 0, 135},     {0, 135, 0},     {135, 0, 0},
      51             :     {135, 135, 0},   {135, 0, 135},   {135, 87, 0},    {0, 120, 120},
      52             :     {120, 120, 120}, {0, 0, 120},     {0, 120, 0},     {120, 0, 0},
      53             :     {120, 120, 0},   {120, 0, 120},   {120, 82, 0},    {0, 120, 120},
      54             :     {120, 120, 120}, {0, 0, 120},     {0, 120, 0},     {120, 0, 0},
      55             :     {120, 120, 0},   {120, 0, 120},   {120, 82, 0},    {0, 105, 105},
      56             :     {105, 105, 105}, {0, 0, 105},     {0, 105, 0},     {105, 0, 0},
      57             :     {105, 105, 0},   {105, 0, 105},   {105, 77, 0},    {0, 105, 105},
      58             :     {105, 105, 105}, {0, 0, 105},     {0, 105, 0},     {105, 0, 0},
      59             :     {105, 105, 0},   {105, 0, 105},   {105, 77, 0},    {0, 90, 90},
      60             :     {90, 90, 90},    {0, 0, 90},      {0, 90, 0},      {90, 0, 0},
      61             :     {90, 90, 0},     {90, 0, 90},     {90, 72, 0},     {0, 90, 90},
      62             :     {90, 90, 90},    {0, 0, 90},      {0, 90, 0},      {90, 0, 0},
      63             :     {90, 90, 0},     {90, 0, 90},     {90, 72, 0},     {0, 75, 75},
      64             :     {75, 75, 75},    {0, 0, 75},      {0, 75, 0},      {75, 0, 0},
      65             :     {75, 75, 0},     {75, 0, 75},     {75, 67, 0},     {0, 75, 75},
      66             :     {75, 75, 75},    {0, 0, 75},      {0, 75, 0},      {75, 0, 0},
      67             :     {75, 75, 0},     {75, 0, 75},     {75, 67, 0},     {0, 60, 60},
      68             :     {60, 60, 60},    {0, 0, 60},      {0, 60, 0},      {60, 0, 0},
      69             :     {60, 60, 0},     {60, 0, 60},     {60, 62, 0},     {0, 60, 60},
      70             :     {60, 60, 60},    {0, 0, 60},      {0, 60, 0},      {60, 0, 0},
      71             :     {60, 60, 0},     {60, 0, 60},     {60, 62, 0},     {0, 45, 45},
      72             :     {45, 45, 45},    {0, 0, 45},      {0, 45, 0},      {45, 0, 0},
      73             :     {45, 45, 0},     {45, 0, 45},     {45, 57, 0},     {0, 45, 45},
      74             :     {45, 45, 45},    {0, 0, 45},      {0, 45, 0},      {45, 0, 0},
      75             :     {45, 45, 0},     {45, 0, 45},     {45, 57, 0},     {0, 30, 30},
      76             :     {30, 30, 30},    {0, 0, 30},      {0, 30, 0},      {30, 0, 0},
      77             :     {30, 30, 0},     {30, 0, 30},     {30, 52, 0},     {0, 30, 30},
      78             :     {30, 30, 30},    {0, 0, 30},      {0, 30, 0},      {30, 0, 0},
      79             :     {30, 30, 0},     {30, 0, 30},     {192, 192, 192}, {28, 0, 100}};
      80             : 
      81             : /************************************************************************/
      82             : /*                           DGNLookupColor()                           */
      83             : /************************************************************************/
      84             : 
      85             : /**
      86             :  * Translate color index into RGB values.
      87             :  *
      88             :  * If no color table has yet been encountered in the file a hard-coded
      89             :  * "default" color table will be used.  This seems to be what Microstation
      90             :  * uses as a color table when there isn't one in a DGN file but I am not
      91             :  * absolutely convinced it is appropriate.
      92             :  *
      93             :  * @param hDGN the file.
      94             :  * @param color_index the color index to lookup.
      95             :  * @param red location to put red component.
      96             :  * @param green location to put green component.
      97             :  * @param blue location to put blue component.
      98             :  *
      99             :  * @return TRUE on success or FALSE on failure.  May fail if color_index is
     100             :  * out of range.
     101             :  */
     102             : 
     103         288 : int DGNLookupColor(DGNHandle hDGN, int color_index, int *red, int *green,
     104             :                    int *blue)
     105             : 
     106             : {
     107         288 :     if (color_index < 0 || color_index > 255)
     108           0 :         return FALSE;
     109             : 
     110         288 :     DGNInfo *psDGN = (DGNInfo *)hDGN;
     111             : 
     112         288 :     if (!psDGN->got_color_table)
     113             :     {
     114         288 :         *red = abyDefaultPCT[color_index][0];
     115         288 :         *green = abyDefaultPCT[color_index][1];
     116         288 :         *blue = abyDefaultPCT[color_index][2];
     117             :     }
     118             :     else
     119             :     {
     120           0 :         *red = psDGN->color_table[color_index][0];
     121           0 :         *green = psDGN->color_table[color_index][1];
     122           0 :         *blue = psDGN->color_table[color_index][2];
     123             :     }
     124             : 
     125         288 :     return TRUE;
     126             : }
     127             : 
     128             : /************************************************************************/
     129             : /*                        DGNGetShapeFillInfo()                         */
     130             : /************************************************************************/
     131             : 
     132             : /**
     133             :  * Fetch fill color for a shape.
     134             :  *
     135             :  * This method will check for a 0x0041 user attribute linkaged with fill
     136             :  * color information for the element.  If found the function returns TRUE,
     137             :  * and places the fill color in *pnColor, otherwise FALSE is returned and
     138             :  * *pnColor is not updated.
     139             :  *
     140             :  * @param hDGN the file.
     141             :  * @param psElem the element.
     142             :  * @param pnColor the location to return the fill color.
     143             :  *
     144             :  * @return TRUE on success or FALSE on failure.
     145             :  */
     146             : 
     147          10 : int DGNGetShapeFillInfo(DGNHandle hDGN, DGNElemCore *psElem, int *pnColor)
     148             : 
     149             : {
     150          10 :     for (int iLink = 0; true; iLink++)
     151             :     {
     152          17 :         int nLinkType = 0;
     153          17 :         int nLinkSize = 0;
     154          17 :         unsigned char *pabyData = DGNGetLinkage(hDGN, psElem, iLink, &nLinkType,
     155             :                                                 nullptr, nullptr, &nLinkSize);
     156          17 :         if (pabyData == nullptr)
     157          10 :             return FALSE;
     158             : 
     159          11 :         if (nLinkType == DGNLT_SHAPE_FILL && nLinkSize >= 9)
     160             :         {
     161           4 :             *pnColor = pabyData[8];
     162           4 :             return TRUE;
     163             :         }
     164           7 :     }
     165             : }
     166             : 
     167             : /************************************************************************/
     168             : /*                        DGNGetAssocID()                               */
     169             : /************************************************************************/
     170             : 
     171             : /**
     172             :  * Fetch association id for an element.
     173             :  *
     174             :  * This method will check if an element has an association id, and if so
     175             :  * returns it, otherwise returning -1.  Association ids are kept as a
     176             :  * user attribute linkage where present.
     177             :  *
     178             :  * @param hDGN the file.
     179             :  * @param psElem the element.
     180             :  *
     181             :  * @return The id or -1 on failure.
     182             :  */
     183             : 
     184           0 : int DGNGetAssocID(DGNHandle hDGN, DGNElemCore *psElem)
     185             : 
     186             : {
     187           0 :     for (int iLink = 0; true; iLink++)
     188             :     {
     189           0 :         int nLinkType = 0;
     190           0 :         int nLinkSize = 0;
     191           0 :         unsigned char *pabyData = DGNGetLinkage(hDGN, psElem, iLink, &nLinkType,
     192             :                                                 nullptr, nullptr, &nLinkSize);
     193           0 :         if (pabyData == nullptr)
     194           0 :             return -1;
     195             : 
     196           0 :         if (nLinkType == DGNLT_ASSOC_ID && nLinkSize >= 8)
     197             :         {
     198           0 :             return pabyData[4] + pabyData[5] * 256 + pabyData[6] * 256 * 256 +
     199           0 :                    pabyData[7] * 256 * 256 * 256;
     200             :         }
     201           0 :     }
     202             : }
     203             : 
     204             : /************************************************************************/
     205             : /*                          DGNRad50ToAscii()                           */
     206             : /*                                                                      */
     207             : /*      Convert one 16-bits Radix-50 to ASCII (3 chars).                */
     208             : /************************************************************************/
     209             : 
     210           0 : void DGNRad50ToAscii(unsigned short sRad50, char *str)
     211             : {
     212           0 :     char ch = '\0';
     213           0 :     unsigned short saQuots[3] = {1600, 40, 1};
     214             : 
     215           0 :     for (int i = 0; i < 3; i++)
     216             :     {
     217           0 :         unsigned short sValue = sRad50;
     218           0 :         sValue /= saQuots[i];
     219             :         /* Map 0..39 to ASCII */
     220           0 :         if (sValue == 0)
     221           0 :             ch = ' '; /* space */
     222           0 :         else if (/*sValue >= 1 &&*/ sValue <= 26)
     223           0 :             ch = (char)(sValue - 1 + 'A'); /* printable alpha A..Z */
     224           0 :         else if (sValue == 27)
     225           0 :             ch = '$'; /* dollar */
     226           0 :         else if (sValue == 28)
     227           0 :             ch = '.'; /* period */
     228           0 :         else if (sValue == 29)
     229           0 :             ch = ' '; /* unused char, emit a space instead */
     230           0 :         else if (/*sValue >= 30 &&*/ sValue <= 39)
     231           0 :             ch = (char)(sValue - 30 + '0'); /* digit 0..9 */
     232           0 :         *str = ch;
     233           0 :         str++;
     234             : 
     235           0 :         sRad50 -= (sValue * saQuots[i]);
     236             :     }
     237             : 
     238             :     /* Do zero-terminate */
     239           0 :     *str = '\0';
     240           0 : }
     241             : 
     242             : /************************************************************************/
     243             : /*                          DGNAsciiToRad50()                           */
     244             : /************************************************************************/
     245             : 
     246           0 : void DGNAsciiToRad50(const char *str, unsigned short *pRad50)
     247             : 
     248             : {
     249           0 :     unsigned short rad50 = 0;
     250             : 
     251           0 :     for (int i = 0; i < 3; i++)
     252             :     {
     253           0 :         if (i >= (int)strlen(str))
     254             :         {
     255           0 :             rad50 = rad50 * 40;
     256           0 :             continue;
     257             :         }
     258             : 
     259           0 :         unsigned short value = 0;
     260             : 
     261           0 :         if (str[i] == '$')
     262           0 :             value = 27;
     263           0 :         else if (str[i] == '.')
     264           0 :             value = 28;
     265           0 :         else if (str[i] == ' ')
     266           0 :             value = 29;
     267           0 :         else if (str[i] >= '0' && str[i] <= '9')
     268           0 :             value = str[i] - '0' + 30;
     269           0 :         else if (str[i] >= 'a' && str[i] <= 'z')
     270           0 :             value = str[i] - 'a' + 1;
     271           0 :         else if (str[i] >= 'A' && str[i] <= 'Z')
     272           0 :             value = str[i] - 'A' + 1;
     273             :         else
     274           0 :             value = 0;
     275             : 
     276           0 :         rad50 = rad50 * 40 + value;
     277             :     }
     278             : 
     279           0 :     *pRad50 = rad50;
     280           0 : }
     281             : 
     282             : /************************************************************************/
     283             : /*                        DGNGetLineStyleName()                         */
     284             : /*                                                                      */
     285             : /*      Read the line style name from symbol table.                     */
     286             : /*      The got name is stored in psLine.                               */
     287             : /************************************************************************/
     288             : #ifdef unused
     289             : int DGNGetLineStyleName(CPL_UNUSED DGNInfo *psDGN, DGNElemMultiPoint *psLine,
     290             :                         char szLineStyle[65])
     291             : {
     292             :     if (psLine->core.attr_bytes > 0 && psLine->core.attr_data[1] == 0x10 &&
     293             :         psLine->core.attr_data[2] == 0xf9 && psLine->core.attr_data[3] == 0x79)
     294             :     {
     295             : #ifdef notdef
     296             :         for (int i = 0; i < SYMBOL_TABLE_SIZE; i++)
     297             :         {
     298             :             if (*((unsigned char *)psDGN->buffer + 0x21e5 + i) ==
     299             :                     psLine->core.attr_data[4] &&
     300             :                 *((unsigned char *)psDGN->buffer + 0x21e6 + i) ==
     301             :                     psLine->core.attr_data[5] &&
     302             :                 *((unsigned char *)psDGN->buffer + 0x21e7 + i) ==
     303             :                     psLine->core.attr_data[6] &&
     304             :                 *((unsigned char *)psDGN->buffer + 0x21e8 + i) ==
     305             :                     psLine->core.attr_data[7])
     306             :             {
     307             :                 memcpy(szLineStyle, (unsigned char *)psDGN->buffer + 0x21e9 + i,
     308             :                        64);
     309             :                 szLineStyle[64] = '\0';
     310             :                 return TRUE;
     311             :             }
     312             :         }
     313             : #endif
     314             :         return FALSE;
     315             :     }
     316             :     else
     317             :     {
     318             :         szLineStyle[0] = '\0';
     319             :         return FALSE;
     320             :     }
     321             : }
     322             : #endif
     323             : 
     324             : /************************************************************************/
     325             : /*                           DGNDumpElement()                           */
     326             : /************************************************************************/
     327             : 
     328             : /**
     329             :  * Emit textual report of an element.
     330             :  *
     331             :  * This function exists primarily for debugging, and will produce a textual
     332             :  * report about any element type to the designated file.
     333             :  *
     334             :  * @param hDGN the file from which the element originated.
     335             :  * @param psElement the element to report on.
     336             :  * @param fp the file (such as stdout) to report the element information to.
     337             :  */
     338             : 
     339           0 : void DGNDumpElement(DGNHandle hDGN, DGNElemCore *psElement, FILE *fp)
     340             : 
     341             : {
     342           0 :     DGNInfo *psInfo = (DGNInfo *)hDGN;
     343             : 
     344           0 :     fprintf(fp, "\n");
     345           0 :     fprintf(fp, "Element:%-12s Level:%2d id:%-6d ",
     346             :             DGNTypeToName(psElement->type), psElement->level,
     347             :             psElement->element_id);
     348             : 
     349           0 :     if (psElement->complex)
     350           0 :         fprintf(fp, "(Complex) ");
     351             : 
     352           0 :     if (psElement->deleted)
     353           0 :         fprintf(fp, "(DELETED) ");
     354             : 
     355           0 :     fprintf(fp, "\n");
     356             : 
     357           0 :     fprintf(fp, "  offset=%d  size=%d bytes\n", psElement->offset,
     358             :             psElement->size);
     359             : 
     360           0 :     fprintf(fp, "  graphic_group:%-3d color:%d weight:%d style:%d\n",
     361             :             psElement->graphic_group, psElement->color, psElement->weight,
     362             :             psElement->style);
     363             : 
     364           0 :     if (psElement->properties != 0)
     365             :     {
     366           0 :         fprintf(fp, "  properties=%d", psElement->properties);
     367           0 :         if (psElement->properties & DGNPF_HOLE)
     368           0 :             fprintf(fp, ",HOLE");
     369           0 :         if (psElement->properties & DGNPF_SNAPPABLE)
     370           0 :             fprintf(fp, ",SNAPPABLE");
     371           0 :         if (psElement->properties & DGNPF_PLANAR)
     372           0 :             fprintf(fp, ",PLANAR");
     373           0 :         if (psElement->properties & DGNPF_ORIENTATION)
     374           0 :             fprintf(fp, ",ORIENTATION");
     375           0 :         if (psElement->properties & DGNPF_ATTRIBUTES)
     376           0 :             fprintf(fp, ",ATTRIBUTES");
     377           0 :         if (psElement->properties & DGNPF_MODIFIED)
     378           0 :             fprintf(fp, ",MODIFIED");
     379           0 :         if (psElement->properties & DGNPF_NEW)
     380           0 :             fprintf(fp, ",NEW");
     381           0 :         if (psElement->properties & DGNPF_LOCKED)
     382           0 :             fprintf(fp, ",LOCKED");
     383             : 
     384           0 :         int nClass = psElement->properties & DGNPF_CLASS;
     385           0 :         if (nClass == DGNC_PATTERN_COMPONENT)
     386           0 :             fprintf(fp, ",PATTERN_COMPONENT");
     387           0 :         else if (nClass == DGNC_CONSTRUCTION_ELEMENT)
     388           0 :             fprintf(fp, ",CONSTRUCTION ELEMENT");
     389           0 :         else if (nClass == DGNC_DIMENSION_ELEMENT)
     390           0 :             fprintf(fp, ",DIMENSION ELEMENT");
     391           0 :         else if (nClass == DGNC_PRIMARY_RULE_ELEMENT)
     392           0 :             fprintf(fp, ",PRIMARY RULE ELEMENT");
     393           0 :         else if (nClass == DGNC_LINEAR_PATTERNED_ELEMENT)
     394           0 :             fprintf(fp, ",LINEAR PATTERNED ELEMENT");
     395           0 :         else if (nClass == DGNC_CONSTRUCTION_RULE_ELEMENT)
     396           0 :             fprintf(fp, ",CONSTRUCTION_RULE_ELEMENT");
     397             : 
     398           0 :         fprintf(fp, "\n");
     399             :     }
     400             : 
     401           0 :     switch (psElement->stype)
     402             :     {
     403           0 :         case DGNST_MULTIPOINT:
     404             :         {
     405           0 :             DGNElemMultiPoint *psLine = (DGNElemMultiPoint *)psElement;
     406             : 
     407           0 :             for (int i = 0; i < psLine->num_vertices; i++)
     408           0 :                 fprintf(fp, "  (%.6f,%.6f,%.6f)\n", psLine->vertices[i].x,
     409             :                         psLine->vertices[i].y, psLine->vertices[i].z);
     410             :         }
     411           0 :         break;
     412             : 
     413           0 :         case DGNST_CELL_HEADER:
     414             :         {
     415           0 :             DGNElemCellHeader *psCell = (DGNElemCellHeader *)psElement;
     416             : 
     417           0 :             fprintf(
     418             :                 fp,
     419             :                 "  totlength=%d, name=%s, class=%x, levels=%02x%02x%02x%02x\n",
     420           0 :                 psCell->totlength, psCell->name, psCell->cclass,
     421           0 :                 psCell->levels[0], psCell->levels[1], psCell->levels[2],
     422           0 :                 psCell->levels[3]);
     423           0 :             fprintf(fp,
     424             :                     "  rnglow=(%.5f,%.5f,%.5f)\n"
     425             :                     "  rnghigh=(%.5f,%.5f,%.5f)\n",
     426             :                     psCell->rnglow.x, psCell->rnglow.y, psCell->rnglow.z,
     427             :                     psCell->rnghigh.x, psCell->rnghigh.y, psCell->rnghigh.z);
     428           0 :             fprintf(fp, "  origin=(%.5f,%.5f,%.5f)\n", psCell->origin.x,
     429             :                     psCell->origin.y, psCell->origin.z);
     430             : 
     431           0 :             if (psInfo->dimension == 2)
     432           0 :                 fprintf(fp, "  xscale=%g, yscale=%g, rotation=%g\n",
     433             :                         psCell->xscale, psCell->yscale, psCell->rotation);
     434             :             else
     435           0 :                 fprintf(fp, "  trans=%g,%g,%g,%g,%g,%g,%g,%g,%g\n",
     436             :                         psCell->trans[0], psCell->trans[1], psCell->trans[2],
     437             :                         psCell->trans[3], psCell->trans[4], psCell->trans[5],
     438             :                         psCell->trans[6], psCell->trans[7], psCell->trans[8]);
     439             :         }
     440           0 :         break;
     441             : 
     442           0 :         case DGNST_CELL_LIBRARY:
     443             :         {
     444           0 :             DGNElemCellLibrary *psCell = (DGNElemCellLibrary *)psElement;
     445             : 
     446           0 :             fprintf(
     447             :                 fp,
     448             :                 "  name=%s, class=%x, levels=%02x%02x%02x%02x, numwords=%d\n",
     449           0 :                 psCell->name, psCell->cclass, psCell->levels[0],
     450           0 :                 psCell->levels[1], psCell->levels[2], psCell->levels[3],
     451             :                 psCell->numwords);
     452           0 :             fprintf(fp, "  dispsymb=%d, description=%s\n", psCell->dispsymb,
     453           0 :                     psCell->description);
     454             :         }
     455           0 :         break;
     456             : 
     457           0 :         case DGNST_SHARED_CELL_DEFN:
     458             :         {
     459           0 :             DGNElemSharedCellDefn *psShared =
     460             :                 (DGNElemSharedCellDefn *)psElement;
     461             : 
     462           0 :             fprintf(fp, "  totlength=%d\n", psShared->totlength);
     463             :         }
     464           0 :         break;
     465             : 
     466           0 :         case DGNST_ARC:
     467             :         {
     468           0 :             DGNElemArc *psArc = (DGNElemArc *)psElement;
     469             : 
     470           0 :             if (psInfo->dimension == 2)
     471           0 :                 fprintf(fp, "  origin=(%.5f,%.5f), rotation=%f\n",
     472             :                         psArc->origin.x, psArc->origin.y, psArc->rotation);
     473             :             else
     474           0 :                 fprintf(fp, "  origin=(%.5f,%.5f,%.5f), quat=%d,%d,%d,%d\n",
     475             :                         psArc->origin.x, psArc->origin.y, psArc->origin.z,
     476             :                         psArc->quat[0], psArc->quat[1], psArc->quat[2],
     477             :                         psArc->quat[3]);
     478           0 :             fprintf(fp, "  axes=(%.5f,%.5f), start angle=%f, sweep=%f\n",
     479             :                     psArc->primary_axis, psArc->secondary_axis, psArc->startang,
     480             :                     psArc->sweepang);
     481             :         }
     482           0 :         break;
     483             : 
     484           0 :         case DGNST_TEXT:
     485             :         {
     486           0 :             DGNElemText *psText = (DGNElemText *)psElement;
     487             : 
     488           0 :             fprintf(fp,
     489             :                     "  origin=(%.5f,%.5f), rotation=%f\n"
     490             :                     "  font=%d, just=%d, length_mult=%g, height_mult=%g\n"
     491             :                     "  string = \"%s\"\n",
     492             :                     psText->origin.x, psText->origin.y, psText->rotation,
     493             :                     psText->font_id, psText->justification, psText->length_mult,
     494           0 :                     psText->height_mult, psText->string);
     495             :         }
     496           0 :         break;
     497             : 
     498           0 :         case DGNST_TEXT_NODE:
     499             :         {
     500           0 :             DGNElemTextNode *psNode = (DGNElemTextNode *)psElement;
     501             : 
     502           0 :             fprintf(fp, "  totlength=%d, num_texts=%d\n", psNode->totlength,
     503             :                     psNode->numelems);
     504           0 :             fprintf(fp,
     505             :                     "  origin=(%.5f,%.5f), rotation=%f\n"
     506             :                     "  font=%d, just=%d, length_mult=%g, height_mult=%g\n",
     507             :                     psNode->origin.x, psNode->origin.y, psNode->rotation,
     508           0 :                     psNode->font_id, psNode->justification, psNode->length_mult,
     509             :                     psNode->height_mult);
     510           0 :             fprintf(fp, "  max_length=%d, used=%d,", psNode->max_length,
     511           0 :                     psNode->max_used);
     512           0 :             fprintf(fp, "  node_number=%d\n", psNode->node_number);
     513             :         }
     514           0 :         break;
     515             : 
     516           0 :         case DGNST_COMPLEX_HEADER:
     517             :         {
     518           0 :             DGNElemComplexHeader *psHdr = (DGNElemComplexHeader *)psElement;
     519             : 
     520           0 :             fprintf(fp, "  totlength=%d, numelems=%d\n", psHdr->totlength,
     521             :                     psHdr->numelems);
     522           0 :             if (psElement->type == DGNT_3DSOLID_HEADER ||
     523           0 :                 psElement->type == DGNT_3DSURFACE_HEADER)
     524             :             {
     525           0 :                 fprintf(fp, "  surftype=%d, boundelms=%d\n", psHdr->surftype,
     526             :                         psHdr->boundelms);
     527             :             }
     528             :         }
     529           0 :         break;
     530             : 
     531           0 :         case DGNST_COLORTABLE:
     532             :         {
     533           0 :             DGNElemColorTable *psCT = (DGNElemColorTable *)psElement;
     534             : 
     535           0 :             fprintf(fp, "  screen_flag: %d\n", psCT->screen_flag);
     536           0 :             for (int i = 0; i < 256; i++)
     537             :             {
     538           0 :                 fprintf(fp, "  %3d: (%3u,%3u,%3u)\n", i, psCT->color_info[i][0],
     539           0 :                         psCT->color_info[i][1], psCT->color_info[i][2]);
     540             :             }
     541             :         }
     542           0 :         break;
     543             : 
     544           0 :         case DGNST_TCB:
     545             :         {
     546           0 :             DGNElemTCB *psTCB = (DGNElemTCB *)psElement;
     547             : 
     548           0 :             fprintf(fp, "  dimension = %d\n", psTCB->dimension);
     549           0 :             fprintf(fp, "  uor_per_subunit = %ld, subunits = `%s'\n",
     550           0 :                     psTCB->uor_per_subunit, psTCB->sub_units);
     551           0 :             fprintf(fp, "  subunits_per_master = %ld, master units = `%s'\n",
     552           0 :                     psTCB->subunits_per_master, psTCB->master_units);
     553           0 :             fprintf(fp, "  origin = (%.5f,%.5f,%.5f)\n", psTCB->origin_x,
     554             :                     psTCB->origin_y, psTCB->origin_z);
     555             : 
     556           0 :             for (int iView = 0; iView < 8; iView++)
     557             :             {
     558           0 :                 DGNViewInfo *psView = psTCB->views + iView;
     559             : 
     560           0 :                 fprintf(fp,
     561             :                         "  View%d: flags=%04X, "
     562             :                         "levels=%02X%02X%02X%02X%02X%02X%02X%02X\n",
     563           0 :                         iView, psView->flags, psView->levels[0],
     564           0 :                         psView->levels[1], psView->levels[2], psView->levels[3],
     565           0 :                         psView->levels[4], psView->levels[5], psView->levels[6],
     566           0 :                         psView->levels[7]);
     567           0 :                 fprintf(fp,
     568             :                         "        origin=(%g,%g,%g)\n        delta=(%g,%g,%g)\n",
     569             :                         psView->origin.x, psView->origin.y, psView->origin.z,
     570             :                         psView->delta.x, psView->delta.y, psView->delta.z);
     571           0 :                 fprintf(fp, "       trans=(%g,%g,%g,%g,%g,%g,%g,%g,%g)\n",
     572             :                         psView->transmatrx[0], psView->transmatrx[1],
     573             :                         psView->transmatrx[2], psView->transmatrx[3],
     574             :                         psView->transmatrx[4], psView->transmatrx[5],
     575             :                         psView->transmatrx[6], psView->transmatrx[7],
     576             :                         psView->transmatrx[8]);
     577             :             }
     578             :         }
     579           0 :         break;
     580             : 
     581           0 :         case DGNST_TAG_SET:
     582             :         {
     583           0 :             DGNElemTagSet *psTagSet = (DGNElemTagSet *)psElement;
     584             : 
     585           0 :             fprintf(fp, "  tagSetName=%s, tagSet=%d, tagCount=%d, flags=%d\n",
     586             :                     psTagSet->tagSetName, psTagSet->tagSet, psTagSet->tagCount,
     587             :                     psTagSet->flags);
     588           0 :             for (int iTag = 0; iTag < psTagSet->tagCount; iTag++)
     589             :             {
     590           0 :                 DGNTagDef *psTagDef = psTagSet->tagList + iTag;
     591             : 
     592           0 :                 fprintf(fp, "    %d: name=%s, type=%d, prompt=%s", psTagDef->id,
     593             :                         psTagDef->name, psTagDef->type, psTagDef->prompt);
     594           0 :                 if (psTagDef->type == 1)
     595           0 :                     fprintf(fp, ", default=%s\n",
     596             :                             psTagDef->defaultValue.string);
     597           0 :                 else if (psTagDef->type == 3 || psTagDef->type == 5)
     598           0 :                     fprintf(fp, ", default=%d\n",
     599             :                             psTagDef->defaultValue.integer);
     600           0 :                 else if (psTagDef->type == 4)
     601           0 :                     fprintf(fp, ", default=%g\n", psTagDef->defaultValue.real);
     602             :                 else
     603           0 :                     fprintf(fp, ", default=<unknown>\n");
     604             :             }
     605             :         }
     606           0 :         break;
     607             : 
     608           0 :         case DGNST_TAG_VALUE:
     609             :         {
     610           0 :             DGNElemTagValue *psTag = (DGNElemTagValue *)psElement;
     611             : 
     612           0 :             fprintf(fp, "  tagType=%d, tagSet=%d, tagIndex=%d, tagLength=%d\n",
     613             :                     psTag->tagType, psTag->tagSet, psTag->tagIndex,
     614             :                     psTag->tagLength);
     615           0 :             if (psTag->tagType == 1)
     616           0 :                 fprintf(fp, "  value=%s\n", psTag->tagValue.string);
     617           0 :             else if (psTag->tagType == 3)
     618           0 :                 fprintf(fp, "  value=%d\n", psTag->tagValue.integer);
     619           0 :             else if (psTag->tagType == 4)
     620           0 :                 fprintf(fp, "  value=%g\n", psTag->tagValue.real);
     621             :         }
     622           0 :         break;
     623             : 
     624           0 :         case DGNST_CONE:
     625             :         {
     626           0 :             DGNElemCone *psCone = (DGNElemCone *)psElement;
     627             : 
     628           0 :             fprintf(fp,
     629             :                     "  center_1=(%g,%g,%g) radius=%g\n"
     630             :                     "  center_2=(%g,%g,%g) radius=%g\n"
     631             :                     "  quat=%d,%d,%d,%d unknown=%d\n",
     632             :                     psCone->center_1.x, psCone->center_1.y, psCone->center_1.z,
     633             :                     psCone->radius_1, psCone->center_2.x, psCone->center_2.y,
     634             :                     psCone->center_2.z, psCone->radius_2, psCone->quat[0],
     635             :                     psCone->quat[1], psCone->quat[2], psCone->quat[3],
     636           0 :                     psCone->unknown);
     637             :         }
     638           0 :         break;
     639             : 
     640           0 :         case DGNST_BSPLINE_SURFACE_HEADER:
     641             :         {
     642           0 :             DGNElemBSplineSurfaceHeader *psSpline =
     643             :                 (DGNElemBSplineSurfaceHeader *)psElement;
     644             : 
     645           0 :             fprintf(fp, "  desc_words=%ld, curve type=%u\n",
     646           0 :                     psSpline->desc_words, psSpline->curve_type);
     647             : 
     648           0 :             fprintf(fp, "  U: properties=%02x", psSpline->u_properties);
     649           0 :             if (psSpline->u_properties != 0)
     650             :             {
     651           0 :                 if (psSpline->u_properties & DGNBSC_CURVE_DISPLAY)
     652             :                 {
     653           0 :                     fprintf(fp, ",CURVE_DISPLAY");
     654             :                 }
     655           0 :                 if (psSpline->u_properties & DGNBSC_POLY_DISPLAY)
     656             :                 {
     657           0 :                     fprintf(fp, ",POLY_DISPLAY");
     658             :                 }
     659           0 :                 if (psSpline->u_properties & DGNBSC_RATIONAL)
     660             :                 {
     661           0 :                     fprintf(fp, ",RATIONAL");
     662             :                 }
     663           0 :                 if (psSpline->u_properties & DGNBSC_CLOSED)
     664             :                 {
     665           0 :                     fprintf(fp, ",CLOSED");
     666             :                 }
     667             :             }
     668           0 :             fprintf(fp, "\n");
     669           0 :             fprintf(fp, "     order=%u\n  %d poles, %d knots, %d rule lines\n",
     670           0 :                     psSpline->u_order, psSpline->num_poles_u,
     671           0 :                     psSpline->num_knots_u, psSpline->rule_lines_u);
     672             : 
     673           0 :             fprintf(fp, "  V: properties=%02x", psSpline->v_properties);
     674           0 :             if (psSpline->v_properties != 0)
     675             :             {
     676           0 :                 if (psSpline->v_properties & DGNBSS_ARC_SPACING)
     677             :                 {
     678           0 :                     fprintf(fp, ",ARC_SPACING");
     679             :                 }
     680           0 :                 if (psSpline->v_properties & DGNBSS_CLOSED)
     681             :                 {
     682           0 :                     fprintf(fp, ",CLOSED");
     683             :                 }
     684             :             }
     685           0 :             fprintf(fp, "\n");
     686           0 :             fprintf(fp, "     order=%u\n  %d poles, %d knots, %d rule lines\n",
     687           0 :                     psSpline->v_order, psSpline->num_poles_v,
     688           0 :                     psSpline->num_knots_v, psSpline->rule_lines_v);
     689             :         }
     690           0 :         break;
     691             : 
     692           0 :         case DGNST_BSPLINE_CURVE_HEADER:
     693             :         {
     694           0 :             DGNElemBSplineCurveHeader *psSpline =
     695             :                 (DGNElemBSplineCurveHeader *)psElement;
     696             : 
     697           0 :             fprintf(fp,
     698             :                     "  desc_words=%ld, curve type=%u\n"
     699             :                     "  properties=%02x",
     700           0 :                     psSpline->desc_words, psSpline->curve_type,
     701           0 :                     psSpline->properties);
     702           0 :             if (psSpline->properties != 0)
     703             :             {
     704           0 :                 if (psSpline->properties & DGNBSC_CURVE_DISPLAY)
     705             :                 {
     706           0 :                     fprintf(fp, ",CURVE_DISPLAY");
     707             :                 }
     708           0 :                 if (psSpline->properties & DGNBSC_POLY_DISPLAY)
     709             :                 {
     710           0 :                     fprintf(fp, ",POLY_DISPLAY");
     711             :                 }
     712           0 :                 if (psSpline->properties & DGNBSC_RATIONAL)
     713             :                 {
     714           0 :                     fprintf(fp, ",RATIONAL");
     715             :                 }
     716           0 :                 if (psSpline->properties & DGNBSC_CLOSED)
     717             :                 {
     718           0 :                     fprintf(fp, ",CLOSED");
     719             :                 }
     720             :             }
     721           0 :             fprintf(fp, "\n");
     722           0 :             fprintf(fp, "  order=%u\n  %d poles, %d knots\n", psSpline->order,
     723           0 :                     psSpline->num_poles, psSpline->num_knots);
     724             :         }
     725           0 :         break;
     726             : 
     727           0 :         case DGNST_BSPLINE_SURFACE_BOUNDARY:
     728             :         {
     729           0 :             DGNElemBSplineSurfaceBoundary *psBounds =
     730             :                 (DGNElemBSplineSurfaceBoundary *)psElement;
     731             : 
     732           0 :             fprintf(fp, "  boundary number=%d, # vertices=%d\n",
     733           0 :                     psBounds->number, psBounds->numverts);
     734           0 :             for (int i = 0; i < psBounds->numverts; i++)
     735             :             {
     736           0 :                 fprintf(fp, "  (%.6f,%.6f)\n", psBounds->vertices[i].x,
     737             :                         psBounds->vertices[i].y);
     738             :             }
     739             :         }
     740           0 :         break;
     741             : 
     742           0 :         case DGNST_KNOT_WEIGHT:
     743             :         {
     744           0 :             DGNElemKnotWeight *psArray = (DGNElemKnotWeight *)psElement;
     745           0 :             int numelems = (psArray->core.size - 36) / 4;
     746           0 :             for (int i = 0; i < numelems; i++)
     747             :             {
     748           0 :                 fprintf(fp, "  %.6f\n", psArray->array[i]);
     749             :             }
     750             :         }
     751           0 :         break;
     752             : 
     753           0 :         default:
     754           0 :             break;
     755             :     }
     756             : 
     757           0 :     if (psElement->attr_bytes > 0)
     758             :     {
     759           0 :         fprintf(fp, "Attributes (%d bytes):\n", psElement->attr_bytes);
     760             : 
     761           0 :         for (int iLink = 0; true; iLink++)
     762             :         {
     763           0 :             int nLinkType = 0;
     764           0 :             int nEntityNum = 0;
     765           0 :             int nMSLink = 0;
     766           0 :             int nLinkSize = 0;
     767             :             // coverity[tained_data]
     768             :             unsigned char *pabyData =
     769           0 :                 DGNGetLinkage(hDGN, psElement, iLink, &nLinkType, &nEntityNum,
     770             :                               &nMSLink, &nLinkSize);
     771           0 :             if (pabyData == nullptr)
     772           0 :                 break;
     773             : 
     774           0 :             fprintf(fp, "Type=0x%04x", nLinkType);
     775           0 :             if (nMSLink != 0 || nEntityNum != 0)
     776           0 :                 fprintf(fp, ", EntityNum=%d, MSLink=%d", nEntityNum, nMSLink);
     777             : 
     778           0 :             int nBytes = static_cast<int>(psElement->attr_data +
     779           0 :                                           psElement->attr_bytes - pabyData);
     780           0 :             if (nBytes < nLinkSize)
     781             :             {
     782           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
     783             :                          "Corrupt linkage, element id:%d, link:%d",
     784             :                          psElement->element_id, iLink);
     785           0 :                 fprintf(fp, " (Corrupt, declared size: %d, assuming size: %d)",
     786             :                         nLinkSize, nBytes);
     787           0 :                 nLinkSize = nBytes;
     788             :             }
     789           0 :             fprintf(fp, "\n  0x");
     790             : 
     791           0 :             for (int i = 0; i < nLinkSize; i++)
     792           0 :                 fprintf(fp, "%02x", pabyData[i]);
     793           0 :             fprintf(fp, "\n");
     794           0 :         }
     795             :     }
     796           0 : }
     797             : 
     798             : /************************************************************************/
     799             : /*                           DGNTypeToName()                            */
     800             : /************************************************************************/
     801             : 
     802             : /**
     803             :  * Convert type to name.
     804             :  *
     805             :  * Returns a human readable name for an element type such as DGNT_LINE.
     806             :  *
     807             :  * @param nType the DGNT_* type code to translate.
     808             :  *
     809             :  * @return a pointer to an internal string with the translation.  This string
     810             :  * should not be modified or freed.
     811             :  */
     812             : 
     813           0 : const char *DGNTypeToName(int nType)
     814             : 
     815             : {
     816             :     static char szNumericResult[16] = {};
     817             : 
     818           0 :     switch (nType)
     819             :     {
     820           0 :         case DGNT_CELL_LIBRARY:
     821           0 :             return "Cell Library";
     822             : 
     823           0 :         case DGNT_CELL_HEADER:
     824           0 :             return "Cell Header";
     825             : 
     826           0 :         case DGNT_LINE:
     827           0 :             return "Line";
     828             : 
     829           0 :         case DGNT_LINE_STRING:
     830           0 :             return "Line String";
     831             : 
     832           0 :         case DGNT_POINT_STRING:
     833           0 :             return "Point String";
     834             : 
     835           0 :         case DGNT_GROUP_DATA:
     836           0 :             return "Group Data";
     837             : 
     838           0 :         case DGNT_SHAPE:
     839           0 :             return "Shape";
     840             : 
     841           0 :         case DGNT_TEXT_NODE:
     842           0 :             return "Text Node";
     843             : 
     844           0 :         case DGNT_DIGITIZER_SETUP:
     845           0 :             return "Digitizer Setup";
     846             : 
     847           0 :         case DGNT_TCB:
     848           0 :             return "TCB";
     849             : 
     850           0 :         case DGNT_LEVEL_SYMBOLOGY:
     851           0 :             return "Level Symbology";
     852             : 
     853           0 :         case DGNT_CURVE:
     854           0 :             return "Curve";
     855             : 
     856           0 :         case DGNT_COMPLEX_CHAIN_HEADER:
     857           0 :             return "Complex Chain Header";
     858             : 
     859           0 :         case DGNT_COMPLEX_SHAPE_HEADER:
     860           0 :             return "Complex Shape Header";
     861             : 
     862           0 :         case DGNT_ELLIPSE:
     863           0 :             return "Ellipse";
     864             : 
     865           0 :         case DGNT_ARC:
     866           0 :             return "Arc";
     867             : 
     868           0 :         case DGNT_TEXT:
     869           0 :             return "Text";
     870             : 
     871           0 :         case DGNT_BSPLINE_POLE:
     872           0 :             return "B-Spline Pole";
     873             : 
     874           0 :         case DGNT_BSPLINE_SURFACE_HEADER:
     875           0 :             return "B-Spline Surface Header";
     876             : 
     877           0 :         case DGNT_BSPLINE_SURFACE_BOUNDARY:
     878           0 :             return "B-Spline Surface Boundary";
     879             : 
     880           0 :         case DGNT_BSPLINE_KNOT:
     881           0 :             return "B-Spline Knot";
     882             : 
     883           0 :         case DGNT_BSPLINE_CURVE_HEADER:
     884           0 :             return "B-Spline Curve Header";
     885             : 
     886           0 :         case DGNT_BSPLINE_WEIGHT_FACTOR:
     887           0 :             return "B-Spline Weight Factor";
     888             : 
     889           0 :         case DGNT_APPLICATION_ELEM:
     890           0 :             return "Application Element";
     891             : 
     892           0 :         case DGNT_SHARED_CELL_DEFN:
     893           0 :             return "Shared Cell Definition";
     894             : 
     895           0 :         case DGNT_SHARED_CELL_ELEM:
     896           0 :             return "Shared Cell Element";
     897             : 
     898           0 :         case DGNT_TAG_VALUE:
     899           0 :             return "Tag Value";
     900             : 
     901           0 :         case DGNT_CONE:
     902           0 :             return "Cone";
     903             : 
     904           0 :         case DGNT_3DSURFACE_HEADER:
     905           0 :             return "3D Surface Header";
     906             : 
     907           0 :         case DGNT_3DSOLID_HEADER:
     908           0 :             return "3D Solid Header";
     909             : 
     910           0 :         default:
     911           0 :             snprintf(szNumericResult, sizeof(szNumericResult), "%d", nType);
     912           0 :             return szNumericResult;
     913             :     }
     914             : }
     915             : 
     916             : /************************************************************************/
     917             : /*                         DGNGetAttrLinkSize()                         */
     918             : /************************************************************************/
     919             : 
     920             : /**
     921             :  * Get attribute linkage size.
     922             :  *
     923             :  * Returns the size, in bytes, of the attribute linkage starting at byte
     924             :  * offset nOffset.  On failure a value of 0 is returned.
     925             :  *
     926             :  * @param hDGN the file from which the element originated.
     927             :  * @param psElement the element to report on.
     928             :  * @param nOffset byte offset within attribute data of linkage to check.
     929             :  *
     930             :  * @return size of linkage in bytes, or zero.
     931             :  */
     932             : 
     933         503 : int DGNGetAttrLinkSize(CPL_UNUSED DGNHandle hDGN, DGNElemCore *psElement,
     934             :                        int nOffset)
     935             : {
     936         503 :     if (psElement->attr_bytes < nOffset + 4)
     937         336 :         return 0;
     938             : 
     939             :     /* DMRS Linkage */
     940         167 :     if ((psElement->attr_data[nOffset + 0] == 0 &&
     941          11 :          psElement->attr_data[nOffset + 1] == 0) ||
     942         156 :         (psElement->attr_data[nOffset + 0] == 0 &&
     943           0 :          psElement->attr_data[nOffset + 1] == 0x80))
     944          11 :         return 8;
     945             : 
     946             :     /* If low order bit of second byte is set, first byte is length */
     947         156 :     if (psElement->attr_data[nOffset + 1] & 0x10)
     948         156 :         return psElement->attr_data[nOffset + 0] * 2 + 2;
     949             : 
     950             :     /* unknown */
     951           0 :     return 0;
     952             : }
     953             : 
     954             : /************************************************************************/
     955             : /*                           DGNGetLinkage()                            */
     956             : /************************************************************************/
     957             : 
     958             : /**
     959             :  * Returns requested linkage raw data.
     960             :  *
     961             :  * A pointer to the raw data for the requested attribute linkage is returned
     962             :  * as well as (potentially) various information about the linkage including
     963             :  * the linkage type, database entity number and MSLink value, and the length
     964             :  * of the raw linkage data in bytes.
     965             :  *
     966             :  * If the requested linkage (iIndex) does not exist a value of zero is
     967             :  * returned.
     968             :  *
     969             :  * The entity number is (loosely speaking) the index of the table within
     970             :  * the current database to which the MSLINK value will refer.  The entity
     971             :  * number should be used to lookup the table name in the MSCATALOG table.
     972             :  * The MSLINK value is the key value for the record in the target table.
     973             :  *
     974             :  * @param hDGN the file from which the element originated.
     975             :  * @param psElement the element to report on.
     976             :  * @param iIndex the zero based index of the linkage to fetch.
     977             :  * @param pnLinkageType variable to return linkage type.  This may be one of
     978             :  * the predefined DGNLT_ values or a different value. This pointer may be NULL.
     979             :  * @param pnEntityNum variable to return the entity number in or NULL if not
     980             :  * required.
     981             :  * @param pnMSLink variable to return the MSLINK value in, or NULL if not
     982             :  * required.
     983             :  * @param pnLength variable to returned the linkage size in bytes or NULL.
     984             :  *
     985             :  * @return pointer to raw internal linkage data.  This data should not be
     986             :  * altered or freed.  NULL returned on failure.
     987             :  */
     988             : 
     989         420 : unsigned char *DGNGetLinkage(DGNHandle hDGN, DGNElemCore *psElement, int iIndex,
     990             :                              int *pnLinkageType, int *pnEntityNum,
     991             :                              int *pnMSLink, int *pnLength)
     992             : 
     993             : {
     994         420 :     int nLinkSize = 0;
     995             : 
     996         420 :     for (int iLinkage = 0, nAttrOffset = 0;
     997         503 :          (nLinkSize = DGNGetAttrLinkSize(hDGN, psElement, nAttrOffset)) != 0;
     998          83 :          iLinkage++, nAttrOffset += nLinkSize)
     999             :     {
    1000         167 :         if (iLinkage == iIndex)
    1001             :         {
    1002          84 :             if (nLinkSize <= 4)
    1003             :             {
    1004           0 :                 CPLError(CE_Failure, CPLE_AssertionFailed, "nLinkSize <= 4");
    1005           0 :                 return nullptr;
    1006             :             }
    1007          84 :             if (nLinkSize + nAttrOffset > psElement->attr_bytes)
    1008             :             {
    1009           0 :                 CPLError(CE_Failure, CPLE_AssertionFailed,
    1010             :                          "nLinkSize + nAttrOffset > psElement->attr_bytes");
    1011           0 :                 return nullptr;
    1012             :             }
    1013             : 
    1014          84 :             int nLinkageType = 0;
    1015          84 :             int nEntityNum = 0;
    1016          84 :             int nMSLink = 0;
    1017          84 :             if (psElement->attr_bytes >= nAttrOffset + 7 &&
    1018          84 :                 psElement->attr_data[nAttrOffset + 0] == 0x00 &&
    1019           4 :                 (psElement->attr_data[nAttrOffset + 1] == 0x00 ||
    1020           0 :                  psElement->attr_data[nAttrOffset + 1] == 0x80))
    1021             :             {
    1022           4 :                 nLinkageType = DGNLT_DMRS;
    1023           4 :                 nEntityNum = psElement->attr_data[nAttrOffset + 2] +
    1024           4 :                              psElement->attr_data[nAttrOffset + 3] * 256;
    1025           4 :                 nMSLink = psElement->attr_data[nAttrOffset + 4] +
    1026           4 :                           psElement->attr_data[nAttrOffset + 5] * 256 +
    1027           4 :                           psElement->attr_data[nAttrOffset + 6] * 65536;
    1028             :             }
    1029          80 :             else if (psElement->attr_bytes >= nAttrOffset + 4)
    1030          80 :                 nLinkageType = psElement->attr_data[nAttrOffset + 2] +
    1031          80 :                                psElement->attr_data[nAttrOffset + 3] * 256;
    1032             : 
    1033             :             // Possibly an external database linkage?
    1034          84 :             if (nLinkSize == 16 && nLinkageType != DGNLT_SHAPE_FILL &&
    1035          72 :                 psElement->attr_bytes >= nAttrOffset + 12)
    1036             :             {
    1037          72 :                 nEntityNum = psElement->attr_data[nAttrOffset + 6] +
    1038          72 :                              psElement->attr_data[nAttrOffset + 7] * 256;
    1039          72 :                 nMSLink = psElement->attr_data[nAttrOffset + 8] |
    1040          72 :                           (psElement->attr_data[nAttrOffset + 9] << 8) |
    1041          72 :                           (psElement->attr_data[nAttrOffset + 10] << 16) |
    1042          72 :                           (psElement->attr_data[nAttrOffset + 11] << 24);
    1043             :             }
    1044             : 
    1045          84 :             if (pnLinkageType != nullptr)
    1046          37 :                 *pnLinkageType = nLinkageType;
    1047          84 :             if (pnEntityNum != nullptr)
    1048          26 :                 *pnEntityNum = nEntityNum;
    1049          84 :             if (pnMSLink != nullptr)
    1050          26 :                 *pnMSLink = nMSLink;
    1051          84 :             if (pnLength != nullptr)
    1052          37 :                 *pnLength = nLinkSize;
    1053             : 
    1054          84 :             return psElement->attr_data + nAttrOffset;
    1055             :         }
    1056             :     }
    1057             : 
    1058         336 :     return nullptr;
    1059             : }
    1060             : 
    1061             : /************************************************************************/
    1062             : /*                         DGNRotationToQuat()                          */
    1063             : /*                                                                      */
    1064             : /*      Compute a quaternion for a given Z rotation.                    */
    1065             : /************************************************************************/
    1066             : 
    1067           0 : void DGNRotationToQuaternion(double dfRotation, int *panQuaternion)
    1068             : 
    1069             : {
    1070           0 :     const double dfRadianRot = (dfRotation / 180.0) * M_PI;
    1071             : 
    1072           0 :     panQuaternion[0] = (int)(cos(-dfRadianRot / 2.0) * 2147483647);
    1073           0 :     panQuaternion[1] = 0;
    1074           0 :     panQuaternion[2] = 0;
    1075           0 :     panQuaternion[3] = (int)(sin(-dfRadianRot / 2.0) * 2147483647);
    1076           0 : }
    1077             : 
    1078             : /************************************************************************/
    1079             : /*                         DGNQuaternionToMatrix()                      */
    1080             : /*                                                                      */
    1081             : /*      Compute a rotation matrix for a given quaternion                */
    1082             : /* FIXME: Write documentation on how to use this matrix                 */
    1083             : /* (i.e. things like row/column major, OpenGL style or not)             */
    1084             : /* kintel 20030819                                                      */
    1085             : /************************************************************************/
    1086             : 
    1087           0 : void DGNQuaternionToMatrix(int *quat, float *mat)
    1088             : {
    1089           0 :     const double q[4] = {1.0 * quat[1] / (1U << 31), 1.0 * quat[2] / (1U << 31),
    1090           0 :                          1.0 * quat[3] / (1U << 31),
    1091           0 :                          1.0 * quat[0] / (1U << 31)};
    1092             : 
    1093           0 :     mat[0 * 3 + 0] =
    1094           0 :         (float)(q[0] * q[0] - q[1] * q[1] - q[2] * q[2] + q[3] * q[3]);
    1095           0 :     mat[0 * 3 + 1] = (float)(2 * (q[2] * q[3] + q[0] * q[1]));
    1096           0 :     mat[0 * 3 + 2] = (float)(2 * (q[0] * q[2] - q[1] * q[3]));
    1097           0 :     mat[1 * 3 + 0] = (float)(2 * (q[0] * q[1] - q[2] * q[3]));
    1098           0 :     mat[1 * 3 + 1] =
    1099           0 :         (float)(-q[0] * q[0] + q[1] * q[1] - q[2] * q[2] + q[3] * q[3]);
    1100           0 :     mat[1 * 3 + 2] = (float)(2 * (q[0] * q[3] + q[1] * q[2]));
    1101           0 :     mat[2 * 3 + 0] = (float)(2 * (q[0] * q[2] + q[1] * q[3]));
    1102           0 :     mat[2 * 3 + 1] = (float)(2 * (q[1] * q[2] - q[0] * q[3]));
    1103           0 :     mat[2 * 3 + 2] =
    1104           0 :         (float)(-q[0] * q[0] - q[1] * q[1] + q[2] * q[2] + q[3] * q[3]);
    1105           0 : }
    1106             : 
    1107             : /************************************************************************/
    1108             : /*                  DGNTransformPointWithQuaternion()                   */
    1109             : /************************************************************************/
    1110             : 
    1111             : #ifdef unused
    1112             : void DGNTransformPointWithQuaternionVertex(CPL_UNUSED int *quat,
    1113             :                                            CPL_UNUSED DGNPoint *v1,
    1114             :                                            CPL_UNUSED DGNPoint *v2)
    1115             : {
    1116             :     /* ==================================================================== */
    1117             :     /*      Original code provided by kintel 20030819, but assumed to be    */
    1118             :     /*      incomplete.                                                     */
    1119             :     /* ==================================================================== */
    1120             : 
    1121             : #ifdef notdef
    1122             :     See below for sketched implementation. kintel 20030819.
    1123             :                                float x,y,z,w;
    1124             :     // FIXME: Convert quat to x,y,z,w
    1125             :     v2.x = w * w * v1.x + 2 * y * w * v1.z - 2 * z * w * v1.y + x * x * v1.x +
    1126             :            2 * y * x * v1.y + 2 * z * x * v1.z - z * z * v1.x - y * y * v1.x;
    1127             :     v2.y = 2 * x * y * v1.x + y * y * v1.y + 2 * z * y * v1.z +
    1128             :            2 * w * z * v1.x - z * z * v1.y + w * w * v1.y - 2 * x * w * v1.z -
    1129             :            x * x * v1.y;
    1130             :     v2.z = 2 * x * z * v1.x + 2 * y * z * v1.y + z * z * v1.z -
    1131             :            2 * w * y * v1.x - y * y * v1.z + 2 * w * x * v1.y - x * x * v1.z +
    1132             :            w * w * v1.z;
    1133             : #endif
    1134             : 
    1135             :     /* ==================================================================== */
    1136             :     /*      Implementation provided by Peggy Jung - 2004/03/05.            */
    1137             :     /*      peggy.jung at moskito-gis dot de.  I haven't tested it.         */
    1138             :     /* ==================================================================== */
    1139             : 
    1140             :     /*  Version: 0.1                                 Datum: 26.01.2004
    1141             : 
    1142             :     IN:
    1143             :     x,y,z               // DGNPoint &v1
    1144             :     quat[]              //
    1145             : 
    1146             :     OUT:
    1147             :     newX, newY, newZ    // DGNPoint &v2
    1148             : 
    1149             :     Author: Peggy Jung
    1150             :     */
    1151             :     /*
    1152             :         double ROT[12];  //rotation matrix for a given quaternion
    1153             :         double xx, xy, xz, xw, yy, yz, yw, zz, zw;
    1154             :         double a, b, c, d, n, x, y, z;
    1155             : 
    1156             :         x = v1->x;
    1157             :         y = v1->y;
    1158             :         z = v1->z;
    1159             : 
    1160             :         n =
    1161             :        sqrt((double)PDP2PC_long(quat[0])*(double)PDP2PC_long(quat[0])+(double)PDP2PC_long(quat[1])*(double)PDP2PC_long(quat[1])+
    1162             :                  (double)PDP2PC_long(quat[2])*(double)PDP2PC_long(quat[2])+(double)PDP2PC_long(quat[3])*(double)PDP2PC_long(quat[3]));
    1163             : 
    1164             :         a = (double)PDP2PC_long(quat[0])/n; //w
    1165             :         b = (double)PDP2PC_long(quat[1])/n; //x
    1166             :         c = (double)PDP2PC_long(quat[2])/n; //y
    1167             :         d = (double)PDP2PC_long(quat[3])/n; //z
    1168             : 
    1169             :         xx      = b*b;
    1170             :         xy      = b*c;
    1171             :         xz      = b*d;
    1172             :         xw      = b*a;
    1173             : 
    1174             :         yy      = c*c;
    1175             :         yz      = c*d;
    1176             :         yw      = c*a;
    1177             : 
    1178             :         zz      = d*d;
    1179             :         zw      = d+a;
    1180             : 
    1181             :         ROT[0] = 1 - 2 * yy - 2 * zz ;
    1182             :         ROT[1] =     2 * xy - 2 * zw ;
    1183             :         ROT[2] =     2 * xz + 2 * yw ;
    1184             : 
    1185             :         ROT[4] =     2 * xy + 2 * zw ;
    1186             :         ROT[5] = 1 - 2 * xx - 2 * zz ;
    1187             :         ROT[6] =     2 * yz - 2 * xw ;
    1188             : 
    1189             :         ROT[8] =     2 * xz - 2 * yw ;
    1190             :         ROT[9] =     2 * yz + 2 * xw ;
    1191             :         ROT[10] = 1 - 2 * xx - 2 * yy ;
    1192             : 
    1193             :         v2->x = ROT[0]*x + ROT[1]*y + ROT[2]*z;
    1194             :         v2->y = ROT[4]*x + ROT[5]*y + ROT[6]*z;
    1195             :         v2->z = ROT[8]*x + ROT[9]*y + ROT[10]*z;
    1196             :     */
    1197             : }
    1198             : #endif

Generated by: LCOV version 1.14