Monday, February 10, 2014

Controlling a Box in CREO from MSExcel by using the VB API

Once the setup up part was finished, we are ready to test this API. In this simple example we are to use the capabilities for parameters the API has. In other exercise we will show something more sophisticated but based fundamentally in the very same ability of CREO for controlling a model via parameters.

The Box

Let's make in CREO a part with the name "box.prt", in it create a solid box with 3 dimensions (Length, Depth and Height).






Then Create 3 parameters for the 3 dimensions above mentioned. Let's name the variables as: LENGTH, DEPTH and HEIGHT





Now, in the "relations" dialog box, relate each parameter with the dimensions in the box





So we are ready from the side of CREO.

The Excel Side

Now on the side of Excel, let's create a new spreadsheet. and format it as is shown in next picture



Let's take advantage of the validation capabilities of Excel for restricting user inputs and avoid incorrect data to be passed to CREO API. So make the 3 dimensions to be more than 10 units and less than 800 units (let's say mm or inches). Also of course The values should be numeric. Next Image shows how it should look like when trying to enter a vale lower than 10 for HEIGHT Parameter.



Add a Button for connecting to CREO,



and insert the next snippet of code in the on-click event(This code allows for you to update the LENGTH from excel, DEPTH and HEIGHT are not updated as you can see in the code lines):

Private Sub UpdateCreo_Click()

Dim asyncConnection As IpfcAsyncConnection
Dim cAC As CCpfcAsyncConnection
Dim session As IpfcBaseSession
Dim model1 As IpfcModel
Dim px As IpfcParameterOwner
Dim p01, p02 As String
Dim p1 As IpfcParameter
Dim p2 As IpfcBaseParameter
Dim Mitem As CMpfcModelItem
Dim pv1 As IpfcParamValue

p01 = "LENGTH"
Range("D5").Select
p02 = ActiveCell.Text

Set cAC = New CCpfcAsyncConnection
Set asyncConnection = cAC.Connect(dbnull, dbnull, dbnull, dbnull)
Set session = asyncConnection.session
Set model1 = session.GetModel("BOX.PRT", EpfcMDL_PART)
Set px = model1
Set p1 = px.GetParam(p01)
Set p2 = p1
Set Mitem = New CMpfcModelItem
Set pv1 = Mitem.CreateDoubleParamValue(CDbl(p02))
p2.Value = pv1

asyncConnection.Disconnect (1)

End Sub


Test the button and this will allow you to update the box's LENGTH from Excel by using the VB API for PTC's CREO.

Thanks.



6 comments:

  1. How do I modify a pattern of co-ordinate system that uses a table for Pattern, using VBA in creo?

    ReplyDelete
  2. Hello,

    I have been playing with this code of a bit, and it has helped me a whole lot. However, I was wondering if you could illustrate how to include the code that would automatically regenerate the file after modifying the parameters in Excel. I cant work it out despite trying all manner of things. It must be said however that my understanding of VBA is pretty basic, as is my understanding of the Creo API....

    Thanks for your help!

    Best Regards,

    Bryan

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Hello friend,
    Thanks for your example, this helped me much. However I would like add the sintaxis for regenerate's Creo automatically. In addition I want modify several parts of assembly. For example: assembly.asm, part1.prt, part2.prt, etc with the same parameter "LENGTH".

    Anew thanks,
    Best Regards
    Andres Guzman
    e-mail: afguzmanvilla@hotmail.com

    ReplyDelete
  5. I really enjoy the blog.Much thanks again. Really Great. salesforce Online Training

    ReplyDelete