Doesn't delete oSymbol in for each loop

Doesn't delete oSymbol in for each loop

Anonymous
Not applicable
614 Views
6 Replies
Message 1 of 7

Doesn't delete oSymbol in for each loop

Anonymous
Not applicable

Hi,

 

In my iLogic rule in a drawing, I need to delete 25 sketch symbols if the boolean from the assembly is false.  Only if I run the rule the sketch symbols will not be deleted.

I guess that the string test can't deleted the SketchSymbol as the rule counts up with 1.

 

I hope that own of you can help, thanks in advance.

Sub Main() 
	''Path to document file
	doc =  IO.Path.GetFileName(ThisDrawing.ModelDocument.FullFileName)
	Dim oSketchedSymbolDef As SketchedSymbolDefinition

	''Searching for a symbol in active sheet
	For Each oSymbol In ThisDrawing.Document.ActiveSheet.SketchedSymbols 
		
		i = i + 1 ''count up i, with plus 1
		If i < 10 Then t = "0" Else t = ""
		
		If i = 26 Then ''exit loop if more then 25 stops
			Exit For
		End If		
		''MsgBox(i & "    -    " & t)
		
		Dim test As String
		test = ("StopNummerPeilmaat" & i)
		
		If Parameter(doc, ("FFrontDoor_" & t & i)) = "False" Then	
			''If name symbol is True/Flase then delete sketchsymbol ;o
			If oSymbol.Name = test Then 
				oSymbol.Delete
			End If
		End If 

		If Parameter(doc, "FBackDoor_" & t & i) = "False" Then	
			If oSymbol.Name = test Then
				oSymbol.Delete
			End If
		End If			
	Next 	
End Sub

 

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

WCrihfield
Mentor
Mentor

Since I don't have your files, I couldn't test if this will work for you or not, but try this.

Sub Main()
	Dim oDDoc As DrawingDocument = ThisDrawing.Document
	Dim oMDoc As AssemblyDocument = ThisDrawing.ModelDocument
	Dim oUParams As UserParameters = oMDoc.ComponentDefinition.Parameters.UserParameters
	Dim oSymbols As SketchedSymbols = oDDoc.ActiveSheet.SketchedSymbols
	Dim oSymbol As SketchedSymbol
	Dim i As Integer
	Dim t As String
	Dim oTest As String
	For i = 1 To oSymbols.Count
		If i < 10 Then t = "0" Else t = ""
		oTest = "StopNummerPeilmaat" & i
		If oUParams.Item("FFrontDoor_" & t & i).Value = False Or _
			oUParams.Item("FBackDoor_" & t & i).Value = False Then
			If oSymbols.Item(i).Name = oTest Then
				oSymbols.Item(i).Delete
			End If
		End If
	Next 	
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 7

Anonymous
Not applicable

Thanks for the quick reply.

I tried to run your code but unfortunately, it didn't work. I did the following tweaks:

For  i = 1 To 25
If oSymbols.Item.Name = oTest Then oSymbols.Item.Delete

 

After running the rule with the tweaks I got the following error message.

error message.png

I guess that the problem lies in that oTest can't be compared to the SketchedSymbol

Sub Main()
	Dim oDDoc As DrawingDocument = ThisDrawing.Document
	Dim oMDoc As AssemblyDocument = ThisDrawing.ModelDocument
	Dim oUParams As UserParameters = oMDoc.ComponentDefinition.Parameters.UserParameters
	Dim oSymbols As SketchedSymbols = oDDoc.ActiveSheet.SketchedSymbols
	Dim oSymbol As SketchedSymbol
	Dim i As Integer
	Dim t As String
	Dim oTest As String
	For i = 1 To 25
		If i < 10 Then t = "0" Else t = ""
		oTest = "StopNummerPeilmaat" & i
		If oUParams.Item("FFrontDoor_" & t & i).Value = False Or _
			oUParams.Item("FBackDoor_" & t & i).Value = False Then
			If oSymbols.Item.Name = oTest Then oSymbols.Item.Delete 
		End If
	Next 	
End Sub

 

0 Likes
Message 4 of 7

WCrihfield
Mentor
Mentor
Accepted solution

The way it's written, it is looking for a UserParameter within the ModelDocument (Assembly) named "FFrontDoor_01" (in first loop), so If it's not finding that UserParameter, it will cause a problem.  It is also expecting the UnitsType of that UserParameter to be Boolean (True/False), and not a multi-value Text type parameter filled with values "True" and "False".  If that's not the way the actual UserParameter is set-up, that will cause a problem too.

It is also looking within the current sheet of your active drawing document for a SketchedSymbol (one that is saved locally or currently placed on the sheet, not just in the library), named "StopNummerPeilmaat1" (not "StopNummerPeilmaat 1" or "StopNummerPeilmaat01") durring the first loop.  If there isn't one with that exact name, it will cause a problem.  Are you sure you don't mean to delete the SketchedSymbolDefinition, instead of just the SketchedSymbol?  Are you sure there isn't supposed to be a space, underscore, or 0, between "StopNummerPeilmaat" and the 1?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 7

J-Camper
Advisor
Advisor
Accepted solution

The error message is because you aren't indexing the item [Item("index" )] here:

If oSymbols.Item.Name = oTest Then oSymbols.Item.Delete

Instead one way to get the correct sketchedsymbol to delete, without indexing, you can run a quick loop checked against your oTest variable:

For Each oSymbol In oSymbols
				If oSymbol.Name = oTest Then oSymbol.Delete : Exit For
			Next

 

Message 6 of 7

Anonymous
Not applicable

Thank you both! I learned a lot from your code!

It did the tick to delete sketch symbols.

 

@J-Camper  one thing I'm curious about is how I should index an item [Item("index" )] in my previous line:

If oSymbols.Item.Name = oTest Then oSymbols.Item.Delete

 

For anyone with somewhat the same problem, this is the code that works for me 🙂

Sub Main()
	doc =  IO.Path.GetFileName(ThisDrawing.ModelDocument.FullFileName)
	Dim oDDoc As DrawingDocument = ThisDrawing.Document
	Dim oMDoc As AssemblyDocument = ThisDrawing.ModelDocument
	Dim oSymbols As SketchedSymbols = oDDoc.ActiveSheet.SketchedSymbols
	Dim oSymbol As SketchedSymbol
	
	Dim i = 1
	Dim t As String
	Dim sTest As String
	
For i = 1 To 25
	If i = 26 Then Exit For
	If i < 10 Then t = "0" Else t = ""
		sTest = "StopNummerPeilmaat" & CStr(i)
		If parameter(doc, "FFrontDoor_" & t & i) = False Then False Or _
			parameter(doc, "FBackDoor_" & t & i) = False Then			
				For Each oSymbol In oSymbols
					If oSymbol.Name = sTest Then oSymbol.Delete : Exit For
				Next 	
		End If
	Next 	
End Sub

 

0 Likes
Message 7 of 7

J-Camper
Advisor
Advisor

If you knew the Index Integer Value to pick an object out of an object collection [EX: SketchedSymbol Object out of SketchedSymbols Collection].  Some collections let you search Item with Integer Value or String Name, but Sketched Symbols don't have unique names when placed multiple times on a sheet.  Because of this You can't Identify the Item with the name or else it wouldn't know which one on the sheet to bind to your iLogic Variable. 

 

If you started your main for loop with "For i As Integer = 1 to oSymbols.Count" and then within that check if the name matches a predetermined pattern you could use a final line of "oSymbols.Item(i).Delete"

 

The way your loop is working you are using the main For Loop to set up the name to check and then within that you are deleting the SketchedSymbol that matches this name.  Which then requires a loop through SketchedSymbols to find one that matches the name and deleting it.

 

By the way, it is limited to deleting 1 instance of the matched name Sketched Symbol because of this line:

 

If oSymbol.Name = sTest Then oSymbol.Delete : Exit For

 

If you have multiple instances of the same name and want to delete all of them you can take out the ": Exit For".  The ":" breaks the line into a "second new line" on the same line and this line is just to exit the loop without looking any further.

Can also be written as:

If oSymbol.Name = sTest Then oSymbol.Delete
Exit For

 

 

 

0 Likes