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

VBA Excel - ImportNewModel

SOLVED
jkramer
Regular Member

VBA Excel - ImportNewModel

Hi,

I'm working on an Excel VBA tool that imports a lot of pvz files into Creo and saves them as .asm's. Works like a charm when I do this by hand (obviously, these are not real CAD models but facet geometry).

If I run the code below, I get a Type Mismatch error on te line starting with Set Model = session.ImportNewModel...

The weird thing is Creo actually imports the pvz, because if I do File > Open > In Session, I can see and open my example asm (testjaap.asm).

Does anyone know how I can get the model opened?? I tried all kinds of things, but nothing works so far...

Thnx!
Jaap

code:

Sub import_pvzs()
Dim asynconn As New pfcls.CCpfcAsyncConnection
Dim conn As pfcls.IpfcAsyncConnection
Dim session As pfcls.IpfcBaseSession
Dim model As IpfcModel
Set conn = asynconn.Connect("", "", ".", 5)
Set session = conn.session
Set model = session.ImportNewModel("C:\local\test_buildassyfromtextfile_transformation_matrix\TEST_ASSY_A0120069, Box Packer\A0133910, CRASH CAMS - Cabinet Module.pvz", EpfcNewModelImportType.EpfcIMPORT_NEW_PRODUCTVIEW, EpfcModelType.EpfcMDL_ASSEMBLY, "testjaap", Nothing)
conn.Disconnect (2)
End Sub

1 ACCEPTED SOLUTION

Accepted Solutions

Re: VBA Excel - ImportNewModel

Strange. Your code worked for me.

 

Ideas:

  • Do you have Tools>References>'Creo VBA API Type Library for Creo Parametric' set?
  • Creo 7 introduced part multi bodies - not sure what impacts this has on the Creo VB API
  • VBA 'Option Explicit' is turned on? I don't think this would affect this but I usually have this off.
  • Is mdl set a public variable somewhere else in VBA?
  • Could always open a case with PTC.

View solution in original post

7 REPLIES 7

Re: VBA Excel - ImportNewModel

Hi,

sample code for STEP import...

Dim mdl As IpfcModel = session.ImportNewModel(
    "D:\WorkDir\Example\heatSink.step",
    EpfcNewModelImportType.EpfcIMPORT_NEW_STEP,
    EpfcModelType.EpfcMDL_PART,
    "MyNewPart",
    Nothing)
mdl.Display()

 


Martin Hanák

Re: VBA Excel - ImportNewModel

Thnx for your reply!

Unfortunately, this seems to be the syntax for Visual Studio, not for Excel.

In Excel I'm not allowed to combine the Dim and the command. And if I split the line in:

Dim mdl As IpfcModel
Set mdl = session.ImportNewModel("C:\local\test_buildassyfromtextfile_transformation_matrix\TEST_ASSY_A0120069, Box Packer\A0133910, CRASH CAMS - Cabinet Module.pvz", EpfcNewModelImportType.EpfcIMPORT_NEW_PRODUCTVIEW, EpfcModelType.EpfcMDL_ASSEMBLY, "testjaap", Nothing)

I get the Type Mismatch error, even though session.ImportNewModel is an IpfcModel according to the documentation (and according to your example).

Anyone have a clue how to cope with these kind of situations in VBA Excel? It's not the first time Excel bugs me with object type problems...

Thnx!

Re: VBA Excel - ImportNewModel

I was able to get this to work in Excel VBA:

Dim mdl As IpfcModel
Set mdl = session.ImportNewModel("C:\Users\Public\Documents\testasm.pvz", EpfcNewModelImportType.EpfcIMPORT_NEW_PRODUCTVIEW, EpfcModelType.EpfcMDL_ASSEMBLY, "mynewpart", Nothing)

'Make sure model is displayed and active.
Dim oWindow As pfcls.IpfcWindow
Set oWindow = session.GetModelWindow(mdl) 'If no model was open in Creo session, model automatically displays.
If oWindow Is Nothing Then Set oWindow = session.CreateModelWindow(mdl) 'Create window before displaying model
mdl.Display
oWindow.Activate 'Active window with new model in it

 The "mynewpart" name doesn't seem to work when importing PVZ assemblies though.

Re: VBA Excel - ImportNewModel

Thnx!

I tried your example, but on my PC I still get the exact same Type Mismatch error.

I also tried with a single part (changing EpfcMDL_ASSEMBLY to EpfcMDL_PART), but that gives the Type Mismatch error, too.

Here's my complete code:

Sub import_pvzs()
Dim asynconn As New pfcls.CCpfcAsyncConnection
Dim conn As pfcls.IpfcAsyncConnection
Dim session As pfcls.IpfcBaseSession
Set conn = asynconn.Connect("", "", ".", 5)
Set session = conn.session
Dim mdl As IpfcModel
Dim oWindow As pfcls.IpfcWindow
'session.ChangeDirectory ("C:\local\test_backup")
Set mdl = session.ImportNewModel("C:\local\test_assy.pvz", EpfcNewModelImportType.EpfcIMPORT_NEW_PRODUCTVIEW, EpfcModelType.EpfcMDL_ASSEMBLY, "test_assy1", Nothing)
'Make sure model is displayed and active.
Set oWindow = session.GetModelWindow(mdl)
If oWindow Is Nothing Then Set oWindow = session.CreateModelWindow(mdl)
mdl.Display
oWindow.Activate 'Active window with new model in it
'session.CurrentModel.Save
conn.Disconnect (2)
End Sub

Can it be that there's something wrong in the configuration of Excel?!

Thanks!

Re: VBA Excel - ImportNewModel

Hi,

maybe version of Creo + version of Excel affects the result.


Martin Hanák

Re: VBA Excel - ImportNewModel

Strange. Your code worked for me.

 

Ideas:

  • Do you have Tools>References>'Creo VBA API Type Library for Creo Parametric' set?
  • Creo 7 introduced part multi bodies - not sure what impacts this has on the Creo VB API
  • VBA 'Option Explicit' is turned on? I don't think this would affect this but I usually have this off.
  • Is mdl set a public variable somewhere else in VBA?
  • Could always open a case with PTC.

View solution in original post

Re: VBA Excel - ImportNewModel

Hi,

thanks again!

Problem solved... I tried the code on a computer running Creo 6, and it worked.

Then, I went back to the PC running Creo 4 (our current version), and saw that Excel had references to both the Creo 2 and Creo 4.

After unchecking Creo 2.0 in the list all worked fine.

I find this reference list in Excel weird and confusing...

All in all a happy end.

Announcements