Thursday, May 22, 2008

Pro/WebLink: Changing Model Relations Quickly with Wildfire 4

Changing relations is often a frustratingly difficult task to perform in a Pro/ENGINEER model. This is especially true for assemblies, even ones with only a few dozen parts.

Up until Wildfire 4, there were many ways to accomplish this involving some kind of file export/import process. These include mapkeys, J-Link, and Pro/WebLink, each having its own complications.

Starting with Wildfire 4, the PFC API's (Pro/WebLink, J-Link, VB API) have been enhanced to allow more direct access to relations. Relations can be read into a string sequence (a form of dynamic array), modified within the sequence, then re-applied to the model very quickly. This makes programmatic editing of relations very simple, fast, and easy.

While using the PFC API's to perform this action on a single part model may seem overkill compared to using mapkeys, consider that mapkeys don't scale well. If you're doing a one-off task, mapkeys work great. When you need to perform the task for all components of an assembly, whether large or small, the API's can accomplish the task much more quickly and reliably. For assemblies with thousands of components, mapkeys just won't get the job done.

What follows is an example of a Pro/WebLink function to add relations to a model from an array. First, existing relations are obtained from the model using the "Relations" property (a stringseq object), then elements of the array are appended to the stringseq using the Append() method. Finally, the modified stringseq is reassociated to the model. The change is immediate and does not require a regeneration.

function addRelations ( model, array ) {

// get relations as stringseq object
var rel_seq = model.Relations;

// append array elements to stringseq
for (var i=0; i<array.length; i++) {
rel_seq.Append(array[i]);
}

// assign modified stringseq to model relations
model.Relations = rel_seq;

}
 
Code to call the function is also pretty basic. Using the CurrentModel property of the session object, the model object can is obtained. Here an array is defined, but even better would be to pull the values from an HTML TextArea or some other data source. Lastly, the addRelations() function is called with the model object and the relations array.

// get current model
var session = pfcGetProESession();
var model = session.CurrentModel;

// define new relations as array
var array = new Array( "abc=123", "def=456" );

// add relations
addRelations(model, array);
 
A recursive procedure to apply relations to assembly components is only moderately more complicated. See my other examples only how to rcursively process assembly components with Pro/WebLink.




Comments and questions are always welcome, either here on my blog or via email at MarcMettes@InversionConsulting.com.

3 comments:

ZuG' said...

Hi,

I tried to add new relations in a model with Visual Basic but I have a problem with the "model.Relations" affectation.

Here is my code:

''''''''''''''
Dim rel As pfcls.Cstringseq
Dim models As pfcls.IpfcModels
Dim model As pfcls.IpfcModel

models = ProE_Session.ListModels
model = models(0)
rel = New pfcls.Cstringseq

rel = model.Relations

'Adding relations as (A=0,B=1,...)
Dim i As Integer
For i = 0 To 9
rel.Append(Chr(65 + i))
Next i

model.Relations = rel
''''''''''

The last line model.Relations = rel gives invalid exeption... Why???

Have you got any ideas ???
Thanks for your help!

Marc Mettes said...

Your code is attempting to create relations like this (without the =0):
A
B
C
D

These are not valid relations, of course.

For testing, instead of using this:

Dim i As Integer
For i = 0 To 9
rel.Append(Chr(65 + i))
Next i


Use this and see if it works:

rel.Append("abc=123")
rel.Append("def=456")
rel.Append("ghi=789")


Send me an email if this is still not working for you.

Marc

sudha said...

hi,
i want know about how to give relations in assembly mode using proe
when i am giving the relations in part its not accepting the relations in a assembly if any knows or having tutorials please mail me.