Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
MegaJerk
in reply to: Ktomberlin

To understand what you were doing wrong you need to understand that the custom snippets of iLogic code like ThisDoc and ThisDrawing are just taking the active document object (that the rule is being run from), and returning something back.

 

In the case of ThisDoc, it’s returning an object of type Document.

 

In the case of ThisDrawing, it’s returning an object of type DrawingDocument.

Before your code ever runs, the compiler will try to resolve those Functions. The problem is when you’re ActiveDocument is of a type that doesn’t match with the returning type, it’s like trying to fit a square peg into a round hole.

To, in the most terrible way possible, highlight what I’m talking about, look at the code below. If you were to run that on a drawing, you’d get three separate message box dialogs that would pop up with information about the document type. Everything would be fine! However, If you attempted to run that code from an Assembly level, the initial two message boxes would come up fine, but once it tried to put the Active AssemblyDocument into a DrawingDocument type, it will error out (on the third dialog).

 

Module hiddenModule 

	' ***********************************
	' YOU CAN IGNORE THIS TOP BIT
	' ***********************************

	Public Dim ThisApplication As Object
	
	
	' Don't pay attention to any of this. 
	' When you try to access the 'ThisApplication' from 
	' a level above the Main() Sub, there are permission
	' issues. All I'm doing here is creating a really 
	' makeshift bridge... 
	
	Sub New ()
		Try
			ThisApplication = GetObject(, "Inventor.Application")
			'MessageBox.Show("ThisApplication Set")
		Catch ex As Exception
			MessageBox.Show("Inventor must be running.", "Inventor.exe Error")
		End Try
	End Sub 
	
	' ***********************************
	' PAY ATTENTION TO THE BELOW
	' ***********************************
	
	Public Function MyThisDoc () As Inventor.Document
		MyThisDoc = ThisApplication.ActiveDocument 
	End Function 
	
	Public Function MyThisDrawing () As Inventor.DrawingDocument
		MyThisDrawing = ThisApplication.ActiveDocument  
	End Function 
	
End Module

Sub Main () 
	Dim oDoc As Document 
	oDoc = MyThisDoc
	
	MessageBox.Show(oDoc.DocumentType.ToString(), "oDoc")
	MessageBox.Show(MyThisDoc.DocumentType.ToString(), "MyThisDoc")
	MessageBox.Show(MyThisDrawing.DocumentType.ToString(), "MyThisDrawing")
	
End Sub 

 

Soooo. Knowing all of that, it was simple to get your code working across every Document environment.

See Below:

Dim oDoc As Document
oDoc = ThisDoc.Document

' While you don't have to declare your 
' string variable, technically, I still
' did out of clarity
Dim modelFullFileName As String

Select Case oDoc.DocumentType 
	Case = kPartDocumentObject
  
		If TypeOf ThisApplication.ActiveEditObject Is Sketch Then
			MessageBox.Show("You have a sketch active in a part file.", "iLogic")
			Else
			MessageBox.Show("This is a part file.", "iLogic")
		End If
  
	Case = kAssemblyDocumentObject
  
		If TypeOf ThisApplication.ActiveEditObject Is Sketch Then
			MessageBox.Show("You have a sketch active in an assembly file.", "iLogic")
			Else
			MessageBox.Show("This is an assembly file.", "iLogic")
		End If
  
	Case = kDrawingDocumentObject
    
		drwquestion = _
			MessageBox.Show("This is a drawing file, Do you want to open the file?", _
			"Ilogic", _
			MessageBoxButtons.YesNo,MessageBoxIcon.Question)
		

		If drwquestion = vbYes Then	
			modelFullFileName = ThisDoc.ModelDocument.FullFileName
			ThisApplication.Documents.Open(modelFullFileName, True)
			Else 
			MessageBox.Show("Nothing Opened", "iLogic")
		End If
  
End Select 



The biggest thing to change (besides turning your ElseIfs into a (in my own opinion) cleaner looking Select Case statement), was that the ThisDrawing was substituted for a ThisDoc. Because we’re already checking the type (in the Select Case), we don’t need to use the Drawing Specific iLogic Function of ThisDrawing to return a DrawingDocument to us.

Instead we can just use the generic Document Type.

Now. If you wanted to use DrawingDocument specific properties that aren’t available to the base Document Class, you would absolutely need to define a new DrawingDocument object, however, in your case, you’re just getting a file name which happens to be a property of the base class type.

Sorry for the long explanation. But things like this can be a mystery later down the road, and it might help you (or others) at some point in the future.



If my solution worked or helped you out, please don't forget to hit the kudos button :slightly_smiling_face:
iLogicCode Injector: goo.gl/uTT1IB

GitHub