Create Attributes

Hi

I want to write a script that will go through all of the existing modules in a project and add some attributes to each module (a DXL task I thought would have been done to death already....code anyone?). The details of the attributes to be added to each module would be retrieved from a CSV file or maybe another DOORS module.

I am confused with the "create(attribute definition)" as described in the dxl manual.

AttrDef create(object
property value...
(default defVal)
attribute(string attrName))

create object type "Integer" attribute "Cost"

The above should create an AttrDef object, but when I run this from a module, an attribute is created in the current module. This does not make sense to me. Shouldn't it be a 2 step process? As per below:

AttrDef ad
Module m
ad = create object type "Integer" attribute "Cost" //Create the AttrDef Object
m.addAttribute(ad) //Add the attribute to the module. (But there is no function to do this)

Any ideas on this?

Cheers
Peter.Dawson - Tue Jan 05 00:50:16 EST 2010

Re: Create Attributes
llandale - Tue Jan 05 13:06:29 EST 2010

It was a very poor decision by QSS years ago to provide many 'attribute' functions that presume the current module. Since the 'current' module is sometimes ambiguous (it may or may not be the last module you opened), DXL that uses these functions should routinely set the current module before using the functions. That may create another problem elsewhere in your code if you presume the currerent module; thus you need to set current back after you use the funcion. In your case:

Module mCurr = current()
Module mod = edit(NameModFull, false)
current = mod
AttrDef ad = create object type "Integer" attribute "Cost"
if (!null mCurr) current = mCurr
if (null ad) then attr didn't create

Perhaps first check if it already exists:
AttrDef ad = find(mod, "Cost")
if (!null ad) then attr already exists, cannot be created

Here's my very very very old function that creates an attribute in a module:

//******************
Module    fSetCurrMod(Module modNew)
{     // Set the "Current" module, returning the previous current
        // module.  You cannot "clear" current by passing null. 
        Module  modPrev = current()
        if (!null modNew and modNew != modPrev)
          (current ModuleRef__) = modNew
        return(modPrev)
}     // end fSetCurrMod()
 
 
 
 
 
 
//*******************
bool    fCreateAttr(Module mod, string NameAttr, string NameType,
                bool DoModule, DoObject, DoInherit, DoHistory, DoBars, DoDate,
                ReplaceAttr, EraseAttr, string &ErrMess)
{     // Create the attribute with the specified properties in the specified module.
        //   The specified Attr Type 'NameType' must already exist.
        // If it does already exist and the user opts to NOT replace it, report an error.
        //   Otherwise optionally erase its old values.
        //   In any case set its properties as defined.
        // Note: Erasing is better than deleting then creating since the
        //   attribute may appear in some view.
        AttrDef ad
        AttrType        at
        ErrMess = ""
        Module  modPrevCurr             // previous current module
        if (null mod or null NameAttr or null NameType or (!DoModule and !DoObject))
        {  ErrMess = "fCreateAttr: Internal Error, bad input."
           return(false)
        }
        at      = find(mod, NameType)
        ad      = find(mod, NameAttr)
        if (null at)
        {  ErrMess = "Attr Type *" NameType "* does not exist"
        }
        elseif (!null ad)
        {             // *** Attribute already Exists ***
           if (!ReplaceAttr)
           {  ErrMess = "Attr Exists."      // User didn't want the attr replaced if it already exists
           }
           elseif (ad.typeName != NameType)
           {  ErrMess = "It is not of type *" NameType "*.  You must delete it manually.\n"
           }
           if (!canWrite(mod, NameAttr))
           {  ErrMess = ErrMess "No Write Access to *" NameAttr "*.  You must resolve this."
           }
           elseif (EraseAttr and !fEraseAttr(mod, NameAttr))
           {  ErrMess        = "Error erasing existing Values of '" NameAttr "'."
           } 
           else
           {}         // everything OK now.
 
           if (!null ErrMess)
           {  ErrMess        = "Attribute *" NameAttr "* already exists.\n" ErrMess
           }
           else
           {         // Exists OK.  Make its characteristics match call parameters
              modPrevCurr = fSetCurrMod(mod)
              noError()
              if (DoModule and DoObject)
              {  ad  = modify (ad, module object                             //-
                      type NameType     history DoHistory       changeBars DoBars       //-
                      date DoDate       inherit DoInherit       attribute  NameAttr
                      )
              }
              elseif (DoModule and !DoObject)
              {  ad  = modify (ad, module                                    //-
                      type NameType     history DoHistory       changeBars DoBars       //-
                      date DoDate       inherit DoInherit       attribute  NameAttr
                      )
              }
              elseif (!DoModule and DoObject)
              {  ad  = modify (ad, object                                    //-
                      type NameType     history DoHistory       changeBars DoBars       //-
                      date DoDate       inherit DoInherit       attribute  NameAttr
                      )
              }
              else{}  // Neither Module nor Object attr??
              ErrMess = lastError()     // Capture the modify error
              if (null ad or !null ErrMess) ErrMess = "Error Modifying Attr <">\n" ErrMess
              fSetCurrMod(modPrevCurr)  // reset current module
           }         // End attempt to modify it.
        }            // end Attr already exists
        else                    // create target attribute
        {  modPrevCurr = fSetCurrMod(mod)
           noError()
           if (DoModule and DoObject)
             ad = create(module object                                  //-
                 type NameType  history DoHistory       changeBars DoBars       //-
                 date DoDate    inherit DoInherit       attribute NameAttr
                     )
           elseif (DoModule and !DoObject)
              ad        = create(module                                         //-
                 type NameType  history DoHistory       changeBars DoBars       //-
                 date DoDate    inherit DoInherit       attribute NameAttr
                  )
           elseif (!DoModule and DoObject)
              ad        = create(object                                         //-
                  type NameType history DoHistory       changeBars DoBars       //-
                  date DoDate   inherit DoInherit       attribute NameAttr
                      )
            else{}    // must be either DoModule or DoObject or Both.
            ErrMess = lastError()       // Capture the Create error, if any
            if (null ad or !null ErrMess) ErrMess = "Error Creating Attr <">\n" ErrMess
            fSetCurrMod(modPrevCurr)
        }
 
        if (null ErrMess)
             return(true)
        else return(false)
 
}    // end fCreateAttr()
 
 
Example:
if (!fCreateAttr(mod, NameAttr, NameType, false, true, false, true, true, true, false, false, ErrMess))

 

 

  • Louie

 

 

Re: Create Attributes
Peter.Dawson - Tue Jan 05 16:47:37 EST 2010

Thanks Louie, much appreciated.