Hello everyone, I'm using ilogic to create an assembly, but I need to change the material of the components. I'm trying to change it when defining the component's operations, but it just doesn't seem to work. I'm trying to use the method:
iProperties.Material = "Copper"
But it just won't work.
The following is a part of my code where I make a part and try to change its material before placing it in the assembly:
Public Sub Main() '/ Assembly Definitions / Dim BaugDok As AssemblyDocument Dim BaugDef As AssemblyComponentDefinition Dim BautDok As PartDocument Dim BautDef As PartComponentDefinition Dim Martrix As Matrix Dim Occ As ComponentOccurrence check = False BaugDok = ThisApplication.ActiveDocument BaugDef = BaugDok.ComponentDefinition '/ Call the function that creates the part / BautDok = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject,ThisApplication.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject,SystemOfMeasureEnum.kDefaultSystemOfMeasure,DraftingStandardEnum.kDefault_DraftingStandard,"{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}")) BautDef = BautDok.ComponentDefinition Call LBW() '/ Give a 45 gegrees orientation / Martrix = ThisApplication.TransientGeometry.CreateMatrix() Martrix.SetToRotation(3.14159265358979 / 4, _ ThisApplication.TransientGeometry.CreateVector(0, 0, 0), ThisApplication.TransientGeometry.CreatePoint(0, 0, 0)) '/ Place component in origin / Martrix.SetTranslation(ThisApplication.TransientGeometry.CreateVector(0, 0, 0)) Occ = BaugDef.Occurrences.AddByComponentDefinition(BautDef, Martrix) End Sub '/ Functions to create part / Sub LBW() Dim partDoc As PartDocument Dim iApp As Inventor.Application Dim partDef As PartComponentDefinition Dim sketch1 As Sketch Dim sk As Sketch Dim tg As Inventor.TransientGeometry Dim SWCurve As SketchArc partDoc = ThisApplication.ActiveDocument partDef = partDoc.ComponentDefinition iApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application") tg = iApp.TransientGeometry Call LBWPROF(sketch1, tg, partDef, sk) iProperties.Material = "Copper" End Sub Public Function LBWPROF(sketch1, tg, partDef, sk) As Double Dim Path As Path Dim PF As Profile Dim SWP As SweepFeature sketch1 = partDef.Sketches.Add(partDef.WorkPlanes.Item(3)) sketch1.SketchCircles.AddByCenterRadius(tg.CreatePoint2d(0, 0), 0.47625) sketch1.SketchCircles.AddByCenterRadius(tg.CreatePoint2d(0, 0), .42035) sk = partDef.Sketches.Add(partDef.WorkPlanes.Item(2)) sk.SketchArcs.AddByCenterStartEndPoint(tg.CreatePoint2d(-2.54/2,0), tg.CreatePoint2d(0,0), tg.CreatePoint2d(-2.54,0),False) SWCurve = partDef.Sketches.Item(2).SketchArcs.Item(1) Path = partDef.Features.CreatePath(SWCurve) PF = partDef.Sketches.Item(1).Profiles.AddForSolid(True) SWP = partDef.Features.SweepFeatures.AddUsingPath(PF, Path, kJoinOperation) End Function
Thanks in advance.
Solved! Go to Solution.
Hello everyone, I'm using ilogic to create an assembly, but I need to change the material of the components. I'm trying to change it when defining the component's operations, but it just doesn't seem to work. I'm trying to use the method:
iProperties.Material = "Copper"
But it just won't work.
The following is a part of my code where I make a part and try to change its material before placing it in the assembly:
Public Sub Main() '/ Assembly Definitions / Dim BaugDok As AssemblyDocument Dim BaugDef As AssemblyComponentDefinition Dim BautDok As PartDocument Dim BautDef As PartComponentDefinition Dim Martrix As Matrix Dim Occ As ComponentOccurrence check = False BaugDok = ThisApplication.ActiveDocument BaugDef = BaugDok.ComponentDefinition '/ Call the function that creates the part / BautDok = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject,ThisApplication.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject,SystemOfMeasureEnum.kDefaultSystemOfMeasure,DraftingStandardEnum.kDefault_DraftingStandard,"{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}")) BautDef = BautDok.ComponentDefinition Call LBW() '/ Give a 45 gegrees orientation / Martrix = ThisApplication.TransientGeometry.CreateMatrix() Martrix.SetToRotation(3.14159265358979 / 4, _ ThisApplication.TransientGeometry.CreateVector(0, 0, 0), ThisApplication.TransientGeometry.CreatePoint(0, 0, 0)) '/ Place component in origin / Martrix.SetTranslation(ThisApplication.TransientGeometry.CreateVector(0, 0, 0)) Occ = BaugDef.Occurrences.AddByComponentDefinition(BautDef, Martrix) End Sub '/ Functions to create part / Sub LBW() Dim partDoc As PartDocument Dim iApp As Inventor.Application Dim partDef As PartComponentDefinition Dim sketch1 As Sketch Dim sk As Sketch Dim tg As Inventor.TransientGeometry Dim SWCurve As SketchArc partDoc = ThisApplication.ActiveDocument partDef = partDoc.ComponentDefinition iApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application") tg = iApp.TransientGeometry Call LBWPROF(sketch1, tg, partDef, sk) iProperties.Material = "Copper" End Sub Public Function LBWPROF(sketch1, tg, partDef, sk) As Double Dim Path As Path Dim PF As Profile Dim SWP As SweepFeature sketch1 = partDef.Sketches.Add(partDef.WorkPlanes.Item(3)) sketch1.SketchCircles.AddByCenterRadius(tg.CreatePoint2d(0, 0), 0.47625) sketch1.SketchCircles.AddByCenterRadius(tg.CreatePoint2d(0, 0), .42035) sk = partDef.Sketches.Add(partDef.WorkPlanes.Item(2)) sk.SketchArcs.AddByCenterStartEndPoint(tg.CreatePoint2d(-2.54/2,0), tg.CreatePoint2d(0,0), tg.CreatePoint2d(-2.54,0),False) SWCurve = partDef.Sketches.Item(2).SketchArcs.Item(1) Path = partDef.Features.CreatePath(SWCurve) PF = partDef.Sketches.Item(1).Profiles.AddForSolid(True) SWP = partDef.Features.SweepFeatures.AddUsingPath(PF, Path, kJoinOperation) End Function
Thanks in advance.
Solved! Go to Solution.
Solved by dialunau. Go to Solution.
This is an example of my project, I'm trying to change the material but nothing seems to work.
This is an example of my project, I'm trying to change the material but nothing seems to work.
I think it would be easier for you if you set up your "LBW" sub so it requests the input variables of the PartDocument and maybe even the PartComponentDefinition, so you don't have to redefine them within the sub. I noticed that currently within that sub you are defining your variable "partDoc" equal to ThisApplication.ActiveDocument. I think this is wrong, because the assembly is the active document. You just created the part document just before you called that sub, and set it as the value of your "BautDok" variable, so it would make sense to supply that "BautDok" variable to the sub, instead of attempting to redefine it within the sub.
Also, since you are simply using all the default settings when creating the part document, you can greatly simplify that line of code from this
BautDok = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject,ThisApplication.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject,SystemOfMeasureEnum.kDefaultSystemOfMeasure,DraftingStandardEnum.kDefault_DraftingStandard,"{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"))
to this:
BautDok = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject)
and it should still work OK.
Also, after you've defined the variable "oMaterial As Asset", I think you may need to change your line which sets its value from this:
oMaterial = partDoc.Materials.Item("Copper")
to this:
oMaterial = partDoc.MaterialAssets.Item("Copper")
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.
Wesley Crihfield
(Not an Autodesk Employee)
I think it would be easier for you if you set up your "LBW" sub so it requests the input variables of the PartDocument and maybe even the PartComponentDefinition, so you don't have to redefine them within the sub. I noticed that currently within that sub you are defining your variable "partDoc" equal to ThisApplication.ActiveDocument. I think this is wrong, because the assembly is the active document. You just created the part document just before you called that sub, and set it as the value of your "BautDok" variable, so it would make sense to supply that "BautDok" variable to the sub, instead of attempting to redefine it within the sub.
Also, since you are simply using all the default settings when creating the part document, you can greatly simplify that line of code from this
BautDok = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject,ThisApplication.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject,SystemOfMeasureEnum.kDefaultSystemOfMeasure,DraftingStandardEnum.kDefault_DraftingStandard,"{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"))
to this:
BautDok = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject)
and it should still work OK.
Also, after you've defined the variable "oMaterial As Asset", I think you may need to change your line which sets its value from this:
oMaterial = partDoc.Materials.Item("Copper")
to this:
oMaterial = partDoc.MaterialAssets.Item("Copper")
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.
Wesley Crihfield
(Not an Autodesk Employee)
Thanks a lot for your explanations, they really gave me a lot of help. However, I still couldn't change the material after those modifications to my code.
I found this thread, and they gave me exactly what I needed to change the material of my part:
So I just added the following code right after calling my LBW Function:
'/ Change part material / Dim oPartMat As MaterialAsset Dim AssetLibs As AssetLibraries = ThisApplication.AssetLibraries Dim AssetLib As AssetLibrary For Each AssetLib In AssetLibs Dim oMaterialAssets As AssetsEnumerator = AssetLib.MaterialAssets Dim oMaterial As MaterialAsset For Each oMaterial In oMaterialAssets If oMaterial.DisplayName = "Steel, Galvanized" Then oPartMat = oMaterial Exit For End If Next Next BautDok.ActiveMaterial = oPartMat
Thanks a lot for your explanations, they really gave me a lot of help. However, I still couldn't change the material after those modifications to my code.
I found this thread, and they gave me exactly what I needed to change the material of my part:
So I just added the following code right after calling my LBW Function:
'/ Change part material / Dim oPartMat As MaterialAsset Dim AssetLibs As AssetLibraries = ThisApplication.AssetLibraries Dim AssetLib As AssetLibrary For Each AssetLib In AssetLibs Dim oMaterialAssets As AssetsEnumerator = AssetLib.MaterialAssets Dim oMaterial As MaterialAsset For Each oMaterial In oMaterialAssets If oMaterial.DisplayName = "Steel, Galvanized" Then oPartMat = oMaterial Exit For End If Next Next BautDok.ActiveMaterial = oPartMat
Can't find what you're looking for? Ask the community or share your knowledge.