Sunteți pe pagina 1din 4

Methods to Automate CREO Design through VB API

Pratheesh Russell.S

A lot of design cases involve editing a preliminary design or design template


to suit the requirement. This paper focuses on the methods that can be used to
automate editing of designs in CREO by means of the CREO VB API. The
advantage of this API is that it can even be used from MS excel, although in this
article I will be focusing on VB.NET. The idea is to change one or more of the
following Parameters, Dimensions and Relations automatically through code
which will change the design. Usually this involves changing one factor manually
and VB code will change the others accordingly. The rest of the article focuses on
how this can be achieved.

Parameters:

This is the most simple of the three methods with lots of online tutorials.
First you must set parameters in CREO and set relations manually to connect the
parameters with relevant dimensions. The next step is to retrieve the parameters
through code you must connect to CREO initially, I wont go into that but that is
the preliminary step. Once connected initialize a few variables Dim params As
IpfcParameters, Dim param As IpfcBaseParameter, Dim paramValue As
IpfcParamValue and set

params = session.CurrentModel.ListParams()

Where session is asyncConnection.Session (refer vbug.pdf, found in CREO


parametrics folder). Now use the following set of code to retrieve all parameters

For i = 1 To (params.Count - 1)
param = params(i)
paramValue = param.Value
If paramValue.discr = 3 Then 'if parameter is real value
Dim str(2) As String
Dim itm As ListViewItem
str(0) = param.name the name of the parameter
str(1) = paramValue.DoubleValue the value of the parameter
<Do something with the parameter here>
End If
Next i
Now to change the value of a parameter and update it.

Dim paramown As IpfcParameterOwner


Dim ipparam As IpfcParameter
Dim ipbaseparam As IpfcBaseParameter
Dim Moditem As CMpfcModelItem
Dim paramval As IpfcParamValue
Dim Paraname as string ="your_param_name" '--> your parameter
name
cAC = New CCpfcAsyncConnection
asyncConnection = cAC.Connect(DBNull.Value, DBNull.Value,
DBNull.Value, DBNull.Value)
session = asyncConnection.Session
model1 = session.GetModel("<model name>",
EpfcModelType.EpfcMDL_PART) ' --> change <model name>
paramown = model1
ipparam = paramown.GetParam(Paraname)
ipbaseparam = ipparam
Moditem = New CMpfcModelItem
paramval = Moditem.CreateDoubleParamValue(CDbl("200")) ' -->
replace value with 200
ipbaseparam.Value = paramval
asyncConnection.Disconnect(1)

It is usually easier if the values like model name, parameter names are saved
initially to a list and retrieved from the saved list when changing it.
Dimensions:
In the first method we indirectly edited the dimensions in this method we
will directly edit it. Although you can retrieve all dimensions and modify them it
will be easier if you set a name to that dimension so that you can identify which
dimension you have change. Now to retrieve the dimensions through code dont
forget to connect to CREO initially.

Dim dimensions As IpfcModelItems


Dim dimension As IpfcDimension
Dim str(2) As String
dimensions =
session.CurrentModel.ListItems(EpfcModelItemType.EpfcITEM_DIMENSION)
For i = 0 To dimensions.Count - 1
dimension = dimensions.Item(i)
str(0) = dimension.Id
str(1) = dimension.GetName()
str(2) = dimension.DimValue.ToString
<Do something with the dimensions here>
Next
We will be using the dimension ID to change the values, while the name will
be used to identify which one must be changed so dont forget to save that also.
Now to change the value of a dimension and update it.

Dim dimension As IpfcBaseDimension


cAC = New CCpfcAsyncConnection
asyncConnection = cAC.Connect(DBNull.Value, DBNull.Value,
DBNull.Value, DBNull.Value)
session = asyncConnection.Session
model1 = session.GetModel("<model name>",
EpfcModelType.EpfcMDL_PART)
dimension = Nothing
dimension =
model1.GetItemById(EpfcModelItemType.EpfcITEM_DIMENSION,
CType("<dimension id>", Integer))
Try
dimension.DimValue = CType("200", Double) '--> change value to 200
Catch ex As Exception
msgbox ("update failed, check if dimension is driven by relation")
End Try
asyncConnection.Disconnect(1)
It is usually easier if the values like model name, ID, dimension names are
saved initially to a list and retrieved from the saved list when changing it.

Relations:
In this method we will edit relations. First we will retrieve the existent
relations through code dont forget to connect to CREO initially

Dim rel_seq As IpfcRelationOwner = session.CurrentModel


Dim rel As Istringseq = rel_seq.Relations
Dim all_relations as string = ""
For i = 0 To rel.Count - 1
all_relations += rel(i) & vbCrLf
Next i
'<Do something with all_relations here>

The following code can be used to update the relations in CREO. Note that
textbox1 is a multiline textbox in which relations are entered. Alternately it can be
an array of string or a string with multiple lines when used internally

cAC = New CCpfcAsyncConnection


asyncConnection = cAC.Connect(DBNull.Value, DBNull.Value,
DBNull.Value, DBNull.Value)
session = asyncConnection.Session
model1 = session.GetModel("<model name>",
EpfcModelType.EpfcMDL_PART)
Dim rel_seq As IpfcRelationOwner = model1
Dim rel As Istringseq = New Cstringseq '= rel_seq.Relations
Try
For Each line As String In TextBox1.Lines
rel.Append(line)
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
rel_seq.Relations = rel
asyncConnection.Disconnect(1)

The above three methods show how easily we can edit designs in CREO
through VB.NET. In most of the cases a combination of these methods will be
used to edit the design.

S-ar putea să vă placă și