EBDN - Community - Question & Answers

  Thursday, 14 June 2018
  2 Replies
  6.5K Visits
0
Votes
Undo
Hello,

I'm trying to change Associated Specification in code but I'm having some troubles with some attributes.

When you change the Associated Specification in the UI, all attributes from the old specification not used in the new specifications are removed, new attributes are added and those attributes which are common for both specifications are moved to their correct tabs.

But when you change the association in code, some attributes are not deleted and are in incorrect tabs. Note: this only happens when there is an attribute in the old specification which is not present in the new specification and it has a value specified.
This problem does not occur when all attributes from the old specification not present in the new specification are empty.

This is the code using which I'm trying to change the Associated Specification:


// func is an ObjectItem representing the object for which I'm trying to change the association.

// Step 1: remove all previous specification associations
foreach (var specAssociation in func.TargetAssociations.Where(ta => ta.Role == AssociationRole.SpecDevToDev))
{
func.TargetAssociations.Delete(AssociationRole.SpecDevToDev, specAssociation.RelatedObject);
}

// Step 2: Set type of the target object to the same as that of the new specification (this is most likely unnecessary)
AttributeItem attribute = func.Attributes.FindById(AttributeId.TId);
attribute.Value = spec.Attributes.FindById(AttributeId.TId).Value;

// Step 3: Associate the target function with the new specification
// Note: spec is an existing specification in the Specifications/Functions folder of the current project
var assoc = func.TargetAssociations.Add(AssociationRole.SpecDevToDev, spec);

// Step 4: Call ReplaceBy on the target function
// this line is important for some reason
func.ReplaceBy(spec, false, ReplaceBehavior.ForceRemoveEmptyAttributes | ReplaceBehavior.WithPreferredMaster);

// Step 5: Save
func.Store


Now, I'd expect the association to be done without the step 4 but for some reason, when I omit this step, nothing really happens.
With the step 4 included, the association actually "executes" but there are some attributes left which shouldn't be there.

https://lh6.googleusercontent.com/7tQL0I8aQPVch7k_0KmJqiGpQqFi9UspJe9BNLwRPAJqKlK82ub8tDqWrlhU2cY_0U9R5YVPST6oGg=w1920-h984-rw

There are three parts in the picture above.
The top part is the dialog for the function AFTER the code was executed.
The left part is the define dialog for the old specification.
The right part is the define dialog for the new specification.

As you can see, there is a 'General' tab in the old specification which is not present in the new specification. But it is still visible in the dialog for the function, even though it shouldn't be there.
The same goes for the tab 'Ex Electrical' - present in the old specification, not present in the new specification - still visible in the dialog for the function.

My question is - is there a better way of changing the Associated Specification? I am not entirely sure that this way is the correct way.
If there isn't another way - why are some attributes still present on the object when they are clearly not in the specification - this does not happen when you change the association in the UI.

Thanks,
Petr Š.