Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Arrange sketch symbols

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
snichols
1056 Views, 13 Replies

Arrange sketch symbols

I am working on some drawing automation. I have an ilogic rule that inserts a sketch symbol based on centermarks from the model. This all works fine. However I need a way to arrange these after they are inserted. I have the code below that returns all of the Y values for the sketch symbols. My thought is I need to make a list in ilogic for all of these and compare the values. How would I do this is the question? The model can any number of components located at various elevations as shown in attachment.

 

Dim oDrawDoc As DrawingDocument

oDrawDoc = ThisApplication.ActiveDocument

oDrawDoc.Update

oSheets = oDrawDoc.Sheets

Dim oSheet As Sheet

Dim oViews As DrawingViews

Dim oView As DrawingView

Dim oSymbol As SketchedSymbol

Dim oSymbols As SketchedSymbols

For Each oSheet In oSheets

     For Each oSymbol In oSheet.SketchedSymbols

          If oSymbol.Definition.Name = "label" Then

                Dim oSymbPoint As Point2d
                
                oSymbPoint = oSymbol.Position

'              MsgBox(oSymbPoint.x & " - " & oSymbPoint.y)

                'next steps are:
                'Create a list of all the oSymbPoint.y values and compare them to see if any overlap and how much (oSymbPoint.y +/- 1/16" to check overlap)
                'If any overlap move them and retest to make sure the move didn't cause any others to overlap
                'Keep checking and moving until none overlap

          End If

     Next

Next


'Code below for moving the sketch symbol'                ' Set a reference to the TransientGeometry object.'                Dim oTG As TransientGeometry'                oTG = ThisApplication.TransientGeometry'                '                'Move the sketch symbol if it is too close to another one.'                Dim oNewPoint As Point2d'                oNewPoint = oTG.CreatePoint2d(oSymbPoint.x, oSymbPoint.y + 1) The "+1" would be the amount of overlap'                oSymbol.Position = oNewPoint

 

13 REPLIES 13
Message 2 of 14
JelteDeJong
in reply to: snichols

you can try some thing like this.

' Create the list
Dim theList As List(Of Double) = New List(Of Double)()

For Each oSheet In oSheets

    For Each oSymbol In oSheet.SketchedSymbols
        If oSymbol.Definition.Name = "label" Then
			' Adding the y values to the list
            theList.Add(oSymbol.Position.y)
        End If
    Next
Next

' Here is the list actual sorted
theList = theList.OrderBy(Function(x) x).ToList()

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 14
snichols
in reply to: JelteDeJong

Ok, so I can get the list and sort the list. Now what I need to do is get the specific sketch symbol associated with one of the values in the list. If I wanted the sketch symbol associated with item 3 in the list how would I get that? Is it even possible?

snichols_0-1590497512676.png

The sketched symbols all have the same name "label", so returning the name wouldn't help. It would need to be the occurrence of that symbol.

my code is below:

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
oDrawDoc.Update
oSheets = oDrawDoc.Sheets
Dim oSheet As Sheet
Dim oViews As DrawingViews
Dim oView As DrawingView
Dim oSymbol As SketchedSymbol
Dim oSymbols As SketchedSymbols
Dim oSymbPoint As Point2d
Dim MyArrayList As New ArrayList
'Dim RefKey As Long

For Each oSheet In oSheets
     For Each oSymbol In oSheet.SketchedSymbols
          If oSymbol.Definition.Name = "label" Then                
                oSymbPoint = oSymbol.Position    
                MyArrayList.add(oSymbPoint.y)
          End If
     Next
Next

myArrayList.Sort
Elevations = myArrayList.Count
Iterations = (Elevations-1)
test = InputListBox("Select one.", myArrayList, myArrayList.item(0), "iLogic", "List")
'RefKey = myArrayList.item(0).ReferenceKey 'This does not work'test2 = MessageBox.Show(RefKey, "Symbol Reference")

 

Message 4 of 14
JhoelForshav
in reply to: snichols

Hi @snichols 

I rewrote your rule a little to use a dictionary(of double, SketchedSymbol) instead. Using that one your selection knows which symbol it's referring to. Try this ilogic rule. Notice how the SketchedSymbol related to the selected Y-position from the inputbox gets selected on the drawing 🙂

 

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
oDrawDoc.Update
oSheets = oDrawDoc.Sheets
Dim oSheet As Sheet
Dim oViews As DrawingViews
Dim oView As DrawingView
Dim oSymbol As SketchedSymbol
Dim oSymbols As SketchedSymbols
Dim oSymbPoint As Point2d

Dim oDict As New Dictionary(Of Double, SketchedSymbol)

For Each oSheet In oSheets
     For Each oSymbol In oSheet.SketchedSymbols
          If oSymbol.Definition.Name = "label" Then                
                oSymbPoint = oSymbol.Position
				oDict.Add(oSymbPoint.Y, oSymbol)
          End If
     Next
Next

Dim sorted = From pair In oDict
             Order By pair.Key
oDict = sorted.ToDictionary(Function(p) p.Key, Function(p) p.Value)
Dim oSelectedSymbol = oDict.Item(InputListBox("Select one.", oDict.Keys, oDict.Keys(0), "iLogic", "List"))

Dim oSelSet As SelectSet = oDrawDoc.SelectSet
oSelSet.Clear
oSelSet.Select(oSelectedSymbol)
Message 5 of 14
snichols
in reply to: JhoelForshav

Thank you for the reply, however I am getting the below error with the code. I'm on Inventor 2017 if that makes a difference. I probably should have stated that from the start. 😞

 

snichols_0-1590503112858.png

 

Message 6 of 14
snichols
in reply to: snichols

Below is my latest code, it doesn't work. It fails at the "Call". I was trying to get the reference key of the symbol.

 

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
oDrawDoc.Update
oSheets = oDrawDoc.Sheets
Dim oSheet As Sheet
Dim oViews As DrawingViews
Dim oView As DrawingView
Dim oSymbol As SketchedSymbol
Dim oSymbols As SketchedSymbols
Dim oSymbPoint As Point2d
Dim MyArrayList As New ArrayList

 Dim nKeyCont As Long
'Calling this method is a must
nKeyCont = oDrawDoc.ReferenceKeyManager.CreateKeyContext
'Get the reference Key from occurrence
Dim oOccRef() As Byte

For Each oSheet In oSheets
     For Each oSymbol In oSheet.SketchedSymbols
          If oSymbol.Definition.Name = "label" Then                
                oSymbPoint = oSymbol.Position
                MyArrayList.add(oSymbPoint.y)
          End If
     Next
Next

myArrayList.Sort
Elevations = myArrayList.Count
Iterations = (Elevations-1)

Dim oString as Object
For i = 0 To Iterations
Try
For Each oString in myArrayList
        If myArrayList.item(i + 1) - myArrayList.item(i) < 0.125 Then
            'Set a reference to the TransientGeometry object.
            Dim oTG As TransientGeometry
            oTG = ThisApplication.TransientGeometry                        
            'Move the sketch symbol if it is too close to another one.
            Dim oNewPoint As Point2d
            oNewPoint = oTG.CreatePoint2d(oSymbPoint.x, myArrayList.item(i) + (0.125 * 2.54))
            'need to specify which symbol to move here, below does not work
            Call myArrayList.item(i + 1).SketchedSymbol.Definition.GetReferenceKey(oOccRef, nKeyCont)
            oSymbol.Position = oNewPoint
            i = i + 1
        End If
Next
Catch
End Try
Next

RuleParametersOutput()
InventorVb.DocumentUpdate()

 What i am trying to accomplish is to move one of the sketch symbols (with code) that are at the same elevation. There are currently 6 sketch symbols named "label", but you can only see 5 in the drawing (see attachment) because the ones at 10'-0" are on top of one another. I need some way to identify the symbol that needs moved but I don't know how to get the symbol from the list item. Anybody have any ideas?

 
 

 

 

Message 7 of 14
JhoelForshav
in reply to: snichols


@snichols wrote:

Thank you for the reply, however I am getting the below error with the code. I'm on Inventor 2017 if that makes a difference. I probably should have stated that from the start. 😞

 

snichols_0-1590503112858.png

 


I've only been able to try it on 2020 and 2018 and unfortunately i connot reproduce the error...

It might help to add a reference to system.linq

AddReference "System.Linq"
Imports System.Linq

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
oDrawDoc.Update
oSheets = oDrawDoc.Sheets
Dim oSheet As Sheet
Dim oViews As DrawingViews
Dim oView As DrawingView
Dim oSymbol As SketchedSymbol
Dim oSymbols As SketchedSymbols
Dim oSymbPoint As Point2d

Dim oDict As New Dictionary(Of Double, SketchedSymbol)

For Each oSheet In oSheets
     For Each oSymbol In oSheet.SketchedSymbols
          If oSymbol.Definition.Name = "label" Then                
                oSymbPoint = oSymbol.Position
				oDict.Add(oSymbPoint.Y, oSymbol)
          End If
     Next
Next

Dim sorted = From pair In oDict
             Order By pair.Key
oDict = sorted.ToDictionary(Function(p) p.Key, Function(p) p.Value)
Dim oSelectedSymbol = oDict.Item(InputListBox("Select one.", oDict.Keys, oDict.Keys(0), "iLogic", "List"))

Dim oSelSet As SelectSet = oDrawDoc.SelectSet
oSelSet.Clear
oSelSet.Select(oSelectedSymbol)

If that doesn't work, maybe @AlexFielder can help? I see this thread is solved:

https://forums.autodesk.com/t5/inventor-customization/using-linq-with-inventor-ilogic/td-p/5136712

But I'm not really sure exactly what was done to make it work.

 

Message 8 of 14
snichols
in reply to: JhoelForshav

I was able to get the rule to run somewhat by adding the import, however when there are 2 or more sketch symbols with the same elevation it throws an error stating an item with the same key has already been added.

 
 
Message 9 of 14
JhoelForshav
in reply to: snichols

@snichols 

I still think the best way to be able to select a symbol from a list is to use a dictionary. I'd suggest using index to prevent getting list items with the same name, like inventor does in the browser tree (Ex: Part1:1, Part1:2, Part 1:3, Part2:1...)

 

Try this iLogic rule to see what i mean 🙂

AddReference "System.Linq"
Imports System.Linq

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
oDrawDoc.Update
oSheets = oDrawDoc.Sheets
Dim oSheet As Sheet
Dim oViews As DrawingViews
Dim oView As DrawingView
Dim oSymbol As SketchedSymbol
Dim oSymbols As SketchedSymbols
Dim oSymbPoint As Point2d

Dim oDict As New Dictionary(Of String, SketchedSymbol)

For Each oSheet In oSheets
	For Each oSymbol In oSheet.SketchedSymbols
		If oSymbol.Definition.Name = "label" Then
			oSymbPoint = oSymbol.Position
			Dim i As Integer = 1
			Dim yString As String = oSymbPoint.Y.ToString & ":" & i
			While oDict.ContainsKey(yString)
				yString = oSymbPoint.Y.ToString & ":" & i
				i += 1
			End While
			oDict.Add(yString, oSymbol)
		End If
	Next
Next

Dim sorted = From pair In oDict
Order By CDbl(pair.Key.Split(":")(0)), CDbl(pair.Key.Split(":")(1)) 'Order by y-pos first, index second
oDict = sorted.ToDictionary(Function(p) p.Key, Function(p) p.Value)
Dim oSelectedSymbol = oDict.Item(InputListBox("Select one.", oDict.Keys, oDict.Keys(0), "iLogic", "List"))

Dim oSelSet As SelectSet = oDrawDoc.SelectSet
oSelSet.Clear
oSelSet.Select(oSelectedSymbol)

 

Message 10 of 14
snichols
in reply to: JhoelForshav

This is great! However I need to automatically select the ones that need moved based on the values in the list. I've never done anything with a dictionary before so I have no clue how to go about it. My thought was something like below but it didn't work. If you could point me in the right direction I would be most appreciative!

 

For Each oDict.Item in oDict.Keys
i = 0
    If oDict.Keys(i + 1).Value < oDict.Keys(i).Value + 0.125 Then
    Dim oSelectedSymbol = oDict.Keys(i + 1)    
    Dim oSelSet As SelectSet = oDrawDoc.SelectSet
    oSelSet.Clear
    oSelSet.Select(oSelectedSymbol)
    
    Dim oTG As TransientGeometry
    oTG = ThisApplication.TransientGeometry                        
    'Move the sketch symbol if it is too close to another one.
    Dim oNewPoint As Point2d
    oNewPoint = oTG.CreatePoint2d(5, oDict.Keys(i).Value + (0.125 * 2.54))
    oSelectedSymbol.Position = oNewPoint
    End If
Next

 

Message 11 of 14
JhoelForshav
in reply to: snichols

@snichols 

In our dictionary the Key is a string based on the Y-position and an index. The value is the aqual SketchedSymbol.

Now that i know what you want, it wasn't really necessary to use a dictionary. A list of all the symbols would be enough. I thought you wanted to be able to select a symbol from an inputlistbox...

 

But now that we've already written the code with a dictionary, lets keep it that way.

I believe this is what you want 🙂

 

AddReference "System.Linq"
Imports System.Linq

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
oDrawDoc.Update
oSheets = oDrawDoc.Sheets
Dim oSheet As Sheet
Dim oViews As DrawingViews
Dim oView As DrawingView
Dim oSymbol As SketchedSymbol
Dim oSymbols As SketchedSymbols
Dim oSymbPoint As Point2d

Dim oDict As New Dictionary(Of String, SketchedSymbol)

For Each oSheet In oSheets
	For Each oSymbol In oSheet.SketchedSymbols
		If oSymbol.Definition.Name = "label" Then
			oSymbPoint = oSymbol.Position
			Dim i As Integer = 1
			Dim yString As String = oSymbPoint.Y.ToString & ":" & i
			While oDict.ContainsKey(yString)
				yString = oSymbPoint.Y.ToString & ":" & i
				i += 1
			End While
			oDict.Add(yString, oSymbol)
		End If
	Next
Next

Dim sorted = From pair In oDict
Order By CDbl(pair.Key.Split(":")(0)), CDbl(pair.Key.Split(":")(1)) 'Order by y-pos first, index second

Dim oTG As TransientGeometry = ThisApplication.TransientGeometry

For i = 0 To oDict.Count - 2
	If oDict.Values(i + 1).Position.Y < oDict.Values(i).Position.Y + 0.125
		'Dim oNewPoint As Point2d = oTG.CreatePoint2d(5, oDict.Values(i).Position.Y + (0.125 * 2.54))
		'If you want them all to have x = 5 then uncomment line above and comment out line below.
		Dim oNewPoint As Point2d = oTG.CreatePoint2d(oDict.Values(i + 1).Position.X, oDict.Values(i).Position.Y + (0.125 * 2.54))
		oDict.Values(i + 1).Position = oNewPoint
	End If
Next
Message 12 of 14
JhoelForshav
in reply to: JhoelForshav

Sorry!

I accidentaly deleted the line that sets oDict to the sorted dictionary!

It should be like this:

AddReference "System.Linq"
Imports System.Linq


Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
oDrawDoc.Update
oSheets = oDrawDoc.Sheets
Dim oSheet As Sheet
Dim oViews As DrawingViews
Dim oView As DrawingView
Dim oSymbol As SketchedSymbol
Dim oSymbols As SketchedSymbols
Dim oSymbPoint As Point2d

Dim oDict As New Dictionary(Of String, SketchedSymbol)

For Each oSheet In oSheets
	For Each oSymbol In oSheet.SketchedSymbols
		If oSymbol.Definition.Name = "label" Then
			oSymbPoint = oSymbol.Position
			Dim i As Integer = 1
			Dim yString As String = oSymbPoint.Y.ToString & ":" & i
			While oDict.ContainsKey(yString)
				yString = oSymbPoint.Y.ToString & ":" & i
				i += 1
			End While
			oDict.Add(yString, oSymbol)
		End If
	Next
Next

Dim sorted = From pair In oDict
Order By CDbl(pair.Key.Split(":")(0)), CDbl(pair.Key.Split(":")(1)) 'Order by y-pos first, index second
oDict = sorted.ToDictionary(Function(p) p.Key, Function(p) p.Value)
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry

For i = 0 To oDict.Count - 2
	If oDict.Values(i + 1).Position.Y < oDict.Values(i).Position.Y + 0.125
		'Dim oNewPoint As Point2d = oTG.CreatePoint2d(5, oDict.Values(i).Position.Y + (0.125 * 2.54))
		'If you want them all to have x = 5 then uncomment line above and comment out line below.
		Dim oNewPoint As Point2d = oTG.CreatePoint2d(oDict.Values(i + 1).Position.X, oDict.Values(i).Position.Y + (0.125 * 2.54))
		oDict.Values(i + 1).Position = oNewPoint
	End If
Next
Message 13 of 14
snichols
in reply to: JhoelForshav

You are THE MAN! That is just perfect! TYVM for the help!

Message 14 of 14
JhoelForshav
in reply to: snichols

Thank you, happy to help! 🙂

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report