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

Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X

How to set material in a part with excel vba

cstarnes
12-Amethyst

How to set material in a part with excel vba

I've searched everywhere for an example of this, and am coming up zeros. The only thing I've found is this article: 


https://www.ptc.com/en/support/article?n=CS148500

... and it doesn't work at all. (The "session" object isn't even instantiated in the example).

Here is what I'm doing:

' Dim statements
' connection stuff
...
... Set oModelDescriptor = oModelDescriptorCreate.Create(ModelType, ModelName, Null) Set oModel = oSession.RetrieveModel(oModelDescriptor)
...
blah blah
...
' do some stuff


All of that ^ works fine, as I can read/set parameters, etc.

I can also read the current material with this:

' get material
If oModel.Type = EpfcModelType.EpfcMDL_PART Then
     Set part = oModel
     Set material = part.CurrentMaterial
     .Cells(iRow, colOriginalMaterial).Value = material.Name
End If


But I'm not having any luck at all setting a new material. I'm trying something like this, but nadda (oModel is set earlier in the procedure):

' Set material

Dim part As IpfcPart
Dim material As IpfcMaterial

Set part = oModel
Set material = part.RetrieveMaterial("steel.mtl")
part.CurrentMaterial = material

 

Any help?

3 REPLIES 3
cstarnes
12-Amethyst
(To:cstarnes)

Helllooooo.......   is this thing on?

Does anyone ever actually answer questions here? I don't think I've ever had a single response.  🙂

Maybe Creo can't found mtl file.

Try manually add new material from mtl into part.

Then use GetMaterial (Name as string). This function return IpfcMaterial. And then set this material to the part properties

part.CurrentMaterial = material

 

PS: in PTC case material = part.RetrieveMaterial("Iron") loading material by mtl file name WITHOUT extension .mtl. In your code you using name with extension

cko
12-Amethyst
12-Amethyst
(To:cstarnes)

- Specifies the material. calculate the weight
- Material file extension is not used
- Modify the code and use

 

 

Set oModel = oSession.CurrentModel
Set oSolid = oModel

'// config.pro option
Call oSession.SetConfigOption("pro_material_dir", Worksheets("data").Cells(20, "B"))
Call oSession.SetConfigOption("mass_property_calculate", "automatic")

Dim oPart As IpfcPart
Dim oMaterial As IpfcMaterial

If Worksheets("File Info").Cells(6, "E") Is Nothing Then

MsgBox "Please select a material file", vbInformation, "ToolBOX VBA"

Exit Sub

ElseIf Worksheets("File Info").Cells(6, "E") = "prt" Then

Set oPart = oModel
Set oMaterial = oPart.RetrieveMaterial(Worksheets("File Info").Cells(12, "C"))

Worksheets("File Info").Cells(13, "C") = oMaterial.Name
oPart.CurrentMaterial = oMaterial

Else

MsgBox "Not a part file", vbInformation, "ToolBOX VBA"

Exit Sub

End If

'// SET Regenerate

Dim RegenInstructions As New CCpfcRegenInstructions
Dim oInstrs As IpfcRegenInstructions

Set oInstrs = RegenInstructions.Create(False, False, Nothing)

'// Regenerate 실행

Call oSolid.Regenerate(oInstrs)
Call oSolid.Regenerate(oInstrs)

'//Mass

Dim oMassProperty As IpfcMassProperty
Set oMassProperty = oSolid.GetMassProperty("")
Worksheets("File Info").Cells(17, "C") = oMassProperty.Mass

MsgBox "Part file weight was calculated", vbInformation, "ToolBOX VBA"

End Sub

Announcements


Top Tags