why is this code make inventor unstable ?

why is this code make inventor unstable ?

Darkforce_the_ilogic_guy
Advisor Advisor
519 Views
2 Replies
Message 1 of 3

why is this code make inventor unstable ?

Darkforce_the_ilogic_guy
Advisor
Advisor

 

 

Public Class MultiTreading
	
	Dim t1 As System.Threading.Thread
	Dim t2 As System.Threading.Thread
	Dim t3 As System.Threading.Thread
	Dim t4 As System.Threading.Thread
End Class 

Sub main ()
Dim A = Lamella_Length
Dim B =Lamella_Width
Dim C =Lamella_Height
Dim D =No_of_horizontal_lamellas
Dim E =Horizontal_spacing_lamellas
Dim F =Vertical_spacing_Lamellas
Dim G =No_of_vertical_lamellas
ThisDoc.Document.Rebuild()
t1 = New System.Threading.Thread(AddressOf first)
t2 = New System.Threading.Thread(AddressOf second)
t3 = New System.Threading.Thread(AddressOf first2)
t4 = New System.Threading.Thread(AddressOf second2)
t1.Start()
t2.Start()
t3.Start()
t4.Start()

End Sub
Sub first()
	ThisDoc.Document.Rebuild()
	Dim oDoc As PartDocument = ThisDoc.Document 
oDef = oDoc.ComponentDefinition

'define appearance library by name
Dim assetLib As AssetLibrary
assetLib = ThisApplication.AssetLibraries.Item("KallesoeMaterialLibrary")

sColor1 = "Wood Pine 1"
sColor2 = "Wood Pine 2"
sColor3 = "Wood Pine 3"
sColor4 = "Wood Pine 4"
sColor5 = "Wood Pine 5"


''define colors to work with
Dim libAsset1 As Asset
libAsset1 = assetLib.AppearanceAssets.Item(sColor1) 

Dim libAsset2 As Asset
libAsset2 = assetLib.AppearanceAssets.Item(sColor2)

Dim libAsset3 As Asset
libAsset3 = assetLib.AppearanceAssets.Item(sColor3)

Dim libAsset4 As Asset
libAsset4 = assetLib.AppearanceAssets.Item(sColor4)

Dim libAsset5 As Asset
libAsset5 = assetLib.AppearanceAssets.Item(sColor5)


' try to Copy the asset locally.
Try
	localAsset1 = libAsset1.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset1 = oDoc.Assets(sColor1)
End Try

' try to Copy the asset locally.
Try
	localAsset2 = libAsset2.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset2 = oDoc.Assets(sColor2)
End Try

Try
	localAsset3 = libAsset3.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset3 = oDoc.Assets(sColor3)
End Try

Try
	localAsset4 = libAsset4.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset4 = oDoc.Assets(sColor4)
End Try
Try
	localAsset5 = libAsset5.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset5 = oDoc.Assets(sColor5)
End Try






' change specific body to specific color 
Dim oSB1 As SurfaceBody
Dim NumberOfB = oDef.SurfaceBodies.Count


For i = 1 To NumberOfB Step 4
	random_number = Int(5 * Rnd) + 1
oSB1 = oDef.SurfaceBodies.Item(i)

If random_number = 1
	If oSB1.Appearance IsNot(localAsset1) Then
	
oSB1.Appearance = localAsset1 'red
End If 
Else If random_number = 2
	If oSB1.Appearance IsNot(localAsset2) Then
	oSB1.Appearance = localAsset2 'red
End If 
	Else If random_number = 3
		If oSB1.Appearance IsNot(localAsset3) Then
		oSB1.Appearance = localAsset3 'red
	End If 
		Else If random_number = 4
			If oSB1.Appearance IsNot(localAsset4) Then
			oSB1.Appearance = localAsset4 'red
		End If 
			Else If random_number = 5
				If oSB1.Appearance  IsNot(localAsset5) Then
				oSB1.Appearance = localAsset5 'red
			End If
			End If
	
	
Next 
	
End Sub


Sub second()
	ThisDoc.Document.Rebuild()
	'MessageBox.Show("Message", "Title")

	Dim oDoc As PartDocument = ThisDoc.Document 
oDef = oDoc.ComponentDefinition

'define appearance library by name
Dim assetLib As AssetLibrary
assetLib = ThisApplication.AssetLibraries.Item("KallesoeMaterialLibrary")

sColor1 = "Wood Pine 1"
sColor2 = "Wood Pine 2"
sColor3 = "Wood Pine 3"
sColor4 = "Wood Pine 4"
sColor5 = "Wood Pine 5"


''define colors to work with
Dim libAsset1 As Asset
libAsset1 = assetLib.AppearanceAssets.Item(sColor1) 

Dim libAsset2 As Asset
libAsset2 = assetLib.AppearanceAssets.Item(sColor2)

Dim libAsset3 As Asset
libAsset3 = assetLib.AppearanceAssets.Item(sColor3)

Dim libAsset4 As Asset
libAsset4 = assetLib.AppearanceAssets.Item(sColor4)

Dim libAsset5 As Asset
libAsset5 = assetLib.AppearanceAssets.Item(sColor5)


' try to Copy the asset locally.
Try
	localAsset1 = libAsset1.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset1 = oDoc.Assets(sColor1)
End Try

' try to Copy the asset locally.
Try
	localAsset2 = libAsset2.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset2 = oDoc.Assets(sColor2)
End Try

Try
	localAsset3 = libAsset3.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset3 = oDoc.Assets(sColor3)
End Try

Try
	localAsset4 = libAsset4.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset4 = oDoc.Assets(sColor4)
End Try
Try
	localAsset5 = libAsset5.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset5 = oDoc.Assets(sColor5)
End Try






' change specific body to specific color 
Dim oSB1 As SurfaceBody
Dim NumberOfB = oDef.SurfaceBodies.Count


For i = 2 To NumberOfB Step 4
	random_number = Int(5 * Rnd) + 1
oSB1 = oDef.SurfaceBodies.Item(i)

If random_number = 1
	If oSB1.Appearance IsNot(localAsset1) Then
	
oSB1.Appearance = localAsset1 'red
End If 
Else If random_number = 2
	If oSB1.Appearance IsNot(localAsset2) Then
	oSB1.Appearance = localAsset2 'red
End If 
	Else If random_number = 3
		If oSB1.Appearance IsNot(localAsset3) Then
		oSB1.Appearance = localAsset3 'red
	End If 
		Else If random_number = 4
			If oSB1.Appearance IsNot(localAsset4) Then
			oSB1.Appearance = localAsset4 'red
		End If 
			Else If random_number = 5
				If oSB1.Appearance  IsNot(localAsset5) Then
				oSB1.Appearance = localAsset5 'red
			End If
			End If
	
	
Next 
	
End Sub


Sub first2()
	ThisDoc.Document.Rebuild()
	Dim oDoc As PartDocument = ThisDoc.Document 
oDef = oDoc.ComponentDefinition

'define appearance library by name
Dim assetLib As AssetLibrary
assetLib = ThisApplication.AssetLibraries.Item("KallesoeMaterialLibrary")

sColor1 = "Wood Pine 1"
sColor2 = "Wood Pine 2"
sColor3 = "Wood Pine 3"
sColor4 = "Wood Pine 4"
sColor5 = "Wood Pine 5"


''define colors to work with
Dim libAsset1 As Asset
libAsset1 = assetLib.AppearanceAssets.Item(sColor1) 

Dim libAsset2 As Asset
libAsset2 = assetLib.AppearanceAssets.Item(sColor2)

Dim libAsset3 As Asset
libAsset3 = assetLib.AppearanceAssets.Item(sColor3)

Dim libAsset4 As Asset
libAsset4 = assetLib.AppearanceAssets.Item(sColor4)

Dim libAsset5 As Asset
libAsset5 = assetLib.AppearanceAssets.Item(sColor5)


' try to Copy the asset locally.
Try
	localAsset1 = libAsset1.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset1 = oDoc.Assets(sColor1)
End Try

' try to Copy the asset locally.
Try
	localAsset2 = libAsset2.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset2 = oDoc.Assets(sColor2)
End Try

Try
	localAsset3 = libAsset3.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset3 = oDoc.Assets(sColor3)
End Try

Try
	localAsset4 = libAsset4.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset4 = oDoc.Assets(sColor4)
End Try
Try
	localAsset5 = libAsset5.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset5 = oDoc.Assets(sColor5)
End Try






' change specific body to specific color 
Dim oSB1 As SurfaceBody
Dim NumberOfB = oDef.SurfaceBodies.Count


For i = 3 To NumberOfB Step 4
	random_number = Int(5 * Rnd) + 1
oSB1 = oDef.SurfaceBodies.Item(i)

If random_number = 1
	If oSB1.Appearance IsNot(localAsset1) Then
	
oSB1.Appearance = localAsset1 'red
End If 
Else If random_number = 2
	If oSB1.Appearance IsNot(localAsset2) Then
	oSB1.Appearance = localAsset2 'red
End If 
	Else If random_number = 3
		If oSB1.Appearance IsNot(localAsset3) Then
		oSB1.Appearance = localAsset3 'red
	End If 
		Else If random_number = 4
			If oSB1.Appearance IsNot(localAsset4) Then
			oSB1.Appearance = localAsset4 'red
		End If 
			Else If random_number = 5
				If oSB1.Appearance  IsNot(localAsset5) Then
				oSB1.Appearance = localAsset5 'red
			End If
			End If
	
	
Next 
	
End Sub

Sub second2()
	ThisDoc.Document.Rebuild()
	Dim oDoc As PartDocument = ThisDoc.Document 
oDef = oDoc.ComponentDefinition

'define appearance library by name
Dim assetLib As AssetLibrary
assetLib = ThisApplication.AssetLibraries.Item("KallesoeMaterialLibrary")

sColor1 = "Wood Pine 1"
sColor2 = "Wood Pine 2"
sColor3 = "Wood Pine 3"
sColor4 = "Wood Pine 4"
sColor5 = "Wood Pine 5"


''define colors to work with
Dim libAsset1 As Asset
libAsset1 = assetLib.AppearanceAssets.Item(sColor1) 

Dim libAsset2 As Asset
libAsset2 = assetLib.AppearanceAssets.Item(sColor2)

Dim libAsset3 As Asset
libAsset3 = assetLib.AppearanceAssets.Item(sColor3)

Dim libAsset4 As Asset
libAsset4 = assetLib.AppearanceAssets.Item(sColor4)

Dim libAsset5 As Asset
libAsset5 = assetLib.AppearanceAssets.Item(sColor5)


' try to Copy the asset locally.
Try
	localAsset1 = libAsset1.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset1 = oDoc.Assets(sColor1)
End Try

' try to Copy the asset locally.
Try
	localAsset2 = libAsset2.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset2 = oDoc.Assets(sColor2)
End Try

Try
	localAsset3 = libAsset3.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset3 = oDoc.Assets(sColor3)
End Try

Try
	localAsset4 = libAsset4.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset4 = oDoc.Assets(sColor4)
End Try
Try
	localAsset5 = libAsset5.CopyTo(oDoc)
Catch
'assume error means it's already local
	localAsset5 = oDoc.Assets(sColor5)
End Try






' change specific body to specific color 
Dim oSB1 As SurfaceBody
Dim NumberOfB = oDef.SurfaceBodies.Count


For i = 4 To NumberOfB Step 4
	random_number = Int(5 * Rnd) + 1
oSB1 = oDef.SurfaceBodies.Item(i)

If random_number = 1
	If oSB1.Appearance IsNot(localAsset1) Then
	
oSB1.Appearance = localAsset1 'red
End If 
Else If random_number = 2
	If oSB1.Appearance IsNot(localAsset2) Then
	oSB1.Appearance = localAsset2 'red
End If 
	Else If random_number = 3
		If oSB1.Appearance IsNot(localAsset3) Then
		oSB1.Appearance = localAsset3 'red
	End If 
		Else If random_number = 4
			If oSB1.Appearance IsNot(localAsset4) Then
			oSB1.Appearance = localAsset4 'red
		End If 
			Else If random_number = 5
				If oSB1.Appearance  IsNot(localAsset5) Then
				oSB1.Appearance = localAsset5 'red
			End If
			End If
	
	
Next 
	
End Sub

This code does work .. but something it crash inventor .. anyone know why ?... that is does is paint solid body in an part random colour .... and use multicore 

0 Likes
520 Views
2 Replies
Replies (2)
Message 2 of 3

_dscholtes_
Advocate
Advocate

I don't know anything about multi-threading, so I can't help you there. I'd like to comment the following on your code:

  1. Why don't you copy the assets to the document first and then start the 4 threads? Now each thread is copying them.
  2. Why don't you put the code pieces that are identical for all subs / threads in a separate sub. The differences between them are few and it makes reading your code easier (also for us). Also there are less places to check when you are making adjustments.
0 Likes
Message 3 of 3

Darkforce_the_ilogic_guy
Advisor
Advisor
This is my first multi thread code ...I do not remember why I did not do it ....maybe it give me some problem reading an variable form another subroutine .. or maybe it was just because it was easier to rewrite the code form single core to multi core
0 Likes