Python Assistant Prompts: Scale Selection Sets


 

Prompt Used:

Can you write a script which implements an interactive tool to scale selected 
elements relative to base point using a scale factor derived from user input.

 

 

Python Assistant Generated Code:

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

import tkinter as tk

class ElementScaleTool(DgnElementSetTool):
    """
    Interactive tool to scale selected elements relative to a base point using a user-supplied scale factor.
    """

    def __init__(self, toolId):
        DgnElementSetTool.__init__(self, toolId)
        self.m_transform = Transform()
        self.m_point = DPoint3d()
        self.m_scale = 1.0

    def show_scale_input_dialog(self):
        """
        Show a dialog to input the scale factor.
        """
        root = tk.Tk()
        root.title("Scale Factor Input")
        tk.Label(root, text="Enter Scale Factor:").pack()
        self.scale_factor = tk.StringVar(value="1.0")
        tk.Entry(root, textvariable=self.scale_factor).pack()
        tk.Button(root, text="OK", command=root.destroy).pack()
        root.mainloop()

    def _OnPostInstall(self):
        """
        Called after the tool is installed. Prompts for scale factor.
        """
        self.show_scale_input_dialog()
        super()._OnPostInstall()
        NotificationManager.OutputPrompt("Identify base point for scale.")

    def _SetupForModify(self, ev, isDynamics):
        """
        Pre-compute the necessary information needed for the _OnElementModify method.
        """
        anchorPt = DPoint3d()
        if not self._GetAnchorPoint(anchorPt):
            return False

        self.m_point = anchorPt
        try:
            self.m_scale = float(self.scale_factor.get())
        except Exception:
            self.m_scale = 1.0
        return True

    def _OnElementModify(self, eeh):
        """
        Apply the transform to the supplied element to scale.
        """
        tInfo = TransformInfo(self.m_transform)
        # Set up a uniform scale matrix about the base point
        tInfo.Transform.form3d[0][0] = self.m_scale
        tInfo.Transform.form3d[1][1] = self.m_scale
        tInfo.Transform.form3d[2][2] = self.m_scale
        tInfo.Transform.form3d[0][3] = self.m_point.x * (1 - self.m_scale)
        tInfo.Transform.form3d[1][3] = self.m_point.y * (1 - self.m_scale)
        tInfo.Transform.form3d[2][3] = self.m_point.z * (1 - self.m_scale)
        return eeh.GetHandler(eMISSING_HANDLER_PERMISSION_Transform).ApplyTransform(eeh, tInfo)

    def _OnModifyComplete(self, ev):
        """
        Called when Element Modify is complete.
        """
        ElementScaleTool.s_instance = None
        SelectionSetManager.GetManager().EmptyAll() # Clear selection
        PyCommandState.StartDefaultCommand()
        return True

    def _OnRestartTool(self):
        """
        Install a new instance of the tool.
        """
        ElementScaleTool.s_instance = None
        ElementScaleTool.InstallNewInstance(self.ToolId)

    def _OnCleanup(self):
        """
        Called when current command is being terminated.
        """
        ElementScaleTool.s_instance = None
        super()._OnCleanup()

    def _GetToolName(self, name):
        """
        Customize the tool name prompt at bottom left.
        """
        return WString("Scale Element")

    def _OnResetButton(self, ev):
        """
        Called when reset button is clicked.
        """
        ElementScaleTool.s_instance = None
        SelectionSetManager.GetManager().EmptyAll() # Clear selection
        PyCommandState.StartDefaultCommand()
        return True

    @staticmethod
    def InstallNewInstance(toolId):
        """
        Create and install a new instance of the tool.
        """
        selection_mgr = SelectionSetManager.GetManager()
        num_selected = selection_mgr.NumSelected()
        if num_selected == 0:
            MessageCenter.ShowErrorMessage("No elements selected to scale.", "", False)
            return True
                
        tool = ElementScaleTool(toolId)
        tool.InstallTool()
        ElementScaleTool.s_instance = tool


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


if __name__ == "__main__":
    main()

 

 

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