Skip to Content

Creating an extended attribute in PD using VBS

Hi,

Am trying to create an extended attribute within PD using vbs

It fails on line:

Set attr = Createobject(model.cls_ExtendedAttribute)

Object not supported - does anyone know how to create a new extended atrribute in a dataitem?

Thank you

Code looks like

ValidationMode = True 'Forces PowerDesigner to validate 
' actions and return errors in the event of a forbidden action
InteractiveMode = im_Batch ' Supresses PowerDesigner dialogs
' Main function
' Create an OOM model with a class diagram
Dim Model
Set model = CreateModel(PdCDM.cls_Model, "|Diagram=ClassDiagram")
model.Name = "Customer Management"
model.Code = "CustomerManagement"
' Get the class diagram
Dim diagram
 Dim cls
 Set cls = model.dataitems.CreateNew()
 cls.Name = "Customer"
 cls.Code = "Customer"
 cls.Comment = "Customer class"
 cls.Description = "The customer class defines the attributes and behaviors of a customer."
 ' Create attributes
 CreateAttributes cls
 ' Create methods
 CreateOperations cls
 ' Create a symbol for the class
 Dim sym
 Set sym = diagram.AttachObject(cls)
 CreateClasses = True
' Create attributes function
Function CreateAttributes(cls)
 Dim attr
 Set attr = Createobject(model.cls_ExtendedAttribute)
 '(PdOOM.cls_Attribute)
 attr.Name = "ID"
 attr.Code = "ID"
 attr.DataType = "int"
 attr.Persistent = True
 attr.PersistentCode = "ID"
 attr.PersistentDataType = "I"
 attr.PrimaryIdentifier = True
 Set attr = cls.CreateObject(PdOOM.cls_Attribute)
 attr.Name = "Name"
 attr.Code = "Name"
 attr.DataType = "String"
 attr.Persistent = True
 attr.PersistentCode = "NAME"
 attr.PersistentDataType = "A30"
 Set attr = cls.CreateObject(PdOOM.cls_Attribute)
 attr.Name = "Phone"
 attr.Code = "Phone"
 attr.DataType = "String"
 attr.Persistent = True
 attr.PersistentCode = "PHONE"
 attr.PersistentDataType = "A20"
 Set attr = cls.CreateObject(PdOOM.cls_Attribute)
 attr.Name = "Email"
 attr.Code = "Email"
 attr.DataType = "String"
 attr.Persistent = True
 attr.PersistentCode = "EMAIL"
 attr.PersistentDataType = "A30"
 CreateAttributes = True
End Function
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • Best Answer
    May 17 at 06:19 PM

    ctempcapture.pngMore code.

    Open your CDM. Run the following code

    Set MyExt = ActiveModel.ExtendedModelDefinitions.CreateNew()
    MyExt.Name = "MyExtension"
    MyExt.Code = "MyExtension"
    'Create extended attribute for dataItem
    Set extendedAttributeObj = MyExt.AddMetaExtension(PdCDM.Cls_DataItem, Cls_ExtendedAttributeTargetItem)
    extendedAttributeObj.name ="Business Unit"
    extendedAttributeObj.code ="Business_Unit"
    extendedAttributeObj.dataType = "12" ' 12 refers to String

    Set extendedAttributeObj = MyExt.AddMetaExtension(PdCDM.Cls_DataItem, Cls_ExtendedAttributeTargetItem)
    extendedAttributeObj.name ="Business Unit Area"
    extendedAttributeObj.code ="Business_Unit_Area"
    extendedAttributeObj.dataType = "12"

    Note datatype should be assigned based on the following screenshot

    Add comment
    10|10000 characters needed characters exceeded

  • May 09 at 12:40 PM

    Are you really sure, that you need to create an EXTENDED attribute? From your code it seems like you are trying to create another attribute inside a class and you are trying to set Datatype, Persistent and other properties in it. Your examples of class attributes are (ID, Name, Phone, etc.), but extended attribute is a new type of property for your object. For example: Class object has standard attributes like Name, Code, Comment, Description, etc. If you want to extend it with new property ("data steward name" for example), you should create extended attribute. Btw. it is much more common, that you create extended attributes manually (once) and use them automatically by script later (many times).

    To sum it up:

    • Class attributes - objects inside your class, defining your class content (example: ID, Name, Phone, etc.)
    • Standard attributes (of a Class object) - DEFAULT properties describing your object (Name, Code, Comment, Description, Annotation, etc.)
    • Extended attributes - USER DEFINED properties describing your object (for example "business owner name" for your class)

    Besides that, you are mixing OOM and CDM models and also mixing Class object and Dataitem object together. Such script cannot work.

    Regards,

    Ondrej

    Add comment
    10|10000 characters needed characters exceeded

  • May 10 at 06:07 AM

    Hi

    What I am trying to do is (all through VBS) as I want to automate the process in a batch job

    Create a CDM based on an excel spreadsheet

    Create the data items that are listed in the excel spreadsheet

    The data item has extended attributes that then need to be added.

    If I were to do it manually - I would create a new CDM, create the extended attributes and then import the spreadsheet - have done this all manually and it works.

    Issues that I have

    I can create the CDM, and I can create the new data items - but not with extended attributes (because in a new CDM the extended attributes do not exist). Am not really a developer so trying to figure things out using the metamodel as much as possible, but there are no examples for creating an extended attribute within a data item

    Add comment
    10|10000 characters needed characters exceeded

    • Hello Catherine - please refer to your earlier question about importing from Excel via a script.

      Here's a possible process for you.

      In a "specified place" accessible from the script, create a CDM that contains two things:

      • a model extension that contains the required extended attributes
      • the "Excel Import" extension - preferably embedded, not a shortcut
      • an Excel Import that works

      The "specified place" could be

      • a windows or fileserver folder
      • your local "Templates" folder
      • a folder in the Repository (perhaps the Library)

      In a script you could do the following (thinking out loud here, haven't tried it myself):

      1. Use the CreateModelFromTemplate method to create a new model based on the existing one - it'll be a clone
      2. In the new model's collection of Excel Imports (Extended Objects with "Excel Import" Stereotype), find the Excel Import
      3. Change the target Excel file if necessary
      4. Now run the "Import" method for the Excel Import (this may need to be qualified, as it's in the extension EXCEL_IMPORT, but
      5. Save the model - or check in to the repository directly

      Steps 2-4 could be replaced with a custom script of course, in which case you don't need the Excel stuff in the model template. If you are using a custom script, the actual import method could be held in an extension in the model template - that way, the script you run automatically could be written in one of several languages.

      Do you have any scripts that open and read Excel files?

  • May 11 at 09:15 PM
    Add comment
    10|10000 characters needed characters exceeded

    • Hi

      Had a look at the code - thank you. Issue is as follows:

      I can created the extended attribute and stereotype as per code however it creates it as attached image:

      What I am trying to do is:

      So my issue is that I don't understand how the meta data model completely works. Using the code that I have:

      Set MyExt = Model.ExtendedModelDefinitions.CreateNew() ********want to be able to say something like

      **************************Set MyExt = Model.ExtendedDATAITEMDefinitions.CreateNew()


      MyExt.Name = "IGC Extensions"
      MyExt.Code = "IGC_Extensions"

      ****************************************************Don't need a sterotype for what I am doing************

      'Create stereotype

      Set MyStype = MyExt.AddMetaExtension(PdCDM.Cls_Model, Cls_extendedattributeTargetItem)

      *****************************************************************************************************************
      MyStype.Name = "Extended Attr"
      MyStype.UseAsMetaClass = true
      'Create extended attribute *************************this part is not an issue*************************
      Set MyExAtt = MyStype.AddMetaExtension(Cls_ExtendedAttributeTargetItem)
      MyExAtt.Name = "Stewards"
      MyExAtt.Label = "Stewards"
      MyExAtt.DataType = "12" ' (String) For a full list of values,

      Not sure if I am making any sense.

      pd-image-1.jpg (48.8 kB)
      pd-img2.jpg (41.9 kB)
  • May 11 at 09:19 PM

    In above page, click

    Creating and Accessing Extensions (Scripting)

    Add comment
    10|10000 characters needed characters exceeded

    • Thanks for the link, Phillip. On my machine, the whole script runs in less than 2 seconds, which is impressive :)

      There are a couple of minor issues with the script as listed in Help:

      • I would not call the Extended Attribute "Type", as the Mobile Device object already has a Type property with its own list of values. In my version I called it "Tablet Type"
      • The script sets all values of Tablet Type to the default value, because there is a mistake in qualifying the name of the extended attribute in this command:
      obj.SetExtendedAttribute "MYEXT.TabletType", myProps(2)

      MYEXT needs to be changed to match the extension's Code