Message: Object reference not set to an instance of an object when used of MultiValue.List in assembly doc

Message: Object reference not set to an instance of an object when used of MultiValue.List in assembly doc

m_reza_shahparast
Contributor Contributor
993 Views
13 Replies
Message 1 of 14

Message: Object reference not set to an instance of an object when used of MultiValue.List in assembly doc

m_reza_shahparast
Contributor
Contributor

When i follow your tutorials and used of This :

 

Dim doc as AssemblyDocument = ThisApplication.ActivateDocument

 

Dim oAssDef as AssemblyComponentDefinition = doc.ComponentDefinition

 

Dim oUsPar as userparametes = oAssDef.parameters.Userparameters

 

Dim oPar as Userparameter = oUsPar.AddByValue("Side", "", unitstypeenum.ktextunits)

 

 

Dim oArr as New ArrayList

oArr = MultiValue.List("Side")

oArr.Add("Left Side")

oArr.Add("Right Side")

MultiValue.List("Side") = oArr

 

 

Get this error:

" Object reference not set to an instance of an object"

 

 

I think "MultiValue.List()" couldn't get object of userparameter

@JelteDeJong 

0 Likes
994 Views
13 Replies
Replies (13)
Message 2 of 14

ryan.rittenhouse
Advocate
Advocate

You've got some Typos in there and setting a multivalue list is done a little differently. Switching up your code to something like this should get you going:

 

Dim doc As AssemblyDocument = ThisDoc.Document
Dim oAssDef As AssemblyComponentDefinition = doc.ComponentDefinition
Dim oUsPar As UserParameters = oAssDef.Parameters.UserParameters
Dim oPar As UserParameter = oUsPar.AddByValue("Side", "", UnitsTypeEnum.kTextUnits)

Dim oArr As ArrayList = MultiValue.List("Side")
oArr.Add("Left Side")
oArr.Add("Right Side")
MultiValue.SetList("Side", oArr.ToArray())

 

If this solved your problem, or answered your question, please click Accept Solution.
0 Likes
Message 3 of 14

m_reza_shahparast
Contributor
Contributor

Hi dear

With Respectfully How I can be able to initialized the "MultiValue.SetList, MultiValue.List, Parameter()  and other ilogic property and sub" in visual Studio class AbstractRuleClass, and then used of this with inherits ?

0 Likes
Message 4 of 14

m_reza_shahparast
Contributor
Contributor

Hidear

thanks for your help

I'll give you feedback when I used your code.

 

0 Likes
Message 5 of 14

WCrihfield
Mentor
Mentor

It sounds like you have created a separate Class block of code, and are trying to use the common iLogic objects within that Class.  This is a common problem, but a very difficult one to explain properly.  The iLogic add-in and its user interface (the iLogic Rule editor dialog) do a whole lot of extra stuff for us, to make automating Inventor simpler and easier for beginners, who do not have much (if any) previous coding experience.  One of the things it does for us is create a bunch of 'Rule Objects' for us (behind the scenes, where we do not see the code supporting it) to readily use in our simpler rules (rules without Class/Module/Structure type blocks of code).  Part of how this is done is with a 'Partial' Class, where we never really see that 'other' partial Class named "ThisRule".  The iLogic add-in will automatically create that Class for us, so we do not need to start all of our rules with a Class statement.  It will also automatically create the 'Sub Main' block for us, if we do not.  But, we never see all that stuff.

 

The most common way to use those types of objects that are unique to the iLogic add-in within other Classes, is to 'pass' them in, either through input variables to methods (subs or functions) defined within the Class (such as the 'New' sub routine), or through public properties defined within the Class.

You can also review the following topic:

https://forums.autodesk.com/t5/inventor-programming-ilogic/object-reference-issue-in-rule/m-p/130311... 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 14

m_reza_shahparast
Contributor
Contributor

Thanks so much

As your opinion whats the best way ????

 

I've many rules in ilogic and want to move all of them to addin with different buttons 

0 Likes
Message 7 of 14

ryan.rittenhouse
Advocate
Advocate

I wouldn't call this the best way, but I often need the Rule Objects in functions that are in an external VB file, and I get them by passing in the iLogicVb object and using the CreateObjectProvider function to get access to the rest of the rule objects. Here's an example using your code, the following would go into VB/whatever:

Sub CreateSideList(iLogicVb As ILowLevelSupport)
	
	Dim doc As AssemblyDocument = iLogicVb.RuleDocument
	Dim oAssDef As AssemblyComponentDefinition = doc.ComponentDefinition
	Dim oUsPar As UserParameters = oAssDef.Parameters.UserParameters
	Dim oPar As UserParameter = oUsPar.AddByValue("Side", "", UnitsTypeEnum.kTextUnits)
	Dim mValue As IMultiValueParam = iLogicVb.CreateObjectProvider(doc).MultiValue
	Dim oArr As ArrayList = mValue.List("Side")
	oArr.Add("Left Side")
	oArr.Add("Right Side")
	mValue.SetList("Side", oArr.ToArray())
	
End Sub

Then, in an inventor rule, you would call it like this:

CreateSideList(iLogicVb)

We make use of an addin to put bottons on our ribbon a lot, but we usually have the button link to iLogic rules so it's easier to update the code as we need to and don't need to update the add-in.

 

Hope this helps

If this solved your problem, or answered your question, please click Accept Solution.
0 Likes
Message 8 of 14

m_reza_shahparast
Contributor
Contributor

It's not work

But so thanks 

0 Likes
Message 9 of 14

m_reza_shahparast
Contributor
Contributor

Sure i'll try it and thank you.

0 Likes
Message 10 of 14

m_reza_shahparast
Contributor
Contributor

Sincerely, thank you for your time

 

Posible for you to tell me how i can initialized the ilowlevelsupport in AbstractRuleClass?

 

Like this:

Public Property ThisApplication as Inventor.Application

 

Public ReadOnly property Thisdoc as iCadDoc

Get

Return new CadDoc(ThisApplication.activedocument)

End get

 

 

Or something along those lines 

 

 

I dont know what to use to "get" to ilowlevelsupport?

0 Likes
Message 11 of 14

WCrihfield
Mentor
Mentor

I am pretty sure that I have seen forum replies where someone from Autodesk has told us that 'creating' or 'getting' the 'iLogicVb' (ILowLevelSupport) object within external resources like add-ins is not possible, and therefore it must be passed in.  There are a few things which are unique to iLogic that we can do in external resources without needing to pass that object in though.  Perhaps the oldest, and best known way is to get the actual ApplicationAddIn object for iLogic, then create a variable set to Object type, or more specifically IiLogicAutomation Type, then set its value with the ApplicationAddIn.Automation property.  Not all add-ins have a useful value for that, but since iLogic has its own internal API, it does have a useful value.  It will act the same as the phrase 'iLogicVb.Automation' does in a normal iLogic rule.

 

PS.  Also, for what its worth, if you go to the Tools tab, then click on the Add-Ins button on the Options panel, it will open the small dialog with two tabs, showing all the current add-ins.  If you select one, then look at the bottom of the dialog, you will see the location of the primary DLL file supporting that add-in.  This of course is not the only file involved, but sometimes you can add a reference to that DLL in your code, then you can explore some of the stuff that they have publicly accessible in that DLL.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 12 of 14

WCrihfield
Mentor
Mentor

Since most of those types of objects are an Interface, instead of a Class, we can not simply use the New keyword to create a new instance of any of them.  So, once we have passed in one of these base Rule Objects like 'iLogicVb', we can use that to set the value of our internal variables.  Then we can create our own internal variables for the other Rule Objects, and populate them from the first object, if it has the scope necessary, as that object does.

 

Below is an basic example Class that requests the iLogicVb object be passed into it within its 'New' Sub routine, like what was suggested multiple times above.  It then uses that to set the value of a private, internal variable, which this Class will use for all other things requiring its capabilities.  Then, from there, it starts populating the values of other similar private, internal variables.  Then it uses one of those variables within a super basic function.

 

Public Class TestClass
	
	Public Sub New(iLogicVb As ILowLevelSupport)
		_iLogicVb = iLogicVb
		_InvApp = _iLogicVb.Application
		_iLogicAuto = _iLogicVb.Automation
		_Doc = _iLogicVb.RuleDocument
		_ObjectProvider = _iLogicVb.CreateObjectProvider(_Doc)
		_ThisDoc = _ObjectProvider.ThisDocument
	End Sub
	
	Private Property _InvApp As Inventor.Application
	Private Property _iLogicVb As ILowLevelSupport
	Private Property _iLogicAuto As IiLogicAutomation
	Private Property _ObjectProvider As IStandardObjectProvider
	Private Property _Doc As Inventor.Document
	Private Property _ThisDoc As ICadDoc
		
	Public Function Prop(sPropertySetName As String, sPropertyName As String) As Object
		Return _ObjectProvider.iProperties.Value(sPropertySetName, sPropertyName)
	End Function
		
End Class

 

and a regular rule that creates a new instance of this Class, passed the required input, then uses its one public function to retrieve the value of a custom iProperty.

 

Dim oMyTestClass As New TestClass(iLogicVb)
Dim oValue As Object = oMyTestClass.Prop("Custom", "Testing")
MsgBox(oValue.ToString,,"")

 

However, in order for this rule to be able to access the definition of that Class, the Class definition must either also be within the same rule, or it must be within an external rule file that has had its option called "Straight VB Code" turned on (checked), and you just include the 'AddVbFile' in the header of this simple rule, followed by the quoted name of that external rule, with file extension included.  And that external rule must be in one of the locations specified within your iLogic Configuration settings, so it can find it.  Otherwise, you may need to specify the full file path to that file within that header line.

 

Edit:  Almost forgot to mention that...in the external rule, where your custom Class is defined, which is set to Straight VB Code', you can include 'Imports' type lines within that rule's header, but you usually can not include 'AddReference' type lines in that rule's header.  So, if that external rule is attempting to access any object Types, or methods that are defined in other references, you will need to include those 'AddReference' lines in the header of the 'regular' rule that will be referencing that external rule, and using it.  In this case the required line would be:

AddReference "Autodesk.Inventor.Interop"

That reference is where the object Types 'Inventor.Application', and 'Inventor.Document' are defined.  Your regular rules will include references to these, and much, much more for you, behind the scenes, where you never see it, to help you out.  But it does not 'help' those externally referenced rules with 'Straight VB Code' set.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 13 of 14

m_reza_shahparast
Contributor
Contributor

You are amazing, thank you for your excellent and comprehensive explanations. I will definitely implement what you said and give you feedback.

 

Sincerely, thank you for your time.

0 Likes
Message 14 of 14

m_reza_shahparast
Contributor
Contributor

You are amazing, thank you for your excellent and comprehensive explanations. I will definitely implement what you said and give you feedback.

 

Sincerely, thank you for your time 🪴🪴🌹

0 Likes