Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't take the material of a part

Status
Not open for further replies.

thetetley

Mechanical
Sep 18, 2013
2
FR
Hello everyone,

I’m trying to make a macro that will get the name, mass, material and density of all the parts that I have selected. The problem is that I can’t get the material!! I tried a lot of different ways to do it but it doesn’t work. I’m not a specialist so it might be just a simple error but it’s impossible to find it… This is my macro:

Sub CATMain()

'--Crear una tabla vacia

Dim table()

'--Declarar el Excel y abrirlo

Dim Excel As Object
Dim objWorkbook
Dim objSheet
Dim workbooks As workbooks
Dim workbook As workbook
Dim Sheets As Object
Dim Sheet As Object
Dim worksheet As Excel.worksheet
Dim myworkbook As Excel.workbook
Dim myworksheet As Excel.worksheet

'--Verificacion de apertura de Excel
On Error Resume Next
Set Excel = GetObject(, "Excel.Application")
If Excel Is Nothing Then
IsExcelRunning = False 'el usuario no tiene Excel abierto
Err.Clear
Else
IsExcelRunning = True 'el usuario tiene Excel abierto
End If

'--Abrir Excel
On Error Resume Next
Set Excel = GetObject(, "Excel.Application")
If Excel Is Nothing Then
Set Excel = CreateObject("Excel.Application")
Err.Clear
Excel.Visible = True
End If
On Error GoTo 0

'--Abrir el archivo del macro
Set objWorkbook = Excel.Workbooks.Open("C:\Users\mq006483\Desktop\Macro de estimado de costos")
objWorkbook.Activate
Set objSheet = objWorkbook.WorkSheets.Item(1)

'--Seleccionar el Producto

Dim oSel As Selection
Set oSel = CATIA.ActiveDocument.Selection
ReDim strArray(0)
strArray(0)="Part"
Dim sStatus As String
sStatus = oSel.SelectElement3(strArray, "Select parts", False, CATMultiSelTriggWhenUserValidatesSelection, false)
oSel.Search "'Assembly Design'.Part,all"
Dim prdProduct as Part
Dim iProduct

'--Redimensionar la tabla

ReDim table(oSel.Count,4)

'--Contar el numero de productos y recorrerlos todos

For iProduct=1 to oSel.Count
Set prdProduct=oSel.Item(iProduct).Value

'--Cargar la part

Dim objInertia as Inertia
On Error Resume Next
Set objInertia=prdProduct.GetTechnologicalObject("Inertia")
Dim getMass As String
getMass = objInertia.Mass
Dim getDensity As String
getDensity = objInertia.Density
Dim partName As String
partName=prdProduct.Name
Dim mat As Object
Dim oManager As MaterialManager
Set oManager = prdProduct.GetItem("CATMatManagerVBExt")
oManager.GetMaterialOnPart prdProduct,mat
matName=mat.Name
MsgBox mat.Name

'--Llenar linea correspondiente

table (iProduct,1)=partName
table (iProduct,2)=getMass
table(iProduct,3)=matName
table (iProduct,4)=getDensity

Next

'--Llenar el Excel

objSheet.Cells(3,1)="Part Number"
objSheet.Cells(3,2)="Mass"
objSheet.Cells(3,3)="Material"
objSheet.Cells(3,4)="Density"


For r=1 to oSel.Count
objSheet.Cells(r+5,1)=table(r,1)
objSheet.Cells(r+5,2)=table(r,2)
objSheet.Cells(r+5,3)=table(r,3)
objSheet.Cells(r+5,4)=table(r,4)

Next

End Sub


I’m pretty sure it’s a problem with the syntax because I saw this macro in a forum and worked:


Sub CATMain()

Dim oProduct As Part
'Set oProduct = CATIA.ActiveDocument.Sheets.Item(1).Views.Item(3).GenerativeBehavior.Document
' If Item1 is a product the macro works if you set oManager.GetMaterialOnPart/Product on product
' If Item1 is a part the macro does not work if you set oManager.GetMaterialOnPart/Product on part

Set oProduct = CATIA.ActiveDocument.Selection.Item2(1).Value
' Use if you are directly in a Part
'Set oProduct = CATIA.ActiveDocument.Product
' Use if you are directly in a Product

Dim oManager As MaterialManager
Set oManager = oProduct.GetItem("CATMatManagerVBExt")

Dim oAppliedMaterial As Object

' Change to set for part or product
oManager.GetMaterialOnPart oProduct,oAppliedMaterial
'oManager.GetMaterialOnProduct oProduct,oAppliedMaterial

msgbox oAppliedMaterial.Name
' To display the name of the material

End Sub


Thanks a lot for your help!!
 
Replies continue below

Recommended for you

Hello, thank you for your answer!

Yes I know that in the forum they said it was not fully finished but when I used it, it worked fine on my product so I adapted it to my macro. The problem must be in the beginning of the macro because the beginning works, apparently, but I can't find the problem. I am going to try another way to go through each part and select it because it might be the reason why it doesn't recognize them

I used the Apply Material button so I thought it would work with the GetMaterialOnPart function but there's maybe an error there too.

I'll keep you informed but please keep helping me!! I'm convinced that the problem is something small and someone will laugh at the simplicity of my problem.

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Top