📄 dgnwrite.cpp
字号:
CPLAssert( psDGN->dimension == 2 ); CPLAssert( nType == DGNT_LINE || nType == DGNT_LINE_STRING || nType == DGNT_SHAPE || nType == DGNT_CURVE || nType == DGNT_BSPLINE );/* -------------------------------------------------------------------- *//* Is this too many vertices to write to a single element? *//* -------------------------------------------------------------------- */ if( (psDGN->dimension == 2 && 38 + 8 * nPointCount >= 512) || (psDGN->dimension == 3 && 38 + 12 * nPointCount >= 512) ) { CPLError( CE_Failure, CPLE_ElementTooBig, "Attempt to create %s element with %d points failed.\n" "Element would be too large.", DGNTypeToName( nType ), nPointCount ); return NULL; }/* -------------------------------------------------------------------- *//* Allocate element. *//* -------------------------------------------------------------------- */ psMP = (DGNElemMultiPoint *) CPLCalloc( sizeof(DGNElemMultiPoint) + sizeof(DGNPoint) * (nPointCount-2), 1 ); psCore = &(psMP->core); DGNInitializeElemCore( hDGN, psCore ); psCore->stype = DGNST_MULTIPOINT; psCore->type = nType;/* -------------------------------------------------------------------- *//* Set multipoint specific information in the structure. *//* -------------------------------------------------------------------- */ psMP->num_vertices = nPointCount; memcpy( psMP->vertices + 0, pasVertices, sizeof(DGNPoint) * nPointCount );/* -------------------------------------------------------------------- *//* Setup Raw data for the multipoint section. *//* -------------------------------------------------------------------- */ if( nType == DGNT_LINE ) { CPLAssert( nPointCount == 2 ); psCore->raw_bytes = 36 + 8 * nPointCount; psCore->raw_data = (unsigned char*) CPLCalloc(psCore->raw_bytes,1); DGNInverseTransformPointToInt( psDGN, pasVertices + 0, psCore->raw_data + 36 ); DGNInverseTransformPointToInt( psDGN, pasVertices + 1, psCore->raw_data + 44 ); } else { CPLAssert( nPointCount >= 2 ); psCore->raw_bytes = 38 + 8 * nPointCount; psCore->raw_data = (unsigned char*) CPLCalloc(psCore->raw_bytes,1); psCore->raw_data[36] = nPointCount % 256; psCore->raw_data[37] = nPointCount/256; for( i = 0; i < nPointCount; i++ ) DGNInverseTransformPointToInt( psDGN, pasVertices + i, psCore->raw_data + 38 + i*8 ); } /* -------------------------------------------------------------------- *//* Set the core raw data, including the bounds. *//* -------------------------------------------------------------------- */ DGNUpdateElemCoreExtended( hDGN, psCore ); sMin = sMax = pasVertices[0]; for( i = 1; i < nPointCount; i++ ) { sMin.x = MIN(pasVertices[i].x,sMin.x); sMax.x = MAX(pasVertices[i].x,sMax.x); sMin.y = MIN(pasVertices[i].y,sMin.y); sMax.y = MAX(pasVertices[i].y,sMax.y); } DGNWriteBounds( psDGN, psCore, &sMin, &sMax ); return psCore;}/************************************************************************//* DGNCreateArcElem2D() *//************************************************************************//** * Create Arc or Ellipse element. * * Create a new 2D arc or ellipse element. The start angle, and sweep angle * are ignored for DGNT_ELLIPSE but used for DGNT_ARC. * * The newly created element will still need to be written to file using * DGNWriteElement(). Also the level and other core values will be defaulted. * Use DGNUpdateElemCore() on the element before writing to set these values. * * @param hDGN the DGN file on which the element will eventually be written. * @param nType either DGNT_ELLIPSE or DGNT_ARC to select element type. * @param dfOriginX the origin (center of rotation) of the arc (X). * @param dfOriginY the origin (center of rotation) of the arc (Y). * @param dfPrimaryAxis the length of the primary axis. * @param dfSecondaryAxis the length of the secondary axis. * @param dfRotation Counterclockwise rotation in degrees. * @param dfStartAngle start angle, degrees counterclockwise of primary axis. * @param dfSweepAngle sweep angle, degrees * * @return the new element (DGNElemArc) or NULL on failure. */DGNElemCore *DGNCreateArcElem2D( DGNHandle hDGN, int nType, double dfOriginX, double dfOriginY, double dfPrimaryAxis, double dfSecondaryAxis, double dfRotation, double dfStartAngle, double dfSweepAngle ){ DGNElemArc *psArc; DGNElemCore *psCore; DGNInfo *psDGN = (DGNInfo *) hDGN; DGNPoint sMin, sMax, sOrigin; CPLAssert( psDGN->dimension == 2 ); CPLAssert( nType == DGNT_ARC || nType == DGNT_ELLIPSE );/* -------------------------------------------------------------------- *//* Allocate element. *//* -------------------------------------------------------------------- */ psArc = (DGNElemArc *) CPLCalloc( sizeof(DGNElemArc), 1 ); psCore = &(psArc->core); DGNInitializeElemCore( hDGN, psCore ); psCore->stype = DGNST_ARC; psCore->type = nType;/* -------------------------------------------------------------------- *//* Set arc specific information in the structure. *//* -------------------------------------------------------------------- */ sOrigin.x = dfOriginX; sOrigin.y = dfOriginY; sOrigin.z = 0.0; psArc->origin = sOrigin; psArc->primary_axis = dfPrimaryAxis; psArc->secondary_axis = dfSecondaryAxis; psArc->rotation = dfRotation; memset( psArc->quat, 0, sizeof(long) * 4 ); psArc->startang = dfStartAngle; psArc->sweepang = dfSweepAngle;/* -------------------------------------------------------------------- *//* Setup Raw data for the arc section. *//* -------------------------------------------------------------------- */ if( nType == DGNT_ARC ) { GInt32 nAngle; double dfScaledAxis; psCore->raw_bytes = 80; psCore->raw_data = (unsigned char*) CPLCalloc(psCore->raw_bytes,1); /* start angle */ nAngle = (int) (dfStartAngle * 360000.0); DGN_WRITE_INT32( nAngle, psCore->raw_data + 36 ); /* sweep angle */ if( dfSweepAngle < 0.0 ) { nAngle = (int) (ABS(dfSweepAngle) * 360000.0); nAngle |= 0x80000000; } else if( dfSweepAngle > 364.9999 ) { nAngle = 0; } else { nAngle = (int) (dfSweepAngle * 360000.0); } DGN_WRITE_INT32( nAngle, psCore->raw_data + 40 ); /* axes */ dfScaledAxis = dfPrimaryAxis / psDGN->scale; memcpy( psCore->raw_data + 44, &dfScaledAxis, 8 ); IEEE2DGNDouble( psCore->raw_data + 44 ); dfScaledAxis = dfSecondaryAxis / psDGN->scale; memcpy( psCore->raw_data + 52, &dfScaledAxis, 8 ); IEEE2DGNDouble( psCore->raw_data + 52 ); /* rotation */ nAngle = (int) (dfRotation * 360000.0); DGN_WRITE_INT32( nAngle, psCore->raw_data + 60 ); /* origin */ DGNInverseTransformPoint( psDGN, &sOrigin ); memcpy( psCore->raw_data + 64, &(sOrigin.x), 8 ); memcpy( psCore->raw_data + 72, &(sOrigin.y), 8 ); IEEE2DGNDouble( psCore->raw_data + 64 ); IEEE2DGNDouble( psCore->raw_data + 72 ); }/* -------------------------------------------------------------------- *//* Setup Raw data for the ellipse section. *//* -------------------------------------------------------------------- */ else { GInt32 nAngle; double dfScaledAxis; psCore->raw_bytes = 72; psCore->raw_data = (unsigned char*) CPLCalloc(psCore->raw_bytes,1); /* axes */ dfScaledAxis = dfPrimaryAxis / psDGN->scale; memcpy( psCore->raw_data + 36, &dfScaledAxis, 8 ); IEEE2DGNDouble( psCore->raw_data + 36 ); dfScaledAxis = dfSecondaryAxis / psDGN->scale; memcpy( psCore->raw_data + 44, &dfScaledAxis, 8 ); IEEE2DGNDouble( psCore->raw_data + 44 ); /* rotation */ nAngle = (int) (dfRotation * 360000.0); DGN_WRITE_INT32( nAngle, psCore->raw_data + 52 ); /* origin */ DGNInverseTransformPoint( psDGN, &sOrigin ); memcpy( psCore->raw_data + 56, &(sOrigin.x), 8 ); memcpy( psCore->raw_data + 64, &(sOrigin.y), 8 ); IEEE2DGNDouble( psCore->raw_data + 56 ); IEEE2DGNDouble( psCore->raw_data + 64 ); psArc->startang = 0.0; psArc->sweepang = 360.0; } /* -------------------------------------------------------------------- *//* Set the core raw data, including the bounds. *//* -------------------------------------------------------------------- */ DGNUpdateElemCoreExtended( hDGN, psCore ); sMin.x = dfOriginX - MAX(dfPrimaryAxis,dfSecondaryAxis); sMin.y = dfOriginY - MAX(dfPrimaryAxis,dfSecondaryAxis); sMin.z = 0.0; sMax.x = dfOriginX + MAX(dfPrimaryAxis,dfSecondaryAxis); sMax.y = dfOriginY + MAX(dfPrimaryAxis,dfSecondaryAxis); sMax.z = 0.0; DGNWriteBounds( psDGN, psCore, &sMin, &sMax ); return psCore;} /************************************************************************//* DGNCreateTextElem() *//************************************************************************//** * Create text element. * * The newly created element will still need to be written to file using * DGNWriteElement(). Also the level and other core values will be defaulted. * Use DGNUpdateElemCore() on the element before writing to set these values. * * @param hDGN the file on which the element will eventually be written. * @param pszText the string of text. * @param nFontId microstation font id for the text. 1 may be used as default. * @param nJustification text justification. One of DGNJ_LEFT_TOP, * DGNJ_LEFT_CENTER, DGNJ_LEFT_BOTTOM, DGNJ_CENTER_TOP, DGNJ_CENTER_CENTER, * DGNJ_CENTER_BOTTOM, DGNJ_RIGHT_TOP, DGNJ_RIGHT_CENTER, DGNJ_RIGHT_BOTTOM. * @param dfLengthMult character width in master units. * @param dfHeightMult character height in master units. * @param dfRotation Counterclockwise text rotation in degrees. * @param dfOriginX Text origin (X). * @param dfOriginY Text origin (Y). * @param dfOriginZ Text origin (Z). * * @return the new element (DGNElemText) or NULL on failure. */DGNElemCore *DGNCreateTextElem( DGNHandle hDGN, const char *pszText, int nFontId, int nJustification, double dfLengthMult, double dfHeightMult, double dfRotation, double dfOriginX, double dfOriginY, double dfOriginZ ){ DGNElemText *psText; DGNElemCore *psCore; DGNInfo *psDGN = (DGNInfo *) hDGN; DGNPoint sMin, sMax; GInt32 nIntValue; CPLAssert( psDGN->dimension == 2 );/* -------------------------------------------------------------------- *//* Allocate element. *//* -------------------------------------------------------------------- */ psText = (DGNElemText *) CPLCalloc( sizeof(DGNElemText)+strlen(pszText), 1 ); psCore = &(psText->core); DGNInitializeElemCore( hDGN, psCore ); psCore->stype = DGNST_TEXT; psCore->type = DGNT_TEXT;/* -------------------------------------------------------------------- *//* Set arc specific information in the structure. *//* -------------------------------------------------------------------- */ psText->font_id = nFontId; psText->justification = nJustification; psText->length_mult = dfLengthMult; psText->height_mult = dfHeightMult; psText->rotation = dfRotation; psText->origin.x = dfOriginX; psText->origin.y = dfOriginY; psText->origin.z = dfOriginZ; strcpy( psText->string, pszText );/* -------------------------------------------------------------------- *//* Setup Raw data for the text specific portion. *//* -------------------------------------------------------------------- */ psCore->raw_bytes = 60 + strlen(pszText); psCore->raw_bytes += (psCore->raw_bytes % 2); psCore->raw_data = (unsigned char*) CPLCalloc(psCore->raw_bytes,1); psCore->raw_data[36] = nFontId; psCore->raw_data[37] = nJustification; nIntValue = (int) (dfLengthMult * 1000.0 / (psDGN->scale * 6.0)); DGN_WRITE_INT32( nIntValue, psCore->raw_data + 38 ); nIntValue = (int) (dfHeightMult * 1000.0 / (psDGN->scale * 6.0)); DGN_WRITE_INT32( nIntValue, psCore->raw_data + 42 ); nIntValue = (int) (dfRotation * 360000.0); DGN_WRITE_INT32( nIntValue, psCore->raw_data + 46 ); DGNInverseTransformPointToInt( psDGN, &(psText->origin), psCore->raw_data + 50 ); psCore->raw_data[58] = strlen(pszText); psCore->raw_data[59] = 0; /* edflds? */ memcpy( psCore->raw_data + 60, pszText, strlen(pszText) ); /* -------------------------------------------------------------------- *//* Set the core raw data, including the bounds. *//* -------------------------------------------------------------------- */ DGNUpdateElemCoreExtended( hDGN, psCore ); sMin.x = dfOriginX - dfLengthMult * strlen(pszText); sMin.y = dfOriginY - dfHeightMult; sMin.z = 0.0; sMax.x = dfOriginX + dfLengthMult * strlen(pszText); sMax.y = dfOriginY + dfHeightMult; sMax.z = 0.0; DGNWriteBounds( psDGN, psCore, &sMin, &sMax ); return psCore;}/************************************************************************//* DGNCreateColorTableElem() *//************************************************************************//** * Create color table element. * * Creates a color table element with the indicated color table. * * Note that color table elements are actally of type DGNT_GROUP_DATA(5) * and always on level 1. Do not alter the level with DGNUpdateElemCore() * or the element will essentially be corrupt. * * The newly created element will still need to be written to file using * DGNWriteElement(). Also the level and other core values will be defaulted. * Use DGNUpdateElemCore() on the element before writing to set these values. * * @param hDGN the file to which the element will eventually be written. * @param nScreenFlag the screen to which the color table applies * (0 = left, 1 = right). * @param abyColorInfo[8][3] array of 256 color entries. The first is * the background color. * * @return the new element (DGNElemColorTable) or NULL on failure. */DGNElemCore *DGNCreateColorTableElem( DGNHandle hDGN, int nScreenFlag, GByte abyColorInfo[256][3] ){ DGNElemColorTable *psCT; DGNElemCore *psCore;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -