Create a List of Sketch Entities

Create a List of Sketch Entities

commonmandan
Contributor Contributor
342 Views
2 Replies
Message 1 of 3

Create a List of Sketch Entities

commonmandan
Contributor
Contributor

I'm having trouble creating a list of sketch entities. I keep getting an "Unable to cast COM object of type 'System.__ComObject' " error. I've tried other things, such as an Object Collection or a generic list. 

In the example below create a new sketch and draw a couple of lines. Run the code and select a line. How do I convert selected sketch entities into a SketchEntitiesEnumerator? The issue that lead to this is that if I create a point or circle, then the oSelectSet count goes to 0, but I still need to reference the lines further in the code. 

 

Private Sub Main
		'In Part document, with sketch activated!

        Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument

        'activate the sketch first
        Dim oActiveEdit As Object = ThisApplication.ActiveEditObject
        'oPartDoc.ComponentDefinition.Sketches

        Dim oPicked As Object = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kSketchCurveFilter, "Select a sketch object")

        Dim oSelectSet As SelectSet = oPartDoc.SelectSet
        oSelectSet.Select(oPicked)

        'Dim oCurve As SketchEntity = Nothing
        AddSelectedMember(oPicked, oSelectSet)
		
		Dim sketchEntities As SketchEntitiesEnumerator
		sketchEntities = oSelectSet
		
		MessageBox.Show(sketchEntities.Count, "Title")


    End Sub



    Private Sub AddSelectedMember(ByVal sketchObject As Object, ByVal SelectSet As SelectSet)

        Select Case sketchObject.Type
            Case ObjectTypeEnum.kSketchLineObject
                Dim oSketchline As SketchLine = sketchObject

                For Each oSketchEntity As Object In oSketchline.EndSketchPoint.AttachedEntities
                    Dim memberFound As Boolean = False
                    For Each SelectedMember As Object In SelectSet

                        If SelectedMember Is oSketchEntity Then
                            memberFound = True
                            Exit For
                        End If

                    Next
                    If memberFound = False Then
                        SelectSet.Select(oSketchEntity)
                        AddSelectedMember(oSketchEntity, SelectSet)
                    End If
                Next
				
				For Each oSketchEntity As Object In oSketchline.StartSketchPoint.AttachedEntities
                    Dim memberFound As Boolean = False
                    For Each SelectedMember As Object In SelectSet

                        If SelectedMember Is oSketchEntity Then
                            memberFound = True
                            Exit For
                        End If

                    Next
                    If memberFound = False Then
                        SelectSet.Select(oSketchEntity)
                        AddSelectedMember(oSketchEntity, SelectSet)
                    End If
                Next


            Case ObjectTypeEnum.kSketchCircleObject

            Case ObjectTypeEnum.kSketchEllipseObject

            Case ObjectTypeEnum.kSketchArcObject
				Dim oSketchArc As SketchArc = sketchObject

                For Each oSketchEntity As Object In oSketchArc.EndSketchPoint.AttachedEntities
                    Dim memberFound As Boolean = False
                    For Each SelectedMember As Object In SelectSet

                        If SelectedMember Is oSketchEntity Then
                            memberFound = True
                            Exit For
                        End If

                    Next
                    If memberFound = False Then
                        SelectSet.Select(oSketchEntity)
                        AddSelectedMember(oSketchEntity, SelectSet)
                    End If
                Next
				
				For Each oSketchEntity As Object In oSketchArc.StartSketchPoint.AttachedEntities
                    Dim memberFound As Boolean = False
                    For Each SelectedMember As Object In SelectSet

                        If SelectedMember Is oSketchEntity Then
                            memberFound = True
                            Exit For
                        End If

                    Next
                    If memberFound = False Then
                        SelectSet.Select(oSketchEntity)
                        AddSelectedMember(oSketchEntity, SelectSet)
                    End If
                Next
			
			Case ObjectTypeEnum.kSketchEllipticalArcObject
                Dim oEllipticalArc As SketchEllipticalArc = sketchObject

                For Each oSketchEntity As Object In oEllipticalArc.EndSketchPoint.AttachedEntities
                    Dim memberFound As Boolean = False
                    For Each SelectedMember As Object In SelectSet

                        If SelectedMember Is oSketchEntity Then
                            memberFound = True
                            Exit For
                        End If

                    Next
                    If memberFound = False Then
                        SelectSet.Select(oSketchEntity)
                        AddSelectedMember(oSketchEntity, SelectSet)
                    End If
                Next
				
				    For Each oSketchEntity As Object In oEllipticalArc.StartSketchPoint.AttachedEntities
                    Dim memberFound As Boolean = False
                    For Each SelectedMember As Object In SelectSet

                        If SelectedMember Is oSketchEntity Then
                            memberFound = True
                            Exit For
                        End If

                    Next
                    If memberFound = False Then
                        SelectSet.Select(oSketchEntity)
                        AddSelectedMember(oSketchEntity, SelectSet)
                    End If
                Next

			Case ObjectTypeEnum.kSketchEquationCurveObject
				Dim oSketchEquation As SketchEquationCurve = sketchObject
				
	            For Each oSketchEntity As Object In oSketchEquation.EndSketchPoint.AttachedEntities
                    Dim memberFound As Boolean = False
                    For Each SelectedMember As Object In SelectSet

                        If SelectedMember Is oSketchEntity Then
                            memberFound = True
                            Exit For
                        End If

                    Next
                    If memberFound = False Then
                        SelectSet.Select(oSketchEntity)
                        AddSelectedMember(oSketchEntity, SelectSet)
                    End If
                Next
				
			    For Each oSketchEntity As Object In oSketchEquation.StartSketchPoint.AttachedEntities
					Dim memberFound As Boolean = False
					For Each SelectedMember As Object In SelectSet
	
						If SelectedMember Is oSketchEntity Then
							memberFound = True
							Exit For
						End If
	
					Next
					If memberFound = False Then
						SelectSet.Select(oSketchEntity)
						AddSelectedMember(oSketchEntity, SelectSet)
					End If
				Next
				
			Case ObjectTypeEnum.kSketchSplineObject
                Dim oSpline As SketchSpline = sketchObject

                For Each oSketchEntity As Object In oSpline.EndSketchPoint.AttachedEntities
                    Dim memberFound As Boolean = False
                    For Each SelectedMember As Object In SelectSet

                        If SelectedMember Is oSketchEntity Then
                            memberFound = True
                            Exit For
                        End If

                    Next
                    If memberFound = False Then
                        SelectSet.Select(oSketchEntity)
                        AddSelectedMember(oSketchEntity, SelectSet)
                    End If
                Next
				
				For Each oSketchEntity As Object In oSpline.StartSketchPoint.AttachedEntities
                    Dim memberFound As Boolean = False
                    For Each SelectedMember As Object In SelectSet

                        If SelectedMember Is oSketchEntity Then
                            memberFound = True
                            Exit For
                        End If

                    Next
                    If memberFound = False Then
                        SelectSet.Select(oSketchEntity)
                        AddSelectedMember(oSketchEntity, SelectSet)
                    End If
                Next

			Case ObjectTypeEnum.kSketchControlPointSplineObject
                Dim oCtrlPointSpline As SketchControlPointSpline = sketchObject

                For Each oSketchEntity As Object In oCtrlPointSpline.EndSketchPoint.AttachedEntities
                    Dim memberFound As Boolean = False
                    For Each SelectedMember As Object In SelectSet

                        If SelectedMember Is oSketchEntity Then
                            memberFound = True
                            Exit For
                        End If

                    Next
                    If memberFound = False Then
                        SelectSet.Select(oSketchEntity)
                        AddSelectedMember(oSketchEntity, SelectSet)
                    End If
                Next
				
				For Each oSketchEntity As Object In oCtrlPointSpline.StartSketchPoint.AttachedEntities
                    Dim memberFound As Boolean = False
                    For Each SelectedMember As Object In SelectSet

                        If SelectedMember Is oSketchEntity Then
                            memberFound = True
                            Exit For
                        End If

                    Next
                    If memberFound = False Then
                        SelectSet.Select(oSketchEntity)
                        AddSelectedMember(oSketchEntity, SelectSet)
                    End If
                Next

			Case ObjectTypeEnum.kSketchOffsetSplineObject
                Dim oOffsetSpline As SketchOffsetSpline = sketchObject

                For Each oSketchEntity As Object In oOffsetSpline.EndSketchPoint.AttachedEntities
                    Dim memberFound As Boolean = False
                    For Each SelectedMember As Object In SelectSet

                        If SelectedMember Is oSketchEntity Then
                            memberFound = True
                            Exit For
                        End If

                    Next
                    If memberFound = False Then
                        SelectSet.Select(oSketchEntity)
                        AddSelectedMember(oSketchEntity, SelectSet)
                    End If
                Next
				
				For Each oSketchEntity As Object In oOffsetSpline.StartSketchPoint.AttachedEntities
                    Dim memberFound As Boolean = False
                    For Each SelectedMember As Object In SelectSet

                        If SelectedMember Is oSketchEntity Then
                            memberFound = True
                            Exit For
                        End If

                    Next
                    If memberFound = False Then
                        SelectSet.Select(oSketchEntity)
                        AddSelectedMember(oSketchEntity, SelectSet)
                    End If
                Next

			
			
            Case Else
				MessageBox.Show("Unhandled type: " + sketchObject.Type, "Other type") 'should never happen...


        End Select
    End Sub

 

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

WCrihfield
Mentor
Mentor
Accepted solution

Hi @commonmandan.  I am not sure what you end goal was with all of that code, but you can not actually create a new SketchEntitiesEnumerator, nor can you add or subtract SketchEntity objects to/from it.  That is the object type returned by the main Sketch object's SketchEntities property.  Are you simply trying to select all sketch geometry that is 'attached' to the selected entity?  I would generally prefer to use a List(Of SketchEntity) type variable in a situation like that, instead.  I believe that one of your main problems is that you are using a variable name that is spelled exactly the same as the Type of object it is to represent, and also has the same capitalization.  That is a really bad coding practice, and often causes all sorts of problems.  In this case, you are using SelectSet as the variable name, to represent a SelectSet Type object within your custom routine (bad), while using oSelectSet in your 'Main' routine (OK).  The variable must always either be spelled differently, or be capitalized differently, or include extra characters, so that it is different than the Type it represents.  Also, it looks like your custom routine just keeps calling itself again, which does not seem very efficient for a task like this.  Maybe it would be better to use a Function, instead of a Sub, then set it up to 'Return' a List(Of SketchEntities) type object when it is done.  It could create its own List at its beginning, then add each entity to it, as they are found, then return that list, instead of calling itself over and over again.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 3

commonmandan
Contributor
Contributor
Accepted solution

That was a poor example because of how much code there is. New List(Of SketchEntity) is what I needed though. Here is a better example for anyone else who finds this in the future.

 

' iLogic Script (List_Of_Entities.vb)
'Create a new ipt, then create a sketch. Place a line, cicle, and arc. With the sketch active, run this script.
Public Sub Main()
	Dim oDrawDoc As PartDocument
    oDrawDoc = ThisDoc.Document
	
	Dim oSketches As PlanarSketches
	oSketches = oDrawDoc.ComponentDefinition.Sketches
	
	Dim oSketch As PlanarSketch
	If TypeOf ThisApplication.ActiveEditObject Is Sketch Then
		oSketch = ThisApplication.ActiveEditObject
	Else
		MessageBox.Show("Need to be in an Active Sketch.", "Title")
		Exit Sub
	End If
	
	' This is the key part of the code, as opposed to SketchLines, SketchCircles, etc.
	Dim oList As New List(Of SketchEntity)

	oList.Add(oSketch.SketchLines.Item(1))
	oList.Add(oSketch.SketchCircles.Item(1))
	oList.Add(oSketch.SketchArcs.Item(1))
	
	MessageBox.Show(oList.Count, "Title")

End Sub
0 Likes