Python Assistant Prompts: Copy Selection Sets


 

Prompt Used:

Can you write a script which implements an interactive tool to copy 
selected elements from a base point specified by the user?

 

 

Python Assistant Generated Code:

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


# Interactive tool to copy selected elements from a base point to a new point specified by the user.
class ElementCopyTool(DgnElementSetTool):
    def __init__(self, toolId):
        DgnElementSetTool.__init__(self, toolId)
        self.m_transform = Transform()
        self.m_transformr = Transform()
        self.m_dynamic = True
        self.base_point = None

    def _OnRestartTool(self):
        ElementCopyTool.s_instance = None
        ElementCopyTool.InstallNewInstance(self.ToolId)

    def _OnCleanup(self):
        ElementCopyTool.s_instance = None
        super()._OnCleanup()

    def _GetToolName(self, name):
        return WString("Copy Element")

    def _OnPostInstall(self):
        AccuSnap.GetInstance().EnableSnap(True)
        super()._OnPostInstall()
        NotificationManager.OutputPrompt("Identify base point for copy.")        

    def _OnDataButton(self, ev):
        # First click: set base point
        if self.base_point is None:
            self.base_point = ev.GetPoint()
            NotificationManager.OutputPrompt("Identify destination point for copy.")
            return True
        # Second click: perform copy
        dest_point = ev.GetPoint()
        vec = DVec3d()
        vec.DifferenceOf(dest_point, self.base_point)
        self.m_transform.InitFrom(vec)
        self.m_transformr.InitFrom(DVec3d() - vec)
        self.m_dynamic = False

        # Copy all selected elements
        selection_mgr = SelectionSetManager.GetManager()
        num_selected = selection_mgr.NumSelected()
        if num_selected == 0:
            MessageCenter.ShowErrorMessage("No elements selected to copy.", "", False)
            self._OnRestartTool()
            return True

        for i in range(num_selected):
            eh = ElementHandle()
            selection_mgr.GetElement(i, eh)
            if not eh.IsValid():
                continue
            # Duplicate the element and apply the transform
            eeh = EditElementHandle()
            eeh.Duplicate(eh)
            tInfo = TransformInfo(self.m_transform)
            handler = eeh.GetHandler(eMISSING_HANDLER_PERMISSION_Transform)
            if handler:
                handler.ApplyTransform(eeh, tInfo)
                eeh.AddToModel()
        MessageCenter.ShowInfoMessage("Elements copied.", "", False)
        self._OnRestartTool()
        return True

    def _OnResetButton(self, ev):
        SelectionSetManager.GetManager().EmptyAll() # Clear selection
        PyCommandState.StartDefaultCommand()
        return True

    @staticmethod
    def InstallNewInstance(toolId=1):
        # Check if elements are selected
        selection_mgr = SelectionSetManager.GetManager()
        num_selected = selection_mgr.NumSelected()
        if num_selected == 0:
            MessageCenter.ShowErrorMessage("No elements selected to copy.", "", False)
            return True

        tool = ElementCopyTool(toolId)
        tool.InstallTool()
        ElementCopyTool.s_instance = tool


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


if __name__ == "__main__":
    main()

 

 

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