cancel
Showing results for 
Search instead for 
Did you mean: 

Reuse entity attributes by script

Former Member
0 Kudos

Hello,

I'm trying to reuse some data items by an event handler using script.

Fore example, i have a table : Table1{id,name,adress}, when i create a new table, the initilize script add all attributes of Table1 in this new table, so i have new_table{id,name,adress}.

Now the problem is when i drop Table2, i loose attributes of Table1.

Is there a function which allow us to reuse attributs ? Because i searched in the metamodel help file without any result

Thank you

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

Thank you guys for your answers.

I think that i didn't explain well my needs.

I don't want to copy an entity to another, neither create new attributes. I have some attributes which are common between all my entities, until now, for each new entity, i use the reuse data items button to add those attributes, but now i want to make it simple and faster. So i created an event handler on my entities, and i want to reuse those data items and not create new attributes with the same code and name and comment ...

former_member185199
Contributor
0 Kudos

but the code i provided you will help you to do it this way,

you just select from another source like activemodel.dataitems, but as far as i Understand thge dataitems, its only a (distinct) set of all attributes and it doesn´t matter if you crate an attribute by code, cloning another attribut or using the dataitem.

anyway, why you dont post your code so far instead of let us guessing what you want?

Former Member
0 Kudos

My code is :

Function %Initialize%(obj)

For each T in ActiveModel.Entities

     If T.Code = "Items_Entity" Then

          For each col in T.Attributes

               Set new_attr = obj.Attributes.CreateNew(col)

               obj.Attributes.Add(new_attr)

          Next

     End If

Next

%Initialize% = True

End Function

former_member186838
Active Participant
0 Kudos

Why not copy the complete table in this case?

If you like to have always a "default" table you can do this with the script below

dim T1,mdl

set mdl = Activemodel

Set T1 = mdl.tables.item(0)

Dim pSel

set pSel = mdl.CreateSelection()

pSel.Objects.Add T1

pSel.CopyToPackage(mdl)

activediagram.AttachObject(mdl.tables.item(1))

former_member185199
Contributor
0 Kudos

Hello,

most likely what you did was not making new attributes but copy the "pointer" from Ent1 to ent2 ,

here is some code which should work:

Option Explicit

Dim pd_App As PdCommon.application

Function copy_table(thename As String, newname As String)

Set pd_App = New PdCommon.application

Dim InputModel As PdCDM.model
Set InputModel = pd_App.OpenModel("U:\PD_Modelle\cdmtest.cdm")


Dim tc, t As PdCDM.Entity
Dim col As PdCommon.ObjectBag

Debug.Print InputModel.DataItems.Count

Set col = InputModel.GetCollectionByName("Entities")
  

For Each t In col
        Output ">" & t.Code
       
       
            If t.Code = thename Then
            Dim a, ac As PdCDM.EntityAttribute
            Set tc = InputModel.Entities.CreateNew(cls_Entity)
            tc.Code = newname
            For Each a In t.Attributes
                Set ac = tc.Attributes.CreateNew(cls_EntityAttribute)
                ac.SetNameAndCode a.Code, a.Code

               '' copy here other stuff u need !


            Next a
            Output "ec-->" & tc.name & "  -  " & tc.ClassName & tc.UOL
            End If
           
Next t

End Function


GeorgeMcGeachie
Active Contributor
0 Kudos

If you copied the columns from table 1 to table 2, I can't see why they'd be deleted from table 1 when you delete table 2.

The PD way of 'reusing' columns is called replication. You can also use column denormalisation.

Former Member
0 Kudos

Thank you George,

Sorry i used the term 'Table' but in reality i'm working on a CDM and using entities. In this case i cant use column denormalization, and the aim of using a reuse is to avoid copy the same data item two times.

If you open the Entity Properties window, in the attributes Tab, there is an icone to reuse data items (The fifth one from left side).

I want to do the same thing by script.