dgnwrite.cpp

来自「支持各种栅格图像和矢量图像读取的库」· C++ 代码 · 共 1,687 行 · 第 1/5 页

CPP
1,687
字号
/* -------------------------------------------------------------------- */    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( nUORPerSubUnit, pabyRawTCB+1116 );        DGN_WRITE_INT32( nSubUnitsPerMasterUnit,pabyRawTCB+1112);    }    else    {        nUORPerSubUnit = DGN_INT32( pabyRawTCB+1116 );        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_TEXT_NODE )    {        DGNElemTextNode *psNode;        psNode = (DGNElemTextNode *)             CPLMalloc(sizeof(DGNElemTextNode));        memcpy( psNode, psSrcElement, sizeof(DGNElemTextNode) );        psClone = (DGNElemCore *) psNode;    }    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 );            if( pasTagList[iTag].type == 1 )                pasTagList[iTag].defaultValue.string =                     CPLStrdup( pasTagList[iTag].defaultValue.string);        }        psTS->tagList = pasTagList;        psClone = (DGNElemCore *) psTS;    }    else if( psSrcElement->stype == DGNST_CONE )    {        DGNElemCone *psCone;        psCone = (DGNElemCone *) CPLMalloc(sizeof(DGNElemCone));        memcpy( psCone, psSrcElement, sizeof(DGNElemCone) );        psClone = (DGNElemCore *) psCone;    }    else if( psSrcElement->stype == DGNST_BSPLINE_SURFACE_HEADER )    {        DGNElemBSplineSurfaceHeader *psSurface;        psSurface = (DGNElemBSplineSurfaceHeader *)           CPLMalloc(sizeof(DGNElemBSplineSurfaceHeader));        memcpy( psSurface, psSrcElement, sizeof(DGNElemBSplineSurfaceHeader) );        psClone = (DGNElemCore *) psSurface;    }    else if( psSrcElement->stype == DGNST_BSPLINE_CURVE_HEADER )    {        DGNElemBSplineCurveHeader *psCurve;        psCurve = (DGNElemBSplineCurveHeader *)           CPLMalloc(sizeof(DGNElemBSplineCurveHeader));        memcpy( psCurve, psSrcElement, sizeof(DGNElemBSplineCurveHeader) );        psClone = (DGNElemCore *) psCurve;    }    else if( psSrcElement->stype == DGNST_BSPLINE_SURFACE_BOUNDARY )    {        DGNElemBSplineSurfaceBoundary *psBSB, *psSrcBSB;        int               nSize;        psSrcBSB = (DGNElemBSplineSurfaceBoundary *) psSrcElement;        nSize = sizeof(DGNElemBSplineSurfaceBoundary)             + sizeof(DGNPoint) * (psSrcBSB->numverts-1);        psBSB = (DGNElemBSplineSurfaceBoundary *) CPLMalloc( nSize );        memcpy( psBSB, psSrcElement, nSize );        psClone = (DGNElemCore *) psBSB;    }    else if( psSrcElement->stype == DGNST_KNOT_WEIGHT )    {        DGNElemKnotWeight *psArray, *psSrcArray;        int               nSize, numelems;        // FIXME: Is it OK to assume that the # of elements corresponds        // directly to the element size? kintel 20051218.        numelems = (psSrcElement->size - 36 - psSrcElement->attr_bytes)/4;        psSrcArray = (DGNElemKnotWeight *) psSrcElement;        nSize = sizeof(DGNElemKnotWeight) + sizeof(long) * (numelems-1);        psArray = (DGNElemKnotWeight *) CPLMalloc( nSize );        memcpy( psArray, psSrcElement, nSize );        psClone = (DGNElemCore *) psArray;    }    else if( psSrcElement->stype == DGNST_SHARED_CELL_DEFN )    {        DGNElemSharedCellDefn *psCH;        psCH = (DGNElemSharedCellDefn *)CPLMalloc(sizeof(DGNElemSharedCellDefn));        memcpy( psCH, psSrcElement, sizeof(DGNElemSharedCellDefn) );        psClone = (DGNElemCore *) psCH;    }    else    {        CPLAssert( FALSE );        return NULL;

⌨️ 快捷键说明

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