Skip to main content
12-Amethyst
April 8, 2013
Solved

Windchill Customization(Giving constraint to an attribute)

  • April 8, 2013
  • 2 replies
  • 13223 views

Hi everybody !

I am very new to this forum & very beginner to windchill 10.1.

Is this possible to add constraint to an attribute through program.

(ex. SingleValuedConstraint, LegalValueList)

Best answer by KD_01

Using this code has one demerit

The OOTB processor can’t persist the value so you have to write a custom processor also.

2 replies

1-Visitor
April 8, 2013

For WC 9.1:

In my experience, manipulating the AttributeContainer of the IBAHolder and the related View objects is overly complex. That statement's probably debatable, but directly manipulating the Persistable constraints seems much easier.

Here's an example of creating a DiscreteSet constraint:

Object values[] = new Object[]{"Blue","Red","Green"};

DiscreteSet ds = new DiscreteSet(values);

WTTypeDefinition td = ....

AttributeDefinition ad = ....;

TypeSingleAttrConstraint constraint = new TypeSingleAttrConstraint();

constraint.setTypeDefinition(td);

constraint.setAttributeDefinition(AttributeDefinitionReference.newAttributeDefinitionReference((AbstractAttributeDefinition)ad));

constraint.setBindingRuleClassName(SingleAttributeConstraintBindingRule.class.getName());

constraint.setEnforcementRuleClassName(DiscreteSetConstraint.class.getName());

constraint.setEnforcementRuleData(ds);

constraint = save/insert(constraint);

You may want to clear/reset any applicable cache afterwards.

KD_0112-AmethystAuthor
12-Amethyst
April 9, 2013

Thanks Matthew.

I am in 10.1 and what I want to do is suppose there is one soft attribute of Document named "New". Now I need a dropdown box in the UI with some value like the attached picture and again that attribute will be a multi valued attribute.

So, how to do that?

I want to give the value programmatically cause those values will come from a txt file.Untitled.png

1-Visitor
April 12, 2013

We defined our own MultiSelectDisplayComponent and renderer. Then we used it in a Data Utility to display a multi-select list of values.

MultiSelectDisplayComponent.java:

import com.ptc.core.components.rendering.guicomponents.*;

import java.util.ArrayList;

public class MultiSelectDisplayComponent extends AttributeGuiComponent implements PrintableComponent

{

private static final long serialVersionUID = 1L;

String attributeName;

int numberOfDisplayLines = 0;

ArrayList<String> keyValues;

ArrayList<String> displayValues;

ArrayList<String> selectedValues;

public MultiSelectDisplayComponent( String attrName, ArrayList<String> keys, ArrayList<String> displayV, ArrayList<String> selectedV, int numLines)

{

attributeName = attrName;

keyValues = keys;

numberOfDisplayLines = numLines;

selectedValues = selectedV;

displayValues = displayV;

setRenderer(new MultiSelectDisplayComponentRenderer());

}

public String getPrintableValue()

{

return "";

}

}

----------------------------------------------------------------

MultiSelectDisplayComponentRenderer.java:

import com.ptc.core.components.rendering.*;

import java.io.PrintWriter;

import java.util.Vector;

import wt.util.WTContext;

import java.lang.reflect.Method;

import wt.util.WTStringUtilities;

import wt.fc.EnumeratedType;

public class MultiSelectDisplayComponentRenderer extends AbstractRenderer

{

private static final long serialVersionUID = 1L;

public MultiSelectDisplayComponentRenderer()

{

}

protected void renderObject(Object obj, PrintWriter pw, RenderingContext rc)

throws RenderingException

{

renderObject((MultiSelectDisplayComponent)obj, pw, rc);

}

protected void renderObject(MultiSelectDisplayComponent comp, PrintWriter pw, RenderingContext rc)

throws RenderingException

{

write(pw, buildMultiSelectComponent(comp));

}

protected String buildMultiSelectComponent(MultiSelectDisplayComponent dispComp)

{

//System.out.println("+++ In MultiSelectDisplayComponentRenderer.buildMultiSelectComponent()");

String htmlString;

Vector<String> theList = new Vector<String>();

String s1 = null;

int numLines = dispComp.numberOfDisplayLines;

if (numLines <= 1 ) numLines = 4;

if (dispComp.selectedValues != null) {

for (int j=0; j<dispComp.selectedValues.size(); j++){

theList.add(dispComp.selectedValues.get(j));

}

}

htmlString = "<select ";

htmlString = htmlString + "name= \"" + dispComp.attributeName + "\" id = \"" + dispComp.attributeName + "\"";

if (dispComp.isRequired()) {

htmlString = htmlString + " class = \"required \"";

}

htmlString = htmlString + " multiple size=" + numLines;

htmlString = htmlString + ">\n";

try {

for (int k=0;k< dispComp.keyValues.size(); k++) {

if ( dispComp.selectedValues == null ) {

String stringV = dispComp.keyValues.get(k);

String displayV = dispComp.displayValues.get(k);

htmlString +=" <OPTION VALUE=\"" + stringV + "\">" + displayV + "\n";

} else {

boolean selected;

String stringV = dispComp.keyValues.get(k);

String displayV = dispComp.displayValues.get(k);

if ( dispComp.selectedValues != null) {

selected = dispComp.selectedValues.contains(stringV);

} else {

selected = false;

}

if (selected) {

htmlString +=" <OPTION VALUE=\"" + stringV + "\" selected>" + displayV + "\n";

} else {

htmlString +=" <OPTION VALUE=\"" + stringV + "\">" + displayV + "\n";

}

}

}

} catch ( Exception ex ) {

ex.printStackTrace();

}

htmlString += "</select>";

//System.out.println("+++ Leaving MultiSelectDisplayComponentRenderer.buildMultiSelectComponent()");

return htmlString;

}

protected boolean isValidForObject(Object obj)

{

return obj instanceof MultiSelectDisplayComponent;

}

}

------------------------------------------------------------------

Usage in Data Utility.getDataValue method:

MultiSelectDisplayComponent multiSelectComp = new MultiSelectDisplayComponent(compId, keyValues, displayValues, selectedValues, listSize);

multiSelectComp.setRequired(isRequired);

return multiSelectComp;

17-Peridot
January 5, 2015

Hi, in 10.2 you have this OOTB, called "External Enumerated Value List". There you can pull the values from a text file or even from a DB like ERP.

wcbru_10_2_M020.png
Happy New Year!
Björn
1-Visitor
May 19, 2015

Hi Kaushik,

I have one doubt. I have one custom column. I want to publish the value of distribution target there. How can I get the value of Distribution target name or number in that column?

I mean, how can I fetch the data of Distribution target IBA there?

Thanks,

Sakil