iLogic Rule, Please help!

iLogic Rule, Please help!

Anonymous
Not applicable
2,719 Views
19 Replies
Message 1 of 20

iLogic Rule, Please help!

Anonymous
Not applicable

Hello everyone. I am trying to make an iLogic rule that we will run from within our main assembly.

 

Here is what I would like it to do:

1- Loop through all of the parts within the assembly (solid, sheet metal, welded assemblies and regular assemblies)

2- Depending on the 4 filetypes, do various operations on custom parameters and user parameters.

     Solid: "RayonDePliage = NA", "MaterialThickness = NA", "MaterialType = NA", "Couleur = A", change part color to "TypePiece".

     Sheet: "RayonDePliage = A", "MaterialThickness = B", "MaterialType = C", "Couleur = D", change part color to "TypePiece".

     Welded: "Couleur = A".

     Regular: "Couleur = A".

 

Here is my problem:

I have a working code for all 4 custom variables (RayonDePliage, MaterialThickness, MaterialType and Couleur" which I can implement easily, but I am having a hard time changing the color of most parts. The part color should be changed to the custom user parameter "Couleur" (in iProperties), but that user parameter is driven by a multi-value custom user parameter (in Fx) called "TypePiece", which does not exists in a lot of our parts. Once the iLogic rule runs into a part/assembly that does not have that parameter (TypePiece), it just stops running somehow, so I would need to create the multi-value user parameter if it does not exist and set it to a pre-defined value called "Principale" and then proceed to change the color of the parts (for solid parts and sheet metal parts only) and set the other 4 custom variables that I have a working code for.

 

 

Here is my code, if you could help, it would mean a lot to me as I am stuck there

As a side note, if it is possible to remove the 3 duplicated "oPartDoc = oDoc" please let me know how, I tried with one "oPartDoc = oDoc" before the Select Case, and it somehow wouldn't work.

 

Sub Main()
'Update custom iProperties and Part Colors for clients/paintshop.
Try
' Get the active assembly document.
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument

' Iterate through all of the documents referenced by the assembly.
Dim oDoc As Document
Dim oPartDoc As PartDocument

For Each oDoc In oAsmDoc.AllReferencedDocuments

' Verify the type of document.
	model = oDoc.DisplayName
	modelFN = oDoc.FullFileName

	Select Case oDoc.SubType
	
	'Verify if it is a solid part.
	Case "{4D29B490-49B2-11D0-93C3-7E0706000000}"
	oPartDoc = oDoc
	MsgBox("1")
Call CustomProp(model) oPartDoc.ActiveRenderStyle = oPartDoc.RenderStyles.Item("Rose")
'Need to add 4 custom variables here 'Verify if it is a sheet metal. Case "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" oPartDoc = oDoc MsgBox("2")
Call CustomProp(model) oPartDoc.ActiveRenderStyle = oPartDoc.RenderStyles.Item("Blé")
'Need to add 4 custom variables here 'Verify if it is an assembly (regular, welded). Case "{E60F81E1-49B3-11D0-93C3-7E0706000000}","{28EC8354-9024-440F-A8A2-0E0E55D635B0}"
oPartDoc = oDoc MsgBox("3")
'Need to add 4 custom variables here End Select Next Catch End Try End Sub Sub CustomProp(modelA As String) ' Modify the "Couleur" iProperties if the parameter TypePiece exists in the part. Try Select Case Parameter(modelA, "TypePiece") Case "Principale" iProperties.Value(modelA, "Custom", "Couleur") = "RAL 1015 - Light Ivory" Case "GalvPeint" iProperties.Value(modelA, "Custom", "Couleur") = "Acier galvanisé" Case "Securite" iProperties.Value(modelA, "Custom", "Couleur") = "Lisse - Jaune" Case "Grillage" iProperties.Value(modelA, "Custom", "Couleur") = "Noir" Case "Usinage" iProperties.Value(modelA, "Custom", "Couleur") = "Acier - Poli" Case "TraySol" iProperties.Value(modelA, "Custom", "Couleur") = "RAL 7042 - Traffic Grey A" Case "Autre1" iProperties.Value(modelA, "Custom", "Couleur") = "Magenta" Case "Autre2" iProperties.Value(modelA, "Custom", "Couleur") = "Cyan" Case "Autre3" iProperties.Value(modelA, "Custom", "Couleur") = "Rouge" Case "Autre4" iProperties.Value(modelA, "Custom", "Couleur") = "Rose" End Select ' If it fails, do nothing. Catch End Try End Sub
0 Likes
2,720 Views
19 Replies
Replies (19)
Message 2 of 20

WCrihfield
Mentor
Mentor

How about something like this:

Sub Main
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MessageBox.Show("THIS RULE [ " & iLogicVb.RuleName & " ]  ONLY WORKS FOR ASSEMBLY DOCUMENTS.", "WRONG DOCUMENT TYPE", MessageBoxButtons.OK, MessageBoxIcon.Error)
	Return
End If

Dim oAdoc As AssemblyDocument = ThisApplication.ActiveDocument

Dim oDef As AssemblyComponentDefinition = oAdoc.ComponentDefinition

Dim oCustProps As PropertySet

Dim oParams As Parameters
Dim oParam As Parameter
Dim oExists As Boolean = False

For Each oOcc As ComponentOccurrence In oDef.Occurrences
	If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		'If oDoc.SubType = "" Then
		'If oDoc.DocumentSubType = 
		Dim oDocA As AssemblyDocument = oOcc.Definition.Document
		oCustProps = oDocA.PropertySets.Item("Inventor User Defined Properties")
		Dim oDefA As AssemblyComponentDefinition = oOcc.Definition
		oParams = oDefA.Parameters
		For Each oParam In oParams
			If oParam.Param.Name = "TypePiece" Then
				oExists = True
				'Call CustomProp(modelA)
			End If
			If oExists = False Then
				'Create the Parameter
				'Then use it
			End If
		Next
	ElseIf oOcc.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
		'If oDoc.SubType = "" Then
		Dim oDocP As PartDocument = oOcc.Definition.Document
		oCustProps = oDocP.PropertySets.Item("Inventor User Defined Properties")
		Dim oDefP As PartComponentDefinition = oOcc.Definition
		oParams = oDefP.Parameters
		For Each oParam In oParams
			If oParam.Param.Name = "TypePiece" Then
				oExists = True
				'Call CustomProp(modelA)
			End If
			If oExists = False Then
				'Create the Parameter
				'Either manually, or with another Sub Call
				oParams.UserParameters.AddByValue("TypePiece", "Principale", UnitsTypeEnum.kTextUnits)
				MultiValue.SetList("TypePiece","Principale","GalvPeint","Securite","Grillage","Usinage","TraySol","Autre1","Autre2","Autre3","Autre4")
				'Then use it
			End If
		Next
	End If
Next
End Sub

Sub CustomProp(modelA As String)
	Select Case Parameter.Param(modelA, "TypePiece").Value
	Case "Principale"
		oCustProps.Item("Couleur").Value = "RAL 1015 - Light Ivory"
	'Etc.
	'Etc.
	'Etc.
	End Select
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 20

J-Camper
Advisor
Advisor

You should put the Select case after the full Try/Catch:

 

Sub CustomProp(modelA As String, PassDoc As Document)

' Modify the "Couleur" iProperties if the parameter TypePiece exists in the part.
Try
          Param = Parameter(modelA, "TypePiece")
Catch
          PassDoc.ComponentDefinition.Parameters.UserParameters.AddByValue("TypePiece", "Principale", "text")
End Try
	Select Case Parameter(modelA, "TypePiece")
	
	Case "Principale"
	iProperties.Value(modelA, "Custom", "Couleur") = "RAL 1015 - Light Ivory"

	Case "GalvPeint"
	iProperties.Value(modelA, "Custom", "Couleur") = "Acier galvanisé"
	
	Case "Securite"
	iProperties.Value(modelA, "Custom", "Couleur") = "Lisse - Jaune"
	
	Case "Grillage"
	iProperties.Value(modelA, "Custom", "Couleur") = "Noir"
	
	Case "Usinage"
	iProperties.Value(modelA, "Custom", "Couleur") = "Acier - Poli"
	
	Case "TraySol"
	iProperties.Value(modelA, "Custom", "Couleur") = "RAL 7042 - Traffic Grey A"
	
	Case "Autre1"
	iProperties.Value(modelA, "Custom", "Couleur") = "Magenta"
	
	Case "Autre2"
	iProperties.Value(modelA, "Custom", "Couleur") = "Cyan"
	
	Case "Autre3"
	iProperties.Value(modelA, "Custom", "Couleur") = "Rouge"
	
	Case "Autre4"
	iProperties.Value(modelA, "Custom", "Couleur") = "Rose"
	
	End Select
End Sub

You have to pass the Document Object to the Subroutine, but I think that should do what you want.

 

0 Likes
Message 4 of 20

Anonymous
Not applicable

Hi, thank you for the quick answer!

I would need to test it, but I am not sure I could debug the code if we want to change something in the past, as opposed to mine which I understand, yours use a bunch of terms I have never seen/used. Is there any way for you to simplify it a bit to look like my piece of code, so that I could understand it? I know I may be asking a bit much, but I would like to understand the code and be able to modify it and learn at the same time, as we are trying to automate a bit of stuff at work and I have never really done any iLogic/VBA. I can decypher a bit of it because I have a small programming background, but not enough to fully understand it 😞

0 Likes
Message 5 of 20

Anonymous
Not applicable

Thank you, I am now at home, but I will try this tomorrow. If this is only what it was missing to make it work, it is ridiculous. I've tried quite a few things, even adding parameters to my subroutine and using the model and modelA parameters, even the fullfilename and setting the Document PartDocument variables again... Which variable should I pass onto the subroutine? 

Call CustomProp(model,oDoc)

 or

Call CustomProp(model,oPartDoc)

 

I would guess the oDoc argument, as you said Document not PartDocument, but just making sure. 

0 Likes
Message 6 of 20

J-Camper
Advisor
Advisor

If you are just changing iProperties you can stick with a generic Document Object "oDoc" because the iProperties and Parameters are mapped the same for part and assembly documents.  If you want the Subroutine to be Part specific to access other part document Objects then pass "oPartDoc".

 

I had some time this evening to play with your code a bit, I got it working:

Sub Main()
'Update custom iProperties and Part Colors for clients/paintshop.
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject
	Exit Sub
End If
' Get the active assembly document.
Dim oMainDoc As AssemblyDocument = ThisApplication.ActiveDocument

' Iterate through all of the documents referenced by the assembly.
Dim oDoc As Document
Dim oPartDoc As PartDocument
Dim oAsmDoc As AssemblyDocument

For Each oDoc In oMainDoc.AllReferencedDocuments
	customPropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
	Try
		oProp = customPropertySet.Item("Couleur").Value
	Catch
		customPropertySet.Add("", "Couleur")
	End Try
	
	Dim oRenderStyle As RenderStyle
	
	'Verify the type of document.
	Select Case oDoc.SubType
	
	'Verify if it is a solid part.
	Case "{4D29B490-49B2-11D0-93C3-7E0706000000}"
	oPartDoc = oDoc
	Call CustomProp(oDoc)
    oRenderStyle = oPartDoc.RenderStyles.Item("Yellow")
    oPartDoc.ActiveRenderStyle = oRenderStyle
    ThisApplication.ActiveView.Update
    'Need to add 4 custom variables here

	'Verify if it is a sheet metal.
	Case "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
	oPartDoc = oDoc
	Call CustomProp(oDoc)
	oRenderStyle = oPartDoc.RenderStyles.Item("Blue")
    oPartDoc.ActiveRenderStyle = oRenderStyle
    ThisApplication.ActiveView.Update
    'Need to add 4 custom variables here

	'Verify if it is an assembly (regular, welded).
	Case "{E60F81E1-49B3-11D0-93C3-7E0706000000}","{28EC8354-9024-440F-A8A2-0E0E55D635B0}"
    oAsmDoc = oDoc
	Call CustomProp(oDoc)
    'Need to add 4 custom variables here

	End Select
	
Next

End Sub

Sub CustomProp(PassDoc As Document)
' Modify the "Couleur" iProperties if the parameter TypePiece exists in the part.
Try
		  Param = PassDoc.ComponentDefinition.Parameters.UserParameters("TypePiece").Value
Catch
          PassDoc.ComponentDefinition.Parameters.UserParameters.AddByValue("TypePiece", "Principale", "Text")
End Try

	Select Case PassDoc.ComponentDefinition.Parameters.UserParameters("TypePiece").Value
	
	Case "Principale"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "RAL 1015 - Light Ivory"

	Case "GalvPeint"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Acier galvanisé"
	
	Case "Securite"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Lisse - Jaune"
	
	Case "Grillage"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Noir"
	
	Case "Usinage"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Acier - Poli"
	
	Case "TraySol"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "RAL 7042 - Traffic Grey A"
	
	Case "Autre1"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Magenta"
	
	Case "Autre2"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Cyan"
	
	Case "Autre3"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Rouge"
	
	Case "Autre4"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Rose"
	
	End Select

End Sub

I tried not to modify it too much because I don't know what your full intentions are.

0 Likes
Message 7 of 20

Anonymous
Not applicable

Thank you for your code, however it doesn't seem to work properly.

It gives me errors somewhere, I don't know why... so I tried to decypher your code and implement the parts into mine and tried to make mine work, it doesn't work either... but I am close to making it work.

Right now, in this code, there are 2 things that aren't working. The first is to create the "TypePiece" as a MultiValue.SetList,  even if I add the line right after the parameter's creation, it refuses to make it a multivalue somehow... and then, if the iProperty "Couleur" doesn't exist, it doesn't want to create it either.

 

Here is the code I have right now:

 

Sub Main()
'Runs a rule in all files of an old assembly to update them.
Try
' Get the active assembly document.
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument

' Iterate through all of the documents referenced by the assembly.
Dim oDoc As Document
Dim oPartDoc As PartDocument

For Each oDoc In oAsmDoc.AllReferencedDocuments

' Verify the type of document.
	'model = filename, modelFN = full filename.
	model = oDoc.DisplayName
	modelFN = oDoc.FullFileName

	Select Case oDoc.SubType
	
	'Verify if it is a solid part.
	Case "{4D29B490-49B2-11D0-93C3-7E0706000000}"

	MsgBox("Part : " & model)
	Call CustomProp(model,oDoc)

	'Verify if it is a sheet metal.
	Case "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"

	MsgBox("Sheet Metal : " & model)
	Call CustomProp(model,oDoc)

	'Verify if it is an assembly (regular, welded).
	Case "{E60F81E1-49B3-11D0-93C3-7E0706000000}","{28EC8354-9024-440F-A8A2-0E0E55D635B0}"
	MsgBox("Assembly : " & model)
	Call CustomProp(model,oDoc)


	End Select

	
Next
Catch
End Try

End Sub


' Modify the "Couleur" iProperties in a part.
Sub CustomProp(modelA As String, PassDoc As Document)

'Detect if "TypePiece" exists, if not, creates it.
	Try
		Param = PassDoc.ComponentDefinition.Parameters.UserParameters("TypePiece").Value
	Catch
		PassDoc.ComponentDefinition.Parameters.UserParameters.AddByValue("TypePiece", "Principale", UnitsTypeEnum.kTextUnits)
		MultiValue.SetList("TypePiece","Principale","GalvPeint","Securite","Grillage","Usinage","TraySol","Autre1","Autre2","Autre3","Autre4")
	End Try
	
'Detect if "Couleur" exists, if not, creates it.
	Try
		Param = PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value
	Catch
		PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "N/A"
	End Try

'Changes the "Couleur" iProperties of the part
	Select Case Parameter(modelA, "TypePiece")
	
	Case "Principale"
	iProperties.Value(modelA, "Custom", "Couleur") = "RAL 1015 - Light Ivory"

	Case "GalvPeint"
	iProperties.Value(modelA, "Custom", "Couleur") = "Acier galvanisé"
	
	Case "Securite"
	iProperties.Value(modelA, "Custom", "Couleur") = "Lisse - Jaune"
	
	Case "Grillage"
	iProperties.Value(modelA, "Custom", "Couleur") = "Noir"
	
	Case "Usinage"
	iProperties.Value(modelA, "Custom", "Couleur") = "Acier - Poli"
	
	Case "TraySol"
	iProperties.Value(modelA, "Custom", "Couleur") = "RAL 7042 - Traffic Grey A"
	
	Case "Autre1"
	iProperties.Value(modelA, "Custom", "Couleur") = "Magenta"
	
	Case "Autre2"
	iProperties.Value(modelA, "Custom", "Couleur") = "Cyan"
	
	Case "Autre3"
	iProperties.Value(modelA, "Custom", "Couleur") = "Rouge"
	
	Case "Autre4"
	iProperties.Value(modelA, "Custom", "Couleur") = "Rose"
	
	End Select
	
End Sub
0 Likes
Message 8 of 20

Anonymous
Not applicable

I have finally been able to debug a bit my code. Right now, it seems like my CustomProp subroutine is creating an issue with ONE specific part... which I don't understand because the part has the same properties as the others (it isn't read-only, it isn't currently not modifiable, it doesn't have "Couleur" or "TypePiece", like many of my other parts... and the other works)

 

If I replace the MsgBox("Allo") by anything else regarding the "TypePiece" user parameter, it fails... but if I leave MsgBox("Allo"), it keeps going and creates the "Couleur" parameter. This is how I've been trying to debug the problem, replacing parts of my code with MsgBox to figure out where it fails.

 

Sub CustomProp(modelA As String, PassDoc As Document)
        
'Detect if "TypePiece" exists, if not, creates it.
    Try
                MsgBox("Allo")
    Catch
        PassDoc.ComponentDefinition.Parameters.UserParameters.AddByValue("TypePiece", "Principale", UnitsTypeEnum.kTextUnits)
                MsgBox("TypePiece invalide")
    End Try
    
'Detect if "Couleur" exists, if not, creates it.
    Try
        ParamA = PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value
    Catch
        PassDoc.PropertySets.Item("Inventor User Defined Properties").Add("N/A","Couleur")
    End Try

End Sub

 

0 Likes
Message 9 of 20

Anonymous
Not applicable

In case you guys want the specific part to test it out... here it is, for some reasons I can't figure out what is wrong with it, but it won't work for the "TypePiece" rule, but it works for my "Couleur" rule.... that's super odd to me.

 

I've been trying for 3 hours now to make it work, but it refuses to. The other tidbits of code works for now but I can't finish it if this part won't work unfortunately.

0 Likes
Message 10 of 20

J-Camper
Advisor
Advisor

What errors were you getting from the code I previously sent?  I had a hard time with the RenderStyle be cause I've never tried to modify it with iLogic, so this time I put it in a try catch so it doesn't terminate the rule when it can't find the right render style.

 

I dropped your part into my test assembly and it works when running the code below:

Sub Main()
'Update custom iProperties and Part Colors for clients/paintshop.
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject
	Exit Sub
End If
' Get the active assembly document.
Dim oMainDoc As AssemblyDocument = ThisApplication.ActiveDocument

' Iterate through all of the documents referenced by the assembly.
Dim oDoc As Document
Dim oPartDoc As PartDocument
Dim oAsmDoc As AssemblyDocument

For Each oDoc In oMainDoc.AllReferencedDocuments
	customPropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
	Try
		oProp = customPropertySet.Item("Couleur").Value
	Catch
		customPropertySet.Add("", "Couleur")
	End Try
	
	Dim oRenderStyle As RenderStyle
	
	'Verify the type of document.
	Select Case oDoc.SubType
	
	'Verify if it is a solid part.
	Case "{4D29B490-49B2-11D0-93C3-7E0706000000}"
	oPartDoc = oDoc
	Call CustomProp(oDoc)
	Try
	    oRenderStyle = oPartDoc.RenderStyles.Item("Yellow")
	    oPartDoc.ActiveRenderStyle = oRenderStyle
	    ThisApplication.ActiveView.Update
	Catch
		MessageBox.Show("Could not find referenced RenderStyle.", "Render Failure")
	End Try
    'Need to add 4 custom variables here

	'Verify if it is a sheet metal.
	Case "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
	oPartDoc = oDoc
	Call CustomProp(oDoc)
	Try
	    oRenderStyle = oPartDoc.RenderStyles.Item("Blue")
	    oPartDoc.ActiveRenderStyle = oRenderStyle
	    ThisApplication.ActiveView.Update
	Catch
		MessageBox.Show("Could not find referenced RenderStyle.", "Render Failure")
	End Try
    'Need to add 4 custom variables here

	'Verify if it is an assembly (regular, welded).
	Case "{E60F81E1-49B3-11D0-93C3-7E0706000000}","{28EC8354-9024-440F-A8A2-0E0E55D635B0}"
    oAsmDoc = oDoc
	Call CustomProp(oDoc)
    'Need to add 4 custom variables here

	End Select
	
Next

End Sub

Sub CustomProp(PassDoc As Document)
Dim valueList() As String = {"""Principale""", """GalvPeint""", """Securite""", """Grillage""", """Usinage""", """TraySol""", """Autre1""", """Autre2""", """Autre3""", """Autre4"""}
' Modify the "Couleur" iProperties if the parameter TypePiece exists in the part.
Try
		  Param = PassDoc.ComponentDefinition.Parameters.UserParameters("TypePiece").Value
Catch
          PassDoc.ComponentDefinition.Parameters.UserParameters.AddByValue("TypePiece", "Principale", "Text")
		  PassDoc.ComponentDefinition.Parameters.UserParameters("TypePiece").ExpressionList.SetExpressionList(valueList)
End Try
	'Making sure list is up to date
	PassDoc.ComponentDefinition.Parameters.UserParameters("TypePiece").ExpressionList.SetExpressionList(valueList)

	Select Case PassDoc.ComponentDefinition.Parameters.UserParameters("TypePiece").Value
	
	Case "Principale"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "RAL 1015 - Light Ivory"

	Case "GalvPeint"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Acier galvanisé"
	
	Case "Securite"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Lisse - Jaune"
	
	Case "Grillage"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Noir"
	
	Case "Usinage"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Acier - Poli"
	
	Case "TraySol"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "RAL 7042 - Traffic Grey A"
	
	Case "Autre1"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Magenta"
	
	Case "Autre2"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Cyan"
	
	Case "Autre3"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Rouge"
	
	Case "Autre4"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Rose"
	
	End Select

End Sub

If you run into errors, screen shot them so we can try to figure out the cause.

0 Likes
Message 11 of 20

Anonymous
Not applicable

Thank you for your quick answer. I tried your code on a really small assembly, it worked flawlessly. HOWEVER, on a larger assembly, I am having a Member Not Found (0x80020003) error when I run the rule in my main assembly.

(In french:)

Erreur de règle: RunExternalRule_AllFiles_OldAssembly, dans le document 000 ML 1232 Sortie.iam

Membre introuvable. (Exception de HRESULT : 0x80020003 (DISP_E_MEMBERNOTFOUND))

 

And in the "more info" tab:

System.Runtime.InteropServices.COMException (0x80020003): Membre introuvable. (Exception de HRESULT : 0x80020003 (DISP_E_MEMBERNOTFOUND))
   à Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack)
   à Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
   à LmiRuleScript.CustomProp(Document PassDoc)
   à LmiRuleScript.Main()
   à Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
   à iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

I looked at your code and I wonder if the PropertySet.Add for "Couleur" could be moved into a Sub? (I am trying to dissect the code as much as possible so that if someone else looks at it, he can attempt to modify it.) I do not see a reason why it wouldn't be possible, as it seems pretty much the same as the TypePiece problem I had.

I noticed you also put "fixed" renderstyles for the parts, is it possible to read the "Couleur" parameter there or should I move the renderstyle into the Sub ? Other than that, I am very pleased with the code ^_^ It isn't too far of from my original version, so I can understand it, I'm just making sure I understand what's going on right now. Thanks again! Hopefully that error message can disappear ^_^

0 Likes
Message 12 of 20

Anonymous
Not applicable

After a little bit more testing, I decided to delete the assembly I was testing the rule on, and then copy the original again and run the rule.... it brings out the same error as in the previous post... and on another assembly, I got the same error, said "ok", ran the rule again and it went a little bit further than it gave me the same error and now it won't go further (by going further, I mean running the rule on more parts)

0 Likes
Message 13 of 20

Anonymous
Not applicable

Sorry for the multiple messages, I don't see an "Edit" option.

This error seems random. I tried to run the rule again, got the error, tried a 2nd time, got the error again, tried a 3rd time and it ran fully.

 

Edit:

I have added a MsgBox to know which parts are problematic, and it appears that there is multiple items which gives this error. It seems to be both parts and assemblies though, and they should not, in theory, cause any issues. If I run the rule in the problematic assemblies, it works flawlessly, but not from the main assembly.

0 Likes
Message 14 of 20

Anonymous
Not applicable

I have figured out that the "TypePiece" is still causing issues with some assemblies/parts. I removed that bit of code and it would populate the parts with "Couleur" if it didn't have it, but it would stop at some "predefined" parts every single time I add the "TypePiece" code, anywhere in the code (before the "Select Case", at all 3 "Case" "actions", start of the sub CustomProp..)

0 Likes
Message 15 of 20

Anonymous
Not applicable

After a BUNCH of searching, it seems as if the loop doesn't like complicated parts. For instance, we often have parts with links to a skeleton file and some other derived parts and the rule tends to fail on those parts(but sometimes it works?????), I tried looking into the AllLeafOccurences instead of AllReferencedDocuments, but I don't know if it would do the same thing, as I would technically need to open all files within the assembly still... I am a bit lost.

 

Here is our we work, if someone wants to replicate it;

We start by making a skeleton part, then we make our main part (conveyor side) based on that skeleton (derived part), and then we make regular parts based on the skeleton parts and/or the main part (conveyor side) to make sure the parts will always follow our needs. But sometimes, we have parts that have 2 or 3 derived parts in them, alongside an excel spreadsheet(or not) and from what I could see... those parts often fails.

 

Also, as a side note, it would be cool if the rule could update the mirrored parts too. It changes the appearance, but only in the dropdown menu, not the actual physical appearance because you have to "edit" the original part for that to change. I don't know if there's a way to do that?

0 Likes
Message 16 of 20

J-Camper
Advisor
Advisor

My only thought about the Derived part issue is that the derived part has the "TypePiece" Parameter passed to it from the Parent and It is throwing an error trying to update the list, so I moved where I update the list into the catch so it will only fire if the rule creates this Parameter.  I threw in one of my derived parts and its parent file and they ran fine.  I also threw an additional Check at the beginning to only run if a Document "IsModifiable" in case that was throwing errors.  I reorganized the subroutines and added a debugging subroutine that deletes the UserParameter and Custom property so I could run the parts with fresh parts [remove if you want, but it won't do anything unless it gets called to run and it is currently commented out]:

Sub Main()
'Update custom iProperties and Part Colors for clients/paintshop.
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject
	Exit Sub
End If
' Get the active assembly document.
Dim oMainDoc As AssemblyDocument = ThisApplication.ActiveDocument

' Iterate through all of the documents referenced by the assembly.
Dim oDoc As Document

For Each oDoc In oMainDoc.AllReferencedDocuments
	If oDoc.IsModifiable = True
	
	'Debugging:
'	Call RestartProps(oDoc)
'	GoTo 100
	
	Call CustomProp(oDoc)
	
		If oDoc.DocumentType = kPartDocumentObject 
			Call RenderSettings(oDoc)
		End If
		
	End If
100 :
Next

'[ 
'UPDATE
InventorVb.DocumentUpdate()
'] UPDATE

End Sub

Sub CustomProp(PassDoc As Document)
	'Verification of Custom iProperty
	customPropertySet = PassDoc.PropertySets.Item("Inventor User Defined Properties")
	Try
		oProp = customPropertySet.Item("Couleur").Value
	Catch
		customPropertySet.Add("", "Couleur")
	End Try
	
	'Verify User Parameter exists and is correct.
	Dim valueList() As String = {"""Principale""", """GalvPeint""", """Securite""", """Grillage""", """Usinage""", """TraySol""", """Autre1""", """Autre2""", """Autre3""", """Autre4""" }
	Try
		Param = PassDoc.ComponentDefinition.Parameters.UserParameters("TypePiece").Value
	Catch
		PassDoc.ComponentDefinition.Parameters.UserParameters.AddByValue("TypePiece", "Principale", "Text")
		PassDoc.ComponentDefinition.Parameters.UserParameters("TypePiece").ExpressionList.SetExpressionList(valueList)
	End Try
	
	Select Case PassDoc.ComponentDefinition.Parameters.UserParameters("TypePiece").Value
	
	Case "Principale"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "RAL 1015 - Light Ivory"

	Case "GalvPeint"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Acier galvanisé"
	
	Case "Securite"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Lisse - Jaune"
	
	Case "Grillage"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Noir"
	
	Case "Usinage"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Acier - Poli"
	
	Case "TraySol"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "RAL 7042 - Traffic Grey A"
	
	Case "Autre1"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Magenta"
	
	Case "Autre2"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Cyan"
	
	Case "Autre3"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Rouge"
	
	Case "Autre4"
	PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value = "Rose"
	
	End Select

End Sub

Sub RenderSettings(PassDoc As PartDocument)
	
	Dim oRenderStyle As RenderStyle
	Dim oRenderStyleName As String = PassDoc.PropertySets.Item("Inventor User Defined Properties").Item("Couleur").Value
	
	'Choose path by subtype of document.
	Select Case PassDoc.SubType
	
	'Verify if it is a solid part.
	Case "{4D29B490-49B2-11D0-93C3-7E0706000000}"
	Try
	    oRenderStyle = PassDoc.RenderStyles.Item(oRenderStyleName) '"Yellow"
	    PassDoc.ActiveRenderStyle = oRenderStyle
	    ThisApplication.ActiveView.Update
	Catch
		MessageBox.Show("Could not find referenced RenderStyle: " & oRenderStyleName, "Render Failure: " & PassDoc.DisplayName)
	End Try
	
    'Need to add 4 custom variables here

	'Verify if it is a sheet metal.
	Case "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
	Try
	    oRenderStyle = PassDoc.RenderStyles.Item(oRenderStyleName) '"Blue"
	    PassDoc.ActiveRenderStyle = oRenderStyle
	    ThisApplication.ActiveView.Update
	Catch
		MessageBox.Show("Could not find referenced RenderStyle: " & oRenderStyleName, "Render Failure: " & PassDoc.DisplayName)
	End Try
    'Need to add 4 custom variables here

	'Verify if it is an assembly (regular, welded).
	Case "{E60F81E1-49B3-11D0-93C3-7E0706000000}","{28EC8354-9024-440F-A8A2-0E0E55D635B0}"
	'Assemblies don't have appearances themselves so do nothing

	End Select
End Sub	

Sub RestartProps(PassDoc As Document)

	customPropertySet = PassDoc.PropertySets.Item("Inventor User Defined Properties")
	Try
		customPropertySet.Item("Couleur").Delete
	Catch
		'Do nothing
	End Try
	
	Try
		PassDoc.ComponentDefinition.Parameters.UserParameters("TypePiece").Delete
	Catch
		'Do nothing
	End Try

End Sub

If you are still getting errors on parts, maybe post more of the parts causing issues so I can look at them in detail.

 

As far as the Appearances, like I said before I've never had a reason to set this with iLogic so I'm not sure the best route without spending some time looking into it.

0 Likes
Message 17 of 20

Anonymous
Not applicable

I am currently testing the new code you provided and I am afraid I may have bad news.

This code is returning the same error as above, but on a different part. This time it returns it when it tries to do a SEW Eurodrive motor/gearbox. Yesterday's code was doing it without any hiccups.

I would like to include the parts that weren't working yesterday, but my boss disagrees as it would basically give our whole conveyor design away because the parts uses our skeleton part and our conveyor's side, which is pretty much anything you need to reverse-engineer it.

Hopefully you encounter the same issue as me with the motor... this is the issue it was giving me with the other parts, I was moving the problematic parts to another place and then running the rule on them and everything was great, but those specific parts it wouldn't do it. (parts and assembly)

 

 

NOTE: I wonder if it could change something if the "TypePiece" would be a custom iProperties multi-value parameter instead of a user-defined parameter (similar to "Couleur", but multi-value).

That could be better for us in the long run, but I would need a script to create the custom iProperties multi-value parameter and assign it the same value as the current user-defined parameter and then delete that parameter that I could run in a main assembly. As currently all of our most used assemblies and parts have the user-defined parameter in them and they contain too many pieces to do it by hand.

0 Likes
Message 18 of 20

Anonymous
Not applicable

I apologize, the code is indeed stopping at the same assembly yesterday's code was stopping.

 

I had my MsgBox a bit too low in the code (in the Select Case instead of prior to it), so it was never reaching the point where it would say that it stops at this file.

 

However, as I said, my boss doesn't want me to send the assembly because it contains all of the necessary data to replicate our conveyors. I will be sending you another assembly though which does the same thing. It runs the code for a few parts (3 or 4) then it stops doing it at a circlip. The assembly probably has 30+ parts in total. Hopefully if you can make it work for this assembly it will correct the issue with our other ones as well.

 

Even if I remove the "TypePiece" part, it stops at the first assembly which has the circlip too. (I don't know if it is just a coincidence or not though!) I am guessing this is because it cannot read the TypePiece part and stops doing the code.

 

Thanks for all the time taken to help by the way! Much appreciated!

0 Likes
Message 19 of 20

J-Camper
Advisor
Advisor

Oh yes please be careful what you post, it's open to anyone that reads the message.  Any way I didn't get any errors with the assembly you sent when running the code.  The only error I got was an open/save event triggered external run that I don't have.

 

What version of Inventor are you using? I have been testing in 2019 Professional, but I noticed the file path for your external rule went through Inventor 2014 folder.  There may be differences in available Objects and Methods that are causing the issue, but that would be weird if it is working on some files but not all files...

 

Is your Inventor up to date?  For what ever release you're using.

0 Likes
Message 20 of 20

Anonymous
Not applicable

We are still using 2016, and I either get the error message OR nothing at all.

I added a MsgBox() to tell me which part it is trying to open, and after like 4 parts it just.... doesn't even tell me a part, it stops randomly... yet it creates the TypePiece and Couleur for the part before it stops (I think it's the sprocket itself? can't recall).

 

As far as the rule that you don't have... yeah, it is the previous version of the rule that we have been trying to modify for the last few days now. That rule was a mess. It was my first ever rule and it only ran when saving a part, so every time we were making an .idw for the paint shop, we had to open the .ipt/.iam, save, close, save the .idw and print a .pdf for the paint shop.... that is why we wanted to automate it only once through the main assembly... would be muchhhh faster and less wastage of time.

0 Likes