' Basic program to display information about an element
'
'--------------------------------------------------------------------
'
'  Copyright (1995) Bentley Systems, Inc., All rights reserved.
'
'    $RCSfile: elemshow.bas,v $
'   $Revision: 1.2 $
'       $Date: 2002/09/01 21:39:14 $
'
'  "MicroStation" is a registered trademark of Bentley Systems, Inc. 
'
'  Limited permission is hereby granted to reproduce and modify this
'  copyrighted material provided that the resulting code is used only
'  in conjunction with Bentley Systems products under the terms of the
'  license agreement provided therein, and that this notice is retained
'  in its entirety in any such reproduction or modification.
'
'--------------------------------------------------------------------

Option Base 1
Dim test as integer
Dim mystr$ as string
Dim mystr1$ as string
Dim designFile as String
dim origin0          as MbePoint
Dim filePos0 as Long

'-------------------------------------------------------------
'
'   get Element Type Name 
'
'-------------------------------------------------------------
Function ElemTypeName$ (elemType as Integer) as String

    Select Case elemType 

    Case MBE_CellLibraryHdr 
        ElemTypeName$ = "CellLibraryHdr"
    Case MBE_CellHeader
        ElemTypeName$ = "CellHeader"
    Case MBE_Line
        ElemTypeName$ = "Line"
    Case MBE_LineString
        ElemTypeName$ = "LineString"
    Case MBE_Shape
        ElemTypeName$ = "Shape"
    Case MBE_TextNode
        ElemTypeName$ = "TextNode"
    Case MBE_Curve
        ElemTypeName$ = "Curve"
    Case MBE_ComplexString
        ElemTypeName$ = "ComplexString"
    Case MBE_Conic
        ElemTypeName$ = "Conic"
    Case MBE_ComplexShape
        ElemTypeName$ = "ComplexShape"
    Case MBE_Ellipse
        ElemTypeName$ = "Ellipse"
    Case MBE_Arc
        ElemTypeName$ = "Arc"
    Case MBE_Text
        ElemTypeName$ = "Text"
    Case MBE_Surface
        ElemTypeName$ = "Surface"
    Case MBE_Solid
        ElemTypeName$ = "Solid"
    Case MBE_BSplinePole
        ElemTypeName$ = "BSplinePole"
    Case MBE_PointString
        ElemTypeName$ = "PointString"
    Case MBE_Cone
        ElemTypeName$ = "Cone"
    Case MBE_BSplineSurface
        ElemTypeName$ = "BSplineSurface"
    Case MBE_BSplineBoundary
        ElemTypeName$ = "BSplineBoundary"
    Case MBE_BSplineKnot
        ElemTypeName$ = "BSplineKnot"
    Case MBE_BSplineCurve
        ElemTypeName$ = "BSplineCurve"
    Case MBE_BSplineWeight
        ElemTypeName$ = "BSplineWeight"
    Case MBE_Dimension
        ElemTypeName$ = "Dimension"
    Case MBE_SharedCellDefinition
        ElemTypeName$ = "SharedCellDefinition"
    Case MBE_SharedCell
        ElemTypeName$ = "SharedCell"
    Case MBE_MultiLine
        ElemTypeName$ = "MultiLine"
    Case MBE_Tag
        ElemTypeName$ = "Tag"
    Case MBE_RasterHeader
        ElemTypeName$ = "RasterHeader"
    Case MBE_RasterComponent
        ElemTypeName$ = "RasterComponent"
    Case MBE_TableEntry         
        ElemTypeName$ = "TableEntry"
    Case MBE_TableHeader                
        ElemTypeName$ = "TableHeader"
    Case MBE_ViewGroup          
        ElemTypeName$ = "ViewGroup"
    Case MBE_View               
        ElemTypeName$ = "View"
    Case MBE_LevelMask          
        ElemTypeName$ = "LevelMask"
    Case MBE_Reference          
        ElemTypeName$ = "Reference"
    Case MBE_MatrixHeader       
        ElemTypeName$ = "MatrixHeader"
    Case MBE_MatrixIntData      
        ElemTypeName$ = "MatrixIntData"
    Case MBE_MatrixDoubleData
        ElemTypeName$ = "MatrixDoubleData"
    Case MBE_MeshHeader         
        ElemTypeName$ = "MeshHeader"
    Case MBE_RefOverride                
        ElemTypeName$ = "RefOverride"
    End Select
    
End Function
'-------------------------------------------------------------
'
'   Show element information
'
'-------------------------------------------------------------
Sub ShowElemInfo (level as Integer, inElem as MbeElement)
    dim range           as MbeRange
    dim origin          as MbePoint
    dim distance        as MbePoint
    dim endPnt          as MbePoint
    dim cellBox()       as MbePoint
    dim points()        as MbePoint
    dim iPoint          as Integer
    dim iMask           as Integer
    dim indent          as Integer
    dim ts$             as String
    dim enterDataFlds() as MbeEDField
    dim numEDFields     as Integer
    dim iEDField        as Integer
    dim area            as Double
    dim perimeter       as Double
    dim volume          as Double
    dim centroid        as MbePoint
    dim filePos1 as long
    dim inElem0 as New MbeElement

    indent = level * 2 + 10
    if test=3 and level=0 then
        open designFile for Append as #1
        print #1,trim$(mystr$);";";mystr1$
        close #1
        test=1
        distance.x = -origin0.x
        distance.y = -origin0.y
        distance.z = 0
        filePos1 = inElem0.fromFile (filePos0)
        stat = inElem0.move (distance)
        stat=inElem0.rewrite()
        mystr$=""
        mystr1$=""
    end if
    If inElem.type = MBE_TextNode then
       test=2
       mystr$=""
       mystr1$=""
    end if
    If inElem.type = MBE_CellHeader then
       test=2
       mystr$=""
       mystr1$=""
    end if
'/////////////test execution
        If inElem.getString (ts$, numEDFields, enterDataFlds) = MBE_Success Then
            if ts$="Douar Ait Haddou" then
               hfhfhgggg=0
            end if
        end if 
'////////////////////
    If inElem.type = MBE_Text and level=1 Then
        If inElem.getString (ts$, numEDFields, enterDataFlds) = MBE_Success Then
            mystr$=mystr$+ts$
        end if 
        if test=2 then
            If inElem.getOrigin (origin) = MBE_Success Then
              mystr1$=trim$(str$(inElem.level)) +";"+trim$(str$(inElem.weight))+";"+inElem.style _
                    +";"+trim$(str$(inElem.color))+";"+trim$(str$(inElem.font))+" ("+inElem.fontName+")" _
                    +";"+trim$(str$(inElem.charHeight))+";"+trim$(str$(inElem.charWidth)) _
                    +";"+trim$(str$(origin.x))+";"+trim$(str$(origin.y))
              origin0.x=origin.x      
              origin0.y=origin.y
            End If
        end if
        test=3
    End If
    If inElem.type = MBE_Text and level=0 Then
        open designFile for Append as #1
        If inElem.getString (ts$, numEDFields, enterDataFlds) = MBE_Success Then
             if len(ts$)=1 then
'               print #1,"*";ts$;";"; inElem.level;";";inElem.weight;";";inElem.style;
'               print #1, ";";inElem.color;";";inElem.font; "(";inElem.fontName;")";
'               print #1, ";";inElem.charHeight;";";inElem.charWidth
'                inElem.color=1 : stat=inElem.rewrite()
             else
              If inElem.getOrigin (origin) = MBE_Success Then
               print #1,trim$(ts$);";"; inElem.level;";";inElem.weight;";";inElem.style;
               print #1, ";";inElem.color;";";inElem.font; "(";inElem.fontName;")";
               print #1, ";";inElem.charHeight;";";inElem.charWidth;";";origin.x;";";origin.y
               inElem.color=0 : stat=inElem.rewrite()
               If inElem.getOrigin (origin) = MBE_Success Then
                     distance.x = -origin.x
                     distance.y = -origin.y
                     distance.z = 0
                     stat = inElem.move (distance)
                     stat=inElem.rewrite()
               end if 
              end if 
            end if  
        End If
        close #1
        test=1
    End If
    
End Sub

'-----------------------------------------------------------------
'
'   Recursive subroutine that processes a simple or complex element
'
'-----------------------------------------------------------------
Sub ProcessElement (nestLevel as Integer, inElem as MbeElement)
    Dim gotNext as Integer

    Do
        ' call function that shows info for element at current position
        Call ShowElemInfo (nestLevel, inELem)

        If inElem.isHeader <> 0 Then
            ' if any components in complex, process them recursively
            If inElem.nextComponent = MBE_Success Then
                Call ProcessElement (nestLevel+1, inElem)
            End If
            gotNext = inElem.nextElement
        Else
            gotNext = inElem.nextComponent
        End If
    Loop While gotNext = MBE_Success
End Sub

'-------------------------------------------------------------
'
'   main - Entry point
'
'-------------------------------------------------------------
Sub main
    Dim element as New MbeElement
    Dim filePos as Long
    Dim startPoint As MbePoint
    Dim point As MbePoint, point2 As MbePoint


    MbeSendCommand "PLACE FENCE ICON "
    startPoint.x = -2679.410000#
    startPoint.y = 4433.561000#
    startPoint.z = 0.000000#
    point.x = startPoint.x
    point.y = startPoint.y
    point.z = startPoint.z
    MbeSendDataPoint point, 1%
    point.x = startPoint.x + 6381.412000#
    point.y = startPoint.y - 7854.045000#
    point.z = startPoint.z
    MbeSendDataPoint point, 1%
    MbeSendCommand "FENCE DELETE "
    MbeSendCommand "LOCK FENCE INSIDE "
    point.x = startPoint.x + 52523.927000#
    point.y = startPoint.y + 6872.290000#
    point.z = startPoint.z
    MbeSendDataPoint point, 1%
    MbeSendCommand "PLACE FENCE ICON "
    MbeSendReset
    MbeSendCommand "PLACE DIALOGTEXT ICON "
    MbeSendAppMessage "TEXTEDIT", "FirstLine Fin"
    point.x = 0
    point.y = 0
    point.z = 0
    MbeSendDataPoint point, 1%
    MbeSendCommand "CHOOSE ELEMENT "
    designFile = mid$(MbeDgnInfo.dgnFileName,1,instr(MbeDgnInfo.dgnFileName,".")-1)+".txt"
    ' read the first element
    filePos = element.fromFile (0)
    test=0
    open designFile for Append as #1
    print #1,"text;Lv;Wt;St;Co;Font;Taille_H;Taille_V; X  ; Y " 
    close #1
    Do While filePos >= 0
        ' show element and nesting level
        Call ProcessElement (0, element)
        filePos0=filePos
        filePos = element.fromFile (filePos + element.fileSize)
    Loop
End Sub
 