Delete selected component

Delete selected component

tomislav.peran
Advocate Advocate
662 Views
6 Replies
Message 1 of 7

Delete selected component

tomislav.peran
Advocate
Advocate

Hello,

 

I use the following code to select a certain component (whether in the pattern or not) and suppress it. 

 

	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 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	
	PickLeafOC.Suppress()	
	GoTo here	
	End Sub

 However, I would also like to see the code when the selected component gets deleted.  I thought to replace Suppress with Delete or Delete2 but that does not work.

 

If somebody knows how to do it, please let me know.

 

Tom

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

A.Acheson
Mentor
Mentor

That should work if your in master level of detail. If you check the manual operation this should be possible. Does it return an error message if the delete doesn't happen?

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 7

WCrihfield
Mentor
Mentor
Accepted solution

If the component that you select is an element of a component pattern, and not its source/original/input element, it will throw an error, saying 'parameter is incorrect'.  That happens if the component is a top level simple part.  However, if the pattern is of a sub-assembly, and you select a part within that sub-assembly, it will delete that component from within the patterned sub-assembly, so that all elements in the patter will be missing that part within the sub-assembly.  This is something that you can check for though.  The ComponentOccurrence object has a property called 'IsPatternElement', which returns a Boolean, that you can check.  If it is a pattern element, you can then use the components PatternElement property to get the OccurrencePatternElement object it represents, and work from there.  You can set a OccurrencePatternElement's Independent property to True if you need to.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 7

tomislav.peran
Advocate
Advocate

Hi WCrihfield,

 

You are correct it seems. The code does not work if the component is an element in the pattern.

 

What I do now is select all elements in the pattern and make them independent. Then I use the code to delete the ones I want to be deleted. This pattern is in assembly, not subassembly. 

 

I do not know how to create a code that will do that. That is too complicated for me :D.

 

But thanks anyway, I know where is the problem now.

 

Tom

 

 

 

 

 

 

0 Likes
Message 5 of 7

tomislav.peran
Advocate
Advocate

Hi A.Acheson,

 

Thanks for your answer.

 

Can you please tell me what you mean by "manual operation" ?

 

I get an error message indeed that parameter is not correct like WCrihfield wrote beneath your post.

 

Tom

0 Likes
Message 6 of 7

A.Acheson
Mentor
Mentor

By manual operation I mean set up the conditions you would do this manually by the user and not by code.
Here is a forum post that has delete a pattern method. The below code integrates the pattern recognition function. It checks if what is selected is a member of a pattern (pattern element) if it is then gets the parent (Main pattern) and returns this Object and deletes. 

 

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 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 
PickLeafOC.Delete 
		
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



 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 7 of 7

tomislav.peran
Advocate
Advocate

Hi Acheson,

 

Thank you very much for your answer. 

 

This code deletes the whole pattern but I want to delete only the element that is chosen (clicked). I think the following line is responsible for deleting the whole pattern:

oOcc = oOcc.PatternElement.Parent 

But I might be wrong ofc 🙂

 

I think the only way is to make the chosen component independent first and then delete it.  

 

Tom 

 

 

 

 

0 Likes