Python Assistant Prompt: Circle by Center and Radius Using DgnPrimitiveTool


 

Prompt Used:

 

Python script to create a Circle with Centre Point and Radius method. 
Implement using class DgnPrimitiveTool.

 

 

Extend the attached example to use and set the active symbology 
(weight, style, color, level).

 

Python Assistant Generated Code:

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

class PlaceCircleTool(DgnPrimitiveTool):
    """
    Tool to place a circle by Center Point and Radius.
    1st data point: center.
    2nd data point: defines radius.
    """
    def __init__(self, toolId=1, toolPrompt=1):
        DgnPrimitiveTool.__init__(self, toolId, toolPrompt)
        self.m_points = DPoint3dArray()
        self.m_self = self

    def _OnPostInstall(self):
        AccuSnap.GetInstance().EnableSnap(True)
        DgnPrimitiveTool._OnPostInstall(self)
        NotificationManager.OutputPrompt("Enter center point for circle:")

    def _OnRestartTool(self):
        PlaceCircleTool.InstallNewInstance(self.GetToolId(), self.GetToolPrompt())

    def _OnDataButton(self, ev):
        pt = ev.GetPoint()
        self.m_points.append(pt)
        if len(self.m_points) == 1:
            NotificationManager.OutputPrompt("Enter radius point for circle:")
            self._BeginDynamics()
            return False
        elif len(self.m_points) == 2:
            center = self.m_points[0]
            radius = self.m_points[1].Distance(center)
            if radius <= 0.0:
                MessageCenter.ShowErrorMessage("Radius must be positive.", "", False)
                self.m_points.clear()
                self._OnRestartTool()
                return False
            eeh = EditElementHandle()
            ell = DEllipse3d.FromCenterRadiusXY(center, radius)
            ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
            status = ArcHandler.CreateArcElement(eeh, None, ell, ACTIVEMODEL.Is3d(), ACTIVEMODEL)
            if status == BentleyStatus.eSUCCESS:
                # Set active symbology (weight, style, color, level)
                ElementPropertyUtils.ApplyActiveSettings(eeh)
                eeh.AddToModel()
                MessageCenter.ShowInfoMessage("Circle placed.", "", False)
            else:
                MessageCenter.ShowErrorMessage("Failed to create circle.", "", False)
            self.m_points.clear()
            self._OnRestartTool()
            return True
        return False

    def _OnDynamicFrame(self, ev):
        if len(self.m_points) != 1:
            return
        center = self.m_points[0]
        radius = ev.GetPoint().Distance(center)
        if radius <= 0.0:
            return
        ell = DEllipse3d.FromCenterRadiusXY(center, radius)
        eeh = EditElementHandle()
        ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
        status = ArcHandler.CreateArcElement(eeh, None, ell, ACTIVEMODEL.Is3d(), ACTIVEMODEL)
        if status != BentleyStatus.eSUCCESS:
            return
        # Set active symbology for dynamic display as well
        ElementPropertyUtils.ApplyActiveSettings(eeh)
        redrawElems = RedrawElems()
        redrawElems.SetDynamicsViews(IViewManager.GetActiveViewSet(), ev.GetViewport())
        redrawElems.SetDrawMode(eDRAW_MODE_TempDraw)
        redrawElems.SetDrawPurpose(DrawPurpose.eDynamics)
        redrawElems.DoRedraw(eeh)

    @staticmethod
    def InstallNewInstance(toolId=1, toolPrompt=1):
        tool = PlaceCircleTool(toolId, toolPrompt)
        tool.InstallTool()

if __name__ == "__main__":
    PlaceCircleTool.InstallNewInstance()

 

 

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