Welcome to the world of MicroStation Python! This guide will walk you through the process of placing cells using COM. We'll cover essential techniques like attaching cell libraries, Place cell utilizing COM functions such as CreateCellElement2, CreateCellElement3 and accessing the last placed element.
To get started, refer to the Python Manager wiki for instructions on creating and loading a new Python project. Name your project "PlaceCellFromLib.py" and save it in a convenient location. Once created, open the project in your preferred code editor to begin writing your Python script.
We will leverage win32com python library that allows you to interact with Windows COM (Component Object Model) objects. COM is a Microsoft technology that enables software components to communicate with each other, regardless of the programming language they're written in.
Here is the complete script.
import math from MSPyBentley import * from MSPyBentleyGeom import * from MSPyECObjects import * from MSPyDgnPlatform import * from MSPyDgnView import * from MSPyMstnPlatform import * import win32com.client as win32 # Get First, Last graphical element from Active Model def GetFirstLastElementIdFromModel(): global ACTIVEMODEL global dgn_model #Get all graphical elements from the model graphicalElements = dgn_model.GetGraphicElements() #elmRefs = iter(graphicalElements) count = 0 for elmRef in graphicalElements: elementId = elmRef.GetElementId() if (count == 0): first_element_id = elementId else: last_element_id = elementId count += 1 if (count > 0): print(f"Total graphical elements in model: {count}") first_eeh = EditElementHandle(first_element_id, dgn_model) print (f"First Element ID: {first_element_id} Element Type: {first_eeh.ElementType}") last_eeh = EditElementHandle(last_element_id, dgn_model) print (f"Last Element ID: {last_element_id} Element Type: {last_eeh.ElementType}") return # PlaceCell3 - COM def PlaceCell3(cellName): global msApp startPt = win32.Record ('Point3d', msApp) startPt.X = 10.0 startPt.Y = 5.0 startPt.Z = 0.0 cellElm = msApp.CreateCellElement3 (cellName, startPt, True) msApp.ActiveModelReference.AddElement (cellElm[0]) # PlaceCell2- COM def PlaceCell2(cellName): global msApp cellOrigin = msApp.Point3dFromXYZ(5, 5, 0) cellScale = msApp.Point3dFromXYZ(1, 1, 1) cellRotation = msApp.Matrix3dFromAxisAndRotationAngle(2, math.radians(45)) #cellElm = msApp.CreateCellElement2 (cellName, cellOrigin, cellScale, True, msApp.Matrix3dIdentity()) cellElm = msApp.CreateCellElement2 (cellName, cellOrigin, cellScale, True, cellRotation) msApp.ActiveModelReference.AddElement (cellElm[0]) # Attach Cell Library def AttachCellLib(cellFile): global msApp msApp.AttachCellLibrary (cellFile) # Main function def main(): global ACTIVEMODEL global dgn_model global msApp ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef dgn_model = ACTIVEMODEL.GetDgnModel() msApp = win32.Dispatch('MicrostationDGN.Application') cell_library_folder = "C:/Program Files/Bentley/MicroStation 2024/MicroStation/Default/Cell/" cell_library_name = "remodel.cel" cell_file = cell_library_folder + cell_library_name cell_name = "TABLE" AttachCellLib(cell_file) PlaceCell2(cell_name) PlaceCell3(cell_name) GetFirstLastElementIdFromModel() # Main if __name__ == "__main__": print ("***** Place Cell COM *****") main()
Load the project “PlaceCellFromLib.py” from the Python Manager dialog and Run/Execute the python script.
The Python script above, places two "TABLE" cells. The first cell is placed using the current active settings, while the second cell is placed with scale set to 1 and rotated 45 degrees.
Second part of the scripts scans through the graphical elements of the Active Model and gets the First and Last Element ID. This method can be used to retrieve the last placed element from the model.
Happy coding!
Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs