Python Assistant Prompts: Reference Attach


 

Prompt Used:

Can you write a script which attaches another DGN file as a reference attachment, 
prompting the user for the file and attachment settings (model logical name, and scale)?

 

 

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
from tkinter import filedialog, simpledialog


def prompt_for_file():
    """
    Prompts the user to select a DGN file.
    :return: The path to the selected DGN file, or None if cancelled.
    """
    root = tk.Tk()
    root.withdraw()
    file_path = filedialog.askopenfilename(
        title="Select a DGN file",
        filetypes=[("DGN files", "*.dgn")]
    )
    root.destroy()
    return file_path

def prompt_for_attachment_settings():
    """
    Prompts the user for attachment settings: model name, logical name, and scale.
    :return: (model_name, logical_name, scale) or (None, None, None) if cancelled.
    """
    root = tk.Tk()
    root.withdraw()
    model_name = simpledialog.askstring("Attachment Settings", 
                                        "Enter the model name to attach:")
    if model_name is None:
        root.destroy()
        return None, None, None
    logical_name = simpledialog.askstring("Attachment Settings", 
                                          "Enter the logical name for the attachment:")
    if logical_name is None:
        root.destroy()
        return None, None, None
    scale = simpledialog.askfloat("Attachment Settings", "Enter the scale (e.g. 1.0):")
    root.destroy()
    if scale is None:
        return None, None, None
    return model_name, logical_name, scale


def attach_dgn_file(file_path, model_name, logical_name, scale):
    """
    Attaches a DGN file as a reference attachment to the active DGN model.
    """
    ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
    if ACTIVEMODEL is None:
        print("No active DGN model found.")
        return

    moniker = DgnDocumentMoniker.CreateFromFileName(file_path)
    if moniker is None:
        print("Failed to create moniker from file path.")
        return

    ret = ACTIVEMODEL.CreateDgnAttachment(moniker, model_name, True)
    if BentleyStatus.eSUCCESS != ret[0]:
        print("Failed to create DGN attachment.")
        return

    attachment = ret[1]
    attachment.SetLogicalName(logical_name)
    attachment.SetStoredScale(scale)

    if BentleyStatus.eSUCCESS != attachment.WriteToModel():
        print("Failed to write attachment to model.")
        return

    print("DGN file attached successfully.")


def main():
    file_path = prompt_for_file()
    if not file_path:
        print("No file selected.")
        return

    model_name, logical_name, scale = prompt_for_attachment_settings()
    if not model_name or not logical_name or scale is None:
        print("Invalid attachment settings.")
        return

    attach_dgn_file(file_path, model_name, logical_name, scale)


if __name__ == "__main__":
    main()

 

 

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