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!!
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!!