Document settings with rule (Form)

Document settings with rule (Form)

Anonymous
Not applicable
2,258 Views
17 Replies
Message 1 of 18

Document settings with rule (Form)

Anonymous
Not applicable

Hi,

I'm new in Inventor and I have one problem.
I want to set Default BOM Structure (Document Settings/Bill of Materials) in my own Form (with Rule) for active part/assembly.

This Form (Rule) will be in template.

 

Is there any simlple way to do this (picture is in attachment)?

 

Thanx!

0 Likes
Accepted solutions (1)
2,259 Views
17 Replies
Replies (17)
Message 2 of 18

matt_jlt
Collaborator
Collaborator

To do this with an iLogic form would require the following

 

Make a text parameter, then make it multi-value and duplicate the BOM Structure type options

Add this to the form (it should be a combo box)

You will then need a rule that reads the multi-value parameter chosen and changes the BOM Structure based on this. Rough example below.

 

Select Case MultiParamName
Case "Phantom"
ThisDocument.ComponentDefinition.BOMStructure = "Chosen BOM Structure Type Here"
Case "Purchased"
ThisDocument.ComponentDefinitino.BOMStructure = ""Chosen BOM Structure Type Here"
End Select

 

Regards, Matt

0 Likes
Message 3 of 18

Anonymous
Not applicable

Hi,

first thank you for helping.

 

But if I use your code for Rule, I got this error (in attachment).

 

What can be wrong? (I want to change document setting for part in part mode, not in assembly)

 

 

0 Likes
Message 4 of 18

matt_jlt
Collaborator
Collaborator

Hi, the code I wrote previously was just an outline to get you going. The code wouldn't actually work. Sorry, i should have been more clear. Try the code below (this is just a portion of what you need to achieve)

 

' Change BOM Structure
Select Case MultiValueParam
Case "Phantom"
	ThisDoc.Document.ComponentDefinition.BOMStructure = "51971" ' Set to Phantom Structure
Case "Normal"
	' Centering plate is required
	ThisDoc.Document.ComponentDefinition.BOMStructure = "51970" ' Set to Normal Structure
End Select

 Regards, Matt.

0 Likes
Message 5 of 18

Anonymous
Not applicable

If I create rule with just

ThisDoc.Document.ComponentDefinition.BOMStructure="51971"' Set to Phantom Structure

works fine. But if I want to use this with multivalue parameter doesn't. (If I set parameter to normal/phantom, no rule is runing).

 

And where can I find numbers like 51971? Is there any list of them?

And one more question. Is there any way for reverse procedure - if I will choose BOM Structure in Document Settings, it will change in Parameter and Form too?

0 Likes
Message 6 of 18

matt_jlt
Collaborator
Collaborator
Accepted solution

 

Did you create a multi value parameter? See image below for what you need.

 

parameter.jpg

 

Add the code below to an iLogic Rule. It should change the bom structure when you change the parameter.

 

' Get part component definition
Dim oCompDef As PartComponentDefinition = ThisDoc.Document.ComponentDefinition

' Change BOM Structure based on multi value parameter
' BOM Structure Enumerator values are available in the programming help menu in Inventor.
Select Case BOMStructureParam
Case "Normal"
	oCompDef.BOMStructure = "51970"
Case "Phantom"
	oCompDef.BOMStructure = "51971"
Case "Reference"
	oCompDef.BOMStructure = "51972"
Case "Purchased"
	oCompDef.BOMStructure = "51973"
Case "Inseparable"
	oCompDef.BOMStructure = "51974"
End Select

 

To change the BOM Structure to match what a user has selected use:

' Get part component definition
Dim oCompDef As PartComponentDefinition = ThisDoc.Document.ComponentDefinition

' Change BOM Structure based on multi value parameter
' BOM Structure Enumerator values are available in the programming help menu in Inventor.
Select Case oCompDef.BOMStructure
Case "51970"
	BOMStructureParam = "Normal"
Case "51971"
	BOMStructureParam = "Phantom"
Case "51972"
	BOMStructureParam = "Reference"
Case "51973"
	BOMStructureParam = "Purchased"
Case "51974"
	BOMStructureParam = "Inseparable"
End Select

 You will have to get the order / events in which the rules are run correct for it to work properly but it should work.

 

Regards, Matt.

Message 7 of 18

Anonymous
Not applicable

Thank you!

It works perfekt!

0 Likes
Message 8 of 18

GosponZ
Collaborator
Collaborator

I did same, don't have error  but BomStructure in active part is not changed. Do i missed something in here?

Thank you

0 Likes
Message 9 of 18

Vladimir.Ananyev
Alumni
Alumni

Hi nicomajstr

>>And where can I find numbers like 51971? Is there any list of them?

BOM structure types are listed in BOMStructureEnum Enumerator (in the Inventor API Help).

Name

Value

Description

kDefaultBOMStructure

51969

The default structure type.

kInseparableBOMStructure

51974

The inseparable structure type.

kNormalBOMStructure

51970

The normal structure type.

kPhantomBOMStructure

51971

The phantom structure type.

kPurchasedBOMStructure

51973

The purchased structure type.

kReferenceBOMStructure

51972

The reference structure type.

kVariesBOMStructure

51975

The structure type varies amongst references.

That means you may use “BOMStructureEnum.kPhantomBOMStructure” instead of 51971.

Cheers,

Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

Message 10 of 18

gordon.bowe
Contributor
Contributor

I have previously created a Form that collects all the iProperties that we should change in to one place, and I would like to add a combo box for setting the default BOM structure of the active part/assembly.

I have created two rules based on the above code from Matt.

 

First rule is SetBOM, and runs the first set of code to set the default BOM structure based on the parameter I created

Second rule is SetParam and that one sets the multi-value parameter to match the default BOM structure defined in the document properties.

 

The issue I am having is that somehow my parameter is "linking" to SetParam, so when i open the Form and change the value, it instantly changes back.

 

My intent was to have a button below the combo box labeled "APPLY" that would run SetBOM, and then use an iTrigger before save to run SetParam, however it seems that SetParam is running automatically.

 

is there a way to change this behavior?  Or maybe there is a way to "synchronize" the two without having to fiddle with two rules and iTriggers?

0 Likes
Message 11 of 18

WCrihfield
Mentor
Mentor

Are these rules you created "Local" rules or "External" rules?

One thing that comes to mind that sounds like what your experiencing, is that when you have a local rule (rule is only located within the document), and that rule has BLUE colored local Parameters within it, this causes the rule to run every time it senses the value of that local parameter has changed.  This may be what's going on with it automatically changing on you when you use the Form.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 12 of 18

gordon.bowe
Contributor
Contributor

They are local rules and that was in fact the issue.  I checked the box under options to stop the rule from running automatically, but I may look in to making the rules external in the future.

 

Using the above code samples mentioned and a user parameter, I am able to get the thing working, however this requires adding the parameter to existing parts.

 

I have found a few examples of "try - catch" statements to attempt setting a parameter and using the catch to create one if it does not exist, but for some reason it is not working for me.  The code runs thru and no parameter is created.  Maybe this is due to the rule being a local rule?

 

I can upload the files

0 Likes
Message 13 of 18

WCrihfield
Mentor
Mentor

If you want a somewhat simple rule to create a UserParameter and set its options you can use something like the following code.

Parameter.UpdateAfterChange = True
iLogicVb.UpdateWhenDone = True
ups = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
oParamName = "CA"
Try
	'Checking to see if this Parameter already exists
	t = Parameter.Param(oParamName)
Catch
	'If this Parameter doesn't exist, create it
	oParameter = ups.AddByExpression(oParamName, 80 ,"in")
	cl = Parameter.Param(oParamName)
	cl.Comment = "This Parameter was created from an iLogic Rule."
	cl.IsKey = True
	cl.ExposedAsProperty = True
	upcpf = cl.CustomPropertyFormat
	upcpf.PropertyType = Inventor.CustomPropertyTypeEnum.kNumberPropertyType
	upcpf.Units = Inventor.UnitsTypeEnum.kInchLengthUnits
	upcpf.Precision = Inventor.CustomPropertyPrecisionEnum.kEightDecimalPlacesPrecision
End Try

'Output Parameters values from this rule to the Model. (Use this before Document Update)
RuleParametersOutput()
'Immediately update the current document.
InventorVb.DocumentUpdate()

I hope this helps.

If this solves your problem, or answers your questions, please click 'Accept As Solution'.

Or if this helps you along the way to solving your problem, please click 'Like'.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 14 of 18

gordon.bowe
Contributor
Contributor

Thank you so much, this works perfectly.

 

I have one final hurdle to overcome.

 

In my "local rule" version, i have separated the definitions as follows:

 

For Assembly documents i have:

 

Dim oCompDef As AssemblyComponentDefinition = ThisDoc.Document.ComponentDefinition

' Change BOM Structure based on multi value parameter
' Set BOM Structure iProperty for use with Vault Select Case oCompDef.BOMStructure Case "51970" BOM_MStr = "Normal" iProperties.Value("Custom", "BOM Structure") = "Normal" Case "51971" BOM_MStr = "Phantom" iProperties.Value("Custom", "BOM Structure") = "Phantom" Case "51972" BOM_MStr = "Reference" iProperties.Value("Custom", "BOM Structure") = "Reference" Case "51973" BOM_MStr = "Purchased" iProperties.Value("Custom", "BOM Structure") = "Purchased" Case "51974" BOM_MStr = "Inseparable" iProperties.Value("Custom", "BOM Structure") = "Inseparable" End Select

and for part documents i have:

 

Dim oCompDef As PartComponentDefinition = ThisDoc.Document.ComponentDefinition

' Change BOM Structure based on multi value parameter
' Set BOM Structure iProperty for use with Vault
Select Case oCompDef.BOMStructure Case "51970" BOM_MStr = "Normal" iProperties.Value("Custom", "BOM Structure") = "Normal" Case "51971" BOM_MStr = "Phantom" iProperties.Value("Custom", "BOM Structure") = "Phantom" Case "51972" BOM_MStr = "Reference" iProperties.Value("Custom", "BOM Structure") = "Reference" Case "51973" BOM_MStr = "Purchased" iProperties.Value("Custom", "BOM Structure") = "Purchased" Case "51974" BOM_MStr = "Inseparable" iProperties.Value("Custom", "BOM Structure") = "Inseparable" End Select

 

 

If i am going to combine these two in an external rule, I think i need to check if the open document is a part or assembly, and then set  oCompDef accordingly.

 

i tried a few things like:

 

 

Dim oDoc As Document = ThisApplication.ActiveDocument

'... Misc code here

If oDoc.DocumentType = kPartDocumentObject Then
  Dim oCompDef As PartComponentDefinition oCompDef = ThisDoc.Document.ComponentDefinition Else If oDoc.DocumentType = kAssemblyDocumentObject Then
Dim oCompDef As AssemblyComponentDefinition oCompDef = ThisDoc.Document.ComponentDefinition

however i get errors that oCompDef is not defined.   So i suppose you cannot define a variable within an "if - then" block?

 

 

full ***Non-Working*** code here:

'Rule will set the Default BOM structure of the document to match the user parameter BOM_MStr
'If the parameter does not exist, it will be created.

Dim oDoc As Document = ThisApplication.ActiveDocument


Parameter.UpdateAfterChange = True
iLogicVb.UpdateWhenDone = True
ups = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
oParamName = "BOM_MStr"

If oDoc.DocumentType = kPartDocumentObject Then
	Dim oCompDef As PartComponentDefinition
	oCompDef = ThisDoc.Document.ComponentDefinition

Else If oDoc.DocumentType = kAssemblyDocumentObject Then
	Dim oCompDef As AssemblyComponentDefinition
	oCompDef = ThisDoc.Document.ComponentDefinition

End If

Try
	'Checking to see if this Parameter already exists
	t = Parameter.Param(oParamName)

Catch
	'If this Parameter doesn't exist, create it
	oParameter = ups.AddByValue(oParamName, "Normal", UnitsTypeEnum.kTextUnits)
	MultiValue.SetList("BOM_MStr", "Normal", "Phantom", "Purchased", "Reference", "Inseperable")

End Try


' Change BOM Structure based on multi value parameter
Select Case BOM_MStr
	Case "Normal"
		oCompDef.BOMStructure = "51970"
	Case "Phantom"
		oCompDef.BOMStructure = "51971"
	Case "Reference"
		oCompDef.BOMStructure = "51972"
	Case "Purchased"
		oCompDef.BOMStructure = "51973"
	Case "Inseparable"
		oCompDef.BOMStructure = "51974"
End Select


'Output Parameters values from this rule to the Model. (Use this before Document Update)
RuleParametersOutput()
'Immediately update the current document.
InventorVb.DocumentUpdate()

 the end goal is to get a form that collects all the iProperties we have to change constantly in to one place.

The button "Apply BOM Changes" will run a rule based on the code I referenced in the "accepted solution".

 

ScreenCapture.PNG

0 Likes
Message 15 of 18

WCrihfield
Mentor
Mentor

OK.  Try this.

Parameter.UpdateAfterChange = True
iLogicVb.UpdateWhenDone = True

Dim oDoc As Document
oDoc = ThisDoc.Document


Dim oPdoc As PartDocument
oPdoc = ThisDoc.Document
Dim oPrtCompDef As PartComponentDefinition
Dim oAdoc As AssemblyDocument
oAdoc = ThisDoc.Document
Dim oAsmCompDef As AssemblyComponentDefinition
Dim oParams As Parameters

If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
	oPrtCompDef = oPdoc.ComponentDefinition
	oParams = oPrtCompDef.Parameters
ElseIf oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
	oAsmCompDef = oAdoc.ComponentDefinition
	oParams = oAsmCompDef.Parameters
Else
	MessageBox.Show("This rule [ " & iLogicVb.RuleName & " ] only works on either Part Documents or Assembly Documents.", "WRONG DOCUMENT TYPE")
	Return
End If

Dim oUParams As UserParameters
oUParams = oParams.UserParameters
Dim oUParam As UserParameter

Dim oParamName As String
oParamName = "BOM_MStr"

Dim oItExists As Boolean
oItExists = False

For Each oUParam In oUParams
	If oUParam.Name = oParamName Then
		oItExists = True
	End If
Next

If oItExists = False Then
	'The specified Parameter doesn't exist, so create it, sets its value(s) & options.
	oParameter = oUParams.AddByValue(oParamName, "Normal", UnitsTypeEnum.kTextUnits)
	MultiValue.SetList("BOM_MStr", "Normal", "Phantom", "Purchased", "Reference", "Inseperable")
ElseIf oItExists = True Then
	If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		If Parameter(oParamName) = "Normal" Then
			oPrtCompDef.BOMStructure = BOMStructureEnum.kNormalBOMStructure
		ElseIf Parameter(oParamName) = "Phantom" Then
			oPrtCompDef.BOMStructure = BOMStructureEnum.kPhantomBOMStructure
		ElseIf Parameter(oParamName) = "Reference" Then
			oPrtCompDef.BOMStructure = BOMStructureEnum.kReferenceBOMStructure
		ElseIf Parameter(oParamName) = "Purchased" Then
			oPrtCompDef.BOMStructure = BOMStructureEnum.kPurchasedBOMStructure
		ElseIf Parameter(oParamName) = "Inseperable" Then
			oPrtCompDef.BOMStructure = BOMStructureEnum.kInseparableBOMStructure
		End If
	ElseIf oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		If Parameter(oParamName) = "Normal" Then
			oAsmCompDef.BOMStructure = BOMStructureEnum.kNormalBOMStructure
		ElseIf Parameter(oParamName) = "Phantom" Then
			oAsmCompDef.BOMStructure = BOMStructureEnum.kPhantomBOMStructure
		ElseIf Parameter(oParamName) = "Reference" Then
			oAsmCompDef.BOMStructure = BOMStructureEnum.kReferenceBOMStructure
		ElseIf Parameter(oParamName) = "Purchased" Then
			oAsmCompDef.BOMStructure = BOMStructureEnum.kPurchasedBOMStructure
		ElseIf Parameter(oParamName) = "Inseperable" Then
			oAsmCompDef.BOMStructure = BOMStructureEnum.kInseparableBOMStructure
		End If
	End If
End If

'Output Parameters values from this rule to the Model. (Use this before Document Update)
RuleParametersOutput()
'Immediately update the current document.
InventorVb.DocumentUpdate()

It may be a bit lengthy, and perhaps not the most efficient, but I think it will get the job done.

You probably already know this, but when you move your iLogic code from a 'Local' rule to an 'External' rule, you have to call out the Parameters differntly for them to continue to work properly.  That's why I used Parameter(oParamName) instead of just BOM_MStr.

Also, another coder on this Forum informed me that a For Each...Next loop statement actually uses less processing resources than a Try...Catch statement, so I changed the portion where it is looking for the Parameter to use this method instead.

You probably noticed that I changed the Select Case section to a If...Then section too.  This is somewhat of a personal preference on my part, because for some odd reason, I seem to have fewer errors down the road using this style.

 

I hope this helps.

If this helps, please click 'Like'.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 16 of 18

gordon.bowe
Contributor
Contributor

@WCrihfield wrote:

It may be a bit lengthy, and perhaps not the most efficient, but I think it will get the job done.

You probably already know this, but when you move your iLogic code from a 'Local' rule to an 'External' rule, you have to call out the Parameters differntly for them to continue to work properly.  That's why I used Parameter(oParamName) instead of just BOM_MStr.

Also, another coder on this Forum informed me that a For Each...Next loop statement actually uses less processing resources than a Try...Catch statement, so I changed the portion where it is looking for the Parameter to use this method instead.

You probably noticed that I changed the Select Case section to a If...Then section too.  This is somewhat of a personal preference on my part, because for some odd reason, I seem to have fewer errors down the road using this style.

 

I hope this helps.

If this helps, please click 'Like'.



I had no idea, that is good to know.

Also I had heard the same thing about Try-Catch and thought about re-writing it, but decided to at least get the core code working first.

 

 

incidentally, your rule isnt working for me either.  It gives no errors or warnings, but i am getting an error when running it.

 

the first thing was that inventor did not like your message box.  RuleName is not a member of 'Autodesk.iLogic.Interfaces.ILowLevelSupport'.

MessageBox.Show("This rule [ " & iLogicVb.RuleName & " ] only works on either Part Documents or Assembly Documents.", "WRONG DOCUMENT TYPE")

But deleting that part let it compile.

then i got this error on run

 

ScreenCapture.PNG

 

I tried reorganizing the program to make it easier to troubleshoot, but that did not help

 

Sub Main()
	'Rule will set the Default BOM structure of the document to match the user parameter BOM_MStr
	'If the parameter does not exist, it will be created.

	'Check for Parameter "BOM_MStr"
	CheckParameter()

	' Change BOM Structure based on multi value parameter
	SetBOM()

End Sub

Function CheckParameter()

	Dim oParams As Parameters

	Dim oUParams As UserParameters = oParams.UserParameters
	Dim oUParam As UserParameter

	Dim oParamName As String = "BOM_MStr"

	Dim oItExists As Boolean = False

	Parameter.UpdateAfterChange = True
	iLogicVb.UpdateWhenDone = True

	For Each oUParam In oUParams
		If oUParam.Name = oParamName Then
			oItExists = True
		End If
	Next

	If oItExists = False Then
		'The specified Parameter doesn't exist, so create it, sets its value(s) & options.
		oParameter = oUParams.AddByValue(oParamName, "Normal", UnitsTypeEnum.kTextUnits)
		MultiValue.SetList("BOM_MStr", "Normal", "Phantom", "Purchased", "Reference", "Inseperable")
	ElseIf oItExists = True Then
		'do nothing
	End If

	'Output Parameters values from this rule to the Model. (Use this before Document Update)
	RuleParametersOutput()
	'Immediately update the current document.
	InventorVb.DocumentUpdate()

End Function

Function SetBOM()

	Dim oDoc As Document = ThisDoc.Document

	Dim oPdoc As PartDocument = ThisDoc.Document
	Dim oPrtCompDef As PartComponentDefinition

	Dim oAdoc As AssemblyDocument = ThisDoc.Document
	Dim oAsmCompDef As AssemblyComponentDefinition

	If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		oPrtCompDef = oPdoc.ComponentDefinition
		oParams = oPrtCompDef.Parameters
	ElseIf oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		oAsmCompDef = oAdoc.ComponentDefinition
		oParams = oAsmCompDef.Parameters
	Else
		MessageBox.Show("This rule only works on either Part Documents or Assembly Documents.", "WRONG DOCUMENT TYPE")
		Return False
	End If

	If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		If Parameter(oParamName) = "Normal" Then
			oPrtCompDef.BOMStructure = BOMStructureEnum.kNormalBOMStructure
		ElseIf Parameter(oParamName) = "Phantom" Then
			oPrtCompDef.BOMStructure = BOMStructureEnum.kPhantomBOMStructure
		ElseIf Parameter(oParamName) = "Reference" Then
			oPrtCompDef.BOMStructure = BOMStructureEnum.kReferenceBOMStructure
		ElseIf Parameter(oParamName) = "Purchased" Then
			oPrtCompDef.BOMStructure = BOMStructureEnum.kPurchasedBOMStructure
		ElseIf Parameter(oParamName) = "Inseperable" Then
			oPrtCompDef.BOMStructure = BOMStructureEnum.kInseparableBOMStructure
		End If
	ElseIf oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		If Parameter(oParamName) = "Normal" Then
			oAsmCompDef.BOMStructure = BOMStructureEnum.kNormalBOMStructure
		ElseIf Parameter(oParamName) = "Phantom" Then
			oAsmCompDef.BOMStructure = BOMStructureEnum.kPhantomBOMStructure
		ElseIf Parameter(oParamName) = "Reference" Then
			oAsmCompDef.BOMStructure = BOMStructureEnum.kReferenceBOMStructure
		ElseIf Parameter(oParamName) = "Purchased" Then
			oAsmCompDef.BOMStructure = BOMStructureEnum.kPurchasedBOMStructure
		ElseIf Parameter(oParamName) = "Inseperable" Then
			oAsmCompDef.BOMStructure = BOMStructureEnum.kInseparableBOMStructure
		End If
	End If

End Function

on my re-organized version, i get an error as well that object reference is not set to an instance of an object.

I am starting to feel that I may be asking too much of inventor / my programming skills. 

0 Likes
Message 17 of 18

WCrihfield
Mentor
Mentor

This certainly seems possible, so I think that if not either of us, then someone else will fix this code to work right for you.

FYI:  One other thing that might help you along the way though.  Your ealier assumption was pretty much correct.

You can't 'define' (exe. Dim oCompDef As PartComponentDefinition) an Object or Variable within a Try...Catch statement, or within an If...Then statement, then use that Object or Variable  properly later, outside of that statement.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 18 of 18

gordon.bowe
Contributor
Contributor

I will give it some time for other people to take a look, but if I don't hear anything after some time, I can at least always fall back to having a local rule and one version for parts and one version for assemblies.

Remember that I had the thing working fine, and only needed to add a new parameter if it did not exist already.

We have figured out how to accomplish that, so I can add that code and move on.

0 Likes