Use ilogic to delete all instances of another named ilogic rule from an assembly and its underlying parts?

Use ilogic to delete all instances of another named ilogic rule from an assembly and its underlying parts?

briant.markham
Enthusiast Enthusiast
1,457 Views
6 Replies
Message 1 of 7

Use ilogic to delete all instances of another named ilogic rule from an assembly and its underlying parts?

briant.markham
Enthusiast
Enthusiast

Is there any way to use ilogic or a vba macro to delete all instances of another named ilogic rule from an assembly and its underlying parts? It also be useful for it to be able to delete said rule from associated drawings.

0 Likes
Accepted solutions (1)
1,458 Views
6 Replies
Replies (6)
Message 2 of 7

Curtis_Waguespack
Consultant
Consultant

Hi @briant.markham 

 

You can use this from a top level assembly, and it will look at all the referenced files and delete a rule of the same name in them. 

 

Getting it to find the referencing drawings, is more about knowing the location of the drawings since the models don't know about those files. But you could have something like look at all the files in a specific folder and open them and delete the rule... I'm not sure if that would work for your situations though.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

Class ThisRule

	Shared oRuleName As String
	Shared oDoc As Document
	Shared oList As New ArrayList
	Shared oRules As Object
	Shared iLogicAuto As Object

	Sub Main

		oList.clear
		Logger.Info(" ")
		Logger.Info("RuleName: " & iLogicVb.RuleName)

		iLogicAuto = iLogicVb.Automation
		oDoc = ThisApplication.ActiveDocument
		Call GetList
		If oList.count = 0 Then Exit Sub

		'get user input
		oRuleName = InputListBox("Select rule to delete:", _
			oList, oList.Item(0), "iLogic", "List Of Rules")

		RUSure = MessageBox.Show("Are you sure you want to delete " _
					& oRuleName & " rule from all the files file?", _
					"iLogic", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

		If RUSure = vbNo Then Return 'exit rule

		'delete rule in top assembly
		Call DeleteRule
		oRules = Nothing

		For Each oDoc In oDoc.AllReferencedDocuments
			Call GetList
			Call DeleteRule
			oRules = Nothing
		Next

	End Sub

	Sub GetList

		'get collection of oRules
		oRules = iLogicAuto.Rules(oDoc)
		If (oRules Is Nothing) Then Exit Sub

		For Each oRule In oRules
			oList.add(oRule.name)
		Next

	End Sub

	Sub DeleteRule

		If (oRules Is Nothing) Then Exit Sub
		Logger.Info(oRuleName)
		Logger.Info(oDoc.FullFileName)

		For Each oRule In oRules
			If oRuleName = oRule.Name Then
				Try
					iLogicAuto.DeleteRule(oDoc, oRuleName)
					Logger.Info(oRuleName & _
						" deleted from ...." & Right(oDoc.FullFileName, 24))
				Catch ex As Exception
					Logger.Info(ex.Message)
				End Try
			End If
		Next
	End Sub

End Class

 

EESignature

0 Likes
Message 3 of 7

briant.markham
Enthusiast
Enthusiast

Thanks, that worked almost perfectly. Concerning the drawings they are always located in the same place as the part or assembly file and have the same name except instead of .ipt or .iam they are all .dwg.

0 Likes
Message 4 of 7

WCrihfield
Mentor
Mentor
Accepted solution

Here is another rule you can try.  It will also attempt to delete the rule from the drawing document associated with each of the main assemblies referenced documents.  I left two options for you to specify the name of the rule you want to find & delete.  You can either just use a simple InputBox, and enter the name manually (just un-comment that line of code), or you can have it get all rules, create a list of their names, then present the list for you to choose from, as in the code posted above.  In this code, I assume the first rule name you specify is the one you want to find/delete throughout main assembly and all other documents.

 

 

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule (" & iLogicVb.RuleName & ") to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oAuto As IiLogicAutomation = iLogicVb.Automation
	Dim oRuleName As String 'specify once, then use that for all

	'[ Option 1 - for getting name of rule to find/delete
	'oRuleName = InputBox("Enter Name Of Rule To Delete.", "Rule Name", "")
	']

	'[ Option 2 for getting name of rule to find/delete
	Dim oRuleNames As New List(Of String)
	Dim oARules As IEnumerable = oAuto.Rules(oADoc)
	For Each oRule As iLogicRule In oARules
		oRuleNames.Add(oRule.Name)
	Next
	oRuleName = InputListBox("", oRuleNames, "", "Choose Rule to Delete.", "Rule Names")
	']

	If String.IsNullOrEmpty(oRuleName) Then Exit Sub
	Try
		oAuto.DeleteRule(oADoc, oRuleName)
	Catch
	End Try
	DeleteRuleFromDrawing(oADoc, oRuleName)
	For Each oRefDoc As Document In oADoc.AllReferencedDocuments
		Try
			oAuto.DeleteRule(oRefDoc, oRuleName)
		Catch
		End Try
		DeleteRuleFromDrawing(oRefDoc, oRuleName)
	Next
End Sub

Sub DeleteRuleFromDrawing(oDoc As Document, oRName As String)
	Dim oDrawingFile As String = System.IO.Path.ChangeExtension(oDoc.FullFileName, ".dwg")
	If System.IO.File.Exists(oDrawingFile) Then
		Dim oDDoc As DrawingDocument = ThisApplication.Documents.Open(oDrawingFile, False)
		Try
			iLogicVb.Automation.DeleteRule(oDDoc, oRName)
			oDDoc.Save2(False)
			oDDoc.Close(True)
		Catch
		End Try
	End If
End Sub

 

 

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

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 7

briant.markham
Enthusiast
Enthusiast

Thank you, it worked perfectly.

0 Likes
Message 6 of 7

davis.j
Advocate
Advocate

@WCrihfield 
This rule is very helpful. Do you think it would be possible to export a list rather than delete the embeded ilogic rules? Perhaps an Excel sheet or even a simple Inventor msg popup.

0 Likes
Message 7 of 7

WCrihfield
Mentor
Mentor

Hi @davis.j.  Yes.  I believe that would be possible.  The simplest way to display a list of rule names would be to use an InputListBox  (Link1, Link2), but then just not use the returned/selected object, if any.  So my example will use that to start with.  Writing data out to a file is a whole other list of possible processes, but I'm not sure how you intend to use that data, so I don't know which format would suit your needs best.  And I don't know if you need the code to iterate down through an assembly structure like the example above or not, so I will try to show a reference Function that could be used in various scenarios for a task like this.  This Function will attempt to return the List(Of String) containing the list of 'local' rule names within a supplied document.  Since it is possible for there to be no rules present in the document, and the simple IEnumerable object does not have a Count property to check, I incorporated a Try...Catch block/statement into the Function to avoid the potential error.

Here is the simple example:

Sub Main
	Dim oDoc As Document = ThisDoc.Document
	Dim oLocalRuleNames As List(Of String) = GetListOfLocalRuleNames(oDoc)
	If IsNothing(oLocalRuleNames) OrElse oLocalRuleNames.Count = 0 Then Exit Sub
	oRuleName = InputListBox("", oLocalRuleNames, "", "Local Rule Names")
End Sub

Function GetListOfLocalRuleNames(oDoc As Document) As List(Of String)
	If IsNothing(oDoc) Then Return Nothing
	Dim oRules As IEnumerable = iLogicVb.Automation.Rules(oDoc)
	Dim oRuleNames As New List(Of String)
	Try 'if there were no rules, this would throw an error
		For Each oRule As iLogicRule In oRules
			oRuleNames.Add(oRule.Name)
		Next
	Catch oEx As Exception
		'Logger.Error("No Rules Found." & vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace)
	End Try
	Return oRuleNames
End Function

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes