Explaining Production Model Function Behavior
🧠 Summary of Behavior
You're asking for:
- Button to Add a new object (Enterprise, Site, Area, Line) — via a pop-up form. 
- The pop-up auto-selects the parent based on the current selection in the Tag Tree. 
- If "Enterprise" is selected → no parent field is shown (it's the root). 
- If "Site", "Area", or "Line" is selected → form pre-fills parent based on tree selection. 
- When "Line" is added: - It creates a folder in the tag provider. 
- Then creates a UDT instance inside that folder. 
 
Let’s break this into implementation-ready steps 👇
🔧 1. Perspective Components Overview
You will need:
| Component | Purpose | 
| Tag Tree | Shows existing Enterprise > Site > Area > Line hierarchy | 
| Button | “Add” button to launch the popup | 
| Popup View | Form to create Enterprise/Site/Area/Line | 
| Text Fields / Dropdowns | For name, type, and parent | 
| Script Transform + System Tag Functions | For tag creation | 
| Optional: UDT Reference | For Line-level auto-instancing | 
🧩 2. Tag Structure Assumption (UDT + Folder)
We’re assuming a tag folder like this:
[default]
└── EnterpriseA
    └── SiteA
        └── Area1
            └── Line1 (folder)
                └── LineUDT_Instance
- Line1 = folder 
- Inside Line1, you instance a UDT: system.tag.addTag(..., type='UDT_INST', parameters={...}) 
🧙 3. Popup View: "Add New Entity"
View Params:
- params.entityType → "Enterprise", "Site", etc. 
- params.parentPath → optional full tag path of parent 
Form Components:
- Name Input (Text Field) 
- Parent Path (Label or Hidden) 
- Submit Button with script 
🧠 4. Scripting Logic (Popup Submit)
Here’s a typical script for submitting the form from the popup:
# Assume parameters are passed into the view params
entityType = self.view.params.entityType
parentPath = self.view.params.parentPath  # May be empty if "Enterprise"
name = self.getSibling("nameField").props.text
fullPath = parentPath + "/" + name if parentPath else name
# Common dictionary
tagConfig = {
    "name": name,
    "type": "Folder",  # default
    "path": fullPath
}
try:
    if entityType.lower() == "line":
        # Create folder
        system.tag.addTag(parentPath=parentPath, name=name, tagType="Folder", attributes={})
        
        # Create UDT inside the new folder
        system.tag.addTag(
            parentPath=fullPath,
            name="Instance1",  # or name+"_UDT"
            tagType="UDT_INST",
            attributes={"UDTParentType": "LineUDT"},  # <-- Your UDT name
            parameters={}  # Optional UDT parameters
        )
    else:
        # Just make the folder for Enterprise/Site/Area
        system.tag.addTag(parentPath=parentPath, name=name, tagType="Folder", attributes={})
    
    system.perspective.closePopup()
except Exception as e:
    system.gui.messageBox("Error creating tag: {}".format(str(e)))
⚠ Note: Use system.tag.addTag() with full caution — this executes on the Gateway scope, so this script should be in a Message Handler or named Gateway script, then called via system.perspective.sendMessage().
🎯 5. Add Button Behavior
- Button bound to selection in the Tag Tree 
- On click: - Detect current selection (e.g., Area1) 
- Determine what child is allowed (e.g., next level is Line) 
- Open popup with: 
 
system.perspective.openPopup(
    'addEntityPopup',
    view='Popups/AddEntity',
    params={
        "entityType": "Line",  # Or Site/Area/Enterprise
        "parentPath": currentSelectionPath
    },
    title="Add New Line",
    modal=True
)
🧠 6. Optional: Determine Next Child Type
You can define a helper dictionary:
hierarchy = {
    "Enterprise": "Site",
    "Site": "Area",
    "Area": "Line",
    "Line": None  # no further children
}
Use this to decide what to add based on the current selection level in your tag path.
✅ Summary Checklist
| Feature | Status | 
| Tag Tree shows hierarchy | ✅ Use Tag Tree component | 
| "Add" button | ✅ Opens popup, checks selection | 
| Popup form | ✅ With type, name, prefilled parent | 
| Line creates folder + UDT | ✅ Special case logic in submit | 
| Scripts run Gateway-safe | ⚠ Use Message Handlers or callables |