by Luke Davenport
I couldn’t resist this challenge – On a recent training course an Inventor user confided the following Inventor problem. For each large assembly drawing he was having to manually place dozens of drawing symbols, numbered from 1 to XX (depending on the size of the assembly) in order to identify each component in a large rectangular layout. Identical components needed a separate designation, and leaders weren’t required, so balloons weren’t an option. Anyway, you won’t have to have read many of my blogs to know that manual repetition makes me angry (kind of like a nerdy CAD hulk) and this prompted a crash course in coding skills to write a little iLogic routine. Check out my video showing it in action, and you can copy and paste the entire code from below.
Just to mention – one limitation of the code is that the view scale recognition will only work for first-view drawing scales between 1:1 and 1:99 – shouldn’t be a huge problem.
itrigger = iTrigger0
' This is the code taken straight from the Being Inventive Blog to find the
‘ view scale
Dim odrawdoc As DrawingDocument
odrawdoc = ThisApplication.ActiveDocument
customPropertySet = odrawdoc.PropertySets.Item("Inventor User Defined Properties")
For i = 1 To odrawdoc.Sheets.Count
' Make sure the desired property exists
Try
prop = customPropertySet.Item("Scale" + Str(i))
Catch
' Assume error means not found
customPropertySet.Add("", "Scale" + Str(i))
End Try
Try
iProperties.Value("Custom", "Scale" + Str(i)) = odrawdoc.sheets.item(i).DrawingViews.Item(1).ScaleString
Catch
End Try
Next i
InventorVb.DocumentUpdate()
'This section of code finds the highest symbol already placed
'[
' Set a reference to the drawing document.
' This assumes a drawing document is active.
oSymbols = oDrawDoc.ActiveSheet.SketchedSymbols
For Each oSymbol In oSymbols 'Find highest placed symbol (assumes the last placed symbol is the highest)
If Left(oSymbol.Name,7) = "CADline" Then 'Only concerned with CADline symbols
Dim symbol_number As String = Right(oSymbol.Name,3) 'Find RHS 3 digits (as string)
Dim InputString As String = symbol_number 'Convert to number
Dim Result As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(InputString, "\d+")
symbol_number_int =(Result.Value)
symbol_number_int_plus_one = symbol_number_int + 1 'Create variable for next symbol to place
symbol_number_int_plus_one_str = CStr(symbol_number_int_plus_one) 'Make this variable a string
End If
Next
']
'This section of code allows user to decide starting value for symbols
'[
If symbol_number_int = "" Then 'If there are no symbols placed already
symbol_number_int = 0
start_value = 1
Else
'Allow user to select starting value for pattern
iftruestartfrom1 = InputRadioBox("Starting Number?", "1", symbol_number_int_plus_one & " (Symbols 1-" & symbol_number_int & " are already placed)", iftruestartfrom1, Title := "Select Starting Number")
If iftruestartfrom1 = True Then
start_value = 1
Else
start_value = symbol_number_int_plus_one
End If
End If
']
'This section of code creates pattern variables using using input
'[
'Allow user to enter pattern values
Dim Row_Qty As Double = InputBox("No. of Rows?", "Pattern Values", Row_Qty)
Dim Col_Qty As Double = InputBox("No. of Columns?", "Pattern Values", Col_Qty)
Dim Col_Spacing As Double = InputBox("Column Spacing?", "Pattern Values", Col_Spacing)
Dim Row_Spacing As Double = InputBox("Row Spacing?", "Pattern Values", Row_Spacing)
Dim View_Scale_Fraction As String 'Get scale of first drawing view
View_Scale_Fraction = iProperties.Value("Custom", "Scale 1") 'Create iProperty with this value
View_Scale = Right(View_Scale_Fraction,2) 'Get the RHS of scale value
View_Scale_Num = Val(View_Scale) 'Convert to value
Scale_Actual = 1/View_Scale_Num 'Create conversion factor to multiply pattern values
Col_Spacing_Actual = (Col_Spacing*Scale_Actual)/10 'Set correct column spacing based on view scale
Row_Spacing_Actual = (Row_Spacing*Scale_Actual)/10 'Set correct row spacing based on view scale
']
'This section of code places sketch symbols
'[
Dim oSketchedSymbolDef As SketchedSymbolDefinition
Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry
i_y = 1 'Set variables for x and y positions of pattern
i_x = 1
i_count = 0
For i_y = 1 To Row_Qty 'Create loop for rows
y_pos = i_y * Row_Spacing_Actual ' Set row spacing increment
For i_x = 1 To Col_Qty 'Create loop for columns
x_pos = i_x * Col_Spacing_Actual 'Set column spacing increment
Try 'the following code creates a new sketched symbol definition, assuming an error here means it already exists.
oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Add("CADline_Callout" & (start_value + i_count))
' Open the sketched symbol definition's sketch for edit. This is done by calling the Edit
' method of the SketchedSymbolDefinition to obtain a DrawingSketch. This actually creates
' a copy of the sketched symbol definition's and opens it for edit.
Dim oSketch As DrawingSketch
Call oSketchedSymbolDef.Edit(oSketch)
Dim oSketchCircle As SketchCircle
If (start_value + i_count) < 100 Then 'Place circle in sketch definition (with larger size if value it contains is larger than 99)
oSketchCircle = oSketch.SketchCircles.AddByCenterRadius(oTG.CreatePoint2d(20, 20), 0.5)
Else
oSketchCircle = oSketch.SketchCircles.AddByCenterRadius(oTG.CreatePoint2d(20, 20), 0.7)
End If
Dim sText As String 'Create string to put the symbol value into
sText = (start_value + i_count)
If (start_value + i_count) < 10 Then ' Change position of placed sketch text depending on whether it is 1, 2 or 3 digits.
oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(19.82, 20.25), sText)
Else If (start_value + i_count) < 100 Then
oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(19.60, 20.25), sText)
Else
oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(19.40, 20.25), sText)
End If
oStyle = oTextBox.Style 'Set properties of sketch text
oStyle.Font = "Tahoma"
oStyle.FontSize = 0.5
oSketchedSymbolDef.ExitEdit 'Exit sketch symbol edit
Catch 'Do nothing if there was an error with the above
End Try
' Obtain a reference to the desired sketched symbol definition.
oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item(("CADline_Callout" & (start_value + i_count)))
'
Dim oSheet As Sheet
oSheet = oDrawDoc.ActiveSheet
'
oATG = ThisApplication.TransientGeometry
'
Dim oSketchedSymbol As SketchedSymbol 'Set position of first placed sketched symbol
oSketchedSymbol = oSheet.SketchedSymbols.Add(oSketchedSymbolDef, oATG.CreatePoint2d((20 + x_pos), (40 - y_pos)), (0), 1,)
i_count = i_count+1
Next
Next
']
Comments
0 comments
Please sign in to leave a comment.