Scroll bar with material in part-template

Scroll bar with material in part-template

Gwennberg
Advocate Advocate
1,160 Views
11 Replies
Message 1 of 12

Scroll bar with material in part-template

Gwennberg
Advocate
Advocate

Hi

I have made a library with specific company materials. Now I want a ilogic script in my part template that start asking me (with a scrollbar) which material I want to use. Any hint?

//Goran

0 Likes
Accepted solutions (2)
1,161 Views
11 Replies
Replies (11)
Message 2 of 12

Cadkunde.nl
Collaborator
Collaborator

This gets all materials from the library to a multivalue parameter.

You just need to make a parameter name "materials" and make it multi value.

You can make a form and set this parameter to a List Box control type

 

Dim oApp As Inventor.Application = ThisApplication
Dim odoc As PartDocument = ThisDoc.Document
Dim MyArrayList As New ArrayList


For i = 1 To odoc.MaterialAssets.Count
	MyArrayList.Add(oApp.ActiveMaterialLibrary.MaterialAssets.Item(i).DisplayName)

Next

MultiValue.List("materials") = MyArrayList

 Or use the Listbox snippet:

d0 = InputListBox("Prompt", MultiValue.List("materials"), d0, Title := "Title", ListName := "List")
0 Likes
Message 3 of 12

WCrihfield
Mentor
Mentor

This is a similar scenario, but gets the materials from an actual asset library, instead of from the current document.  You just need to edit the name of the library, or edit the full file name of your library file for it to open and source the materials from.

 

Dim oMatList As New List(Of String)
Dim oALibs As AssetLibraries = ThisApplication.AssetLibraries
Dim oLib As AssetLibrary
Try
	oLib = oALibs.Item("MyMatLibName")
Catch
	oLib = oALibs.Open("FullFileNameOfLibraryFile")
Catch
	MsgBox("Could not find that material library.", vbCritical, "")
	Exit Sub
End Try
If IsNothing(oLib) Then Exit Sub
Dim oMAs As AssetsEnumerator = oLib.MaterialAssets
If oMAs.Count = 0 Then Exit Sub
For Each oMA As MaterialAsset In oMAs
	oMatList.Add(oMA.DisplayName)
Next
Dim oChosenMatName As String
oChosenMatName = InputListBox("", oMatList, oMatList.Item(0), "Materials", "List Of Materials")
Dim oChosenMat As MaterialAsset = oMAs.Item(oChosenMatName)
MsgBox("oChosenMat.DisplayName = " & oChosenMat.DisplayName,,"")

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 12

Gwennberg
Advocate
Advocate

Thank's Mr Crihfield

The script works fine but I have still two problems.

1) The script works on an Inventor-adsklib-file but not on our company unique file. Got error, see attached screendumps 1 and 2

2) The material does not seems to change then I use the Inventor-adsklib-file which works. see screendump 3

BR/Goran

 

0 Likes
Message 5 of 12

Cadkunde.nl
Collaborator
Collaborator

Looking back at my code i see i made an error.

 

For i = 1 To odoc.MaterialAssets.Count
	MyArrayList.Add(oApp.ActiveMaterialLibrary.MaterialAssets.Item(i).DisplayName)

Should be

 

For i = 1 To oApp.ActiveMaterialLibrary.MaterialAssets.Count
	MyArrayList.Add(oApp.ActiveMaterialLibrary.MaterialAssets.Item(i).DisplayName)

 

0 Likes
Message 6 of 12

WCrihfield
Mentor
Mentor

What type of file is your 'company unique file', if not a ".adsklib"?  I did not know that you wanted the chosen material to be set as the 'active' material in the 'active' document, but that can be easily fixed.  You can replace the MsgBox line at the end of the code I posted with these following lines:

Dim oLocalCopy As MaterialAsset = Nothing
Try : oLocalCopy = oChosenMat.CopyTo(ThisDoc.Document, True) : Catch : End Try
Try : ThisDoc.Document.ActiveMaterial = oLocalCopy : Catch : End Try

 You can not set a 'library only' asset directly as the 'active' one in a document.  You first have to create a 'local' copy of it, then set that local copy as the 'active' one.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 12

Gwennberg
Advocate
Advocate

Thanks Mr Crihfield

That must be something corupt with our adsklib-file. I also tried Cadkundes answer but I got the same error when the code tried to read our adsklib-file. Strange. I have no problem when I use the material-changing manuel in this file.

 

0 Likes
Message 8 of 12

Cadkunde.nl
Collaborator
Collaborator

Maybe check your project file and see if appearance and Material library has no error and your company adsklib is set to default

0 Likes
Message 9 of 12

GosponZ
Collaborator
Collaborator

Code is working perfect but it is list of all material in Library. Would it be possible to see list from favorites?

Thanks

0 Likes
Message 10 of 12

Gwennberg
Advocate
Advocate

Monday morning and the adsklib works!?! Strange. 
I attached your last code and now I got a local copy of my choosen material but how can I get it to be the active one in my new part? Can it be done with the script?

//Goran

0 Likes
Message 11 of 12

WCrihfield
Mentor
Mentor
Accepted solution

Hi @Gwennberg.  However your current code is laid out, once you have a reference to a local copy of the material (Asset) you want to set as your part's 'active' material, then you can set that as the value of the PartDocument.ActiveMaterial property.  That is a Read/Write property whose value is an Asset.  The part type document is the only which has that property.  Assemblies do not have a set material, because they do not contain any of their own solids...with the exception of weldment type assemblies and the weld beads you can create in them.  You can see this is what I was attempting to do in the last line of code I posted above.

Edit:  There is also an iLogic shortcut snippet (Link1, Link2) for setting the material of a part (or the material of part type component in an assembly), if you like that sort of thing.  But it might not be so obvious which document it will effect in some complicated situations:

iProperties.Material = "MyMaterialName"

...plus, I assume that a material with that name must already exist within the target part for this to work.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 12 of 12

Gwennberg
Advocate
Advocate
Accepted solution

Thank you  WCrihfield

Now it works as I want. 

BR/Goran

 

Dim oMatList As New List(Of String)
Dim oALibs As AssetLibraries = ThisApplication.AssetLibraries
Dim oLib As AssetLibrary
Try
	oLib = oALibs.Item("EasyLaserMaterial")	
Catch
	oLib = oALibs.Open("C:\_Vault Workspace\System Configurations\Inventor 2023\Design Data\Materials\EasyLaserMaterial.adsklib")
Catch
	MsgBox("Could not find that material library.", vbCritical, "")
	Exit Sub
End Try
If IsNothing(oLib) Then Exit Sub
Dim oMAs As AssetsEnumerator = oLib.MaterialAssets
If oMAs.Count = 0 Then Exit Sub
For Each oMA As MaterialAsset In oMAs
	oMatList.Add(oMA.DisplayName)
Next
Dim oChosenMatName As String
oChosenMatName = InputListBox("", oMatList, oMatList.Item(0), "Materials", "List Of Materials")
Dim oChosenMat As MaterialAsset = oMAs.Item(oChosenMatName)

Dim oLocalCopy As MaterialAsset = Nothing
Try : oLocalCopy = oChosenMat.CopyTo(ThisDoc.Document, True) : Catch : End Try
Try : ThisDoc.Document.ActiveMaterial = oLocalCopy : Catch : End Try
iProperties.Material = oChosenMat.DisplayName