Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X
I am trying to learn/use this Creo API with my limited Visual Basic (/for applications) knowledge. I am kind of copying and pasting my way thru this. I am trying to create a code using Excel to update the revision block for a set of drawings (the block is dumb). But I can't seem to even get Excel to find the tables in a currently opened drawing. If I can get the code to identfy the table, modify the data in the table, and save the file, I will be a happy camper.
Dont laugh.....I am a Mechanical not a Software Eng.
Public Sub release_drawing()
On Error GoTo RunError
' This routine will open a Pro/E file from your WorkingDirectory in a new window:
'
Dim asynconn As New pfcls.CCpfcAsyncConnection
Dim conn As pfcls.IpfcAsyncConnection
Dim session As pfcls.IpfcBaseSession
Dim oModel As pfcls.IpfcModel
'Make an asynchronous connection with Pro/ENGINEER
Set conn = asynconn.Connect(Null, Null, ".", 5)
'Get the current session
Set session = conn.session
'List tables found on open drawing
Dim TableObj As pfcls.IpfcTableOwner
Dim table_list As CpfcTables
Set table_list = TableObj.ListTables() '<-----------------------------ERROR OCCURS HERE
MsgBox table_list
'
'bunch of error handling, variable clean up below here, and disconnetion below here that all seems to be funcitoning properly.
'
I have found the documentation for this API lacking to say the least. If anyone can help it would be greatly appreciated.
Also if anyone knows of some good resources/examples to help learn my way around this Creo API that would help alot as well.
Thanks in advance.
Parallel to the above question....does anyone even us the Creo API or does everyone just slog thru hundreds of drawings and models because that is what they are use to?
and also bump.
hmmmmm moderation eh....lets see what this does.
Two things:
1st: Bump
2nd: Is automating Creo something anyone does or is it just that everyone else in the world works for a company big enough to aford windchill? (not being a smarta** this is a legit question).
Thanks
Hi brian,
i also did some Pro|E programming using VB with WF4.
Even if i never did code for tables, i watched your code and noticed this part of your code:
'List tables found on open drawing
Dim TableObj As pfcls.IpfcTableOwner
Dim table_list As CpfcTables
Set table_list = TableObj.ListTables()
first you declare TableObj and table_List
and then you try to get the tables by asking TableObj to give you a list of tables, but...
i can not see code filling the variable TableObj with an valid object, therefore, in my opinion, TableObj is nothing when calling TableObj.ListTables(), isn't it?
maybe you should add some code like this to get TableObj first (not tested!!)
oModel = session.CurrentModel
TableObj = ctype(oModel,IpfcTableOwner)
Due to your comment in the code, i think you are german, so here the same in my motherlanguage too
Du deklarierst TableObj als Tabellenbesitzer, damit erhältst du eine leere Variable vom Typ TableOwner
Aber bis zu dem Aufruf Set table_list = TableObj.ListTables() weist du dieser Variable kein Objekt zu.
Und solange diese Variable Nothing ist, kann sie keine Tabellen ausgeben.
Ich denke du musst noch ein paar Codezeilen ergänzen. (siehe oben)
Grüße
Andreas
Andreas,
Thank you very much for your reply I was begining to think I was the only one who had ever used this API.
I am sorry I am not German.......I'm from Texas (that is probably why my english is so bad!)
Once I went thru and stepped thru the logic of my code I came to the same conclusion as you. That I needed to define "TableObj" in some manner.
Below is my updated code
Public Sub release_drawing()
Dim i As Integer
On Error GoTo RunError
' This routine will open a Pro/E file from your WorkingDirectory in a new window:
'
Dim asynconn As New pfcls.CCpfcAsyncConnection
Dim conn As pfcls.IpfcAsyncConnection
Dim session As pfcls.IpfcBaseSession
Dim oModel As pfcls.IpfcBaseSession
'Make an asynchronous connection with Pro/ENGINEER
Set conn = asynconn.Connect(Null, Null, ".", 5)
'Get the current session
Set session = conn.session
Dim TableObj As pfcls.IpfcTableOwner
Dim table_list As CpfcTables
Set oModel = session.CurrentModel
Set TableObj = oModel.IpfcTableOwner <------------------------------------compile error here
Set table_list = TableObj.ListTables
MsgBox table_list
But now I get a Compile error: Method or data member not found
on the "set TableObj" line.
I couldn't use your recomended "cType" method as I am using Visual Basic for Applications (excel VBA) and it doesn't support "ctype".
I have researched cType and (correct me if I am wrong) all it does is change the variable type, for instance from an Varient to an Object, correct?
So I just "Dim-ed" TableObj as the IpfcTableOwner.
I am begining to think I am out of my league, I dont even know if I have the oModel and TableObj variables defined correctly. I hope all of this makes sense I am in no way a programmer I am just trying to learn as I go.
Any other help you can give is greatly appreciated.
Brian
Hey Brian,
using VBA may result in a challenge but shouldn't be unsolvable.
I don't mind bad english when it's still understandable, in fact i don't think yours is bad.
That you need to define TableObj is fact.
And when i think of my programm reading parameters, i can imagine the solution isn't that complicate as we think.
Here an extract of my parameter reading program:
Dim model as IpfcModel = Nothing
model = Session.CurrentModel
Dim p As IpfcParameterOwner
'normally i would use ctype(model, IpfcParameterOwner) for correctness here
'but in this early stage of my .net programming knowledge, I prefered to enable auto conversion
p = model
Dim pp As IpfcParameter = p.GetParam("POSITIONSNUMMER")
Dim tempvalue as String = pp.GetScaledValue.StringValue
So, as you can see, i also needed some kind of Owner-Object and the solution was:
The model IS the owner of Parameters and you just can use it as IpfcParameterOwner.
Please try the Codeline "Set TableObj = oModel" and let me know if it works!
best whishes
Andreas
Andreas,
I don't known how you knew to do that but it worked! I had to update a couple of other things at the same time but here is what the working code ended up looking like. I have noted the things I had to change.
Public Sub release_drawing()
Dim i As Integer
On Error GoTo RunError
' This routine will open a Pro/E file from your WorkingDirectory in a new window:
'
Dim asynconn As New pfcls.CCpfcAsyncConnection
Dim conn As pfcls.IpfcAsyncConnection
Dim session As pfcls.IpfcBaseSession
Dim oModel As pfcls.IpfcModel '<-------------changed
'Make an asynchronous connection with Pro/ENGINEER
Set conn = asynconn.Connect(Null, Null, ".", 5)
'Get the current session
Set session = conn.session
Dim TableObj As pfcls.IpfcTableOwner
Dim table_list As CpfcTables
Set oModel = session.CurrentModel
Set TableObj = oModel '<-------------changed
Set table_list = TableObj.ListTables()
MsgBox table_list.Count '<-------------changed
If I am ever in Germany (or where ever you are curretly living) I am going to look you up and buy you the biggest beer they have!
All I have to do now is figure out what to do with the tables now that I have them!
I only have one last semi-related question. How did you become profecient working with this API? Did you already have formal VB training, or was it just trial and error? Did you find any good resources for working with the API (books, websites, etc.)?
Thanks again for your help,
Brian
Hi Brian,
i'm glad it helped you.
And your beer idea is a great one
I didn't start programming Pro|E api from scratch, I already hat some years of self-explored-expierience in VBA programming of Excel
If I would have startet with Pro|E api and programming at the same time, maybe i would have thrown it away
Many of my Code i took from the documents delivered with your installation.
Have a look for a folder called "vbapi" in your installationfolder. One helpful document is the vbug.pdf but you also can use the wizard.
As I now found this forum, maybe I'll post here once too and then you can answer me
Good luck for your project, feel free to post again!
Andreas
Brian
Here is some code i use for managing drawing tables.
In general this code loops and searches all tables in the active drawing that came from a format.
The code looks for a table where the content of the first cell="Qty." (this is to make sure i got the correct table)
It also deletes all tables that does not confirm to the above.
Hope it helps 🙂
By the way, VBA in Creo is a great tool so dont give up....
Dim table As IpfcTable =
Nothing
Dim tables As
IpfcTables
tables = active_drawing.ListTables()
For i = 0 To
tables.Count - 1
table = tables.Item(i)
If table.CheckIfIsFromFormat(sheetno) = True
Then
' Manage BOM table
If KeepBOM = True
Then
' Check if table is a BOM table
Dim MyCell As
IpfcTableCell
MyCell = (
New CCpfcTableCell
).Create(1, 1)
Dim
mode
mode = (
New CCpfcParamMode
).DWGTABLE_NORMAL
Try
Dim
textseq = table.GetText(MyCell, mode)
' If table is not a BOM table, delete it
If textseq.Item(0).ToString <> "Qty."
Then
active_drawing.DeleteTable(table,
False
)
End
If
Catch
' Cell has no value, delete table
active_drawing.DeleteTable(table,
False
)
End
Try
Else
active_drawing.DeleteTable(table,
False
)
End
If
End
If
Next
Hugo,
What you are describing is exactly what I was trying to do! Loop thru the first value in all tables to try and find the one I am looking for and update its values. It might take me a day or two to get back to this but as soon as I get a few minutes to work on this I will try to integrate your code with mine and I will let you know what happens.
Thanks,
Brian
Hi,
any update on this topic,
same i want to call drawing table parameter from excle sheet.
i am using creo2.0