delete a collection of components from an assembly

delete a collection of components from an assembly

tmathieson
Advocate Advocate
249 Views
2 Replies
Message 1 of 3

delete a collection of components from an assembly

tmathieson
Advocate
Advocate

Hello All. Again,  i am in deed of some programming assistance.  i am writing code to delete a bunch of components from an assembly based on certain criteria.  the code is working but it takes a while.  for example, 30 seconds to delete 60 parts.  however, if i choose the same components/parts interactively by a window, they delete in less than 10 seconds.  so  i thought that maybe by making a collection set, like for a pattern for example, i could delete them all at once and it would be quicker... however, i am stumbling on deleting the collection.is this possible?  i assume there must be some way to delete all at once but....

 

Dim DELETE_THESE As ObjectCollection = oTO.CreateObjectCollection
Dim oObjects_R As ObjectCollection = oTO.CreateObjectCollection

	If  InStr(CLEAR_BOX_NAME, "FLAT-CH") Or InStr(CLEAR_BOX_NAME, "-RA-")Then 'DELETE CHANNELS FOR S02
		Logger.Debug("CHANNEL" & CLEAR_BOX_NAME)
		DELETE_THESE.Add(oCc23.Name)
	End If
			If oCc23.ActiveModelState = "NO_HOLES" Then
			'Logger.Debug("let's move..." & oOcc.Name)
			DELETE_THESE.Add(oCc23.Name)  
			End If
	If InStr(CLEAR_BOX_NAME, "BC") >0 Then
Logger.Debug ("let delete it")
	End If
Next
HOW_MANY = DELETE_THESE.Count
Logger.Debug("WE ARE DELETEING..." & HOW_MANY)
'For Each occName In DELETE_THESE
'	Components.Delete(occName)
'Next
Components.Delete(DELETE_THESE)

 as always, any help/suggestions are gladly appreciated!

0 Likes
Accepted solutions (1)
250 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor
Accepted solution

Hi @tmathieson.  Although there are several methods for dealing with collections of components at one time, I do not believe there is a built-in method for deleting a collection of components at one time.  However, there is still a pretty simple way to do it.  You will want to 'clear' the assembly's SelectSet first.  Then use SelectSet.SelectMultiple method to add your ObjectCollection to it.  Then execute the "AppDeleteCmd" command.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 3

tmathieson
Advocate
Advocate

Hey @WCrihfield , thanks !  this got me on the correct track.  had looked at selectset but couldn't figure out how to declare/initialize it. with your guidance, and digging around some more, it is now performing! have included the updated code here in case it might be useful to other...

 

Dim DELETE_THESE As ObjectCollection  = oTO.CreateObjectCollection
Dim oObjects_R As ObjectCollection = oTO.CreateObjectCollection
For Each oCc23 As ComponentOccurrence In oCD.Occurrences
	'Dim doc As Document = oCc.Definition.Document
	CLEAR_BOX_NAME = oCc23.Name
'	LOGGER.Debug(CLEAR_BOX_NAME)
	If InStr(CLEAR_BOX_NAME, PROJECT_NUMBER) >0 And InStr(CLEAR_BOX_NAME, "FLAT") Or InStr(CLEAR_BOX_NAME, "SPLICE") >0 Or _ 
		InStr(CLEAR_BOX_NAME, "BC") >0 Then 'Or InStr(CLEAR_BOX_NAME, "-S") >0 
		Dim oPartdoc As ComponentOccurrence
        oPartdoc = oCc23
		DELETE_THESE.Add(oPartdoc) 
		Logger.Debug("OTHER..." & CLEAR_BOX_NAME)
		Continue For
	End If
	If  InStr(CLEAR_BOX_NAME, "FLAT-CH") Or InStr(CLEAR_BOX_NAME, "-RA-")Then 'DELETE CHANNELS FOR S02
		Logger.Debug("CHANNEL" & CLEAR_BOX_NAME)
		Dim oPartdoc As ComponentOccurrence
        oPartdoc = oCc23
		DELETE_THESE.Add(oPartdoc)
		'DELETE_THESE.Add(oCc23.Name)
	End If
			If oCc23.ActiveModelState = "NO_HOLES" Then
		Dim oPartdoc As ComponentOccurrence
        oPartdoc = oCc23
		DELETE_THESE.Add(oPartdoc)
			End If
Next
HOW_MANY = DELETE_THESE.Count
Logger.Debug("WE ARE DELETEING..." & HOW_MANY)
Try
AssyDoc.SelectSet.Clear
Catch ex As Exception
	Logger.Debug("set not cleared")
End Try
AssyDoc.SelectSet.SelectMultiple(DELETE_THESE)
ThisApplication.CommandManager.ControlDefinitions.Item("AppDeleteCmd").Execute
DELETE_THESE.Clear
AssyDoc.Save
End Sub

 again, thanks for your help!  saved me from pulling more hair out :+) !

0 Likes