Question about declaring a document variable

Question about declaring a document variable

Anonymous
Not applicable
795 Views
5 Replies
Message 1 of 6

Question about declaring a document variable

Anonymous
Not applicable

Howdy all,

Am i correct in the assumption that ilogic checks declarations throughout a rule before running the rule as opposed to checking them from top to bottom?

I have a rule that uses ThisDrawing.ModelDocument near the bottom of the code and ilogic spits out the error:

       "ThisDrawing: This document "D08A01307 - 4081 CONNECTION PLATE" is not a drawing."

My issue with this is that i have"

If oDocType <> DocumentTypeEnum.kDrawingDocumentObject Then 
    MessageBox.Show("This Rule Only Works On Drawings", "iLogic")
    Return
End If

to check make sure that is the rule is run on anything other than a .idw, the user gets my message box.

And if my assumption is correct, is there a workaround?

 

Thanks in advance!

 

 

 

0 Likes
796 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor

How did you define oDocType?

Is it:

 

 

Dim oDocType As DocumentTypeEnum = ThisApplication.ActiveDocumentType

 

 

or is it:

 

 

Dim oDocType As DocumentTypeEnum = ThisDoc.Document.DocumentType

 

 

 

ThisDoc, ThisDrawing, ThisAssembly, ThisBOM, are all Rule Objects, and by default always refer to the document (or object) that the current rule is within (when it is a local rule).  However, ThisApplication.ActiveDocument can be a different document than ThisDoc.Document, depending on the situation.  You can Activate documents within your rule using a Sub, so that they become the 'active' document, or 'active' sheet, etc, even if they were not the active object when the rule began.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

Anonymous
Not applicable
Dim oDocType As Inventor.DocumentTypeEnum = oDoc.DocumentType

 "You can Activate documents within your rule using a Sub, so that they become the 'active' document, or 'active' sheet, etc, even if they were not the active object when the rule began."


i'm not quite seaweed green anymore i'm only like moss green as far as ilogic goes!! 😁  Where might i find an example of this?

 

0 Likes
Message 4 of 6

WCrihfield
Mentor
Mentor

Here is one fairly simple rule you can use to test this behavior.

To test this rule:

  • Create a new test drawing and save it somewhere within your project's workspace.
  • Create a new local rule within that drawing, then copy and paste the below iLogic code into it, and save it
  • This rule will create a new drawing document to test with
    • Ensure the path and file name specified within the rule for the new document are OK, before running.
    • You will need to change the drawing template file name to match yours

It will leave both the original and the new drawings open when it finishes (by design), but you can delete them later if you want to.  I've included a lot of comments and messages to guide you through what's going on.

Here's the code:

 

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
	MsgBox("A Drawing Document must be active for this rule (" & iLogicVb.RuleName & ") to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oDDoc As DrawingDocument = ThisDrawing.Document
'if this rule is a local rule (saved within the document), both of the following will always refer to the local document
'ThisDoc.Document & ThisDrawing.Document

'right now both the 'Active' document and ThisDrawing.Document are both the same document
'because it was active in Inventor when I started this rule, and this rule is saved within this document
MsgBox("ThisApplication.ActiveDocument.FullFileName = " & ThisApplication.ActiveDocument.FullFileName & vbCrLf & vbCrLf & _
"ThisDrawing.Document.FullFileName = " & ThisDrawing.Document.FullFileName, vbOKOnly + vbInformation, " ")

'now, lets create a new drawing, then save it, then check the status of both statements again
'it will fail if the document has not been saved yet, because it will not have a FullFileName yet.
'if opened visibly (True), then that drawing will become the 'active' document
'if opened invisibly (False), then the first drawing will remain the active document
'<<<< CHANGE THIS FILE PATH IF YOU NEED TO >>>>
Dim oPath As String = System.IO.Path.GetDirectoryName(oDDoc.FullFileName) & "\"
'<<<< CHANGE THIS TO THE NAME OF YOUR DRAWING TEMPLATE FILE >>>>
Dim oTemplate As String = ThisApplication.DesignProjectManager.ActiveDesignProject.TemplatesPath & "\A-Size Standard Drawing.idw"
Dim oOtherDrg As DrawingDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject, oTemplate, True) 'True = visible
oOtherDrg.SaveAs(oPath & "Testing123.idw", False) 'True = SaveCopyAs / False = normal SaveAs

MsgBox("Just created & saved a new drawing document.  Now lets try this again." & vbCrLf & _
"ThisApplication.ActiveDocument.FullFileName = " & ThisApplication.ActiveDocument.FullFileName & vbCrLf & vbCrLf & _
"ThisDrawing.Document.FullFileName = " & ThisDrawing.Document.FullFileName, vbOKOnly + vbInformation, " ")


'here's where the magic usually happens
'all Inventor document objects have an Activate sub routine, that will make them the 'active' document
oDDoc.Activate
'now let's test those two document references again
MsgBox("Just 'Activated' the original drawing again.  Now lets try this again." & vbCrLf & _
"ThisApplication.ActiveDocument.FullFileName = " & ThisApplication.ActiveDocument.FullFileName & vbCrLf & vbCrLf & _
"ThisDrawing.Document.FullFileName = " & ThisDrawing.Document.FullFileName, vbOKOnly + vbInformation, " ")

 

   You could also Open another drawing document in the middle, instead of Creating a new one, and it will have the same effect.  When the 'other' document is either Opened or Created visibly, that document will become the 'active' document.  If Opened or Created invisibly, whatever document was active before, will remain active.

   Now when working from an external iLogic rule, instead of a local one, it becomes even more fuzzy.  I believe whichever document was 'active' when the rule starts, will remain the focus of all the (ThisDoc.Document, ThisDrawing.Document, ThisAssembly.Document) type document references, while ThisApplication.ActiveDocument will always refer to which ever document was 'active' when and where that reference is used.  For example, if you define a document variable at the start of your rule and set it to ThisApplication.ActiveDocument, but use either the 'Activate' sub or another technique that causes other documents to become 'active' later in the rule, then you use the original document variable again after that point, that variable will still refer to which ever document was active at the beginning of the rule, not necessarily the document that is currently 'active' at that later position in the rule.

   This is why I usually prefer to create a document variable as specifically as possible and stick with that variable throughout my code, instead of using additional references like ThisDoc.Document or ThisApplication.ActiveDocument any more times down through my code, if I don't have to.  Using these types of references multiple times throughout the code just complicates things and opens it up for potential errors.

 

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

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 6

Anonymous
Not applicable
'if opened visibly (True), then that drawing will become the 'active' document
'if opened invisibly (False), then the first drawing will remain the active document

What is "opened visibly"?

0 Likes
Message 6 of 6

WCrihfield
Mentor
Mentor

Sorry for the delayed response.  I was on vacation for the past week and a half.

When you use the following technique to open Inventor documents, you can specify if you want to open the target document "Visibly" or not.

Dim oDoc As DrawingDocument = ThisApplication.Documents.Open("C:\Temp\Test.idw",True)

If you specify True, it will open the target document Visibly.  And if you specify False, it will open the target document without it being visible (or in the background).  Opening documents in the background is often preferred, because it uses less system resources and usually makes the rule process faster.  Opening documents is often needed to load the document into memory, so you can work with its iProperties (and some other things).  But you should always remember to either close each document immediately after your done using it, or use a cleanup line of code near the end of your rule to close all 'unreferenced' documents, similar to this:

ThisApplication.Documents.CloseAll(True)

where 'True' means to only close all the 'unreferenced' documents.  If you specify False, then it acts as you would expect, and closes all documents.

An unreferenced document, is any document which is partially or fully loaded in memory, but has not been opened visibly, and nothing at that point of your code is still referencing it or any thing within it.

 

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

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes