Convert code to iLogic - delete selected pattern

Convert code to iLogic - delete selected pattern

tomislav.peran
Advocate Advocate
647 Views
9 Replies
Message 1 of 10

Convert code to iLogic - delete selected pattern

tomislav.peran
Advocate
Advocate

Hello,

 

This is a continuation of one of my previous posts (https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/delete-selected-component/m-p/114407...).

 

Would it be possible to convert the following code to iLogic? I do not understand what is going on when I see code like this separated into two parts...

 

Sub Main	

MessageBox.Show("Select parent of the pattern", "Delete pattern")

here :	

If ThisApplication.ActiveDocumentType <> kAssemblyDocumentObject Then MessageBox.Show("This rule is designed to only work in assembly documents.", "Wrong Document Type") : Exit Sub	

Dim PickLeafOC As ComponentOccurrence = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select a Component to Delete")	

If IsNothing(PickLeafOC) Then Exit Sub' If nothing gets selected then we're done	
	
oObjToDelete = GetAssyLevelItemToDelete(PickLeafOC) 

oObjToDelete.Delete 

Try
PickLeafOC.Delete
Catch 
End Try

GoTo here	

End Sub

Function GetAssyLevelItemToDelete(oOcc As Object) As Object 
	
If oOcc.IsPatternElement 
	
oOcc = oOcc.PatternElement.Parent 

oOcc = GetAssyLevelItemToDelete(oOcc) 

End If 
Return oOcc 
End Function

Tom 

0 Likes
Accepted solutions (1)
648 Views
9 Replies
Replies (9)
Message 2 of 10

WCrihfield
Mentor
Mentor
Accepted solution

Hi @tomislav.peran.  The code you posted appears to already be in iLogic.  It consists of two routines for efficiency, to avoid having the code within the second routine present in the main routine an unknown multiple of times.  The second routine there appears to be a 'recursive' routine.  That means that it will continue to run itself as many times as needed to get to its goal (the correct value/object to return to the 'calling' routine).  If the component you 'Pick' is within a pattern, or within a pattern of a pattern of a pattern, it will be a ComponentPatternElement object, and that routine will attempt to get the parent of the parent of that pattern element, however many levels it takes to get there, and return that object to the main routine.  Is it not working OK for you?  Do you need it to work differently?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 10

WCrihfield
Mentor
Mentor

Here is something a bit simpler, and in one routine that you can try out.  It checks if the component you picked is a pattern element, then if so it simply attempts to make that pattern element 'Independent' first, then attempts to delete it.  Then if it is not a pattern element, it just tries to delete it.  Although this may seem short and simple, it is still likely not fool proof for every possible scenario.

 

Sub Main
	here :	
	If ThisApplication.ActiveDocumentType <> kAssemblyDocumentObject Then MessageBox.Show("This rule is designed to only work in assembly documents.", "Wrong Document Type") : Exit Sub	
	Dim PickedOcc As ComponentOccurrence = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select a Component to Delete")	
	If IsNothing(PickedOcc) Then Exit Sub' If nothing gets selected then we're done	
	If PickedOcc.IsPatternElement Then
		Try : PickedOcc.PatternElement.Independent = True : Catch : End Try
		Try : PickedOcc.Delete : Catch : End Try
	Else 'it is not a pattern element
		Try : PickedOcc.Delete : Catch : End Try
	End If
	GoTo here
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 10

tomislav.peran
Advocate
Advocate

Hi WChrihfield,

 

The code works well except when you click on a component in a pattern that is not a "parent component". The parent component will be deleted only when you click on it first. That is why I added MessageBox and "Try Catch End Try"

 

Is it possible to adjust that?

 

I have tried it myself but I got stuck figuring out how this code with two parts work. Thank you for your explanation. I will look more into this "calling" and "recursive" routine. Now I know what it is called and what is idea about it. 

0 Likes
Message 5 of 10

tomislav.peran
Advocate
Advocate

I tried your code. It deletes an element that is in the pattern. But idea is to delete the whole pattern at once including the parent. 

 

The first code does that nicely if a parent is clicked.

 

But more important was to understand what is going on in the code and why are there 2 parts in it. 

 

0 Likes
Message 6 of 10

WCrihfield
Mentor
Mentor

Sorry about that.  I did not fully understand the design intent of this code.  So, if the component you pick is an element in a pattern, you want that element's parent pattern to be deleted, correct?  What if the component you pick is 2, 3, or more levels deep (element of a pattern of a pattern of a pattern)?  Do you want to delete the entire structure of all parent patterns, all the way to the top, or just the immediate parent pattern (first level up from picked)?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 10

tomislav.peran
Advocate
Advocate

Idea is to delete everything. But I would also love to see how would you delete the immediate level if you have a pattern in a pattern. Just out of curiosity even though I never have that case 😄 

 

0 Likes
Message 8 of 10

WCrihfield
Mentor
Mentor

One more question I forgot to ask.  If deleting all parent levels of a patterned component, do you want the original component/components that were used as the input when making the top level pattern to also be deleted, or left behind?  For instance, normally when I delete a component pattern, it will leave the original component(s) there, but just get rid of the pattern feature itself, as if the pattern had not been created in the first place.  But I believe we have access to those original objects when climbing the object ladder up through the patterns/parents.

It may take take two different versions of the code to produce the two different results.  One would only attempt to delete the immediate parent pattern, and the other version would attempts to go all the way to the top.  If only going one level, that code might be able to stay in one routine, instead of two.  When an unknown number of levels is involved, that is when you need a recursive routine that can continue to call itself an unknown number of times.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 10

tomislav.peran
Advocate
Advocate

I want to delete the original component as well. Absolutely. That is my end goal. To delete the whole pattern and the original component that was used to create the pattern. 

 

I think I understand now a bit more why recursive routines are used...

 

0 Likes
Message 10 of 10

WCrihfield
Mentor
Mentor

I created a few variations of code for this task today, but kept encountering odd problems, keeping my tests from completely succeeding, for some reason.  Tried climbing up from the bottom of the structure (the selected component), and also tried going down from the top, of a main component pattern, to find the component that we selected.  Some success, but not complete success yet.  My example assembly had a component pattern of a part that was three layers deep.  (Created a pattern.  Created a pattern of that previous pattern.  Created a pattern of that whole previous pattern.)  My rule was eliminating large sections of it, but not everything.  If it was just a single pattern, no problem...worked every time.  I may work on it more on Monday, if I remember it.  Have a great weekend.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)