Welcome to the world of MicroStation Python! In our previous wikis, we explored how to create simple elements in MicroStation using Python. This time, we will dive into creating Complex elements. Additionally, we will set Element Properties (Symbology), including the Fill property, for the Complex Shape element.
Let us dive into creating Complex elements such as Complex Chain and Complex Shape using Python. Refer to the Python Manager wiki to create and load a python project. Name your project as "CreateComplexElement.py" and save it to your preferred directory. Open the project in the editor for writing your Python script.
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 CreateComplexChainElement() function creates a complex chain element and includes error checking to ensure the element is successfully created and added to the model.
It begins by creating a Chain Header using the ChainHeaderHandler.CreateChainHeaderElement() method.
Note the 3rd parameter isClosed: is set to False for a CMPLXSTRINGELM.
Next multiple line elements are created using the LineHandler.CreateLineElement() method and is added to a chain header using the ChainHeaderHandler.AddComponentElement() method.
Update the chain elements range once all components have been added using the ChainHeaderHandler.AddComponentComplete() method.
def CreateComplexChainElement(): global ACTIVEMODEL cc_eeh = EditElementHandle() NUM_LINES = 3 lines0 = EditElementHandle() lines1 = EditElementHandle() lines2 = EditElementHandle() Lines = [lines0, lines1, lines2] m_points = [[DPoint3d() for i in range(2)] for j in range(NUM_LINES)] # Create Lines and add to Chain Header ChainHeaderHandler.CreateChainHeaderElement(cc_eeh, None, False, ACTIVEMODEL.Is3d(), ACTIVEMODEL) for j in range(0, NUM_LINES): if (j == 0): m_points[j][0].x = 0; m_points[j][0].y = 0 m_points[j][1].x = 0; m_points[j][1].y = 5000 if (j == 1): m_points[j][0].x = 0; m_points[j][0].y = 5000 m_points[j][1].x = 5000; m_points[j][1].y = 5000 if (j == 2): m_points[j][0].x = 5000; m_points[j][0].y = 5000 m_points[j][1].x = 5000; m_points[j][1].y = 0 pt1 = DPoint3d(m_points[j][0].x, m_points[j][0].y, m_points[j][0].z) pt2 = DPoint3d(m_points[j][1].x, m_points[j][1].y, m_points[j][1].z) seg = DSegment3d(pt1, pt2) LineHandler.CreateLineElement(Lines[j], None, seg, ACTIVEMODEL.Is3d(), ACTIVEMODEL) ChainHeaderHandler.AddComponentElement (cc_eeh, Lines[j]) # Update Complex Chain element range ChainHeaderHandler.AddComponentComplete(cc_eeh) # Add the Complex Chain element to model if BentleyStatus.eSUCCESS != cc_eeh.AddToModel(): return False return True
Following script sets the Properties (Symbology) of the Complex Chain element, including color and weight.
# Set Element Properties (Symbology) to Element color = 100 weight = 2 propertiesSetter = ElementPropertiesSetter() propertiesSetter.SetColor(color) propertiesSetter.SetWeight(weight) propertiesSetter.Apply(cc_eeh)
The CreateComplexShapeElement() function takes base point as a parameter and creates a complex shape element. Function includes error checking to ensure the element is successfully created and added to the model.
It begins by creating Arc, Line String elements using the ArcHandler.CreateArcElement(), LineStringHandler.CreateLineStringElement() methods.
Next a Chain Header is created using the ChainHeaderHandler.CreateChainHeaderElement() method.
Note the 3rd parameter isClosed: is set to True for a CMPLXSHAPEELM.
Arc, Line String element is then added to the chain header using the ChainHeaderHandler.AddComponentElement() method.
Update the chain elements range once all components have been added using the ChainHeaderHandler.AddComponentComplete() method.
def CreateComplexShapeElement (basePt): global g_1mu global ACTIVEMODEL a_eeh = EditElementHandle() l_eeh = EditElementHandle() cs_eeh = EditElementHandle() # Create Arc Element pt1 = DPoint3d() pt2 = DPoint3d() pt3 = DPoint3d() pt1.x = basePt.x pt1.y = basePt.y pt1.z = 0.0 pt2.x = g_1mu*1.3 pt2.y = + g_1mu*0.7 pt2.z = 0.0 pt3.x = basePt.x + g_1mu pt3.y = basePt.y + g_1mu pt3.z = basePt.z el3d = DEllipse3d.FromPointsOnArc(pt3, pt2, pt1) status = ArcHandler.CreateArcElement(a_eeh, None, el3d, ACTIVEMODEL.Is3d(), ACTIVEMODEL) if BentleyStatus.eSUCCESS != status: return False # Create LineString Element pt2.x = pt1.x + g_1mu pt2.y = pt1.y points = DPoint3dArray() points.append(pt1) points.append(pt2) points.append(pt3) status = LineStringHandler.CreateLineStringElement(l_eeh, None, points, ACTIVEMODEL.Is3d(), ACTIVEMODEL) if BentleyStatus.eSUCCESS != status: return False
# Create Complex Shape Header Element
ChainHeaderHandler.CreateChainHeaderElement(cs_eeh, None, True, ACTIVEMODEL.Is3d(), ACTIVEMODEL)
ChainHeaderHandler.AddComponentElement (cs_eeh, a_eeh)
ChainHeaderHandler.AddComponentElement (cs_eeh, l_eeh) ChainHeaderHandler.AddComponentComplete(cs_eeh) # Set color, weight to the created element color = 100 weight = 2 propertiesSetter = ElementPropertiesSetter() propertiesSetter.SetColor(color) propertiesSetter.SetWeight(weight) propertiesSetter.Apply(cs_eeh) # Add and Set fill color fill_color = 31 IAreaFillPropertiesEdit = cs_eeh.GetHandler() IAreaFillPropertiesEdit.AddSolidFill(cs_eeh, fill_color, True) # Add the Complex Shape element to model if BentleyStatus.eSUCCESS != cs_eeh.AddToModel(): return False return True
Following script sets Properties (Symbology) of the Complex Shape element, including color and weight.
# Set color, weight to the created element color = 100 weight = 2 propertiesSetter = ElementPropertiesSetter() propertiesSetter.SetColor(color) propertiesSetter.SetWeight(weight) propertiesSetter.Apply(cs_eeh)
Following script sets a Fill property to the closed Complex Shape element.
# Add and Set fill color fill_color = 31 IAreaFillPropertiesEdit = cs_eeh.GetHandler() IAreaFillPropertiesEdit.AddSolidFill(cs_eeh, fill_color, True)
The main function initializes the global variables and calls the functions to create the complex elements.
Here is the complete script.
from MSPyBentley import * from MSPyBentleyGeom import * from MSPyECObjects import * from MSPyDgnPlatform import * from MSPyDgnView import * from MSPyMstnPlatform import * # Create Complex Shape element def CreateComplexShapeElement (basePt): global g_1mu global ACTIVEMODEL a_eeh = EditElementHandle() l_eeh = EditElementHandle() cs_eeh = EditElementHandle() # Create Arc Element pt1 = DPoint3d() pt2 = DPoint3d() pt3 = DPoint3d() pt1.x = basePt.x pt1.y = basePt.y pt1.z = 0.0 pt2.x = g_1mu*1.3 pt2.y = + g_1mu*0.7 pt2.z = 0.0 pt3.x = basePt.x + g_1mu pt3.y = basePt.y + g_1mu pt3.z = basePt.z el3d = DEllipse3d.FromPointsOnArc(pt3, pt2, pt1) status = ArcHandler.CreateArcElement(a_eeh, None, el3d, ACTIVEMODEL.Is3d(), ACTIVEMODEL) if BentleyStatus.eSUCCESS != status: return False # Create LineString Element pt2.x = pt1.x + g_1mu pt2.y = pt1.y points = DPoint3dArray() points.append(pt1) points.append(pt2) points.append(pt3) status = LineStringHandler.CreateLineStringElement(l_eeh, None, points, ACTIVEMODEL.Is3d(), ACTIVEMODEL) if BentleyStatus.eSUCCESS != status: return False # Create Complex Shape Header Element
ChainHeaderHandler.CreateChainHeaderElement(cs_eeh, None, True, ACTIVEMODEL.Is3d(), ACTIVEMODEL)
ChainHeaderHandler.AddComponentElement (cs_eeh, a_eeh)
ChainHeaderHandler.AddComponentElement (cs_eeh, l_eeh)
ChainHeaderHandler.AddComponentComplete(cs_eeh)
# Set color, weight to the created element color = 100 weight = 2 propertiesSetter = ElementPropertiesSetter() propertiesSetter.SetColor(color) propertiesSetter.SetWeight(weight) propertiesSetter.Apply(cs_eeh) # Add and Set fill color fill_color = 31 IAreaFillPropertiesEdit = cs_eeh.GetHandler() IAreaFillPropertiesEdit.AddSolidFill(cs_eeh, fill_color, True) # Add the Complex Shape element to model if BentleyStatus.eSUCCESS != cs_eeh.AddToModel(): return False return True # Create Complex Chain element def CreateComplexChainElement(): global ACTIVEMODEL cc_eeh = EditElementHandle() NUM_LINES = 3 lines0 = EditElementHandle() lines1 = EditElementHandle() lines2 = EditElementHandle() Lines = [lines0, lines1, lines2] m_points = [[DPoint3d() for i in range(2)] for j in range(NUM_LINES)] # Create Lines and add to Chain Header ChainHeaderHandler.CreateChainHeaderElement(cc_eeh, None, False, ACTIVEMODEL.Is3d(), ACTIVEMODEL) for j in range(0, NUM_LINES): if (j == 0): m_points[j][0].x = 0; m_points[j][0].y = 0 m_points[j][1].x = 0; m_points[j][1].y = 5000 if (j == 1): m_points[j][0].x = 0; m_points[j][0].y = 5000 m_points[j][1].x = 5000; m_points[j][1].y = 5000 if (j == 2): m_points[j][0].x = 5000; m_points[j][0].y = 5000 m_points[j][1].x = 5000; m_points[j][1].y = 0 pt1 = DPoint3d(m_points[j][0].x, m_points[j][0].y, m_points[j][0].z) pt2 = DPoint3d(m_points[j][1].x, m_points[j][1].y, m_points[j][1].z) seg = DSegment3d(pt1, pt2) LineHandler.CreateLineElement(Lines[j], None, seg, ACTIVEMODEL.Is3d(), ACTIVEMODEL) ChainHeaderHandler.AddComponentElement (cc_eeh, Lines[j]) # Update Complex Chain element range ChainHeaderHandler.AddComponentComplete(cc_eeh) # Set Element Properties (Symbology) to Element color = 100 weight = 2 propertiesSetter = ElementPropertiesSetter() propertiesSetter.SetColor(color) propertiesSetter.SetWeight(weight) propertiesSetter.Apply(cc_eeh) # Add the Complex Chain element to model if BentleyStatus.eSUCCESS != cc_eeh.AddToModel(): return False return True def main(): # Global Variables global g_1mu global ACTIVEMODEL # Get the active DGN model reference ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef dgnModel = ACTIVEMODEL.GetDgnModel() modelInfo = dgnModel.GetModelInfo() g_1mu = modelInfo.GetUorPerStorage() # Create Complex Chain element if True != CreateComplexChainElement(): print("Complex Chain creation failed...") # Create Complex Shape element if True != CreateComplexShapeElement(DPoint3d(g_1mu, 0, 0)): print("Complex Shape creation failed...") PyCadInputQueue.SendKeyin("FIT VIEW EXTENDED") #main if __name__ == "__main__": print ("Create Complex Elements...") main()
Select project "CreateComplexElement.py" from the Python Manager dialog and Run/Execute the python script.
Voila! A complex string and a complex shape element are successfully created and added to your active MicroStation model.
Enhance the above code to add, register key-ins within MicroStation for the above Create complex chain and complex shape functions. Refer to the delivered examples and documentation.
Happy coding!
Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs