Python Assistant Prompts: Find a Closed Boundary


 

Prompt Used:

Can you write a script which performs a flood operation from a user-clicked point 
to identify a closed boundary and create a shape element representing that area?

 

 

Python Assistant Generated Code:

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

# This script defines a tool that lets the user click a point, performs a flood operation 
# to find a closed boundary, and creates a shape element representing the flooded area 
# in the active model.

class FloodShapeCreator(DgnRegionElementTool):
    def __init__(self):
        """
        Initializes the FloodShapeCreator tool.
        """
        DgnRegionElementTool.__init__(self)  # Call base class constructor
        self.m_regionParams = RegionParams()
        self.m_self = self  # Keep self reference

    def _WantFloodDynamicArea(self):
        # Show valid flood region under the cursor without waiting for accept point.
        return True

    def _GetRegionCreateMode(self):
        # Use region parameters for flood creation.
        return DgnRegionElementTool.eREGION_CREATE_ByParams

    def _GetRegionParams(self):
        """
        Setup region params to create region by flood.
        """
        self.m_regionParams.SetType(RegionType.eFlood)  # Flood using seed point
        self.m_regionParams.SetFloodParams(RegionLoops.eOuter, 0.0)  # Allow regions with holes
        return self.m_regionParams

    def _OnProcessRegionResult(self, eeh):
        """
        Process a region element created by this tool.
        Applies active symbology and area fill, then adds to model.
        """
        ElementPropertyUtils.ApplyActiveSettings(eeh)
        ElementPropertyUtils.ApplyActiveAreaSettings(eeh, -1)
        eeh.AddToModel()
        return BentleyStatus.eSUCCESS

    def _OnRestartTool(self):
        """
        Restart the tool by installing a new instance.
        """
        FloodShapeCreator.InstallNewInstance()

    @staticmethod
    def InstallNewInstance():
        """
        Create and install a new instance of the tool.
        """
        tool = FloodShapeCreator()
        tool.InstallTool()


def main():
    # To start the tool, call:
    FloodShapeCreator.InstallNewInstance()


if __name__ == "__main__":
    main()

 

 

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