Simple code giving me a error

Simple code giving me a error

AMN3161
Advocate Advocate
473 Views
8 Replies
Message 1 of 9

Simple code giving me a error

AMN3161
Advocate
Advocate

Just to say now, i am very very new to logic and i have been stumbling my way through it for years

 

'

W_1 = ""
W_2 = ""
W_3 = ""
W_4 = ""
W_5 = ""


W_Numbering = "0"


Dim DrawDoc As DrawingDocument = ThisDoc.Document
Dim ActiveSht As Sheet = DrawDoc.ActiveSheet
Dim SketchedSymbolName As String = "Stark_Weld_Symbol"

For Each Sym As SketchedSymbol In ActiveSht.SketchedSymbols
	If Sym.Definition.Name <> SketchedSymbolName Then Continue For
		
	'Get text from first text box
	Dim TxtResult As String = Sym.GetResultText(Sym.Definition.Sketch.TextBoxes(1))
	
	W_Numbering = W_Numbering + 1
	
	"W_" & W_Numbering = TxtResult
	
Next

 

I just want the TextResult to send its data to the properties i created at the beginning of the rule so every time it runs it will use W_1 then next time use W_2

 

I know i am missing something stupid from being novice

0 Likes
474 Views
8 Replies
Replies (8)
Message 2 of 9

jjstr8
Collaborator
Collaborator

You cannot dynamically create the name of a variable.  Here's an example of one way to do it with a List collection.

 

Dim W_List As New List(Of String)
Dim DrawDoc As DrawingDocument = ThisDoc.Document
Dim ActiveSht As Sheet = DrawDoc.ActiveSheet
Dim SketchedSymbolName As String = "Stark_Weld_Symbol"

For Each Sym As SketchedSymbol In ActiveSht.SketchedSymbols
	If Sym.Definition.Name <> SketchedSymbolName Then Continue For
		
	'Get text from first text box
	Dim TxtResult As String = Sym.GetResultText(Sym.Definition.Sketch.TextBoxes(1))
	W_List.Add(TxtResult)
Next

'These are here to print the list to the logger
Logger.Info("Results iterated using For Each")
For Each result As String In W_List
	Logger.Info(result)
Next

Logger.Info(System.Environment.NewLine)
Logger.Info("Results iterated using a For loop")
For i = 0 To W_List.Count - 1
	Logger.Info(W_List(i))
Next

'Both iteration methods produce the same list

 

0 Likes
Message 3 of 9

AMN3161
Advocate
Advocate

So i never used a logger before, i was thinking of maybe using a array to create a bunch of custom iproperties and use the data, then delete the custom iproperties because i know how those function.

 

With a logger, how would i sent that all that data to a excel BOM? If difficult i can just use a array

0 Likes
Message 4 of 9

jjstr8
Collaborator
Collaborator

I used the iLogic Logger function just for demonstration purposes to show what information was captured.  I'm not sure about what your looking to do from a higher level, but if you're processing drawings for a given assembly the workflow may look like this:

  • Iterate through all components in the assembly
    • Open each component's drawing (if it exists)
    • Perform the symbol search from your original post 
    • Output the results

It's the "output results" step that has a few options.  If you're intent on the iProperties route, you would need to add them to the underlying component so they can appear in the assembly's BOM.  You would also have to add the custom iProperties columns to the BOM before exporting it to Excel.  Another possibility is to write the results to a CSV text file that you would open in Excel.  Lastly, you could create an Excel file through code and write the results to the cells in whatever structure you want.

 

0 Likes
Message 5 of 9

WCrihfield
Mentor
Mentor

Hi @AMN3161.  Just an observation based on the original code you posted...

Is the 'W_Numbering' represent a local parameter in your document, or is it just a simple variable that only exists within that rule's code?  If it represents a parameter, then what units to you have it set to (text or numerical)?  If it is just a simple variable that only exists within the code, then you may need to change how you are setting its initial value.  You are setting "0" to it at first, which is a String, then later you are trying to use that variable as if it represented an Integer data type.  It will not work both ways, so if you want to use it as representing an Integer, you need to get rid of the quotation marks around the initial zero value, so it is not understood as a String.  Just wanted to point that little detail out, in case that was one of the main problems you may have been encountering. 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 9

AMN3161
Advocate
Advocate

'W_Numbering is just a local parameter that exist only in the rule. What i am trying to do is have it take the sketch prompt text of "Stark_Weld_Symbol" and send the first instance of the sketch symbols text to W_1 then the next sketch prompt text to W_2 and so on.

 

From there i was have a long list of local parameters with text from the sketch symbols that i can use to export to cells in a excel document or build into a table on the drawing. Not sure what direction i want to go with at the end but right now my goal is have all the text from all the sketch symbols populating either local parameters or maybe iproperties. 

 

 

0 Likes
Message 7 of 9

J-Camper
Advisor
Advisor

@AMN3161,

 

This should set the values to iProperties:

Sub Main

	Dim DrawDoc As DrawingDocument = TryCast(ThisDoc.Document, DrawingDocument)
	If IsNothing(DrawDoc) Then Logger.Debug("Not run in Drawing Document") : Exit Sub
		
	Dim ActiveSht As Sheet = DrawDoc.ActiveSheet
	Dim SketchedSymbolName As String = "Stark_Weld_Symbol"

	Dim prefix As String = "W_"
	Dim Index As Integer = 1
	Dim PropertiesCollection As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap

	For Each Sym As SketchedSymbol In ActiveSht.SketchedSymbols
		If Sym.Definition.Name <> SketchedSymbolName Then Continue For
			
		'Get text from first text box
		Dim TxtResult As String = Sym.GetResultText(Sym.Definition.Sketch.TextBoxes(1))
		'collect a name associated with a value
		PropertiesCollection.Add(prefix & Index, TxtResult)
		'Increment Naming
		Index+=1
		
	Next

	If PropertiesCollection.Count < 1 Then Logger.Debug("Nothing Collected") : Exit Sub
		
	Call iPropertyBatchSetting(DrawDoc, PropertiesCollection)

End Sub

Sub iPropertyBatchSetting(oDoc As Document, propList As NameValueMap)

	Dim customIprops As PropertySet = oDoc.PropertySets("Inventor User Defined Properties")
	
	For i = 1 To propList.Count
		
		Try
			prop = customIprops(propList.Name(i))
			prop.Value = propList.Value(propList.Name(i))
		Catch
			customIprops.Add(propList.Value(propList.Name(i)), propList.Name(i))
		End Try
		
	Next
	
End Sub

I'm using a namevaluemap to  set up the iProperty Name and it's corresponding value.  Then I'm feeding that map to a sub routine i use for setting a group of iProperties.  You could send the information directly to an excel file instead if you wanted.

 

I am not deleting similarly named iProperties, so if a symbol is deleted after the rule is run, then the number of iProperties will be incorrect.  If you would like to add some functionality around this topic I would need more information, so let me know.

 

Let me know if you have any questions or if this is not working as intended.

0 Likes
Message 8 of 9

jjstr8
Collaborator
Collaborator

@AMN3161 :  Just a note on @J-Camper 's post.  The iProperties will be in the drawing files themselves.  If you want to see the results in the/an assembly's BOM after you've processed the drawings, you'll have to modify the code to put the iProperties in the model that the drawing references.  This would likely be the first member in the ReferencedDocuments collection.  I think it's just making the following change:

 

Call iPropertyBatchSetting(DrawDoc.ReferencedDocuments(1), PropertiesCollection)
0 Likes
Message 9 of 9

AMN3161
Advocate
Advocate

@J-Camper and @WCrihfield 

 

I appreciated both of your help but your method was a little too confusing for me to understand but i found my own approach

'User questions

MessageBox.Show("What Excel Document do you want the Weld Symbols in?", "Excel File")

'Open dialog box
Dim oFileDlg1A As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg1A)
oFileDlg1A.Filter = "Excel Files (*.xls;*.xlsx)|*.xls;*.xlsx"
oFileDlg1A.InitialDirectory = oOrigRefName1A
oFileDlg1A.CancelError = True
On Error Resume Next
oFileDlg1A.ShowOpen()
If Err.Number <> 0 Then
Return
ElseIf oFileDlg1A.FileName <> "" Then
selectedfile1A = oFileDlg1A.FileName

End If

MessageBox.Show("You selected: " & selectedfile1A, "iLogic")


LetterStart = InputBox("What column do you want the data in?", "Question", "A")

NumberStart = InputBox("What row do you want the data in?", "Question", "1")

Sheet_Name = InputBox("What is the sheet name?", "Question", "Sheet1")

'collect weld notes



Dim DrawDoc As DrawingDocument = ThisDoc.Document
Dim ActiveSht As Sheet = DrawDoc.ActiveSheet
Dim SketchedSymbolName As String = "Stark_Weld_Symbol"

For Each Sym As SketchedSymbol In ActiveSht.SketchedSymbols
	If Sym.Definition.Name <> SketchedSymbolName Then Continue For
		
	'Get text from first text box
	Dim TxtResult As String = Sym.GetResultText(Sym.Definition.Sketch.TextBoxes(1))
	
	GoExcel.Open(selectedfile1A, "Sheet_Name")

	
	iProperties.Value("Custom", "Weld_Number") = TxtResult

	GoExcel.CellValue(selectedfile1A, Sheet_Name, LetterStart & NumberStart) = iProperties.Value("Custom", "Weld_Number")
		
	NumberStart = NumberStart + 1

	GoExcel.Save

	
Next


 what do you guys think?

Edit: i realize now 

GoExcel.Open(selectedfile1A, "Sheet_Name")

isnt really doing anything but it works to populate the excel for me