Replace title block from template & update date / user

Replace title block from template & update date / user

j.wolbers-NoTech
Enthusiast Enthusiast
4,387 Views
21 Replies
Message 1 of 22

Replace title block from template & update date / user

j.wolbers-NoTech
Enthusiast
Enthusiast

Hi,

 

 

I actually have three small problems.

 

Recently we changed the title block in our drawing template. So when we create new drawings, this goes well. But when we copy an existing drawing in Vault, you keep the old title block. How can I ensure that this title block is automatically replaced (by the title block from the template) using Ilogic?

 

Furthermore, our drawings currently have the '' creation date '' in the title block. This is fine of course, but when we copy a drawing you always keep the old date of the original assembly. How do I make sure this is updated to the last '' save '' date or something like that?

 

Furthermore, I would also like to see the property <DESIGNER> updated when a part is copied from the vault database? Is this possible? Now many drawings remain in my own name when new colleagues copy files. This is because I previously worked alone.

 

I hope someone can help me with this.

 

 

Kinds regards,
Jeffrey

0 Likes
Accepted solutions (3)
4,388 Views
21 Replies
Replies (21)
Message 2 of 22

pball
Mentor
Mentor

If the title block has the same name in the old and new drawings it is quite easy to replace, if they aren't the same it's still easy I just haven't actually done that though.

 

 

        'Get current drawing
        Dim oDrawDoc As DrawingDocument
        oDrawDoc = ThisApplication.ActiveDocument

        'Get template titleblock
        Dim oDrawTBlockDef As TitleBlockDefinition
        Dim oTemplateTBlockDef As TitleBlockDefinition
        Dim oTemplateBorderDef As BorderDefinition
        Dim oTemplateDoc As DrawingDocument

        oDrawTBlockDef = oDrawDoc.ActiveSheet.TitleBlock.Definition

        If (oDrawTBlockDef.Name = "Titleblock A") Then
            oTemplateDoc = ThisApplication.Documents.Open("Z:\Templates\Drawing A Template.idw", False)
            oTemplateTBlockDef = oTemplateDoc.TitleBlockDefinitions.Item("Titleblock A")
            Call oTemplateTBlockDef.CopyTo(oDrawDoc, True)
        End If
        oTemplateDoc.Close()

 

  If the currently open drawing has Titleblock A on the current sheet it will replace that title block with the version from the template file referenced.

 

Updating the two properties is a simple matter of using iProperties which you should be able to find many examples of on the forum.

 

If you're planning on doing this as iLogic you'll just need to decide if you want this to run automatically or manually. It'd be possible to have it triggered when you open a document and it would always be up to date, but the creation date and designer would be updated every time also. I personally made an addin with a manual button that updates the title block and an iProperties editor where a user can correct the date and names for a drawing.

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

WCrihfield
Mentor
Mentor

Would this work for you?

(You would have to edit the names of the Template file and the target Border Definition and Title Block Definition in the Template file, to match your situation before it will work properly for you.)

 

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Drawing Documents. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If

	Dim oDDoc As DrawingDocument = ThisDrawing.Document
	ReplaceTBandBorder(oDDoc)
	oDDoc.PropertySets.Item("Design Tracking Properties").Item("Creation Time").Value = System.IO.File.GetLastWriteTime(oDDoc.FullFileName)
	oDDoc.PropertySets.Item("Design Tracking Properties").Item("Designer").Value = ThisApplication.GeneralOptions.UserName
End Sub

Sub ReplaceTBandBorder(ByRef oDrDoc As DrawingDocument)
	Dim oSheets As Sheets = oDrDoc.Sheets
	Dim oSheet As Sheet
	Dim oTB As TitleBlock
	'Delete TitleBlock & Border from each sheet
	For Each oSheet In oSheets
		oSheet.Activate
		oSheet.TitleBlock.Delete
		oSheet.Border.Delete
	Next
	'Re-Activate the first sheet again
	oSheets.Item(1).Activate
	'Attempt to delete all TitleBlockDefinitions
	Dim oTBDefs As TitleBlockDefinitions = oDrDoc.TitleBlockDefinitions
	For Each oTBDef As TitleBlockDefinition In oTBDefs
		If oTBDef.IsReferenced = False Then
			oTBDef.Delete
		ElseIf oTBDef.IsReferenced = True Then
			MsgBox("Title Block Def Named '" & oTBDef.Name & "' is referenced, and will not be deleted.",vbOKOnly+vbInformation, "CAN'T BE DELETED")
		End If
	Next
	'Attempt to delete all BorderDefinitions
	Dim oBDefs As BorderDefinitions = oDrDoc.BorderDefinitions
	For Each oBDef As BorderDefinition In oBDefs
		If oBDef.IsReferenced = False Then
			oBDef.Delete
		ElseIf oBDef.IsReferenced = True Then
			MsgBox("Border Def. Named '" & oBDef.Name & "' is referenced, and will not be deleted.",vbOKOnly+vbInformation, "CAN'T BE DELETED")
		End If
	Next
	'Specify the Drawing Template document to copy from
	Dim oTFN As String = "S:\Engineering\Templates\A-Size Standard Drawing.idw"
	Dim oTemplate As DrawingDocument = ThisApplication.Documents.Open(oTFN, False)
	Dim oNewTBDef As TitleBlockDefinition
	Dim oNewBDef As BorderDefinition
	'Copy BorderDefinition from Template to this drawing, and replace if already exists
	Dim oTemBDefs As BorderDefinitions = oTemplate.BorderDefinitions
	For Each oTemBDef As BorderDefinition In oTemBDefs
		If oTemBDef.Name.Contains("A-SIZE LANDSCAPE .25 OFF ALL EDGES") Then
			oNewBDef = oTemBDef.CopyTo(oDrDoc, True)
		End If
	Next
	'Copy TitleBlockDefinition from Template to this drawing, and replace if already exists
	Dim oTemTBDefs As TitleBlockDefinitions = oTemplate.TitleBlockDefinitions
	For Each oTemTBDef As TitleBlockDefinition In oTemTBDefs
		If oTemTBDef.Name = "A-SIZE FULL WIDTH BOTTOM" Then
			oNewTBDef = oTemTBDef.CopyTo(oDrDoc, True)
		End If
	Next
	'Place the new Border & Title Block on all sheets
	For Each oSheet In oSheets
		oSheet.Activate
		oSheet.AddBorder(oNewBDef)
		oSheet.AddTitleBlock(oNewTBDef, TitleBlockLocationEnum.kBottomRightPosition)
	Next
	oTemplate.Close(True)
	oTemplate.ReleaseReference
	'Re-Activate the first sheet again
	oSheets.Item(1).Activate
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 22

j.wolbers-NoTech
Enthusiast
Enthusiast

Hi @WCrihfield 

 

Thanks for your Ilogic code.

 

If I understand correctly, this code looks at the design properties of the drawing itself and not the Iproperties of the 3D model. If so, that's okay!

 

I don't think it would be a problem if I change "Creation Time" to "Creation Date", do you?

 

I just don't see anywhere to enter my Title Block name (sorry for my inexperience with Ilogic). In principle, the border does not need to be replaced, but it is allowed.

 

Kinds regards,

Jeffrey

0 Likes
Message 5 of 22

WCrihfield
Mentor
Mentor

Yes, my code is changing the creation date of the drawing document to match the date of the last time the drawing file was written to.  If you need this scenario changed, that would be easy enough.  Do you want change the creation date of both the model document and the drawing document, just the model, or just the drawing?  Do you want the creation date within the drawing to directly reference that data from the model document's iProperties (or last file write date), instead of from the drawings own creation/last write date?  The "Creation Time" iProperty I specified in my last code is the same one shown within the Project tab of the standard iProperties dialog.  Is your title block using that property, or is it using a different Custom iProperty?

Just let me know the exact scenario you want and I'll make it happen for you, even if you want this code to effect both the drawing and the model documents.

 

Within the last code I posted the following line is where I specified which drawing template file I wanted it to open:

Dim oTFN As String = "S:\Engineering\Templates\A-Size Standard Drawing.idw"

You should change that string after the =, to match the path and file name of the drawing template file you want it to reference your newer title block from.

Also, if you don't need to replace the border, you can delete all that code to simplify the solution for you.

Also, in my last posted code, I specified the name of the Title Block Definition I wanted to copy to my new drawing within this If...Then statement:

	For Each oTemTBDef As TitleBlockDefinition In oTemTBDefs
		If oTemTBDef.Name = "A-SIZE FULL WIDTH BOTTOM" Then
			oNewTBDef = oTemTBDef.CopyTo(oDrDoc, True)
		End If
	Next

You will need to change "A-SIZE FULL WIDTH BOTTOM" to the name of the title block definition you want within your drawing template file.

 

Here is an updated code, without the border stuff, and with more iProperty options (some commented out, so you can delete the ones you don't need).

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Drawing Documents. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If

	Dim oDDoc As DrawingDocument = ThisDrawing.Document
	ReplaceTB(oDDoc)
	Dim oMDoc As Document = ThisDrawing.ModelDocument
'	'drawing only
'	oDDoc.PropertySets.Item("Design Tracking Properties").Item("Creation Time").Value = System.IO.File.GetLastWriteTime(oDDoc.FullFileName)
'	oDDoc.PropertySets.Item("Design Tracking Properties").Item("Designer").Value = ThisApplication.GeneralOptions.UserName
'	'Model only
'	oMDoc.PropertySets.Item("Design Tracking Properties").Item("Creation Time").Value = System.IO.File.GetLastWriteTime(oMDoc.FullFileName)
'	oMDoc.PropertySets.Item("Design Tracking Properties").Item("Designer").Value = ThisApplication.GeneralOptions.UserName
	'Model data to drawing only
	oDDoc.PropertySets.Item("Design Tracking Properties").Item("Creation Time").Value = System.IO.File.GetLastWriteTime(oMDoc.FullFileName)
	oDDoc.PropertySets.Item("Design Tracking Properties").Item("Designer").Value = oMDoc.PropertySets.Item("Design Tracking Properties").Item("Designer").Value
End Sub

Sub ReplaceTB(ByRef oDrDoc As DrawingDocument)
	Dim oSheets As Sheets = oDrDoc.Sheets
	Dim oSheet As Sheet
	Dim oTB As TitleBlock
	'Delete TitleBlock & Border from each sheet
	For Each oSheet In oSheets
		oSheet.Activate
		oSheet.TitleBlock.Delete
	Next
	'Re-Activate the first sheet again
	oSheets.Item(1).Activate
	'Attempt to delete all TitleBlockDefinitions
	Dim oTBDefs As TitleBlockDefinitions = oDrDoc.TitleBlockDefinitions
	For Each oTBDef As TitleBlockDefinition In oTBDefs
		If oTBDef.IsReferenced = False Then
			oTBDef.Delete
		ElseIf oTBDef.IsReferenced = True Then
			MsgBox("Title Block Def Named '" & oTBDef.Name & "' is referenced, and will not be deleted.",vbOKOnly+vbInformation, "CAN'T BE DELETED")
		End If
	Next
	'Specify the Drawing Template document to copy from
	Dim oTFN As String = "S:\Engineering\Templates\A-Size Standard Drawing.idw"
	Dim oTemplate As DrawingDocument = ThisApplication.Documents.Open(oTFN, False)
	Dim oNewTBDef As TitleBlockDefinition
	'Copy TitleBlockDefinition from Template to this drawing, and replace if already exists
	Dim oTemTBDefs As TitleBlockDefinitions = oTemplate.TitleBlockDefinitions
	For Each oTemTBDef As TitleBlockDefinition In oTemTBDefs
		If oTemTBDef.Name = "A-SIZE FULL WIDTH BOTTOM" Then
			oNewTBDef = oTemTBDef.CopyTo(oDrDoc, True)
		End If
	Next
	'Place the new Border & Title Block on all sheets
	For Each oSheet In oSheets
		oSheet.Activate
		oSheet.AddTitleBlock(oNewTBDef, TitleBlockLocationEnum.kBottomRightPosition)
	Next
	oTemplate.Close(True)
	oTemplate.ReleaseReference
	'Re-Activate the first sheet again
	oSheets.Item(1).Activate
End Sub

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

Vote For My IDEAS 💡and Explore My CONTRIBUTIONS

Inventor 2020 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 22

j.wolbers-NoTech
Enthusiast
Enthusiast

Hi @WCrihfield 

 

I would like the following fields in drawing Iproperties to be updated.

- Designer
- Creation Date

 

I've removed the parts I don't need from your code (see code below).

However, when I test the code I get the following message.

 

picture.PNG

If I click further, I get the message below

 

error.PNG

After this error message, the title block has strangely been removed ...

The properties have not yet been updated either, but this is probably because the code is not running completely.

 

iLogic code with drawing part only

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Drawing Documents. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If

	Dim oDDoc As DrawingDocument = ThisDrawing.Document
	ReplaceTB(oDDoc)
	Dim oMDoc As Document = ThisDrawing.ModelDocument
'	'drawing only
	oDDoc.PropertySets.Item("Design Tracking Properties").Item("Creation Time").Value = System.IO.File.GetLastWriteTime(oDDoc.FullFileName)
	oDDoc.PropertySets.Item("Design Tracking Properties").Item("Designer").Value = ThisApplication.GeneralOptions.UserName
End Sub

Sub ReplaceTB(ByRef oDrDoc As DrawingDocument)
	Dim oSheets As Sheets = oDrDoc.Sheets
	Dim oSheet As Sheet
	Dim oTB As TitleBlock
	'Delete TitleBlock & Border from each sheet
	For Each oSheet In oSheets
		oSheet.Activate
		oSheet.TitleBlock.Delete
	Next
	'Re-Activate the first sheet again
	oSheets.Item(1).Activate
	'Attempt to delete all TitleBlockDefinitions
	Dim oTBDefs As TitleBlockDefinitions = oDrDoc.TitleBlockDefinitions
	For Each oTBDef As TitleBlockDefinition In oTBDefs
		If oTBDef.IsReferenced = False Then
			oTBDef.Delete
		ElseIf oTBDef.IsReferenced = True Then
			MsgBox("Title Block Def Named '" & oTBDef.Name & "' is referenced, and will not be deleted.",vbOKOnly+vbInformation, "CAN'T BE DELETED")
		End If
	Next
	'Specify the Drawing Template document to copy from
	Dim oTFN As String = "C:\WF\System Configurations\2020\Inventor\Templates"
	Dim oTemplate As DrawingDocument = ThisApplication.Documents.Open(oTFN, False)
	Dim oNewTBDef As TitleBlockDefinition
	'Copy TitleBlockDefinition from Template to this drawing, and replace if already exists
	Dim oTemTBDefs As TitleBlockDefinitions = oTemplate.TitleBlockDefinitions
	For Each oTemTBDef As TitleBlockDefinition In oTemTBDefs
		If oTemTBDef.Name = "Notech" Then
			oNewTBDef = oTemTBDef.CopyTo(oDrDoc, True)
		End If
	Next
	'Place the new Border & Title Block on all sheets
	For Each oSheet In oSheets
		oSheet.Activate
		oSheet.AddTitleBlock(oNewTBDef, TitleBlockLocationEnum.kBottomRightPosition)
	Next
	oTemplate.Close(True)
	oTemplate.ReleaseReference
	'Re-Activate the first sheet again
	oSheets.Item(1).Activate
End Sub

 

 

Hopefully you can help me with this.

 

Kinds regards,

Jeffrey

 

0 Likes
Message 7 of 22

WCrihfield
Mentor
Mentor

I think I see where the problem is within your last posted code.

The following line:

Dim oTFN As String = "C:\WF\System Configurations\2020\Inventor\Templates"

is not complete.  It isn't specifying a drawing template file.  See my earlier example code.  It looks like you need to add some more to the end of that string.  Maybe something like this:

Dim oTFN As String = "C:\WF\System Configurations\2020\Inventor\Templates\Standard Drawing.idw"

 If you fix this line, it will likely make the rule run OK.  But if you want, you could add a couple of Try...Catch...End Try statements in there, to help avoid potential errors.  Or even enclose most if not all of the code within an InteractionEvent, so it is all seen as one action, and then the whole thing can be undone in one click.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 22

j.wolbers-NoTech
Enthusiast
Enthusiast

Hi @WCrihfield

 

I had indeed not entered the name of the template correctly.

After I have adjusted this the following happens:

 

After starting the code I first get the message below

 

1.PNG

 

If I continue, the title block will be replaced!
But then I receive the following error ..

 

2.PNG3.PNG

 

If I continue the designer will be updated but the date will not change unfortunately.

 

Kinds regards,

Jeffrey

 

0 Likes
Message 9 of 22

j.wolbers-NoTech
Enthusiast
Enthusiast

Hi @WCrihfield 

 

Sorry for the double reply but the Autodesk forum gave all kinds of errors while posting.

 

I had indeed forgotten to adjust that properly.

However, the message below continues to emerge.

1.PNG

If I continue, the title block will be replaced!
Then the following message comes up ...

 

2.PNG3.PNG

 

At the end the '' Designer '' has been updated but the date unfortunately not.

 

Kinds regards,

Jeffrey

0 Likes
Message 10 of 22

WCrihfield
Mentor
Mentor
Accepted solution

   Well, the error message seems to be suggesting that it doesn't like the following line in the code:

 

 

oTemplate.ReleaseReference

 

 

 If this line is causing the error, you can just delete it.  It is a memory clean-up tool, that might not even be necessary in this situation.  That "ReleaseReference" is used to release Inventor's reference to a document that has been opened invisibly (in the background), which frees up that system memory, and releases any sort of "write lock" that your system might have put on that file as a result of Inventor actively referencing it.

However, the oTemplate.Close line could very well be all we need in this short code.

You can try commenting that line out, then see what happens, and if that doesn't work, try commenting the Close line instead, and see if that works.  It likely just doesn't like both of them at the same time.

    There is another line I often use in larger codes, where I may be opening a large number of documents invisibly, that is also designed to clean-up all these invisibly opened documents, that are no longer being referenced, or needed.

 

 

ThisApplication.Documents.CloseAll(True)

 

 

 The True is important in this line, because that's what tells it to only close all the unreferenced documents, instead of all regular visible documents that are open.

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 11 of 22

j.wolbers-NoTech
Enthusiast
Enthusiast

@WCrihfield 

 

I have tested both options.
The notifications ''Can't be deleted'' still keeps popping up.

The error code ''HRESULT...'' no longer appears so that's good!

 

Now I have found the link below via google ..

Maybe this has something to do with it?

 

https://adndevblog.typepad.com/manufacturing/2012/06/titleblockdefinition-cannot-be-deleted-even-tho... 

 

Kinds regards,

Jeffrey

 

Message 12 of 22

WCrihfield
Mentor
Mentor
Accepted solution

Yes. Sorry, I didn't elaborate on that point.  That's why I put that check in there.  You can comment out or delete that message if you want.  It's just there to let you know what's going on.

Is it working OK other than that, though?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 13 of 22

j.wolbers-NoTech
Enthusiast
Enthusiast

@WCrihfield 

 

Thank you.

Works perfectly!

 

Kinds regards,

Jeffrey

Message 14 of 22

j.wolbers-NoTech
Enthusiast
Enthusiast

Hi @WCrihfield 

 

Today I found out there is another error popping up. This only happens when saving a new drawing (so not a copy of an existing drawing).

 

Do you have an idea how this can be solved?

 

1.PNG2.PNG

 

Kinds regards,

Jeffrey

Message 15 of 22

WCrihfield
Mentor
Mentor

The error message indicates it is having trouble accessing a PartsList object.  Perhaps your code needs to check to see if the PartsLists.Count > 0 (or similar), before trying to access the individual PartsList.  Or if it is sometimes possible the target PartsList may not exists yet, and you want the code to create it, you could use a Try...Catch...End Try block of code, where you try to access the PartsList in the Try portion, and if that fails, you can use the Catch portion to create it.  Often times you can avoid using the Try...Catch...End Try block by using a series of If...Then and/or Loop blocks, but that's up to your coding style.  It would be difficult to be more specific with a solution without seeing the underlying code and understanding what files may be open at the time, and what objects may or may not exist already within those documents.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 16 of 22

j.wolbers-NoTech
Enthusiast
Enthusiast

@WCrihfield 

 

Sorry, I added the wrong images.
I had already solved this problem myself.

This concerns the error message below:

 

1.PNG2.PNG 

 

Is it possible that the title block will only be replaced on drawings older than a certain date (before it is updated to the last writing date of course)? Because in principle it is nonsense that the title block is replaced with a new drawing.

 

Regards,

Jeffrey

Message 17 of 22

WCrihfield
Mentor
Mentor
Accepted solution

Ah...I see it now.  I didn't account for the conversion from a DataTime object to either a Date object or a String object, that only includes the date data without the time data.  My mistake.

Here is some educational example code you can play with that should clear things up.

 

Dim oDoc As Document = ThisDoc.Document
Dim oLastWriteDateAndTime As DateTime = IO.File.GetLastWriteTime(oDoc.FullFileName)
Dim oLastWriteDate As Date = IO.File.GetLastWriteTime(oDoc.FullFileName).Date
Dim oLastWriteDateString As String = IO.File.GetLastWriteTime(oDoc.FullFileName).ToShortDateString
MsgBox("oLastWriteDateAndTime As DateTime = " & oLastWriteDateAndTime)
MsgBox("oLastWriteDate As Date = " & oLastWriteDate)
MsgBox("oLastWriteDateString As String = " & oLastWriteDateString)

'Set the oldest allowable date to leave the title block alone.
Dim oOldDate As Date = DateValue("03/05/2016")  ' this will often accept many formats that can be understood as representing a date.
Dim oFutureDate As Date = DateValue("12/29/2020")

'Comparing dates
MsgBox("Older date compared to newer date = " & oLastWriteDate.CompareTo(oOldDate)) 'should equal 1, because oLastWriteDate is later than oOldDate
MsgBox("When dates are equal = " & Today.CompareTo(Today)) 'should equal 0, because they are equal (Today is a DateAndTime property)
MsgBox("Older or current date compared to future date = " & oLastWriteDate.CompareTo(oFutureDate)) 'should equal -1, because oLastWriteDate is earlier than oFutureDate

 

So yes.  You could set your code up do only replace the title block if the Creation Date is older than a certain specified date.

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)

Message 18 of 22

j.wolbers-NoTech
Enthusiast
Enthusiast

Hi @WCrihfield 


That's how the code eventually turned out.
Everything works so that's great.

However, there is one more thing that I do not understand... When I right click on '' open newest drawing '' in Inventor, I get an error message (see image below). I only get this message once per drawing. When I try to retrieve the drawing again later, this error message does not appear.

When I click on '' open drawing from vault '' in the vault browser add-in, I don't get an error message.

 

That is strange, isn't it?

 

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Drawing Documents. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If

	Dim oDDoc As DrawingDocument = ThisDrawing.Document
	
	
	
	Dim oDoc As Document = ThisDoc.Document

Dim oLastCreationDate as Date = oDDoc.PropertySets.Item("Design Tracking Properties").Item("Creation Time").value
'msgbox(oLastCreationDate)
'Set the oldest allowable date to leave the title block alone.
Dim oOldDate As Date = DateValue("19/10/2020")  ' this will often accept many formats that can be understood as representing a date.
Dim lValue as string = Format(Now, "dd/MM/yy")

'msgbox(lvalue)

'Comparing dates
'MsgBox("Older date compared to newer date = " & oLastCreationDate.CompareTo(oOldDate)) 'should equal 1, because oLastCreationDate is later than oOldDate


if oLastCreationDate.CompareTo(oOldDate) = -1 then

	ReplaceTB(oDDoc)
	Dim oMDoc As Document = ThisDrawing.ModelDocument
'	'drawing only
	oDDoc.PropertySets.Item("Design Tracking Properties").Item("Creation Time").Value = lValue
	oDDoc.PropertySets.Item("Design Tracking Properties").Item("Designer").Value = ThisApplication.GeneralOptions.UserName
end if


end Sub

function ReplaceTB(ByRef oDrDoc As DrawingDocument)
	Dim oSheets As Sheets = oDrDoc.Sheets
	Dim oSheet As Sheet
	Dim oTB As TitleBlock
	'Delete TitleBlock & Border from each sheet
	For Each oSheet In oSheets
		oSheet.Activate
		oSheet.TitleBlock.Delete
	Next
	'Re-Activate the first sheet again
	oSheets.Item(1).Activate
	'Attempt to delete all TitleBlockDefinitions
	Dim oTBDefs As TitleBlockDefinitions = oDrDoc.TitleBlockDefinitions
	For Each oTBDef As TitleBlockDefinition In oTBDefs
		If oTBDef.IsReferenced = False Then
			oTBDef.Delete
		'ElseIf oTBDef.IsReferenced = True Then
			'MsgBox("Title Block Def Named '" & oTBDef.Name & "' is referenced, and will not be deleted.",vbOKOnly+vbInformation, "CAN'T BE DELETED")
		End If
	Next
	'Specify the Drawing Template document to copy from
	Dim oTFN As String = "C:\WF\System Configurations\2020\Inventor\Templates\Template Notech.idw"
	Dim oTemplate As DrawingDocument = ThisApplication.Documents.Open(oTFN, False)
	Dim oNewTBDef As TitleBlockDefinition
	'Copy TitleBlockDefinition from Template to this drawing, and replace if already exists
	Dim oTemTBDefs As TitleBlockDefinitions = oTemplate.TitleBlockDefinitions
	For Each oTemTBDef As TitleBlockDefinition In oTemTBDefs
		If oTemTBDef.Name = "Notech" Then
			oNewTBDef = oTemTBDef.CopyTo(oDrDoc, True)
		End If
	Next
	'Place the new Border & Title Block on all sheets
	For Each oSheet In oSheets
		oSheet.Activate
		oSheet.AddTitleBlock(oNewTBDef, TitleBlockLocationEnum.kBottomRightPosition)
	Next
	oTemplate.Close(True)
	'oTemplate.ReleaseReference
	'Re-Activate the first sheet again
	oSheets.Item(1).Activate
End function

 

Regards,

Jeffrey

 

Message 19 of 22

WCrihfield
Mentor
Mentor

I'm not sure about that error.  I'm not familiar with the "open newest drawing" option.  Is this related to Vault?  I'm likely not going to be much help when relating to Vault functionality or behavior though, because our company is not currently using it, so I'm just not familiar enough with it.

 

The only thing that comes to mind is that sometimes the actions you make happen by code, aren't always recorded the same way as when you do them manually.  At least not as far as the Undo and Redo system is concerned.  This is more evident when using other forms of code execution, such as Batch files, rather than with iLogic.  From what I've observed while using ilogic & VBA over the years, the Undo/Redo system seems to record the actions as normal (similar to manual actions).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 20 of 22

Anonymous
Not applicable

Deleted this, seemed to do the trick, don't know why though:

 

oTemplate.Close(True)