Welcome to the world of MicroStation Python! In our earlier wiki, we explored how to create basic geometric elements. In this wiki, we will delve into creating 3D geometric elements in MicroStation using Python.
Let us dive into creating 3D geometric elements such as Solid, Surface, Slab, and Cone using Python. Refer to the Python Manager wiki to create and load a python project. Name your project as "Create3DElements.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 *
Create a Solid Element
The CreateASolidElement() function takes a base point as input, which defines the initial location of the Solid element. Let us walk through the steps to create a Solid element. The script includes error checking to ensure the Solid element is successfully created and added to the model.
def CreateASolidElement (basePt): global ACTIVEMODEL global dgnModel global g_1mu 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)
# Create Solid Element solid_eeh = SolidUtil.Convert.ElementToBody(cs_eeh, True, True, False) frontDistance = g_1mu * 1 ret = SolidUtil.Modify.ThickenSheet(solid_eeh[1], frontDistance, 0) if BentleyStatus.eSUCCESS == ret: newElement = EditElementHandle() ret1 = SolidUtil.Convert.BodyToElement(newElement,solid_eeh[1],cs_eeh,dgnModel) if(BentleyStatus.eSUCCESS == ret1): # Set Symbology color = 6 propertiesSetter = ElementPropertiesSetter() propertiesSetter.SetColor(color) propertiesSetter.Apply(newElement) newElement.AddToModel() return True return False
Create a Surface Element
The CreateASurfaceElement() function takes a base point as input, which defines the initial location of the Surface element. Let us walk through the steps to create a Surface element. The script includes error checking to ensure the Surface element is successfully created and added to the model.
def createASurfaceElement (basePoint): p_eeh = EditElementHandle() s_eeh = EditElementHandle() # Define Line String coordinates pt0 = basePoint pt1 = DPoint3d (0, 0, 0) pt2 = DPoint3d (0, 0, 0) pt3 = DPoint3d (0, 0, 0) pt4 = DPoint3d (0, 0, 0) pt5 = DPoint3d (0, 0, 0) # Coordinates of Line String element pt1.x = pt0.x; pt1.y = pt0.y - g_1mu/2; pt1.z = pt0.z; pt2.x = pt1.x + g_1mu/2; pt2.y = pt1.y; pt2.z = pt0.z; pt3.x = pt2.x; pt3.y = pt2.y - g_1mu/2; pt3.z = pt0.z; pt4.x = pt3.x + g_1mu/2; pt4.y = pt3.y; pt4.z = pt0.z; pt5.x = pt4.x; pt5.y = pt0.y; pt5.z = pt0.z; # Add coordinates to DPoint3d array linePts = DPoint3dArray() linePts.append(pt0) linePts.append(pt1) linePts.append(pt2) linePts.append(pt3) linePts.append(pt4) linePts.append(pt5) linePts.append(pt0) # Create Line String element status = LineStringHandler.CreateLineStringElement(p_eeh, None, linePts, ACTIVEMODEL.Is3d(), ACTIVEMODEL) if BentleyStatus.eSUCCESS != status: return False # Define extrude point and vector pt11 = pt0 pt12 = DVec3d(0, 0, g_1mu) # Create Surface from Line element status1 = SurfaceHandler.CreateProjectionElement(s_eeh, None, p_eeh, pt11, pt12, None, False, ACTIVEMODEL) if BentleyStatus.eSUCCESS != status1: return False # Set Symbology color = 143 propertiesSetter = ElementPropertiesSetter() propertiesSetter.SetColor(color) propertiesSetter.Apply(s_eeh) # Add the surface element to model if BentleyStatus.eSUCCESS != s_eeh.AddToModel(): return False return True
Create a Slab Element
The createASlabElement() function takes a base point as input, which defines the initial location of the Slab element. Let us walk through the steps to create a Slab element. The script includes error checking to ensure the Slab element is successfully created and added to the model.
def createASlabElement(basePoint): global ACTIVEMODEL global dgnModel global g_1mu shape_eeh = EditElementHandle() # Set Shape coordinates offset = 1.0 * g_1mu points = DPoint3dArray() points.append (DPoint3d (basePoint.x, basePoint.y, 0.0)) points.append (DPoint3d (basePoint.x + offset, basePoint.y, 0.0)) points.append (DPoint3d (basePoint.x + offset, basePoint.y + offset, 0.0)) points.append (DPoint3d (basePoint.x, basePoint.y + offset, 0.0)) points.append (DPoint3d (basePoint.x, basePoint.y, 0.0)) # Create Shape element status = ShapeHandler.CreateShapeElement (shape_eeh, None, points, ACTIVEMODEL.Is3d(), ACTIVEMODEL) if BentleyStatus.eSUCCESS != status: return False # Create Slab Element solid_eeh = SolidUtil.Convert.ElementToBody(shape_eeh, True, True, False) frontDistance = 0.1 * g_1mu ret = SolidUtil.Modify.ThickenSheet(solid_eeh[1], frontDistance, 0) if BentleyStatus.eSUCCESS == ret: newElement = EditElementHandle() ret1 = SolidUtil.Convert.BodyToElement (newElement,solid_eeh[1],shape_eeh, dgnModel) if(BentleyStatus.eSUCCESS == ret1): # Set Symbology color = 8 propertiesSetter = ElementPropertiesSetter() propertiesSetter.SetColor(color) propertiesSetter.Apply(newElement) newElement.AddToModel() return True return False
Create a Cone Element
The CreateAConeElement() function takes a base point as input, which defines the initial location of the Cone element. Let us walk through the steps to create a Cone element. The script includes error checking to ensure the Cone element is successfully created and added to the model.
def CreateAConeElement (basePoint): topPt = DPoint3d(0, 0, 0) topPt.x = basePoint.x topPt.y = basePoint.y topPt.z += 2.3*g_1mu/2 # Get rotation matrix from angle rotMat = RotMatrix.FromAxisAndRotationAngle(0, 0.0) # Handle to Cone element c_eeh = EditElementHandle() # Create Cone element status = ConeHandler.CreateConeElement(c_eeh, None, 0.25*g_1mu/2, 0.8*g_1mu/2, topPt, basePoint, rotMat, True, ACTIVEMODEL) if BentleyStatus.eSUCCESS != status: return False # Set Symbology color = 3 propertiesSetter = ElementPropertiesSetter() propertiesSetter.SetColor(color) propertiesSetter.Apply(c_eeh) # Add Cone element to model if BentleyStatus.eSUCCESS != c_eeh.AddToModel(): return False return True
Putting it all Together: The main Function
The main function initializes the active model and calls the functions to create the 3D geometric 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 Solid Element def CreateASolidElement (basePt): global ACTIVEMODEL global dgnModel global g_1mu 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)
# Create Solid Element solid_eeh = SolidUtil.Convert.ElementToBody(cs_eeh, True, True, False) frontDistance = g_1mu * 1 ret = SolidUtil.Modify.ThickenSheet(solid_eeh[1], frontDistance, 0) if BentleyStatus.eSUCCESS == ret: newElement = EditElementHandle() ret1 = SolidUtil.Convert.BodyToElement(newElement,solid_eeh[1],cs_eeh,dgnModel) if(BentleyStatus.eSUCCESS == ret1): # Set Symbology color = 6 propertiesSetter = ElementPropertiesSetter() propertiesSetter.SetColor(color) propertiesSetter.Apply(newElement) newElement.AddToModel() return True return False # Create Surface Element def createASurfaceElement (basePoint): p_eeh = EditElementHandle() s_eeh = EditElementHandle() # Define Line String coordinates pt0 = basePoint pt1 = DPoint3d (0, 0, 0) pt2 = DPoint3d (0, 0, 0) pt3 = DPoint3d (0, 0, 0) pt4 = DPoint3d (0, 0, 0) pt5 = DPoint3d (0, 0, 0) # Coordinates of Line String element pt1.x = pt0.x; pt1.y = pt0.y - g_1mu/2; pt1.z = pt0.z; pt2.x = pt1.x + g_1mu/2; pt2.y = pt1.y; pt2.z = pt0.z; pt3.x = pt2.x; pt3.y = pt2.y - g_1mu/2; pt3.z = pt0.z; pt4.x = pt3.x + g_1mu/2; pt4.y = pt3.y; pt4.z = pt0.z; pt5.x = pt4.x; pt5.y = pt0.y; pt5.z = pt0.z; # Add coordinates to DPoint3d array linePts = DPoint3dArray() linePts.append(pt0) linePts.append(pt1) linePts.append(pt2) linePts.append(pt3) linePts.append(pt4) linePts.append(pt5) linePts.append(pt0) # Create Line String element status = LineStringHandler.CreateLineStringElement(p_eeh, None, linePts, ACTIVEMODEL.Is3d(), ACTIVEMODEL) if BentleyStatus.eSUCCESS != status: return False # Define extrude point and vector pt11 = pt0 pt12 = DVec3d(0, 0, g_1mu) # Create Surface from Line element status1 = SurfaceHandler.CreateProjectionElement(s_eeh, None, p_eeh, pt11, pt12, None, False, ACTIVEMODEL) if BentleyStatus.eSUCCESS != status1: return False # Set Symbology color = 143 propertiesSetter = ElementPropertiesSetter() propertiesSetter.SetColor(color) propertiesSetter.Apply(s_eeh) # Add the surface element to model if BentleyStatus.eSUCCESS != s_eeh.AddToModel(): return False return True # Create Slab Element def createASlabElement(basePoint): global ACTIVEMODEL global dgnModel global g_1mu shape_eeh = EditElementHandle() # Set Shape coordinates offset = 1.0 * g_1mu points = DPoint3dArray() points.append (DPoint3d (basePoint.x, basePoint.y, 0.0)) points.append (DPoint3d (basePoint.x + offset, basePoint.y, 0.0)) points.append (DPoint3d (basePoint.x + offset, basePoint.y + offset, 0.0)) points.append (DPoint3d (basePoint.x, basePoint.y + offset, 0.0)) points.append (DPoint3d (basePoint.x, basePoint.y, 0.0)) # Create Shape element status = ShapeHandler.CreateShapeElement (shape_eeh, None, points, ACTIVEMODEL.Is3d(), ACTIVEMODEL) if BentleyStatus.eSUCCESS != status: return False # Create Slab Element solid_eeh = SolidUtil.Convert.ElementToBody(shape_eeh, True, True, False) frontDistance = 0.1 * g_1mu ret = SolidUtil.Modify.ThickenSheet(solid_eeh[1], frontDistance, 0) if BentleyStatus.eSUCCESS == ret: newElement = EditElementHandle() ret1 = SolidUtil.Convert.BodyToElement (newElement,solid_eeh[1],shape_eeh, dgnModel) if(BentleyStatus.eSUCCESS == ret1): # Set Symbology color = 8 propertiesSetter = ElementPropertiesSetter() propertiesSetter.SetColor(color) propertiesSetter.Apply(newElement) newElement.AddToModel() return True return False # Create Cone Element def CreateAConeElement (basePoint): topPt = DPoint3d(0, 0, 0) topPt.x = basePoint.x topPt.y = basePoint.y topPt.z += 2.3*g_1mu/2 # Get rotation matrix from angle rotMat = RotMatrix.FromAxisAndRotationAngle(0, 0.0) # Handle to Cone element c_eeh = EditElementHandle() # Create Cone element status = ConeHandler.CreateConeElement(c_eeh, None, 0.25*g_1mu/2, 0.8*g_1mu/2, topPt, basePoint, rotMat, True, ACTIVEMODEL) if BentleyStatus.eSUCCESS != status: return False # Set Symbology color = 3 propertiesSetter = ElementPropertiesSetter() propertiesSetter.SetColor(color) propertiesSetter.Apply(c_eeh) # Add Cone element to model if BentleyStatus.eSUCCESS != c_eeh.AddToModel(): return False return True # Main function def main(): print ("3D Elements...") global g_1mu global ACTIVEMODEL global dgnModel ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef dgnModel = ACTIVEMODEL.GetDgnModel() modelInfo = dgnModel.GetModelInfo() g_1mu = modelInfo.GetUorPerStorage() # Create a Solid element basePt = DPoint3d.FromZero() if True != CreateASolidElement(DPoint3d(g_1mu, 0, 0)): print("Create Solid Element failed...") # Create a Surface element basePt.x += g_1mu*2.0 basePt.y -= g_1mu*1.0 basePt.z -= g_1mu*0.0 if True != createASurfaceElement (basePt): print("Create Surface Element failed...") # Create a Surface element basePt.x += g_1mu*1.0 basePt.y -= g_1mu*3.0 basePt.z -= g_1mu*0.0 if True != createASlabElement(basePt): print("Create Slab Element failed...") # Create a Surface element basePt.x += g_1mu*0.5 basePt.y += g_1mu*0.5 basePt.z += g_1mu*0.1 if True != CreateAConeElement (basePt): print("Create Cone Element failed...") # main if __name__ == "__main__": main()
Select project "Create3DElements.py" from the Python Manager dialog and Run/Execute the python script.
Voila! 3D elements are successfully created and added to your active MicroStation model.
Explore the examples, documentation and challenge yourself to create other 3D geometries.
Happy coding!
Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs