iLogic claims that SketchBlock.Definition.Profiles is Nothing

iLogic claims that SketchBlock.Definition.Profiles is Nothing

KenPalAlt
Contributor Contributor
135 Views
2 Replies
Message 1 of 3

iLogic claims that SketchBlock.Definition.Profiles is Nothing

KenPalAlt
Contributor
Contributor

I have a sketch with 6 sketch blocks.

KenPalAlt_0-1744184013636.png

The blocks are very simple, fully constrained and definitely do contain a profile as they are used in the extrusion (see the leftmost 2 in image).

 

The code accepts a List (of Single) that updates an extrusion to only include as many profiles as there are items in the list.

It also repositions the blocks to the X-value in the list (the blocks are constrained on Y), but that is removed in my example for readability.

 

The issue is that iLogic throws an error when I try to grab the profiles of the blocks. It claims that

oBlock.Definition.Profiles

is nothing.

 

Class MyRule
	Sub Main
		Dim Block_Positions_List As New List(Of Single)
		Block_Positions_List.Add(350)
		Block_Positions_List.Add(2500)
		Block_Positions_List.Add(-1500)
		Call UpdateExtrusion("Cutouts_Sketch", "Cutouts_Extrusion", Block_Positions_List)
	End Sub
	
	' Code to move blocks to specific Y-value removed
	' Extrude only Block_Positions_List.Count profiles
	Sub UpdateExtrusion(ByVal sketchName As String, ByVal extrusionName As String, ByVal Block_Positions_List As List(Of Single))
	
	    Dim oDoc As PartDocument = ThisApplication.ActiveDocument
	    Dim oPart As PartComponentDefinition = oDoc.ComponentDefinition
	    Dim oSketch As PlanarSketch
	    Dim oExtrusion As ExtrudeFeature
		oSketch = oPart.Sketches.Item(sketchName)
		
	    Dim oSketchBlocks As SketchBlocks = oSketch.SketchBlocks
	    Dim numSketchBlocks As Integer = oSketchBlocks.Count
	    Dim numPositions As Integer = Block_Positions_List.Count
	    Dim profilesToExtrude As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
	
	    For i As Integer = 0 To numSketchBlocks - 1
	        Dim oBlock As SketchBlock = oSketchBlocks.Item(i + 1)
	
	        If i < numPositions Then
	            ' **debugging**
	            If oBlock.Definition Is Nothing Then
	                MessageBox.Show("Block " & (i + 1) & ": oBlock.Definition is Nothing.", "iLogic Debug")
	            Else
	                MessageBox.Show("Block " & (i + 1) & ": oBlock.Definition is valid.", "iLogic Debug")
	                If oBlock.Definition.Profiles Is Nothing Then
	                    MessageBox.Show("Block " & (i + 1) & ": oBlock.Definition.Profiles is Nothing.", "iLogic Debug")
	                Else
	                    MessageBox.Show("Block " & (i + 1) & ": oBlock.Definition.Profiles exists.", "iLogic Debug")
	                    If oBlock.Definition.Profiles.Count > 0 Then
	                        MessageBox.Show("Block " & (i + 1) & ": Profiles.Count = " & oBlock.Definition.Profiles.Count, "iLogic Debug")
	                        Try
	                            profilesToExtrude.Add(oBlock.Definition.Profiles.Item(1)) ' Assuming one profile per block
	                        Catch ex As Exception
	                            MessageBox.Show("Error adding profile for block " & (i + 1) & ": " & ex.Message, "iLogic Error")
	                        End Try
	                    Else
	                        MessageBox.Show("Warning: No profiles found for block " & (i + 1) & ". Skipping.", "iLogic Warning")
	                    End If
	                End If
	            End If
	
	        Else
	            ' Code to handle superfluous profiles removed for readability
	        End If
	    Next
	
	    ' **Update the extrusion profiles**
	    Try
	        oExtrusion.Definition.Profiles = profilesToExtrude
	    Catch ex As Exception
	        MessageBox.Show("Error setting extrusion profiles: " & ex.Message, "iLogic Error")
	    End Try
	
	    ' Update the model
	    Try
	        oDoc.Update()
	    Catch ex As Exception
	        MessageBox.Show("Error updating the model: " & ex.Message, "iLogic Error")
	    End Try
	
	End Sub
End Class

 

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

Stakin
Collaborator
Collaborator
Accepted solution
Class MyRule
	Sub Main
		Dim Block_Positions_List As New List(Of Single)
		Block_Positions_List.Add(350)
		Block_Positions_List.Add(2500)
		Block_Positions_List.Add(-1500)
		Block_Positions_List.Add(-2000)
		Call UpdateExtrusion("Cutouts_Sketch", "Cutouts_Extrusion", Block_Positions_List)
	End Sub
	
	' Code to move blocks to specific Y-value removed
	' Extrude only Block_Positions_List.Count profiles
	Sub UpdateExtrusion(ByVal sketchName As String, ByVal extrusionName As String, ByVal Block_Positions_List As List(Of Single))
	
	    Dim oDoc As PartDocument = ThisApplication.ActiveDocument
	    Dim oPart As PartComponentDefinition = oDoc.ComponentDefinition
	    Dim oSketch As PlanarSketch
	    Dim oExtrusion As ExtrudeFeature
		oSketch = oPart.Sketches.Item(sketchName)
		oExtrusion=oPart.Features.ExtrudeFeatures(extrusionName)
	    Dim oSketchBlocks As SketchBlocks = oSketch.SketchBlocks
	    Dim numSketchBlocks As Integer = oSketchBlocks.Count
	    Dim numPositions As Integer = Block_Positions_List.Count
	Dim oProfile As Profile
    Dim oPathSegments As ObjectCollection
    oPathSegments = ThisApplication.TransientObjects.CreateObjectCollection
	    For i As Integer = 0 To numSketchBlocks - 1
	        Dim oBlock As SketchBlock = oSketchBlocks.Item(i + 1)
	        If i < numPositions Then
	            ' **debugging**
			    Dim oEntity As SketchEntity
			    For Each oEntity In oSketch.SketchEntities
			      If oEntity.Construction = True Then Continue For
				  If oEntity.Type = ObjectTypeEnum.kSketchPointObject Then Continue For
				If	oEntity.SketchBlockPath(1).Name=oBlock.Name Then
				  oPathSegments.Add(oEntity)
			  	End If 
			    Next
	        Else
	        End If
	    Next

	oProfile=oSketch.Profiles.AddForSolid(False,oPathSegments)

	    Try
	        oExtrusion.Definition.Profile = oProfile
	    Catch ex As Exception
	        MessageBox.Show("Error setting extrusion profiles: " & ex.Message, "iLogic Error")
	    End Try
	
	    ' Update the model
	    Try
	        oDoc.Update()
	    Catch ex As Exception
	        MessageBox.Show("Error updating the model: " & ex.Message, "iLogic Error")
	    End Try
	
	End Sub
End Class
Message 3 of 3

KenPalAlt
Contributor
Contributor

Many thanks, works great!

0 Likes