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!

CATIA Macro: How to select a "Part" with SelectElement2 to ApplyMaterialOnPart ???

Status
Not open for further replies.

bastiensegond

Aerospace
Jul 5, 2011
21
FR
Hello everybody,

In my product I want with macro to :

1- Select a Part with the mouse,
2- Apply a material on this Part.

This is my macro but it doesn't work very well.


Sub Main()


'Selection de la part
Dim MySelection
Dim MyDoc As Document
Dim MyPart As PartDocument
Dim MyProduct As ProductDocument

Set MyProduct = CATIA.ActiveDocument
Set MySelection = MyProduct.selection
ReDim strArray(0)
strArray(0) = "Part"

Dim Status As String
Status = MySelection.SelectElement2(strArray, "Select a Part", True)

If Status = "Cancel" Then
Exit Sub
End If


'Ouverture et gestion de la part
Set oPartDocument = CATIA.ActiveDocument

'Dim oRootPart As part
'Set oRootPart = oPartDocument.part

Dim oRootPart As Product
Set oRootPart = oPartDocument.Product

Set oManager = oRootPart.GetItem("CATMatManagerVBExt")
Dim oMainBody As Body
'Set oMainBody = oRootPart.MainBody

'Ouverture du catalogue matériaux
Dim oMaterial_document As Document
Set oMaterial_document = CATIA.Documents.Open("L:\Programme_attribution_matériaux\Catalogue_Bastien.CATMaterial")

'Selection de la famille
Dim cFamilies_list As MaterialFamilies
Set cFamilies_list = oMaterial_document.Families
Dim oFirst_family As MaterialFamily
Set oFirst_family = cFamilies_list.Item(1)


'Selection du matériau
Dim cMaterials_list As Materials
Set cMaterials_list = oFirst_family.Materials
Dim oMaterial As Material

'Boucle pour ajouter les noms des matériaux dans la combobox
Dim nombremat As Integer
nombremat = cMaterials_list.Count
For i = 1 To nombremat
UserForm7.ComboBox1.AddItem (cMaterials_list.Item(i).Name)
Next i

UserForm7.Show

'Modifier valeur propriétés matériaux
SAMYoungModulus = CATIA.ActiveDocument.Families.Item(1).Materials.Item(UserForm7.ComboBox1.Value).AnalysisMaterial.PutValue("SAMYoungModulus", UserForm7.MDY.Value)
SAMDensity = CATIA.ActiveDocument.Families.Item(1).Materials.Item(UserForm7.ComboBox1.Value).AnalysisMaterial.PutValue("SAMDensity", UserForm7.D.Value)
SAMThermalExpansion = CATIA.ActiveDocument.Families.Item(1).Materials.Item(UserForm7.ComboBox1.Value).AnalysisMaterial.PutValue("SAMThermalExpansion", UserForm7.ET.Value)
SAMPoissonRatio = CATIA.ActiveDocument.Families.Item(1).Materials.Item(UserForm7.ComboBox1.Value).AnalysisMaterial.PutValue("SAMPoissonRatio", UserForm7.CDP.Value)
SAMShearModulus = CATIA.ActiveDocument.Families.Item(1).Materials.Item(UserForm7.ComboBox1.Value).AnalysisMaterial.PutValue("SAMShearModulus", UserForm7.MDC.Value)

'Nom du matériau
Set oMaterial = cMaterials_list.Item(UserForm7.ComboBox1.Value)

'Appliquer un matériau
LinkMode = 1
oManager.ApplyMaterialOnPart MySelection.part, oMaterial, LinkMode 'ERROR IS HERE

'Nettoyage des userforms
UserForm7.ComboBox1.Clear
UserForm7.MDY.Value = ""
UserForm7.MDC.Value = ""
UserForm7.CDP.Value = ""
UserForm7.D.Value = ""
UserForm7.ET.Value = ""

'Enregistrement et fermeture du catalogue
CATIA.Documents.Item("Catalogue_Bastien.CATMaterial").Save
CATIA.Documents.Item("Catalogue_Bastien.CATMaterial").Close


End Sub





Thanks !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Top