Create ComponentOccurrence Type Class - Assign Object to Base of Class

Create ComponentOccurrence Type Class - Assign Object to Base of Class

C_Haines_ENG
Collaborator Collaborator
723 Views
5 Replies
Message 1 of 6

Create ComponentOccurrence Type Class - Assign Object to Base of Class

C_Haines_ENG
Collaborator
Collaborator

Hello all,

 

Is there a way to assign the initial variable used to create a class as an object? Similar to how you have a ComponentOccurrence object, that has further parameters. It might be easier to explain with code:

 

Sub Main
	
	Dim oAsm As AssemblyDocument = ThisDoc.Document
	Part.oApp = ThisApplication
		
	Dim Part_1 As New Part
	Dim Part_2 As New Part
	
	If Part_1 Is Part_2 Then MsgBox("Parts are the same!")
	
End Sub

Class Part
	
	Public Shared oApp As Inventor.Application
	
	Frame = oApp.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "SELECT PART 1")
	
	Public Property Name As String = Frame.Name
	
End Class

This doesn't work of course, but It would be nice to have the base item represent an object. I don't think this is possible but It would be very nice. 

 

The same way the base of a ComponentOccurrence is an object, can a class be the same or do I need to set a variable representing the Component. 

0 Likes
Accepted solutions (1)
724 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor

Hi @C_Haines_ENG.  What you posted is a little confusing.  What is your end goal of trying to create something like that?  In iLogic rules, the ComponentOccurrence object is an Interface.

WCrihfield_0-1730298585824.png

You can not use the 'New' keyword to create a new instance of an Interface, like you can a Class.  But a Class can 'Implement' an Interface, to use it.  However, if you just want to be able to add functionality to an 'instance' of a ComponentOccurrence, such as one that a variable in your code represents (after it has been assigned a value), then you may be able to use a Module.  A Module can be used to define 'extensions' to instances of existing Types.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

C_Haines_ENG
Collaborator
Collaborator

The goal is to add properties to a ComponentOccurance, 

 

I don't see how this is possible with a module, could you put a simple example?

0 Likes
Message 4 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Here is an example of a Module being used within an iLogic rule, to add what acts like a Property to the ComponentOccurrence type object, as an 'extension' of that instance.  This adds a 'member' named 'IsVirtual', which provides a Boolean Type value.

Class ThisRule
	Sub Main
		Dim oPickedOcc As ComponentOccurrence = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select a Component.")
		If oPickedOcc Is Nothing Then Return
		If oPickedOcc.IsVirtual() Then
			MsgBox("IsVirtual = True",,"")
		Else
			MsgBox("IsVirtual = False",,"")
		End If
	End Sub
End Class

Friend Module ComponentOccurrenceModule
	
	<System.Runtime.CompilerServices.Extension>
	Public Function IsVirtual(oOcc As Inventor.ComponentOccurrence) As Boolean
		If Not oOcc.Suppressed Then
			If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
				Return True
			End If
		End If
		Return False
	End Function
End Module

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 6

C_Haines_ENG
Collaborator
Collaborator

Very very nice, Ill have to figure out how to shrink the size a little bit, but other than that, awesome!

 

Does this do the calculation every time you call on that property? That could get... slow.

0 Likes
Message 6 of 6

WCrihfield
Mentor
Mentor

Not sure what 'calculation' you mean, but yes, it does the 'checks' / 'processing' each time it is called.  If we try to access the ComponentOccurrence.Definition property of one that is currently Suppressed, it will throw an error.  And the only way that comes to mind for me to check if a component is 'virtual' or not, is by checking the Type of its ComponentDefinition, using that property, therefore it must always do 2 checks, to avoid the potential error.  You could also use a Try...Catch...End Try statement, without that first check, but that may require even more processing, due to 'handling' an Exception.

 

Unfortunately, we can only add 'extension methods' (Subs or Functions), and not 'extension properties'.  If you needed actual properties, then you would have to create a derived Class, which Implements the ComponentOccurrence Interface.  But if you do that, then it requires you to also Implement each of the 'parent' Interface's Properties, Methods, and Events, which would require a TON of code...that we do not have access to the 'source' code for, to properly duplicate.

 

Edit:  Also, if you are worried about 'space' (you mentioned wanting to 'shrink it'), then you could put the extensions Module code out in its own separate external iLogic rule file, and set its 'Straight VB Code' option on.  Then, to use that Module's resources from another 'regular' rule, you would have to add the 'AddVbFile' line of code within the 'Header' of your other rule (the rule wanting to use it), followed by the file name (including file extension) of that external iLogic rule file.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes