Updating the old idw with updated Global styles and Title Block from Template

Updating the old idw with updated Global styles and Title Block from Template

damjan_baraga
Enthusiast Enthusiast
500 Views
3 Replies
Message 1 of 4

Updating the old idw with updated Global styles and Title Block from Template

damjan_baraga
Enthusiast
Enthusiast

I made I logic, which update syles and Title block from Teamplate in old idw. I have problew with inserting new Title block on Sheet (line 63)

 

' Check that the active document is a Drawing file
If ThisApplication.ActiveDocument.DocumentType <> kDrawingDocumentObject Then
    MessageBox.Show("This rule can only run from an IDW file", "IDW-Updater", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Exit Sub
End If

' Check if the document is saved
Dim activeDoc As Document = ThisApplication.ActiveDocument
If String.IsNullOrEmpty(activeDoc.FullFileName) Then
    MessageBox.Show("The current drawing has not been saved yet. Please save the drawing first!", "Save", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Exit Sub
End If

' Check if the drawing has at least one view
Dim hasView As Boolean = False
For Each sheet As Sheet In activeDoc.Sheets
    If Sheet.DrawingViews.Count > 0 Then
        hasView = True
        Exit For
    End If
Next

If Not hasView Then
    MessageBox.Show("No views found in the drawing. Please add at least one view.", "Missing Views", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    Exit Sub
End If

'Update local syle from global
OpenDoc = ThisDoc.Document
oStyles = OpenDoc.StylesManager.Styles
        For Each oStyle As Style In oStyles
            If Not oStyle.UpToDate Then
                oStyle.UpdateFromGlobal
            End If 
        Next
		
' Update drawing resources from the template
Dim oBorderName As String = "MOST"
Dim oTitleBlockName1 As String = "DIN rez.deli"

'open source Template file 
oResourceFile = "C:\Users\Public\Documents\Autodesk\Inventor 2025\Templates\MOST_SLO-rez deli.idw"

Dim oSourceFile As DrawingDocument
oSourceFile = ThisApplication.Documents.Open(oResourceFile, False)

'copy the resources from the source file
Dim oDoc As DrawingDocument = ThisDoc.Document
oSourceFile.BorderDefinitions.Item(oBorderName).CopyTo(oDoc, True)
oSourceFile.TitleBlockDefinitions.Item(oTitleBlockName1).CopyTo(oDoc, True)

'close source file
oSourceFile.Close(True)


' Replace title block  in all sheets
For Each oSheet In oDoc.Sheets
		oSheet.Activate
		 If oSheet.TitleBlock IsNot Nothing Then
			oSheet.TitleBlock.Delete
		End If

oSheet.AddTitleBlock(oTitleBlockName1)
Next oSheet

 

 

damjan_baraga_0-1732867511233.png

 

0 Likes
501 Views
3 Replies
Replies (3)
Message 2 of 4

JelteDeJong
Mentor
Mentor

I expect that the problem is that you are trying to add a title block while one has already been added. That is impossible. You might want to try to delete the title block before you add a new one. Try adding this on line 35:

oSheet.TitleBlock.Delete()

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 4

damjan_baraga
Enthusiast
Enthusiast

Thanks for the reply. "Delete" is already done in the If loop, so this should not be a problem. Everything works smoothly till line 63, when the Title block should be inserted in Sheet. Rule put new Title blocks in Drawing Resources and delete old one, if is present.

damjan_baraga_0-1733116335670.png

 

0 Likes
Message 4 of 4

dino_hajdinjak897JN
Explorer
Explorer

I made comparison with the code, that I once wrote/copied, the difference lies in::

- additional code: " Dim titleBlock As TitleBlockDefinition = oDrDoc.TitleBlockDefinitions.Cast(Of TitleBlockDefinition).
Where(Function(t) t.Name = "VIRS 2021 - weldment").FirstOrDefault() " <-- this comes before oSheet.Addtitleblock(oTitleBlockName1)

- closing template after updating all sheets

 

here the code:

 

'Specify the Drawing Template document to copy from
Dim oDrDoc As DrawingDocument
oDrDoc = ThisApplication.ActiveDocument
 
Dim oTFN As String = "S:\Nastavitve za Vault\Templates\VIRS 2022.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 = "VIRS 2021" Or oTemTBDef.Name = "VIRS 2021 - weldment" Then
oNewTBDef = oTemTBDef.CopyTo(oDrDoc, True)
End If
Next
'Place the new Border & Title Block on all sheets
Dim oSheets As Sheets = oDrDoc.Sheets
Dim oSheet As Sheet
For Each oSheet In oSheets
oSheet.Activate
 
If (oSheet.TitleBlock IsNot Nothing) Then
oSheet.TitleBlock.Delete()
End If
If zvar = True Then
Dim titleBlock As TitleBlockDefinition = oDrDoc.TitleBlockDefinitions.Cast(Of TitleBlockDefinition).
Where(Function(t) t.Name = "VIRS 2021 - weldment").FirstOrDefault()
oSheet.AddTitleBlock(titleBlock)
Else
Dim titleBlock As TitleBlockDefinition = oDrDoc.TitleBlockDefinitions.Cast(Of TitleBlockDefinition).
Where(Function(t) t.Name = "VIRS 2021").FirstOrDefault()
oSheet.AddTitleBlock(titleBlock)
End If
ThisApplication.ActiveView.Fit
Next
oTemplate.Close(True)