MicroStation Python: Curve Stroke and Offset


 

Welcome to the world of MicroStation Python! In this wiki, we'll explore how to search a Curve element by its unique identifier, Element ID and apply stroke and offset functions to it.

 

Curve Stroke and Offset in MicroStation with Python

To get started, refer to the Python Manager wiki for instructions on creating and loading a new Python project. Name your project "CurveOffset.py" and save it in a convenient location. Once created, open the project in your preferred code editor to begin writing your Python script.

 

Here is the complete script.

 

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


# Offset Curve function
def CurveStrokeOffset(eId, offset_val):
    global ACTIVEMODEL
    global dgn_model
    global dgn_file
    global uor

    eeh = EditElementHandle(eId, dgn_model)

    if (eeh == None):
        print ("Not a valid element...")
        return False
    
    #eleType = eeh.GetElementType()
    curve = ICurvePathQuery.ElementToCurveVector(eeh)
    points = DPoint3dArray()
    options = IFacetOptions.CreateForCurves()

    elm_count = 0
    it = iter(curve)
    for primitive in it:
        elm_count += 1
        if (ICurvePrimitive.eCURVE_PRIMITIVE_TYPE_Invalid != 
		    primitive.GetCurvePrimitiveType()):
                    primitive.AddStrokes(points, options)

    curve_elm = CurveVector.CreateLinear(points, CurveVector.eBOUNDARY_TYPE_Open)
    offset_distance = offset_val * uor
    offset = CurveOffsetOptions(offset_distance)
    curve_elm = curve_elm.CloneOffsetCurvesXY(offset)

    line_eeh1 = EditElementHandle()
    if BentleyStatus.eSUCCESS != DraftingElementSchema.ToElement(line_eeh1, 
                                    curve_elm, None, ACTIVEMODEL.Is3d(), 
				    ACTIVEMODEL):
        return False

    # Set Element Properties
    color = 1
    lineWeight = 3
    propertiesSetter = ElementPropertiesSetter()
    propertiesSetter.SetColor(color)
    propertiesSetter.SetWeight(lineWeight)
    propertiesSetter.Apply(line_eeh1)

	# Add the line element to model
    if BentleyStatus.eSUCCESS != line_eeh1.AddToModel():
        return False
    
    return True


# Main function
def main():
    global ACTIVEMODEL
    global dgn_model
    global dgn_file
    global uor

    # Globals
    ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
    dgn_model = ACTIVEMODEL.GetDgnModel()
    dgn_file = dgn_model.GetDgnFile()
    model_info = dgn_model.GetModelInfo()
    uor = model_info.GetUorPerStorage()

    elementId = 1103

    if True != CurveStrokeOffset(elementId, -0.5):
        print("Curve Offset failed...")

    if True != CurveStrokeOffset(elementId, 0.5):
        print("Curve Offset failed...")


# main
if __name__ == "__main__":
    print ("***** Curve Offset *****")
    main()

 

Run/Execute project

Load the project "CurveOffset.py" from the Python Manager dialog and Run/Execute the python script.

Curve element is located by ID and then stroke, offset operations are applied.

 

 

Run the script using the attached DGN file to verify its functionality.

 

File:

CurveSample.dgn

 

Happy coding!

 

 

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