iLogic option-Straight VB code: area accepts header statements only

iLogic option-Straight VB code: area accepts header statements only

snappyjazz
Collaborator Collaborator
9,842 Views
7 Replies
Message 1 of 8

iLogic option-Straight VB code: area accepts header statements only

snappyjazz
Collaborator
Collaborator

I have an ilogic rule with the Straight VB code option selected. Now there's an issue with the main sub (See below). The issue says:

"This area accepts header statements only, Such as 'Imports', 'AddReference', 'Option' and others. Ordinary rule code belongs in the lower area."

 

The thing is, the rule has a header section containing the inports, and the main sub is in the lower area. It doesn't make sense. Do you how to rectify the issue?

image.png

 

Imports System
Imports System.Type
Imports System.Activator
Imports SysInter = System.Runtime.InteropServices
Imports System.IO
Imports Inv = Inventor
'By:
'Date: 10/11/19

Public Sub Main()
	MsgBox("Main")
	GetDoc()
End Sub

'=\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/==\/=

#Region "Some Sub to Test"
Public Sub GetDoc()
        
		If ThisDoc.Document.Type = Inv.DocumentTypeEnum.kAssemblyDocumentObject Then
			MsgBox("Document is an Assembly.")
		Else If ThisDoc.Document.Type = Inv.DocumentTypeEnum.kDrawingDocumentObject Then
			MsgBox("Document is an Drawing.")
		Else If ThisDoc.Document.Type = Inv.DocumentTypeEnum.kPartDocumentObject Then
			MsgBox("Document is an Drawing.")
		Else
			MsgBox("Not a typical document type.")
		End If


    End Sub

#End Region
0 Likes
9,843 Views
7 Replies
Replies (7)
Message 2 of 8

MjDeck
Autodesk
Autodesk

Straight VB code has different syntax requirements than regular iLogic code.

It requires a Class declaration. Add a line like this:

Class VbCode

before the Sub Main line. Set the name of the class to whatever you want.

Then at the bottom of the rule, add this line:

End Class

That will get rid of the error.

Another problem: ThisDoc is not available in Straight VB code. You can declare your own ThisDoc property (of type ICadDoc).
How do you plan to use this Straight VB rule?

 


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 3 of 8

snappyjazz
Collaborator
Collaborator

You said: "You can declare your own ThisDoc property (of type ICadDoc)."

This is probably much simpler than I'm making it. I can't seem to get it to work. Can you show me?

 

"How do you plan to use this Straight VB rule?"

I write my code in visual studio and paste it into an ilogic rule. Doing it this way, there's always clean up to do with the code. So if I can find a way so I don't have to mess with the code after putting it in a rule would be nice.

Also, since I don't fully understand how straight vb code works; I would like to know if having one ilogic rule with multiple sub routines works better for me, than having multiple rules.

How do you use straight vb code in ilogic?

 

 

 

 

0 Likes
Message 4 of 8

MjDeck
Autodesk
Autodesk

One way to use ThisDoc in a straight VB rule is to add it as an argument to a class constructor. The straight VB rule would look like this:

Class DocumentUtils
Private Property ThisDoc As ICadDoc
Public Sub New(thisDoc As ICadDoc)
	Me.ThisDoc = thisDoc
End Sub
Public Sub Main()
' ...
End Sub End Class

If you put that in a rule named "DocumentUtils", then you can call it from another rule like this:

AddVbRule "DocumentUtils"

Dim docUtils As New DocumentUtils(ThisDoc)
docUtils.Main()

Straight VB rules are useful if you have code that is required in several different rules. You can keep it in one place and use AddVbRule to reference it wherever you need it.

One caveat: unlike regular rules, a straight VB rule is not compiled when you save it. It only gets compiled when a rule that consumes it is compiled or run. If you change a straight VB rule, you also have to change (or Regenerate) all the rules that reference that rule. Or another option is to save, close, and then reopen the document.
There's some more info on this page.

 


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 5 of 8

MjDeck
Autodesk
Autodesk

Note: your rule needs to use ThisDoc.Document.DocumentType instead of ThisDoc.Document.Type.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 6 of 8

snappyjazz
Collaborator
Collaborator

Is ICadDoc a data type that's only available in an iLogic rule set to "Straight VB code"? I've added that to my code in visual studio and ICadDoc doesn't work as a data type.

0 Likes
Message 7 of 8

MjDeck
Autodesk
Autodesk

ICadDoc is an Interface in Visual Basic. You can use it in Visual Studio if you add a reference to Autodesk.iLogic.Interfaces.dll (in the Inventor bin folder).

To get the actual objects into your DLL, you have to pass them in from the iLogic rule. One way to pass them in would be in a constructor, similar to the Straight VB rule example.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 8 of 8

Anonymous
Not applicable

I'm facing a similar problem: I'm trying to use Inventor.PartDocument in a Straight VB Code rule, but I think the Straight VBs can't access PartDocument as well. 

 

Isn't there another way of achieving it without passing objects during class construction? I'm asking this because I was thinking about creating a Module instead of a Class, and a Module has no constructor.

 

I made a post about it, if any of you could check: https://forums.autodesk.com/t5/inventor-customization/can-t-quot-dim-as-partdocument-quot-in-rule-ma...

0 Likes