<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: ilogic rule called from within add-in is not working completely in Inventor Programming - iLogic, Macros, AddIns &amp; Apprentice</title>
    <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-rule-called-from-within-add-in-is-not-working-completely/m-p/13165006#M174171</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/7812054"&gt;@WCrihfield&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much for your good explanation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your code.&lt;/P&gt;&lt;P&gt;Unfortunately the rule cannot delete all the borders.&lt;/P&gt;&lt;P&gt;&amp;nbsp;in the above code it says:&lt;/P&gt;&lt;PRE&gt;'delete all unreferenced BorderDefinitions
	For Each oBDef As BorderDefinition In oDrawingDoc.BorderDefinitions
		If (Not oBDef.IsDefault) AndAlso (Not oBDef.IsReferenced) Then
			oBDef.Delete()
		End If
	Next oBDef&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem seems to be that those borders are "referenced" and therefore cannot be deleted.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Jakob_Hamburg_0-1732179795762.png" style="width: 934px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1436756iC03AAC84C4B2DA8F/image-dimensions/934x55?v=v2" width="934" height="55" role="button" title="Jakob_Hamburg_0-1732179795762.png" alt="Jakob_Hamburg_0-1732179795762.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do not quite understand, why those borders are referenced since they are not on the sheets (because your code deletes the borders on the sheets in line 31).&lt;/P&gt;&lt;P&gt;Is it, that Inventor has not realized that they have been deleted and that is why they are still referenced?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(&lt;SPAN&gt;Autodesk help says: A border definition is referenced whenever it is used on a sheet. A referenced border definition cannot be deleted.&lt;/SPAN&gt;)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since the rule cannot delete the borders it adds "copy of..".&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Jakob_Hamburg_1-1732180497088.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1436762i3CC272BADC670EAF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Jakob_Hamburg_1-1732180497088.png" alt="Jakob_Hamburg_1-1732180497088.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you know how to delete the initial borders in the drawing recourses?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 21 Nov 2024 09:16:31 GMT</pubDate>
    <dc:creator>Jakob_Hamburg</dc:creator>
    <dc:date>2024-11-21T09:16:31Z</dc:date>
    <item>
      <title>ilogic rule called from within add-in is not working completely</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-rule-called-from-within-add-in-is-not-working-completely/m-p/13156769#M174037</link>
      <description>&lt;P&gt;Hello.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a problem with an ilogic that is called from an add-inn.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We are using an add-inn (not made by me) that executes an ilogic named "start" when the inventor file is saved.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;doc = ThisDoc.Document
oDoc = ThisApplication.ActiveDocument

Dim docType As DocumentTypeEnum = doc.DocumentType
' docSubType As String = doc.SubType
Dim docSubType As String  = oDoc.SubType

Select Case docType
	Case DocumentTypeEnum.kAssemblyDocumentObject
		'IAM
		iLogicVb.RunExternalRule("MassProperty")
		iLogicVb.RunExternalRule("DisplayName")
	Case DocumentTypeEnum.kDrawingDocumentObject
		'idw/dwg
		'iLogicVb.RunExternalRule("DisplayName")
		iLogicVb.RunExternalRule("Mass Drawing")
		iLogicVb.RunExternalRule("UpdateDrwResources")
		'iLogicVb.RunExternalRule("update_drawing_rcsrcs_for_new_drw")
	Case DocumentTypeEnum.kPartDocumentObject
	   	'ipt
		iLogicVb.RunExternalRule("DisplayName")
		iLogicVb.RunExternalRule("MassProperty")
		iLogicVb.RunExternalRule("MaterialGrade")
		
		If docSubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
			'Sheet Metal
			iLogicVb.RunExternalRule("SheetMetalExtents")
					Else If docSubType = "{4D29B490-49B2-11D0-93C3-7E0706000000}"
			' "Vanlig" part
					iLogicVb.RunExternalRule("PartExtents")
		End If
End Select&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In case it is a drawing the ilogic "Start" fires up an iLogic named "UpdateDrwRecources".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I execute the ilogic "Start" through our add-inn (by saving the drawing) the iLogic&amp;nbsp;"UpdateDrwRecources" will be found and executed but not in a sufficient way. I can see by using message boxes in that&amp;nbsp;iLogic&amp;nbsp;"UpdateDrwRecources" that it is executed but it is also supposed to delete drawing borders and replace those with latest from the drawing template...which is not happening.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But when I execute the iLogic "Start" manually from the external iLogic tab, everything is working as supposed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it possible that certain operations within the iLogic (like deleting borders) is not possible when executed initially by the ad-inn?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(I know it is a quite nested logic and could be probably done in better way but here it is set-up like this at the moment.)&lt;/P&gt;</description>
      <pubDate>Mon, 18 Nov 2024 10:05:11 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-rule-called-from-within-add-in-is-not-working-completely/m-p/13156769#M174037</guid>
      <dc:creator>Jakob_Hamburg</dc:creator>
      <dc:date>2024-11-18T10:05:11Z</dc:date>
    </item>
    <item>
      <title>Re: ilogic rule called from within add-in is not working completely</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-rule-called-from-within-add-in-is-not-working-completely/m-p/13157105#M174039</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/3717904"&gt;@Jakob_Hamburg&lt;/a&gt;.&amp;nbsp; When you say these iLogic rules get ran "at save", do you mean 'before' or 'after' the save?&amp;nbsp; If these things are being done to the drawing 'after' the save, then those changes will not be saved.&amp;nbsp; So, if your drawing gets closed after that, then those changes would get lost.&amp;nbsp; This is one reason that it is usually recommended to make changes to documents before they save, rather than after they save.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, why are you using two different code phrases to set the values of two different variables at the start of the rule, but not 'declaring' either variable (using 'Dim' or similar), and not specifying that they will be 'Document' type variables?&amp;nbsp; Then you are using one variable for checking DocumentType, while using the other variable when checking SubType.&amp;nbsp; In some situations, those two code phrases can return different documents.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, I would suggest that you use a slightly different method for running your rules.&lt;/P&gt;
&lt;P&gt;Instead of using this method:&amp;nbsp;&amp;nbsp;&lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=0487090c-5867-52c3-825a-4adb1536fe0c" target="_blank" rel="noopener"&gt;ILowLevelSupport.RunExternalRule Method (String)&lt;/A&gt;&amp;nbsp;[iLogicVb.RunExternalRule(sExternalRuleName)], you should try using the very similar&amp;nbsp;&lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=20a9a482-0e9c-6b88-a6f2-e200b332ee62" target="_blank" rel="noopener"&gt;IiLogicAutomation.RunExternalRule Method&lt;/A&gt;&amp;nbsp;[iLogicVb.Automation.RunExternalRule(oDoc, sExternalRuleName)], which allows you to specify a Document object for the external rule to focus on.&amp;nbsp; This method gives us an extra step of control.&amp;nbsp; However, your external rule must be set-up properly to be able to recognize that Document specification, in order for it to work properly.&amp;nbsp; That usually means using the 'ThisDoc.Doument' code phrase to acquire/specify which Document you want your rule focused on, instead of the 'ThisApplication.ActiveDocument' code phrase.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Nov 2024 12:44:42 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-rule-called-from-within-add-in-is-not-working-completely/m-p/13157105#M174039</guid>
      <dc:creator>WCrihfield</dc:creator>
      <dc:date>2024-11-18T12:44:42Z</dc:date>
    </item>
    <item>
      <title>Re: ilogic rule called from within add-in is not working completely</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-rule-called-from-within-add-in-is-not-working-completely/m-p/13159485#M174077</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/7812054"&gt;@WCrihfield&lt;/a&gt;.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much for your explanations.&lt;/P&gt;&lt;P&gt;I am running these iLogic "before" save.&lt;/P&gt;&lt;P&gt;You are totally right with the mess in my iLogic rules...(I inherited those rules and try not touch too much since they are running..but only running until I have to make some modification due to new requirements.)&lt;/P&gt;&lt;P&gt;I updated the iLogic "Start" but that is not the cause of the problem, I think.&lt;/P&gt;&lt;LI-CODE lang="general"&gt;'Dim oDoc As Document = ThisDoc.Document
Dim oDoc As Document = ThisApplication.ActiveDocument

Dim docType As DocumentTypeEnum = oDoc.DocumentType
' docSubType As String = doc.SubType
Dim docSubType As String  = oDoc.SubType

Select Case docType
	Case DocumentTypeEnum.kAssemblyDocumentObject
		'IAM
		iLogicVb.RunExternalRule("MassProperty")
		iLogicVb.RunExternalRule("DisplayName")
	Case DocumentTypeEnum.kDrawingDocumentObject
		'idw/dwg
		'iLogicVb.RunExternalRule("DisplayName")
		iLogicVb.RunExternalRule("Mass Drawing")
		iLogicVb.Automation.RunExternalRule(oDoc,"UpdateDrwResources")
		'iLogicVb.RunExternalRule("update_drawing_rcsrcs_for_new_drw")
	Case DocumentTypeEnum.kPartDocumentObject
	   	'ipt
		iLogicVb.RunExternalRule("DisplayName")
		iLogicVb.RunExternalRule("MassProperty")
		iLogicVb.RunExternalRule("MaterialGrade")
		
		If docSubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
			'Sheet Metal
			iLogicVb.RunExternalRule("SheetMetalExtents")
					Else If docSubType = "{4D29B490-49B2-11D0-93C3-7E0706000000}"
			' "Vanlig" part
					iLogicVb.RunExternalRule("PartExtents")
		End If
End Select
	&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The ilogic "Start" fires up an iLogic named "UpdateDrwRecources". And that&amp;nbsp;"UpdateDrwRecources" is working differntly based on how it is executed.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;We have an add-in that executes "before save" the ilogic "Start". When the (external) ilogic is executed manually instead by "run rule" it acts as it should.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;(I will describe the behaviour in a few minutes)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Nov 2024 08:36:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-rule-called-from-within-add-in-is-not-working-completely/m-p/13159485#M174077</guid>
      <dc:creator>Jakob_Hamburg</dc:creator>
      <dc:date>2024-11-19T08:36:40Z</dc:date>
    </item>
    <item>
      <title>Re: ilogic rule called from within add-in is not working completely</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-rule-called-from-within-add-in-is-not-working-completely/m-p/13159608#M174078</link>
      <description>&lt;P&gt;The problem is in the iLogic&amp;nbsp;&lt;SPAN&gt;named "UpdateDrwRecources".&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The rule basically deletes all drawing recourses and afterward adds/replaces them by the ones from the drawing template.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;When the rule is executed from the add-inn the deleting of existing borders (for example) is not working.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;This screenshot shows the add-inn to execute the ilogic "Start"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Jakob_Hamburg_0-1732007475180.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1435586i422780D148F90736/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Jakob_Hamburg_0-1732007475180.png" alt="Jakob_Hamburg_0-1732007475180.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I outcomment the last step of the&amp;nbsp;iLogic&amp;nbsp;&lt;SPAN&gt;"UpdateDrwRecources" in which new drawing recources are added, I can see that the deleting is not happening, if the rule is executed initially by the add-inn (via ilogic Start in between):&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Jakob_Hamburg_1-1732007641230.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1435590i0B13FB2024ACB1AF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Jakob_Hamburg_1-1732007641230.png" alt="Jakob_Hamburg_1-1732007641230.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(the rule itself is running if executed by add-inn, I could check that with message boxes)&lt;/P&gt;&lt;P&gt;sorry for the messy code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;Sub main
'List of Variables    
	Dim oDrawingDoc As Inventor.DrawingDocument
    Dim oSheetNumber As Integer
	Dim oActiveSheetNumber As Integer
	Dim oActiveSheetName As String	
	Dim oSheet As Sheet
    Dim oSourceTitleBlockDef As TitleBlockDefinition
    Dim oNewTitleBlockDef As TitleBlockDefinition
	Dim oBorderName As String

'Selects this document
	'oDrawingDoc = ThisApplication.ActiveDocument
	oDrawingDoc = ThisDoc.Document

'Checks if there is a Title Block on the Active Sheet
	'oDrawingDoc = ThisApplication.ActiveDocument
        If oDrawingDoc.ActiveSheet.TitleBlock Is Nothing Then
		MessageBox.Show ("Please Insert a Title Block on this Sheet!")
		Exit Sub
        End If

'Gets the Name of the Active Sheet to Activate it at the end of the Code
	oActiveSheetName = ActiveSheet.Name

'Gets the Name of the Title Block Currently in the Drawing to Insert at the end of this code
    oSheet = oDrawingDoc.ActiveSheet
    
    If (oSheet.TitleBlock.Name = "company_name Title block") Then
        oTitleBlockName = "company_name Title block"
	 Else:
        MessageBox.Show ("This drawing has an unknown title block")
        Exit Sub
    End If
	
'Gets the Name of the Border Currently in the Drawing to Insert at the end of this code 
Try
	 oBorderName = oSheet.Border.Name
Catch
	MessageBox.Show("No border exists", "Title")
	Exit Sub
End Try

'Deletes ALL of the Title Blocks Shown in the Drawing
For oSheetNumber = 1 To oDrawingDoc.Sheets.Count
        oDrawingDoc.Sheets(oSheetNumber).Activate
        If Not oDrawingDoc.ActiveSheet.TitleBlock Is Nothing Then
            oDrawingDoc.ActiveSheet.TitleBlock.Delete
        End If
    Next oSheetNumber

	'MessageBox.Show (oActiveSheetName &amp;amp; vbCr &amp;amp; oTitleBlockName)

	Dim oTitle As TitleBlockDefinition
	Dim oTitleBlock As TitleBlock
	
	For Each oTitle In oDrawingDoc.TitleBlockDefinitions
        If oTitle.IsReferenced = False Then
        oTitle.Delete
        End If
		Next
		
'Deletes ALL of the Borders Shown in the Drawing
For oSheetNumber = 1 To oDrawingDoc.Sheets.Count
        oDrawingDoc.Sheets(oSheetNumber).Activate
        If Not oDrawingDoc.ActiveSheet.Border Is Nothing Then
            oDrawingDoc.ActiveSheet.Border.Delete
        End If
    Next oSheetNumber


'Clear drawing resources

If oCurrentPane = "Vault" Then
ThisApplication.ActiveDocument.BrowserPanes.Item("Model").Activate
End If
	
	'delete sheet formats
	Dim oDoc As DrawingDocument
	oDoc = ThisApplication.ActiveEditDocument
	
	Dim oPane As BrowserPane
	oPane = oDoc.BrowserPanes.ActivePane
	
	Dim oDocNode As BrowserNode
	oDocNode = oPane.TopNode
	
	Dim oDrawingResourceNode As BrowserNode
	oDrawingResourceNode = oDocNode.BrowserNodes.Item("Drawing Resources")
		
	Dim oTitleBlocksNode As BrowserNode
	oTitleBlocksNode = oDrawingResourceNode.BrowserNodes.Item("Sheet Formats")
	
	Dim oBordersNode As BrowserNode
	oBordersNode = oDrawingResourceNode.BrowserNodes.Item("Borders")

	Dim oTitleBlocks As BrowserNode
	oTitleBlocks = oDrawingResourceNode.BrowserNodes.Item("Title Blocks")		
		
	Dim oNode As BrowserNode 
	For Each oNode In oTitleBlocksNode.BrowserNodes'oDocNode.BrowserNodes
	' If the node is visible and expanded, collapse it.
	If oNode.Visible = True And oNode.Expanded = False Then
	oNode.Expanded = True
	End If
	Next
	
	For Each oNode In oBordersNode.BrowserNodes'oDocNode.BrowserNodes
	' If the node is visible and expanded, collapse it.
	If oNode.Visible = True And oNode.Expanded = False Then
	oNode.Expanded = True
	End If
	Next
	
	For Each oNode In oTitleBlocks.BrowserNodes'oDocNode.BrowserNodes
	' If the node is visible and expanded, collapse it.
	If oNode.Visible = True And oNode.Expanded = False Then
	oNode.Expanded = True
	End If
	Next
	
	
	
	Dim oSheetFormatNode As BrowserNode
	For Each oSheetFormatNode In oTitleBlocksNode.BrowserNodes
	' Select the node in the browser.
	oSheetFormatNode.DoSelect
	
	' Execute the delete command.
	ThisApplication.CommandManager.ControlDefinitions.Item("AppDeleteCmd").Execute
	Next

	'Dim oBordersNode As BrowserNode
	For Each oBordersNode In oBordersNode.BrowserNodes
	' Select the node in the browser.
	oBordersNode.DoSelect
	
	' Execute the delete command.
	ThisApplication.CommandManager.ControlDefinitions.Item("AppDeleteCmd").Execute
	Next
	
	For Each oTitleBlocks In oTitleBlocks.BrowserNodes
	' Select the node in the browser.
	oTitleBlocks.DoSelect
	
	' Execute the delete command.
	ThisApplication.CommandManager.ControlDefinitions.Item("AppDeleteCmd").Execute
	Next
	
	
	
'Adds the Title Blocks from the Template File
ThisDrawing.ResourceFileName = "C:\WS_company_name\Templates\Standard.idw"
ThisDrawing.KeepExtraResources = True

'ActiveSheet.TitleBlock = "company_name Title block"
'ActiveSheet.Border = oBorderName

'Add Borders
ActiveSheet.Border = "A1"
ActiveSheet.Border = "A2"
ActiveSheet.Border = "A3"
ActiveSheet.Border = "A4"
ActiveSheet.Border = "A0"



''Adds the Same Title Block as in the initial drawing
''due to the fact that we use different title blocks for different sub-companies we do not bring in the title block from the template anymore
For oSheetNumber = 1 To oDrawingDoc.Sheets.Count
        oDrawingDoc.Sheets(oSheetNumber).Activate
	ActiveSheet.TitleBlock = oTitleBlockName
	ActiveSheet.Border = oBorderName
    Next oSheetNumber
'MsgBox(oBorderName)
''Activates the Sheet by Full Name
ActiveSheet = ThisDrawing.Sheet(oActiveSheetName)

'MsgBox("test: rule UpdateDrwRecources is executed now")

End Sub&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I cannot figure out if there is a limitation to execute the delete command&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;	&lt;SPAN&gt;' Execute the delete command.&lt;/SPAN&gt;
	&lt;SPAN&gt;ThisApplication&lt;/SPAN&gt;.&lt;SPAN&gt;CommandManager&lt;/SPAN&gt;.&lt;SPAN&gt;ControlDefinitions&lt;/SPAN&gt;.&lt;SPAN&gt;Item&lt;/SPAN&gt;(&lt;SPAN&gt;"AppDeleteCmd"&lt;/SPAN&gt;).&lt;SPAN&gt;Execute&lt;/SPAN&gt;
	&lt;SPAN&gt;Next&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;or if my code is not clear enough and it is unclear in which document the deleting should happen.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;As I said, when the iLogic "Start" and through that the&amp;nbsp;iLogic "UpdateDrwRecources" is run manually everything is working.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Do you have an advice for me?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Nov 2024 09:40:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-rule-called-from-within-add-in-is-not-working-completely/m-p/13159608#M174078</guid>
      <dc:creator>Jakob_Hamburg</dc:creator>
      <dc:date>2024-11-19T09:40:34Z</dc:date>
    </item>
    <item>
      <title>Re: ilogic rule called from within add-in is not working completely</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-rule-called-from-within-add-in-is-not-working-completely/m-p/13160696#M174105</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/3717904"&gt;@Jakob_Hamburg&lt;/a&gt;.&amp;nbsp; This morning, I just read through your replies since my last reply.&amp;nbsp; That is a lot of good information, so thank you.&amp;nbsp; It is also a lot to process and think about.&amp;nbsp; It is often difficult for me (and others in my position, seeking to help others on the forum) to determine how much coding experience you may already have, or how much you may already know about all the related concepts involved, from a code based point of view, so it is difficult to know how much I/we may need to explain.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general, executing a 'command' (&lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-ControlDefinition" target="_blank" rel="noopener"&gt;ControlDefinition&lt;/A&gt;) (as opposed to using an API method) will always 'act upon' whichever Document that happens to be 'active' (visibly showing on your screen at that exact moment), no matter which Document reference your code may be currently working with at that moment in time, because there is no way to tell the command which document you want it to act upon.&amp;nbsp; Because of this, it is generally not a good idea to execute commands from code that may be running remotely, in response to events happening.&amp;nbsp; Because events can happen to (or because of) documents that are not currently 'active', which can trigger the code to run, but the command would be acting upon the currently active document, instead of the one in the background which actually triggered the event.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using proper/correct/reliable Document references, and ways to refer to specific documents, within your code that is going to be ran remotely, by events, is extremely important.&amp;nbsp; It can be difficult to understand how all the available/possible ways to refer to a document work at first.&amp;nbsp; And the iLogic add-in offers us a lot of seemingly simple tools that seem to be very simple to use.&amp;nbsp; However, one thing that is fairly common with most iLogic tools is the 'lack' of a specific Document reference being involved anywhere.&amp;nbsp; This leaves us wandering...which Document will this piece of code be 'focused' on when it runs?...and how can I be absolutely sure that it will be working on the specific Document I want it to be working on in various situations?&amp;nbsp; Because of those uncertainties, I often revert back to using the main Inventor API tools, instead of the tools that are unique to the iLogic API.&amp;nbsp; When using the Inventor API way of doing things, you must step up or down the API object model structure, which is very logical and predictable, and one of those steps is pretty much always a specific Document object, making that reference pretty clear.&amp;nbsp; There are some relatively new, more advanced ways to specify which document some of the uniquely iLogic tools will focus their attention on, in more reliable ways, but that is a whole other discussion.&amp;nbsp; I do still use the '&lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=Application_ActiveDocument" target="_blank" rel="noopener"&gt;ThisApplication.ActiveDocument&lt;/A&gt;' code phrase in some very specific situations, where I need to make sure that my code is only working on the document that is currently, visibly showing on the screen.&amp;nbsp; But I use the iLogic code phrase '&lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=08f07d9d-b1e9-f145-072d-9b413a49a4c7" target="_blank" rel="noopener"&gt;ThisDoc.Document&lt;/A&gt;' far more, and in far more types of situations than the other phrase, because it is very dynamic, and I now understand a lot more about how it works in several different types of situations than I used to, based on years of experience.&amp;nbsp; The rules/code it very likely not working correctly when being ran remotely (by your add-in) because it is trying to work on the wrong document.&amp;nbsp; I generally tell folks to 'get' their reference to a document that the code should be focused on&amp;nbsp;set to a single variable once, either at, or near the start of the rule, then make absolutely sure that every other line of code after that within the whole rule, is using that same exact variable (one way or another) when accessing, reading, or writing to that document, in any way.&amp;nbsp; So of you have a variable like 'oDoc' that has already had its value set to a Document, then do not use terms like &lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=40e8d82c-2ef4-1a94-b16c-bd7f0510ab50" target="_blank" rel="noopener"&gt;ThisDoc&lt;/A&gt;, or &lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=2e4ce76d-88ae-c9de-0a5d-3767726f0397" target="_blank" rel="noopener"&gt;ActiveSheet&lt;/A&gt;, or &lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=f22a0d38-c6e2-326c-3835-e5eb4e424350" target="_blank" rel="noopener"&gt;ThisDrawing&lt;/A&gt;, or &lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=Application_ActiveDocument" target="_blank" rel="noopener"&gt;ActiveDocument&lt;/A&gt;, or &lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=Application_ActiveEditDocument" target="_blank" rel="noopener"&gt;ActiveEditDocument&lt;/A&gt;, or any others after that point, because one/some of those other terms may be pointing to a different Document than your 'oDoc' variable is set to.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an alternate version of your code that you can try out:&lt;/P&gt;
&lt;LI-CODE lang="general"&gt;Sub Main
	Dim oDrawingDoc As Inventor.DrawingDocument = ThisDoc.Document
	Dim oSheets As Inventor.Sheets = oDrawingDoc.Sheets
	Dim oActiveSheet As Inventor.Sheet = oDrawingDoc.ActiveSheet
	Dim sOriginalTitleBlockName As String = "company_name Title block"
	Dim sOriginalBorderName As String
	'Checks if there is a Title Block on the Active Sheet
	If oActiveSheet.TitleBlock Is Nothing Then
		MessageBox.Show("Please Insert a Title Block on this Sheet!")
		Exit Sub
	ElseIf oActiveSheet.TitleBlock.Name &amp;lt;&amp;gt; sOriginalTitleBlockName Then
		MessageBox.Show("This drawing has an unknown title block")
		Exit Sub
	End If

	'Gets the Name of the Border Currently in the Drawing to Insert at the end of this code 
	Try
		sOriginalBorderName = oActiveSheet.Border.Name
	Catch
		MessageBox.Show("No border exists", "Title")
		Exit Sub
	End Try

	'Delete all TitleBlocks &amp;amp; Borders from all sheets
	For Each oSheet As Inventor.Sheet In oSheets
		oSheet.Activate
		If Not oSheet.TitleBlock Is Nothing Then
			oSheet.TitleBlock.Delete
		End If
		If Not oSheet.Border Is Nothing Then
			oSheet.Border.Delete
		End If
	Next oSheet
	'delete all unreferenced TitleBlockDefinitions
	For Each oTBDef As TitleBlockDefinition In oDrawingDoc.TitleBlockDefinitions
		If Not oTBDef.IsReferenced Then oTBDef.Delete
	Next oTBDef
	'delete all unreferenced BorderDefinitions
	For Each oBDef As BorderDefinition In oDrawingDoc.BorderDefinitions
		If (Not oBDef.IsDefault) AndAlso (Not oBDef.IsReferenced) Then
			oBDef.Delete()
		End If
	Next oBDef
	'open drawing template (invisibly - in the background)
	Dim sTemplate As String = "C:\WS_company_name\Templates\Standard.idw"
	Dim oTemplate As DrawingDocument = ThisApplication.Documents.Open(sTemplate, False)
	'copy BorderDefinitions from template to this drawing
	Dim oNewBDef As BorderDefinition = Nothing
	Dim oBDefNames() As String = {"A0", "A1", "A2", "A3", "A4"}
	For Each oTempBDef As BorderDefinition In oTemplate.BorderDefinitions
		For Each sBDefName As String In oBDefNames
			If oTempBDef.Name = sBDefName Then
				If sBDefName = sOriginalBorderName Then
					oNewBDef = oTempBDef.CopyTo(oDrawingDoc)
				Else
					oTempBDef.CopyTo(oDrawingDoc)
				End If
			End If
		Next sBDefName
	Next oTempBDef
	'copy TitleBlockDefinitions from template to this drawing
	Dim oNewTBDef As TitleBlockDefinition = Nothing
	For Each oTempTBDef As TitleBlockDefinition In oTemplate.TitleBlockDefinitions
		If oTempTBDef.Name = sOriginalTitleBlockName Then
			oNewTBDef = oTempTBDef.CopyTo(oDrawingDoc)
		End If
	Next oTempTBDef
	oTemplate.Close(True) 'True means skip save
	'add Borders &amp;amp; TitleBlocks back to all sheets
	For Each oSheet As Inventor.Sheet In oSheets
		oSheet.Activate
		Try
			oSheet.AddBorder(oNewBDef)
		Catch
			'what to do if adding Border fails
		End Try
		Try
			oSheet.AddTitleBlock(oNewTBDef)
		Catch
			'what to do if adding TitleBlock fails
		End Try
	Next oSheet
	'MsgBox(oBorderName)
	oActiveSheet.Activate 'activate originally active sheet again
	'MsgBox("test: rule UpdateDrwRecources is executed now")
End Sub&lt;/LI-CODE&gt;
&lt;P&gt;If this solved your problem, or answered your question, please click &lt;SPAN&gt;&lt;STRONG&gt; ACCEPT SOLUTION &lt;/STRONG&gt;&lt;/SPAN&gt;.&lt;BR /&gt;Or, if this helped you, please click (LIKE or KUDOS) &lt;img class="lia-deferred-image lia-image-emoji" src="https://forums.autodesk.com/html/@7401B55A0A518861312A0F851CD29320/emoticons/1f44d.png" alt=":thumbs_up:" title=":thumbs_up:" /&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Nov 2024 16:55:43 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-rule-called-from-within-add-in-is-not-working-completely/m-p/13160696#M174105</guid>
      <dc:creator>WCrihfield</dc:creator>
      <dc:date>2024-11-19T16:55:43Z</dc:date>
    </item>
    <item>
      <title>Re: ilogic rule called from within add-in is not working completely</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-rule-called-from-within-add-in-is-not-working-completely/m-p/13165006#M174171</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/7812054"&gt;@WCrihfield&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much for your good explanation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your code.&lt;/P&gt;&lt;P&gt;Unfortunately the rule cannot delete all the borders.&lt;/P&gt;&lt;P&gt;&amp;nbsp;in the above code it says:&lt;/P&gt;&lt;PRE&gt;'delete all unreferenced BorderDefinitions
	For Each oBDef As BorderDefinition In oDrawingDoc.BorderDefinitions
		If (Not oBDef.IsDefault) AndAlso (Not oBDef.IsReferenced) Then
			oBDef.Delete()
		End If
	Next oBDef&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem seems to be that those borders are "referenced" and therefore cannot be deleted.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Jakob_Hamburg_0-1732179795762.png" style="width: 934px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1436756iC03AAC84C4B2DA8F/image-dimensions/934x55?v=v2" width="934" height="55" role="button" title="Jakob_Hamburg_0-1732179795762.png" alt="Jakob_Hamburg_0-1732179795762.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do not quite understand, why those borders are referenced since they are not on the sheets (because your code deletes the borders on the sheets in line 31).&lt;/P&gt;&lt;P&gt;Is it, that Inventor has not realized that they have been deleted and that is why they are still referenced?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(&lt;SPAN&gt;Autodesk help says: A border definition is referenced whenever it is used on a sheet. A referenced border definition cannot be deleted.&lt;/SPAN&gt;)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since the rule cannot delete the borders it adds "copy of..".&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Jakob_Hamburg_1-1732180497088.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1436762i3CC272BADC670EAF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Jakob_Hamburg_1-1732180497088.png" alt="Jakob_Hamburg_1-1732180497088.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you know how to delete the initial borders in the drawing recourses?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2024 09:16:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-rule-called-from-within-add-in-is-not-working-completely/m-p/13165006#M174171</guid>
      <dc:creator>Jakob_Hamburg</dc:creator>
      <dc:date>2024-11-21T09:16:31Z</dc:date>
    </item>
    <item>
      <title>Re: ilogic rule called from within add-in is not working completely</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-rule-called-from-within-add-in-is-not-working-completely/m-p/13165057#M174172</link>
      <description>&lt;P&gt;sorry..I think your code is fine.&lt;/P&gt;&lt;P&gt;I see why the borders are referenced. It is because my drawing template has sheet formats in it.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Jakob_Hamburg_2-1732181936712.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1436778i5916DB8E56458D5D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Jakob_Hamburg_2-1732181936712.png" alt="Jakob_Hamburg_2-1732181936712.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Those somehow keep the borders referenced.&lt;/P&gt;&lt;P&gt;I should get those deleted first. (But have to figure out why they are there in the first place.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I added a piece of code in your code above (after line 33). now it is working.&lt;/P&gt;&lt;P&gt;I added:&lt;/P&gt;&lt;PRE&gt;	&lt;SPAN&gt;'delete all sheet formats&lt;/SPAN&gt;
		&lt;SPAN&gt;For&lt;/SPAN&gt; &lt;SPAN&gt;Each&lt;/SPAN&gt; &lt;SPAN&gt;oSFDef&lt;/SPAN&gt; &lt;SPAN&gt;As&lt;/SPAN&gt; &lt;SPAN&gt;SheetFormat&lt;/SPAN&gt; &lt;SPAN&gt;In&lt;/SPAN&gt; &lt;SPAN&gt;oDrawingDoc&lt;/SPAN&gt;.&lt;SPAN&gt;SheetFormats&lt;/SPAN&gt;
   			 &lt;SPAN&gt;oSFDef&lt;/SPAN&gt;.&lt;SPAN&gt;Delete&lt;/SPAN&gt;
		&lt;SPAN&gt;Next&lt;/SPAN&gt;
	
	&lt;SPAN&gt;'delete all unreferenced TitleBlockDefinitions&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2024 10:33:01 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-rule-called-from-within-add-in-is-not-working-completely/m-p/13165057#M174172</guid>
      <dc:creator>Jakob_Hamburg</dc:creator>
      <dc:date>2024-11-21T10:33:01Z</dc:date>
    </item>
  </channel>
</rss>

