MicroStation Python: GCS Attached to Model


 

Welcome to the world of MicroStation Python! Understanding the Geographic Coordinate System (GCS) associated with your MicroStation model is crucial for accurate data analysis and processing. This wiki will guide you through extracting and exploring GCS details using Python scripting.

 

GCS Attached to Model

Let us dive into creating and extracting GCS data attached to a Model. Refer to the Python Manager wiki to create and load a python project. Name your project as “GCSInfo.py” and save it to your preferred directory. Open the project in the editor for writing your Python script.

 

Import Modules

The script begins by importing several modules offering functions and classes to interact with MicroStation and to create geometric elements.

from MSPyBentley import *
from MSPyBentleyGeom import *
from MSPyECObjects import *
from MSPyDgnPlatform import *
from MSPyDgnView import *
from MSPyMstnPlatform import *

 

The Function

The main function of this script is GetGCSInformation().

def GetGCSInformation():
    ...

 

Let us break down its steps:

 

  1. Active Model

 

A MicroStation session may access many models. The following script gets the Active Model Reference from a session.

 

    ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
    if ACTIVEMODEL is None:
        return False

 

  1. DGN GCS

 

Next, it attempts to obtain the DgnGCS object, associated with the active model. If successful, it prints various GCS properties like name, description, projection, EPSG code, datum information, and EPSG datum code.

 

DgnGCS object can be written to and read from a model. Internally, whenever a request is made for DgnGCS corresponding to a particular model, the GCS is retrieved from the file and associated with that model, so subsequent requests for that GCS are very efficient. For a comprehensive list of available DgnGCS parameters and their extraction methods, refer to the documentation, MicroStation Python API | DgnGCS.

 

 

    dgn_gcs = DgnGCS.FromModel (ACTIVEMODEL, True)
    if (dgn_gcs is None):
        return False

 

Putting it all Together: The main Function

The main function simply calls the GetGCSInformation() function and checks its return value.

Here is the complete script.

 

from MSPyBentley import *
from MSPyBentleyGeom import *
from MSPyECObjects import *
from MSPyDgnPlatform import *
from MSPyDgnView import *
from MSPyMstnPlatform import *


# GetGCSInformation function
def GetGCSInformation():
    # Get GCS attached to model
    ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
    if ACTIVEMODEL is None:
        return False

    # Get GCS attached to model
    dgn_gcs = DgnGCS.FromModel (ACTIVEMODEL, True)
    if (dgn_gcs is None):
        return False
    else:
        print ("Name: " + dgn_gcs.GetName())
        print ("Description: " + dgn_gcs.GetDescription())
        print ("Projection: " + dgn_gcs.GetProjection())
        print ("EPSG Code: " + str(dgn_gcs.GetEPSGCode()))

        print ("Datum Name:: " + dgn_gcs.GetDatumName())
        print ("Datum Description: " + dgn_gcs.GetDatumDescription())
        print ("Datum Code: " + str(dgn_gcs.GetDatumCode()))

        print ("EPSG Datum Code: " + str(dgn_gcs.GetEPSGDatumCode()))

    return True


# Main function
def main():
    print ("GCS Information")
    print ("---------------")

    if True != GetGCSInformation():
        print("No GCS attached...")


# main
if __name__ == "__main__":
    main()

 

Run/Execute project

Select project “GCSInfo.py” from the Python Manager dialog and Run/Execute the python script.

If GCS, is attached to the model, “MicroStation - Text Window” displays GCS properties like name, description, projection, EPSG code, datum information, and EPSG datum code.

 

A screenshot of a computerDescription automatically generated

 

Extend the above code to create a custom GCS and attach it to the Model. Refer to the delivered examples and documentation.

 

Happy coding!

 

 

Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs