SelectSet (or Multiple Select) and Assign to Layer

SelectSet (or Multiple Select) and Assign to Layer

dmurray3QNXP
Explorer Explorer
613 Views
3 Replies
Message 1 of 4

SelectSet (or Multiple Select) and Assign to Layer

dmurray3QNXP
Explorer
Explorer

Hello all,

 

First time posting, been rummaging through the Forums for a while and have come across a lot of really good info.

 

I am trying to make a chunk that will take my selections inside a drawing and move them to my "Red" layer; pretty simple. But what I have found, is that when I grab these objects from the SelectSet, or even better [Grab Select iLogic Example] which returns an ObjectsEnumerator, the code won't dynamically access the object!

 

Below is working code, however I don't want to have a "Select Case" and have to DirectCast() based on the TypeName?! It is so beyond redundant...

I feel like the For Each loop should only be that single line that is commented out, but for some reason it produces some junk error:

Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))

 

Working Code:

Dim oDraw As Inventor.DrawingDocument = ThisDoc.Document
Dim oMgr = oDraw.StylesManager
Dim sSet As SelectSet = oDraw.SelectSet

Dim desLayer As Inventor.Layer = oMgr.Layers("Red")

'Filtering out objects that have already been assigned. Future code to possibly revert objects away from "Red" back to "Default"
For Each _ent In sSet.OfType(Of Object).Where(Function(x) x.Layer.Name <> desLayer.Name)
'       _ent.Layer = desLayer    '<--- This line should be Gold
	Select Case TypeName(_ent)
		Case "Balloon" : CType(_ent, Balloon).Layer = desLayer
		Case "SketchEntity" : DirectCast(_ent, Inventor.SketchEntity).Layer = desLayer
		Case "GeneralNote" : DirectCast(_ent, Inventor.GeneralNote).Layer = desLayer
		Case "LinearGeneralDimension" : DirectCast(_ent, LinearGeneralDimension).Layer = desLayer
  	End Select
Next

 

As you could imagine, this code snippet has only 4 drawing objects, but obviously I want it to include Notes, SketchedSymbols, Ordinate Dims, Thread Notes.... Whatever I have selected. Especially with that code example above, I could be selecting lots!

 

Any ideas?

0 Likes
Accepted solutions (1)
614 Views
3 Replies
Replies (3)
Message 2 of 4

basautomationservices
Advocate
Advocate
Accepted solution

In c# you have a type called 'dynamic' which makes possible what you want. The VB.net equivalent is 'Option strict off' in combination with 'object'. This seems not to work in iLogic.

 

I suggest using reflection to access the 'layer' property. 

 

Dim oDraw As Inventor.DrawingDocument = ThisDoc.Document
Dim oMgr = oDraw.StylesManager
Dim sSet As SelectSet = oDraw.SelectSet
Dim desLayer As Inventor.Layer = oMgr.Layers("Border (ISO)")
Dim pName = "Layer"

For Each ent In sSet.OfType(Of Object).Where(Function(x) x.Layer.Name <> desLayer.Name)
    Dim t = ent.GetType()
    t.InvokeMember(pName, BindingFlags.SetProperty, Nothing, ent, New Object() {desLayer})
Next

Contact me for custom app development info@basautomationservices.com. Follow below links to view my Inventor appstore apps.

Free apps: Smart Leader | Part Visibility Utility | Mate Origins

Paid apps: Frame Stiffener Tool | Constrain Plane Toggle | Property Editor Pro


0 Likes
Message 3 of 4

dmurray3QNXP
Explorer
Explorer

Great, that works!

Interesting function, thanks for the reference.

 

Had to expand the "BindingFlags" FYI for anyone else coming along later:

t.InvokeMember("Layer", System.Reflection.BindingFlags.SetProperty, Nothing, _ent, New Object() {desLayer})
0 Likes
Message 4 of 4

basautomationservices
Advocate
Advocate

Oh ye. That's true. I used 'Imports System.Reflection' at the top. Forgot to paste it, didn't know if it was a default import in ilogic.

 

We need to use InvokeMember here instead of being able to access the property info due to the underlying object being a 'System.__ComObject'. Otherwise it would be possible to access the PropertyInfo and set the property from there.

Contact me for custom app development info@basautomationservices.com. Follow below links to view my Inventor appstore apps.

Free apps: Smart Leader | Part Visibility Utility | Mate Origins

Paid apps: Frame Stiffener Tool | Constrain Plane Toggle | Property Editor Pro


0 Likes