iLogic to copy material to document

iLogic to copy material to document

gert-leonvanlier
Collaborator Collaborator
1,127 Views
3 Replies
Message 1 of 4

iLogic to copy material to document

gert-leonvanlier
Collaborator
Collaborator

Hello,

 

I am tryin to write some iLogic code to check if particular materials (plural) exists in de local document and if not copy the materials to the document. I found this in the help. The materials to check on I placed in an array. A for each should then go through this array and check if the material exists and if not copy it to the document. Unfortunately it does not work like this. When I change the 'i' in the for each to for example "Yellow Paint" it works.

 

What am I doing wrong?

Dim oDoc As PartDocument = ThisDoc.Document
Dim oCompDef As ComponentDefinition = oDoc.ComponentDefinition
Dim oBody As SurfaceBody
Dim assetLib As AssetLibrary
Dim localAsset As Asset
Dim libAsset As Asset
Dim Material As Object
Dim i As Integer

materialArray = New String() {
	MainColour & "Paint",
	"Belt",
	"Black Paint",
	"Roller",
	"Steel"
	}
	
i = 1
For Each Material In materialArray
	On Error Resume Next
	localAsset = oDoc.Assets.Item(i)
	If Err.Number <> 0 Then
		On Error GoTo 0
		assetLib = ThisApplication.AssetLibraries.Item("MHSEMaterialLibrary")
		libAsset = assetLib.AppearanceAssets.Item(i)
		localAsset = libAsset.CopyTo(oDoc)
	End If
	i = i + 1
Next
0 Likes
Accepted solutions (1)
1,128 Views
3 Replies
Replies (3)
Message 2 of 4

Sergio.D.Suárez
Mentor
Mentor

Hi, I can not try the following code because I do not have that personalized library. I suppose that every item within the matrix should be inside this library.
See if it can help you.

 

Dim oDoc As PartDocument = ThisDoc.Document
Dim oCompDef As ComponentDefinition = oDoc.ComponentDefinition
Dim oBody As SurfaceBody
Dim assetLib As AssetLibrary
Dim localAsset As Asset
Dim libAsset As Asset
Dim Material As Object

materialArray = New String() {
	MainColour & "Paint",
	"Belt",
	"Black Paint",
	"Roller",
	"Steel"
	}

For i As Integer =0 To UBound(materialArray)
	assetLib = ThisApplication.AssetLibraries.Item("MHSEMaterialLibrary")
	libAsset = assetLib.AppearanceAssets.Item(i)
	Try
		localAsset = libAsset.CopyTo(oDoc)
	Catch
		localAsset = oDoc.Assets(i)
	End Try
Next

 I hope you can solve your problem. regards


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

0 Likes
Message 3 of 4

gert-leonvanlier
Collaborator
Collaborator
Accepted solution

I changed my code to this and this works:

 

Dim oDoc As PartDocument = ThisDoc.Document
Dim oCompDef As ComponentDefinition = oDoc.ComponentDefinition
Dim oBody As SurfaceBody
Dim assetLib As AssetLibrary
Dim localAsset As Asset
Dim libAsset As Asset
Dim Material As Object
Dim materialArray As New ArrayList
Dim i As Integer

materialArray.Add(MainColour & " Paint")
materialArray.Add("MHS Belt")
materialArray.Add("Black Paint")
materialArray.Add("MHS Roller")
materialArray.Add("Steel")

i = 0
For Each Material In materialArray
	On Error Resume Next
	localAsset = oDoc.Assets.Item(materialArray(i))
	If Err.Number <> 0 Then
		On Error GoTo 0
		assetLib = ThisApplication.AssetLibraries.Item("MHSEMaterialLibrary")
		libAsset = assetLib.AppearanceAssets.Item(materialArray(i))
		localAsset = libAsset.CopyTo(oDoc)
	End If
	i = i + 1
Next

 

 EDIT:

And this also works:

i = 0
For Each Material In materialArray
	assetLib = ThisApplication.AssetLibraries.Item("MHSEMaterialLibrary")
	libAsset = assetLib.AppearanceAssets.Item(materialArray(i))
	Try
		localAsset = libAsset.CopyTo(oDoc)
	Catch
		localAsset = oDoc.Assets.Item(materialArray(i))
	End Try			
	i = i + 1
Next
Message 4 of 4

rschader
Advocate
Advocate

In the last code segment you shared, you can eliminate the "i" counter  if you change it to:

 

i = 0
For Each Material In materialArray
	assetLib = ThisApplication.AssetLibraries.Item("MHSEMaterialLibrary")
	'libAsset = assetLib.AppearanceAssets.Item(materialArray(i))
libAsset = assetLib.AppearanceAssets.Item(Material) Try localAsset = libAsset.CopyTo(oDoc) Catch localAsset = oDoc.Assets.Item(materialArray(i)) End Try i = i + 1 Next

I commented out the line referencing i for the array index. You should also change it in the catch portion, which I just now noticed.

0 Likes