/** * Created by: sturm * Creation date: 16.02.2016 * * DXL Editor is provided by SODIUS (www.sodius.com) */ pragma encoding, "UTF-8" string Empty[] = {} const string Checker[] = {" "} Module CurrentModule = null DB CBox = create "Copy string to enum" DBE DCCheckBox DBE AttrNameList DBE AttrEnumList DBE CopyButton DBE OutputText Buffer OutputBuffer = create void printToOutput(const string msg) { OutputBuffer += msg setFromBuffer(OutputText, OutputBuffer) } Module checkModule() { Module currMod = current Module if (currMod == null or isEdit(currMod) == null) { errorBox("Module is not edtiable.\n\nPlease run skript from an editable module.") halt } return currMod } void fillAttrName(Module mod, DBE nam, bool dcOnly) { //print "fillAttrName(" fullName(mod) ", DBE, " dcOnly ")\n" AttrDef ad empty(nam) int i = 0 for ad in mod do { if (ad.system == true) continue if (ad.object == false) continue if (ad.typeName != "String") continue if (dcOnly and (ad.name)[0:2] != "DC ") continue insert(nam, i, ad.name "") i++ } } void fillAttrEnum(Module mod, DBE enm, bool dcOnly) { //print "fillAttrEnum(" fullName(mod) ", DBE, " dcOnly ")\n" AttrType at empty(enm) int i = 0 for at in mod do { if (at.system == true) continue if (at.type != attrEnumeration) continue if (dcOnly and (at.name)[0:2] != "DC ") continue insert(enm, i, at.name "") i++ } } void renameAttribute(Module mod, string oldName, string newName) { AttrDef oldDef = find(mod, oldName) AttrDef newDef = find(mod, newName) if (oldDef == null) { errorBox(CBox, "Attribute '" oldName "' does not exist in module '" fullName(mod) "'.") return } if (newDef != null) { errorBox(CBox, "Attribute '" newName "' does already exist in module '" fullName(mod) "'.") return } modify(oldDef, setName, newName) } void createAttribute(Module mod, string atName, string atType) { AttrDef atDef = find(mod, atName) AttrType atTyp = find(mod, atType) if (atDef != null) { errorBox(CBox, "Attribute '" atName "' does already exist in module '" fullName(mod) "'.") return } if (atTyp == null) { errorBox(CBox, "Attribut type '" atType "' does not exists in module '" fullName(mod) "'.") return } create object type atType attribute atName } void copyValues(Module mod, string fromAttr, string toAttr) { Object obj AttrDef toDef = find(mod, toAttr) for obj in mod do { if (obj.fromAttr "" == "") { printToOutput "." continue } else { printToOutput "*" } if (attributeValue(toDef, obj.fromAttr "") == true) { obj.toAttr = obj.fromAttr "" } else { printToOutput "\n# '" obj.fromAttr "' is not a valid value for attribute: '" toDef.name "' (type: '" toDef.typeName "')." printToOutput "\n#\tObject '" identifier obj "' in '" fullName(mod) "'\n" } } printToOutput "\n" } void deleteAttribute(Module mod, string atName) { AttrDef ad = find(mod, atName) if (ad == null) { errorBox(CBox, "Attribute '" atName "' does not exist in module '" fullName(mod) "'.") return } delete(ad) } void checkCheckCB(DBE elem) { bool dcOnly = (int get(elem)) == 1 //print "checkcheckCD(): '" dcOnly "'\n" fillAttrName(CurrentModule, AttrNameList, dcOnly) fillAttrEnum(CurrentModule, AttrEnumList, dcOnly) } void copyCB(DB elem) { //print "copyCB()\n" string atName = get(AttrNameList) string atType = get(AttrEnumList) if (atName == "" or atType == "") { infoBox(CBox, "Please select an attribute name and an attribute type.") return } string tmp = ".temp" //print "Attr: '" atName "'\n" //print "Type: '" atType "'\n" printToOutput "// 1. rename attribute '" atName "' to '" atName tmp "'\n" renameAttribute(CurrentModule, atName, atName tmp) printToOutput "// 2. create new attribute '" atName "' with enum-type '" atType "'\n" createAttribute(CurrentModule, atName, atType) printToOutput "// 3. copy for every object from '" atName tmp "' to '" atName "'\n" copyValues(CurrentModule, atName tmp, atName) printToOutput "// 4. delete '" atName tmp "' attribute\n" deleteAttribute(CurrentModule, atName tmp) } /** * MAIN */ CurrentModule = checkModule() DCCheckBox = checkBox(CBox, "Only DC attributes/enums", Checker, 1) set(DCCheckBox, checkCheckCB) AttrNameList = list(CBox, "Select an attribut name", 400, 5, Empty) AttrEnumList = list(CBox, "Select an enum type", 400, 5, Empty) CopyButton = apply(CBox, "Copy values", copyCB) OutputText = text(CBox, "Output", "", 400, 100, true) realize CBox fillAttrName(CurrentModule, AttrNameList, true) fillAttrEnum(CurrentModule, AttrEnumList, true) show CBox