Bentley Communities
Bentley Communities
  • Site
  • User
  • Site
  • Search
  • User
  • Welcome
  • Products
  • Support
  • About
  • More
  • Cancel
MicroStation
  • Product Communities
  • More
MicroStation
MicroStation Wiki Searching and Evaluating Data in a drawing with VBA: Part 3 - Reading Properties of Elements and Exporting to a Text File
    • Sign in

    • -MicroStation Wiki
      • +Learning Tips
      • -MicroStation
        • +3D Printing - MicroStation
        • +Animation - MicroStation
        • +Archive / Backup - MicroStation
        • +Base Geometry - MicroStation
        • +Batch Processing - MicroStation
        • +CONNECT Advisor - MicroStation
        • CONNECT Integration - MicroStation
        • +CONNECTION Client
        • +Cells - MicroStation
        • +Change Tracking - MicroStation
        • +Configuration - MicroStation
        • +Coordinate Systems - MicroStation
        • +Curves - MicroStation
        • +Custom Linestyles - MicroStation
        • +DGN - MicroStation
        • +DWG - MicroStation
        • +Database - MicroStation
        • +Detailing Symbols - MicroStation
        • +Dimensions - MicroStation
        • +Documentation & Help - MicroStation
        • +Drawing Aids - MicroStation
        • +Exception - MicroStation
        • +FAQ - MicroStation
        • +Feature Based Solids Modeling - MicroStation
        • +File Access - MicroStation
        • +GUI - MicroStation
        • +General - MicroStation
        • +Graphics Display - MicroStation
        • +Groups - MicroStation
        • +Hardware / Input Devices - MicroStation
        • +Import/Export - MicroStation
        • +Installation - MicroStation
        • +Interchange - MicroStation
        • +Interference - MicroStation
        • +Internationalization - MicroStation
        • +Levels - MicroStation
        • +Licensing - MicroStation
        • +Manipulation - MicroStation
        • +Markup / Redline - MicroStation
        • +Measure - MicroStation
        • +Mesh - MicroStation
        • +Models - MicroStation
        • +Multi-Lines - MicroStation
        • +OLE - MicroStation
        • +Other - MicroStation
        • +Parametrics - MicroStation
        • +Patterning - MicroStation
        • +Point Cloud - MicroStation
        • +Printing - MicroStation
        • -Programming - MicroStation
          • +General - Programming - MicroStation
          • +Macro Recorder - Programming - MicroStation
          • +MDL - Programming - MicroStation
          • -VBA - Programming - MicroStation
            • Automatic execution when opening or closing drawings
            • Automatic Subroutine Loading
            • Automatically generate plots with VBA
            • Avoiding Inaccuracies in VBA Methods - .FacetSolidAsShapes for SmartSolids
            • Bring All Text to a New Level with VBA
            • Browsing References for Specific Items
            • Calculating the range of Rotated Cells
            • Changing Colors of Levels in VBA
            • Changing display priority for references
            • Changing Element Colors from RGB to Indexed Color
            • Changing Layer Colors from RGB to an Indexed Color
            • Changing the Alignment of All Texts with VBA
            • Changing the display order of elements
            • Changing the presentation order of Layers
            • Changing the Transparency Settings of Levels with VBA
            • Cleaning Property Data with VBA
            • COM Server error
            • Create Engineering (HTML) Links in VBA
            • Creating Coordinates as Latitude and Longitude using VBA Lines
            • Creating VBA Levels - 'Level name is duplicate'
            • Deleting Lines of Length 0 Using VBA
            • Errors Attempting to Load VBA Projects
            • Errors while attempting to load VBA
            • Exporting the RGB Values of the Color Table to a Text File in VBA
            • Forms in an .mvba do not open some pc's
            • How to read the RGB values of the Attached Color Table
            • Keyin to load the VBA Project Manager in MicroStation Connect
            • Leaving a VBA Tools Dialog Box Open
            • Linking to VBA Elements with User Attributes
            • New Text Font Cannot be Assigned
            • Placing annotation cells with VBA
            • Print Organizer Control with VBA
            • Printing all Sheet Models to PDF Using VBA
            • Reading the Length of All Arcs
            • Removing All Object Data from Selected Cells in VBA
            • Replacing Points with Circles Using VBA
            • Replacing Text with VBA - Part 1: Introduction
            • Replacing Text with VBA - Part 2: Complex Structures
            • Running a VBA Routine by Keyin
            • -Searching and Evaluating Data in a drawing with VBA
              • Searching and Evaluating Data in a drawing with VBA: Part 1 - Preparation.
              • Searching and Evaluating Data in a drawing with VBA: Part 10 - Reading Attribute Data
              • Searching and Evaluating Data in a drawing with VBA: Part 2 - Reading properties of Elements
              • Searching and Evaluating Data in a drawing with VBA: Part 3 - Reading Properties of Elements and Exporting to a Text File
              • Searching and Evaluating Data in a drawing with VBA: Part 4 - Saving data to a formatted CSV file
              • Searching and Evaluating Data in a drawing with VBA: Part 5 - Reading More General Properties of Elements
              • Searching and Evaluating Data in a drawing with VBA: Part 6 - Evaluating Simple Complex Elements
              • Searching and Evaluating Data in a drawing with VBA: Part 7 - Evaluating General Complex Elements
              • Searching and Evaluating Data in a drawing with VBA: Part 8 - Evaluating Nested Complex Elements
              • Searching and Evaluating Data in a drawing with VBA: Part 9 - PropertyHandler for Element Analysis
            • Searching and Selecting Text Fields with VBA
            • Updating sequence control with VBA
            • Using a VBA Macro
            • Using the VBA object PropertyHandler to change the Element Information
            • VBA - Interface Error: 0x80040502
            • VBA Error in Execution: Project or Library Not Found
            • What are these "Default.mvba" files?
        • +Project Navigation - MicroStation
        • +Properties - MicroStation
        • +RSS Feeds - MicroStation
        • +Reference - MicroStation
        • +Security - MicroStation
        • +Selection - MicroStation
        • +Settings - MicroStation
        • +Sheet Composition - MicroStation
        • +Solids - MicroStation
        • +Standards - MicroStation
        • +Surfaces - MicroStation
        • +Tables - MicroStation
        • +Text - MicroStation
        • +UI Customization - MicroStation
        • +Units - MicroStation
        • +View - MicroStation
        • +Visualization - MicroStation
        • Welcome Page - MicroStation
        • +Accreditation - MicroStation
        • +i.Models - MicroStation
        • +ProjectWise Integration
        • +Raster - MicroStation
    • +Administration Wiki
    • +Annotations Wiki
    • +Bentley View Wiki
    • +MicroStation PowerDraft
    • +Programming Wiki
    • +Visualization Wiki

     
     Questions about this article, topic, or product? Click here. 

    Searching and Evaluating Data in a drawing with VBA: Part 3 - Reading Properties of Elements and Exporting to a Text File

       
      Applies To 
       
      Product(s): MicroStation
      Version(s): 08.11.09.578
      Environment:  Windows 7 32 bit,Windows 7 64 bit
      Area:  Programming
      Subarea:  VBA
      Original Author: Tristan Anderson, Bentley Technical Support Group
       

     

    In the previous section, we went over reading the properties of elements that we have already selected directly or by using a fence.
    This is a particular interest when running VBA evaluation programs in batch mode on entire series of drawings that we don't want to have to individually open each drawing file to examine the elements.
    There are often certain criteria already set such as element type, lines, level information, font used in texts, etc.

    "Element Scan Criteria" by which you can filter search results of data are also helpful. We can run the following variable for establishing such criteria:

    Dim Sc As New ElementScanCriteria

    This tool is very powerful and offers a variety of ways that the VBA editor helps us with this functionality by proposing us possible criteria.

    After defining the element type for 'Sc', simply enter Sc followed by a period in the following line, and a new drop-down menu will appear with many options:

    We see a long list of options that add or remove certain features (Include or Exclude). We control the criteria by which is selected. 
    The best way to illustrate this is with an example. We are guided by the previous example to only select lines. We will accomplish this in two steps:

    1. We will exclude all types of items out of the search:
     
     Sc.ExcludeAllTypes

    2. We add only the lines for a search:

     Sc.IncludeType msdElementTypeLine

    When selecting the correct element, typing VBA helps us again with a large drop-down menu with all possible reference numbers:

    Generally, the VBA provides selection options for all objects such as properties, methods, or additional parameters.

    If the terms offered are not very clear, the Help center is there for additional explanations and help. If you choose such a concept, clicking the word with the cursor will bring up the help menu open to the selected word, which provides many topics and additional examples. 
    By this definition, Sc now includes a filter definition. Only elements of type "Line" are permitted. This filter will scan the drawing and select those elements that fit the filter criteria.
    In the "Do While" loop, we set the query to only check for a line, as that is the filter we have chosen. This is shown in the following code:

    Sub  elementinfo ()
    Dim ele As LineElement
    Dim Ee As  ElementEnumerator
    Dim startpoint, endpoint As  Point3D
    Dim Sc As New ElementScanCriteria
    Sc.ExcludeAllTypes
    Sc.IncludeType msdElementTypeLine
    Set Ee = ActiveModelReference.GraphicalElementCache.scan(Sc)
    Do While Ee.MoveNext
            Set ele = Ee.Current.AsLineElement
            startpunkt = ele.startPoint
            Endpoint = ele.EndPoint
            Debug.Print "Startpoint is (xyz): " & startpoint.X, startpoint.Y, startpoint.Z
            Debug.Print "endpoint is (xyz):"  & endpoint.X, endpoint.Y, endpoint.Z
    Loop
    End Sub

    It is easily seen that the amount of output data can potentially be immense, and the Immediate window is no longer sufficient. 
    This can easily be fixed but redirecting the data into a text file. 
    To do this, we will open a file at the beginning, and close the file at the end.
    In the middle, we will forward the data into the first opened file instead of using debug.print.
    The following lines will be added to the program:

    file = ActiveDesignFile.FullName + "- data.txt"
    Open file For Output As #1
    ......
    Print #1, “text”
    ......
    Close #1

    In the end, the program will look like this:

    Sub elementinfo ()
    Dim ele As LineElement
    Dim Ee As ElementEnumerator
    Dim startpoint, endpoint As  Point3D
    Dim Sc As New ElementScanCriteria
    Sc.ExcludeAllTypes
    Sc.IncludeType msdElementTypeLine
    Dim file As String
    file = ActiveDesignFile.FullName + "- data.txt"
    Open file For Output As #1
    Set Ee = ActiveModelReference.GraphicalElementCache.Scan(Sc)
    Do While Ee.MoveNext
            Set ele = Ee.Current.AsLineElement
            startpoint = ele.startPoint
            Endpoint = ele.EndPoint
            Print #1, "Startpoint is (xyz): " & startpoint.X, startpoint.Y, startpoint.Z
            Print #1, "is the end point (xyz):"  & endpoint.X, endpoint.Y, endpoint.Z
    Loop
    Close #1
    End Sub

    The informal term ActiveDesignFile.FullName is the path and name of the open DGN file. The text file we are using to draw the data is easy to find because it is in the same directory as the original drawing. The Expression "For Output as #1" creates a new file if one is already present. If there is already an existing file, it will be overwritten with the new data. 
    If you want to append the new data at the end of the existing file, the following line must be added:

    Open file For Append As #1

    <<  RETURN TO PART 2 <<             >> CONTINUE ON TO PART 4 >>

    • VBAWindows 7 32 bit
    • MicroStation
    • Windows 7 64 bit
    • Programming
    • en
    • 08.11.09.578
    • SELECTsupport
    • Share
    • History
    • More
    • Cancel
    • Tristan Anderson Created by Bentley Colleague Tristan Anderson
    • When: Mon, Jun 22 2015 12:48 PM
    • Tristan Anderson Last revision by Bentley Colleague Tristan Anderson
    • When: Mon, Jun 22 2015 1:04 PM
    • Revisions: 3
    • Comments: 0
    Recommended
    Related
    Communities
    • Home
    • Getting Started
    • Community Central
    • Products
    • Support
    • Secure File Upload
    • Feedback
    Support and Services
    • Home
    • Product Support
    • Downloads
    • Subscription Services Portal
    Training and Learning
    • Home
    • About Bentley Institute
    • My Learning History
    • Reference Books
    Social Media
    •    LinkedIn
    •    Facebook
    •    Twitter
    •    YouTube
    •    RSS Feed
    •    Email

    © 2021 Bentley Systems, Incorporated  |  Contact Us  |  Privacy |  Terms of Use  |  Cookies