cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Windchill Customization(Giving constraint to an attribute)

SOLVED
Highlighted
KD
Participant

Windchill Customization(Giving constraint to an attribute)

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)

Tags (3)
1 ACCEPTED SOLUTION

Accepted Solutions

Re: Windchill Customization(Giving constraint to an attribute)

Using this code has one demerit

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

View solution in original post

21 REPLIES 21

Re: Windchill Customization(Giving constraint to an attribute)

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.

Re: Windchill Customization(Giving constraint to an attribute)

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

Re: Windchill Customization(Giving constraint to an attribute)

Any suggestion please

Re: Windchill Customization(Giving constraint to an attribute)

Use Lov type attribute and give values seperated by "|".

Re: Windchill Customization(Giving constraint to an attribute)

but that will be from UI ,my value is coming from a text file so I have to give value programmatically

Re: Windchill Customization(Giving constraint to an attribute)

Have you considered using a DataUtility to read your text file and populate the drop-down when the UI is rendered?

Re: Windchill Customization(Giving constraint to an attribute)

Yes I tried that I used the StringInputComponent to make a dropdown list but I have to again make it a MultiValuedAttribute there I am stuck

Re: Windchill Customization(Giving constraint to an attribute)

Does the stuff I gave you not work in 10?

Re: Windchill Customization(Giving constraint to an attribute)

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;