Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

replacing a border and title block in an existing drawing ilogic

19 REPLIES 19
Reply
Message 1 of 20
Anonymous
4386 Views, 19 Replies

replacing a border and title block in an existing drawing ilogic

Hi,

 

I have found an ilogic that works for me but when the drawing border and title block is the same name..it doesn't update!! probably because it thinks that its the same even though I have done the modification in our template file. 

 

Format:HTML Format Version:1.0 StartHTML: 165 EndHTML: 7995 StartFragment: 314 EndFragment: 7963 StartSelection: 314 EndSelection: 314SyntaxEditor Code Snippet

'delete sheet formats

ThisDrawing.ResourceFileName = "K:\Mech\INVENTOR2016\Inventor Resources\Templates\RAUTE_2016.dwg"
ThisDrawing.KeepExtraResources = True

Dim oDoc As DrawingDocument
oDoc = ThisApplication.ActiveEditDocument

Dim oCurrentNumber  As Sheet
oCurrentNumber = oDoc.ActiveSheet

' Iterate through the sheets, and delete the title blocks and symbols
Dim oSheet As Sheet
For Each oSheet In oDoc.Sheets
    oSheet.Activate
    
    Try
    oSheet.TitleBlock.Delete
    Catch
    'catch error if no border found
    End Try
    
    'set new border
    ActiveSheet.TitleBlock = "RAUTE A TITLE BLOCK"
    
    Try
    oSheet.Border.Delete
    Catch
    'catch error if no border found
    End Try
    
    'set new border
    ActiveSheet.Border = "RAUTE A-SIZE BORDER"
Next

'set back to original sheet
oCurrentNumber.Activate

what I end up doing is running this code first to rename the old blocks, what I want to do is to combine them... and delete the RAUTE A-SIZE BORDER-OLD

 

Format:HTML Format Version:1.0 StartHTML: 165 EndHTML: 14307 StartFragment: 314 EndFragment: 14275 StartSelection: 314 EndSelection: 314SyntaxEditor Code Snippet

oDoc = ThisDoc.Document
Dim oSheets As Sheets
Dim oSheet As Sheet
Dim oTitleBlock As TitleBlock 

oSheets = oDoc.Sheets
For Each oSheet In oSheets

    'handle errors 
    'such as a sheet with no title block present
    On Error Resume Next
    
    'look at title blocks
    oTitleBlock = oSheet.TitleBlock
    For Each oTitleBlock In oDoc.TitleBlocks
        If oTitleBlock.Name = "RAUTE A TITLE BLOCK" Then 
        'rename the instance of the sheet
        oTitleBlock.Name = "RAUTE A TITLE BLOCK-OLD" 
        'also rename the definition in the Drawing Resources folder
            oTitleBlock.Definition.Name = "RAUTE A TITLE BLOCK-OLD" 
        Else If oTitleBlock.Definition.Name = "RAUTE A TITLE BLOCK" Then
        'rename the definition in the Drawing Resources folder
            oTitleBlock.Definition.Name = "RAUTE A TITLE BLOCK-OLD"
               Else                                                             
        End If
        Next
        
    oBorder = oSheet.Border
    For Each oBorder In oDoc.Border
        If oBorder.Name = "RAUTE A-SIZE BORDER" Then 
        'rename the instance of the sheet
        oBorder.Name = "RAUTE A-SIZE BORDER-OLD" 
        'also rename the definition in the Drawing Resources folder
            oBorder.Definition.Name = "RAUTE A-SIZE BORDER-OLD" 
        Else If oBorder.Definition.Name = "RAUTE A-SIZE BORDER" Then
        'rename the definition in the Drawing Resources folder
            oBorder.Definition.Name = "RAUTE A-SIZE BORDER-OLD" 
               Else                                                             
        End If
        Next
    
Next

 

 Thanks in advance for the help!

 

 

19 REPLIES 19
Message 2 of 20
bshbsh
in reply to: Anonymous

What a coincidence, I was trying to do this yesterday and got stuck as well.

Replacing titleblocks with the copy of the new one from the template is easy. Just delete all titleblocks from all sheets so the definition is not referenced anywhere, then you can just delete the old definition, and copy the new one.

Borders are where I got stuck. I can not delete the definition because it is the DefaultBorder. I haven't found out yet how to "unset" this being the default and set the new one as the default instead through the API. Maybe the only way could be to edit it's sketch, remove everything, and copy over the new definition's sketch elements?

Message 3 of 20
MechMachineMan
in reply to: bshbsh

ProTip:

 

Trying to accomplish something with code? Try doing it manually through the UI first!

If you can't do it through the UI, it's ALMOST guaranteed you can't do it through the API.

 

Such is the case with trying to delete the default border.


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 4 of 20
Anonymous
in reply to: bshbsh

I would not use the default template if I were you..you should make your own custom then you will be able to replace delete..i have mine working now..but its like 3 different rules, I don't know how to combine it in one..but the first rules deletes sheet definitions, then replace borders new with old then 3rd rules deletes all the unused borders and title blocks.

 

Let me know if you want me to post these rules.

 

Regards,
Joel

Message 5 of 20
andrew_canfield
in reply to: Anonymous

"3rd rule deletes all the unused borders and title blocks"

 

Could you share this please?

 

Regards

 

Andrew

Message 6 of 20

Hi Try This:-

 

Sub Main()

Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim oSheet As Sheet = oDrawDoc.ActiveSheet
Dim oBorder As Border = oSheet.Border



'Query Drawing Border insertion requirement
question = MessageBox.Show("Do you want to replace the existing drawing border with a new drawing border?", "Border Requirement", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If question = vbYes Then
	DeleteBorders
	InsertBorder
	DeleteRevTable
	ActivateSheetOne
End If

End Sub


Sub DeleteBorders() 'this code deletes all the drawing sheet borders inthe active idw
'MessageBox.Show("deleteborder sub", "Title")

Dim oDrawDoc1 As DrawingDocument = ThisApplication.ActiveDocument
Dim oSheet1 As Sheet = oDrawDoc1.ActiveSheet

' Check to see if the sheet already has a border and delete it if it does.
For Each oSheet1 In ThisApplication.ActiveDocument.Sheets
	If Not oSheet1.Border Is Nothing Then
        oSheet1.Border.Delete
    End If
Next

End Sub


Sub InsertBorder() ' this code inserts a user selected drawing border on all drawing sheets in the active idw
'MessageBox.Show("insertborder sub", "Title")

'user slects a drawing border to insert
	Dim oDrawDoc3 As DrawingDocument = ThisApplication.ActiveDocument
	Dim oSheet3 As Sheet = oDrawDoc3.ActiveSheet
	Dim borderDef As BorderDefinition
	Dim strSelectedBorder As String = "Result2"
	Dim strBorderList As New ArrayList
	
	strBorderRequired = True

	Dim strDrawDoc As Inventor.DrawingDocument = ThisApplication.ActiveDocument
          	
	' change this file name & location to use a different 	
	ThisDrawing.ResourceFileName = "C:\Vault Workspace\Resources\Templates\your border.idw"
	Dim SourceFile = ThisDrawing.ResourceFileName
                    
    Dim strSourceIDW As DrawingDocument
    strSourceIDW = ThisApplication.Documents.Open(SourceFile, False)

	For Each borderDef In strSourceIDW.BorderDefinitions
        strBorderList.Add(borderDef.Name)
    Next
			
	strSelectedBorder = InputListBox("Please select a border.", strBorderList, strSelectedBorder, "Border Selection", "Available Borders")

	For Each borderDef In strSourceIDW.BorderDefinitions
        If (StrComp(borderDef.Name, strSelectedBorder, vbTextCompare) = 0) Then
            CopyFrom = borderDef.CopyTo(strDrawDoc, True)
       	End If
	Next
	strSourceIDW.Close()
	
	Dim oNewBorderDef As BorderDefinition
	oNewBorderDef = oDrawDoc3.BorderDefinitions.Item(borderDef.Name)

	For Each oSheet3 In oDrawDoc3.Sheets
			oSheet3.AddBorder(oNewBorderDef)
	Next

End Sub


Sub DeleteRevTable() 'this code deletes the current rev table from the idw
'MessageBox.Show("rev table sub", "Title")

Dim oDrawDoc2 As Inventor.DrawingDocument = ThisApplication.ActiveDocument
Dim oSheet2 As Sheet = oDrawDoc2.ActiveSheet
Dim oRevTable As RevisionTable = oSheet2.RevisionTables.Item(1)

Dim oDoc as Document = ThisDoc.Document
For Each oSheet2 in oDoc.Sheets
	If oSheet2.RevisionTables.Count>0 Then
		For Each  oRevTable In oSheet2.RevisionTables
		oRevTable.Delete
		Next
	End If
Next

End Sub

Sub ActivateSheetOne() 'makes drawing sheet 1 active in the idw
'MessageBox.Show("activesheet1 sub", "Title")

ActiveSheet = ThisDrawing.Sheet("Sheet:1")

End Sub

 

just change this  "C:\Vault Workspace\Resources\Templates\your border.idw" to your template location and name. 

Message 7 of 20
jmcgookin
in reply to: Tony_Yates

@Tony_Yates This code is great for what I am currently trying to achieve. When I run this though, I do receive an error and not sure if the code is supposed to make the chosen border replacement active or if the user has to activate it manually. After the error, it does bring the new border into the drawing resources/Borders section but does not make it active. I would like some help on removing the user input box to select a border and have the code select the border on it's own. Meaning it will always be the same border from the reference file that I want to be the replacement in all idw files.

 

Error Info:

Error in rule: Plot With Plot Stamp #2 - iLogic Rule, in document: F-LA333-00001.idw

Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

 

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.BorderDefinition.get_Name()
at ThisRule.InsertBorder()
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

Any help would be very appreciated!

Jeremy

Message 8 of 20
Tony_Yates
in reply to: jmcgookin

Hi Jeremy,

 

Delete the red text:-

 

'Query Drawing Border insertion requirement
question = MessageBox.Show("Do you want to replace the existing drawing border with a new drawing border?", "Border Requirement", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If question = vbYes Then
	DeleteBorders
	InsertBorder
	'DeleteRevTable
	ActivateSheetOne
End If
Message 9 of 20

Hi, Tony_Yates

I have the same Error, even after deleted the red text.

Is it possible to do the same code for Sketched Symbols?

I've tryied to change Borders to Sketched Symbols, but it failed.

I need onle insert Sketched Symbols from SourceFile

Message 10 of 20

Hi,

 

Add the name of your sketched symbol where ???? is.

----------------------------------------------------------

Public Sub Main Sketchsymbol_Delete()

Dim oDoc = ThisApplication.ActiveDocument


Dim oSheet As Sheet

For Each oSheet In oDoc.Sheets

Dim oSketchedSymbolDef As SketchedSymbol
For Each oSketchedSymbolDef in oSheet.SketchedSymbols
If osketchedSymbolDef.Name = "????" Then
oSketchedSymbolDef.delete 'It will delete all the sketched symbols
End If
Next

Next
End Sub

Message 11 of 20

If i Understand correctly, this code delete Symbols in opened drawing. I mean

Public Sub Main Sketchsymbol_Delete()

Dim oDoc = ThisApplication.ActiveDocument


Dim oSheet As Sheet

For Each oSheet In oDoc.Sheets

Dim oSketchedSymbolDef As SketchedSymbol
For Each oSketchedSymbolDef in oSheet.SketchedSymbols
If osketchedSymbolDef.Name = "My symbols 1" Then
oSketchedSymbolDef.delete 'It will delete all the sketched symbols
End If

If osketchedSymbolDef.Name = "My symbols 2" Then
oSketchedSymbolDef.delete 'It will delete all the sketched symbols
End If

Next

Next
End Sub

 

I've tried to change previously code with border. see below. Is it Possible to use it? Could you please check

Sub Main()

Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim oSheet As Sheet = oDrawDoc.ActiveSheet
Dim oSketchedSymbol As SketchedSymbol = oSheet.SketchedSymbol

'Query Drawing Border insertion requirement
	InsertSketchedSymbol
	ActivateSheetOne
End Sub

Sub InsertSketchedSymbol() ' this code inserts a user selected drawing SketchedSymbol on all drawing sheets in the active idw
'MessageBox.Show("insertSketchedSymbol sub", "Title")

'user slects a drawing SketchedSymbol to insert
	Dim oDrawDoc3 As DrawingDocument = ThisApplication.ActiveDocument
	Dim oSheet3 As Sheet = oDrawDoc3.ActiveSheet
	Dim SketchedSymbolDef As TitleBlockDefinition
	Dim strSelectedSketchedSymbol As String = "Result2"
	Dim strSketchedSymbolList As New ArrayList
	
	strSketchedSymbolRequired = True

	Dim strDrawDoc As Inventor.DrawingDocument = ThisApplication.ActiveDocument
          	
	' change this file name & location to use a different 	
	ThisDrawing.ResourceFileName = "C:\Inventor Working Area\Templates\Inventor\ØS\2020\Template.idw"
	Dim SourceFile = ThisDrawing.ResourceFileName
                    
    Dim strSourceIDW As DrawingDocument
    strSourceIDW = ThisApplication.Documents.Open(SourceFile, False)

	For Each SketchedSymbolDef In strSourceIDW.SketchedSymbolDefinitions
        strSketchedSymbolList.Add(SketchedSymbolDef.Name)
    Next
			
	strSelectedSketchedSymbol = InputListBox("Please select a SketchedSymbol.", strSketchedSymbolList, strSelectedSketchedSymbol, "SketchedSymbol Selection", "Available SketchedSymbol")

	For Each SketchedSymbolDef In strSourceIDW.SketchedSymbolDefinitions
        If (StrComp(SketchedSymbolDef.Name, strSelectedSketchedSymbol, vbTextCompare) = 0) Then
            CopyFrom = SketchedSymbolDef.CopyTo(strDrawDoc, True)
       	End If
	Next
	strSourceIDW.Close()
	
	Dim oNewSketchedSymbolDef As SketchedSymbolDefinition
	oNewSketchedSymbolDef = oDrawDoc3.SketchedSymbolDefinitions.Item(SketchedSymbolDef.Name)

	For Each oSheet3 In oDrawDoc3.Sheets
			oSheet3.AddSketchedSymbol(oNewSketchedSymbolDef)
	Next

End Sub

Sub DeleteRevTable() 'this code deletes the current rev table from the idw
'MessageBox.Show("rev table sub", "Title")

Dim oDrawDoc2 As Inventor.DrawingDocument = ThisApplication.ActiveDocument
Dim oSheet2 As Sheet = oDrawDoc2.ActiveSheet
Dim oRevTable As RevisionTable = oSheet2.RevisionTables.Item(1)

Dim oDoc As Document = ThisDoc.Document
For Each oSheet2 In oDoc.Sheets
	If oSheet2.RevisionTables.Count>0 Then
		For Each  oRevTable In oSheet2.RevisionTables
		oRevTable.Delete
		Next
	End If
Next

End Sub


Sub ActivateSheetOne() 'makes drawing sheet 1 active in the idw
'MessageBox.Show("activesheet1 sub", "Title")

ActiveSheet = ThisDrawing.Sheet("Sheet:1")

End Sub

 

 

Message 12 of 20

I figured it out.

But if i use code for Delete and Insert Border i have error

Error Info:

Error in rule: InsertBorder - iLogic Rule, in document: MyDrawing.idw

Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

 

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.BorderDefinition.get_Name()
at ThisRule.InsertBorder()
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 i use

ub Main()

Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim oSheet As Sheet = oDrawDoc.ActiveSheet
Dim oBorder As Border = oSheet.Border

'Query Drawing Border insertion requirement

	DeleteBorders
	InsertBorder
	DeleteRevTable
	ActivateSheetOne

End Sub

Sub DeleteBorders() 'this code deletes all the drawing sheet borders inthe active idw
'MessageBox.Show("deleteborder sub", "Title")

Dim oDrawDoc1 As DrawingDocument = ThisApplication.ActiveDocument
Dim oSheet1 As Sheet = oDrawDoc1.ActiveSheet

' Check to see if the sheet already has a border and delete it if it does.
For Each oSheet1 In ThisApplication.ActiveDocument.Sheets
	If Not oSheet1.Border Is Nothing Then
        oSheet1.Border.Delete
    End If
Next

End Sub


Sub InsertBorder() ' this code inserts a user selected drawing border on all drawing sheets in the active idw
'MessageBox.Show("insertborder sub", "Title")

'user slects a drawing border to insert
	Dim oDrawDoc3 As DrawingDocument = ThisApplication.ActiveDocument
	Dim oSheet3 As Sheet = oDrawDoc3.ActiveSheet
	Dim borderDef As BorderDefinition
	Dim strSelectedBorder As String = "Result2"
	Dim strBorderList As New ArrayList
	
	strBorderRequired = True

	Dim strDrawDoc As Inventor.DrawingDocument = ThisApplication.ActiveDocument
          	
	' change this file name & location to use a different 	
	ThisDrawing.ResourceFileName = "C:\Inventor Working Area\Templates\Inventor\2020\MyTemplate.idw"
	Dim SourceFile = ThisDrawing.ResourceFileName
                    
    Dim strSourceIDW As DrawingDocument
    strSourceIDW = ThisApplication.Documents.Open(SourceFile, False)

	For Each borderDef In strSourceIDW.BorderDefinitions
        strBorderList.Add(borderDef.Name)
    Next
			
	strSelectedBorder = InputListBox("Please select a border.", strBorderList, strSelectedBorder, "Border Selection", "Available Borders")

	For Each borderDef In strSourceIDW.BorderDefinitions
        If (StrComp(borderDef.Name, strSelectedBorder, vbTextCompare) = 0) Then
            CopyFrom = borderDef.CopyTo(strDrawDoc, True)
       	End If
	Next
	strSourceIDW.Close()
	
	Dim oNewBorderDef As BorderDefinition
	oNewBorderDef = oDrawDoc3.BorderDefinitions.Item(borderDef.Name)

	For Each oSheet3 In oDrawDoc3.Sheets
			oSheet3.AddBorder(oNewBorderDef)
	Next

End Sub


Sub DeleteRevTable() 'this code deletes the current rev table from the idw
'MessageBox.Show("rev table sub", "Title")

Dim oDrawDoc2 As Inventor.DrawingDocument = ThisApplication.ActiveDocument
Dim oSheet2 As Sheet = oDrawDoc2.ActiveSheet
Dim oRevTable As RevisionTable = oSheet2.RevisionTables.Item(1)

Dim oDoc As Document = ThisDoc.Document
For Each oSheet2 In oDoc.Sheets
	If oSheet2.RevisionTables.Count>0 Then
		For Each  oRevTable In oSheet2.RevisionTables
		oRevTable.Delete
		Next
	End If
Next

End Sub

Sub ActivateSheetOne() 'makes drawing sheet 1 active in the idw
'MessageBox.Show("activesheet1 sub", "Title")

ActiveSheet = ThisDrawing.Sheet("Sheet:1")

End Sub

 

 

Message 13 of 20

hi, could you please help me? how do create code “insert title block” for Active Sheet, not for all sheets of drawing. And when i use code i have massage “The template is opened”.I think it has to do with this line «strSourceClose ()”.   Else, i rewritten your code for Border to title block and when i use it, i can only insert one title block. Thank you!

Message 14 of 20
blandb
in reply to: ilya.galaktionov

I am more than likely missing something, but do the snippets not give you what you need??

 

ilogic.PNG

Autodesk Certified Professional
Message 15 of 20
ilya.galaktionov
in reply to: blandb

Thank you!

I create this Code

ThisDrawing.ResourceFileName = "C:\Inventor Working Area\Templates\Inventor\MyTemplate.idw"
ThisDrawing.KeepExtraResources = True
Dim SourceFile = ThisDrawing.ResourceFileName
                    
    Dim strSourceIDW As DrawingDocument
    strSourceIDW = ThisApplication.Documents.Open(SourceFile, False)
	
Dim oDoc As DrawingDocument
oDoc = ThisApplication.ActiveEditDocument
oDrawDoc = ThisDoc.Document
Dim oCurrentNumber  As Sheet
oCurrentNumber = oDoc.ActiveSheet

' Iterate through the sheets, and delete the title blocks and symbols
Dim oSheet As Sheet = oDrawDoc.ActiveSheet
    
            
    'set new border
    ActiveSheet.TitleBlock = "My Title Block"
      

'set back to original sheet
oCurrentNumber.Activate
strSourceIDW.Close()
Message 16 of 20
creggo
in reply to: ilya.galaktionov

Getting the same error.  It's so close, but fails to insert the new border.

Any clever boffins can help with this one please? 🤞

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
   at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
   at Inventor.BorderDefinition.get_Name()
   at ThisRule.InsertBorder()
   at ThisRule.Main()
   at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
   at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

Message 17 of 20
AlexFielder
in reply to: creggo

Hi @creggo,

 

Here is the updated rule I shared with you yesterday for the benefit of future searchers:

 

 

Option Explicit on
Sub Main()

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
	MessageBox.Show("This rule only works for Drawing Documents.", "Wrong Document Type", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
	Exit Sub
End If

Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim oSheet As Sheet = oDrawDoc.ActiveSheet
Dim oBorder As Border = oSheet.Border

'Query Drawing Border insertion requirement
Dim question As DialogResult = MessageBox.Show("Do you want to replace the existing drawing border with a new drawing border?" _
& vbLf & "THIS CANNOT BE UNDONE!", "Border Requirement", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If question = vbYes Then
	DeleteBorders
	InsertBorder
	DeleteRevTable
	ActivateSheetOne
End If

End Sub


Sub DeleteBorders() 'this code deletes all the drawing sheet borders inthe active idw
'MessageBox.Show("deleteborder sub", "Title")
Logger.Info("running delete borders")
Dim oDrawDoc1 As DrawingDocument = ThisApplication.ActiveDocument
Dim oSheet1 As Sheet = oDrawDoc1.ActiveSheet

' Check to see if the sheet already has a border and delete it if it does.
For Each oSheet1 In ThisApplication.ActiveDocument.Sheets
	If Not oSheet1.Border Is Nothing Then
        oSheet1.Border.Delete
    End If
Next
Logger.Info("finished running delete borders")
End Sub


Sub InsertBorder() ' this code inserts a user selected drawing border on all drawing sheets in the active idw
'MessageBox.Show("insertborder sub", "Title")

'user slects a drawing border to insert
	Dim oDrawDoc3 As DrawingDocument = ThisApplication.ActiveDocument
	Dim oSheet3 As Sheet = oDrawDoc3.ActiveSheet
	Dim borderDef As BorderDefinition
	Dim strSelectedBorder As String = "Result2"
	Dim strBorderList As New ArrayList
	
	Dim strBorderRequired As Boolean = True

	Dim strDrawDoc As Inventor.DrawingDocument = ThisApplication.ActiveDocument
          	
	' change this file name & location to use a different 	
	ThisDrawing.ResourceFileName = "C:\Users\alex.fielder\OneDrive\Inventor\Designs\ManAndMachine\Templates\Standard.dwg"
	Dim SourceFile = ThisDrawing.ResourceFileName
                    
    Dim strSourceIDW As DrawingDocument
    strSourceIDW = ThisApplication.Documents.Open(SourceFile, False)

	For Each borderDef In strSourceIDW.BorderDefinitions
        strBorderList.Add(borderDef.Name)
    Next
			
	strSelectedBorder = InputListBox("Please select a border.", strBorderList, strSelectedBorder, "Border Selection", "Available Borders")
	If strSelectedBorder = "Default Border" Then
		MessageBox.Show("Changing the Default Border border is NOT ALLOWED, exiting")
		logger.Error("User attempted to delete Default Border; this is not allowed by the API!")
		Exit Sub
	End If
	
	Dim NewBorder As BorderDefinition
	For Each borderDef In strSourceIDW.BorderDefinitions
        If (borderDef.Name = strSelectedBorder) Then
            NewBorder = borderDef.CopyTo(strDrawDoc, True)
			Exit For
       	End If
	Next
	
	strSourceIDW.Close()
	
	Dim oNewBorderDef As BorderDefinition
	oNewBorderDef = oDrawDoc3.BorderDefinitions.Item(NewBorder.Name)

	For Each oSheet3 In oDrawDoc3.Sheets
			oSheet3.AddBorder(oNewBorderDef)
	Next

End Sub


Sub DeleteRevTable() 'this code deletes the current rev table from the idw
'MessageBox.Show("rev table sub", "Title")

Dim oDrawDoc2 As Inventor.DrawingDocument = ThisApplication.ActiveDocument
Dim oSheet2 As Sheet = oDrawDoc2.ActiveSheet

Dim oDoc As Document = ThisDoc.Document
For Each oSheet2 In oDoc.Sheets
	If oSheet2.RevisionTables.Count>0 Then
		For Each oRevTable As RevisionTable In oSheet2.RevisionTables
			oRevTable.Delete
		Next
	End If
Next

End Sub

Sub ActivateSheetOne() 'makes drawing sheet 1 active in the idw
'MessageBox.Show("activesheet1 sub", "Title")
ActiveSheet = ThisDrawing.Sheet("Sheet:1")
End Sub

 

😀

Message 18 of 20

Hello! 

 

I very much like this iLogic, but I would need an update on this.

Is there anybody that can help me with this?

I would like to have ALL borders and TITLEBLOCKS deleted in the .idw, then ALL borders and TITLEBLOCKS copy/paste into it. So not only the active ones on the sheet, but also the ones in the 'drawing resources'.

I assume, logicly the rule will have to delete the active ones on the sheet(s) and then delete the ones in the 'drawing resources'. After that a copy/paste from the template .idw.

 

I don't know if my question is clear, if not, please tell me what exactly.

Thank you for your help!

Message 19 of 20
bshbsh
in reply to: maarten_desmet

this is vba though and no error checking whatsoever, but you may get the idea. it's simple.

problem is you can't simply delete the default borderdefinition.

 

Public Sub borders()
    Dim InvDoc As Document
    Set InvDoc = ThisApplication.ActiveEditDocument
    Dim SourceDoc As Document
    Set SourceDoc = ThisApplication.Documents.Open("D:\Path\To\Your\IDW_Template_file.idw", False)
    
    For Each Sheet In InvDoc.Sheets
        If Not Sheet.TitleBlock Is Nothing Then Sheet.TitleBlock.Delete
        If Not Sheet.Border Is Nothing Then Sheet.Border.Delete
    Next
    For Each TitleBlockDefinition In InvDoc.TitleBlockDefinitions
        If TitleBlockDefinition.IsReferenced = False Then TitleBlockDefinition.Delete
    Next
    For Each BorderDefinition In InvDoc.BorderDefinitions
        If BorderDefinition.IsReferenced = False Then BorderDefinition.Delete
    Next
    
    For Each TitleBlockDefinition In SourceDoc.TitleBlockDefinitions
        Set TargetHeader = TitleBlockDefinition.CopyTo(InvDoc, True)
    Next
    For Each BorderDefinition In SourceDoc.BorderDefinitions
        On Error Resume Next
        Set TargetBorder = BorderDefinition.CopyTo(InvDoc, True)
        On Error GoTo 0
    Next
    SourceDoc.Close(True)
End Sub

 

 

Message 20 of 20
WCrihfield
in reply to: maarten_desmet

Hi @maarten_desmet.  This code is pretty much the same as the last VBA example, but is in iLogic, and does a couple minor things differently.  To use it, you would first need to edit the name of the drawing template file that you will be copying TitleBlockDefinitions & BorderDefinitions from.  And if you want the code to save the drawing after the changes have been made, just un-comment the last line of code.

 

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Exit Sub
	Dim oDDoc As DrawingDocument = ThisDoc.Document
	Dim sTPath As String = ThisApplication.DesignProjectManager.ActiveDesignProject.TemplatesPath
	Dim sTFile As String = sTPath & "MyDrawingTemplate.idw"
	Dim oTemplate As DrawingDocument = ThisApplication.Documents.Open(sTFile, False)
	Dim oSheets As Inventor.Sheets = oDDoc.Sheets
	Dim oTrans As Inventor.Transaction
	oTrans = ThisApplication.TransactionManager.StartTransaction(oDDoc, "Update Borders & TitleBlocks")
	For Each oSheet As Inventor.Sheet In oSheets
		If oSheet.TitleBlock IsNot Nothing Then Try : oSheet.TitleBlock.Delete : Catch : End Try
		If oSheet.Border IsNot Nothing Then Try : oSheet.Border.Delete : Catch : End Try
	Next
	For Each oTBDef As TitleBlockDefinition In oDDoc.TitleBlockDefinitions
		If oTBDef.IsReferenced = False Then Try : oTBDef.Delete : Catch : End Try
	Next
	For Each oBDef As BorderDefinition In oDDoc.BorderDefinitions
		If oBDef.IsReferenced = False Then Try : oBDef.Delete : Catch : End Try
	Next
	For Each oBDef As BorderDefinition In oTemplate.BorderDefinitions
		Dim oNewBDef As BorderDefinition : Try : oNewBDef = oBDef.CopyTo(oDDoc, True) : Catch : End Try
	Next
	For Each oTBDef As TitleBlockDefinition In oTemplate.TitleBlockDefinitions
		Dim oNewTBDef As BorderDefinition : Try : oNewTBDef = oTBDef.CopyTo(oDDoc, True) : Catch : End Try
	Next
	oTrans.End
	oTemplate.Close(True)
	If oDDoc.RequiresUpdate Then oDDoc.Update2(True)
	'If oDDoc.Dirty Then oDDoc.Save2(False)
End Sub

Edit:  Just keep in mind that this code does not insert a new Border or TitleBlock on each sheet after the fact, because I do not know which Border & which TitleBlock you will want placed back into each sheet.  The actual working code I use at work is much longer than this, covers more than just borders & title blocks, and has a lot of customized stuff in it.  You will want to add more lines of code just before the "oTrans.End" line, to loop back through all of your drawing's sheets and add the border & title block you want to each sheet.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report