dgnwrite.cpp

来自「GIS系统支持库Geospatial Data Abstraction Libr」· C++ 代码 · 共 1,683 行 · 第 1/5 页

CPP
1,683
字号
}    /************************************************************************//*                             DGNCreate()                              *//************************************************************************//** * Create new DGN file. * * This function will create a new DGN file based on the provided seed * file, and return a handle on which elements may be read and written. * * The following creation flags may be passed: * <ul> * <li> DGNCF_USE_SEED_UNITS: The master and subunit resolutions and names * from the seed file will be used in the new file.  The nMasterUnitPerSubUnit, * nUORPerSubUnit, pszMasterUnits, and pszSubUnits arguments will be ignored. * <li> DGNCF_USE_SEED_ORIGIN: The origin from the seed file will be used * and the X, Y and Z origin passed into the call will be ignored.  * <li> DGNCF_COPY_SEED_FILE_COLOR_TABLE: Should the first color table occuring * in the seed file also be copied?  * <li> DGNCF_COPY_WHOLE_SEED_FILE: By default only the first three elements * (TCB, Digitizer Setup and Level Symbology) are copied from the seed file.  * If this flag is provided the entire seed file is copied verbatim (with the * TCB origin and units possibly updated). * </ul> *  * @param pszNewFilename the filename to create.  If it already exists * it will be overwritten. * @param pszSeedFile the seed file to copy header from. * @param nCreationFlags An ORing of DGNCF_* flags that are to take effect. * @param dfOriginX the X origin for the file.   * @param dfOriginY the Y origin for the file.  * @param dfOriginZ the Z origin for the file.  * @param nSubUnitPerMasterUnit the number of subunits in one master unit. * @param nUORPerSubUnit the number of UOR (units of resolution) per subunit. * @param pszMasterUnits the name of the master units (2 characters).  * @param pszSubUnits the name of the subunits (2 characters).  */DGNHandle      DGNCreate( const char *pszNewFilename, const char *pszSeedFile,                  int nCreationFlags,                  double dfOriginX, double dfOriginY, double dfOriginZ,                 int nSubUnitsPerMasterUnit, int nUORPerSubUnit,                  const char *pszMasterUnits, const char *pszSubUnits ){    DGNInfo *psSeed, *psDGN;    FILE    *fpNew;    DGNElemCore *psSrcTCB;/* -------------------------------------------------------------------- *//*      Open seed file, and read TCB element.                           *//* -------------------------------------------------------------------- */    psSeed = (DGNInfo *) DGNOpen( pszSeedFile, FALSE );    if( psSeed == NULL )        return NULL;    DGNSetOptions( psSeed, DGNO_CAPTURE_RAW_DATA );        psSrcTCB = DGNReadElement( psSeed );    CPLAssert( psSrcTCB->raw_bytes >= 1536 );    /* -------------------------------------------------------------------- *//*      Open output file.                                               *//* -------------------------------------------------------------------- */    fpNew = VSIFOpen( pszNewFilename, "wb" );    if( fpNew == NULL )    {        CPLError( CE_Failure, CPLE_OpenFailed,                   "Failed to open output file: %s", pszNewFilename );        return NULL;    }/* -------------------------------------------------------------------- *//*      Modify TCB appropriately for the output file.                   *//* -------------------------------------------------------------------- */    GByte *pabyRawTCB = (GByte *) CPLMalloc(psSrcTCB->raw_bytes);        memcpy( pabyRawTCB, psSrcTCB->raw_data, psSrcTCB->raw_bytes );    if( !(nCreationFlags & DGNCF_USE_SEED_UNITS) )    {        memcpy( pabyRawTCB+1120, pszMasterUnits, 2 );        memcpy( pabyRawTCB+1122, pszSubUnits, 2 );        DGN_WRITE_INT32( (unsigned char) nUORPerSubUnit, pabyRawTCB+1116 );        DGN_WRITE_INT32((unsigned char)nSubUnitsPerMasterUnit,pabyRawTCB+1112);    }    else    {        nUORPerSubUnit = DGN_INT32( pabyRawTCB+1120 );        nSubUnitsPerMasterUnit = DGN_INT32( pabyRawTCB+1112 );    }    if( !(nCreationFlags & DGNCF_USE_SEED_ORIGIN) )    {        dfOriginX *= (nUORPerSubUnit * nSubUnitsPerMasterUnit);        dfOriginY *= (nUORPerSubUnit * nSubUnitsPerMasterUnit);        dfOriginZ *= (nUORPerSubUnit * nSubUnitsPerMasterUnit);        memcpy( pabyRawTCB+1240, &dfOriginX, 8 );        memcpy( pabyRawTCB+1248, &dfOriginY, 8 );        memcpy( pabyRawTCB+1256, &dfOriginZ, 8 );        IEEE2DGNDouble( pabyRawTCB+1240 );        IEEE2DGNDouble( pabyRawTCB+1248 );        IEEE2DGNDouble( pabyRawTCB+1256 );    }/* -------------------------------------------------------------------- *//*      Write TCB and EOF to new file.                                  *//* -------------------------------------------------------------------- */    unsigned char abyEOF[2];        VSIFWrite( pabyRawTCB, psSrcTCB->raw_bytes, 1, fpNew );    CPLFree( pabyRawTCB );    abyEOF[0] = 0xff;    abyEOF[1] = 0xff;        VSIFWrite( abyEOF, 2, 1, fpNew );    DGNFreeElement( psSeed, psSrcTCB );/* -------------------------------------------------------------------- *//*      Close and re-open using DGN API.                                *//* -------------------------------------------------------------------- */    VSIFClose( fpNew );    psDGN = (DGNInfo *) DGNOpen( pszNewFilename, TRUE );/* -------------------------------------------------------------------- *//*      Now copy over elements according to options in effect.          *//* -------------------------------------------------------------------- */    DGNElemCore *psSrcElement, *psDstElement;    while( (psSrcElement = DGNReadElement( psSeed )) != NULL )    {        if( (nCreationFlags & DGNCF_COPY_WHOLE_SEED_FILE)            || (psSrcElement->stype == DGNST_COLORTABLE                 && nCreationFlags & DGNCF_COPY_SEED_FILE_COLOR_TABLE)            || psSrcElement->element_id <= 2 )        {            psDstElement = DGNCloneElement( psSeed, psDGN, psSrcElement );            DGNWriteElement( psDGN, psDstElement );            DGNFreeElement( psDGN, psDstElement );        }        DGNFreeElement( psSeed, psSrcElement );    }    DGNClose( psSeed );        return psDGN;}/************************************************************************//*                          DGNCloneElement()                           *//************************************************************************//** * Clone a retargetted element. * * Creates a copy of an element in a suitable form to write to a * different file than that it was read from.  * * NOTE: At this time the clone operation will fail if the source * and destination file have a different origin or master/sub units.  * * @param hDGNSrc the source file (from which psSrcElement was read). * @param hDGNDst the destination file (to which the returned element may be * written).  * @param psSrcElement the element to be cloned (from hDGNSrc).  * * @return NULL on failure, or an appropriately modified copy of  * the source element suitable to write to hDGNDst.  */DGNElemCore *DGNCloneElement( DGNHandle hDGNSrc, DGNHandle hDGNDst,                               DGNElemCore *psSrcElement ){    DGNElemCore *psClone = NULL;    DGNLoadTCB( hDGNDst );/* -------------------------------------------------------------------- *//*      Per structure specific copying.  The core is fixed up later.    *//* -------------------------------------------------------------------- */    if( psSrcElement->stype == DGNST_CORE )    {        psClone = (DGNElemCore *) CPLMalloc(sizeof(DGNElemCore));        memcpy( psClone, psSrcElement, sizeof(DGNElemCore) );    }    else if( psSrcElement->stype == DGNST_MULTIPOINT )    {        DGNElemMultiPoint *psMP, *psSrcMP;        int               nSize;        psSrcMP = (DGNElemMultiPoint *) psSrcElement;        nSize = sizeof(DGNElemMultiPoint)             + sizeof(DGNPoint) * (psSrcMP->num_vertices-2);        psMP = (DGNElemMultiPoint *) CPLMalloc( nSize );        memcpy( psMP, psSrcElement, nSize );        psClone = (DGNElemCore *) psMP;    }    else if( psSrcElement->stype == DGNST_ARC )    {        DGNElemArc *psArc;        psArc = (DGNElemArc *) CPLMalloc(sizeof(DGNElemArc));        memcpy( psArc, psSrcElement, sizeof(DGNElemArc) );        psClone = (DGNElemCore *) psArc;    }    else if( psSrcElement->stype == DGNST_TEXT )    {        DGNElemText       *psText, *psSrcText;        int               nSize;        psSrcText = (DGNElemText *) psSrcElement;        nSize = sizeof(DGNElemText) + strlen(psSrcText->string);        psText = (DGNElemText *) CPLMalloc( nSize );        memcpy( psText, psSrcElement, nSize );        psClone = (DGNElemCore *) psText;    }    else if( psSrcElement->stype == DGNST_COMPLEX_HEADER )    {        DGNElemComplexHeader *psCH;        psCH = (DGNElemComplexHeader *)             CPLMalloc(sizeof(DGNElemComplexHeader));        memcpy( psCH, psSrcElement, sizeof(DGNElemComplexHeader) );        psClone = (DGNElemCore *) psCH;    }    else if( psSrcElement->stype == DGNST_COLORTABLE )    {        DGNElemColorTable *psCT;        psCT = (DGNElemColorTable *) CPLMalloc(sizeof(DGNElemColorTable));        memcpy( psCT, psSrcElement, sizeof(DGNElemColorTable) );        psClone = (DGNElemCore *) psCT;    }    else if( psSrcElement->stype == DGNST_TCB )    {        DGNElemTCB *psTCB;        psTCB = (DGNElemTCB *) CPLMalloc(sizeof(DGNElemTCB));        memcpy( psTCB, psSrcElement, sizeof(DGNElemTCB) );        psClone = (DGNElemCore *) psTCB;    }    else if( psSrcElement->stype == DGNST_CELL_HEADER )    {        DGNElemCellHeader *psCH;        psCH = (DGNElemCellHeader *) CPLMalloc(sizeof(DGNElemCellHeader));        memcpy( psCH, psSrcElement, sizeof(DGNElemCellHeader) );        psClone = (DGNElemCore *) psCH;    }    else if( psSrcElement->stype == DGNST_CELL_LIBRARY )    {        DGNElemCellLibrary *psCL;                psCL = (DGNElemCellLibrary *) CPLMalloc(sizeof(DGNElemCellLibrary));        memcpy( psCL, psSrcElement, sizeof(DGNElemCellLibrary) );        psClone = (DGNElemCore *) psCL;    }    else if( psSrcElement->stype == DGNST_TAG_VALUE )    {        DGNElemTagValue *psTV;                psTV = (DGNElemTagValue *) CPLMalloc(sizeof(DGNElemTagValue));        memcpy( psTV, psSrcElement, sizeof(DGNElemTagValue) );        if( psTV->tagType == 1 )            psTV->tagValue.string = CPLStrdup( psTV->tagValue.string );        psClone = (DGNElemCore *) psTV;    }    else if( psSrcElement->stype == DGNST_TAG_SET )    {        DGNElemTagSet *psTS;        int iTag;        DGNTagDef *pasTagList;                psTS = (DGNElemTagSet *) CPLMalloc(sizeof(DGNElemTagSet));        memcpy( psTS, psSrcElement, sizeof(DGNElemTagSet) );        psTS->tagSetName = CPLStrdup( psTS->tagSetName );                pasTagList = (DGNTagDef *)             CPLMalloc( sizeof(DGNTagDef) * psTS->tagCount );        memcpy( pasTagList, psTS->tagList,                 sizeof(DGNTagDef) * psTS->tagCount );        for( iTag = 0; iTag < psTS->tagCount; iTag++ )        {            pasTagList[iTag].name = CPLStrdup( pasTagList[iTag].name );            pasTagList[iTag].prompt = CPLStrdup( pasTagList[iTag].prompt );        }        psClone = (DGNElemCore *) psTS;    }    else    {        CPLAssert( FALSE );        return NULL;    }/* -------------------------------------------------------------------- *//*      Copy core raw data, and attributes.                             *//* -------------------------------------------------------------------- */    if( psClone->raw_bytes != 0 )    {        psClone->raw_data = (unsigned char *) CPLMalloc(psClone->raw_bytes);        memcpy( psClone->raw_data, psSrcElement->raw_data,                 psClone->raw_bytes );    }    if( psClone->attr_bytes != 0 )    {        psClone->attr_data = (unsigned char *) CPLMalloc(psClone->attr_bytes);        memcpy( psClone->attr_data, psSrcElement->attr_data,                 psClone->attr_bytes );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?