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

(Not an Autodesk Employee)