Python Assistant Prompts: Create Custom Item Types


 

Prompt Used:

Can you write a script which defines a new Item Type library and adds 
Item Type definitions with various property types (text, number, boolean)?

 

 

Python Assistant Generated Code:

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


# This script creates a new Item Type Library and adds an Item Type with text, number, 
# and boolean properties.
def CreateItemType():
    # Define names for the library and item type
    item_type_lib_name = "CustomLibrary"
    item_type_name = "MyItemType"

    # Get the active DGN file
    dgn_file = ISessionMgr.GetActiveDgnFile()

    # Create a new Item Type Library
    item_type_lib = ItemTypeLibrary(item_type_lib_name, dgn_file, False)

    # Add a new Item Type definition
    item_type = item_type_lib.AddItemType(item_type_name, False)

    # Add a text property
    text_prop = item_type.AddProperty("Description", False)
    text_prop.SetType(CustomProperty.Type1.eString)
    text_prop.SetDefaultValue(ECValue("Default description"))

    # Add a number property (integer)
    int_prop = item_type.AddProperty("Count", False)
    int_prop.SetType(CustomProperty.Type1.eInteger)
    int_prop.SetDefaultValue(ECValue(0))

    # Add a number property (double/float)
    float_prop = item_type.AddProperty("Length", False)
    float_prop.SetType(CustomProperty.Type1.eDouble)
    float_prop.SetDefaultValue(ECValue(0.0))

    # Add a boolean property
    bool_prop = item_type.AddProperty("IsActive", False)
    bool_prop.SetType(CustomProperty.Type1.eBoolean)
    bool_prop.SetDefaultValue(ECValue(False))

    # Write changes to the DGN file
    item_type_lib.Write()

    print(f"Item Type Library '{item_type_lib_name}' with Item Type '{item_type_name}' and properties created successfully.")


def main():
    CreateItemType()


if __name__ == "__main__":
    main()

 

 

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