iLogic script "dirtying" drawing

iLogic script "dirtying" drawing

pball
Mentor Mentor
1,068 Views
7 Replies
Message 1 of 8

iLogic script "dirtying" drawing

pball
Mentor
Mentor

I've been adding some iLogic to our drawing templates and I've noticed an unwanted side effect. I have the rule run after open in case the model properties were changed. If nothing was updated and the code makes no changes the drawing will still prompt to be saved when you close it. I noticed two different things in my code that are causing the "changes" which prompts the save but I cannot figure out how to eliminate it.

 

My code does two main things. The first is it checks if the model has it's own iLogic rule for updating the material, if not this code will update the material saved in a custom iProperty. The second operation checks the length of two title block fields and changes the text box width scale if the text is too long.

 

The call to edit the title block and exit the title block show up in the undo history, which means the file was "changed" and will prompt to save. I thought about adding some code to undo those operations if nothing was changed but I'm pretty sure even if you undo any operations the file will still prompt to save.

 

Anyone know a way to get around dirtying the drawing while still being able to check if the values are up to date? I tried some code down below to access the title block in a different way that might not dirty the file, but it seems like it's not possible to change values when accessing the title block with that method.

 

Dim docFile As Document
If ThisDoc.ModelDocument IsNot Nothing Then
	docFile = ThisDoc.ModelDocument
Else
	Return
End If

'Check if part has iLogic rule named Material, if not this will update Matrl iProp even if Matrl already exists
Dim rules as IEnumerable = iLogicVb.Automation.Rules(docFile)
Dim rule As Object
If (rules IsNot Nothing) Then
	For Each rule In rules
		If (rule.Name = "Material") Then MatFound = True
	Next rule
End If

'define the property set
customPropertySet = docFile.PropertySets.Item("Inventor User Defined Properties")
designPropertySet = docFile.PropertySets.Item("Design Tracking Properties")

'look for the custom propety and add it if not found 
Try
	If (customPropertySet.Item("MATRL").Value = "" Or MatFound <> True) Then
		customPropertySet.Item("MATRL").Value = designPropertySet.Item("Material").Value.toupper
	End If
Catch
	customPropertySet.Add("", "MATRL")
	customPropertySet.Item("MATRL").Value = designPropertySet.Item("Material").Value.toupper
End Try

'Titleblock text fiting
Dim odrawdoc As DrawingDocument
odrawdoc = ThisApplication.ActiveDocument

Desc = designPropertySet.Item("Description").Value
Matrl = customPropertySet.Item("MATRL").Value
   
Dim oTitleBlockDef As TitleBlockDefinition
oTitleBlockDef = odrawdoc.ActiveSheet.TitleBlock.Definition

Dim oSketch As DrawingSketch
Call oTitleBlockDef.Edit(oSketch)

Dim count As Integer
count = oSketch.TextBoxes.count
While (count > 0)
    If (oSketch.TextBoxes.Item(count).Text = "<DESCRIPTION>") Then
		If (oSketch.TextBoxes.Item(count).WidthScale <> Iif(Len(Desc) < 30, 1, 29 / (Len(Desc) - 1))) Then
		    oSketch.TextBoxes.Item(count).WidthScale = Iif(Len(Desc) < 30, 1, 29 / (Len(Desc) - 1))
		End If
	Else If (oSketch.TextBoxes.Item(count).Text = "<MATRL>") Then
		If (oSketch.TextBoxes.Item(count).WidthScale <> Iif(Len(Matrl) < 26, 1, 25 / (Len(Matrl) - 1))) Then
        	oSketch.TextBoxes.Item(count).WidthScale = Iif(Len(Matrl) < 26, 1, 25 / (Len(Matrl) - 1))
		End If
    End If
    count = count - 1
End While
    
Call oTitleBlockDef.ExitEdit

iLogicVb.UpdateWhenDone = True

Failed test

    Dim odrawdoc As DrawingDocument
    Set odrawdoc = ThisApplication.ActiveDocument

    Dim invDoc2 As Document
    Set invDoc2 = ThisApplication.Documents.ItemByName(odrawdoc.ReferencedDocuments.Item(odrawdoc.ReferencedDocuments.count).FullDocumentName)
    Dim Desc As String
    Desc = invDoc2.PropertySets.Item("Design Tracking Properties").Item("Description").Value
    Dim Matrl As String
    Matrl = invDoc2.PropertySets.Item("Inventor User Defined Properties").Item("MATRL").Value

Dim oTitleBlock As TitleBlock
Dim oTextBox As TextBox
Dim oSheet As Sheet

For Each oSheet In ThisApplication.ActiveDocument.Sheets
    Set oTitleBlock = oSheet.TitleBlock
    For Each oTextBox In oTitleBlock.Definition.Sketch.TextBoxes

        If (oTextBox.Text = "<DESCRIPTION>") Then
            If (oTextBox.WidthScale <> IIf(Len(Desc) < 30, 1, 29 / (Len(Desc) - 1))) Then
                Debug.Print oTextBox.WidthScale
            End If
        ElseIf (oTextBox.Text = "<MATRL>") Then
            If (oTextBox.WidthScale <> IIf(Len(Matrl) < 26, 1, 25 / (Len(Matrl) - 1))) Then
                Debug.Print oTextBox.WidthScale
'This line errors, it seems it's not possible to change things accessing the title block this way oTextBox.WidthScale = 1 End If End If Next Next
Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
1,069 Views
7 Replies
Replies (7)
Message 2 of 8

nmunro
Collaborator
Collaborator

Have a look at Inventor's transactions. You could wrap your title block edit in one and abort the transaction if no changes are required. Have a look at the API help, they are pretty easy to implement.

 

        


https://c3mcad.com

0 Likes
Message 3 of 8

pball
Mentor
Mentor
I'm using the transaction manager for wrapping a few operations into a single undo step in some other script. I did not know that item could be aborted. Thanks I'll be trying that out shortly.
Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 4 of 8

pball
Mentor
Mentor

Well my edit time expired while I tried updating my previous post, so yay double posting.

Well I tried wrapping the whole script in a single transaction and it's aborting since there is nothing being updated and the undo has nothing listed but opening the document. Sadly the document is still dirty. So it seems even running a script is dirtying the drawing. I also did a sanity check by disabling the after open trigger and the document is not dirty after opening and it is the script that is dirtying it. I think I'll be making an inquiry directly to Autodesk and see if they have any input on the matter.

I would personally think if nothing happens to the drawing it shouldn't be dirty, but who knows what actually dirties a file.

 

Edit:

I tried running an Ilogic rule which only started and aborted a transaction and nothing else, that still dirtied the file.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 5 of 8

Jef_E
Collaborator
Collaborator

Don't know if this could be of any help.

 

http://adndevblog.typepad.com/manufacturing/2013/08/inventor-dirty-flag-of-inventor-document.html



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
Message 6 of 8

pball
Mentor
Mentor
I don't believe that setting really applies. If I'm understanding that correctly, enabling that will cause Inventor to prompt for a save more than with it disabled.
Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 7 of 8

MjDeck
Autodesk
Autodesk

If you haven't already found a solution, please see the attached rule. It has a loop to go over the title block sketch twice. The first time, it doesn't open the sketch for editing but just checks to see if anything needs to be changed. If changes are required, then it will open the sketch for edit and actually make the changes. It's a bit of extra code, but it should work.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 8 of 8

pball
Mentor
Mentor

Thanks for posting that, I like seeing other methods of doing things. I actually found another solution using the transaction abort I learned from this thread and setting the dirty flag which I saw elsewhere. I don't remember when I put it all together but I apparently forgot about this thread by then. The general code is below with some pseudo code in the middle.

 

This checks if the document is dirty when the script starts, starts a transaction to wrap the script in a single undo operation, and sets a variable to track changes. In the body of the script the user must update the variable "updated" if any changes are made. At the end if no changes were made the transaction is canceled and if the document was not dirty the dirty flag is set to false in case it was set by the script. If changes were made the transaction is ended and the document updated.

 

Dim isDirty As Boolean = ThisApplication.activeDocument.Dirty
Dim oTrans As Transaction  = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Drawing Update Stuff")
Dim updated As Boolean = False

if (something <> somethingelse) then
  'update stuff
  updated = True
end if

'If changes were made, update and exit, else undo everything and try to exit cleanly
If (updated = False) Then 
	oTrans.abort
	If Not isDirty Then ThisApplication.activeDocument.Dirty = False
Else
	oTrans.End
	iLogicVb.UpdateWhenDone = True
End If
Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes